aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNao Pross <np@0hm.ch>2024-03-02 18:13:54 +0100
committerNao Pross <np@0hm.ch>2024-03-02 18:13:54 +0100
commit595bb7f952721524b35e811008bf27ccb5ca398b (patch)
tree7f6e8d217b3cd4f1814244469c6c9e4cd320f7f9
parentAdd shorthands to index classes and make leaves representable (diff)
downloadmdpoly-595bb7f952721524b35e811008bf27ccb5ca398b.tar.gz
mdpoly-595bb7f952721524b35e811008bf27ccb5ca398b.zip
Add constants file and introduce EPS for numerical precision
-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