Coefficients in multidimensional polynomials

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

Coefficient[polynom,x*x]

This also gives the Coefficient of b*x*xx as b*x. I made a workaround to get only the coefficient corresponding to a*x*x.

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

Is there a better way?

• It would help if you included an example for polynom. – Mr.Wizard Jun 9 '15 at 0:00

You could just use CoefficientList. Here is an example:

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

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

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

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

This shows that the coefficient of the powers $x^2x$ 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. – mcocdawc Jun 9 '15 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? – mcocdawc Jun 9 '15 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 Jun 9 '15 at 15:46

One another possibility might be this:

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

(*   a x x   *)