]> gitweb.michael.orlitzky.com - sage.d.git/commitdiff
matrix_algebra: implement one() dynamically.
authorMichael Orlitzky <michael@orlitzky.com>
Wed, 3 Mar 2021 15:16:42 +0000 (10:16 -0500)
committerMichael Orlitzky <michael@orlitzky.com>
Wed, 3 Mar 2021 15:16:42 +0000 (10:16 -0500)
mjo/matrix_algebra.py

index 4049ef653a8a2688c2b9d4399f2b5386212aae47..5b8b267f5120a4a319f77777885b19731437f518 100644 (file)
@@ -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()
 
@@ -299,16 +320,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() )
-
-