Reputation: 1332
I have a variety of SymPy expressions involving UndefinedFunction instances:
f = Function('f')
g = Function('g')
e = f(x) / g(x)
How can I obtain a list of the function invocations appearing in such expressions? In this example, I'd like to get [f(x), g(x)]
.
I'm aware of free_symbols
but it kicks back set([x])
(as it should).
Upvotes: 6
Views: 330
Reputation: 91580
You are right that you want to use atoms
, but be aware that all functions in SymPy subclass from Function
, not just undefined functions. So you'll also get
>>> (sin(x) + f(x)).atoms(Function)
set([f(x), sin(x)])
So you'll want to further reduce your list to only those functions that are UndefinedFunction
s. Note that UndefinedFunction
is the metaclass of f
, so do to this, you need something like
>>> [i for i in expr.atoms(Function) if isinstance(i.__class__, UndefinedFunction)]
[f(x)]
Upvotes: 3
Reputation: 1332
It turns out that the atoms
member method can accept a type on which to filter. So
e.atoms(Function)
returns
set([f(x), g(x)])
as I'd wanted. And that fancier things like
e.diff(x).atoms(Derivative)
are possible.
Upvotes: 1