From: Michael Orlitzky Date: Sat, 24 Jan 2026 01:11:39 +0000 (-0500) Subject: mjo/matrix_algebra.py: support the original __getitem__ syntax X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=492e5eb77e5dd5f5cf5eae0bb9de638b3fae74a0;p=sage.d.git mjo/matrix_algebra.py: support the original __getitem__ syntax To support submodules, we need __getitem__() to support its original behavior of returning coordinates when given all three indices. --- diff --git a/mjo/matrix_algebra.py b/mjo/matrix_algebra.py index 43beb01..5750cb8 100644 --- a/mjo/matrix_algebra.py +++ b/mjo/matrix_algebra.py @@ -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()