diff options
author | Nao Pross <np@0hm.ch> | 2024-05-25 13:37:34 +0200 |
---|---|---|
committer | Nao Pross <np@0hm.ch> | 2024-05-25 13:37:34 +0200 |
commit | f86679d3d5a65fe4f43fb878f6ba77c0b9a4c1db (patch) | |
tree | 5092b6fc98b0f86ab76c4cf8fa872a4061995a18 | |
parent | Fix ConcatenateExpr bug and expose concatenate (diff) | |
download | polymatrix-f86679d3d5a65fe4f43fb878f6ba77c0b9a4c1db.tar.gz polymatrix-f86679d3d5a65fe4f43fb878f6ba77c0b9a4c1db.zip |
Create shorthand PolyMatrix.scalar() for .at(0,0)
-rw-r--r-- | polymatrix/polymatrix/mixins.py | 11 |
1 files changed, 11 insertions, 0 deletions
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. |