diff options
author | Nao Pross <np@0hm.ch> | 2024-05-09 16:12:59 +0200 |
---|---|---|
committer | Nao Pross <np@0hm.ch> | 2024-05-09 16:13:41 +0200 |
commit | d8da5fcc48d3e1049dacdcdcb13404b924aadfd9 (patch) | |
tree | 2f1180bbdd50a161f050b88636751837861f2d38 | |
parent | Separate out computation of power in PowerExprMixin into staticmethod (diff) | |
download | polymatrix-d8da5fcc48d3e1049dacdcdcb13404b924aadfd9.tar.gz polymatrix-d8da5fcc48d3e1049dacdcdcb13404b924aadfd9.zip |
Create helper PolyDict.is_constant to check if a polynomial is a constant
-rw-r--r-- | polymatrix/polymatrix/index.py | 14 |
1 files changed, 14 insertions, 0 deletions
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 |