def test_cse_gh174():
    x = se.symbols('x')
    funcs = [se.cos(x)**i for i in range(5)]
    f_lmb = se.Lambdify([x], funcs)
    f_cse = se.LambdifyCSE([x], funcs)
    a = np.array([1, 2, 3])
    assert np.allclose(f_lmb(a), f_cse(a))
def test_cse_numpy():
    if not HAVE_NUMPY:  # nosetests work-around
        return
    args, exprs, inp, ref = _get_cse_exprs()
    lmb = se.LambdifyCSE(args, exprs)
    out = lmb(inp)
    assert allclose(out, ref)
def test_cse_array_input():
    args, exprs, inp, ref = _get_cse_exprs()
    inp = array.array('d', inp)
    lmb = se.LambdifyCSE(
        args, exprs, concatenate=lambda tup: tup[0] + array.array('d', tup[1]))
    out = lmb(inp, use_numpy=False)
    assert allclose(out, ref)
def test_cse_list_input():
    args, exprs, inp, ref = _get_cse_exprs()
    lmb = se.LambdifyCSE(args,
                         exprs,
                         concatenate=lambda tup: tup[0] + list(tup[1]))
    out = lmb(inp, use_numpy=False)
    assert allclose(out, ref)
def test_Lambdify_scalar_vector_matrix_cse():
    _test_Lambdify_scalar_vector_matrix(
        lambda *args: se.LambdifyCSE(*args, backend='lambda'))
    if se.have_llvm:
        _test_Lambdify_scalar_vector_matrix(
            lambda *args: se.LambdifyCSE(*args, backend='llvm'))
def test_cse_big():
    args, exprs, inp = _get_cse_exprs_big()
    lmb = se.LambdifyCSE(args, exprs)
    out = lmb(inp)
    ref = [expr.xreplace(dict(zip(args, inp))) for expr in exprs]
    assert allclose(out, ref)
def test_cse():
    args, exprs, inp, ref = _get_cse_exprs()
    lmb = se.LambdifyCSE(args, exprs)
    out = lmb(inp)
    assert allclose(out, ref)
Exemple #8
0
    3 * p[3] - 2 * p[4] - 3 * p[6] - 6 * p[7] - 9 * p[8] - 12 * p[9] +
    exp(x[0]) + exp(x[1]) + 15 * exp(x[10]) + 2 * exp(x[11]) + 3 * exp(x[12]) +
    4 * exp(x[13]) + 4 * exp(x[2]) + 3 * exp(x[3]) + 2 * exp(x[4]) +
    3 * exp(x[6]) + 6 * exp(x[7]) + 9 * exp(x[8]) + 12 * exp(x[9]),
    -5 * p[10] - p[2] - p[3] - p[6] - 2 * p[7] - 3 * p[8] - 4 * p[9] +
    5 * exp(x[10]) + exp(x[2]) + exp(x[3]) + exp(x[6]) + 2 * exp(x[7]) +
    3 * exp(x[8]) + 4 * exp(x[9]), -p[1] - 2 * p[11] - 3 * p[12] - 4 * p[13] -
    p[4] + exp(x[1]) + 2 * exp(x[11]) + 3 * exp(x[12]) + 4 * exp(x[13]) +
    exp(x[4]), -p[10] - 2 * p[11] - p[12] - p[13] - p[5] - p[6] - p[7] - p[8] -
    p[9] + exp(x[10]) + 2 * exp(x[11]) + exp(x[12]) + exp(x[13]) + exp(x[5]) +
    exp(x[6]) + exp(x[7]) + exp(x[8]) + exp(x[9])
]

lmb_sp = sp.lambdify(args, exprs, modules='math')
lmb_se = se.Lambdify(args, exprs)
lmb_se_cse = se.LambdifyCSE(args, exprs)
lmb_se_llvm = se.Lambdify(args, exprs, backend='llvm')

inp = np.ones(28)

lmb_sp(*inp)
tim_sympy = clock()
for i in range(500):
    res_sympy = lmb_sp(*inp)
tim_sympy = clock() - tim_sympy

lmb_se(inp)
tim_se = clock()
res_se = np.empty(len(exprs))
for i in range(500):
    res_se = lmb_se(inp)