我正在寻找一种稳健的方法来寻找一个sympy表达式中的函数参数。例如,在下面的示例中,如何检索 f 从 operator 是吗?
f
operator
import sympy x, y = sympy.symbols('x y') f = sympy.Function('f') operator = x*f(x, y).diff(x) + y*f(x, y) print(operator.free_symbols) # prints {x, y}
方法 expr.atoms(class) 返回中所有对象的集合 expr 这就是那个类的实例。所以,
expr.atoms(class)
expr
operator.atoms(sympy.Function)
会回来的 f(x, y) 是的。如果要忽略函数的参数,请使用
f(x, y)
[a.func for a in operator.atoms(sympy.Function)]
(对于同一物体 a , a.func 是它的“功能”部分 a.args 是它的“参数”部分。)
a
a.func
a.args
注意,如果 operator 包括,比如说, sin(x) ,这也将包含在上述代码中只返回 未定义 (非内置)函数,替换 sympy.Function 通过 sympy.function.AppliedUndef 作为中的类选择器 atoms .
sin(x)
sympy.Function
sympy.function.AppliedUndef
atoms