Skip to content

Commit ee58c60

Browse files
committed
Fix two issues in LambdifyCSE
1. sympy doesn't sympify its inputs, so sympify before sending to sympy 2. subs can be empty and the code doesn't handle that
1 parent bb532cc commit ee58c60

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

symengine/lib/symengine_wrapper.pyx

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2845,17 +2845,22 @@ def LambdifyCSE(args, exprs, real=True, cse=None, concatenate=None):
28452845
from sympy import cse
28462846
if concatenate is None:
28472847
from numpy import concatenate
2848-
subs, new_exprs = cse(exprs)
2849-
cse_symbs, cse_exprs = zip(*subs)
2850-
lmb = Lambdify(tuple(args) + cse_symbs, new_exprs, real=real)
2851-
cse_lambda = Lambdify(args, cse_exprs, real=real)
2852-
2853-
def cb(inp, out=None, **kwargs):
2854-
cse_vals = cse_lambda(inp, **kwargs)
2855-
new_inp = concatenate((inp, cse_vals))
2856-
return lmb(new_inp, out, **kwargs)
2848+
from sympy import sympify as ssympify
2849+
subs, new_exprs = cse([ssympify(expr) for expr in exprs])
2850+
if subs:
2851+
cse_symbs, cse_exprs = zip(*subs)
2852+
lmb = Lambdify(tuple(args) + cse_symbs, new_exprs, real=real)
2853+
cse_lambda = Lambdify(args, cse_exprs, real=real)
2854+
2855+
def cb(inp, out=None, **kwargs):
2856+
cse_vals = cse_lambda(inp, **kwargs)
2857+
new_inp = concatenate((inp, cse_vals))
2858+
return lmb(new_inp, out, **kwargs)
2859+
2860+
return cb
2861+
else:
2862+
return Lambdify(args, exprs, real=real)
28572863

2858-
return cb
28592864

28602865
def has_symbol(obj, symbol=None):
28612866
cdef Basic b = sympify(obj)

0 commit comments

Comments
 (0)