From b16c3338bb6ecf1bf74a40aa072d060ed49143be Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Mon, 27 May 2024 11:19:34 +0200 Subject: Fix pretty printing of PolyMatrix objects --- polymatrix/polymatrix/index.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/polymatrix/polymatrix/index.py b/polymatrix/polymatrix/index.py index 83eccf5..ba9d508 100644 --- a/polymatrix/polymatrix/index.py +++ b/polymatrix/polymatrix/index.py @@ -53,6 +53,9 @@ class MonomialIndex(tuple[VariableIndex]): return "MonomialIndex(" + ",".join(repr(v) for v in self) + ")" def __str__(self): + if len(self) == 0: + return "" + return " * ".join(str(v) for v in sorted(self)) @property @@ -164,7 +167,9 @@ class PolyDict(UserDict[MonomialIndex, int | float]): return f"{self.__class__.__qualname__}({super().__repr__()})" def __str__(self) -> str: - return " + ".join(f"{str(c)} * {str(m)}" for m, c in self.items()) + return " + ".join(f"{str(c)} * {str(m)}" + if len(m) > 0 else f"{str(c)}" + for m, c in self.items()) def __setitem__(self, key: Iterable[VariableIndex] | MonomialIndex, value: int | float): if not isinstance(key, MonomialIndex): -- cgit v1.2.1