# how to extract some terms out from an expression

I have an expression as below：

$-\frac{1}{2} A[a,v] A[b,h] A[c,v] A[d,h] \text{Cos}[\theta ]^2+\frac{1}{2} A[a,h] A[b,v] A[c,v] A[d,h] \text{Cos}[\theta ]^2+\frac{1}{2} A[a,v] A[b,h] A[c,h] A[d,v] \text{Cos}[\theta ]^2-\frac{1}{2} A[a,h] A[b,v] A[c,h] A[d,v] \text{Cos}[\theta ]^2+\frac{1}{2} A[a,v]^2 A[b,h] A[d,h] \text{Cos}[\theta ] \text{Sin}[\theta ]-\frac{1}{2} A[a,h] A[a,v] A[b,v] A[d,h] \text{Cos}[\theta ] \text{Sin}[\theta ]+\frac{1}{2} A[b,v] A[c,h] A[c,v] A[d,h] \text{Cos}[\theta ] \text{Sin}[\theta ]-\frac{1}{2} A[b,h] A[c,v]^2 A[d,h] \text{Cos}[\theta ] \text{Sin}[\theta ]-\frac{1}{2} A[a,h] A[a,v] A[b,h] A[d,v] \text{Cos}[\theta ] \text{Sin}[\theta ]+\frac{1}{2} A[a,h]^2 A[b,v] A[d,v] \text{Cos}[\theta ] \text{Sin}[\theta ]-\frac{1}{2} A[b,v] A[c,h]^2 A[d,v] \text{Cos}[\theta ] \text{Sin}[\theta ]+\frac{1}{2} A[b,h] A[c,h] A[c,v] A[d,v] \text{Cos}[\theta ] \text{Sin}[\theta ]-\frac{1}{2} A[a,v] A[b,v] A[c,h] A[d,h] \text{Sin}[\theta ]^2+\frac{1}{2} A[a,v] A[b,h] A[c,v] A[d,h] \text{Sin}[\theta ]^2+\frac{1}{2} A[a,h] A[b,v] A[c,h] A[d,v] \text{Sin}[\theta ]^2-\frac{1}{2} A[a,h] A[b,h] A[c,v] A[d,v] \text{Sin}[\theta ]^2$

There are 16 terms here and every term has for function $A$.

In some terms the first parameter of four function $A$ are different from each other( a,b,c,d), in the other terms the four first paramenter are partial overlap(a,a,b,d;...).

how can I get the first kind. (the result is as below:)

$-\frac{1}{2} A[a,v] A[b,h] A[c,v] A[d,h] \text{Cos}[\theta ]^2+\frac{1}{2} A[a,h] A[b,v] A[c,v] A[d,h] \text{Cos}[\theta ]^2+\frac{1}{2} A[a,v] A[b,h] A[c,h] A[d,v] \text{Cos}[\theta ]^2-\frac{1}{2} A[a,h] A[b,v] A[c,h] A[d,v] \text{Cos}[\theta ]^2-\frac{1}{2} A[a,v] A[b,v] A[c,h] A[d,h] \text{Sin}[\theta ]^2+\frac{1}{2} A[a,v] A[b,h] A[c,v] A[d,h] \text{Sin}[\theta ]^2+\frac{1}{2} A[a,h] A[b,v] A[c,h] A[d,v] \text{Sin}[\theta ]^2-\frac{1}{2} A[a,h] A[b,h] A[c,v] A[d,v] \text{Sin}[\theta ]^2$

this is the input form for our mma:

 -(1/2) A[a, v] A[b, h] A[c, v] A[d, h] Cos[\[Theta]]^2 +
1/2 A[a, h] A[b, v] A[c, v] A[d, h] Cos[\[Theta]]^2 +
1/2 A[a, v] A[b, h] A[c, h] A[d, v] Cos[\[Theta]]^2 -
1/2 A[a, h] A[b, v] A[c, h] A[d, v] Cos[\[Theta]]^2 +
1/2 A[a, v]^2 A[b, h] A[d, h] Cos[\[Theta]] Sin[\[Theta]] -
1/2 A[a, h] A[a, v] A[b, v] A[d, h] Cos[\[Theta]] Sin[\[Theta]] +
1/2 A[b, v] A[c, h] A[c, v] A[d, h] Cos[\[Theta]] Sin[\[Theta]] -
1/2 A[b, h] A[c, v]^2 A[d, h] Cos[\[Theta]] Sin[\[Theta]] -
1/2 A[a, h] A[a, v] A[b, h] A[d, v] Cos[\[Theta]] Sin[\[Theta]] +
1/2 A[a, h]^2 A[b, v] A[d, v] Cos[\[Theta]] Sin[\[Theta]] -
1/2 A[b, v] A[c, h]^2 A[d, v] Cos[\[Theta]] Sin[\[Theta]] +
1/2 A[b, h] A[c, h] A[c, v] A[d, v] Cos[\[Theta]] Sin[\[Theta]] -
1/2 A[a, v] A[b, v] A[c, h] A[d, h] Sin[\[Theta]]^2 +
1/2 A[a, v] A[b, h] A[c, v] A[d, h] Sin[\[Theta]]^2 +
1/2 A[a, h] A[b, v] A[c, h] A[d, v] Sin[\[Theta]]^2 -
1/2 A[a, h] A[b, h] A[c, v] A[d, v] Sin[\[Theta]]^2

• Could you please paste this expression in a copyable form (i.e. plain Mathematica InputForm)? While $\LaTeX$ looks pretty and more readable for people, in this case we want to use the expression in Mathematica more than read it. – Szabolcs Dec 11 '12 at 15:44

In principle, you are saying that A[var1_, var2_] behaves kind of like a gamma matrix w.r.t. the first variable. So, you may want to create a replacement rule:

Clear[exp];

exp = -(1/2) A[a, v] A[b, h] A[c, v] A[d, h] Cos[\[Theta]]^2 +
1/2 A[a, h] A[b, v] A[c, v] A[d, h] Cos[\[Theta]]^2 +
1/2 A[a, v] A[b, h] A[c, h] A[d, v] Cos[\[Theta]]^2 -
1/2 A[a, h] A[b, v] A[c, h] A[d, v] Cos[\[Theta]]^2 +
1/2 A[a, v]^2 A[b, h] A[d, h] Cos[\[Theta]] Sin[\[Theta]] -
1/2 A[a, h] A[a, v] A[b, v] A[d, h] Cos[\[Theta]] Sin[\[Theta]] +
1/2 A[b, v] A[c, h] A[c, v] A[d, h] Cos[\[Theta]] Sin[\[Theta]] -
1/2 A[b, h] A[c, v]^2 A[d, h] Cos[\[Theta]] Sin[\[Theta]] -
1/2 A[a, h] A[a, v] A[b, h] A[d, v] Cos[\[Theta]] Sin[\[Theta]] +
1/2 A[a, h]^2 A[b, v] A[d, v] Cos[\[Theta]] Sin[\[Theta]] -
1/2 A[b, v] A[c, h]^2 A[d, v] Cos[\[Theta]] Sin[\[Theta]] +
1/2 A[b, h] A[c, h] A[c, v] A[d, v] Cos[\[Theta]] Sin[\[Theta]] -
1/2 A[a, v] A[b, v] A[c, h] A[d, h] Sin[\[Theta]]^2 +
1/2 A[a, v] A[b, h] A[c, v] A[d, h] Sin[\[Theta]]^2 +
1/2 A[a, h] A[b, v] A[c, h] A[d, v] Sin[\[Theta]]^2 -
1/2 A[a, h] A[b, h] A[c, v] A[d, v] Sin[\[Theta]]^2;

exp /. A[var_, var2_] A[var_, var3_] -> 0 /. A[_, _]^2 -> 0


which gives the output you want.

• great! It is helpfull. – cmc Dec 11 '12 at 16:48
• I like this one better than my answer. Slick! – Mr.Wizard Dec 11 '12 at 17:07
• @Mr.Wizard :) thanks! – gpap Dec 12 '12 at 8:27

Filter your expression as follows:

expression // Cases[#, _ A[a, _] A[b, _] A[c, _] A[d, _]] & // Apply[Plus, #] &


Cases extracts all the terms in the sum that have the required structure and puts them in a list, then Apply converts the list back to a sum.

• it does work very well. I love it very much. Thanks a lot. – cmc Dec 11 '12 at 16:44

Another pattern based approach, with your expression assigned to expr:

Plus @@
Cases[
expr,
(Except[_A | _A^_] ..) x : _A .. /; UnsameQ @@ First /@ {x}
]