def test_step_function(): # test step function # step function is a function of t s = step_function([0,4,5],[2,4,6]) tval = np.array([-0.1,0,3.9,4,4.1,5.1]) lam = lambdify(t, s) yield assert_array_equal(lam(tval), [0, 2, 2, 4, 4, 6]) s = step_function([0,4,5],[4,2,1]) lam = lambdify(t, s) yield assert_array_equal(lam(tval), [0, 4, 4, 2, 2, 1])
def test_alias2(): f = F.aliased_function('f', lambda x: 2*x) g = F.aliased_function('f', lambda x: np.sqrt(x)) x = sympy.Symbol('x') l1 = aliased.lambdify(x, f(x)) l2 = aliased.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)
def test_blocks(): on_off = [[1,2],[3,4]] tval = np.array([0.4,1.4,2.4,3.4]) b = blocks(on_off) lam = lambdify(t, b) yield assert_array_equal(lam(tval), [0, 1, 0, 1]) b = blocks(on_off, amplitudes=[3,5]) lam = lambdify(t, b) yield assert_array_equal(lam(tval), [0, 3, 0, 5])
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_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_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_define(): t = F.Term('t') expr = sympy.exp(3*t) yield assert_equal, str(expr), 'exp(3*t)' newf = F.define('f', expr) yield assert_equal, str(newf), 'f(t)' f = aliased.lambdify(t, newf) tval = np.random.standard_normal((3,)) yield assert_almost_equal, np.exp(3*tval), f(tval)
def test_interp(): times = [0,4,5.] values = [2.,4,6] for int_func in (interp, linear_interp): s = int_func(times, values, bounds_error=False) tval = np.array([-0.1,0.1,3.9,4.1,5.1]) res = lambdify(t, s)(tval) yield assert_array_equal(np.isnan(res), [True, False, False, False, True]) yield assert_array_almost_equal(res[1:-1], [2.05, 3.95, 4.2]) # specifying kind as linear is OK s = linear_interp(times, values, kind='linear')
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)
def test_convolve_functions(): # replicate convolution # This is a square wave on [0,1] f1 = (t > 0) * (t < 1) # ff1 is the numerical implementation of same lam_f1 = lambdify(t, f1) ff1 = lambda x : lam_f1(x).astype(np.int) # The convolution of ``f1`` with itself is a triangular wave on # [0,2], peaking at 1 with height 1 tri = convolve_functions(f1, f1, [0,2], 1.0e-3, name='conv') yield assert_equal(str(tri), 'conv(t)') ftri = lambdify(t, tri) time, value = numerical_convolve(ff1, ff1, [0, 2], 1.0e-3) y = ftri(time) # numerical convolve about the same as ours yield assert_array_almost_equal(value, y) # peak is at 1 yield assert_array_almost_equal(time[np.argmax(y)], 1) # Flip the interval and get the same result tri = convolve_functions(f1, f1, [2, 0], 1.0e-3) ftri = lambdify(t, tri) y = ftri(time) yield assert_array_almost_equal(value, y) # offset square wave by 1 f2 = (t > 1) * (t < 2) tri = convolve_functions(f1, f2, [0,3], 1.0e-3) ftri = lambdify(t, tri) y = ftri(time) yield assert_array_almost_equal(time[np.argmax(y)], 2) # offset both by 1 and start interval at one tri = convolve_functions(f2, f2, [1,3], 1.0e-3) ftri = lambdify(t, tri) # get numerical version lam_f2 = lambdify(t, f2) ff2 = lambda x : lam_f2(x).astype(np.int) time, value = numerical_convolve(ff2, ff2, [1, 3], 1.0e-3) # and our version, compare y = ftri(time) yield assert_array_almost_equal(y, value)
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})