I have an expression which consists of terms with undefined function calls a[n]
:
example = 1 - c^2 + c a[1] a[2] + 1/2 c^2 a[1]^2 a[2]^2 + c a[1] a[3]
Now I want to transform each term with individual a
s to a different function v[m1,m2,m3]
, such that v[[m_i]]
is the exponent of a[[i]]
. In addition, I want that the coefficient of v[[m_i]]
is multiplied by the (exponent of a[[i]]
+ 1):
fct[example]
(* example -> (1 - c^2)*(0+1)*(0+1)*(0+1)*v[0,0,0] + c*(1+1)*(1+1)*(0+1)*v[1,1,0] +
+ 1/2 c^2*(2+1)*(2+1)*(0+1)*v[2,2,0] + c*(1+1)*(0+1)*(1+1)*v[1,0,1]
= (1 - c^2)*v[0,0,0] + c*4*v[1,1,0] +
+ 1/2 c^2*9*v[2,2,0] + c*4*v[1,0,1]
*)
I found one solution, but it is annoyingly slow, and I was hoping that somebody finds faster methods. The idea is to multiply example
with a[1]^2 * a[2]^2 * a[3]^2
(such that every term has the form of coeff * a[1]^n1*a[2]^n2*a[3]^n3
), then use a Replace
-Rule:
fct[expr_] :=
Expand[expr*a[1]^2*a[2]^2*a[3]^2] /.
{a[1]^n1_*a[2]^n2_*a[3]^n3_ -> (n1-1)*(n2-1)*(n3-1)*v[n1-2, n2-2, n3-2]}
fct[example] (* v[0, 0, 0] - c^2 v[0, 0, 0] + 4 c v[1, 0, 1] + 4 c v[1, 1, 0] +
9/2 c^2 v[2, 2, 0] *)
My specific questions:
How can I make the algorithm general for arbitrary
a[n]
with n>3? (For the way I did it here, I dont know how to access the exponents more general)How can I make the algorithm faster? (It takes already ~5sec for n=8, and roughly 80 terms of
a[1]^n1*a[2]^n2*a[3]^n3
inexample
.)