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:
ClearAll[f];
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.
edit:
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]
returns:
reduction[1/8, {A + B, 1, 2, 3, 6}]