From f86679d3d5a65fe4f43fb878f6ba77c0b9a4c1db Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Sat, 25 May 2024 13:37:34 +0200 Subject: Create shorthand PolyMatrix.scalar() for .at(0,0) --- polymatrix/polymatrix/mixins.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/polymatrix/polymatrix/mixins.py b/polymatrix/polymatrix/mixins.py index 4cb89b3..f5cdb48 100644 --- a/polymatrix/polymatrix/mixins.py +++ b/polymatrix/polymatrix/mixins.py @@ -34,6 +34,17 @@ class PolyMatrixMixin(ABC): p.at(*entry) # if you have a MatrixIndex """ + def scalar(self) -> PolyDict: + """ + If the polymatrix is scalar, that is, a 1x1 matrix, this is a shorthand + for .at(0,0) for readability. Raises an error polymatrix is not scalar. + """ + if self.shape != (1,1): + raise AttributeError("Use of .scalar() is not allowed because " + f"it has shape {self.shape}") + + return self.at(0, 0) + def entries(self) -> Iterable[tuple[MatrixIndex, PolyDict]]: """ Iterate over the non-zero entries of the polynomial matrix. -- cgit v1.2.1