X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=mjo%2Fmatrix_algebra.py;h=a67a9b4a1c0692d31aceffa3097343aa0df99dae;hb=23ced4147cf68d9e5d7a00be958ad3c579436f50;hp=4049ef653a8a2688c2b9d4399f2b5386212aae47;hpb=ab0536f4db17eb78f3623927653b8f7f1a7e6808;p=sage.d.git diff --git a/mjo/matrix_algebra.py b/mjo/matrix_algebra.py index 4049ef6..a67a9b4 100644 --- a/mjo/matrix_algebra.py +++ b/mjo/matrix_algebra.py @@ -37,7 +37,7 @@ class MatrixAlgebraElement(IndexedFreeModuleElement): l[i][j] += v*e return l - def __repr__(self): + def _repr_(self): r""" Display this matrix as a table. @@ -158,6 +158,22 @@ class MatrixAlgebra(CombinatorialFreeModule): the entries come from a commutative and associative ring. This is problematic in several interesting matrix algebras, like those where the entries are quaternions or octonions. + + SETUP:: + + sage: from mjo.matrix_algebra import MatrixAlgebra + + EXAMPLES:: + + The existence of a unit element is determined dynamically:: + + sage: MatrixAlgebra(ZZ,ZZ,2).one() + +---+---+ + | 1 | 0 | + +---+---+ + | 0 | 1 | + +---+---+ + """ Element = MatrixAlgebraElement @@ -168,6 +184,11 @@ 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() ) + if "Associative" in entry_algebra.category().axioms(): category = category.Associative() @@ -211,10 +232,38 @@ class MatrixAlgebra(CombinatorialFreeModule): ncols = nrows def product_on_basis(self, mon1, mon2): + r""" + + SETUP:: + + sage: from mjo.octonions import Octonions + sage: from mjo.matrix_algebra import MatrixAlgebra + + TESTS:: + + sage: O = Octonions(QQ) + sage: e = O.gens() + sage: e[2]*e[1] + -e3 + sage: A = MatrixAlgebra(O,QQ,2) + sage: A.product_on_basis( (0,0,e[2]), (0,0,e[1]) ) + +-----+---+ + | -e3 | 0 | + +-----+---+ + | 0 | 0 | + +-----+---+ + + """ (i,j,e1) = mon1 (k,l,e2) = mon2 if j == k: - return self.monomial((i,l,e1*e2)) + # If e1*e2 has a negative sign in front of it, + # then (i,l,e1*e2) won't be a monomial! + p = e1*e2 + if (i,l,p) in self.indices(): + return self.monomial((i,l,p)) + else: + return -self.monomial((i,l,-p)) else: return self.zero() @@ -299,16 +348,3 @@ class HurwitzMatrixAlgebraElement(MatrixAlgebraElement): class HurwitzMatrixAlgebra(MatrixAlgebra): Element = HurwitzMatrixAlgebraElement - - def one(self): - r""" - SETUP:: - - sage: from mjo.matrix_algebra import HurwitzMatrixAlgebra - - """ - return sum( (self.monomial((i,i,self.entry_algebra().one())) - for i in range(self.nrows()) ), - self.zero() ) - -