Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jlbosse opened this issue Sep 25, 2023 · 2 comments

Comments

@jlbosse
Copy link

jlbosse commented Sep 25, 2023

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
Copy link
Collaborator

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
Copy link
Author

jlbosse commented Sep 25, 2023

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.

@jlbosse jlbosse closed this as completed Sep 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants