diff options
author | Nao Pross <np@0hm.ch> | 2024-06-02 15:39:32 +0200 |
---|---|---|
committer | Nao Pross <np@0hm.ch> | 2024-06-02 15:39:32 +0200 |
commit | 408be59cafdad8d5058dd4d055746267de6ac4dc (patch) | |
tree | 2b0d4f155a500b6bee992d02234a5b0af694ec55 | |
parent | Give better name to member of BroadcastPolyMatrix (diff) | |
download | polymatrix-408be59cafdad8d5058dd4d055746267de6ac4dc.tar.gz polymatrix-408be59cafdad8d5058dd4d055746267de6ac4dc.zip |
Create helper PolyDict.terms_of_degree(d)
-rw-r--r-- | polymatrix/polymatrix/index.py | 6 |
1 files changed, 6 insertions, 0 deletions
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 |