From d8da5fcc48d3e1049dacdcdcb13404b924aadfd9 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Thu, 9 May 2024 16:12:59 +0200 Subject: Create helper PolyDict.is_constant to check if a polynomial is a constant --- polymatrix/polymatrix/index.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/polymatrix/polymatrix/index.py b/polymatrix/polymatrix/index.py index c906b35..e50df43 100644 --- a/polymatrix/polymatrix/index.py +++ b/polymatrix/polymatrix/index.py @@ -161,6 +161,20 @@ class PolyDict(UserDict[MonomialIndex, int | float]): return self[MonomialIndex.constant()] return 0 + def is_constant(self) -> bool: + """ + Check whether the polynomial is a constant polynomial. + """ + # polynomial is zero + if len(self.keys()) == 0: + return True + + if len(self.keys()) > 1: + return False + + m, *_ = self.keys() + return MonomialIndex.is_constant(m) + 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