[英]Sympy: Get functions from expression
To get all variables from a sympy expression, one can call .free_symbols
on the expression. 要从sympy表达式中获取所有变量,可以在表达式上调用
.free_symbols
。 I would like to retrieve all functions used in an expression. 我想检索表达式中使用的所有函数 。 For example, from
y
in 例如,从
y
in
from sympy import *
f = Function('f')
g = Function('g')
x = Symbol('x')
y = f(x) + 2*g(x)
I'd like to get f
and g
. 我想得到
f
和g
。
Any hints? 任何提示?
atoms
does the trick: atoms
可以解决问题:
for f in y.atoms(Function):
print(f.func)
For all functions, use atoms(Function)
. 对于所有函数,使用
atoms(Function)
。
In [40]: (f(x) + sin(x)).atoms(Function)
Out[40]: set([f(x), sin(x)])
For only undefined functions, use atoms(AppliedUndef)
. 对于仅未定义的函数,请使用
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.