I have sums of the form
sum = a f[1,1,0] + b f[1,2,0] + c f[1,2,1] + ...
It is always linear in f[a,b,c]
. I would like a function that returns a list of all f[a,b,c]
appearing in the sum
. Following an answer from this question, I have created the following function, which behaves correctly when evaluated on sum
:
getVariables[expr_, f_] := DeleteDuplicates@Cases[expr,f, Infinity];
getVariables[sum, f[_,_,_]]
(*{f[1, 1, 0], f[1, 2, 0], f[1, 2, 1], ...}*)
The problem is that when there's a single function it does not work:
getVariables[f[a,b,c], f[_,_,_]]
(* {} *)
It only fails when it's exactly f[a,b,c]
and works fine for e.g. 2f[a,b,c]
.
What am I missing here? I do not get why it would fail in that case.
Infinity
(which is equivalent to{1, Infinity}
does not include level0
(i.e. the whole expression), which is why nothing is found inf[a,b,c]
. You can use{0, Infinity}
orAll
instead to also include level0
$\endgroup$ – Lukas Lang Oct 13 '19 at 15:19