Sympy:从表达式中获取函数

5
要从sympy表达式中获取所有变量,可以在表达式上调用.free_symbols。我想检索表达式中使用的所有函数。例如,在以下代码中检索出y
from sympy import *

f = Function('f')
g = Function('g')

x = Symbol('x')

y = f(x) + 2*g(x)

我想获取 fg

有什么提示吗?

2个回答

7
`atoms` 能够解决这个问题:
for f in y.atoms(Function):
    print(f.func)

5

对于所有的函数,请使用 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)])

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接