2 Euclidean Jordan Algebras. These are formally-real Jordan Algebras;
3 specifically those where u^2 + v^2 = 0 implies that u = v = 0. They
4 are used in optimization, and have some additional nice methods beyond
5 what can be supported in a general Jordan Algebra.
8 from itertools
import repeat
10 from sage
.algebras
.quatalg
.quaternion_algebra
import QuaternionAlgebra
11 from sage
.categories
.magmatic_algebras
import MagmaticAlgebras
12 from sage
.combinat
.free_module
import CombinatorialFreeModule
13 from sage
.matrix
.constructor
import matrix
14 from sage
.matrix
.matrix_space
import MatrixSpace
15 from sage
.misc
.cachefunc
import cached_method
16 from sage
.misc
.lazy_import
import lazy_import
17 from sage
.misc
.prandom
import choice
18 from sage
.misc
.table
import table
19 from sage
.modules
.free_module
import FreeModule
, VectorSpace
20 from sage
.rings
.all
import (ZZ
, QQ
, RR
, RLF
, CLF
,
23 from mjo
.eja
.eja_element
import FiniteDimensionalEuclideanJordanAlgebraElement
24 lazy_import('mjo.eja.eja_subalgebra',
25 'FiniteDimensionalEuclideanJordanSubalgebra')
26 from mjo
.eja
.eja_utils
import _mat2vec
28 class FiniteDimensionalEuclideanJordanAlgebra(CombinatorialFreeModule
):
30 def _coerce_map_from_base_ring(self
):
32 Disable the map from the base ring into the algebra.
34 Performing a nonsense conversion like this automatically
35 is counterpedagogical. The fallback is to try the usual
36 element constructor, which should also fail.
40 sage: from mjo.eja.eja_algebra import random_eja
44 sage: set_random_seed()
45 sage: J = random_eja()
47 Traceback (most recent call last):
49 ValueError: not a naturally-represented algebra element
65 sage: from mjo.eja.eja_algebra import (JordanSpinEJA, random_eja)
69 By definition, Jordan multiplication commutes::
71 sage: set_random_seed()
72 sage: J = random_eja()
73 sage: x,y = J.random_elements(2)
79 The ``field`` we're given must be real::
81 sage: JordanSpinEJA(2,QQbar)
82 Traceback (most recent call last):
84 ValueError: field is not real
88 if not field
.is_subring(RR
):
89 # Note: this does return true for the real algebraic
90 # field, and any quadratic field where we've specified
92 raise ValueError('field is not real')
95 self
._natural
_basis
= natural_basis
98 category
= MagmaticAlgebras(field
).FiniteDimensional()
99 category
= category
.WithBasis().Unital()
101 fda
= super(FiniteDimensionalEuclideanJordanAlgebra
, self
)
103 range(len(mult_table
)),
106 self
.print_options(bracket
='')
108 # The multiplication table we're given is necessarily in terms
109 # of vectors, because we don't have an algebra yet for
110 # anything to be an element of. However, it's faster in the
111 # long run to have the multiplication table be in terms of
112 # algebra elements. We do this after calling the superclass
113 # constructor so that from_vector() knows what to do.
114 self
._multiplication
_table
= [
115 list(map(lambda x
: self
.from_vector(x
), ls
))
120 def _element_constructor_(self
, elt
):
122 Construct an element of this algebra from its natural
125 This gets called only after the parent element _call_ method
126 fails to find a coercion for the argument.
130 sage: from mjo.eja.eja_algebra import (JordanSpinEJA,
132 ....: RealSymmetricEJA)
136 The identity in `S^n` is converted to the identity in the EJA::
138 sage: J = RealSymmetricEJA(3)
139 sage: I = matrix.identity(QQ,3)
140 sage: J(I) == J.one()
143 This skew-symmetric matrix can't be represented in the EJA::
145 sage: J = RealSymmetricEJA(3)
146 sage: A = matrix(QQ,3, lambda i,j: i-j)
148 Traceback (most recent call last):
150 ArithmeticError: vector is not in free module
154 Ensure that we can convert any element of the two non-matrix
155 simple algebras (whose natural representations are their usual
156 vector representations) back and forth faithfully::
158 sage: set_random_seed()
159 sage: J = HadamardEJA.random_instance()
160 sage: x = J.random_element()
161 sage: J(x.to_vector().column()) == x
163 sage: J = JordanSpinEJA.random_instance()
164 sage: x = J.random_element()
165 sage: J(x.to_vector().column()) == x
169 msg
= "not a naturally-represented algebra element"
171 # The superclass implementation of random_element()
172 # needs to be able to coerce "0" into the algebra.
174 elif elt
in self
.base_ring():
175 # Ensure that no base ring -> algebra coercion is performed
176 # by this method. There's some stupidity in sage that would
177 # otherwise propagate to this method; for example, sage thinks
178 # that the integer 3 belongs to the space of 2-by-2 matrices.
179 raise ValueError(msg
)
181 natural_basis
= self
.natural_basis()
182 basis_space
= natural_basis
[0].matrix_space()
183 if elt
not in basis_space
:
184 raise ValueError(msg
)
186 # Thanks for nothing! Matrix spaces aren't vector spaces in
187 # Sage, so we have to figure out its natural-basis coordinates
188 # ourselves. We use the basis space's ring instead of the
189 # element's ring because the basis space might be an algebraic
190 # closure whereas the base ring of the 3-by-3 identity matrix
191 # could be QQ instead of QQbar.
192 V
= VectorSpace(basis_space
.base_ring(), elt
.nrows()*elt
.ncols())
193 W
= V
.span_of_basis( _mat2vec(s
) for s
in natural_basis
)
194 coords
= W
.coordinate_vector(_mat2vec(elt
))
195 return self
.from_vector(coords
)
200 Return a string representation of ``self``.
204 sage: from mjo.eja.eja_algebra import JordanSpinEJA
208 Ensure that it says what we think it says::
210 sage: JordanSpinEJA(2, field=QQ)
211 Euclidean Jordan algebra of dimension 2 over Rational Field
212 sage: JordanSpinEJA(3, field=RDF)
213 Euclidean Jordan algebra of dimension 3 over Real Double Field
216 fmt
= "Euclidean Jordan algebra of dimension {} over {}"
217 return fmt
.format(self
.dimension(), self
.base_ring())
219 def product_on_basis(self
, i
, j
):
220 return self
._multiplication
_table
[i
][j
]
222 def _a_regular_element(self
):
224 Guess a regular element. Needed to compute the basis for our
225 characteristic polynomial coefficients.
229 sage: from mjo.eja.eja_algebra import random_eja
233 Ensure that this hacky method succeeds for every algebra that we
234 know how to construct::
236 sage: set_random_seed()
237 sage: J = random_eja()
238 sage: J._a_regular_element().is_regular()
243 z
= self
.sum( (i
+1)*gs
[i
] for i
in range(len(gs
)) )
244 if not z
.is_regular():
245 raise ValueError("don't know a regular element")
250 def _charpoly_basis_space(self
):
252 Return the vector space spanned by the basis used in our
253 characteristic polynomial coefficients. This is used not only to
254 compute those coefficients, but also any time we need to
255 evaluate the coefficients (like when we compute the trace or
258 z
= self
._a
_regular
_element
()
259 # Don't use the parent vector space directly here in case this
260 # happens to be a subalgebra. In that case, we would be e.g.
261 # two-dimensional but span_of_basis() would expect three
263 V
= VectorSpace(self
.base_ring(), self
.vector_space().dimension())
264 basis
= [ (z
**k
).to_vector() for k
in range(self
.rank()) ]
265 V1
= V
.span_of_basis( basis
)
266 b
= (V1
.basis() + V1
.complement().basis())
267 return V
.span_of_basis(b
)
272 def _charpoly_coeff(self
, i
):
274 Return the coefficient polynomial "a_{i}" of this algebra's
275 general characteristic polynomial.
277 Having this be a separate cached method lets us compute and
278 store the trace/determinant (a_{r-1} and a_{0} respectively)
279 separate from the entire characteristic polynomial.
281 (A_of_x
, x
, xr
, detA
) = self
._charpoly
_matrix
_system
()
282 R
= A_of_x
.base_ring()
287 # Guaranteed by theory
290 # Danger: the in-place modification is done for performance
291 # reasons (reconstructing a matrix with huge polynomial
292 # entries is slow), but I don't know how cached_method works,
293 # so it's highly possible that we're modifying some global
294 # list variable by reference, here. In other words, you
295 # probably shouldn't call this method twice on the same
296 # algebra, at the same time, in two threads
297 Ai_orig
= A_of_x
.column(i
)
298 A_of_x
.set_column(i
,xr
)
299 numerator
= A_of_x
.det()
300 A_of_x
.set_column(i
,Ai_orig
)
302 # We're relying on the theory here to ensure that each a_i is
303 # indeed back in R, and the added negative signs are to make
304 # the whole charpoly expression sum to zero.
305 return R(-numerator
/detA
)
309 def _charpoly_matrix_system(self
):
311 Compute the matrix whose entries A_ij are polynomials in
312 X1,...,XN, the vector ``x`` of variables X1,...,XN, the vector
313 corresponding to `x^r` and the determinent of the matrix A =
314 [A_ij]. In other words, all of the fixed (cachable) data needed
315 to compute the coefficients of the characteristic polynomial.
320 # Turn my vector space into a module so that "vectors" can
321 # have multivatiate polynomial entries.
322 names
= tuple('X' + str(i
) for i
in range(1,n
+1))
323 R
= PolynomialRing(self
.base_ring(), names
)
325 # Using change_ring() on the parent's vector space doesn't work
326 # here because, in a subalgebra, that vector space has a basis
327 # and change_ring() tries to bring the basis along with it. And
328 # that doesn't work unless the new ring is a PID, which it usually
332 # Now let x = (X1,X2,...,Xn) be the vector whose entries are
336 # And figure out the "left multiplication by x" matrix in
339 monomial_matrices
= [ self
.monomial(i
).operator().matrix()
340 for i
in range(n
) ] # don't recompute these!
342 ek
= self
.monomial(k
).to_vector()
344 sum( x
[i
]*(monomial_matrices
[i
]*ek
)
345 for i
in range(n
) ) )
346 Lx
= matrix
.column(R
, lmbx_cols
)
348 # Now we can compute powers of x "symbolically"
349 x_powers
= [self
.one().to_vector(), x
]
350 for d
in range(2, r
+1):
351 x_powers
.append( Lx
*(x_powers
[-1]) )
353 idmat
= matrix
.identity(R
, n
)
355 W
= self
._charpoly
_basis
_space
()
356 W
= W
.change_ring(R
.fraction_field())
358 # Starting with the standard coordinates x = (X1,X2,...,Xn)
359 # and then converting the entries to W-coordinates allows us
360 # to pass in the standard coordinates to the charpoly and get
361 # back the right answer. Specifically, with x = (X1,X2,...,Xn),
364 # W.coordinates(x^2) eval'd at (standard z-coords)
368 # W-coords of (standard coords of x^2 eval'd at std-coords of z)
370 # We want the middle equivalent thing in our matrix, but use
371 # the first equivalent thing instead so that we can pass in
372 # standard coordinates.
373 x_powers
= [ W
.coordinate_vector(xp
) for xp
in x_powers
]
374 l2
= [idmat
.column(k
-1) for k
in range(r
+1, n
+1)]
375 A_of_x
= matrix
.column(R
, n
, (x_powers
[:r
] + l2
))
376 return (A_of_x
, x
, x_powers
[r
], A_of_x
.det())
380 def characteristic_polynomial(self
):
382 Return a characteristic polynomial that works for all elements
385 The resulting polynomial has `n+1` variables, where `n` is the
386 dimension of this algebra. The first `n` variables correspond to
387 the coordinates of an algebra element: when evaluated at the
388 coordinates of an algebra element with respect to a certain
389 basis, the result is a univariate polynomial (in the one
390 remaining variable ``t``), namely the characteristic polynomial
395 sage: from mjo.eja.eja_algebra import JordanSpinEJA, TrivialEJA
399 The characteristic polynomial in the spin algebra is given in
400 Alizadeh, Example 11.11::
402 sage: J = JordanSpinEJA(3)
403 sage: p = J.characteristic_polynomial(); p
404 X1^2 - X2^2 - X3^2 + (-2*t)*X1 + t^2
405 sage: xvec = J.one().to_vector()
409 By definition, the characteristic polynomial is a monic
410 degree-zero polynomial in a rank-zero algebra. Note that
411 Cayley-Hamilton is indeed satisfied since the polynomial
412 ``1`` evaluates to the identity element of the algebra on
415 sage: J = TrivialEJA()
416 sage: J.characteristic_polynomial()
423 # The list of coefficient polynomials a_0, a_1, a_2, ..., a_n.
424 a
= [ self
._charpoly
_coeff
(i
) for i
in range(r
+1) ]
426 # We go to a bit of trouble here to reorder the
427 # indeterminates, so that it's easier to evaluate the
428 # characteristic polynomial at x's coordinates and get back
429 # something in terms of t, which is what we want.
431 S
= PolynomialRing(self
.base_ring(),'t')
433 S
= PolynomialRing(S
, R
.variable_names())
436 return sum( a
[k
]*(t
**k
) for k
in range(len(a
)) )
439 def inner_product(self
, x
, y
):
441 The inner product associated with this Euclidean Jordan algebra.
443 Defaults to the trace inner product, but can be overridden by
444 subclasses if they are sure that the necessary properties are
449 sage: from mjo.eja.eja_algebra import random_eja
453 Our inner product is "associative," which means the following for
454 a symmetric bilinear form::
456 sage: set_random_seed()
457 sage: J = random_eja()
458 sage: x,y,z = J.random_elements(3)
459 sage: (x*y).inner_product(z) == y.inner_product(x*z)
463 X
= x
.natural_representation()
464 Y
= y
.natural_representation()
465 return self
.natural_inner_product(X
,Y
)
468 def is_trivial(self
):
470 Return whether or not this algebra is trivial.
472 A trivial algebra contains only the zero element.
476 sage: from mjo.eja.eja_algebra import (ComplexHermitianEJA,
481 sage: J = ComplexHermitianEJA(3)
487 sage: J = TrivialEJA()
492 return self
.dimension() == 0
495 def multiplication_table(self
):
497 Return a visual representation of this algebra's multiplication
498 table (on basis elements).
502 sage: from mjo.eja.eja_algebra import JordanSpinEJA
506 sage: J = JordanSpinEJA(4)
507 sage: J.multiplication_table()
508 +----++----+----+----+----+
509 | * || e0 | e1 | e2 | e3 |
510 +====++====+====+====+====+
511 | e0 || e0 | e1 | e2 | e3 |
512 +----++----+----+----+----+
513 | e1 || e1 | e0 | 0 | 0 |
514 +----++----+----+----+----+
515 | e2 || e2 | 0 | e0 | 0 |
516 +----++----+----+----+----+
517 | e3 || e3 | 0 | 0 | e0 |
518 +----++----+----+----+----+
521 M
= list(self
._multiplication
_table
) # copy
522 for i
in range(len(M
)):
523 # M had better be "square"
524 M
[i
] = [self
.monomial(i
)] + M
[i
]
525 M
= [["*"] + list(self
.gens())] + M
526 return table(M
, header_row
=True, header_column
=True, frame
=True)
529 def natural_basis(self
):
531 Return a more-natural representation of this algebra's basis.
533 Every finite-dimensional Euclidean Jordan Algebra is a direct
534 sum of five simple algebras, four of which comprise Hermitian
535 matrices. This method returns the original "natural" basis
536 for our underlying vector space. (Typically, the natural basis
537 is used to construct the multiplication table in the first place.)
539 Note that this will always return a matrix. The standard basis
540 in `R^n` will be returned as `n`-by-`1` column matrices.
544 sage: from mjo.eja.eja_algebra import (JordanSpinEJA,
545 ....: RealSymmetricEJA)
549 sage: J = RealSymmetricEJA(2)
551 Finite family {0: e0, 1: e1, 2: e2}
552 sage: J.natural_basis()
554 [1 0] [ 0 1/2*sqrt2] [0 0]
555 [0 0], [1/2*sqrt2 0], [0 1]
560 sage: J = JordanSpinEJA(2)
562 Finite family {0: e0, 1: e1}
563 sage: J.natural_basis()
570 if self
._natural
_basis
is None:
571 M
= self
.natural_basis_space()
572 return tuple( M(b
.to_vector()) for b
in self
.basis() )
574 return self
._natural
_basis
577 def natural_basis_space(self
):
579 Return the matrix space in which this algebra's natural basis
582 if self
._natural
_basis
is None or len(self
._natural
_basis
) == 0:
583 return MatrixSpace(self
.base_ring(), self
.dimension(), 1)
585 return self
._natural
_basis
[0].matrix_space()
589 def natural_inner_product(X
,Y
):
591 Compute the inner product of two naturally-represented elements.
593 For example in the real symmetric matrix EJA, this will compute
594 the trace inner-product of two n-by-n symmetric matrices. The
595 default should work for the real cartesian product EJA, the
596 Jordan spin EJA, and the real symmetric matrices. The others
597 will have to be overridden.
599 return (X
.conjugate_transpose()*Y
).trace()
605 Return the unit element of this algebra.
609 sage: from mjo.eja.eja_algebra import (HadamardEJA,
614 sage: J = HadamardEJA(5)
616 e0 + e1 + e2 + e3 + e4
620 The identity element acts like the identity::
622 sage: set_random_seed()
623 sage: J = random_eja()
624 sage: x = J.random_element()
625 sage: J.one()*x == x and x*J.one() == x
628 The matrix of the unit element's operator is the identity::
630 sage: set_random_seed()
631 sage: J = random_eja()
632 sage: actual = J.one().operator().matrix()
633 sage: expected = matrix.identity(J.base_ring(), J.dimension())
634 sage: actual == expected
638 # We can brute-force compute the matrices of the operators
639 # that correspond to the basis elements of this algebra.
640 # If some linear combination of those basis elements is the
641 # algebra identity, then the same linear combination of
642 # their matrices has to be the identity matrix.
644 # Of course, matrices aren't vectors in sage, so we have to
645 # appeal to the "long vectors" isometry.
646 oper_vecs
= [ _mat2vec(g
.operator().matrix()) for g
in self
.gens() ]
648 # Now we use basis linear algebra to find the coefficients,
649 # of the matrices-as-vectors-linear-combination, which should
650 # work for the original algebra basis too.
651 A
= matrix
.column(self
.base_ring(), oper_vecs
)
653 # We used the isometry on the left-hand side already, but we
654 # still need to do it for the right-hand side. Recall that we
655 # wanted something that summed to the identity matrix.
656 b
= _mat2vec( matrix
.identity(self
.base_ring(), self
.dimension()) )
658 # Now if there's an identity element in the algebra, this should work.
659 coeffs
= A
.solve_right(b
)
660 return self
.linear_combination(zip(self
.gens(), coeffs
))
663 def peirce_decomposition(self
, c
):
665 The Peirce decomposition of this algebra relative to the
668 In the future, this can be extended to a complete system of
669 orthogonal idempotents.
673 - ``c`` -- an idempotent of this algebra.
677 A triple (J0, J5, J1) containing two subalgebras and one subspace
680 - ``J0`` -- the algebra on the eigenspace of ``c.operator()``
681 corresponding to the eigenvalue zero.
683 - ``J5`` -- the eigenspace (NOT a subalgebra) of ``c.operator()``
684 corresponding to the eigenvalue one-half.
686 - ``J1`` -- the algebra on the eigenspace of ``c.operator()``
687 corresponding to the eigenvalue one.
689 These are the only possible eigenspaces for that operator, and this
690 algebra is a direct sum of them. The spaces ``J0`` and ``J1`` are
691 orthogonal, and are subalgebras of this algebra with the appropriate
696 sage: from mjo.eja.eja_algebra import random_eja, RealSymmetricEJA
700 The canonical example comes from the symmetric matrices, which
701 decompose into diagonal and off-diagonal parts::
703 sage: J = RealSymmetricEJA(3)
704 sage: C = matrix(QQ, [ [1,0,0],
708 sage: J0,J5,J1 = J.peirce_decomposition(c)
710 Euclidean Jordan algebra of dimension 1...
712 Vector space of degree 6 and dimension 2...
714 Euclidean Jordan algebra of dimension 3...
718 Every algebra decomposes trivially with respect to its identity
721 sage: set_random_seed()
722 sage: J = random_eja()
723 sage: J0,J5,J1 = J.peirce_decomposition(J.one())
724 sage: J0.dimension() == 0 and J5.dimension() == 0
726 sage: J1.superalgebra() == J and J1.dimension() == J.dimension()
729 The identity elements in the two subalgebras are the
730 projections onto their respective subspaces of the
731 superalgebra's identity element::
733 sage: set_random_seed()
734 sage: J = random_eja()
735 sage: x = J.random_element()
736 sage: if not J.is_trivial():
737 ....: while x.is_nilpotent():
738 ....: x = J.random_element()
739 sage: c = x.subalgebra_idempotent()
740 sage: J0,J5,J1 = J.peirce_decomposition(c)
741 sage: J1(c) == J1.one()
743 sage: J0(J.one() - c) == J0.one()
747 if not c
.is_idempotent():
748 raise ValueError("element is not idempotent: %s" % c
)
750 # Default these to what they should be if they turn out to be
751 # trivial, because eigenspaces_left() won't return eigenvalues
752 # corresponding to trivial spaces (e.g. it returns only the
753 # eigenspace corresponding to lambda=1 if you take the
754 # decomposition relative to the identity element).
755 trivial
= FiniteDimensionalEuclideanJordanSubalgebra(self
, ())
756 J0
= trivial
# eigenvalue zero
757 J5
= VectorSpace(self
.base_ring(), 0) # eigenvalue one-half
758 J1
= trivial
# eigenvalue one
760 for (eigval
, eigspace
) in c
.operator().matrix().left_eigenspaces():
761 if eigval
== ~
(self
.base_ring()(2)):
764 gens
= tuple( self
.from_vector(b
) for b
in eigspace
.basis() )
765 subalg
= FiniteDimensionalEuclideanJordanSubalgebra(self
, gens
)
771 raise ValueError("unexpected eigenvalue: %s" % eigval
)
776 def orthogonal_idempotents(self
):
778 Generate a set of `r` orthogonal idempotents for this algebra,
779 where `r` is its rank.
781 This implementation is based on the so-called "central
782 orthogonal idempotents" implemented for (semisimple) centers
783 of SageMath ``FiniteDimensionalAlgebrasWithBasis``. Since all
784 Euclidean Jordan algebas are commutative (and thus equal to
785 their own centers) and semisimple, the method should work more
786 or less as implemented, if it ever worked in the first place.
787 (I don't know the justification for the original implementation.
790 How it works: we loop through the algebras generators, looking
791 for their eigenspaces. If there's more than one eigenspace,
792 and if they result in more than one subalgebra, then we split
793 those subalgebras recursively until we get to subalgebras of
794 dimension one (whose idempotent is the unit element). Why does
795 some generator have to produce at least two subalgebras? I
796 dunno. But it seems to work.
798 Beware that Koecher defines the "center" of a Jordan algebra to
799 be something else, because the usual definition is stupid in a
800 (necessarily commutative) Jordan algebra.
802 if self
.dimension() == 1:
805 for g
in self
.gens():
806 eigenpairs
= g
.operator().matrix().right_eigenspaces()
807 if len(eigenpairs
) >= 2:
809 for eigval
, eigspace
in eigenpairs
:
810 # Make sub-EJAs from the matrix eigenspaces...
811 sb
= tuple( self
.from_vector(b
) for b
in eigspace
.basis() )
813 # This will fail if e.g. the eigenspace basis
814 # contains two elements and their product
815 # isn't a linear combination of the two of
816 # them (i.e. the generated EJA isn't actually
818 s
= FiniteDimensionalEuclideanJordanSubalgebra(self
, sb
)
819 subalgebras
.append(s
)
822 if len(subalgebras
) >= 2:
823 # apply this method recursively.
824 return tuple( c
.superalgebra_element()
825 for subalgebra
in subalgebras
826 for c
in subalgebra
.orthogonal_idempotents() )
828 # If we got here, the algebra didn't decompose, at least not when we looked at
829 # the eigenspaces corresponding only to basis elements of the algebra. The
830 # implementation I stole says that this should work because of Schur's Lemma,
831 # so I personally blame Schur's Lemma if it does not.
832 raise Exception("Schur's Lemma didn't work!")
835 def random_elements(self
, count
):
837 Return ``count`` random elements as a tuple.
841 sage: from mjo.eja.eja_algebra import JordanSpinEJA
845 sage: J = JordanSpinEJA(3)
846 sage: x,y,z = J.random_elements(3)
847 sage: all( [ x in J, y in J, z in J ])
849 sage: len( J.random_elements(10) ) == 10
853 return tuple( self
.random_element() for idx
in range(count
) )
858 Return the rank of this EJA.
862 The author knows of no algorithm to compute the rank of an EJA
863 where only the multiplication table is known. In lieu of one, we
864 require the rank to be specified when the algebra is created,
865 and simply pass along that number here.
869 sage: from mjo.eja.eja_algebra import (JordanSpinEJA,
870 ....: RealSymmetricEJA,
871 ....: ComplexHermitianEJA,
872 ....: QuaternionHermitianEJA,
877 The rank of the Jordan spin algebra is always two::
879 sage: JordanSpinEJA(2).rank()
881 sage: JordanSpinEJA(3).rank()
883 sage: JordanSpinEJA(4).rank()
886 The rank of the `n`-by-`n` Hermitian real, complex, or
887 quaternion matrices is `n`::
889 sage: RealSymmetricEJA(4).rank()
891 sage: ComplexHermitianEJA(3).rank()
893 sage: QuaternionHermitianEJA(2).rank()
898 Ensure that every EJA that we know how to construct has a
899 positive integer rank, unless the algebra is trivial in
900 which case its rank will be zero::
902 sage: set_random_seed()
903 sage: J = random_eja()
907 sage: r > 0 or (r == 0 and J.is_trivial())
914 def vector_space(self
):
916 Return the vector space that underlies this algebra.
920 sage: from mjo.eja.eja_algebra import RealSymmetricEJA
924 sage: J = RealSymmetricEJA(2)
925 sage: J.vector_space()
926 Vector space of dimension 3 over...
929 return self
.zero().to_vector().parent().ambient_vector_space()
932 Element
= FiniteDimensionalEuclideanJordanAlgebraElement
935 class KnownRankEJA(object):
937 A class for algebras that we actually know we can construct. The
938 main issue is that, for most of our methods to make sense, we need
939 to know the rank of our algebra. Thus we can't simply generate a
940 "random" algebra, or even check that a given basis and product
941 satisfy the axioms; because even if everything looks OK, we wouldn't
942 know the rank we need to actuallty build the thing.
944 Not really a subclass of FDEJA because doing that causes method
945 resolution errors, e.g.
947 TypeError: Error when calling the metaclass bases
948 Cannot create a consistent method resolution
949 order (MRO) for bases FiniteDimensionalEuclideanJordanAlgebra,
954 def _max_test_case_size():
956 Return an integer "size" that is an upper bound on the size of
957 this algebra when it is used in a random test
958 case. Unfortunately, the term "size" is quite vague -- when
959 dealing with `R^n` under either the Hadamard or Jordan spin
960 product, the "size" refers to the dimension `n`. When dealing
961 with a matrix algebra (real symmetric or complex/quaternion
962 Hermitian), it refers to the size of the matrix, which is
963 far less than the dimension of the underlying vector space.
965 We default to five in this class, which is safe in `R^n`. The
966 matrix algebra subclasses (or any class where the "size" is
967 interpreted to be far less than the dimension) should override
968 with a smaller number.
973 def random_instance(cls
, field
=QQ
, **kwargs
):
975 Return a random instance of this type of algebra.
977 Beware, this will crash for "most instances" because the
978 constructor below looks wrong.
980 if cls
is TrivialEJA
:
981 # The TrivialEJA class doesn't take an "n" argument because
985 n
= ZZ
.random_element(cls
._max
_test
_case
_size
()) + 1
986 return cls(n
, field
, **kwargs
)
989 class HadamardEJA(FiniteDimensionalEuclideanJordanAlgebra
, KnownRankEJA
):
991 Return the Euclidean Jordan Algebra corresponding to the set
992 `R^n` under the Hadamard product.
994 Note: this is nothing more than the Cartesian product of ``n``
995 copies of the spin algebra. Once Cartesian product algebras
996 are implemented, this can go.
1000 sage: from mjo.eja.eja_algebra import HadamardEJA
1004 This multiplication table can be verified by hand::
1006 sage: J = HadamardEJA(3)
1007 sage: e0,e1,e2 = J.gens()
1023 We can change the generator prefix::
1025 sage: HadamardEJA(3, prefix='r').gens()
1029 def __init__(self
, n
, field
=QQ
, **kwargs
):
1030 V
= VectorSpace(field
, n
)
1031 mult_table
= [ [ V
.gen(i
)*(i
== j
) for j
in range(n
) ]
1034 fdeja
= super(HadamardEJA
, self
)
1035 return fdeja
.__init
__(field
, mult_table
, rank
=n
, **kwargs
)
1037 def inner_product(self
, x
, y
):
1039 Faster to reimplement than to use natural representations.
1043 sage: from mjo.eja.eja_algebra import HadamardEJA
1047 Ensure that this is the usual inner product for the algebras
1050 sage: set_random_seed()
1051 sage: J = HadamardEJA.random_instance()
1052 sage: x,y = J.random_elements(2)
1053 sage: X = x.natural_representation()
1054 sage: Y = y.natural_representation()
1055 sage: x.inner_product(y) == J.natural_inner_product(X,Y)
1059 return x
.to_vector().inner_product(y
.to_vector())
1062 def random_eja(field
=QQ
, nontrivial
=False):
1064 Return a "random" finite-dimensional Euclidean Jordan Algebra.
1068 sage: from mjo.eja.eja_algebra import random_eja
1073 Euclidean Jordan algebra of dimension...
1076 eja_classes
= KnownRankEJA
.__subclasses
__()
1078 eja_classes
.remove(TrivialEJA
)
1079 classname
= choice(eja_classes
)
1080 return classname
.random_instance(field
=field
)
1087 class MatrixEuclideanJordanAlgebra(FiniteDimensionalEuclideanJordanAlgebra
):
1089 def _max_test_case_size():
1090 # Play it safe, since this will be squared and the underlying
1091 # field can have dimension 4 (quaternions) too.
1094 def __init__(self
, field
, basis
, rank
, normalize_basis
=True, **kwargs
):
1096 Compared to the superclass constructor, we take a basis instead of
1097 a multiplication table because the latter can be computed in terms
1098 of the former when the product is known (like it is here).
1100 # Used in this class's fast _charpoly_coeff() override.
1101 self
._basis
_normalizers
= None
1103 # We're going to loop through this a few times, so now's a good
1104 # time to ensure that it isn't a generator expression.
1105 basis
= tuple(basis
)
1107 if rank
> 1 and normalize_basis
:
1108 # We'll need sqrt(2) to normalize the basis, and this
1109 # winds up in the multiplication table, so the whole
1110 # algebra needs to be over the field extension.
1111 R
= PolynomialRing(field
, 'z')
1114 if p
.is_irreducible():
1115 field
= field
.extension(p
, 'sqrt2', embedding
=RLF(2).sqrt())
1116 basis
= tuple( s
.change_ring(field
) for s
in basis
)
1117 self
._basis
_normalizers
= tuple(
1118 ~
(self
.natural_inner_product(s
,s
).sqrt()) for s
in basis
)
1119 basis
= tuple(s
*c
for (s
,c
) in zip(basis
,self
._basis
_normalizers
))
1121 Qs
= self
.multiplication_table_from_matrix_basis(basis
)
1123 fdeja
= super(MatrixEuclideanJordanAlgebra
, self
)
1124 return fdeja
.__init
__(field
,
1127 natural_basis
=basis
,
1132 def _charpoly_coeff(self
, i
):
1134 Override the parent method with something that tries to compute
1135 over a faster (non-extension) field.
1137 if self
._basis
_normalizers
is None:
1138 # We didn't normalize, so assume that the basis we started
1139 # with had entries in a nice field.
1140 return super(MatrixEuclideanJordanAlgebra
, self
)._charpoly
_coeff
(i
)
1142 basis
= ( (b
/n
) for (b
,n
) in zip(self
.natural_basis(),
1143 self
._basis
_normalizers
) )
1145 # Do this over the rationals and convert back at the end.
1146 J
= MatrixEuclideanJordanAlgebra(QQ
,
1149 normalize_basis
=False)
1150 (_
,x
,_
,_
) = J
._charpoly
_matrix
_system
()
1151 p
= J
._charpoly
_coeff
(i
)
1152 # p might be missing some vars, have to substitute "optionally"
1153 pairs
= zip(x
.base_ring().gens(), self
._basis
_normalizers
)
1154 substitutions
= { v: v*c for (v,c) in pairs }
1155 result
= p
.subs(substitutions
)
1157 # The result of "subs" can be either a coefficient-ring
1158 # element or a polynomial. Gotta handle both cases.
1160 return self
.base_ring()(result
)
1162 return result
.change_ring(self
.base_ring())
1166 def multiplication_table_from_matrix_basis(basis
):
1168 At least three of the five simple Euclidean Jordan algebras have the
1169 symmetric multiplication (A,B) |-> (AB + BA)/2, where the
1170 multiplication on the right is matrix multiplication. Given a basis
1171 for the underlying matrix space, this function returns a
1172 multiplication table (obtained by looping through the basis
1173 elements) for an algebra of those matrices.
1175 # In S^2, for example, we nominally have four coordinates even
1176 # though the space is of dimension three only. The vector space V
1177 # is supposed to hold the entire long vector, and the subspace W
1178 # of V will be spanned by the vectors that arise from symmetric
1179 # matrices. Thus for S^2, dim(V) == 4 and dim(W) == 3.
1180 field
= basis
[0].base_ring()
1181 dimension
= basis
[0].nrows()
1183 V
= VectorSpace(field
, dimension
**2)
1184 W
= V
.span_of_basis( _mat2vec(s
) for s
in basis
)
1186 mult_table
= [[W
.zero() for j
in range(n
)] for i
in range(n
)]
1189 mat_entry
= (basis
[i
]*basis
[j
] + basis
[j
]*basis
[i
])/2
1190 mult_table
[i
][j
] = W
.coordinate_vector(_mat2vec(mat_entry
))
1198 Embed the matrix ``M`` into a space of real matrices.
1200 The matrix ``M`` can have entries in any field at the moment:
1201 the real numbers, complex numbers, or quaternions. And although
1202 they are not a field, we can probably support octonions at some
1203 point, too. This function returns a real matrix that "acts like"
1204 the original with respect to matrix multiplication; i.e.
1206 real_embed(M*N) = real_embed(M)*real_embed(N)
1209 raise NotImplementedError
1213 def real_unembed(M
):
1215 The inverse of :meth:`real_embed`.
1217 raise NotImplementedError
1221 def natural_inner_product(cls
,X
,Y
):
1222 Xu
= cls
.real_unembed(X
)
1223 Yu
= cls
.real_unembed(Y
)
1224 tr
= (Xu
*Yu
).trace()
1227 # It's real already.
1230 # Otherwise, try the thing that works for complex numbers; and
1231 # if that doesn't work, the thing that works for quaternions.
1233 return tr
.vector()[0] # real part, imag part is index 1
1234 except AttributeError:
1235 # A quaternions doesn't have a vector() method, but does
1236 # have coefficient_tuple() method that returns the
1237 # coefficients of 1, i, j, and k -- in that order.
1238 return tr
.coefficient_tuple()[0]
1241 class RealMatrixEuclideanJordanAlgebra(MatrixEuclideanJordanAlgebra
):
1245 The identity function, for embedding real matrices into real
1251 def real_unembed(M
):
1253 The identity function, for unembedding real matrices from real
1259 class RealSymmetricEJA(RealMatrixEuclideanJordanAlgebra
, KnownRankEJA
):
1261 The rank-n simple EJA consisting of real symmetric n-by-n
1262 matrices, the usual symmetric Jordan product, and the trace inner
1263 product. It has dimension `(n^2 + n)/2` over the reals.
1267 sage: from mjo.eja.eja_algebra import RealSymmetricEJA
1271 sage: J = RealSymmetricEJA(2)
1272 sage: e0, e1, e2 = J.gens()
1280 In theory, our "field" can be any subfield of the reals::
1282 sage: RealSymmetricEJA(2, AA)
1283 Euclidean Jordan algebra of dimension 3 over Algebraic Real Field
1284 sage: RealSymmetricEJA(2, RR)
1285 Euclidean Jordan algebra of dimension 3 over Real Field with
1286 53 bits of precision
1290 The dimension of this algebra is `(n^2 + n) / 2`::
1292 sage: set_random_seed()
1293 sage: n_max = RealSymmetricEJA._max_test_case_size()
1294 sage: n = ZZ.random_element(1, n_max)
1295 sage: J = RealSymmetricEJA(n)
1296 sage: J.dimension() == (n^2 + n)/2
1299 The Jordan multiplication is what we think it is::
1301 sage: set_random_seed()
1302 sage: J = RealSymmetricEJA.random_instance()
1303 sage: x,y = J.random_elements(2)
1304 sage: actual = (x*y).natural_representation()
1305 sage: X = x.natural_representation()
1306 sage: Y = y.natural_representation()
1307 sage: expected = (X*Y + Y*X)/2
1308 sage: actual == expected
1310 sage: J(expected) == x*y
1313 We can change the generator prefix::
1315 sage: RealSymmetricEJA(3, prefix='q').gens()
1316 (q0, q1, q2, q3, q4, q5)
1318 Our natural basis is normalized with respect to the natural inner
1319 product unless we specify otherwise::
1321 sage: set_random_seed()
1322 sage: J = RealSymmetricEJA.random_instance()
1323 sage: all( b.norm() == 1 for b in J.gens() )
1326 Since our natural basis is normalized with respect to the natural
1327 inner product, and since we know that this algebra is an EJA, any
1328 left-multiplication operator's matrix will be symmetric because
1329 natural->EJA basis representation is an isometry and within the EJA
1330 the operator is self-adjoint by the Jordan axiom::
1332 sage: set_random_seed()
1333 sage: x = RealSymmetricEJA.random_instance().random_element()
1334 sage: x.operator().matrix().is_symmetric()
1339 def _denormalized_basis(cls
, n
, field
):
1341 Return a basis for the space of real symmetric n-by-n matrices.
1345 sage: from mjo.eja.eja_algebra import RealSymmetricEJA
1349 sage: set_random_seed()
1350 sage: n = ZZ.random_element(1,5)
1351 sage: B = RealSymmetricEJA._denormalized_basis(n,QQ)
1352 sage: all( M.is_symmetric() for M in B)
1356 # The basis of symmetric matrices, as matrices, in their R^(n-by-n)
1360 for j
in range(i
+1):
1361 Eij
= matrix(field
, n
, lambda k
,l
: k
==i
and l
==j
)
1365 Sij
= Eij
+ Eij
.transpose()
1371 def _max_test_case_size():
1372 return 4 # Dimension 10
1375 def __init__(self
, n
, field
=QQ
, **kwargs
):
1376 basis
= self
._denormalized
_basis
(n
, field
)
1377 super(RealSymmetricEJA
, self
).__init
__(field
, basis
, n
, **kwargs
)
1380 class ComplexMatrixEuclideanJordanAlgebra(MatrixEuclideanJordanAlgebra
):
1384 Embed the n-by-n complex matrix ``M`` into the space of real
1385 matrices of size 2n-by-2n via the map the sends each entry `z = a +
1386 bi` to the block matrix ``[[a,b],[-b,a]]``.
1390 sage: from mjo.eja.eja_algebra import \
1391 ....: ComplexMatrixEuclideanJordanAlgebra
1395 sage: F = QuadraticField(-1, 'i')
1396 sage: x1 = F(4 - 2*i)
1397 sage: x2 = F(1 + 2*i)
1400 sage: M = matrix(F,2,[[x1,x2],[x3,x4]])
1401 sage: ComplexMatrixEuclideanJordanAlgebra.real_embed(M)
1410 Embedding is a homomorphism (isomorphism, in fact)::
1412 sage: set_random_seed()
1413 sage: n_max = ComplexMatrixEuclideanJordanAlgebra._max_test_case_size()
1414 sage: n = ZZ.random_element(n_max)
1415 sage: F = QuadraticField(-1, 'i')
1416 sage: X = random_matrix(F, n)
1417 sage: Y = random_matrix(F, n)
1418 sage: Xe = ComplexMatrixEuclideanJordanAlgebra.real_embed(X)
1419 sage: Ye = ComplexMatrixEuclideanJordanAlgebra.real_embed(Y)
1420 sage: XYe = ComplexMatrixEuclideanJordanAlgebra.real_embed(X*Y)
1427 raise ValueError("the matrix 'M' must be square")
1429 # We don't need any adjoined elements...
1430 field
= M
.base_ring().base_ring()
1434 a
= z
.list()[0] # real part, I guess
1435 b
= z
.list()[1] # imag part, I guess
1436 blocks
.append(matrix(field
, 2, [[a
,b
],[-b
,a
]]))
1438 return matrix
.block(field
, n
, blocks
)
1442 def real_unembed(M
):
1444 The inverse of _embed_complex_matrix().
1448 sage: from mjo.eja.eja_algebra import \
1449 ....: ComplexMatrixEuclideanJordanAlgebra
1453 sage: A = matrix(QQ,[ [ 1, 2, 3, 4],
1454 ....: [-2, 1, -4, 3],
1455 ....: [ 9, 10, 11, 12],
1456 ....: [-10, 9, -12, 11] ])
1457 sage: ComplexMatrixEuclideanJordanAlgebra.real_unembed(A)
1459 [ 10*i + 9 12*i + 11]
1463 Unembedding is the inverse of embedding::
1465 sage: set_random_seed()
1466 sage: F = QuadraticField(-1, 'i')
1467 sage: M = random_matrix(F, 3)
1468 sage: Me = ComplexMatrixEuclideanJordanAlgebra.real_embed(M)
1469 sage: ComplexMatrixEuclideanJordanAlgebra.real_unembed(Me) == M
1475 raise ValueError("the matrix 'M' must be square")
1476 if not n
.mod(2).is_zero():
1477 raise ValueError("the matrix 'M' must be a complex embedding")
1479 # If "M" was normalized, its base ring might have roots
1480 # adjoined and they can stick around after unembedding.
1481 field
= M
.base_ring()
1482 R
= PolynomialRing(field
, 'z')
1484 F
= field
.extension(z
**2 + 1, 'i', embedding
=CLF(-1).sqrt())
1487 # Go top-left to bottom-right (reading order), converting every
1488 # 2-by-2 block we see to a single complex element.
1490 for k
in range(n
/2):
1491 for j
in range(n
/2):
1492 submat
= M
[2*k
:2*k
+2,2*j
:2*j
+2]
1493 if submat
[0,0] != submat
[1,1]:
1494 raise ValueError('bad on-diagonal submatrix')
1495 if submat
[0,1] != -submat
[1,0]:
1496 raise ValueError('bad off-diagonal submatrix')
1497 z
= submat
[0,0] + submat
[0,1]*i
1500 return matrix(F
, n
/2, elements
)
1504 def natural_inner_product(cls
,X
,Y
):
1506 Compute a natural inner product in this algebra directly from
1511 sage: from mjo.eja.eja_algebra import ComplexHermitianEJA
1515 This gives the same answer as the slow, default method implemented
1516 in :class:`MatrixEuclideanJordanAlgebra`::
1518 sage: set_random_seed()
1519 sage: J = ComplexHermitianEJA.random_instance()
1520 sage: x,y = J.random_elements(2)
1521 sage: Xe = x.natural_representation()
1522 sage: Ye = y.natural_representation()
1523 sage: X = ComplexHermitianEJA.real_unembed(Xe)
1524 sage: Y = ComplexHermitianEJA.real_unembed(Ye)
1525 sage: expected = (X*Y).trace().vector()[0]
1526 sage: actual = ComplexHermitianEJA.natural_inner_product(Xe,Ye)
1527 sage: actual == expected
1531 return RealMatrixEuclideanJordanAlgebra
.natural_inner_product(X
,Y
)/2
1534 class ComplexHermitianEJA(ComplexMatrixEuclideanJordanAlgebra
, KnownRankEJA
):
1536 The rank-n simple EJA consisting of complex Hermitian n-by-n
1537 matrices over the real numbers, the usual symmetric Jordan product,
1538 and the real-part-of-trace inner product. It has dimension `n^2` over
1543 sage: from mjo.eja.eja_algebra import ComplexHermitianEJA
1547 In theory, our "field" can be any subfield of the reals::
1549 sage: ComplexHermitianEJA(2, AA)
1550 Euclidean Jordan algebra of dimension 4 over Algebraic Real Field
1551 sage: ComplexHermitianEJA(2, RR)
1552 Euclidean Jordan algebra of dimension 4 over Real Field with
1553 53 bits of precision
1557 The dimension of this algebra is `n^2`::
1559 sage: set_random_seed()
1560 sage: n_max = ComplexHermitianEJA._max_test_case_size()
1561 sage: n = ZZ.random_element(1, n_max)
1562 sage: J = ComplexHermitianEJA(n)
1563 sage: J.dimension() == n^2
1566 The Jordan multiplication is what we think it is::
1568 sage: set_random_seed()
1569 sage: J = ComplexHermitianEJA.random_instance()
1570 sage: x,y = J.random_elements(2)
1571 sage: actual = (x*y).natural_representation()
1572 sage: X = x.natural_representation()
1573 sage: Y = y.natural_representation()
1574 sage: expected = (X*Y + Y*X)/2
1575 sage: actual == expected
1577 sage: J(expected) == x*y
1580 We can change the generator prefix::
1582 sage: ComplexHermitianEJA(2, prefix='z').gens()
1585 Our natural basis is normalized with respect to the natural inner
1586 product unless we specify otherwise::
1588 sage: set_random_seed()
1589 sage: J = ComplexHermitianEJA.random_instance()
1590 sage: all( b.norm() == 1 for b in J.gens() )
1593 Since our natural basis is normalized with respect to the natural
1594 inner product, and since we know that this algebra is an EJA, any
1595 left-multiplication operator's matrix will be symmetric because
1596 natural->EJA basis representation is an isometry and within the EJA
1597 the operator is self-adjoint by the Jordan axiom::
1599 sage: set_random_seed()
1600 sage: x = ComplexHermitianEJA.random_instance().random_element()
1601 sage: x.operator().matrix().is_symmetric()
1607 def _denormalized_basis(cls
, n
, field
):
1609 Returns a basis for the space of complex Hermitian n-by-n matrices.
1611 Why do we embed these? Basically, because all of numerical linear
1612 algebra assumes that you're working with vectors consisting of `n`
1613 entries from a field and scalars from the same field. There's no way
1614 to tell SageMath that (for example) the vectors contain complex
1615 numbers, while the scalar field is real.
1619 sage: from mjo.eja.eja_algebra import ComplexHermitianEJA
1623 sage: set_random_seed()
1624 sage: n = ZZ.random_element(1,5)
1625 sage: field = QuadraticField(2, 'sqrt2')
1626 sage: B = ComplexHermitianEJA._denormalized_basis(n, field)
1627 sage: all( M.is_symmetric() for M in B)
1631 R
= PolynomialRing(field
, 'z')
1633 F
= field
.extension(z
**2 + 1, 'I')
1636 # This is like the symmetric case, but we need to be careful:
1638 # * We want conjugate-symmetry, not just symmetry.
1639 # * The diagonal will (as a result) be real.
1643 for j
in range(i
+1):
1644 Eij
= matrix(F
, n
, lambda k
,l
: k
==i
and l
==j
)
1646 Sij
= cls
.real_embed(Eij
)
1649 # The second one has a minus because it's conjugated.
1650 Sij_real
= cls
.real_embed(Eij
+ Eij
.transpose())
1652 Sij_imag
= cls
.real_embed(I
*Eij
- I
*Eij
.transpose())
1655 # Since we embedded these, we can drop back to the "field" that we
1656 # started with instead of the complex extension "F".
1657 return ( s
.change_ring(field
) for s
in S
)
1660 def __init__(self
, n
, field
=QQ
, **kwargs
):
1661 basis
= self
._denormalized
_basis
(n
,field
)
1662 super(ComplexHermitianEJA
,self
).__init
__(field
, basis
, n
, **kwargs
)
1665 class QuaternionMatrixEuclideanJordanAlgebra(MatrixEuclideanJordanAlgebra
):
1669 Embed the n-by-n quaternion matrix ``M`` into the space of real
1670 matrices of size 4n-by-4n by first sending each quaternion entry `z
1671 = a + bi + cj + dk` to the block-complex matrix ``[[a + bi,
1672 c+di],[-c + di, a-bi]]`, and then embedding those into a real
1677 sage: from mjo.eja.eja_algebra import \
1678 ....: QuaternionMatrixEuclideanJordanAlgebra
1682 sage: Q = QuaternionAlgebra(QQ,-1,-1)
1683 sage: i,j,k = Q.gens()
1684 sage: x = 1 + 2*i + 3*j + 4*k
1685 sage: M = matrix(Q, 1, [[x]])
1686 sage: QuaternionMatrixEuclideanJordanAlgebra.real_embed(M)
1692 Embedding is a homomorphism (isomorphism, in fact)::
1694 sage: set_random_seed()
1695 sage: n_max = QuaternionMatrixEuclideanJordanAlgebra._max_test_case_size()
1696 sage: n = ZZ.random_element(n_max)
1697 sage: Q = QuaternionAlgebra(QQ,-1,-1)
1698 sage: X = random_matrix(Q, n)
1699 sage: Y = random_matrix(Q, n)
1700 sage: Xe = QuaternionMatrixEuclideanJordanAlgebra.real_embed(X)
1701 sage: Ye = QuaternionMatrixEuclideanJordanAlgebra.real_embed(Y)
1702 sage: XYe = QuaternionMatrixEuclideanJordanAlgebra.real_embed(X*Y)
1707 quaternions
= M
.base_ring()
1710 raise ValueError("the matrix 'M' must be square")
1712 F
= QuadraticField(-1, 'i')
1717 t
= z
.coefficient_tuple()
1722 cplxM
= matrix(F
, 2, [[ a
+ b
*i
, c
+ d
*i
],
1723 [-c
+ d
*i
, a
- b
*i
]])
1724 realM
= ComplexMatrixEuclideanJordanAlgebra
.real_embed(cplxM
)
1725 blocks
.append(realM
)
1727 # We should have real entries by now, so use the realest field
1728 # we've got for the return value.
1729 return matrix
.block(quaternions
.base_ring(), n
, blocks
)
1734 def real_unembed(M
):
1736 The inverse of _embed_quaternion_matrix().
1740 sage: from mjo.eja.eja_algebra import \
1741 ....: QuaternionMatrixEuclideanJordanAlgebra
1745 sage: M = matrix(QQ, [[ 1, 2, 3, 4],
1746 ....: [-2, 1, -4, 3],
1747 ....: [-3, 4, 1, -2],
1748 ....: [-4, -3, 2, 1]])
1749 sage: QuaternionMatrixEuclideanJordanAlgebra.real_unembed(M)
1750 [1 + 2*i + 3*j + 4*k]
1754 Unembedding is the inverse of embedding::
1756 sage: set_random_seed()
1757 sage: Q = QuaternionAlgebra(QQ, -1, -1)
1758 sage: M = random_matrix(Q, 3)
1759 sage: Me = QuaternionMatrixEuclideanJordanAlgebra.real_embed(M)
1760 sage: QuaternionMatrixEuclideanJordanAlgebra.real_unembed(Me) == M
1766 raise ValueError("the matrix 'M' must be square")
1767 if not n
.mod(4).is_zero():
1768 raise ValueError("the matrix 'M' must be a quaternion embedding")
1770 # Use the base ring of the matrix to ensure that its entries can be
1771 # multiplied by elements of the quaternion algebra.
1772 field
= M
.base_ring()
1773 Q
= QuaternionAlgebra(field
,-1,-1)
1776 # Go top-left to bottom-right (reading order), converting every
1777 # 4-by-4 block we see to a 2-by-2 complex block, to a 1-by-1
1780 for l
in range(n
/4):
1781 for m
in range(n
/4):
1782 submat
= ComplexMatrixEuclideanJordanAlgebra
.real_unembed(
1783 M
[4*l
:4*l
+4,4*m
:4*m
+4] )
1784 if submat
[0,0] != submat
[1,1].conjugate():
1785 raise ValueError('bad on-diagonal submatrix')
1786 if submat
[0,1] != -submat
[1,0].conjugate():
1787 raise ValueError('bad off-diagonal submatrix')
1788 z
= submat
[0,0].vector()[0] # real part
1789 z
+= submat
[0,0].vector()[1]*i
# imag part
1790 z
+= submat
[0,1].vector()[0]*j
# real part
1791 z
+= submat
[0,1].vector()[1]*k
# imag part
1794 return matrix(Q
, n
/4, elements
)
1798 def natural_inner_product(cls
,X
,Y
):
1800 Compute a natural inner product in this algebra directly from
1805 sage: from mjo.eja.eja_algebra import QuaternionHermitianEJA
1809 This gives the same answer as the slow, default method implemented
1810 in :class:`MatrixEuclideanJordanAlgebra`::
1812 sage: set_random_seed()
1813 sage: J = QuaternionHermitianEJA.random_instance()
1814 sage: x,y = J.random_elements(2)
1815 sage: Xe = x.natural_representation()
1816 sage: Ye = y.natural_representation()
1817 sage: X = QuaternionHermitianEJA.real_unembed(Xe)
1818 sage: Y = QuaternionHermitianEJA.real_unembed(Ye)
1819 sage: expected = (X*Y).trace().coefficient_tuple()[0]
1820 sage: actual = QuaternionHermitianEJA.natural_inner_product(Xe,Ye)
1821 sage: actual == expected
1825 return RealMatrixEuclideanJordanAlgebra
.natural_inner_product(X
,Y
)/4
1828 class QuaternionHermitianEJA(QuaternionMatrixEuclideanJordanAlgebra
,
1831 The rank-n simple EJA consisting of self-adjoint n-by-n quaternion
1832 matrices, the usual symmetric Jordan product, and the
1833 real-part-of-trace inner product. It has dimension `2n^2 - n` over
1838 sage: from mjo.eja.eja_algebra import QuaternionHermitianEJA
1842 In theory, our "field" can be any subfield of the reals::
1844 sage: QuaternionHermitianEJA(2, AA)
1845 Euclidean Jordan algebra of dimension 6 over Algebraic Real Field
1846 sage: QuaternionHermitianEJA(2, RR)
1847 Euclidean Jordan algebra of dimension 6 over Real Field with
1848 53 bits of precision
1852 The dimension of this algebra is `2*n^2 - n`::
1854 sage: set_random_seed()
1855 sage: n_max = QuaternionHermitianEJA._max_test_case_size()
1856 sage: n = ZZ.random_element(1, n_max)
1857 sage: J = QuaternionHermitianEJA(n)
1858 sage: J.dimension() == 2*(n^2) - n
1861 The Jordan multiplication is what we think it is::
1863 sage: set_random_seed()
1864 sage: J = QuaternionHermitianEJA.random_instance()
1865 sage: x,y = J.random_elements(2)
1866 sage: actual = (x*y).natural_representation()
1867 sage: X = x.natural_representation()
1868 sage: Y = y.natural_representation()
1869 sage: expected = (X*Y + Y*X)/2
1870 sage: actual == expected
1872 sage: J(expected) == x*y
1875 We can change the generator prefix::
1877 sage: QuaternionHermitianEJA(2, prefix='a').gens()
1878 (a0, a1, a2, a3, a4, a5)
1880 Our natural basis is normalized with respect to the natural inner
1881 product unless we specify otherwise::
1883 sage: set_random_seed()
1884 sage: J = QuaternionHermitianEJA.random_instance()
1885 sage: all( b.norm() == 1 for b in J.gens() )
1888 Since our natural basis is normalized with respect to the natural
1889 inner product, and since we know that this algebra is an EJA, any
1890 left-multiplication operator's matrix will be symmetric because
1891 natural->EJA basis representation is an isometry and within the EJA
1892 the operator is self-adjoint by the Jordan axiom::
1894 sage: set_random_seed()
1895 sage: x = QuaternionHermitianEJA.random_instance().random_element()
1896 sage: x.operator().matrix().is_symmetric()
1901 def _denormalized_basis(cls
, n
, field
):
1903 Returns a basis for the space of quaternion Hermitian n-by-n matrices.
1905 Why do we embed these? Basically, because all of numerical
1906 linear algebra assumes that you're working with vectors consisting
1907 of `n` entries from a field and scalars from the same field. There's
1908 no way to tell SageMath that (for example) the vectors contain
1909 complex numbers, while the scalar field is real.
1913 sage: from mjo.eja.eja_algebra import QuaternionHermitianEJA
1917 sage: set_random_seed()
1918 sage: n = ZZ.random_element(1,5)
1919 sage: B = QuaternionHermitianEJA._denormalized_basis(n,QQ)
1920 sage: all( M.is_symmetric() for M in B )
1924 Q
= QuaternionAlgebra(QQ
,-1,-1)
1927 # This is like the symmetric case, but we need to be careful:
1929 # * We want conjugate-symmetry, not just symmetry.
1930 # * The diagonal will (as a result) be real.
1934 for j
in range(i
+1):
1935 Eij
= matrix(Q
, n
, lambda k
,l
: k
==i
and l
==j
)
1937 Sij
= cls
.real_embed(Eij
)
1940 # The second, third, and fourth ones have a minus
1941 # because they're conjugated.
1942 Sij_real
= cls
.real_embed(Eij
+ Eij
.transpose())
1944 Sij_I
= cls
.real_embed(I
*Eij
- I
*Eij
.transpose())
1946 Sij_J
= cls
.real_embed(J
*Eij
- J
*Eij
.transpose())
1948 Sij_K
= cls
.real_embed(K
*Eij
- K
*Eij
.transpose())
1951 # Since we embedded these, we can drop back to the "field" that we
1952 # started with instead of the quaternion algebra "Q".
1953 return ( s
.change_ring(field
) for s
in S
)
1956 def __init__(self
, n
, field
=QQ
, **kwargs
):
1957 basis
= self
._denormalized
_basis
(n
,field
)
1958 super(QuaternionHermitianEJA
,self
).__init
__(field
, basis
, n
, **kwargs
)
1961 class BilinearFormEJA(FiniteDimensionalEuclideanJordanAlgebra
, KnownRankEJA
):
1963 The rank-2 simple EJA consisting of real vectors ``x=(x0, x_bar)``
1964 with the half-trace inner product and jordan product ``x*y =
1965 (x0*y0 + <B*x_bar,y_bar>, x0*y_bar + y0*x_bar)`` where ``B`` is a
1966 symmetric positive-definite "bilinear form" matrix. It has
1967 dimension `n` over the reals, and reduces to the ``JordanSpinEJA``
1968 when ``B`` is the identity matrix of order ``n-1``.
1972 sage: from mjo.eja.eja_algebra import (BilinearFormEJA,
1973 ....: JordanSpinEJA)
1977 When no bilinear form is specified, the identity matrix is used,
1978 and the resulting algebra is the Jordan spin algebra::
1980 sage: J0 = BilinearFormEJA(3)
1981 sage: J1 = JordanSpinEJA(3)
1982 sage: J0.multiplication_table() == J0.multiplication_table()
1987 We can create a zero-dimensional algebra::
1989 sage: J = BilinearFormEJA(0)
1993 We can check the multiplication condition given in the Jordan, von
1994 Neumann, and Wigner paper (and also discussed on my "On the
1995 symmetry..." paper). Note that this relies heavily on the standard
1996 choice of basis, as does anything utilizing the bilinear form matrix::
1998 sage: set_random_seed()
1999 sage: n = ZZ.random_element(5)
2000 sage: M = matrix.random(QQ, max(0,n-1), algorithm='unimodular')
2001 sage: B = M.transpose()*M
2002 sage: J = BilinearFormEJA(n, B=B)
2003 sage: eis = VectorSpace(M.base_ring(), M.ncols()).basis()
2004 sage: V = J.vector_space()
2005 sage: sis = [ J.from_vector(V([0] + (M.inverse()*ei).list()))
2006 ....: for ei in eis ]
2007 sage: actual = [ sis[i]*sis[j]
2008 ....: for i in range(n-1)
2009 ....: for j in range(n-1) ]
2010 sage: expected = [ J.one() if i == j else J.zero()
2011 ....: for i in range(n-1)
2012 ....: for j in range(n-1) ]
2013 sage: actual == expected
2016 def __init__(self
, n
, field
=QQ
, B
=None, **kwargs
):
2018 self
._B
= matrix
.identity(field
, max(0,n
-1))
2022 V
= VectorSpace(field
, n
)
2023 mult_table
= [[V
.zero() for j
in range(n
)] for i
in range(n
)]
2032 z0
= x0
*y0
+ (self
._B
*xbar
).inner_product(ybar
)
2033 zbar
= y0
*xbar
+ x0
*ybar
2034 z
= V([z0
] + zbar
.list())
2035 mult_table
[i
][j
] = z
2037 # The rank of this algebra is two, unless we're in a
2038 # one-dimensional ambient space (because the rank is bounded
2039 # by the ambient dimension).
2040 fdeja
= super(BilinearFormEJA
, self
)
2041 return fdeja
.__init
__(field
, mult_table
, rank
=min(n
,2), **kwargs
)
2043 def inner_product(self
, x
, y
):
2045 Half of the trace inner product.
2047 This is defined so that the special case of the Jordan spin
2048 algebra gets the usual inner product.
2052 sage: from mjo.eja.eja_algebra import BilinearFormEJA
2056 Ensure that this is one-half of the trace inner-product when
2057 the algebra isn't just the reals (when ``n`` isn't one). This
2058 is in Faraut and Koranyi, and also my "On the symmetry..."
2061 sage: set_random_seed()
2062 sage: n = ZZ.random_element(2,5)
2063 sage: M = matrix.random(QQ, max(0,n-1), algorithm='unimodular')
2064 sage: B = M.transpose()*M
2065 sage: J = BilinearFormEJA(n, B=B)
2066 sage: x = J.random_element()
2067 sage: y = J.random_element()
2068 sage: x.inner_product(y) == (x*y).trace()/2
2072 xvec
= x
.to_vector()
2074 yvec
= y
.to_vector()
2076 return x
[0]*y
[0] + (self
._B
*xbar
).inner_product(ybar
)
2079 class JordanSpinEJA(BilinearFormEJA
):
2081 The rank-2 simple EJA consisting of real vectors ``x=(x0, x_bar)``
2082 with the usual inner product and jordan product ``x*y =
2083 (<x,y>, x0*y_bar + y0*x_bar)``. It has dimension `n` over
2088 sage: from mjo.eja.eja_algebra import JordanSpinEJA
2092 This multiplication table can be verified by hand::
2094 sage: J = JordanSpinEJA(4)
2095 sage: e0,e1,e2,e3 = J.gens()
2111 We can change the generator prefix::
2113 sage: JordanSpinEJA(2, prefix='B').gens()
2118 Ensure that we have the usual inner product on `R^n`::
2120 sage: set_random_seed()
2121 sage: J = JordanSpinEJA.random_instance()
2122 sage: x,y = J.random_elements(2)
2123 sage: X = x.natural_representation()
2124 sage: Y = y.natural_representation()
2125 sage: x.inner_product(y) == J.natural_inner_product(X,Y)
2129 def __init__(self
, n
, field
=QQ
, **kwargs
):
2130 # This is a special case of the BilinearFormEJA with the identity
2131 # matrix as its bilinear form.
2132 return super(JordanSpinEJA
, self
).__init
__(n
, field
, **kwargs
)
2135 class TrivialEJA(FiniteDimensionalEuclideanJordanAlgebra
, KnownRankEJA
):
2137 The trivial Euclidean Jordan algebra consisting of only a zero element.
2141 sage: from mjo.eja.eja_algebra import TrivialEJA
2145 sage: J = TrivialEJA()
2152 sage: 7*J.one()*12*J.one()
2154 sage: J.one().inner_product(J.one())
2156 sage: J.one().norm()
2158 sage: J.one().subalgebra_generated_by()
2159 Euclidean Jordan algebra of dimension 0 over Rational Field
2164 def __init__(self
, field
=QQ
, **kwargs
):
2166 fdeja
= super(TrivialEJA
, self
)
2167 # The rank is zero using my definition, namely the dimension of the
2168 # largest subalgebra generated by any element.
2169 return fdeja
.__init
__(field
, mult_table
, rank
=0, **kwargs
)