]> gitweb.michael.orlitzky.com - sage.d.git/commitdiff
mjo/matrix_algebra.py: support the original __getitem__ syntax
authorMichael Orlitzky <michael@orlitzky.com>
Sat, 24 Jan 2026 01:11:39 +0000 (20:11 -0500)
committerMichael Orlitzky <michael@orlitzky.com>
Sat, 24 Jan 2026 01:11:39 +0000 (20:11 -0500)
To support submodules, we need __getitem__() to support its original
behavior of returning coordinates when given all three indices.

mjo/matrix_algebra.py

index 43beb0137b1eafd1afb3b733e329aea4bd914da3..5750cb8ae4ab43910069916381544ee9b12b3f56 100644 (file)
@@ -92,24 +92,43 @@ class MatrixAlgebraElement(IndexedFreeModuleElement):
 
     def __getitem__(self, indices):
         r"""
+        Override the usual ``__getitem__`` to support retrieving
+        the entries of the matrix when only two indices are
+        supplied. The three-index form is still needed for e.g.
+        constructing submodules.
 
         SETUP::
 
             sage: from mjo.matrix_algebra import MatrixAlgebra
 
-        EXAMPLES::
+        EXAMPLES:
+
+        In this case the entries and the coordinates are the same,
+        but we can retrieve them both ways::
 
             sage: M = MatrixAlgebra(2,ZZ,ZZ)([[1,2],[3,4]])
             sage: M[0,0]
             1
+            sage: M[0,0,1]
+            1
             sage: M[0,1]
             2
+            sage: M[0,1,1]
+            2
             sage: M[1,0]
             3
+            sage: M[1,0,1]
+            3
             sage: M[1,1]
             4
-
+            sage: M[1,1,1]
+            4
         """
+        if len(indices) == 3:
+            return super().__getitem__(indices)
+
+        # If only two coordinates are given, return the matrix
+        # entry at the given position.
         i,j = indices
         d = self.monomial_coefficients()
         A = self.parent().entry_algebra()