5
$\begingroup$

I have a list of polynomials. Each polynomial looks like:

p[1, 2, 5] p[3, 6, 9] p[4, 7, 8] - 3 p[1, 2, 4] p[3, 6, 9] p[5, 7, 8]

How to convert each of these polynomials into a list. For example, I would like the above become:

{{1, {1, 2, 5}, {3, 6, 9}, {4, 7, 8}}, {-3, {1, 2, 4}, {3, 6, 9}, {5, 7, 8}}}

Thank you very much!

$\endgroup$
5
  • 2
    $\begingroup$ Lists must use list brackets. See The Four Kinds of Bracketing in the Wolfram Language $\endgroup$
    – Bob Hanlon
    Commented Mar 30 at 2:07
  • 1
    $\begingroup$ Apply[List, expr, {0, 2}] $\endgroup$
    – I.M.
    Commented Mar 30 at 8:45
  • $\begingroup$ ...or even Apply[List, X, {0, ∞}] to be more general $\endgroup$
    – Roman
    Commented Mar 30 at 10:08
  • $\begingroup$ @I.M. This doesn't give the coefficient 1 for the first term though $\endgroup$
    – MelaGo
    Commented Mar 30 at 22:38
  • $\begingroup$ @MelaGo, right, perhaps having 1 might be redundant like mult by 1, up to OP thou $\endgroup$
    – I.M.
    Commented Mar 31 at 9:23

4 Answers 4

4
$\begingroup$

A variant of E. Chan-López answer

pol = p[1, 2, 5] p[3, 6, 9] p[4, 7, 8] - 3 p[1, 2, 4] p[3, 6, 9] p[5, 7, 8];

ReplaceAll[p :> List] @ Query[All, All, 1][FactorList /@ List @@ pol]

{{1, {1, 2, 5}, {3, 6, 9}, {4, 7, 8}}, {-3, {1, 2, 4}, {3, 6, 9}, {5, 7, 8}}}

Or, as Roman commented:

(FactorList /@ List @@ pol)[[All, All, 1]] /. p -> List

same result

$\endgroup$
2
  • $\begingroup$ You can write this simpler as (FactorList /@ List @@ X)[[All, All, 1]] /. p -> List $\endgroup$
    – Roman
    Commented May 3 at 16:43
  • $\begingroup$ Thank you, Roman, I updated the answer $\endgroup$
    – eldo
    Commented May 3 at 16:53
3
$\begingroup$
pol = p[1, 2, 5]  p[3, 6, 9]  p[4, 7, 8] - 3  p[1, 2, 4]  p[3, 6, 9]  p[5, 7, 8];

Using FactorList and the following rules:

FactorList[#][[All, 1]] & /@ (pol /. Plus[a_, b_] :> {a, b}) /. p[a___] :> {a}

{{1, {1, 2, 5}, {3, 6, 9}, {4, 7, 8}}, {-3, {1, 2, 4}, {3, 6, 9}, {5, 7, 8}}}

$\endgroup$
2
$\begingroup$
pol = p[1, 2, 5] p[3, 6, 9] p[4, 7, 8] - 3 p[1, 2, 4] p[3, 6, 9] p[5, 7, 8];

If[AtomQ[#[[1]]], List @@@ #, Join[{1}, List @@@ #]] & /@ 
 List @@@ List @@ pol

{{1, {1, 2, 5}, {3, 6, 9}, {4, 7, 8}}, {-3, {1, 2, 4}, {3, 6, 9}, {5, 7, 8}}}

$\endgroup$
0
$\begingroup$

Use RuleDelayed( lhs:>rhs )

polynomial = 
  p[1, 2, 5] p[3, 6, 9] p[4, 7, 8] - 
   3 p[1, 2, 4] p[3, 6, 9] p[5, 7, 8];

(*Convert the polynomial into a list of terms*)
termsList = List @@ polynomial;

(*Map each term to extract the coefficient and the indices*)
formattedList = termsList /. Plus | Minus -> Sequence;
formattedList = 
  formattedList /. 
   a_. p[x_, y_, z_] p[u_, v_, w_] p[s_, t_, q_] :> {a, {x, y, z}, {u,
       v, w}, {s, t, q}};

formattedList
{{1, {4, 7, 8}, {3, 6, 9}, {1, 2, 5}}, {-3, {5, 7, 8}, {3, 6, 9}, {1, 2, 4}}}
$\endgroup$

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.