From 40f05711fd912960d31328d9ada747f3730104b8 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Wed, 2 Feb 2022 10:45:44 -0500 Subject: [PATCH] matrix_algebra.py: speed up __getitem__() and trace(). --- mjo/matrix_algebra.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/mjo/matrix_algebra.py b/mjo/matrix_algebra.py index bc46c2a..6817af1 100644 --- a/mjo/matrix_algebra.py +++ b/mjo/matrix_algebra.py @@ -111,7 +111,12 @@ class MatrixAlgebraElement(IndexedFreeModuleElement): """ i,j = indices - return self.rows()[i][j] + d = self.monomial_coefficients() + A = self.parent().entry_algebra() + return A.sum( d[k]*k[2] + for k in d + if k[0] == i and k[1] == j ) + def trace(self): r""" @@ -136,8 +141,9 @@ class MatrixAlgebraElement(IndexedFreeModuleElement): [0 2] """ - zero = self.parent().entry_algebra().zero() - return sum( (self[i,i] for i in range(self.nrows())), zero ) + d = self.monomial_coefficients() + A = self.parent().entry_algebra() + return A.sum( d[k]*k[2] for k in d if k[0] == k[1] ) def matrix_space(self): r""" @@ -203,7 +209,7 @@ class MatrixAlgebra(CombinatorialFreeModule): self._nrows = n - # Since the scalar ring is real but the entries are not, + # Since the scalar ring is (say) real but the entries are not, # sticking a "1" in each position doesn't give us a basis for # the space. We actually need to stick each of e0, e1, ... (a # basis for the entry algebra itself) into each position. -- 2.44.2