JuliaPy / SymPy.jl

Julia interface to SymPy via PyCall
http://juliapy.github.io/SymPy.jl/
MIT License
266 stars 61 forks source link

`lambdify(..., use_julia_code=true, invoke_latest=false)` fails with powers and fractions #524

Closed jlbosse closed 10 months ago

jlbosse commented 10 months ago

Lambdifying expressions that involve fractions right after powers fails due to missing spaces in the sympy expression. See the following MWE:

using SymPy
a, b = symbols("a b")
expr = a * sin(a)^2 / (2*b)
f_expr = lambdify(expr, use_julia_code=true, invoke_latest=false)

fails with the error message

ERROR: Base.Meta.ParseError("invalid syntax \"2./\"; add space(s) to clarify")
Stacktrace:
 [1] #parse#3
   @ ./meta.jl:236 [inlined]
 [2] parse
   @ ./meta.jl:232 [inlined]
 [3] parse(str::String; raise::Bool, depwarn::Bool)
   @ Base.Meta ./meta.jl:267
 [4] parse(str::String)
   @ Base.Meta ./meta.jl:266
 [5] convert_expr(ex::Sym; fns::Dict{Any, Any}, values::Dict{Any, Any}, use_julia_code::Bool)
   @ SymPy ~/.julia/packages/SymPy/VAz58/src/lambdify.jl:265
jverzani commented 10 months ago

Yes,

I don't have too much control over that. When called with use_julia_code=true we just get this back:

julia> expr = a * sin(a)^2 / (2*b)
     2   
a⋅sin (a)
─────────
   2⋅b   

julia> sympy.julia_code(expr)
"a.*sin(a).^2./(2*b)"

You can see the lack of spaces produced by sympy. That issue sits with the Python package. If you do not pass in use_julia_code=true, then the code path chosen is basically this:

julia> convert(Expr, expr)
:((1 / 2) * a * b ^ -1 * sin(a) ^ 2)

Which should work with lambdify assuming you know the order of the arguments.

jlbosse commented 10 months ago

Thanks, I guess then there is nothing that can be done (short of opening an issue on the SymPy project) here.

For future users running into this: I am using the approach shown in the documentation in the example block after "More performant functions can be produced using the following patter" together with RuntimeGeneratedFunctions.jl to produce fast julia functions from the results of a sympy calculation.