Skip to content

Commit 8045d19

Browse files
committed
tweak lambdify
1 parent 394c307 commit 8045d19

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/lambdify.jl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,17 @@ function lambdify(ex::Sym, vars=free_symbols(ex);
219219
invoke_latest=true)
220220
if isempty(vars)
221221
# can't call N(ex) here...
222-
v = ex.evalf()
223-
if v.is_real == True
224-
val = convert(Real, v)
222+
flag = pygetattr(ex, "evalf", nothing)
223+
if isnothing(flag)
224+
val = pyconvert(Real, ex)
225225
else
226-
val = Complex(convert(Real, real(v)), convert(Real, imag(v)))
226+
v = ex.evalf()
227+
if pyconvert(Bool, v.is_real)
228+
val = pyconvert(Real, v)
229+
else
230+
a,b = pyconvert.(Real, (real(v), imag(v)))
231+
val = Complex(a, b)
232+
end
227233
end
228234
return (ts...) -> val
229235
end

test/tests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,13 @@ end
633633
@test u(.5) == 1
634634
@test u(1.5) == 0
635635

636+
# SymPy issue 567; constants
637+
u = lambdify(Sym(1//2))
638+
@test u() == u(1,2,3) == 1/2
639+
@syms x
640+
ex = integrate(sqrt(1 + (1/x)^2), (x, 1/sympy.E, sympy.E))
641+
@test lambdify(ex)() 3.1961985135995072
642+
636643
# i2 = SymPy.lambdify_expr(x^2,name=:square)
637644
# @test i2.head == :function
638645
# @test i2.args[1].args[1] == :square

0 commit comments

Comments
 (0)