Coefficients in multidimensional polynomials

I have a multidimensional polynomial depending on {x[1],x[2],x[3],x[4]}. If I want to selectively collect the coefficient corresponding to e.g. a*x[1]*x[2] I write:

 Coefficient[polynom,x[1]*x[2]]


This also gives the Coefficient of b*x[1]*x[2]x[3] as b*x[3]. I made a workaround to get only the coefficient corresponding to a*x[1]*x[2].

 Select[Coefficient[polynom,x[1]*x[2]],AtomQ]


Is there a better way?

• It would help if you included an example for polynom. Commented Jun 9, 2015 at 0:00

You could just use CoefficientList. Here is an example:

poly =
Sum[a[i, j, k] x[1]^i x[2]^j x[3]^k, {i, 0, 2}, {j, 0, 2}, {k, 0, 2}]

(*
==> a[0, 0, 0] + a[1, 0, 0] x[1] + a[2, 0, 0] x[1]^2 +
a[0, 1, 0] x[2] + a[1, 1, 0] x[1] x[2] + a[2, 1, 0] x[1]^2 x[2] +
a[0, 2, 0] x[2]^2 + a[1, 2, 0] x[1] x[2]^2 +
a[2, 2, 0] x[1]^2 x[2]^2 + a[0, 0, 1] x[3] + a[1, 0, 1] x[1] x[3] +
a[2, 0, 1] x[1]^2 x[3] + a[0, 1, 1] x[2] x[3] +
a[1, 1, 1] x[1] x[2] x[3] + a[2, 1, 1] x[1]^2 x[2] x[3] +
a[0, 2, 1] x[2]^2 x[3] + a[1, 2, 1] x[1] x[2]^2 x[3] +
a[2, 2, 1] x[1]^2 x[2]^2 x[3] + a[0, 0, 2] x[3]^2 +
a[1, 0, 2] x[1] x[3]^2 + a[2, 0, 2] x[1]^2 x[3]^2 +
a[0, 1, 2] x[2] x[3]^2 + a[1, 1, 2] x[1] x[2] x[3]^2 +
a[2, 1, 2] x[1]^2 x[2] x[3]^2 + a[0, 2, 2] x[2]^2 x[3]^2 +
a[1, 2, 2] x[1] x[2]^2 x[3]^2 + a[2, 2, 2] x[1]^2 x[2]^2 x[3]^2
*)

Extract[CoefficientList[poly, Array[x, {3}]], {2, 0, 1} + 1]

(* ==> a[2, 0, 1] *)


This shows that the coefficient of the powers $x[1]^2x[3]$ is a[2,0,1]. In the Extract command, the powers {2,0,1} are turned into indices by adding 1 to all of them.

This approach is especially suitable if you want to extract more than one coefficient, because you can construct the CoefficientList once and store it for future use.

• Thank you very much! Applying this solution I got a new question. Commented Jun 9, 2015 at 12:27
• According to this question: mathematica.stackexchange.com/questions/50675/… One can write Part[CoefficientList[poly, Array[x, {3}]], Sequence @@ ({2, 0, 1} + 1)] Now I am bothering with the general distinction between Extract and Part. Extract takes a list as argument for taking one element from an array, while Part needs several arguments and reads a list in one argument to give out e.g. several rows/columns. So in general Part is better for "slicing" arrays and can do everything what can be done with Extract. In this special example Extract is shorter? Commented Jun 9, 2015 at 12:37
• Yes, that's really a matter of taste, but I thought Extract looks more natural than Apply[Sequence,{2,0,1}+1] - I think people should use Extract more...
– Jens
Commented Jun 9, 2015 at 15:46

One another possibility might be this:

 a*x[1]*x[2] + b*x[1]*x[2]*x[3] /. x[n_] /; n != 1 && n != 2 -> 0

(*   a x[1] x[2]   *)