# Patterns of function-arguments in expressions

I want a function that compares an expression with a specific pattern. The expression has always the following form:

expr = F1[x1]F2[y1] + F1[x2]F2[y2] + F1[x3]F2[y3]


which is a sum of three terms, each term being the product ofF1[..]*F2[..] with arbitrary arguments.

Now what I need is a function that searches the arguments of the expression for a specific pattern.

As an example, I want the following pattern: x1! = x2, x1 == x3, y1 != y 2, y2 != y3.

v = F1[a]*F2[b] + F1[c]*F2[d] + F1[a]*F2[e]; IsMyPattern[v] (*True*)
w = F1[a]*F2[a] + F1[c]*F2[c] + F1[a]*F2[e]; IsMyPattern[w] (*True*)
x = F1[a]*F2[b] + F1[a]*F2[d] + F1[a]*F2[e]; IsMyPattern[x] (*False, because x1 == x2*)
y = F1[a]*F2[b] + F1[c]*F2[d] + F1[f]*F2[e]; IsMyPattern[y] (*False, because x1 != x3*)
z = F1[a]*F2[b] + F1[c]*F2[b] + F1[a]*F2[e]; IsMyPattern[z] (*False, because y1 != y2*)


The only idea I have is to use ToString and apply string manipulations, but thats ugly and most likely slow.

Update

A followup question on the same issue: How can I make the IsMyPattern function more general, such that it matches inputs which have (complex) coefficients and different signs?

k = 2*F1[a]*F2[b] + F1[c]*F2[d] + F1[a]*F2[e]; IsMyPattern[k] (*True*)
l = F1[a]*F2[b] - F1[c]*F2[d] - F1[a]*F2[e]; IsMyPattern[l] (*True*)
m = I*F1[a]*F2[b] + 3*F1[c]*F2[d] - F1[a]*F2[e]; IsMyPattern[m] (*True*)


Update2

The solution to this problem is remarkable simple, one just needs to add a BlankNullSequence. The function (for arbitrary number of arguments) looks like this:

 IsMyPattern[expr_] := MatchQ[expr, ___ F1[x1__]*F2[y1__] + ___ F1[x2__]*F2[y2__] + ___ F1[x3__]*F2[y3__] /; {x1} =!= {x2} && {x1} === {x3} && {y1} =!= {y2} && {y2} =!= {y3}]


IsMyPattern[expr_] :=
MatchQ[expr, F1[x1_]*F2[y1_] + F1[x2_]*F2[y2_] + F1[x3_]*F2[y3_] /;
(x1 =!= x2 && x1 === x3 && y1 =!= y2 &&  y2 =!= y3)]

Row[{
v = F1[a]*F2[b] + F1[c]*F2[d] + F1[a]*F2[e]; IsMyPattern[v] (*True*),
w = F1[a]*F2[a] + F1[c]*F2[c] + F1[a]*F2[e]; IsMyPattern[w] (*True*),
x = F1[a]*F2[b] + F1[a]*F2[d] + F1[a]*F2[e]; IsMyPattern[x] (*False,because x1=]x2*),
y = F1[a]*F2[b] + F1[c]*F2[d] + F1[f]*F2[e]; IsMyPattern[y] (*False,because x1≠x3*),
z = F1[a]*F2[b] + F1[c]*F2[b] + F1[a]*F2[e]; IsMyPattern[z] (*False,because y1≠y2*)}, "  "]

(*  True, True, False, False, False   *)

• Thanks alot, works perfectly. MatchQ, another instruction to be remembered :). Mar 9 '14 at 1:56
• @user3378652: Glad to hear. I highly recommend reading the various tutorials re: pattern matching, and doing a search for posts here - there are all sorts of subtleties (and options/controls) regarding just how precisely Mathematica considers things to "match". E.g., MatchQ[yy + xx, xx[_] + yy[_]] is True, since the forms are equivalent. If you needed them to be in the precise order, you need to structure your test accordingly.
– ciao
Mar 9 '14 at 2:04
• Thats a very good suggestions, I will defintivly go through posts here, and get more familiar with the Mathematica language style, plan it for the next week. I have posted a small follow-up question, maybe you know a quick solution for that aswell? THANKS! Mar 9 '14 at 4:43
• @user3378652: Saw update, this is really a case of read-try-learn: the best way to really understand pattern matching in Mathematica is to build some use cases and play around with possibilities. That will get you the "ah ha!" moments that reinforce what's going on under the covers. When you try things and get stuck, ask a question.
– ciao
Mar 9 '14 at 6:34
• Thanks for the advice, it was pretty simple, and its very useful to know the things I needed to read. If you have a favorite Pattern-Matching function, please give me the advice, otherwise i'm happy so far with what google showed me. Mar 10 '14 at 13:47