解压 sympylambdaify 函数
下面的代码我有。
from sympy import *
x = symbols('x')
expr = sin(x)
# Use sympy.lambdify() method
f = lambdify(x, expr, "math")
如果 print(f) 给出 '
',有没有办法取回表达式(sin(x)) 来自 f?
Below code I have.
from sympy import *
x = symbols('x')
expr = sin(x)
# Use sympy.lambdify() method
f = lambdify(x, expr, "math")
If print(f)
its giving '<function _lambdifygenerated at 0x100d643a0>
', is there any way to get back the expression(sin(x)
) from f?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
help(f)
显示:f.__doc__
是相同的字符串。由于我们指定了“math”,标量可以工作,但数组不行:
help(f)
displays:f.__doc__
is the same string.Since we specified 'math', scalars work, but not arrays:
我进入了 Sympy 的源代码,发现了这一行,其中羔羊化函数的内容在变量
funcstr
中定义:https://github.com/sympy/sympy/blob/88b9cba9d7e633ec769baf1fc5267acfd7f54788/sympy/utilities/lambdify.py#L863
目前 Sympy 不返回
funcstr
,但我在本地安装中修改了它,这样它就可以了,而且似乎成为你所追求的。我获取内容的方式有点狡猾,而且不一定可靠,但我希望它能为您提供一个工作起点。
请注意,如果您使用参数
cse
,则此提取将不准确,因为它将错过之前的简化。I have gone into the source code of Sympy and found this line where the content of the lambdified function is defined in the variable
funcstr
:https://github.com/sympy/sympy/blob/88b9cba9d7e633ec769baf1fc5267acfd7f54788/sympy/utilities/lambdify.py#L863
Currently Sympy does not return
funcstr
, but I modified it in my local installation so that it does, and it seems to be what you are after.The way I got the content out is a bit dodgy, and not necessarily robust, but I hope it gives you a starting point to work with.
Beware that if you used the argument
cse
, this extraction will not be accurate, since it will miss out on the previous simplifications.