ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 21 Jan 2024 20:40:16 +0100Specific term order of polynomialshttps://ask.sagemath.org/question/75602/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.Sun, 21 Jan 2024 12:01:34 +0100https://ask.sagemath.org/question/75602/specific-term-order-of-polynomials/Comment by Max Alekseyev for <p>I'm studying the Lagrange resolvent method for solving n-degree equations. This is what I have so far:</p>
<pre><code># 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)}$"))
</code></pre>
<p>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}$</p>
<p>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?</p>
<p>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} )$</p>
<p>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.</p>
https://ask.sagemath.org/question/75602/specific-term-order-of-polynomials/?comment=75604#post-id-75604A simple solution would be writing your own function for printing terms of a polynomial in desired order.Sun, 21 Jan 2024 14:33:54 +0100https://ask.sagemath.org/question/75602/specific-term-order-of-polynomials/?comment=75604#post-id-75604Answer by rburing for <p>I'm studying the Lagrange resolvent method for solving n-degree equations. This is what I have so far:</p>
<pre><code># 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)}$"))
</code></pre>
<p>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}$</p>
<p>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?</p>
<p>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} )$</p>
<p>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.</p>
https://ask.sagemath.org/question/75602/specific-term-order-of-polynomials/?answer=75606#post-id-75606You 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)))
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; I would only replace the use of `html` and `$`'s by `LatexExpr` as I've done in this answer.Sun, 21 Jan 2024 17:29:44 +0100https://ask.sagemath.org/question/75602/specific-term-order-of-polynomials/?answer=75606#post-id-75606Comment by kejv2 for <p>You can choose a term ordering for (all elements of) the polynomial ring by defining e.g.</p>
<pre><code>R.<a,b,c,d> = PolynomialRing(QQ, order='lex')
</code></pre>
<p>That doesn't help to achieve your desired ordering though.</p>
<p>You can do it with a custom function (that is independent of the term ordering on the polynomial ring):</p>
<pre><code>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)))
</code></pre>
<p>It works on your example:</p>
<pre><code>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)
</code></pre>
<p>$$\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}$$</p>
<p>The function could be improved for polynomials containing negative coefficients.</p>
<p>Your own code looks mostly fine to me; I would only replace the use of <code>html</code> and <code>$</code>'s by <code>LatexExpr</code> as I've done in this answer.</p>
https://ask.sagemath.org/question/75602/specific-term-order-of-polynomials/?comment=75610#post-id-75610Thanks. 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.Sun, 21 Jan 2024 20:40:16 +0100https://ask.sagemath.org/question/75602/specific-term-order-of-polynomials/?comment=75610#post-id-75610