# Specific term order of polynomials

I'm studying the Lagrange resolvent method for solving n-degree equations. This is what I have so far:

# a,b,c,d represent the roots of a 4-degree polynomial
R.<a,b,c,d> = QQ[]

# w will be treated as a primitive 4th root of unity
K.<w> = R[]

F = a + b*w + c*w^2 + d*w^3

# Lagrange resolvent
W = F^4 % (w^4 - 1)

# list of coefficients of w^i in W
Ws = [W[i] for i in range(4)]

S4 = SymmetricGroup(R.gens())

# compute the orbit of W under the action of S4
WOrbit = set([tuple([ws(s(R.gens())) for ws in Ws]) for s in S4])

# pretty print the orbit
for indt, t in enumerate(WOrbit):
print(f"Res {indt + 1}:")
for ind, i in enumerate(t):
show(html(f"${latex(w^ind)}$: ${latex(i)}$"))


This works quite nicely except for the term ordering of the coefficients (polynomials in the roots a,b,c,d). For example, for two elements of the orbit, I have the following constant terms: $1: 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}$ $1: a^{4} + 6 a^{2} b^{2} + b^{4} + 12 a b c^{2} + c^{4} + 12 a^{2} c d + 12 b^{2} c d + 12 a b d^{2} + 6 c^{2} d^{2} + d^{4}$

I would like some consistent ordering of the terms. I guess the inconsistency is caused by permuting the variables. Is there any way to adapt the term ordering according to the used permutation?

Also, is it possible to achieve an ordering first by the "degree signature" and then lexicographically? E.g. the first example should be ordered as (I grouped the terms with the same degree signature): $1: (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} )$

Last but not least, since I'm a beginner in Sage I would appreciate any comments on my solution. I'm sure there must be nicer or more efficient solutions for some of my steps.

edit retag close merge delete

A simple solution would be writing your own function for printing terms of a polynomial in desired order.

( 2024-01-21 14:33:54 +0200 )edit

Sort by ยป oldest newest most voted

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)
# Degree signature sort, preserving the 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)
# Degree signature sort, preserving the 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)))


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; I would only replace the use of html and \$'s by LatexExpr as I've done in this answer.

more

1

Thanks. While the idea is clear and correct, for future readers I should just note that it's better to precompute the sorted(exponents[i]) expressions. Also, the two functions can (and should) be trivially deduplicated.

( 2024-01-21 20:40:16 +0200 )edit