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.Mon, 05 Feb 2024 21:12:45 +0100Implement Weight Ordershttps://ask.sagemath.org/question/75829/implement-weight-orders/I am having trouble implementing weight orders in SageMath.
Suppose we are given the polynomial ring $K[x, y, z]$, and weight vectors $w_1 = (1, 2, 3), w_2 = (4, 5, 6)$.
For exponent vectors $a, b$, I would like to implement a term order that decides $a > b$ according to:
- $w_1 a > w_1 b$
- $w_1 a = w_1 b$ and $w_2 a > w_2 b$
- $w_1 a = w_1 b$ and $w_2 a = w_2 b$ and $a >_{LEX} b$, i.e. in case of two ties $>$ defaults to the standard lexicographic term order.
How can I implement such a term order in SageMath?Mon, 05 Feb 2024 19:04:12 +0100https://ask.sagemath.org/question/75829/implement-weight-orders/Answer by rburing for <p>I am having trouble implementing weight orders in SageMath.
Suppose we are given the polynomial ring $K[x, y, z]$, and weight vectors $w_1 = (1, 2, 3), w_2 = (4, 5, 6)$.
For exponent vectors $a, b$, I would like to implement a term order that decides $a > b$ according to:</p>
<ul>
<li>$w_1 a > w_1 b$</li>
<li>$w_1 a = w_1 b$ and $w_2 a > w_2 b$</li>
<li>$w_1 a = w_1 b$ and $w_2 a = w_2 b$ and $a >_{LEX} b$, i.e. in case of two ties $>$ defaults to the standard lexicographic term order.</li>
</ul>
<p>How can I implement such a term order in SageMath?</p>
https://ask.sagemath.org/question/75829/implement-weight-orders/?answer=75830#post-id-75830SageMath [supports](https://doc.sagemath.org/html/en/reference/polynomial_rings/sage/rings/polynomial/term_order.html) term orders defined by integer matrices (hence all possible term orders).
We want a matrix $W$ such that $a > b$ according to your ordering iff $Wa >_{\text{lex}} Wb$.
The square matrix is specified by the tuple of its integer entries, row-wise:
w1 = (1,2,3)
w2 = (4,5,6)
R.<x,y,z> = PolynomialRing(QQ, order=TermOrder(w1 + w2 + (1, 1, 1)))
Double-check that it's the correct ordering:
def my_order(a,b):
d1a = tuple(vector(w1).pairwise_product(vector(a)))
d1b = tuple(vector(w1).pairwise_product(vector(b)))
if d1a != d1b:
return d1a > d1b
d2a = tuple(vector(w2).pairwise_product(vector(a)))
d2b = tuple(vector(w2).pairwise_product(vector(b)))
if d2a != d2b:
return d2a > d2b
return a > b
from functools import cmp_to_key
my_key = cmp_to_key(lambda a,b: my_order(a.exponents()[0], b.exponents()[0]))
for d in range(1,50):
print(d)
M = sum((R.monomials_of_degree(k) for k in range(d)), [])
M_order1 = sorted(M)
M_order2 = sorted(M, key=my_key)
print(M_order1 == M_order2)
Output:
1
True
2
True
3
True
4
True
5
True
6
True
7
True
8
True
9
True
10
...
49
True
So it works.Mon, 05 Feb 2024 21:12:45 +0100https://ask.sagemath.org/question/75829/implement-weight-orders/?answer=75830#post-id-75830