X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=mjo%2Feja%2Feja_algebra.py;h=5dee5d012d6c19d6fc5d79f804ad8aff2757c3d0;hb=ea99a21239882d478c1a458a0411b1eb0588b84b;hp=e32bc24cbb2741fca9de2285de703b7adf1497af;hpb=3c7644ecfe369b6f83aa707b87d7a1f9aa246e27;p=sage.d.git diff --git a/mjo/eja/eja_algebra.py b/mjo/eja/eja_algebra.py index e32bc24..5dee5d0 100644 --- a/mjo/eja/eja_algebra.py +++ b/mjo/eja/eja_algebra.py @@ -1459,7 +1459,22 @@ class ConcreteEuclideanJordanAlgebra(RationalBasisEuclideanJordanAlgebra): class MatrixEuclideanJordanAlgebra: @staticmethod - def real_embed(M): + def dimension_over_reals(): + r""" + The dimension of this matrix's base ring over the reals. + + The reals are dimension one over themselves, obviously; that's + just `\mathbb{R}^{1}`. Likewise, the complex numbers `a + bi` + have dimension two. Finally, the quaternions have dimension + four over the reals. + + This is used to determine the size of the matrix returned from + :meth:`real_embed`, among other things. + """ + raise NotImplementedError + + @classmethod + def real_embed(cls,M): """ Embed the matrix ``M`` into a space of real matrices. @@ -1472,15 +1487,21 @@ class MatrixEuclideanJordanAlgebra: real_embed(M*N) = real_embed(M)*real_embed(N) """ - raise NotImplementedError + if M.ncols() != M.nrows(): + raise ValueError("the matrix 'M' must be square") + return M - @staticmethod - def real_unembed(M): + @classmethod + def real_unembed(cls,M): """ The inverse of :meth:`real_embed`. """ - raise NotImplementedError + if M.ncols() != M.nrows(): + raise ValueError("the matrix 'M' must be square") + if not ZZ(M.nrows()).mod(cls.dimension_over_reals()).is_zero(): + raise ValueError("the matrix 'M' must be a real embedding") + return M @staticmethod def jordan_product(X,Y): @@ -1488,36 +1509,65 @@ class MatrixEuclideanJordanAlgebra: @classmethod def trace_inner_product(cls,X,Y): + r""" + Compute the trace inner-product of two real-embeddings. + + SETUP:: + + sage: from mjo.eja.eja_algebra import (RealSymmetricEJA, + ....: ComplexHermitianEJA, + ....: QuaternionHermitianEJA) + + EXAMPLES:: + + This gives the same answer as it would if we computed the trace + from the unembedded (original) matrices:: + + sage: set_random_seed() + sage: J = ComplexHermitianEJA.random_instance() + sage: x,y = J.random_elements(2) + sage: Xe = x.to_matrix() + sage: Ye = y.to_matrix() + sage: X = ComplexHermitianEJA.real_unembed(Xe) + sage: Y = ComplexHermitianEJA.real_unembed(Ye) + sage: expected = (X*Y).trace().real() + sage: actual = ComplexHermitianEJA.trace_inner_product(Xe,Ye) + sage: actual == expected + True + + :: + + sage: set_random_seed() + sage: J = QuaternionHermitianEJA.random_instance() + sage: x,y = J.random_elements(2) + sage: Xe = x.to_matrix() + sage: Ye = y.to_matrix() + sage: X = QuaternionHermitianEJA.real_unembed(Xe) + sage: Y = QuaternionHermitianEJA.real_unembed(Ye) + sage: expected = (X*Y).trace().coefficient_tuple()[0] + sage: actual = QuaternionHermitianEJA.trace_inner_product(Xe,Ye) + sage: actual == expected + True + + """ Xu = cls.real_unembed(X) Yu = cls.real_unembed(Y) tr = (Xu*Yu).trace() try: # Works in QQ, AA, RDF, et cetera. - return tr.real() + return tr.real() / cls.dimension_over_reals() except AttributeError: # A quaternion doesn't have a real() method, but does # have coefficient_tuple() method that returns the # coefficients of 1, i, j, and k -- in that order. - return tr.coefficient_tuple()[0] + return tr.coefficient_tuple()[0] / cls.dimension_over_reals() class RealMatrixEuclideanJordanAlgebra(MatrixEuclideanJordanAlgebra): @staticmethod - def real_embed(M): - """ - The identity function, for embedding real matrices into real - matrices. - """ - return M - - @staticmethod - def real_unembed(M): - """ - The identity function, for unembedding real matrices from real - matrices. - """ - return M + def dimension_over_reals(): + return 1 class RealSymmetricEJA(ConcreteEuclideanJordanAlgebra, @@ -1631,17 +1681,31 @@ class RealSymmetricEJA(ConcreteEuclideanJordanAlgebra, return cls(n, **kwargs) def __init__(self, n, **kwargs): + # We know this is a valid EJA, but will double-check + # if the user passes check_axioms=True. + if "check_axioms" not in kwargs: kwargs["check_axioms"] = False + super(RealSymmetricEJA, self).__init__(self._denormalized_basis(n), self.jordan_product, self.trace_inner_product, **kwargs) + + # TODO: this could be factored out somehow, but is left here + # because the MatrixEuclideanJordanAlgebra is not presently + # a subclass of the FDEJA class that defines rank() and one(). self.rank.set_cache(n) - self.one.set_cache(self(matrix.identity(ZZ,n))) + idV = matrix.identity(ZZ, self.dimension_over_reals()*n) + self.one.set_cache(self(idV)) + class ComplexMatrixEuclideanJordanAlgebra(MatrixEuclideanJordanAlgebra): @staticmethod - def real_embed(M): + def dimension_over_reals(): + return 2 + + @classmethod + def real_embed(cls,M): """ Embed the n-by-n complex matrix ``M`` into the space of real matrices of size 2n-by-2n via the map the sends each entry `z = a + @@ -1683,9 +1747,8 @@ class ComplexMatrixEuclideanJordanAlgebra(MatrixEuclideanJordanAlgebra): True """ + super(ComplexMatrixEuclideanJordanAlgebra,cls).real_embed(M) n = M.nrows() - if M.ncols() != n: - raise ValueError("the matrix 'M' must be square") # We don't need any adjoined elements... field = M.base_ring().base_ring() @@ -1699,8 +1762,8 @@ class ComplexMatrixEuclideanJordanAlgebra(MatrixEuclideanJordanAlgebra): return matrix.block(field, n, blocks) - @staticmethod - def real_unembed(M): + @classmethod + def real_unembed(cls,M): """ The inverse of _embed_complex_matrix(). @@ -1731,11 +1794,9 @@ class ComplexMatrixEuclideanJordanAlgebra(MatrixEuclideanJordanAlgebra): True """ + super(ComplexMatrixEuclideanJordanAlgebra,cls).real_unembed(M) n = ZZ(M.nrows()) - if M.ncols() != n: - raise ValueError("the matrix 'M' must be square") - if not n.mod(2).is_zero(): - raise ValueError("the matrix 'M' must be a complex embedding") + d = cls.dimension_over_reals() # If "M" was normalized, its base ring might have roots # adjoined and they can stick around after unembedding. @@ -1753,9 +1814,9 @@ class ComplexMatrixEuclideanJordanAlgebra(MatrixEuclideanJordanAlgebra): # Go top-left to bottom-right (reading order), converting every # 2-by-2 block we see to a single complex element. elements = [] - for k in range(n/2): - for j in range(n/2): - submat = M[2*k:2*k+2,2*j:2*j+2] + for k in range(n/d): + for j in range(n/d): + submat = M[d*k:d*k+d,d*j:d*j+d] if submat[0,0] != submat[1,1]: raise ValueError('bad on-diagonal submatrix') if submat[0,1] != -submat[1,0]: @@ -1763,38 +1824,7 @@ class ComplexMatrixEuclideanJordanAlgebra(MatrixEuclideanJordanAlgebra): z = submat[0,0] + submat[0,1]*i elements.append(z) - return matrix(F, n/2, elements) - - - @classmethod - def trace_inner_product(cls,X,Y): - """ - Compute a matrix inner product in this algebra directly from - its real embedding. - - SETUP:: - - sage: from mjo.eja.eja_algebra import ComplexHermitianEJA - - TESTS: - - This gives the same answer as the slow, default method implemented - in :class:`MatrixEuclideanJordanAlgebra`:: - - sage: set_random_seed() - sage: J = ComplexHermitianEJA.random_instance() - sage: x,y = J.random_elements(2) - sage: Xe = x.to_matrix() - sage: Ye = y.to_matrix() - sage: X = ComplexHermitianEJA.real_unembed(Xe) - sage: Y = ComplexHermitianEJA.real_unembed(Ye) - sage: expected = (X*Y).trace().real() - sage: actual = ComplexHermitianEJA.trace_inner_product(Xe,Ye) - sage: actual == expected - True - - """ - return RealMatrixEuclideanJordanAlgebra.trace_inner_product(X,Y)/2 + return matrix(F, n/d, elements) class ComplexHermitianEJA(ConcreteEuclideanJordanAlgebra, @@ -1912,12 +1942,20 @@ class ComplexHermitianEJA(ConcreteEuclideanJordanAlgebra, def __init__(self, n, **kwargs): + # We know this is a valid EJA, but will double-check + # if the user passes check_axioms=True. + if "check_axioms" not in kwargs: kwargs["check_axioms"] = False + super(ComplexHermitianEJA, self).__init__(self._denormalized_basis(n), self.jordan_product, self.trace_inner_product, **kwargs) + # TODO: this could be factored out somehow, but is left here + # because the MatrixEuclideanJordanAlgebra is not presently + # a subclass of the FDEJA class that defines rank() and one(). self.rank.set_cache(n) - # TODO: pre-cache the identity! + idV = matrix.identity(ZZ, self.dimension_over_reals()*n) + self.one.set_cache(self(idV)) @staticmethod def _max_random_instance_size(): @@ -1933,7 +1971,11 @@ class ComplexHermitianEJA(ConcreteEuclideanJordanAlgebra, class QuaternionMatrixEuclideanJordanAlgebra(MatrixEuclideanJordanAlgebra): @staticmethod - def real_embed(M): + def dimension_over_reals(): + return 4 + + @classmethod + def real_embed(cls,M): """ Embed the n-by-n quaternion matrix ``M`` into the space of real matrices of size 4n-by-4n by first sending each quaternion entry `z @@ -1972,10 +2014,9 @@ class QuaternionMatrixEuclideanJordanAlgebra(MatrixEuclideanJordanAlgebra): True """ + super(QuaternionMatrixEuclideanJordanAlgebra,cls).real_embed(M) quaternions = M.base_ring() n = M.nrows() - if M.ncols() != n: - raise ValueError("the matrix 'M' must be square") F = QuadraticField(-1, 'I') i = F.gen() @@ -1998,8 +2039,8 @@ class QuaternionMatrixEuclideanJordanAlgebra(MatrixEuclideanJordanAlgebra): - @staticmethod - def real_unembed(M): + @classmethod + def real_unembed(cls,M): """ The inverse of _embed_quaternion_matrix(). @@ -2029,11 +2070,9 @@ class QuaternionMatrixEuclideanJordanAlgebra(MatrixEuclideanJordanAlgebra): True """ + super(QuaternionMatrixEuclideanJordanAlgebra,cls).real_unembed(M) n = ZZ(M.nrows()) - if M.ncols() != n: - raise ValueError("the matrix 'M' must be square") - if not n.mod(4).is_zero(): - raise ValueError("the matrix 'M' must be a quaternion embedding") + d = cls.dimension_over_reals() # Use the base ring of the matrix to ensure that its entries can be # multiplied by elements of the quaternion algebra. @@ -2045,10 +2084,10 @@ class QuaternionMatrixEuclideanJordanAlgebra(MatrixEuclideanJordanAlgebra): # 4-by-4 block we see to a 2-by-2 complex block, to a 1-by-1 # quaternion block. elements = [] - for l in range(n/4): - for m in range(n/4): + for l in range(n/d): + for m in range(n/d): submat = ComplexMatrixEuclideanJordanAlgebra.real_unembed( - M[4*l:4*l+4,4*m:4*m+4] ) + M[d*l:d*l+d,d*m:d*m+d] ) if submat[0,0] != submat[1,1].conjugate(): raise ValueError('bad on-diagonal submatrix') if submat[0,1] != -submat[1,0].conjugate(): @@ -2059,38 +2098,7 @@ class QuaternionMatrixEuclideanJordanAlgebra(MatrixEuclideanJordanAlgebra): z += submat[0,1].imag()*k elements.append(z) - return matrix(Q, n/4, elements) - - - @classmethod - def trace_inner_product(cls,X,Y): - """ - Compute a matrix inner product in this algebra directly from - its real embedding. - - SETUP:: - - sage: from mjo.eja.eja_algebra import QuaternionHermitianEJA - - TESTS: - - This gives the same answer as the slow, default method implemented - in :class:`MatrixEuclideanJordanAlgebra`:: - - sage: set_random_seed() - sage: J = QuaternionHermitianEJA.random_instance() - sage: x,y = J.random_elements(2) - sage: Xe = x.to_matrix() - sage: Ye = y.to_matrix() - sage: X = QuaternionHermitianEJA.real_unembed(Xe) - sage: Y = QuaternionHermitianEJA.real_unembed(Ye) - sage: expected = (X*Y).trace().coefficient_tuple()[0] - sage: actual = QuaternionHermitianEJA.trace_inner_product(Xe,Ye) - sage: actual == expected - True - - """ - return RealMatrixEuclideanJordanAlgebra.trace_inner_product(X,Y)/4 + return matrix(Q, n/d, elements) class QuaternionHermitianEJA(ConcreteEuclideanJordanAlgebra, @@ -2209,12 +2217,21 @@ class QuaternionHermitianEJA(ConcreteEuclideanJordanAlgebra, def __init__(self, n, **kwargs): + # We know this is a valid EJA, but will double-check + # if the user passes check_axioms=True. + if "check_axioms" not in kwargs: kwargs["check_axioms"] = False + super(QuaternionHermitianEJA, self).__init__(self._denormalized_basis(n), self.jordan_product, self.trace_inner_product, **kwargs) + # TODO: this could be factored out somehow, but is left here + # because the MatrixEuclideanJordanAlgebra is not presently + # a subclass of the FDEJA class that defines rank() and one(). self.rank.set_cache(n) - # TODO: cache one()! + idV = matrix.identity(ZZ, self.dimension_over_reals()*n) + self.one.set_cache(self(idV)) + @staticmethod def _max_random_instance_size(): @@ -2279,18 +2296,20 @@ class HadamardEJA(ConcreteEuclideanJordanAlgebra): def inner_product(x,y): return x.inner_product(y) - # Don't orthonormalize because our basis is already - # orthonormal with respect to our inner-product. - if not 'orthonormalize' in kwargs: - kwargs['orthonormalize'] = False + # New defaults for keyword arguments. Don't orthonormalize + # because our basis is already orthonormal with respect to our + # inner-product. Don't check the axioms, because we know this + # is a valid EJA... but do double-check if the user passes + # check_axioms=True. Note: we DON'T override the "check_field" + # default here, because the user can pass in a field! + if "orthonormalize" not in kwargs: kwargs["orthonormalize"] = False + if "check_axioms" not in kwargs: kwargs["check_axioms"] = False + - # But also don't pass check_field=False here, because the user - # can pass in a field! standard_basis = FreeModule(ZZ, n).basis() super(HadamardEJA, self).__init__(standard_basis, jordan_product, inner_product, - check_axioms=False, **kwargs) self.rank.set_cache(n) @@ -2413,6 +2432,10 @@ class BilinearFormEJA(ConcreteEuclideanJordanAlgebra): zbar = y0*xbar + x0*ybar return P((z0,) + tuple(zbar)) + # We know this is a valid EJA, but will double-check + # if the user passes check_axioms=True. + if "check_axioms" not in kwargs: kwargs["check_axioms"] = False + n = B.nrows() standard_basis = FreeModule(ZZ, n).basis() super(BilinearFormEJA, self).__init__(standard_basis, @@ -2519,14 +2542,11 @@ class JordanSpinEJA(BilinearFormEJA): # Don't orthonormalize because our basis is already # orthonormal with respect to our inner-product. - if not 'orthonormalize' in kwargs: - kwargs['orthonormalize'] = False + if "orthonormalize" not in kwargs: kwargs["orthonormalize"] = False # But also don't pass check_field=False here, because the user # can pass in a field! - super(JordanSpinEJA, self).__init__(B, - check_axioms=False, - **kwargs) + super(JordanSpinEJA, self).__init__(B, **kwargs) @staticmethod def _max_random_instance_size(): @@ -2579,6 +2599,11 @@ class TrivialEJA(ConcreteEuclideanJordanAlgebra): jordan_product = lambda x,y: x inner_product = lambda x,y: 0 basis = () + + # New defaults for keyword arguments + if "orthonormalize" not in kwargs: kwargs["orthonormalize"] = False + if "check_axioms" not in kwargs: kwargs["check_axioms"] = False + super(TrivialEJA, self).__init__(basis, jordan_product, inner_product,