From 408be59cafdad8d5058dd4d055746267de6ac4dc Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Sun, 2 Jun 2024 15:39:32 +0200 Subject: Create helper PolyDict.terms_of_degree(d) --- polymatrix/polymatrix/index.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/polymatrix/polymatrix/index.py b/polymatrix/polymatrix/index.py index ba9d508..bab991f 100644 --- a/polymatrix/polymatrix/index.py +++ b/polymatrix/polymatrix/index.py @@ -201,6 +201,12 @@ class PolyDict(UserDict[MonomialIndex, int | float]): # This is an alias for readability yield from self.items() + def terms_of_degree(self, d: int) -> Iterable[tuple[MonomialIndex, int | float]]: + """ Iterate over terms of degree `d` with a non-zero coefficient. """ + yield from ((m, coeff) + for m, coeff in self.terms() + if m.degree == d) + def monomials(self) -> Iterable[MonomialIndex]: """ Get monomials that compose the polynomial. """ # This is an alias for readability -- cgit v1.2.1