<

Question: How can an equation be sorted by numerical value of coefficients and degree?

I want to collect up the equation terms by the numerical value of the terms coefficient? Have tried sort collect combine...
So far the best I have come up with is nops(indets(on each term). And put them in seperate lists. This still doesn't quiet do the trick.
I am looking to achieve. Would to happy to have then as seperate lists or equations.

(a_1^5+a_2^5...)+5(a_1^4a_2+a_1^4a_3....)+10(a_1^3a_2^2 ....)+20(  ....   )+......+60(a_1^2a_2a_3a_4+ a_1a_2^2a_3a_4....)


 

restart

pn := (a[1]+a[2]+a[3]+a[4])^5

(a[1]+a[2]+a[3]+a[4])^5

pn1 := expand(pn)

a[1]^5+5*a[1]^4*a[2]+5*a[1]^4*a[3]+5*a[1]^4*a[4]+10*a[1]^3*a[2]^2+20*a[1]^3*a[2]*a[3]+20*a[1]^3*a[2]*a[4]+10*a[1]^3*a[3]^2+20*a[1]^3*a[3]*a[4]+10*a[1]^3*a[4]^2+10*a[1]^2*a[2]^3+30*a[1]^2*a[2]^2*a[3]+30*a[1]^2*a[2]^2*a[4]+30*a[1]^2*a[2]*a[3]^2+60*a[1]^2*a[2]*a[3]*a[4]+30*a[1]^2*a[2]*a[4]^2+10*a[1]^2*a[3]^3+30*a[1]^2*a[3]^2*a[4]+30*a[1]^2*a[3]*a[4]^2+10*a[1]^2*a[4]^3+5*a[1]*a[2]^4+20*a[1]*a[2]^3*a[3]+20*a[1]*a[2]^3*a[4]+30*a[1]*a[2]^2*a[3]^2+60*a[1]*a[2]^2*a[3]*a[4]+30*a[1]*a[2]^2*a[4]^2+20*a[1]*a[2]*a[3]^3+60*a[1]*a[2]*a[3]^2*a[4]+60*a[1]*a[2]*a[3]*a[4]^2+20*a[1]*a[2]*a[4]^3+5*a[1]*a[3]^4+20*a[1]*a[3]^3*a[4]+30*a[1]*a[3]^2*a[4]^2+20*a[1]*a[3]*a[4]^3+5*a[1]*a[4]^4+a[2]^5+5*a[2]^4*a[3]+5*a[2]^4*a[4]+10*a[2]^3*a[3]^2+20*a[2]^3*a[3]*a[4]+10*a[2]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30*a[2]^2*a[3]*a[4]^2+10*a[2]^2*a[4]^3+5*a[2]*a[3]^4+20*a[2]*a[3]^3*a[4]+30*a[2]*a[3]^2*a[4]^2+20*a[2]*a[3]*a[4]^3+5*a[2]*a[4]^4+a[3]^5+5*a[3]^4*a[4]+10*a[3]^3*a[4]^2+10*a[3]^2*a[4]^3+5*a[3]*a[4]^4+a[4]^5

L1 := []; L2 := []; L3 := []; L4 := []; for i to nops(els) do if nops(indets(els[i])) = 1 then L1 := [op(L1), els[i]] elif nops(indets(els[i])) = 2 then L2 := [op(L2), els[i]] elif nops(indets(els[i])) = 3 then L3 := [op(L3), els[i]] else L4 := [op(L4), els[i]] end if end do; L1; L2; L3; L4

[60*a[1]*a[2]*a[3]*a[4]^2, 60*a[1]*a[2]*a[3]^2*a[4], 60*a[1]*a[2]^2*a[3]*a[4], 60*a[1]^2*a[2]*a[3]*a[4]]

indets(els[7])

{a[1], a[4]}

NULL

indets(els(5))

{}

`~`[op](1 .. -1, L2)

[5, a[1], a[2]^4, 5, a[1], a[3]^4, 5, a[1], a[4]^4, 10, a[1]^2, a[2]^3, 10, a[1]^2, a[3]^3, 10, a[1]^2, a[4]^3, 10, a[1]^3, a[2]^2, 10, a[1]^3, a[3]^2, 10, a[1]^3, a[4]^2, 5, a[1]^4, a[2], 5, a[1]^4, a[3], 5, a[1]^4, a[4], 5, a[2], a[3]^4, 5, a[2], a[4]^4, 10, a[2]^2, a[3]^3, 10, a[2]^2, a[4]^3, 10, a[2]^3, a[3]^2, 10, a[2]^3, a[4]^2, 5, a[2]^4, a[3], 5, a[2]^4, a[4], 5, a[3], a[4]^4, 10, a[3]^2, a[4]^3, 10, a[3]^3, a[4]^2, 5, a[3]^4, a[4]]

NULL

op(2, L2[1])

a[1]

op(3, L2[1])

a[2]^4``

Download 30-7-22_Q_sort_equation_by_numerical_coeffs.mw

Please Wait...