summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNao Pross <np@0hm.ch>2024-04-29 14:07:59 +0200
committerNao Pross <np@0hm.ch>2024-04-29 14:07:59 +0200
commit7e06be4d56b4fa76a0cf2cfc23c8daedd9f1e245 (patch)
tree26ec11b78949d09ed81ba11925e51a7753475dd6
parentCreate PolyMatrixAsAffineExpression as replacement for DenseExpr (diff)
downloadpolymatrix-7e06be4d56b4fa76a0cf2cfc23c8daedd9f1e245.tar.gz
polymatrix-7e06be4d56b4fa76a0cf2cfc23c8daedd9f1e245.zip
Add total order to MonomialIndex
Diffstat (limited to '')
-rw-r--r--polymatrix/polymatrix/typing.py22
1 files changed, 19 insertions, 3 deletions
diff --git a/polymatrix/polymatrix/typing.py b/polymatrix/polymatrix/typing.py
index ce33e67..cebf636 100644
--- a/polymatrix/polymatrix/typing.py
+++ b/polymatrix/polymatrix/typing.py
@@ -35,10 +35,18 @@ class VariableIndex(NamedTuple):
class MonomialIndex(tuple[VariableIndex]):
- """
+ r"""
Index for a monomial, i.e. a product of `VariableIndex`.
- `MonomialIndex` has a partial order with respect to the degree.
+ `MonomialIndex` has a total order, they can be sorted with respect to the
+ degree. If they have the same degree then they are sorted with respect to
+ the index of the first variable.
+
+ For example :math:`x^2 z \succeq xy` because the former has degree 3 while
+ the latter is quadratic. Then :math:`yz \succeq xy` (assuming :math:`x` has
+ index 0, :math:`y` has index 1 and :math:`z` 2) because :math:`y \succeq x`.
+
+ Constant monomial indices are considered equal.
"""
@property
@@ -50,7 +58,15 @@ class MonomialIndex(tuple[VariableIndex]):
return sum(v.power for v in self)
def __lt__(self, other):
- """ Monomial indices can be sorted with respect to the exponent value. """
+ # Constants are equal
+ if MonomialIndex.is_constant(self):
+ return False
+
+ # they have the same degree the index counts
+ if self.degree == other.degree:
+ # Assumes that monomialindex is sorted!
+ return self[0] < other[0]
+
return self.degree < other.degree
@staticmethod