Suppose I have an expression: $W = A_{i,p,q...}\,\, a_ia_ja_ka_l...$, where $A_{i,p,q...}$ is an expression that does not depend on $a$, the indexes $i,j,k,l,p,q...$ are integers and all the integers within $a_i$ the terms must be different. The number of the variables $a$ in the term can be arbitrary, but more that one.

I need to process those the terms with a function, that takes as arguments the coefficient $A_{i,p,q...}$ and a list of the indexes ${i,j,k,l...}$.

Thus I wrote:

f[A_ Subscript[a, n1_] Subscript[a, n2_] /; 
    FreeQ[A, a] && n1 != n2] := reduction[A, {n1, n2}];
f[A_ Subscript[a, n1_] Subscript[a, n2_] Subscript[a, n3_] /; 
    FreeQ[A, a] && n1 != n2 != n3] := 
  reduction[A, {n1, n2, n3}];
f[A_ Subscript[a, n1_] Subscript[a, n2_] Subscript[a, n3_] Subscript[
     a, n4_] /; FreeQ[A, a] && n1 != n2 != n3 != n4] := 
  reduction[A, {n1, n2, n3, n4}];
f[A_] := reductionError[A]

where reduction is one of my functions.

Everything works works for a term up to four variables, obviously, the question is, how to generalize it to an arbitrary number? I cannot use Coefficient, as I don't know the values of the subscripts.


One may think about a function like this:

f[A_ B_ /; FreeQ[A, a]] := 
  reduction [A, # /. Subscript[a, j_] :> j & /@ List @@ (B)];

However it fails on complex expressions:

f[(A + B) Subscript[a, 1] Subscript[a, 2] Subscript[a, 3]
   Subscript[a, 6]/8]


reduction[1/8, {A + B, 1, 2, 3, 6}]
  • $\begingroup$ Your edit invalidates everything you wrote so far! It's clear that as soon as you put anything else than subscripts in your call, it's not a multiplication of subscripts any more and your function-pattern does probably no longer match. $\endgroup$
    – halirutan
    Mar 18, 2015 at 3:53

1 Answer 1


As far as I can see, you don't have to do anything at all. Your pattern says a multiplication of subscripted a's that should not have the same subscript. Have you thought about happens if they have the same subscript?

test[x Subscript[a, 1] Subscript[a, 2] Subscript[a, 1]]//FullForm
(* List[1,3,4,5][Times[x,Power[Subscript[a,1],2],Subscript[a,2]]] *)

Note the Power in the output. Therefore, as soon as you have two identical subscripts in your call, it's not a multiplication of subscripts anymore and it is basically enougth to say (don't forget to ClearAll[f] before you do):

f[Times[A_, t : Subscript[a, _] ..] /; FreeQ[A, a]] := 
 With[{subs = {t}[[All, 2]]}, reduction[A, subs]]

The hardest part is to see that t is the sequence of subscripted variables and by putting {} around it you can turn it into a list and extract all subscripts.

Btw, using global variable a in this definition and using Subscript at all cries for trouble.

  • $\begingroup$ Yes, this works! Of course, I substituted the local variable to a here for simplicity of the code. Why the subscripts are evil? $\endgroup$
    – galadog
    Mar 18, 2015 at 4:00
  • $\begingroup$ Evaluate this Subscript[y, 1] = 3 and then try to clear the variable again. If you know how to do it, you are prepared to use Subscript. $\endgroup$
    – halirutan
    Mar 18, 2015 at 4:04
  • $\begingroup$ I have checked your expression on the most of my cases, and it matched perfectly always. The variables $a_i$ will never get a numerical value, so I on the safe side. $\endgroup$
    – galadog
    Mar 18, 2015 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.