1
$\begingroup$

I asked a question about converting a polynomial into a list.

Now I have a list of polynomials. For example,

r1={ p[1,2,4], p[1,2,5], p[1,3,4]*p[1,2,5], p[1,3,4]*p[1,2,5]-3*p[1,2,3]*p[2,3,4], p[1,3,4]*p[1,2,5]*p[3,6,9]-3*p[1,2,3]*p[2,3,4]*p[2,5,8], p[1,3,4]*p[1,2,5]*p[3,6,9]-3*p[1,2,3]*p[2,3,4]}

The polynomials in r1 have different degrees and a polynomial can be non-homogeneous. I would like to obtain:

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

How to obtain it? Thank you very much.

$\endgroup$
2
  • $\begingroup$ Could just use Map with one of the methods from responses to the prior question. $\endgroup$ Commented Mar 31 at 3:12
  • $\begingroup$ r1 /. {Times -> List, p -> List} $\endgroup$
    – Roland F
    Commented Mar 31 at 6:47

1 Answer 1

1
$\begingroup$

This gets a bit complicated because MMA will automatically evaluate addition of structurally equal elements. Toward this aim, we change addition (subtraction is an addition of the negative element) to a list and mark it by: "q":

rule0 = c1_ + c2_ -> {q, {c1, c2}, q};

In the end, we will undo this.

Then we write rules for changing p[...] and product thereof to lists:

rule1 = (c1_Integer : 1)  p[c2__] -> {{c1, {{c2}}}};
rule2 = (c1_Integer : 1)  p[c2__]  p[c3__] -> {c1, {{c2}, {c3}}};
rule3 = (c1_Integer : 1) p[c2__]  p[c3__]  p[ c4__] -> {c1, {{c2}, {c3}, {c4}}} ;

Finally, we need to undo the "{q" and "q}":

rule4 = {q, c__, q} -> c;

With these rules we may now transform r1:

 r1 /. rule0 /. {rule3, rule2, rule1}   /. rule4

{{{1, {{1, 2, 4}}}}, {{1, {{1, 2, 5}}}}, {1, {{1, 2, 5}, {1, 3, 
    4}}}, {{1, {{1, 2, 5}, {1, 3, 4}}}, {-3, {{1, 2, 3}, {2, 3, 
     4}}}}, {{-3, {{1, 2, 3}, {2, 3, 4}, {2, 5, 8}}}, {1, {{1, 2, 
     5}, {1, 3, 4}, {3, 6, 9}}}}, {{-3, {{1, 2, 3}, {2, 3, 
     4}}}, {1, {{1, 2, 5}, {1, 3, 4}, {3, 6, 9}}}}}
$\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.