+ SETUP::
+
+ sage: from mjo.eja.eja_algebra import \
+ ....: QuaternionMatrixEuclideanJordanAlgebra
+
+ EXAMPLES::
+
+ sage: Q = QuaternionAlgebra(QQ,-1,-1)
+ sage: i,j,k = Q.gens()
+ sage: x = 1 + 2*i + 3*j + 4*k
+ sage: M = matrix(Q, 1, [[x]])
+ sage: QuaternionMatrixEuclideanJordanAlgebra.real_embed(M)
+ [ 1 2 3 4]
+ [-2 1 -4 3]
+ [-3 4 1 -2]
+ [-4 -3 2 1]
+
+ Embedding is a homomorphism (isomorphism, in fact)::
+
+ sage: set_random_seed()
+ sage: n_max = QuaternionMatrixEuclideanJordanAlgebra._max_test_case_size()
+ sage: n = ZZ.random_element(n_max)
+ sage: Q = QuaternionAlgebra(QQ,-1,-1)
+ sage: X = random_matrix(Q, n)
+ sage: Y = random_matrix(Q, n)
+ sage: Xe = QuaternionMatrixEuclideanJordanAlgebra.real_embed(X)
+ sage: Ye = QuaternionMatrixEuclideanJordanAlgebra.real_embed(Y)
+ sage: XYe = QuaternionMatrixEuclideanJordanAlgebra.real_embed(X*Y)
+ sage: Xe*Ye == XYe
+ True
+
+ """
+ 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()
+
+ blocks = []
+ for z in M.list():
+ t = z.coefficient_tuple()
+ a = t[0]
+ b = t[1]
+ c = t[2]
+ d = t[3]
+ cplxM = matrix(F, 2, [[ a + b*i, c + d*i],
+ [-c + d*i, a - b*i]])
+ realM = ComplexMatrixEuclideanJordanAlgebra.real_embed(cplxM)
+ blocks.append(realM)
+
+ # We should have real entries by now, so use the realest field
+ # we've got for the return value.
+ return matrix.block(quaternions.base_ring(), n, blocks)
+
+
+
+ @staticmethod
+ def real_unembed(M):
+ """
+ The inverse of _embed_quaternion_matrix().
+
+ SETUP::
+
+ sage: from mjo.eja.eja_algebra import \
+ ....: QuaternionMatrixEuclideanJordanAlgebra
+
+ EXAMPLES::
+
+ sage: M = matrix(QQ, [[ 1, 2, 3, 4],
+ ....: [-2, 1, -4, 3],
+ ....: [-3, 4, 1, -2],
+ ....: [-4, -3, 2, 1]])
+ sage: QuaternionMatrixEuclideanJordanAlgebra.real_unembed(M)
+ [1 + 2*i + 3*j + 4*k]
+
+ TESTS:
+
+ Unembedding is the inverse of embedding::
+
+ sage: set_random_seed()
+ sage: Q = QuaternionAlgebra(QQ, -1, -1)
+ sage: M = random_matrix(Q, 3)
+ sage: Me = QuaternionMatrixEuclideanJordanAlgebra.real_embed(M)
+ sage: QuaternionMatrixEuclideanJordanAlgebra.real_unembed(Me) == M
+ True
+
+ """
+ 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")
+
+ # Use the base ring of the matrix to ensure that its entries can be
+ # multiplied by elements of the quaternion algebra.
+ field = M.base_ring()
+ Q = QuaternionAlgebra(field,-1,-1)
+ i,j,k = Q.gens()
+
+ # Go top-left to bottom-right (reading order), converting every
+ # 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):
+ submat = ComplexMatrixEuclideanJordanAlgebra.real_unembed(
+ M[4*l:4*l+4,4*m:4*m+4] )
+ if submat[0,0] != submat[1,1].conjugate():
+ raise ValueError('bad on-diagonal submatrix')
+ if submat[0,1] != -submat[1,0].conjugate():
+ raise ValueError('bad off-diagonal submatrix')
+ z = submat[0,0].real()
+ z += submat[0,0].imag()*i
+ z += submat[0,1].real()*j
+ z += submat[0,1].imag()*k
+ elements.append(z)
+
+ return matrix(Q, n/4, elements)
+
+
+ @classmethod
+ def natural_inner_product(cls,X,Y):
+ """
+ Compute a natural 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.natural_representation()
+ sage: Ye = y.natural_representation()
+ sage: X = QuaternionHermitianEJA.real_unembed(Xe)
+ sage: Y = QuaternionHermitianEJA.real_unembed(Ye)
+ sage: expected = (X*Y).trace().coefficient_tuple()[0]
+ sage: actual = QuaternionHermitianEJA.natural_inner_product(Xe,Ye)
+ sage: actual == expected
+ True
+
+ """
+ return RealMatrixEuclideanJordanAlgebra.natural_inner_product(X,Y)/4
+
+
+class QuaternionHermitianEJA(QuaternionMatrixEuclideanJordanAlgebra):