# Revision history [back]

It's undocumented but SageMath supports 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

for d in range(1,20):
print(d)
M = sum((R.monomials_of_degree(k) for k in range(d)), [])
M_order1 = sorted(M)
from functools import cmp_to_key
M_order2 = sorted(M, key=cmp_to_key(lambda a,b: my_order(a.exponents()[0], b.exponents()[0])))
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
...
19
True


So it works.

It's undocumented but SageMath supports 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,20):
range(1,50):
print(d)
M = sum((R.monomials_of_degree(k) for k in range(d)), [])
M_order1 = sorted(M)
from functools import cmp_to_key
M_order2 = sorted(M, key=cmp_to_key(lambda a,b: my_order(a.exponents()[0], b.exponents()[0])))
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
...
19
True


So it works.

It's undocumented but SageMath supports supports 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
...
19
True


So it works.

SageMath supports 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
...
19
49
True


So it works.