1
$\begingroup$

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?

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

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.

$\endgroup$
  • $\begingroup$ Thank you very much! Applying this solution I got a new question. $\endgroup$ – mcocdawc Jun 9 '15 at 12:27
  • $\begingroup$ 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? $\endgroup$ – mcocdawc Jun 9 '15 at 12:37
  • $\begingroup$ 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... $\endgroup$ – Jens Jun 9 '15 at 15:46
1
$\begingroup$

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]   *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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