1
$\begingroup$

We have polynomial in three variables x, y, z.

How to list all coefficients of odd powers of z or y and any power of x?

Red are coefficients of z y^(2n) or z^3 y^(2n).

Green are coefficients of y z^(2n) or y^3 z^(2n).

Yellow are coefficients of z^3 y or z y^3 or z y (or z^3 y^3 though there are not any).

Collect[(a1 x + a2 y + a3 z)^4 + (b1 x + b2 y + b3 z)^4 + (c1 x + 
    c2 y + c3 z)^4, {z, y, x}, Factor]

enter image description here

I can do it by selecting parts of CoefficientList and the parts defined manually.

I can automatize creation of the list of parts but the code would be cumbersome and hard to read.

I would prefer a code that it would be evident what the code is doing at the first glance.

From my code it is not evident that it selects coefficients of odd powers of z, y.

DeleteCases[
  CoefficientList[(a1 x + a2 y + a3 z)^4 + (b1 x + b2 y + 
          b3 z)^4 + (c1 x + c2 y + c3 z)^4, {z, y, 
        x}][[Sequence @@ #]] & /@ {{1, 2}, {1, 4}, {2, 1}, {2, 2}, {2,
       3}, {2, 4}, {3, 2}, {3, 4}, {4, 1}, {4, 2}, {4, 3}, {4, 4}} // 
   Flatten, 0] // Factor

(* {4 (a1^3 a2 + b1^3 b2 + c1^3 c2), 
 4 (a1 a2^3 + b1 b2^3 + c1 c2^3), 4 (a1^3 a3 + b1^3 b3 + c1^3 c3), 
 12 (a1^2 a2 a3 + b1^2 b2 b3 + c1^2 c2 c3), 
 12 (a1 a2^2 a3 + b1 b2^2 b3 + c1 c2^2 c3), 
 4 (a2^3 a3 + b2^3 b3 + c2^3 c3), 
 12 (a1 a2 a3^2 + b1 b2 b3^2 + c1 c2 c3^2), 
 4 (a1 a3^3 + b1 b3^3 + c1 c3^3), 4 (a2 a3^3 + b2 b3^3 + c2 c3^3)} *)
$\endgroup$

3 Answers 3

2
$\begingroup$

I found solution with CoefficientRules. The code is easy to read since we immediately see selection is done by OddQ on the first two ({1,2}) variables, i.e. z, y.

CoefficientRules[(a1 x + a2 y + a3 z)^4 + (b1 x + b2 y + 
     b3 z)^4 + (c1 x + c2 y + c3 z)^4, {z, y, x}];
Select[%, Or @@ OddQ@#[[1, {1, 2}]] &][[All, 2]]
$\endgroup$
2
  • $\begingroup$ +1 Do you not need to keep the associated powers of y and z to know what is what? $\endgroup$
    – JimB
    Commented Sep 24, 2023 at 19:26
  • $\begingroup$ No, coefficients can be in any order, but with CoefficientRules you can keep also associations if needed. $\endgroup$ Commented Sep 24, 2023 at 19:57
2
$\begingroup$

We can create a function called SelectMonomials with the option to retrieve the coefficients:

SelectMonomials[pol_, vars_?VectorQ, cond_, 
type : (Automatic | "ToCoefficientList") : Automatic] /; 
PolynomialQ[pol, vars] := 
Switch[type, Automatic, Select[MonomialList[pol // Expand, vars], cond], 
"ToCoefficientList", ReplaceAll[Thread[vars -> 1]][
Select[MonomialList[pol // Expand, vars], cond]]]

Testing SelectMonomials:

pol = (a1 x + a2 y + a3 z)^4 + (b1 x + b2 y + b3 z)^4 + (c1 x + c2 y + c3 z)^4;

SelectMonomials[pol, {x, y, z}, OddQ[Exponent[#, y]] || OddQ[Exponent[#, z]] &]

enter image description here

Using the optional argument "ToCoefficientList":

SelectMonomials[pol, {x, y, z}, OddQ[Exponent[#, y]] || OddQ[Exponent[#, z]] &, "ToCoefficientList"]

enter image description here

I hope you will find it useful!

$\endgroup$
1
$\begingroup$

One way would be to first get rid of the terms that you do not need:

lstZ = z^(2 #) -> 0 & /@ Range[2]
lstY = y^(2 #) -> 0 & /@ Range[2]

(*  {z^2 -> 0, z^4 -> 0}

{y^2 -> 0, y^4 -> 0}  *)

poly2 = (poly // Expand) /. lstZ /. lstY

(* a1^4 x^4 + b1^4 x^4 + c1^4 x^4 + 4 a1^3 a2 x^3 y + 4 b1^3 b2 x^3 y + 
 4 c1^3 c2 x^3 y + 4 a1 a2^3 x y^3 + 4 b1 b2^3 x y^3 + 
 4 c1 c2^3 x y^3 + 4 a1^3 a3 x^3 z + 4 b1^3 b3 x^3 z + 
 4 c1^3 c3 x^3 z + 12 a1^2 a2 a3 x^2 y z + 12 b1^2 b2 b3 x^2 y z + 
 12 c1^2 c2 c3 x^2 y z + 4 a2^3 a3 y^3 z + 4 b2^3 b3 y^3 z + 
 4 c2^3 c3 y^3 z + 4 a1 a3^3 x z^3 + 4 b1 b3^3 x z^3 + 
 4 c1 c3^3 x z^3 + 4 a2 a3^3 y z^3 + 4 b2 b3^3 y z^3 + 4 c2 c3^3 y z^3  *)

Now:

lst1 = Table[y^(2 n + 1), {n, 0, 2}];
lst2 = Table[z^(2 n + 1), {n, 0, 2}];
lst3 = {y*z, y*z^2, y*z^3, y^2*z, y^3*z};

and

Coefficient[poly2, #] & /@ lst2 /. y -> 0

(*  {4 a1^3 a3 x^3 + 4 b1^3 b3 x^3 + 4 c1^3 c3 x^3, 
 4 a1 a3^3 x + 4 b1 b3^3 x + 4 c1 c3^3 x, 0}  *)

Coefficient[poly2, #] & /@ lst1 /. z -> 0

(* {4 a1^3 a2 x^3 + 4 b1^3 b2 x^3 + 4 c1^3 c2 x^3, 
 4 a1 a2^3 x + 4 b1 b2^3 x + 4 c1 c2^3 x, 0}  *)

Coefficient[poly2, #] & /@ lst3

(*  {12 a1^2 a2 a3 x^2 + 12 b1^2 b2 b3 x^2 + 12 c1^2 c2 c3 x^2, 0, 
 4 a2 a3^3 + 4 b2 b3^3 + 4 c2 c3^3, 0, 
 4 a2^3 a3 + 4 b2^3 b3 + 4 c2^3 c3}  *)

Hope I understood you right, and this is what you need.

Have fun!

$\endgroup$
2
  • $\begingroup$ Thanks, I found a solution, see my answer. There are 9 coefficients that fulfill conditions, it seems you missed one. And from the code it is not evident what it is doing. $\endgroup$ Commented Sep 24, 2023 at 15:10
  • 2
    $\begingroup$ @azerbajdzan My task is only to point a direction of solving the problem. Details and subtleties are already up to you. After all it is your problem, not mine. $\endgroup$ Commented Sep 24, 2023 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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