diff options
author | Nao Pross <np@0hm.ch> | 2024-05-27 11:19:34 +0200 |
---|---|---|
committer | Nao Pross <np@0hm.ch> | 2024-05-27 11:19:34 +0200 |
commit | b16c3338bb6ecf1bf74a40aa072d060ed49143be (patch) | |
tree | 084de96e09063b7ad9ce81b2113e910d27525309 | |
parent | Fix bug in Expression.__sub__, update other operator overloadings (diff) | |
download | polymatrix-b16c3338bb6ecf1bf74a40aa072d060ed49143be.tar.gz polymatrix-b16c3338bb6ecf1bf74a40aa072d060ed49143be.zip |
Fix pretty printing of PolyMatrix objects
-rw-r--r-- | polymatrix/polymatrix/index.py | 7 |
1 files changed, 6 insertions, 1 deletions
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): |