def integrate(integral, beam_type, a, m=None, t=None, v=None, n=None, decimal_precision=DEFAULT_DECIMAL_PRECISION, **kwargs): cached_subs = integral(beam_type, m, t, v, n, decimal_precision).subs('a', a) f = lambda y: cached_subs.evalf(n=decimal_precision, subs={'y': y}) with mpmath.workdps(decimal_precision): result = mpmath.quad(f, (0., a), **kwargs) # If not converted to `sympy.Float` precision will be lost after the # original `mpmath` context is restored if isinstance(result, tuple): # Integration error included return tuple(Float(x, decimal_precision) for x in result) else: return Float(result, decimal_precision)
def count_roots(p): """ Counts the number of roots of the polynomial object 'p' on the interior of the unit ball using an integral. """ return mp.quad(lambda z: mp.exp(1.0j * z) * p(mp.exp(1.0j * z)), [0., 2 * np.pi]) / (2 * np.pi)
def contour_int(f, c, t0, t1): """ Evaluate the integral of the function 'f' parameterized by the function 'c' with initial and final parameter values 't0' and 't1'. """ return complex(mp.quad(lambda t: f(c(t)) * mp.diff(c, t), (t0, t1)))
print diff assert diff < 1E-10 print 'OK' # print ref_element.riesz_basis # print phys_element.riesz_basis # F : x_hat --> x = a(1-x)/2 + b(1+x)/2 Finv = (2*x - b - a)/(b-a) J = (b-a)/2. mapped_riesz = [f.subs(x, Finv)/J for f in ref_element.riesz_basis] # print mapped_riesz for rmap, r in zip(mapped_riesz, phys_element.riesz_basis): print '>', quad(lambdify(x, (rmap-r)**2), [a, b])**0.5 mapped_basis = [f.subs(x, Finv) for f in ref_element.sym_basis] # print mapped_basis for rmap, r in zip(mapped_basis, phys_element.sym_basis): print '>>', quad(lambdify(x, (rmap-r)**2), [a, b])**0.5 coef = np.random.rand(degree+1) f = sum(c*x**i for i, c in enumerate(coef)) f_lambda = lambdify(x, f) for element, cell in [(ref_element, (-1, 1)), (phys_element, (a, b))]: for i in range(dim): print element.eval_dof(j, f_lambda),\ element.eval_dof_riesz(j, f),\ ref_element.eval_dof_riesz_cell(j, f, cell)
from sympy import * from sympy.mpmath import e, quad x = Symbol("x") f = 1 / (1.28 - e ** (-x - 0.01)) f_im = lambda t: f.subs(x, t) seg = [0, 0.4] js = quad(f_im, seg) def rect_integral(func, seg, n): h = (seg[1] - seg[0]) / n intgr = 0 for k in range(1, n + 1): intgr += h * func.subs(x, seg[0] + (2 * k - 1) * h / 2) fdiff = func.diff(x, 2) fdiffm = max(fdiff.subs(x, seg[0]), fdiff.subs(x, seg[1])) error = fdiffm * (seg[1] - seg[0]) ** 3 / (24 * n ** 2) return intgr, error def trap_integral(func, seg, n): h = (seg[1] - seg[0]) / n intgr = h / 2 * (func.subs(x, seg[0]) + func.subs(x, seg[1]))
dx = VolumeMeasure(Rectangle([0, 1], [1, 3])) # print f*dx A = [0, 0, 0] B = [1, 0, 0] C = [0, 1, 0] D = [0, 0, 1] dx = VolumeMeasure(Tetrahedron(A, B, C, D)) # print 1*dx # print 1*dV(A, B, C, D) f = x + y + z print f*dV([[0, 3], [0, 2], [0, 1]]) f = x + y print f*dV([[0, 3], [0, 2]]) f = x print f*dV([[1, 10]]) dW = dV([[1, 10]]) + dV([[2, 3]]) print f*dV([[1, 10]]) + f*dV([[2, 3]]), f*dW f = x + y + z print quad(lambdify([x, y, z], f), [0, 3], [0, 2], [0, 1]) f = x + y print quad(lambdify([x, y], f), [0, 3], [0, 2]) f = x print quad(lambdify([x], f), [1, 10]) # print 1*VolumeMeasure(Interval(1, 10)) # print quad(lambdify(x, S(1)), [1, 10])
def test_C(basis, C): '''Test gradient matrix of the basis.''' # The linear form is the Trial`*Test integrated over (-1, 1) a_form = lambda u, v: quad(lambdify(x, u.diff(x, 1)*v), [-1, 1]) return test_matrix(basis, C, a_form)
def test_A(basis, A): '''Test stiffness matrix of the basis.''' # The linear form is the H^1_0 inner product over (-1, 1) a_form = lambda u, v: quad(lambdify(x, u.diff(x, 1)*v.diff(x, 1)), [-1, 1]) return test_matrix(basis, A, a_form)
def test_M(basis, M): '''Test mass matrix of the basis.''' # The linear form is the L^2 inner product over (-1, 1) a_form = lambda u, v: quad(lambdify(x, u*v), [-1, 1]) return test_matrix(basis, M, a_form)