def test_vector_scalar(x, y): df2d = np.atleast_2d(df(x)) dv = np.atleast_2d(np.zeros_like(y)).transpose() dgxy2d = g(forward.seed(x) * y).dvalue.toarray() check_general(x, lambda x: stack(f(x), y), csr_matrix(np.vstack( (df2d, dv)))) check_general(x, lambda x: stack(y, f(x)), csr_matrix(np.vstack( (dv, df2d)))) check_general(x, lambda x: stack(f(x), g(x * y)), csr_matrix(np.vstack((df2d, dgxy2d)))) check_general(x, lambda x: stack(g(x * y), f(x)), csr_matrix(np.vstack((dgxy2d, df2d))))
def test_simple(x, func, result): def f(x): return eval(func, globals(), dict(x=x)) return check_general(x, f, result)
def test_vector(x, func, mat): f = lambdify(func, dict(ns='sg')) check_general(x, f, mat)
def check_dot3(x, M): def g(x): return 5 * x**3 + 3 * x def f(x): return g(dot(M, x)) dg = g(forward.seed(dot(M, x))).dvalue check_general(x, f, dot(dg, M))
def check_dot2(x, M): def g(x): return 5 * x**3 + 3 * x dg = g(forward.seed(x)).dvalue def f(x): return dot(M, g(x)) check_general(x, f, dot(M, dg))
def check_dot1(x, M): def f(x): return dot(M, x) check_general(x, f, M)
def test_simple(x): check_general(x, lambda x: stack(f(x), g(x)), csr_matrix([[df(x)], [dg(x)]])) check_general(x, lambda x: stack(f(x), 0), csr_matrix([[df(x)], [0]])) check_general(x, lambda x: stack(0, f(x)), csr_matrix([[0], [df(x)]]))