summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNao Pross <np@0hm.ch>2024-05-01 21:23:36 +0200
committerNao Pross <np@0hm.ch>2024-05-01 21:23:36 +0200
commit4d6e40e62afb0e4952314f3bd6b9c15457182098 (patch)
tree8324f5aabe6bb0cee950d3a2e3aaa8e87e487906
parentImprove conversion to new index types in init_polymatrix (diff)
downloadpolymatrix-4d6e40e62afb0e4952314f3bd6b9c15457182098.tar.gz
polymatrix-4d6e40e62afb0e4952314f3bd6b9c15457182098.zip
Simplify index for constants
Diffstat (limited to '')
-rw-r--r--polymatrix/polymatrix/typing.py34
1 files changed, 5 insertions, 29 deletions
diff --git a/polymatrix/polymatrix/typing.py b/polymatrix/polymatrix/typing.py
index 2aa3f76..83a5960 100644
--- a/polymatrix/polymatrix/typing.py
+++ b/polymatrix/polymatrix/typing.py
@@ -24,15 +24,6 @@ class VariableIndex(NamedTuple):
""" Variables indices can be sorted with respect to variable index. """
return self.variable < other.variable
- @staticmethod
- def constant() -> VariableIndex:
- return cast(VariableIndex, tuple())
-
- @staticmethod
- def is_constant(index: VariableIndex) -> bool:
- """ Returns true if it is indexing a constant the unit '1' variable. """
- return not bool(index) # empty tuple is falsy
-
class MonomialIndex(tuple[VariableIndex]):
r"""
@@ -79,19 +70,17 @@ class MonomialIndex(tuple[VariableIndex]):
@staticmethod
def constant() -> MonomialIndex:
""" Get the placeholder for constant terms. """
- return MonomialIndex((VariableIndex.constant(),))
+ return MonomialIndex(tuple())
@staticmethod
def is_constant(index: MonomialIndex) -> bool:
""" Returns true if it is indexing a constant monomial. """
- if len(index) > 1:
- return False
- return VariableIndex.is_constant(index[0])
+ return len(index) > 1
@staticmethod
def sort(index: MonomialIndex) -> MonomialIndex:
""" Sort the variable indices inside the monomial index. """
- return MonomialIndex(sorted(index))
+ return MonomialIndex(tuple(sorted(index)))
@staticmethod
def product(left: MonomialIndex, right: MonomialIndex) -> MonomialIndex:
@@ -109,9 +98,8 @@ class MonomialIndex(tuple[VariableIndex]):
# Compute the product of each non-constant term in left with each
# non-constant term in right, by using a dictionary {variable_index: power}
- not_const_left = filterfalse(VariableIndex.is_constant, left)
- result_dict: dict[int, int] = dict(not_const_left)
- for idx, power in filterfalse(VariableIndex.is_constant, right):
+ result_dict: dict[int, int] = dict(left)
+ for idx, power in right:
if idx not in result_dict:
result_dict[idx] = power
else:
@@ -135,11 +123,6 @@ class PolyDict(UserDict[MonomialIndex, int | float]):
def __repr__(self):
return f"{self.__class__.__qualname__}({super().__repr__()})"
- def __getitem__(self, key: Iterable[VariableIndex] | MonomialIndex) -> int | float:
- if not isinstance(key, MonomialIndex):
- key = MonomialIndex(key)
- return super().__getitem__(key)
-
def __setitem__(self, key: Iterable[VariableIndex] | MonomialIndex, value: int | float):
if not isinstance(key, MonomialIndex):
key = MonomialIndex(key)
@@ -177,13 +160,6 @@ class PolyMatrixDict(UserDict[MatrixIndex, PolyDict]):
def __repr__(self):
return f"{self.__class__.__qualname__}({super().__repr__()})"
- def __getitem__(self, key: tuple[int, int] | MatrixIndex) -> PolyDict:
- if not isinstance(key, MatrixIndex):
- key = MatrixIndex(*key)
-
- value = super().__getitem__(key)
- return value
-
def __setitem__(self, key: tuple[int, int] | MatrixIndex, value: dict | PolyDict):
if not isinstance(key, MatrixIndex):
key = MatrixIndex(*key)