From ff52d9b0a65d1dbc652d33bd9e84b7251fc74422 Mon Sep 17 00:00:00 2001 From: john verzani Date: Mon, 8 Aug 2022 14:20:14 -0400 Subject: [PATCH] work around for issue #474 (#475) --- Project.toml | 2 +- src/lambdify.jl | 11 +++++++---- test/tests.jl | 5 +++++ 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/Project.toml b/Project.toml index 785e1ad..60af4dd 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "SymPy" uuid = "24249f21-da20-56a4-8eb1-6a02cf4ae2e6" -version = "1.1.6" +version = "1.1.7" [deps] CommonEq = "3709ef60-1bee-4518-9f2f-acd86f176c50" diff --git a/src/lambdify.jl b/src/lambdify.jl index 06a0d0d..f3a0a9f 100644 --- a/src/lambdify.jl +++ b/src/lambdify.jl @@ -41,13 +41,16 @@ __ALL__(xs...) = all(xs) __ZERO__(xs...) = 0 # not quite a match; NaN not θ(0) when evaluated at 0 w/o second argument __HEAVISIDE__ = (a...) -> (a[1] < 0 ? 0 : (a[1] > 0 ? 1 : (length(a) > 1 ? a[2] : NaN))) +__POW__(x, y::Int) = Base.literal_pow(^, x, Val(y)) # otherwise +__POW__(a,b) = (a)^(b) # __SYMPY__ALL__, fn_map = Dict( "Add" => :+, "Sub" => :-, "Mul" => :*, # :(SymPy.__PROD__) "Div" => :/, - "Pow" => :^, + # "Pow" => :^, + "Pow" => :(SymPy.__POW__), "re" => :real, "im" => :imag, "Abs" => :abs, @@ -115,8 +118,8 @@ function walk_expression(ex; values=Dict(), fns=Dict()) end """ - lambdify(ex, vars=free_symbols(); - fns=Dict(), values=Dict, use_julia_code=false, + lambdify(ex, vars=free_symbols(); + fns=Dict(), values=Dict, use_julia_code=false, invoke_latest=true) Take a symbolic expression and return a `Julia` function or expression to build a function. @@ -140,7 +143,7 @@ julia> @syms x y z (x, y, z) julia> ex = x^2 * sin(x) - 2 + 2 x ⋅sin(x) julia> fn = lambdify(ex); diff --git a/test/tests.jl b/test/tests.jl index bb46140..8424a9c 100644 --- a/test/tests.jl +++ b/test/tests.jl @@ -768,6 +768,11 @@ end @test lambdify(p)(2) == 2 @test lambdify(p)(-2) == (-2)^2 + ## Issue #474 with rational powers + @syms x + ex = 1/(2*x*sympy.pi^(1//4)) + @test N(ex(2)) ≈ lambdify(ex)(2) + ## Issue catch all for N; floats only @syms x ex = integrate(sqrt(1 + (1/x)^2), (x, 1/sympy.E, sympy.E))