I'm trying to do some math with sympy, but I don't seem to figure out how to support division:
from sympy import Symbol, lambdify
a = Symbol('a')
b = Symbol('b')
c = a * b
lambdified = lambdify(c.args, c)
value = lambdified(a=2, b=3)
print(value)
x = Symbol('x')
y = Symbol('y')
z = x / y
lambdified_2 = lambdify(z.args, z)
value_2 = lambdified_2(x=2, y=3)
print(value_2)
This returns:
def _lambdifygenerated(x, 1/y):
^
SyntaxError: invalid syntax
I tried to fix this by passing free_symbols
instead, this works but raises the error:
x = Symbol('x')
y = Symbol('y')
z = x / y
lambdified_2 = lambdify(z.free_symbols, z)
value_2 = lambdified_2(x=2, y=3)
print(value_2)
returning
SymPyDeprecationWarning:
Passing the function arguments to lambdify() as a set is deprecated. This
leads to unpredictable results since sets are unordered. Instead, use a list
or tuple for the function arguments.
Now, I could fix this by casting z.free_symbols
to a tuple, but I'm sure there's a better way of doing this. How should I pass the arguments?
To be clear, I don't want to pass [x,y]
in the args because I want to only have to pass the equation later on.