]> gitweb.michael.orlitzky.com - sage.d.git/blobdiff - mjo/matrix_algebra.py
eja: fix representation of "zero" in MatrixAlgebra.
[sage.d.git] / mjo / matrix_algebra.py
index 73286ff452adf3323a9fdf534dfbeba8c6777bbd..29a37d1e7c9bdee3dee6b36960126d5f545753fe 100644 (file)
@@ -57,8 +57,18 @@ class MatrixAlgebraElement(IndexedFreeModuleElement):
             | 0 | 0 |
             +---+---+
 
+        TESTS::
+
+            sage: MatrixAlgebra(0,ZZ,ZZ).zero()
+            0
+
         """
-        return table(self.rows(), frame=True)._repr_()
+        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.
+            return self.parent().entry_algebra().zero().__repr__()
+
+        return table(rs, frame=True)._repr_()
 
 
     def list(self):
@@ -185,9 +195,8 @@ 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()
@@ -352,9 +361,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 +424,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)