diff options
author | Nao Pross <np@0hm.ch> | 2024-04-25 18:50:00 +0200 |
---|---|---|
committer | Nao Pross <np@0hm.ch> | 2024-04-25 18:50:18 +0200 |
commit | 6b86a0f45e658201efd6116ac0cd948d882af484 (patch) | |
tree | 25f824b90bb272ab96e0cc70cf022d5fd50efac3 | |
parent | Add __repr__ methods to some polymatrix.typing classes (diff) | |
download | polymatrix-6b86a0f45e658201efd6116ac0cd948d882af484.tar.gz polymatrix-6b86a0f45e658201efd6116ac0cd948d882af484.zip |
Make is_constant for MonomialIndex and VariableIndex static methods
also fix an error in VariableIndex.is_constant
Diffstat (limited to '')
-rw-r--r-- | polymatrix/polymatrix/typing.py | 16 |
1 files 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 |