def _get_array(): X, Y, Z = inp = array.array('d', [1, 2, 3]) args = x, y, z = se.symbols('x y z') exprs = [x+y+z, se.sin(x)*se.log(y)*se.exp(z)] ref = [X+Y+Z, math.sin(X)*math.log(Y)*math.exp(Z)] def check(arr): assert all([abs(x1-x2) < 1e-13 for x1, x2 in zip(ref, arr)]) return args, exprs, inp, check
def test_jacobian2(): import numpy as np x, y = se.symbols('x, y') args = se.Matrix(2, 1, [x, y]) v = se.Matrix(2, 1, [x**3 * y, (x+1)*(y+1)]) jac = v.jacobian(args) lmb = se.Lambdify(args, jac) out = np.empty((2, 2)) inp = X, Y = 7, 11 out = lmb(inp) assert np.atleast_1d(out).shape == (2, 2) assert np.allclose(out, [[3 * X**2 * Y, X**3], [Y + 1, X + 1]])
def _get_2_to_2by2_numpy(): import numpy as np args = x, y = se.symbols('x y') exprs = np.array([[x+y+1.0, x*y], [x/y, x**y]]) l = se.Lambdify(args, exprs) def check(A, inp): X, Y = inp assert abs(A[0, 0] - (X+Y+1.0)) < 1e-15 assert abs(A[0, 1] - (X*Y)) < 1e-15 assert abs(A[1, 0] - (X/Y)) < 1e-15 assert abs(A[1, 1] - (X**Y)) < 1e-13 return l, check
def _get_1_to_2by3_matrix(): x = se.symbols('x') args = x, exprs = se.Matrix(2, 3, [x+1, x+2, x+3, 1/x, 1/(x*x), 1/(x**3.0)]) l = se.Lambdify(args, exprs) def check(A, inp): X, = inp assert abs(A[0, 0] - (X+1)) < 1e-15 assert abs(A[0, 1] - (X+2)) < 1e-15 assert abs(A[0, 2] - (X+3)) < 1e-15 assert abs(A[1, 0] - (1/X)) < 1e-15 assert abs(A[1, 1] - (1/(X*X))) < 1e-15 assert abs(A[1, 2] - (1/(X**3.0))) < 1e-15 return l, check
def test_Lambdify(): n = 7 args = x, y, z = se.symbols('x y z') l = se.Lambdify(args, [x+y+z, x**2, (x-y)/z, x*y*z]) assert allclose(l(range(n, n+len(args))), [3*n+3, n**2, -1/(n+2), n*(n+1)*(n+2)])