summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNao Pross <np@0hm.ch>2024-05-09 15:31:16 +0200
committerNao Pross <np@0hm.ch>2024-05-09 15:31:16 +0200
commit181d8f67b6a4243911d211caa027a6e92e803e82 (patch)
treeecc448751f5f2a4da3d59bf491d596bd3227fca5
parentCreate PowerExprMixin (diff)
downloadpolymatrix-181d8f67b6a4243911d211caa027a6e92e803e82.tar.gz
polymatrix-181d8f67b6a4243911d211caa027a6e92e803e82.zip
Add helper method to PolyDict.constant to get constant term
-rw-r--r--polymatrix/polymatrix/index.py6
1 files changed, 6 insertions, 0 deletions
diff --git a/polymatrix/polymatrix/index.py b/polymatrix/polymatrix/index.py
index d31e987..c906b35 100644
--- a/polymatrix/polymatrix/index.py
+++ b/polymatrix/polymatrix/index.py
@@ -155,6 +155,12 @@ class PolyDict(UserDict[MonomialIndex, int | float]):
key = MonomialIndex(key)
return super().__setitem__(key, value)
+ def constant(self) -> int | float:
+ """ Get the constant term of the polynomial. """
+ if MonomialIndex.constant() in self:
+ return self[MonomialIndex.constant()]
+ return 0
+
def terms(self) -> Iterable[tuple[MonomialIndex, int | float]]:
""" Iterate over terms with a non-zero coefficient. """
# This is an alias for readability