I am wondering how to find the symbols connected with Functions in a sympy expression. I am aware of .free_symbols
, .atoms(Function)
as well as .atoms(AppliedUndef)
. Here is some code to show why none of these does what I need.
f1 = Function(r'f_1')
f2 = Function(r'f_2')
c1, x = symbols(r'c_1, x')
expr = c1+f1(x)+f2(x)
print(expr)
# c_1 + f_1(x) + f_2(x)
print(expr.free_symbols)
# {x, c_1}
print(expr.atoms(Function))
# {f_1(x), f_2(x)}
from sympy.core.function import AppliedUndef
print(expr.atoms(AppliedUndef))
# {f_1(x), f_2(x)}
(The comments are the outputs of each print line). So .free_symbols
is nice, it gives me c_1
and x
. However, it does not return the symbols associated with the functions f_1
and f_2
. (First question: why? Are they not free somehow?). .atoms(Function)
does not help either. It finds the functions, but does not return their associated symbols (e.g. f_1
), it returns the whole function call (e.g. f_1(x)
).
Main question: How do I find the symbols f_1
and f_2
in above expression?
Background: The reason I want this, is because I would like to lambdify
in the following way
expr_num = lambdify([c1, f1, f2, x], expr)
but instead of giving the argument [c1, f1, f2, x]
manually, I would like to find all necessary symbols in the expression.