Ask Your Question

Revision history [back]

You can choose a term ordering for (all elements of) the polynomial ring by defining e.g.

R.<a,b,c,d> = PolynomialRing(QQ, order='lex')

That doesn't help to achieve your desired ordering though.

You can do it with a custom function (that is independent of the term ordering on the polynomial ring):

def my_print(f):
    exponents = f.exponents()
    coeffs = f.coefficients()
    monomials = f.monomials()
    ordering = range(len(exponents))
    # Lexicographic sort.
    ordering = sorted(ordering, key=lambda i: exponents[i], reverse=True)
    # Signature sort, preserving the (lexicographic) ordering of terms with the same signature.
    ordering = sorted(ordering, key=lambda i: tuple(sorted(exponents[i])))
    print(" + ".join(f"{coeffs[i]*monomials[i]}" for i in ordering))

def my_show(f):
    exponents = f.exponents()
    coeffs = f.coefficients()
    monomials = f.monomials()
    ordering = range(len(exponents))
    # Lexicographic sort.
    ordering = sorted(ordering, key=lambda i: exponents[i], reverse=True)
    # Signature sort, preserving the (lexicographic) ordering of terms with the same signature.
    ordering = sorted(ordering, key=lambda i: tuple(sorted(exponents[i])))
    show(LatexExpr(" + ".join(latex(coeffs[i]*monomials[i]) for i in ordering)))

It works on your example:

sage: R.<a,b,c,d> = QQ[]
sage: f = a^4 + b^4 + 12*a^2*b*c + 6*b^2*c^2 + c^4 + 12*a*b^2*d + 12*a*c^2*d + 6*a^2*d^2 + 12*b*c*d^2 + d^4
sage: my_print(f)
a^4 + b^4 + c^4 + d^4 + 6*a^2*d^2 + 6*b^2*c^2 + 12*a^2*b*c + 12*a*b^2*d + 12*a*c^2*d + 12*b*c*d^2
sage: my_show(f)

$$\displaystyle a^{4} + b^{4} + c^{4} + d^{4} + 6 a^{2} d^{2} + 6 b^{2} c^{2} + 12 a^{2} b c + 12 a b^{2} d + 12 a c^{2} d + 12 b c d^{2}$$

The function could be improved for polynomials containing negative coefficients.

Your own code looks fine to me.

You can choose a term ordering for (all elements of) the polynomial ring by defining e.g.

R.<a,b,c,d> = PolynomialRing(QQ, order='lex')

That doesn't help to achieve your desired ordering though.

You can do it with a custom function (that is independent of the term ordering on the polynomial ring):

def my_print(f):
    exponents = f.exponents()
    coeffs = f.coefficients()
    monomials = f.monomials()
    ordering = range(len(exponents))
    # Lexicographic sort.
    ordering = sorted(ordering, key=lambda i: exponents[i], reverse=True)
    # Signature Degree signature sort, preserving the (lexicographic) ordering of terms with the same signature.
    ordering = sorted(ordering, key=lambda i: tuple(sorted(exponents[i])))
    print(" + ".join(f"{coeffs[i]*monomials[i]}" for i in ordering))

def my_show(f):
    exponents = f.exponents()
    coeffs = f.coefficients()
    monomials = f.monomials()
    ordering = range(len(exponents))
    # Lexicographic sort.
    ordering = sorted(ordering, key=lambda i: exponents[i], reverse=True)
    # Signature Degree signature sort, preserving the (lexicographic) ordering of terms with the same signature.
    ordering = sorted(ordering, key=lambda i: tuple(sorted(exponents[i])))
    show(LatexExpr(" + ".join(latex(coeffs[i]*monomials[i]) for i in ordering)))

It works on your example:

sage: R.<a,b,c,d> = QQ[]
sage: f = a^4 + b^4 + 12*a^2*b*c + 6*b^2*c^2 + c^4 + 12*a*b^2*d + 12*a*c^2*d + 6*a^2*d^2 + 12*b*c*d^2 + d^4
sage: my_print(f)
a^4 + b^4 + c^4 + d^4 + 6*a^2*d^2 + 6*b^2*c^2 + 12*a^2*b*c + 12*a*b^2*d + 12*a*c^2*d + 12*b*c*d^2
sage: my_show(f)

$$\displaystyle a^{4} + b^{4} + c^{4} + d^{4} + 6 a^{2} d^{2} + 6 b^{2} c^{2} + 12 a^{2} b c + 12 a b^{2} d + 12 a c^{2} d + 12 b c d^{2}$$

The function could be improved for polynomials containing negative coefficients.

Your own code looks mostly fine to me.

me; I would only replace the use of html and $'s by LatexExpr as I've done in this answer.