I would like to write a function similar to Cases
, but can search for a group of related patterns together. For example,
casesList[f[x] g[x] + f[y] + g[y]/f[z], {f[a_], g[a_]}]
{{f[x], g[x]}, {f[y], g[y]}}
By "related patterns", I mean that in the above example a_
matches the same x
for both f
and g
at the first list, and matches y
at the second list.
Note that here the input {f[a_], g[a_]}
could be any other patterns. As another example,
caseList[D[f[x,y],x] + D[f[x,y],y] + D[g[x,y],x] + D[g[x,y],y],
{D[f_[x_,y_],x_], D[f_[x_,y_],y_]}]
{{Derivative[1, 0][f][x, y], Derivative[0, 1][f][x, y]}, {Derivative[1, 0][g][x, y], Derivative[0, 1][g][x, y]}}
Is there a simple way to do this? Thanks!
EDIT: To further clarify the question, I'd like to compare the situation with Cases for a list. For example,
Cases[{{f[a], f[b]}, {f[c], f[c]}}, {f[a_], f[a_]}, Infinity]
{{f[c], f[c]}}
Cases[{{f[c], f[c]}}, {f[a_], f[b_]}, Infinity]
{{f[c], f[c]}}
In the above two examples, Cases
does exactly what I want. However, more generally the expressions which match f[a_]
does not necessarily stays in a list structure, but rather may be at elsewhere in the expression. This is the major difficulty I met.
Alternatives[]
? $\endgroup$Alternatives
to do this. For example,Cases[f[x] g[x] + f[y] + g[y]/f[z], f[a_] | g[a_], Infinity]
gives{f[y], f[x], g[x], f[z], g[y]}
. This is not what I want, as I gave in the example output. The patterna_
inf[a_]
andg[a_]
are treated separately here. However I want them to match for the same thing in each sublist. $\endgroup$Alternatives
is not the direct solution to your problem. I still think that not everything is clear. E.g. the pattern list{f[a_],f[b_]}
will only return something when there are twof
in your expression with different argument, right? $\endgroup${f[a_],f[b_]}
is twof
's, with either the same or different arguments. The situation can be compared with ReplaceAll. For example{f[a],f[a]}/.{f[a_],f[b_]}->0
. Herea_
andb_
can stand for the same thing. (On the other hand,{f[a],f[b]}/.{f[a_],f[a_]}->0
will not trigger the replacement.) $\endgroup$