[英]Sympy: Get functions from expression
要从sympy表达式中获取所有变量,可以在表达式上调用.free_symbols
。 我想检索表达式中使用的所有函数 。 例如,从y
in
from sympy import *
f = Function('f')
g = Function('g')
x = Symbol('x')
y = f(x) + 2*g(x)
我想得到f
和g
。
任何提示?
atoms
可以解决问题:
for f in y.atoms(Function):
print(f.func)
对于所有函数,使用atoms(Function)
。
In [40]: (f(x) + sin(x)).atoms(Function)
Out[40]: set([f(x), sin(x)])
对于仅未定义的函数,请使用atoms(AppliedUndef)
。
In [41]: from sympy.core.function import AppliedUndef
In [42]: (f(x) + sin(x)).atoms(AppliedUndef)
Out[42]: set([f(x)])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.