The lambdify function of Sympy can only convert our expression to the lambda function.
x, y = symbols('x y')
expr = x + 2*y
f = lambdify(x, expr)
But how to output the expression as a Python function? something like:
def expr(x, y):
return x + 2*y
Or is that possible to auto-generate a python script file expr.py
for our function expression using Sympy?
In short, need to achieve what matlabFunction
does in Matlab (Convert symbolic to matlab function)
Not quite sure if that is what you are looking for, but sympy has the octave (matlab) code printing option.
Here a quick example of what it does:
import sympy as sym
x,y = sym.symbols('x y')
expr = x + 2*y
expr2 = sym.sin(x) + sym.I*sym.exp(y)
sym.pprint(expr) #prints nice human readable output
sym.pprint(sym.printing.octave.octave_code(expr)) #prints octave (matlab) compatible output
sym.pprint(expr2)
sym.pprint(sym.printing.octave.octave_code(expr2))