]> gitweb.michael.orlitzky.com - sage.d.git/blobdiff - mjo/eja/eja_subalgebra.py
eja: drop custom gram_schmidt() routine that isn't noticeably faster.
[sage.d.git] / mjo / eja / eja_subalgebra.py
index 9685defd95b1fdab00487ee1d719ab225e6812e7..c372e50072c73a50281dd45d07eec62a62848277 100644 (file)
@@ -1,9 +1,82 @@
 from sage.matrix.constructor import matrix
-from sage.structure.category_object import normalize_names
 
 from mjo.eja.eja_algebra import FiniteDimensionalEuclideanJordanAlgebra
 from mjo.eja.eja_element import FiniteDimensionalEuclideanJordanAlgebraElement
 
+class FiniteDimensionalEuclideanJordanElementSubalgebraElement(FiniteDimensionalEuclideanJordanAlgebraElement):
+    """
+    SETUP::
+
+        sage: from mjo.eja.eja_algebra import random_eja
+
+    TESTS::
+
+    The natural representation of an element in the subalgebra is
+    the same as its natural representation in the superalgebra::
+
+        sage: set_random_seed()
+        sage: A = random_eja().random_element().subalgebra_generated_by()
+        sage: y = A.random_element()
+        sage: actual = y.natural_representation()
+        sage: expected = y.superalgebra_element().natural_representation()
+        sage: actual == expected
+        True
+
+    The left-multiplication-by operator for elements in the subalgebra
+    works like it does in the superalgebra, even if we orthonormalize
+    our basis::
+
+        sage: set_random_seed()
+        sage: x = random_eja(AA).random_element()
+        sage: A = x.subalgebra_generated_by(orthonormalize_basis=True)
+        sage: y = A.random_element()
+        sage: y.operator()(A.one()) == y
+        True
+
+    """
+
+    def superalgebra_element(self):
+        """
+        Return the object in our algebra's superalgebra that corresponds
+        to myself.
+
+        SETUP::
+
+            sage: from mjo.eja.eja_algebra import (RealSymmetricEJA,
+            ....:                                  random_eja)
+
+        EXAMPLES::
+
+            sage: J = RealSymmetricEJA(3)
+            sage: x = sum(J.gens())
+            sage: x
+            e0 + e1 + e2 + e3 + e4 + e5
+            sage: A = x.subalgebra_generated_by()
+            sage: A(x)
+            f1
+            sage: A(x).superalgebra_element()
+            e0 + e1 + e2 + e3 + e4 + e5
+
+        TESTS:
+
+        We can convert back and forth faithfully::
+
+            sage: set_random_seed()
+            sage: J = random_eja()
+            sage: x = J.random_element()
+            sage: A = x.subalgebra_generated_by()
+            sage: A(x).superalgebra_element() == x
+            True
+            sage: y = A.random_element()
+            sage: A(y.superalgebra_element()) == y
+            True
+
+        """
+        return self.parent().superalgebra().linear_combination(
+          zip(self.parent()._superalgebra_basis, self.to_vector()) )
+
+
+
 
 class FiniteDimensionalEuclideanJordanElementSubalgebra(FiniteDimensionalEuclideanJordanAlgebra):
     """
@@ -11,76 +84,98 @@ class FiniteDimensionalEuclideanJordanElementSubalgebra(FiniteDimensionalEuclide
 
     SETUP::
 
-        sage: from mjo.eja.eja_algebra import FiniteDimensionalEuclideanJordanAlgebra
+        sage: from mjo.eja.eja_algebra import (ComplexHermitianEJA,
+        ....:                                  JordanSpinEJA)
 
     TESTS:
 
-    Ensure that non-clashing names are chosen::
-
-        sage: m1 = matrix.identity(QQ,2)
-        sage: m2 = matrix(QQ, [[0,1],
-        ....:                  [1,0]])
-        sage: J = FiniteDimensionalEuclideanJordanAlgebra(QQ,
-        ....:                                             [m1,m2],
-        ....:                                             2,
-        ....:                                             names='f')
-        sage: J.variable_names()
-        ('f0', 'f1')
-        sage: A = sum(J.gens()).subalgebra_generated_by()
-        sage: A.variable_names()
-        ('g0', 'g1')
+    Ensure that our generator names don't conflict with the superalgebra::
+
+        sage: J = JordanSpinEJA(3)
+        sage: J.one().subalgebra_generated_by().gens()
+        (f0,)
+        sage: J = JordanSpinEJA(3, prefix='f')
+        sage: J.one().subalgebra_generated_by().gens()
+        (g0,)
+        sage: J = JordanSpinEJA(3, prefix='b')
+        sage: J.one().subalgebra_generated_by().gens()
+        (c0,)
+
+    Ensure that we can find subalgebras of subalgebras::
+
+        sage: A = ComplexHermitianEJA(3).one().subalgebra_generated_by()
+        sage: B = A.one().subalgebra_generated_by()
+        sage: B.dimension()
+        1
 
     """
-    @staticmethod
-    def __classcall_private__(cls, elt):
-        superalgebra = elt.parent()
-
-        # First compute the vector subspace spanned by the powers of
-        # the given element.
-        V = superalgebra.vector_space()
-        superalgebra_basis = [superalgebra.one()]
-        basis_vectors = [superalgebra.one().vector()]
-        W = V.span_of_basis(basis_vectors)
-        for exponent in range(1, V.dimension()):
-            new_power = elt**exponent
-            basis_vectors.append( new_power.vector() )
-            try:
-                W = V.span_of_basis(basis_vectors)
-                superalgebra_basis.append( new_power )
-            except ValueError:
-                # Vectors weren't independent; bail and keep the
-                # last subspace that worked.
-                break
-
-        # Make the basis hashable for UniqueRepresentation.
-        superalgebra_basis = tuple(superalgebra_basis)
-
-        # Now figure out the entries of the right-multiplication
-        # matrix for the successive basis elements b0, b1,... of
-        # that subspace.
-        F = superalgebra.base_ring()
-        mult_table = []
-        for b_right in superalgebra_basis:
-                b_right_rows = []
-                # The first row of the right-multiplication matrix by
-                # b1 is what we get if we apply that matrix to b1. The
-                # second row of the right multiplication matrix by b1
-                # is what we get when we apply that matrix to b2...
-                #
-                # IMPORTANT: this assumes that all vectors are COLUMN
-                # vectors, unlike our superclass (which uses row vectors).
-                for b_left in superalgebra_basis:
-                    # Multiply in the original EJA, but then get the
-                    # coordinates from the subalgebra in terms of its
-                    # basis.
-                    this_row = W.coordinates((b_left*b_right).vector())
-                    b_right_rows.append(this_row)
-                b_right_matrix = matrix(F, b_right_rows)
-                mult_table.append(b_right_matrix)
-
-        for m in mult_table:
-            m.set_immutable()
-        mult_table = tuple(mult_table)
+    def __init__(self, elt, orthonormalize_basis):
+        self._superalgebra = elt.parent()
+        category = self._superalgebra.category().Associative()
+        V = self._superalgebra.vector_space()
+        field = self._superalgebra.base_ring()
+
+        # A half-assed attempt to ensure that we don't collide with
+        # the superalgebra's prefix (ignoring the fact that there
+        # could be super-superelgrbas in scope). If possible, we
+        # try to "increment" the parent algebra's prefix, although
+        # this idea goes out the window fast because some prefixen
+        # are off-limits.
+        prefixen = [ 'f', 'g', 'h', 'a', 'b', 'c', 'd' ]
+        try:
+            prefix = prefixen[prefixen.index(self._superalgebra.prefix()) + 1]
+        except ValueError:
+            prefix = prefixen[0]
+
+        # This list is guaranteed to contain all independent powers,
+        # because it's the maximal set of powers that could possibly
+        # be independent (by a dimension argument).
+        powers = [ elt**k for k in range(V.dimension()) ]
+        power_vectors = [ p.to_vector() for p in powers ]
+        P = matrix(field, power_vectors)
+
+        if orthonormalize_basis == False:
+            # In this case, we just need to figure out which elements
+            # of the "powers" list are redundant... First compute the
+            # vector subspace spanned by the powers of the given
+            # element.
+
+            # Figure out which powers form a linearly-independent set.
+            ind_rows = P.pivot_rows()
+
+            # Pick those out of the list of all powers.
+            superalgebra_basis = tuple(map(powers.__getitem__, ind_rows))
+
+            # If our superalgebra is a subalgebra of something else, then
+            # these vectors won't have the right coordinates for
+            # V.span_of_basis() unless we use V.from_vector() on them.
+            basis_vectors = map(power_vectors.__getitem__, ind_rows)
+        else:
+            # If we're going to orthonormalize the basis anyway, we
+            # might as well just do Gram-Schmidt on the whole list of
+            # powers. The redundant ones will get zero'd out. If this
+            # looks like a roundabout way to orthonormalize, it is.
+            # But converting everything from algebra elements to vectors
+            # to matrices and then back again turns out to be about
+            # as fast as reimplementing our own Gram-Schmidt that
+            # works in an EJA.
+            G,_ = P.gram_schmidt(orthonormal=True)
+            basis_vectors = [ g for g in G.rows() if not g.is_zero() ]
+            superalgebra_basis = [ self._superalgebra.from_vector(b)
+                                   for b in basis_vectors ]
+
+        W = V.span_of_basis( V.from_vector(v) for v in basis_vectors )
+        n = len(superalgebra_basis)
+        mult_table = [[W.zero() for i in range(n)] for j in range(n)]
+        for i in range(n):
+            for j in range(n):
+                product = superalgebra_basis[i]*superalgebra_basis[j]
+                # product.to_vector() might live in a vector subspace
+                # if our parent algebra is already a subalgebra. We
+                # use V.from_vector() to make it "the right size" in
+                # that case.
+                product_vector = V.from_vector(product.to_vector())
+                mult_table[i][j] = W.coordinate_vector(product_vector)
 
         # The rank is the highest possible degree of a minimal
         # polynomial, and is bounded above by the dimension. We know
@@ -90,61 +185,170 @@ class FiniteDimensionalEuclideanJordanElementSubalgebra(FiniteDimensionalEuclide
         # its rank too.
         rank = W.dimension()
 
-        # EJAs are power-associative, and this algebra is nothin but
-        # powers.
-        assume_associative=True
-
-        # Figure out a non-conflicting set of names to use.
-        valid_names = ['f','g','h','a','b','c','d']
-        name_idx = 0
-        names = normalize_names(W.dimension(), valid_names[0])
-        # This loops so long as the list of collisions is nonempty.
-        # Just crash if we run out of names without finding a set that
-        # don't conflict with the parent algebra.
-        while [y for y in names if y in superalgebra.variable_names()]:
-            name_idx += 1
-            names = normalize_names(W.dimension(), valid_names[name_idx])
-
-        cat = superalgebra.category().Associative()
-
-        # TODO: compute this and actually specify it.
-        natural_basis = None
-
-        fdeja = super(FiniteDimensionalEuclideanJordanElementSubalgebra, cls)
-        return fdeja.__classcall__(cls,
-                                   F,
-                                   mult_table,
-                                   rank,
-                                   superalgebra_basis,
-                                   W,
-                                   assume_associative=assume_associative,
-                                   names=names,
-                                   category=cat,
-                                   natural_basis=natural_basis)
-
-    def __init__(self,
-                 field,
-                 mult_table,
-                 rank,
-                 superalgebra_basis,
-                 vector_space,
-                 assume_associative=True,
-                 names='f',
-                 category=None,
-                 natural_basis=None):
-
-        self._superalgebra = superalgebra_basis[0].parent()
-        self._vector_space = vector_space
+        natural_basis = tuple( b.natural_representation()
+                               for b in superalgebra_basis )
+
+
+        self._vector_space = W
         self._superalgebra_basis = superalgebra_basis
 
+
         fdeja = super(FiniteDimensionalEuclideanJordanElementSubalgebra, self)
-        fdeja.__init__(field,
-                       mult_table,
-                       rank,
-                       assume_associative=assume_associative,
-                       names=names,
-                       category=category,
-                       natural_basis=natural_basis)
+        return fdeja.__init__(field,
+                              mult_table,
+                              rank,
+                              prefix=prefix,
+                              category=category,
+                              natural_basis=natural_basis)
+
+
+    def _a_regular_element(self):
+        """
+        Override the superalgebra method to return the one
+        regular element that is sure to exist in this
+        subalgebra, namely the element that generated it.
+
+        SETUP::
+
+            sage: from mjo.eja.eja_algebra import random_eja
+
+        TESTS::
+
+            sage: set_random_seed()
+            sage: J = random_eja().random_element().subalgebra_generated_by()
+            sage: J._a_regular_element().is_regular()
+            True
+
+        """
+        if self.dimension() == 0:
+            return self.zero()
+        else:
+            return self.monomial(1)
+
+
+    def _element_constructor_(self, elt):
+        """
+        Construct an element of this subalgebra from the given one.
+        The only valid arguments are elements of the parent algebra
+        that happen to live in this subalgebra.
+
+        SETUP::
+
+            sage: from mjo.eja.eja_algebra import RealSymmetricEJA
+            sage: from mjo.eja.eja_subalgebra import FiniteDimensionalEuclideanJordanElementSubalgebra
+
+        EXAMPLES::
+
+            sage: J = RealSymmetricEJA(3)
+            sage: x = sum( i*J.gens()[i] for i in range(6) )
+            sage: K = FiniteDimensionalEuclideanJordanElementSubalgebra(x,False)
+            sage: [ K(x^k) for k in range(J.rank()) ]
+            [f0, f1, f2]
+
+        ::
+
+        """
+        if elt == 0:
+            # Just as in the superalgebra class, we need to hack
+            # this special case to ensure that random_element() can
+            # coerce a ring zero into the algebra.
+            return self.zero()
+
+        if elt in self.superalgebra():
+            coords = self.vector_space().coordinate_vector(elt.to_vector())
+            return self.from_vector(coords)
+
+
+
+    def one(self):
+        """
+        Return the multiplicative identity element of this algebra.
+
+        The superclass method computes the identity element, which is
+        beyond overkill in this case: the superalgebra identity
+        restricted to this algebra is its identity. Note that we can't
+        count on the first basis element being the identity -- it migth
+        have been scaled if we orthonormalized the basis.
+
+        SETUP::
+
+            sage: from mjo.eja.eja_algebra import (RealCartesianProductEJA,
+            ....:                                  random_eja)
+
+        EXAMPLES::
+
+            sage: J = RealCartesianProductEJA(5)
+            sage: J.one()
+            e0 + e1 + e2 + e3 + e4
+            sage: x = sum(J.gens())
+            sage: A = x.subalgebra_generated_by()
+            sage: A.one()
+            f0
+            sage: A.one().superalgebra_element()
+            e0 + e1 + e2 + e3 + e4
+
+        TESTS:
+
+        The identity element acts like the identity over the rationals::
+
+            sage: set_random_seed()
+            sage: x = random_eja().random_element()
+            sage: A = x.subalgebra_generated_by()
+            sage: x = A.random_element()
+            sage: A.one()*x == x and x*A.one() == x
+            True
+
+        The identity element acts like the identity over the algebraic
+        reals with an orthonormal basis::
+
+            sage: set_random_seed()
+            sage: x = random_eja(AA).random_element()
+            sage: A = x.subalgebra_generated_by(orthonormalize_basis=True)
+            sage: x = A.random_element()
+            sage: A.one()*x == x and x*A.one() == x
+            True
+
+        The matrix of the unit element's operator is the identity over
+        the rationals::
+
+            sage: set_random_seed()
+            sage: x = random_eja().random_element()
+            sage: A = x.subalgebra_generated_by()
+            sage: actual = A.one().operator().matrix()
+            sage: expected = matrix.identity(A.base_ring(), A.dimension())
+            sage: actual == expected
+            True
+
+        The matrix of the unit element's operator is the identity over
+        the algebraic reals with an orthonormal basis::
+
+            sage: set_random_seed()
+            sage: x = random_eja(AA).random_element()
+            sage: A = x.subalgebra_generated_by(orthonormalize_basis=True)
+            sage: actual = A.one().operator().matrix()
+            sage: expected = matrix.identity(A.base_ring(), A.dimension())
+            sage: actual == expected
+            True
+
+        """
+        if self.dimension() == 0:
+            return self.zero()
+        else:
+            sa_one = self.superalgebra().one().to_vector()
+            sa_coords = self.vector_space().coordinate_vector(sa_one)
+            return self.from_vector(sa_coords)
+
+
+    def natural_basis_space(self):
+        """
+        Return the natural basis space of this algebra, which is identical
+        to that of its superalgebra.
+
+        This is correct "by definition," and avoids a mismatch when the
+        subalgebra is trivial (with no natural basis to infer anything
+        from) and the parent is not.
+        """
+        return self.superalgebra().natural_basis_space()
 
 
     def superalgebra(self):
@@ -164,94 +368,23 @@ class FiniteDimensionalEuclideanJordanElementSubalgebra(FiniteDimensionalEuclide
         EXAMPLES::
 
             sage: J = RealSymmetricEJA(3)
-            sage: x = sum( i*J.gens()[i] for i in range(6) )
-            sage: K = FiniteDimensionalEuclideanJordanElementSubalgebra(x)
+            sage: x = J.monomial(0) + 2*J.monomial(2) + 5*J.monomial(5)
+            sage: K = FiniteDimensionalEuclideanJordanElementSubalgebra(x,False)
             sage: K.vector_space()
-            Vector space of degree 6 and dimension 3 over Rational Field
+            Vector space of degree 6 and dimension 3 over...
             User basis matrix:
-            [ 1  0  0  1  0  1]
-            [ 0  1  2  3  4  5]
-            [ 5 11 14 26 34 45]
-            sage: (x^0).vector()
-            (1, 0, 0, 1, 0, 1)
-            sage: (x^1).vector()
-            (0, 1, 2, 3, 4, 5)
-            sage: (x^2).vector()
-            (5, 11, 14, 26, 34, 45)
+            [ 1  0  1  0  0  1]
+            [ 1  0  2  0  0  5]
+            [ 1  0  4  0  0 25]
+            sage: (x^0).to_vector()
+            (1, 0, 1, 0, 0, 1)
+            sage: (x^1).to_vector()
+            (1, 0, 2, 0, 0, 5)
+            sage: (x^2).to_vector()
+            (1, 0, 4, 0, 0, 25)
 
         """
         return self._vector_space
 
 
-    class Element(FiniteDimensionalEuclideanJordanAlgebraElement):
-        def __init__(self, A, elt=None):
-            """
-            SETUP::
-
-                sage: from mjo.eja.eja_algebra import RealSymmetricEJA
-                sage: from mjo.eja.eja_subalgebra import FiniteDimensionalEuclideanJordanElementSubalgebra
-
-            EXAMPLES::
-
-                sage: J = RealSymmetricEJA(3)
-                sage: x = sum( i*J.gens()[i] for i in range(6) )
-                sage: K = FiniteDimensionalEuclideanJordanElementSubalgebra(x)
-                sage: [ K(x^k) for k in range(J.rank()) ]
-                [f0, f1, f2]
-
-            ::
-
-            """
-            if elt in A.superalgebra():
-                    # Try to convert a parent algebra element into a
-                    # subalgebra element...
-                try:
-                    coords = A.vector_space().coordinates(elt.vector())
-                    elt = A(coords)
-                except AttributeError:
-                    # Catches a missing method in elt.vector()
-                    pass
-
-            FiniteDimensionalEuclideanJordanAlgebraElement.__init__(self,
-                                                                    A,
-                                                                    elt)
-
-        def superalgebra_element(self):
-            """
-            Return the object in our algebra's superalgebra that corresponds
-            to myself.
-
-            SETUP::
-
-                sage: from mjo.eja.eja_algebra import (RealSymmetricEJA,
-                ....:                                  random_eja)
-
-            EXAMPLES::
-
-                sage: J = RealSymmetricEJA(3)
-                sage: x = sum(J.gens())
-                sage: x
-                e0 + e1 + e2 + e3 + e4 + e5
-                sage: A = x.subalgebra_generated_by()
-                sage: A(x)
-                f1
-                sage: A(x).superalgebra_element()
-                e0 + e1 + e2 + e3 + e4 + e5
-
-            TESTS:
-
-            We can convert back and forth faithfully::
-
-                sage: set_random_seed()
-                sage: J = random_eja()
-                sage: x = J.random_element()
-                sage: A = x.subalgebra_generated_by()
-                sage: A(x).superalgebra_element() == x
-                True
-                sage: y = A.random_element()
-                sage: A(y.superalgebra_element()) == y
-                True
-
-            """
-            return self.parent().superalgebra().linear_combination(
-              zip(self.vector(), self.parent()._superalgebra_basis) )
+    Element = FiniteDimensionalEuclideanJordanElementSubalgebraElement