Converting symbolic function to numeric with SymPy

You can define your arguments explicitly to avoid this issue. You can see an example in the lambdify documentation:

julia> @doc lambdify
  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.

    β€’  ex::Sym a symbolic expression with 0, 1, or more free symbols

    β€’  vars a container of symbols to use for the function arguments. The default is free_symbols which has a specific
       ordering. Specifying vars allows this default ordering of arguments to be customized.

    β€’  fns::Dict, vals::Dict: Dictionaries that allow customization of the function that walks the expression ex and
       creates the corresponding AST for a Julia expression. See SymPy.fn_map and SymPy.val_map for the default
       mappings of sympy functions and values into Julia's AST.

    β€’  use_julia_code::Bool: use SymPy's conversion to an expression, the default is false

    β€’  invoke_latest=true: if true will call eval and Base.invokelatest to return a function that should not have any
       world age issue. If false will return a Julia expression that can be evaled to produce a function.

  Example:

  julia> using SymPy

  julia> @syms x y z
  (x, y, z)

  julia> ex = x^2 * sin(x)
   2
  x β‹…sin(x)

  julia> fn = lambdify(ex);

  julia> fn(pi)
  1.2086779438644711e-15

  julia> ex = x + 2y + 3z
  x + 2β‹…y + 3β‹…z

  julia> fn = lambdify(ex);

  julia> fn(1,2,3) # order is by free_symbols
  14

  julia> ex(x=>1, y=>2, z=>3)
  14

  julia> fn = lambdify(ex, (y,x,z));

  julia> fn(1,2,3)
  13
1 Like