aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--mdpoly/abc.py11
-rw-r--r--mdpoly/constants.py1
2 files changed, 12 insertions, 0 deletions
diff --git a/mdpoly/abc.py b/mdpoly/abc.py
index 53b16a5..7ac21dd 100644
--- a/mdpoly/abc.py
+++ b/mdpoly/abc.py
@@ -5,6 +5,7 @@ Defines:
- Representations of polynomials
"""
from .types import Number, Shape, MatrixIndex, PolyIndex
+from .constants import NUMERICS_EPS
from typing import Self, Sequence, Protocol, runtime_checkable
from abc import abstractmethod
@@ -60,6 +61,10 @@ class Repr(Protocol):
""" Set value of polynomial entry """
@abstractmethod
+ def set_zero(self, entry: MatrixIndex, term: PolyIndex) -> None:
+ """ Set an entry to zero """
+
+ @abstractmethod
def entries(self) -> Sequence[MatrixIndex]:
""" Return indices to non-zero entries of the matrix """
@@ -68,6 +73,12 @@ class Repr(Protocol):
""" Return indices to non-zero terms in the polynomial at the given
matrix entry """
+ def zero_smaller_than(self, eps: Number = NUMERICS_EPS) -> None:
+ for entry in self.entries():
+ for term in self.terms(entry):
+ if self.at(entry, term) < eps:
+ self.set_zero(entry, term)
+
def __iter__(self) -> Sequence[tuple[MatrixIndex, PolyIndex, Number]]:
""" Iterate over non-zero entries of the representations """
for entry in self.entries():
diff --git a/mdpoly/constants.py b/mdpoly/constants.py
new file mode 100644
index 0000000..2d62186
--- /dev/null
+++ b/mdpoly/constants.py
@@ -0,0 +1 @@
+NUMERICS_EPS = 1e-12