From 181d8f67b6a4243911d211caa027a6e92e803e82 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Thu, 9 May 2024 15:31:16 +0200 Subject: Add helper method to PolyDict.constant to get constant term --- polymatrix/polymatrix/index.py | 6 ++++++ 1 file changed, 6 insertions(+) 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 -- cgit v1.2.1