]> gitweb.michael.orlitzky.com - sage.d.git/blobdiff - mjo/eja/eja_element.py
eja: start fixing Cartesian products of Cartesian products.
[sage.d.git] / mjo / eja / eja_element.py
index 7388e963ceaf42c0bdf41851e2b8019de5e69b3a..9a770ae5f68b3e19f3946ca7716f299a3ff82685 100644 (file)
@@ -4,7 +4,7 @@ from sage.modules.free_module import VectorSpace
 from sage.modules.with_basis.indexed_element import IndexedFreeModuleElement
 
 from mjo.eja.eja_operator import FiniteDimensionalEJAOperator
-from mjo.eja.eja_utils import _mat2vec
+from mjo.eja.eja_utils import _mat2vec, _scale
 
 class FiniteDimensionalEJAElement(IndexedFreeModuleElement):
     """
@@ -910,7 +910,7 @@ class FiniteDimensionalEJAElement(IndexedFreeModuleElement):
         M = matrix([(self.parent().one()).to_vector()])
         old_rank = 1
 
-        # Specifying the row-reduction algorithm can e.g.  help over
+        # Specifying the row-reduction algorithm can e.g. help over
         # AA because it avoids the RecursionError that gets thrown
         # when we have to look too hard for a root.
         #
@@ -1077,7 +1077,9 @@ class FiniteDimensionalEJAElement(IndexedFreeModuleElement):
         SETUP::
 
             sage: from mjo.eja.eja_algebra import (ComplexHermitianEJA,
-            ....:                                  QuaternionHermitianEJA)
+            ....:                                  HadamardEJA,
+            ....:                                  QuaternionHermitianEJA,
+            ....:                                  RealSymmetricEJA)
 
         EXAMPLES::
 
@@ -1107,14 +1109,35 @@ class FiniteDimensionalEJAElement(IndexedFreeModuleElement):
             [0 0 0 0 0 0 1 0]
             [0 0 0 0 0 0 0 1]
 
+        This also works in Cartesian product algebras::
+
+            sage: J1 = HadamardEJA(1)
+            sage: J2 = RealSymmetricEJA(2)
+            sage: J = cartesian_product([J1,J2])
+            sage: x = sum(J.gens())
+            sage: x.to_matrix()[0]
+            [1]
+            sage: x.to_matrix()[1]
+            [                  1 0.7071067811865475?]
+            [0.7071067811865475?                   1]
+
         """
         B = self.parent().matrix_basis()
         W = self.parent().matrix_space()
 
-        # This is just a manual "from_vector()", but of course
-        # matrix spaces aren't vector spaces in sage, so they
-        # don't have a from_vector() method.
-        return W.linear_combination( zip(B, self.to_vector()) )
+        if self.parent()._matrix_basis_is_cartesian:
+            # Aaaaand linear combinations don't work in Cartesian
+            # product spaces, even though they provide a method
+            # with that name. This is special-cased because the
+            # _scale() function is slow.
+            pairs = zip(B, self.to_vector())
+            return sum( ( _scale(b, alpha) for (b,alpha) in pairs ),
+                        W.zero())
+        else:
+            # This is just a manual "from_vector()", but of course
+            # matrix spaces aren't vector spaces in sage, so they
+            # don't have a from_vector() method.
+            return W.linear_combination( zip(B, self.to_vector()) )
 
 
 
@@ -1380,7 +1403,20 @@ class FiniteDimensionalEJAElement(IndexedFreeModuleElement):
 
         SETUP::
 
-            sage: from mjo.eja.eja_algebra import random_eja
+            sage: from mjo.eja.eja_algebra import (random_eja,
+            ....:                                  HadamardEJA,
+            ....:                                  RealSymmetricEJA)
+
+        EXAMPLES:
+
+        We can create subalgebras of Cartesian product EJAs that are not
+        themselves Cartesian product EJAs (they're just "regular" EJAs)::
+
+            sage: J1 = HadamardEJA(3)
+            sage: J2 = RealSymmetricEJA(2)
+            sage: J = cartesian_product([J1,J2])
+            sage: J.one().subalgebra_generated_by()
+            Euclidean Jordan algebra of dimension 1 over Algebraic Real Field
 
         TESTS:
 
@@ -1413,12 +1449,12 @@ class FiniteDimensionalEJAElement(IndexedFreeModuleElement):
             True
 
         """
-        from mjo.eja.eja_subalgebra import FiniteDimensionalEJASubalgebra
         powers = tuple( self**k for k in range(self.degree()) )
-        A = FiniteDimensionalEJASubalgebra(self.parent(),
-                                           powers,
-                                           associative=True,
-                                           **kwargs)
+        A = self.parent().subalgebra(powers,
+                                     associative=True,
+                                     check_field=False,
+                                     check_axioms=False,
+                                     **kwargs)
         A.one.set_cache(A(self.parent().one()))
         return A
 
@@ -1619,40 +1655,3 @@ class FiniteDimensionalEJAElement(IndexedFreeModuleElement):
 
         """
         return self.trace_inner_product(self).sqrt()
-
-
-
-class CartesianProductEJAElement(FiniteDimensionalEJAElement):
-
-    def to_matrix(self):
-        r"""
-        SETUP::
-
-            sage: from mjo.eja.eja_algebra import (HadamardEJA,
-            ....:                                  RealSymmetricEJA)
-
-        EXAMPLES::
-
-            sage: J1 = HadamardEJA(1)
-            sage: J2 = RealSymmetricEJA(2)
-            sage: J = cartesian_product([J1,J2])
-            sage: x = sum(J.gens())
-            sage: x.to_matrix()[0]
-            [1]
-            sage: x.to_matrix()[1]
-            [                  1 0.7071067811865475?]
-            [0.7071067811865475?                   1]
-
-        """
-        B = self.parent().matrix_basis()
-        W = self.parent().matrix_space()
-
-        # Aaaaand linear combinations don't work in Cartesian
-        # product spaces, even though they provide a method
-        # with that name.
-        pairs = zip(B, self.to_vector())
-        sigma = W.zero()
-        for (b,alpha) in pairs:
-            # sum(...) ALSO doesn't work on Cartesian products.
-            sigma += W(tuple(alpha*b_i for b_i in b))
-        return sigma