def test_lambdify(): # Test lambdify with implemented functions # first test basic (sympy) lambdify f = sympy.cos yield assert_equal(lambdify(x, f(x))(0), 1) yield assert_equal(lambdify(x, 1 + f(x))(0), 2) yield assert_equal(lambdify((x, y), y + f(x))(0, 1), 2) # make an implemented function and test f = aliased_function("f", lambda x : x+100) yield assert_equal(lambdify(x, f(x))(0), 100) yield assert_equal(lambdify(x, 1 + f(x))(0), 101) yield assert_equal(lambdify((x, y), y + f(x))(0, 1), 101) # Error for functions with same name and different implementation f2 = aliased_function("f", lambda x : x+101) yield assert_raises(ValueError, lambdify, x, f(f2(x))) # our lambdify, like sympy's lambdify, can also handle tuples, # lists, dicts as expressions lam = lambdify(x, (f(x), x)) yield assert_equal(lam(3), (103, 3)) lam = lambdify(x, [f(x), x]) yield assert_equal(lam(3), [103, 3]) lam = lambdify(x, [f(x), (f(x), x)]) yield assert_equal(lam(3), [103, (103, 3)]) lam = lambdify(x, {f(x): x}) yield assert_equal(lam(3), {103: 3}) lam = lambdify(x, {f(x): x}) yield assert_equal(lam(3), {103: 3}) lam = lambdify(x, {x: f(x)}) yield assert_equal(lam(3), {3: 103})
def test_2d(): B1, B2 = [gen_BrownianMotion() for _ in range(2)] B1s = aliased_function("B1", B1) B2s = aliased_function("B2", B2) s, t = sympy.symbols('s', 't') e = B1s(s)+B2s(t) ee = lambdify((s,t), e) yield assert_almost_equal(ee(B1.x, B2.x), B1.y + B2.y)
def test_aliased_function(): # Here we check if the default returned functions are anonymous - in # the sense that we can have more than one function with the same name f = aliased_function('f', lambda x: 2*x) g = aliased_function('f', lambda x: np.sqrt(x)) l1 = lambdify(x, f(x)) l2 = lambdify(x, g(x)) yield assert_equal(str(f(x)), str(g(x))) yield assert_equal(l1(3), 6) yield assert_equal(l2(3), np.sqrt(3)) # check that we can pass in a sympy function as input func = sympy.Function('myfunc') yield assert_false(hasattr(func, 'alias')) f = aliased_function(func, lambda x: 2*x) yield assert_true(hasattr(func, 'alias'))
def test_1d(): B = gen_BrownianMotion() Bs = aliased_function("B", B) t = sympy.Symbol('t') expr = 3*sympy.exp(Bs(t)) + 4 expected = 3*np.exp(B.y)+4 ee_vec = lambdify(t, expr) yield assert_almost_equal(ee_vec(B.x), expected) # with any arbitrary symbol b = sympy.Symbol('b') expr = 3*sympy.exp(Bs(b)) + 4 ee_vec = lambdify(b, expr) yield assert_almost_equal(ee_vec(B.x), expected)
def test_vectorize(): theta = sympy.Symbol('theta') num_func = lambdify(theta, sympy.cos(theta)) yield assert_equal(num_func(0), 1) # we don't need to do anything for a naturally numpy'ed function yield assert_array_almost_equal(num_func([0, np.pi]), [1, -1]) # but we do for single valued functions func = aliased_function('f', lambda x: x**2) num_func = lambdify(theta, func(theta)) yield assert_equal(num_func(2), 4) # ** on a list raises a type error yield assert_raises(TypeError, num_func, [2, 3]) # so vectorize num_func = vectorize(func(theta), theta)
def test_1d(): B = gen_BrownianMotion() Bs = aliased_function("B", B) t = sympy.Symbol('t') def_t = sympy.DeferredVector('t') def_expr = 3*sympy.exp(Bs(def_t)) + 4 expected = 3*np.exp(B.y)+4 ee_lam = lambdify(def_t, def_expr) yield assert_almost_equal(ee_lam(B.x), expected) # use vectorize to do the deferred stuff expr = 3*sympy.exp(Bs(t)) + 4 ee_vec = vectorize(expr) yield assert_almost_equal(ee_vec(B.x), expected) # with any arbitrary symbol b = sympy.Symbol('b') expr = 3*sympy.exp(Bs(b)) + 4 ee_vec = vectorize(expr, b) yield assert_almost_equal(ee_vec(B.x), expected)