Convert symbolic expressions to Python functions using SymPy

后端 未结 1 1574
轮回少年
轮回少年 2021-01-06 00:09

I have a rather large symbolic function that is evaluated for different values of a parameter in a loop. In each iteration, after finding the expression of the function, par

1条回答
  •  失恋的感觉
    2021-01-06 00:48

    You want lambdify.

    f = lambdify(((x, y, z),), lagrange_eqs(a))
    

    will give you a Python function f that you can evaluate like f((1, 2, 3)) (for x=1, y=2, z=3). I have made the arguments in a tuple so that it will work with scipy's fsolve.

    You can set the modules flag to lambdify to determine where the exp function will come from. For instance, to use numpy, use lambdify((x, y, z), lagrange_eqs(a), modules="numpy"). To use the standard library math library, use modules="math". By default, numpy is used if it is installed, otherwise math is used.

    0 讨论(0)
提交回复
热议问题