Hi Rajeev,

On 26 Mai, 20:08, Rajeev Singh <[email protected]> wrote:
> Why is the order of terms in the list not the same that in string of p (a^2
> in string appears before a*b and the opposite happens for the list) ? What
> can I do to make them in same order?

One can sort lists in Python (see the Python docs), and one can also
state how the list elements shall be sorted.

Here, the list elements are pairs (coefficient,monomial), and we want
to sort so that the monomials increase, whereas the coefficients do
not matter.

Hence, you can do:

sage: p^3
a^6 + 2*a^5*b + 2*a^3*b*a^2 + 4*a^3*b*a*b + 2*a*b*a^4 + 4*a*b*a^3*b +
4*a*b*a*b*a^2 + 8*a*b*a*b*a*b
sage: L = list(p^3); L  # that's not ordered how you want it
[(1, a^6), (2, a^5*b), (2, a^3*b*a^2), (4, a*b*a*b*a^2), (4,
a*b*a^3*b), (2, a*b*a^4), (8, a*b*a*b*a*b), (4, a^3*b*a*b)]
sage: L.sort(cmp=lambda x,y:cmp(x[1],y[1]))  # sort by the monomials
sage: L
[(1, a^6), (2, a^5*b), (2, a^3*b*a^2), (4, a^3*b*a*b), (2, a*b*a^4),
(4, a*b*a^3*b), (4, a*b*a*b*a^2), (8, a*b*a*b*a*b)]

Remark: If you do
  sage: p._repr_??<hit return after ??>
then you see how printing p is implemented.

Cheers,
Simon

-- 
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org

Reply via email to