From 595bb7f952721524b35e811008bf27ccb5ca398b Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Sat, 2 Mar 2024 18:13:54 +0100 Subject: Add constants file and introduce EPS for numerical precision --- mdpoly/abc.py | 11 +++++++++++ mdpoly/constants.py | 1 + 2 files changed, 12 insertions(+) create mode 100644 mdpoly/constants.py 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 @@ -59,6 +60,10 @@ class Repr(Protocol): def set(self, entry: MatrixIndex, term: PolyIndex, value: Number) -> None: """ 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 -- cgit v1.2.1