X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=mjo%2Fmatrix_algebra.py;h=6817af1e046d65efeab32f45dc17396cd7fb4906;hb=HEAD;hp=73286ff452adf3323a9fdf534dfbeba8c6777bbd;hpb=a87cbac5c3e1f3a6422b2152609f8209676fb4cb;p=sage.d.git diff --git a/mjo/matrix_algebra.py b/mjo/matrix_algebra.py index 73286ff..6817af1 100644 --- a/mjo/matrix_algebra.py +++ b/mjo/matrix_algebra.py @@ -57,7 +57,18 @@ class MatrixAlgebraElement(IndexedFreeModuleElement): | 0 | 0 | +---+---+ + TESTS:: + + sage: MatrixAlgebra(0,ZZ,ZZ).zero() + [] + """ + if self.nrows() == 0 or self.ncols() == 0: + # Otherwise we get a crash or a blank space, depending on + # how hard we work for it. This is what MatrixSpace(..., + # 0) returns. + return "[]" + return table(self.rows(), frame=True)._repr_() @@ -100,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""" @@ -125,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""" @@ -137,7 +154,6 @@ class MatrixAlgebraElement(IndexedFreeModuleElement): TESTS:: - sage: set_random_seed() sage: entries = QuaternionAlgebra(QQ,-1,-1) sage: M = MatrixAlgebra(3, entries, QQ) sage: M.random_element().matrix_space() == M @@ -185,16 +201,15 @@ class MatrixAlgebra(CombinatorialFreeModule): if "Unital" in entry_algebra.category().axioms(): category = category.Unital() entry_one = entry_algebra.one() - self.one = lambda: sum( (self.monomial((i,i,entry_one)) - for i in range(self.nrows()) ), - self.zero() ) + self.one = lambda: self.sum( (self.monomial((i,i,entry_one)) + for i in range(self.nrows()) ) ) if "Associative" in entry_algebra.category().axioms(): category = category.Associative() 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. @@ -352,9 +367,10 @@ class MatrixAlgebra(CombinatorialFreeModule): # We have to convert alpha_g because a priori it lives in the # base ring of the entry algebra. R = self.base_ring() - return self.sum( R(alpha_g)*self.monomial( (i,l,g) ) - for (alpha_g, g) - in zip(p, self.entry_algebra_gens())) + return self.sum_of_terms( (((i,l,g), R(alpha_g)) + for (alpha_g, g) + in zip(p, self.entry_algebra_gens()) ), + distinct=True) else: return self.zero() @@ -414,9 +430,10 @@ class MatrixAlgebra(CombinatorialFreeModule): # We have to convert alpha_g because a priori it lives in the # base ring of the entry algebra. R = self.base_ring() - return self.sum( R(alpha_g)*self.monomial( (i,j,g) ) - for (alpha_g, g) - in zip(p, self.entry_algebra_gens())) + return self.sum_of_terms( (((i,j,g), R(alpha_g)) + for (alpha_g, g) + in zip(p, self.entry_algebra_gens()) ), + distinct=True) return self.sum( entry_to_element(i,j,entries[i][j]) for j in range(ncols)