Sympy:从表达式中获取函数 [英] Sympy: Get functions from expression
本文介绍了Sympy:从表达式中获取函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
要从 sympy 表达式中获取所有变量,可以对表达式调用 .free_symbols
.我想检索表达式中使用的所有函数.例如,从 y
in
To get all variables from a sympy expression, one can call .free_symbols
on the expression. I would like to retrieve all functions used in an expression. For example, from y
in
from sympy import *
f = Function('f')
g = Function('g')
x = Symbol('x')
y = f(x) + 2*g(x)
我想要f
和g
.
有什么提示吗?
推荐答案
对于所有函数,使用 atoms(Function)
.
For all functions, use atoms(Function)
.
In [40]: (f(x) + sin(x)).atoms(Function)
Out[40]: set([f(x), sin(x)])
对于未定义的函数,使用atoms(AppliedUndef)
.
For only undefined functions, use atoms(AppliedUndef)
.
In [41]: from sympy.core.function import AppliedUndef
In [42]: (f(x) + sin(x)).atoms(AppliedUndef)
Out[42]: set([f(x)])
这篇关于Sympy:从表达式中获取函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文