From 6b86a0f45e658201efd6116ac0cd948d882af484 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Thu, 25 Apr 2024 18:50:00 +0200 Subject: Make is_constant for MonomialIndex and VariableIndex static methods also fix an error in VariableIndex.is_constant --- polymatrix/polymatrix/typing.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/polymatrix/polymatrix/typing.py b/polymatrix/polymatrix/typing.py index 06b65cd..ce33e67 100644 --- a/polymatrix/polymatrix/typing.py +++ b/polymatrix/polymatrix/typing.py @@ -28,9 +28,10 @@ class VariableIndex(NamedTuple): def constant() -> VariableIndex: return cast(VariableIndex, tuple()) - def is_constant(self) -> bool: + @staticmethod + def is_constant(index: VariableIndex) -> bool: """ Returns true if it is indexing a constant the unit '1' variable. """ - return len(self) > 1 + return not bool(index) # empty tuple is falsy class MonomialIndex(tuple[VariableIndex]): @@ -62,11 +63,12 @@ class MonomialIndex(tuple[VariableIndex]): """ Get the placeholder for constant terms. """ return MonomialIndex((VariableIndex.constant(),)) - def is_constant(self) -> bool: + @staticmethod + def is_constant(index: MonomialIndex) -> bool: """ Returns true if it is indexing a constant monomial. """ - if len(self) > 1: + if len(index) > 1: return False - return self[0].is_constant() + return VariableIndex.is_constant(index[0]) @staticmethod def sort(index: MonomialIndex) -> MonomialIndex: @@ -81,10 +83,10 @@ class MonomialIndex(tuple[VariableIndex]): For example if left is the index of :math:`xy` and right is the index of :math:`y^2` this functions returns the index of :math:`xy^3`. """ - if left.is_constant(): + if MonomialIndex.is_constant(left): return right - if right.is_constant(): + if MonomialIndex.is_constant(right): return left # Compute the product of each non-constant term in left with each -- cgit v1.2.1