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 sage
.algebras
.quatalg
.quaternion_algebra
import QuaternionAlgebra
9 from sage
.categories
.magmatic_algebras
import MagmaticAlgebras
10 from sage
.combinat
.free_module
import CombinatorialFreeModule
11 from sage
.matrix
.constructor
import matrix
12 from sage
.matrix
.matrix_space
import MatrixSpace
13 from sage
.misc
.cachefunc
import cached_method
14 from sage
.misc
.prandom
import choice
15 from sage
.misc
.table
import table
16 from sage
.modules
.free_module
import FreeModule
, VectorSpace
17 from sage
.rings
.integer_ring
import ZZ
18 from sage
.rings
.number_field
.number_field
import NumberField
19 from sage
.rings
.polynomial
.polynomial_ring_constructor
import PolynomialRing
20 from sage
.rings
.rational_field
import QQ
21 from sage
.rings
.real_lazy
import CLF
, RLF
22 from sage
.structure
.element
import is_Matrix
24 from mjo
.eja
.eja_element
import FiniteDimensionalEuclideanJordanAlgebraElement
25 from mjo
.eja
.eja_utils
import _mat2vec
27 class FiniteDimensionalEuclideanJordanAlgebra(CombinatorialFreeModule
):
28 # This is an ugly hack needed to prevent the category framework
29 # from implementing a coercion from our base ring (e.g. the
30 # rationals) into the algebra. First of all -- such a coercion is
31 # nonsense to begin with. But more importantly, it tries to do so
32 # in the category of rings, and since our algebras aren't
33 # associative they generally won't be rings.
34 _no_generic_basering_coercion
= True
46 sage: from mjo.eja.eja_algebra import random_eja
50 By definition, Jordan multiplication commutes::
52 sage: set_random_seed()
53 sage: J = random_eja()
54 sage: x = J.random_element()
55 sage: y = J.random_element()
61 self
._natural
_basis
= natural_basis
64 category
= MagmaticAlgebras(field
).FiniteDimensional()
65 category
= category
.WithBasis().Unital()
67 fda
= super(FiniteDimensionalEuclideanJordanAlgebra
, self
)
69 range(len(mult_table
)),
72 self
.print_options(bracket
='')
74 # The multiplication table we're given is necessarily in terms
75 # of vectors, because we don't have an algebra yet for
76 # anything to be an element of. However, it's faster in the
77 # long run to have the multiplication table be in terms of
78 # algebra elements. We do this after calling the superclass
79 # constructor so that from_vector() knows what to do.
80 self
._multiplication
_table
= [ map(lambda x
: self
.from_vector(x
), ls
)
81 for ls
in mult_table
]
84 def _element_constructor_(self
, elt
):
86 Construct an element of this algebra from its natural
89 This gets called only after the parent element _call_ method
90 fails to find a coercion for the argument.
94 sage: from mjo.eja.eja_algebra import (JordanSpinEJA,
95 ....: RealCartesianProductEJA,
96 ....: RealSymmetricEJA)
100 The identity in `S^n` is converted to the identity in the EJA::
102 sage: J = RealSymmetricEJA(3)
103 sage: I = matrix.identity(QQ,3)
104 sage: J(I) == J.one()
107 This skew-symmetric matrix can't be represented in the EJA::
109 sage: J = RealSymmetricEJA(3)
110 sage: A = matrix(QQ,3, lambda i,j: i-j)
112 Traceback (most recent call last):
114 ArithmeticError: vector is not in free module
118 Ensure that we can convert any element of the two non-matrix
119 simple algebras (whose natural representations are their usual
120 vector representations) back and forth faithfully::
122 sage: set_random_seed()
123 sage: J = RealCartesianProductEJA(5)
124 sage: x = J.random_element()
125 sage: J(x.to_vector().column()) == x
127 sage: J = JordanSpinEJA(5)
128 sage: x = J.random_element()
129 sage: J(x.to_vector().column()) == x
134 # The superclass implementation of random_element()
135 # needs to be able to coerce "0" into the algebra.
138 natural_basis
= self
.natural_basis()
139 basis_space
= natural_basis
[0].matrix_space()
140 if elt
not in basis_space
:
141 raise ValueError("not a naturally-represented algebra element")
143 # Thanks for nothing! Matrix spaces aren't vector spaces in
144 # Sage, so we have to figure out its natural-basis coordinates
145 # ourselves. We use the basis space's ring instead of the
146 # element's ring because the basis space might be an algebraic
147 # closure whereas the base ring of the 3-by-3 identity matrix
148 # could be QQ instead of QQbar.
149 V
= VectorSpace(basis_space
.base_ring(), elt
.nrows()*elt
.ncols())
150 W
= V
.span_of_basis( _mat2vec(s
) for s
in natural_basis
)
151 coords
= W
.coordinate_vector(_mat2vec(elt
))
152 return self
.from_vector(coords
)
157 Return a string representation of ``self``.
161 sage: from mjo.eja.eja_algebra import JordanSpinEJA
165 Ensure that it says what we think it says::
167 sage: JordanSpinEJA(2, field=QQ)
168 Euclidean Jordan algebra of dimension 2 over Rational Field
169 sage: JordanSpinEJA(3, field=RDF)
170 Euclidean Jordan algebra of dimension 3 over Real Double Field
173 fmt
= "Euclidean Jordan algebra of dimension {} over {}"
174 return fmt
.format(self
.dimension(), self
.base_ring())
176 def product_on_basis(self
, i
, j
):
177 return self
._multiplication
_table
[i
][j
]
179 def _a_regular_element(self
):
181 Guess a regular element. Needed to compute the basis for our
182 characteristic polynomial coefficients.
186 sage: from mjo.eja.eja_algebra import random_eja
190 Ensure that this hacky method succeeds for every algebra that we
191 know how to construct::
193 sage: set_random_seed()
194 sage: J = random_eja()
195 sage: J._a_regular_element().is_regular()
200 z
= self
.sum( (i
+1)*gs
[i
] for i
in range(len(gs
)) )
201 if not z
.is_regular():
202 raise ValueError("don't know a regular element")
207 def _charpoly_basis_space(self
):
209 Return the vector space spanned by the basis used in our
210 characteristic polynomial coefficients. This is used not only to
211 compute those coefficients, but also any time we need to
212 evaluate the coefficients (like when we compute the trace or
215 z
= self
._a
_regular
_element
()
216 # Don't use the parent vector space directly here in case this
217 # happens to be a subalgebra. In that case, we would be e.g.
218 # two-dimensional but span_of_basis() would expect three
220 V
= VectorSpace(self
.base_ring(), self
.vector_space().dimension())
221 basis
= [ (z
**k
).to_vector() for k
in range(self
.rank()) ]
222 V1
= V
.span_of_basis( basis
)
223 b
= (V1
.basis() + V1
.complement().basis())
224 return V
.span_of_basis(b
)
228 def _charpoly_coeff(self
, i
):
230 Return the coefficient polynomial "a_{i}" of this algebra's
231 general characteristic polynomial.
233 Having this be a separate cached method lets us compute and
234 store the trace/determinant (a_{r-1} and a_{0} respectively)
235 separate from the entire characteristic polynomial.
237 (A_of_x
, x
, xr
, detA
) = self
._charpoly
_matrix
_system
()
238 R
= A_of_x
.base_ring()
240 # Guaranteed by theory
243 # Danger: the in-place modification is done for performance
244 # reasons (reconstructing a matrix with huge polynomial
245 # entries is slow), but I don't know how cached_method works,
246 # so it's highly possible that we're modifying some global
247 # list variable by reference, here. In other words, you
248 # probably shouldn't call this method twice on the same
249 # algebra, at the same time, in two threads
250 Ai_orig
= A_of_x
.column(i
)
251 A_of_x
.set_column(i
,xr
)
252 numerator
= A_of_x
.det()
253 A_of_x
.set_column(i
,Ai_orig
)
255 # We're relying on the theory here to ensure that each a_i is
256 # indeed back in R, and the added negative signs are to make
257 # the whole charpoly expression sum to zero.
258 return R(-numerator
/detA
)
262 def _charpoly_matrix_system(self
):
264 Compute the matrix whose entries A_ij are polynomials in
265 X1,...,XN, the vector ``x`` of variables X1,...,XN, the vector
266 corresponding to `x^r` and the determinent of the matrix A =
267 [A_ij]. In other words, all of the fixed (cachable) data needed
268 to compute the coefficients of the characteristic polynomial.
273 # Turn my vector space into a module so that "vectors" can
274 # have multivatiate polynomial entries.
275 names
= tuple('X' + str(i
) for i
in range(1,n
+1))
276 R
= PolynomialRing(self
.base_ring(), names
)
278 # Using change_ring() on the parent's vector space doesn't work
279 # here because, in a subalgebra, that vector space has a basis
280 # and change_ring() tries to bring the basis along with it. And
281 # that doesn't work unless the new ring is a PID, which it usually
285 # Now let x = (X1,X2,...,Xn) be the vector whose entries are
289 # And figure out the "left multiplication by x" matrix in
292 monomial_matrices
= [ self
.monomial(i
).operator().matrix()
293 for i
in range(n
) ] # don't recompute these!
295 ek
= self
.monomial(k
).to_vector()
297 sum( x
[i
]*(monomial_matrices
[i
]*ek
)
298 for i
in range(n
) ) )
299 Lx
= matrix
.column(R
, lmbx_cols
)
301 # Now we can compute powers of x "symbolically"
302 x_powers
= [self
.one().to_vector(), x
]
303 for d
in range(2, r
+1):
304 x_powers
.append( Lx
*(x_powers
[-1]) )
306 idmat
= matrix
.identity(R
, n
)
308 W
= self
._charpoly
_basis
_space
()
309 W
= W
.change_ring(R
.fraction_field())
311 # Starting with the standard coordinates x = (X1,X2,...,Xn)
312 # and then converting the entries to W-coordinates allows us
313 # to pass in the standard coordinates to the charpoly and get
314 # back the right answer. Specifically, with x = (X1,X2,...,Xn),
317 # W.coordinates(x^2) eval'd at (standard z-coords)
321 # W-coords of (standard coords of x^2 eval'd at std-coords of z)
323 # We want the middle equivalent thing in our matrix, but use
324 # the first equivalent thing instead so that we can pass in
325 # standard coordinates.
326 x_powers
= [ W
.coordinate_vector(xp
) for xp
in x_powers
]
327 l2
= [idmat
.column(k
-1) for k
in range(r
+1, n
+1)]
328 A_of_x
= matrix
.column(R
, n
, (x_powers
[:r
] + l2
))
329 return (A_of_x
, x
, x_powers
[r
], A_of_x
.det())
333 def characteristic_polynomial(self
):
335 Return a characteristic polynomial that works for all elements
338 The resulting polynomial has `n+1` variables, where `n` is the
339 dimension of this algebra. The first `n` variables correspond to
340 the coordinates of an algebra element: when evaluated at the
341 coordinates of an algebra element with respect to a certain
342 basis, the result is a univariate polynomial (in the one
343 remaining variable ``t``), namely the characteristic polynomial
348 sage: from mjo.eja.eja_algebra import JordanSpinEJA
352 The characteristic polynomial in the spin algebra is given in
353 Alizadeh, Example 11.11::
355 sage: J = JordanSpinEJA(3)
356 sage: p = J.characteristic_polynomial(); p
357 X1^2 - X2^2 - X3^2 + (-2*t)*X1 + t^2
358 sage: xvec = J.one().to_vector()
366 # The list of coefficient polynomials a_1, a_2, ..., a_n.
367 a
= [ self
._charpoly
_coeff
(i
) for i
in range(n
) ]
369 # We go to a bit of trouble here to reorder the
370 # indeterminates, so that it's easier to evaluate the
371 # characteristic polynomial at x's coordinates and get back
372 # something in terms of t, which is what we want.
374 S
= PolynomialRing(self
.base_ring(),'t')
376 S
= PolynomialRing(S
, R
.variable_names())
379 # Note: all entries past the rth should be zero. The
380 # coefficient of the highest power (x^r) is 1, but it doesn't
381 # appear in the solution vector which contains coefficients
382 # for the other powers (to make them sum to x^r).
384 a
[r
] = 1 # corresponds to x^r
386 # When the rank is equal to the dimension, trying to
387 # assign a[r] goes out-of-bounds.
388 a
.append(1) # corresponds to x^r
390 return sum( a
[k
]*(t
**k
) for k
in range(len(a
)) )
393 def inner_product(self
, x
, y
):
395 The inner product associated with this Euclidean Jordan algebra.
397 Defaults to the trace inner product, but can be overridden by
398 subclasses if they are sure that the necessary properties are
403 sage: from mjo.eja.eja_algebra import random_eja
407 The inner product must satisfy its axiom for this algebra to truly
408 be a Euclidean Jordan Algebra::
410 sage: set_random_seed()
411 sage: J = random_eja()
412 sage: x = J.random_element()
413 sage: y = J.random_element()
414 sage: z = J.random_element()
415 sage: (x*y).inner_product(z) == y.inner_product(x*z)
419 if (not x
in self
) or (not y
in self
):
420 raise TypeError("arguments must live in this algebra")
421 return x
.trace_inner_product(y
)
424 def is_trivial(self
):
426 Return whether or not this algebra is trivial.
428 A trivial algebra contains only the zero element.
432 sage: from mjo.eja.eja_algebra import ComplexHermitianEJA
436 sage: J = ComplexHermitianEJA(3)
439 sage: A = J.zero().subalgebra_generated_by()
444 return self
.dimension() == 0
447 def multiplication_table(self
):
449 Return a visual representation of this algebra's multiplication
450 table (on basis elements).
454 sage: from mjo.eja.eja_algebra import JordanSpinEJA
458 sage: J = JordanSpinEJA(4)
459 sage: J.multiplication_table()
460 +----++----+----+----+----+
461 | * || e0 | e1 | e2 | e3 |
462 +====++====+====+====+====+
463 | e0 || e0 | e1 | e2 | e3 |
464 +----++----+----+----+----+
465 | e1 || e1 | e0 | 0 | 0 |
466 +----++----+----+----+----+
467 | e2 || e2 | 0 | e0 | 0 |
468 +----++----+----+----+----+
469 | e3 || e3 | 0 | 0 | e0 |
470 +----++----+----+----+----+
473 M
= list(self
._multiplication
_table
) # copy
474 for i
in range(len(M
)):
475 # M had better be "square"
476 M
[i
] = [self
.monomial(i
)] + M
[i
]
477 M
= [["*"] + list(self
.gens())] + M
478 return table(M
, header_row
=True, header_column
=True, frame
=True)
481 def natural_basis(self
):
483 Return a more-natural representation of this algebra's basis.
485 Every finite-dimensional Euclidean Jordan Algebra is a direct
486 sum of five simple algebras, four of which comprise Hermitian
487 matrices. This method returns the original "natural" basis
488 for our underlying vector space. (Typically, the natural basis
489 is used to construct the multiplication table in the first place.)
491 Note that this will always return a matrix. The standard basis
492 in `R^n` will be returned as `n`-by-`1` column matrices.
496 sage: from mjo.eja.eja_algebra import (JordanSpinEJA,
497 ....: RealSymmetricEJA)
501 sage: J = RealSymmetricEJA(2)
503 Finite family {0: e0, 1: e1, 2: e2}
504 sage: J.natural_basis()
506 [1 0] [ 0 1/2*sqrt2] [0 0]
507 [0 0], [1/2*sqrt2 0], [0 1]
512 sage: J = JordanSpinEJA(2)
514 Finite family {0: e0, 1: e1}
515 sage: J.natural_basis()
522 if self
._natural
_basis
is None:
523 M
= self
.natural_basis_space()
524 return tuple( M(b
.to_vector()) for b
in self
.basis() )
526 return self
._natural
_basis
529 def natural_basis_space(self
):
531 Return the matrix space in which this algebra's natural basis
534 if self
._natural
_basis
is None or len(self
._natural
_basis
) == 0:
535 return MatrixSpace(self
.base_ring(), self
.dimension(), 1)
537 return self
._natural
_basis
[0].matrix_space()
543 Return the unit element of this algebra.
547 sage: from mjo.eja.eja_algebra import (RealCartesianProductEJA,
552 sage: J = RealCartesianProductEJA(5)
554 e0 + e1 + e2 + e3 + e4
558 The identity element acts like the identity::
560 sage: set_random_seed()
561 sage: J = random_eja()
562 sage: x = J.random_element()
563 sage: J.one()*x == x and x*J.one() == x
566 The matrix of the unit element's operator is the identity::
568 sage: set_random_seed()
569 sage: J = random_eja()
570 sage: actual = J.one().operator().matrix()
571 sage: expected = matrix.identity(J.base_ring(), J.dimension())
572 sage: actual == expected
576 # We can brute-force compute the matrices of the operators
577 # that correspond to the basis elements of this algebra.
578 # If some linear combination of those basis elements is the
579 # algebra identity, then the same linear combination of
580 # their matrices has to be the identity matrix.
582 # Of course, matrices aren't vectors in sage, so we have to
583 # appeal to the "long vectors" isometry.
584 oper_vecs
= [ _mat2vec(g
.operator().matrix()) for g
in self
.gens() ]
586 # Now we use basis linear algebra to find the coefficients,
587 # of the matrices-as-vectors-linear-combination, which should
588 # work for the original algebra basis too.
589 A
= matrix
.column(self
.base_ring(), oper_vecs
)
591 # We used the isometry on the left-hand side already, but we
592 # still need to do it for the right-hand side. Recall that we
593 # wanted something that summed to the identity matrix.
594 b
= _mat2vec( matrix
.identity(self
.base_ring(), self
.dimension()) )
596 # Now if there's an identity element in the algebra, this should work.
597 coeffs
= A
.solve_right(b
)
598 return self
.linear_combination(zip(self
.gens(), coeffs
))
601 def random_element(self
):
602 # Temporary workaround for https://trac.sagemath.org/ticket/28327
603 if self
.is_trivial():
606 s
= super(FiniteDimensionalEuclideanJordanAlgebra
, self
)
607 return s
.random_element()
612 Return the rank of this EJA.
616 The author knows of no algorithm to compute the rank of an EJA
617 where only the multiplication table is known. In lieu of one, we
618 require the rank to be specified when the algebra is created,
619 and simply pass along that number here.
623 sage: from mjo.eja.eja_algebra import (JordanSpinEJA,
624 ....: RealSymmetricEJA,
625 ....: ComplexHermitianEJA,
626 ....: QuaternionHermitianEJA,
631 The rank of the Jordan spin algebra is always two::
633 sage: JordanSpinEJA(2).rank()
635 sage: JordanSpinEJA(3).rank()
637 sage: JordanSpinEJA(4).rank()
640 The rank of the `n`-by-`n` Hermitian real, complex, or
641 quaternion matrices is `n`::
643 sage: RealSymmetricEJA(2).rank()
645 sage: ComplexHermitianEJA(2).rank()
647 sage: QuaternionHermitianEJA(2).rank()
649 sage: RealSymmetricEJA(5).rank()
651 sage: ComplexHermitianEJA(5).rank()
653 sage: QuaternionHermitianEJA(5).rank()
658 Ensure that every EJA that we know how to construct has a
659 positive integer rank::
661 sage: set_random_seed()
662 sage: r = random_eja().rank()
663 sage: r in ZZ and r > 0
670 def vector_space(self
):
672 Return the vector space that underlies this algebra.
676 sage: from mjo.eja.eja_algebra import RealSymmetricEJA
680 sage: J = RealSymmetricEJA(2)
681 sage: J.vector_space()
682 Vector space of dimension 3 over...
685 return self
.zero().to_vector().parent().ambient_vector_space()
688 Element
= FiniteDimensionalEuclideanJordanAlgebraElement
691 class RealCartesianProductEJA(FiniteDimensionalEuclideanJordanAlgebra
):
693 Return the Euclidean Jordan Algebra corresponding to the set
694 `R^n` under the Hadamard product.
696 Note: this is nothing more than the Cartesian product of ``n``
697 copies of the spin algebra. Once Cartesian product algebras
698 are implemented, this can go.
702 sage: from mjo.eja.eja_algebra import RealCartesianProductEJA
706 This multiplication table can be verified by hand::
708 sage: J = RealCartesianProductEJA(3)
709 sage: e0,e1,e2 = J.gens()
725 We can change the generator prefix::
727 sage: RealCartesianProductEJA(3, prefix='r').gens()
730 Our inner product satisfies the Jordan axiom::
732 sage: set_random_seed()
733 sage: n = ZZ.random_element(1,5)
734 sage: J = RealCartesianProductEJA(n)
735 sage: x = J.random_element()
736 sage: y = J.random_element()
737 sage: z = J.random_element()
738 sage: (x*y).inner_product(z) == y.inner_product(x*z)
742 def __init__(self
, n
, field
=QQ
, **kwargs
):
743 V
= VectorSpace(field
, n
)
744 mult_table
= [ [ V
.gen(i
)*(i
== j
) for j
in range(n
) ]
747 fdeja
= super(RealCartesianProductEJA
, self
)
748 return fdeja
.__init
__(field
, mult_table
, rank
=n
, **kwargs
)
750 def inner_product(self
, x
, y
):
751 return _usual_ip(x
,y
)
756 Return a "random" finite-dimensional Euclidean Jordan Algebra.
760 For now, we choose a random natural number ``n`` (greater than zero)
761 and then give you back one of the following:
763 * The cartesian product of the rational numbers ``n`` times; this is
764 ``QQ^n`` with the Hadamard product.
766 * The Jordan spin algebra on ``QQ^n``.
768 * The ``n``-by-``n`` rational symmetric matrices with the symmetric
771 * The ``n``-by-``n`` complex-rational Hermitian matrices embedded
772 in the space of ``2n``-by-``2n`` real symmetric matrices.
774 * The ``n``-by-``n`` quaternion-rational Hermitian matrices embedded
775 in the space of ``4n``-by-``4n`` real symmetric matrices.
777 Later this might be extended to return Cartesian products of the
782 sage: from mjo.eja.eja_algebra import random_eja
787 Euclidean Jordan algebra of dimension...
791 # The max_n component lets us choose different upper bounds on the
792 # value "n" that gets passed to the constructor. This is needed
793 # because e.g. R^{10} is reasonable to test, while the Hermitian
794 # 10-by-10 quaternion matrices are not.
795 (constructor
, max_n
) = choice([(RealCartesianProductEJA
, 6),
797 (RealSymmetricEJA
, 5),
798 (ComplexHermitianEJA
, 4),
799 (QuaternionHermitianEJA
, 3)])
800 n
= ZZ
.random_element(1, max_n
)
801 return constructor(n
, field
=QQ
)
805 def _real_symmetric_basis(n
, field
):
807 Return a basis for the space of real symmetric n-by-n matrices.
811 sage: from mjo.eja.eja_algebra import _real_symmetric_basis
815 sage: set_random_seed()
816 sage: n = ZZ.random_element(1,5)
817 sage: B = _real_symmetric_basis(n, QQbar)
818 sage: all( M.is_symmetric() for M in B)
822 # The basis of symmetric matrices, as matrices, in their R^(n-by-n)
826 for j
in xrange(i
+1):
827 Eij
= matrix(field
, n
, lambda k
,l
: k
==i
and l
==j
)
831 Sij
= Eij
+ Eij
.transpose()
833 Sij
= Sij
/ _real_symmetric_matrix_ip(Sij
,Sij
).sqrt()
838 def _complex_hermitian_basis(n
, field
):
840 Returns a basis for the space of complex Hermitian n-by-n matrices.
842 Why do we embed these? Basically, because all of numerical linear
843 algebra assumes that you're working with vectors consisting of `n`
844 entries from a field and scalars from the same field. There's no way
845 to tell SageMath that (for example) the vectors contain complex
846 numbers, while the scalar field is real.
850 sage: from mjo.eja.eja_algebra import _complex_hermitian_basis
854 sage: set_random_seed()
855 sage: n = ZZ.random_element(1,5)
856 sage: R = PolynomialRing(QQ, 'z')
858 sage: field = NumberField(z**2 - 2, 'sqrt2', embedding=RLF(2).sqrt())
859 sage: B = _complex_hermitian_basis(n, field)
860 sage: all( M.is_symmetric() for M in B)
864 R
= PolynomialRing(field
, 'z')
866 F
= NumberField(z
**2 + 1, 'I', embedding
=CLF(-1).sqrt())
869 # This is like the symmetric case, but we need to be careful:
871 # * We want conjugate-symmetry, not just symmetry.
872 # * The diagonal will (as a result) be real.
876 for j
in xrange(i
+1):
877 Eij
= matrix(F
, n
, lambda k
,l
: k
==i
and l
==j
)
879 Sij
= _embed_complex_matrix(Eij
)
882 # Beware, orthogonal but not normalized! The second one
883 # has a minus because it's conjugated.
884 Sij_real
= _embed_complex_matrix(Eij
+ Eij
.transpose())
886 Sij_imag
= _embed_complex_matrix(I
*Eij
- I
*Eij
.transpose())
889 # Normalize these with our inner product before handing them back.
890 # And since we embedded them, we can drop back to the "field" that
891 # we started with instead of the complex extension "F".
892 return tuple( (s
/ _complex_hermitian_matrix_ip(s
,s
).sqrt()).change_ring(field
)
897 def _quaternion_hermitian_basis(n
, field
):
899 Returns a basis for the space of quaternion Hermitian n-by-n matrices.
901 Why do we embed these? Basically, because all of numerical linear
902 algebra assumes that you're working with vectors consisting of `n`
903 entries from a field and scalars from the same field. There's no way
904 to tell SageMath that (for example) the vectors contain complex
905 numbers, while the scalar field is real.
909 sage: from mjo.eja.eja_algebra import _quaternion_hermitian_basis
913 sage: set_random_seed()
914 sage: n = ZZ.random_element(1,5)
915 sage: B = _quaternion_hermitian_basis(n, QQ)
916 sage: all( M.is_symmetric() for M in B )
920 Q
= QuaternionAlgebra(QQ
,-1,-1)
923 # This is like the symmetric case, but we need to be careful:
925 # * We want conjugate-symmetry, not just symmetry.
926 # * The diagonal will (as a result) be real.
930 for j
in xrange(i
+1):
931 Eij
= matrix(Q
, n
, lambda k
,l
: k
==i
and l
==j
)
933 Sij
= _embed_quaternion_matrix(Eij
)
936 # Beware, orthogonal but not normalized! The second,
937 # third, and fourth ones have a minus because they're
939 Sij_real
= _embed_quaternion_matrix(Eij
+ Eij
.transpose())
941 Sij_I
= _embed_quaternion_matrix(I
*Eij
- I
*Eij
.transpose())
943 Sij_J
= _embed_quaternion_matrix(J
*Eij
- J
*Eij
.transpose())
945 Sij_K
= _embed_quaternion_matrix(K
*Eij
- K
*Eij
.transpose())
951 def _multiplication_table_from_matrix_basis(basis
):
953 At least three of the five simple Euclidean Jordan algebras have the
954 symmetric multiplication (A,B) |-> (AB + BA)/2, where the
955 multiplication on the right is matrix multiplication. Given a basis
956 for the underlying matrix space, this function returns a
957 multiplication table (obtained by looping through the basis
958 elements) for an algebra of those matrices.
960 # In S^2, for example, we nominally have four coordinates even
961 # though the space is of dimension three only. The vector space V
962 # is supposed to hold the entire long vector, and the subspace W
963 # of V will be spanned by the vectors that arise from symmetric
964 # matrices. Thus for S^2, dim(V) == 4 and dim(W) == 3.
965 field
= basis
[0].base_ring()
966 dimension
= basis
[0].nrows()
968 V
= VectorSpace(field
, dimension
**2)
969 W
= V
.span_of_basis( _mat2vec(s
) for s
in basis
)
971 mult_table
= [[W
.zero() for j
in range(n
)] for i
in range(n
)]
974 mat_entry
= (basis
[i
]*basis
[j
] + basis
[j
]*basis
[i
])/2
975 mult_table
[i
][j
] = W
.coordinate_vector(_mat2vec(mat_entry
))
980 def _embed_complex_matrix(M
):
982 Embed the n-by-n complex matrix ``M`` into the space of real
983 matrices of size 2n-by-2n via the map the sends each entry `z = a +
984 bi` to the block matrix ``[[a,b],[-b,a]]``.
988 sage: from mjo.eja.eja_algebra import _embed_complex_matrix
992 sage: R = PolynomialRing(QQ, 'z')
994 sage: F = NumberField(z**2 + 1, 'i', embedding=CLF(-1).sqrt())
995 sage: x1 = F(4 - 2*i)
996 sage: x2 = F(1 + 2*i)
999 sage: M = matrix(F,2,[[x1,x2],[x3,x4]])
1000 sage: _embed_complex_matrix(M)
1009 Embedding is a homomorphism (isomorphism, in fact)::
1011 sage: set_random_seed()
1012 sage: n = ZZ.random_element(5)
1013 sage: R = PolynomialRing(QQ, 'z')
1015 sage: F = NumberField(z**2 + 1, 'i', embedding=CLF(-1).sqrt())
1016 sage: X = random_matrix(F, n)
1017 sage: Y = random_matrix(F, n)
1018 sage: actual = _embed_complex_matrix(X) * _embed_complex_matrix(Y)
1019 sage: expected = _embed_complex_matrix(X*Y)
1020 sage: actual == expected
1026 raise ValueError("the matrix 'M' must be square")
1027 field
= M
.base_ring()
1030 a
= z
.vector()[0] # real part, I guess
1031 b
= z
.vector()[1] # imag part, I guess
1032 blocks
.append(matrix(field
, 2, [[a
,b
],[-b
,a
]]))
1034 # We can drop the imaginaries here.
1035 return matrix
.block(field
.base_ring(), n
, blocks
)
1038 def _unembed_complex_matrix(M
):
1040 The inverse of _embed_complex_matrix().
1044 sage: from mjo.eja.eja_algebra import (_embed_complex_matrix,
1045 ....: _unembed_complex_matrix)
1049 sage: A = matrix(QQ,[ [ 1, 2, 3, 4],
1050 ....: [-2, 1, -4, 3],
1051 ....: [ 9, 10, 11, 12],
1052 ....: [-10, 9, -12, 11] ])
1053 sage: _unembed_complex_matrix(A)
1055 [ 10*i + 9 12*i + 11]
1059 Unembedding is the inverse of embedding::
1061 sage: set_random_seed()
1062 sage: R = PolynomialRing(QQ, 'z')
1064 sage: F = NumberField(z**2 + 1, 'i', embedding=CLF(-1).sqrt())
1065 sage: M = random_matrix(F, 3)
1066 sage: _unembed_complex_matrix(_embed_complex_matrix(M)) == M
1072 raise ValueError("the matrix 'M' must be square")
1073 if not n
.mod(2).is_zero():
1074 raise ValueError("the matrix 'M' must be a complex embedding")
1076 field
= M
.base_ring() # This should already have sqrt2
1077 R
= PolynomialRing(field
, 'z')
1079 F
= NumberField(z
**2 + 1,'i', embedding
=CLF(-1).sqrt())
1082 # Go top-left to bottom-right (reading order), converting every
1083 # 2-by-2 block we see to a single complex element.
1085 for k
in xrange(n
/2):
1086 for j
in xrange(n
/2):
1087 submat
= M
[2*k
:2*k
+2,2*j
:2*j
+2]
1088 if submat
[0,0] != submat
[1,1]:
1089 raise ValueError('bad on-diagonal submatrix')
1090 if submat
[0,1] != -submat
[1,0]:
1091 raise ValueError('bad off-diagonal submatrix')
1092 z
= submat
[0,0] + submat
[0,1]*i
1095 return matrix(F
, n
/2, elements
)
1098 def _embed_quaternion_matrix(M
):
1100 Embed the n-by-n quaternion matrix ``M`` into the space of real
1101 matrices of size 4n-by-4n by first sending each quaternion entry
1102 `z = a + bi + cj + dk` to the block-complex matrix
1103 ``[[a + bi, c+di],[-c + di, a-bi]]`, and then embedding those into
1108 sage: from mjo.eja.eja_algebra import _embed_quaternion_matrix
1112 sage: Q = QuaternionAlgebra(QQ,-1,-1)
1113 sage: i,j,k = Q.gens()
1114 sage: x = 1 + 2*i + 3*j + 4*k
1115 sage: M = matrix(Q, 1, [[x]])
1116 sage: _embed_quaternion_matrix(M)
1122 Embedding is a homomorphism (isomorphism, in fact)::
1124 sage: set_random_seed()
1125 sage: n = ZZ.random_element(5)
1126 sage: Q = QuaternionAlgebra(QQ,-1,-1)
1127 sage: X = random_matrix(Q, n)
1128 sage: Y = random_matrix(Q, n)
1129 sage: actual = _embed_quaternion_matrix(X)*_embed_quaternion_matrix(Y)
1130 sage: expected = _embed_quaternion_matrix(X*Y)
1131 sage: actual == expected
1135 quaternions
= M
.base_ring()
1138 raise ValueError("the matrix 'M' must be square")
1140 R
= PolynomialRing(QQ
, 'z')
1142 F
= NumberField(z
**2 + 1, 'i', embedding
=CLF(-1).sqrt())
1147 t
= z
.coefficient_tuple()
1152 cplx_matrix
= matrix(F
, 2, [[ a
+ b
*i
, c
+ d
*i
],
1153 [-c
+ d
*i
, a
- b
*i
]])
1154 blocks
.append(_embed_complex_matrix(cplx_matrix
))
1156 # We should have real entries by now, so use the realest field
1157 # we've got for the return value.
1158 return matrix
.block(quaternions
.base_ring(), n
, blocks
)
1161 def _unembed_quaternion_matrix(M
):
1163 The inverse of _embed_quaternion_matrix().
1167 sage: from mjo.eja.eja_algebra import (_embed_quaternion_matrix,
1168 ....: _unembed_quaternion_matrix)
1172 sage: M = matrix(QQ, [[ 1, 2, 3, 4],
1173 ....: [-2, 1, -4, 3],
1174 ....: [-3, 4, 1, -2],
1175 ....: [-4, -3, 2, 1]])
1176 sage: _unembed_quaternion_matrix(M)
1177 [1 + 2*i + 3*j + 4*k]
1181 Unembedding is the inverse of embedding::
1183 sage: set_random_seed()
1184 sage: Q = QuaternionAlgebra(QQ, -1, -1)
1185 sage: M = random_matrix(Q, 3)
1186 sage: _unembed_quaternion_matrix(_embed_quaternion_matrix(M)) == M
1192 raise ValueError("the matrix 'M' must be square")
1193 if not n
.mod(4).is_zero():
1194 raise ValueError("the matrix 'M' must be a complex embedding")
1196 Q
= QuaternionAlgebra(QQ
,-1,-1)
1199 # Go top-left to bottom-right (reading order), converting every
1200 # 4-by-4 block we see to a 2-by-2 complex block, to a 1-by-1
1203 for l
in xrange(n
/4):
1204 for m
in xrange(n
/4):
1205 submat
= _unembed_complex_matrix(M
[4*l
:4*l
+4,4*m
:4*m
+4])
1206 if submat
[0,0] != submat
[1,1].conjugate():
1207 raise ValueError('bad on-diagonal submatrix')
1208 if submat
[0,1] != -submat
[1,0].conjugate():
1209 raise ValueError('bad off-diagonal submatrix')
1210 z
= submat
[0,0].real() + submat
[0,0].imag()*i
1211 z
+= submat
[0,1].real()*j
+ submat
[0,1].imag()*k
1214 return matrix(Q
, n
/4, elements
)
1217 # The usual inner product on R^n.
1219 return x
.to_vector().inner_product(y
.to_vector())
1221 # The inner product used for the real symmetric simple EJA.
1222 # We keep it as a separate function because e.g. the complex
1223 # algebra uses the same inner product, except divided by 2.
1224 def _matrix_ip(X
,Y
):
1225 X_mat
= X
.natural_representation()
1226 Y_mat
= Y
.natural_representation()
1227 return (X_mat
*Y_mat
).trace()
1229 def _real_symmetric_matrix_ip(X
,Y
):
1230 return (X
*Y
).trace()
1232 def _complex_hermitian_matrix_ip(X
,Y
):
1233 # This takes EMBEDDED matrices.
1234 Xu
= _unembed_complex_matrix(X
)
1235 Yu
= _unembed_complex_matrix(Y
)
1236 # The trace need not be real; consider Xu = (i*I) and Yu = I.
1237 return ((Xu
*Yu
).trace()).vector()[0] # real part, I guess
1239 class RealSymmetricEJA(FiniteDimensionalEuclideanJordanAlgebra
):
1241 The rank-n simple EJA consisting of real symmetric n-by-n
1242 matrices, the usual symmetric Jordan product, and the trace inner
1243 product. It has dimension `(n^2 + n)/2` over the reals.
1247 sage: from mjo.eja.eja_algebra import RealSymmetricEJA
1251 sage: J = RealSymmetricEJA(2)
1252 sage: e0, e1, e2 = J.gens()
1262 The dimension of this algebra is `(n^2 + n) / 2`::
1264 sage: set_random_seed()
1265 sage: n = ZZ.random_element(1,5)
1266 sage: J = RealSymmetricEJA(n)
1267 sage: J.dimension() == (n^2 + n)/2
1270 The Jordan multiplication is what we think it is::
1272 sage: set_random_seed()
1273 sage: n = ZZ.random_element(1,5)
1274 sage: J = RealSymmetricEJA(n)
1275 sage: x = J.random_element()
1276 sage: y = J.random_element()
1277 sage: actual = (x*y).natural_representation()
1278 sage: X = x.natural_representation()
1279 sage: Y = y.natural_representation()
1280 sage: expected = (X*Y + Y*X)/2
1281 sage: actual == expected
1283 sage: J(expected) == x*y
1286 We can change the generator prefix::
1288 sage: RealSymmetricEJA(3, prefix='q').gens()
1289 (q0, q1, q2, q3, q4, q5)
1291 Our inner product satisfies the Jordan axiom::
1293 sage: set_random_seed()
1294 sage: n = ZZ.random_element(1,5)
1295 sage: J = RealSymmetricEJA(n)
1296 sage: x = J.random_element()
1297 sage: y = J.random_element()
1298 sage: z = J.random_element()
1299 sage: (x*y).inner_product(z) == y.inner_product(x*z)
1302 Our basis is normalized with respect to the natural inner product::
1304 sage: set_random_seed()
1305 sage: n = ZZ.random_element(1,5)
1306 sage: J = RealSymmetricEJA(n)
1307 sage: all( b.norm() == 1 for b in J.gens() )
1310 Left-multiplication operators are symmetric because they satisfy
1313 sage: set_random_seed()
1314 sage: n = ZZ.random_element(1,5)
1315 sage: x = RealSymmetricEJA(n).random_element()
1316 sage: x.operator().matrix().is_symmetric()
1320 def __init__(self
, n
, field
=QQ
, **kwargs
):
1322 # We'll need sqrt(2) to normalize the basis, and this
1323 # winds up in the multiplication table, so the whole
1324 # algebra needs to be over the field extension.
1325 R
= PolynomialRing(field
, 'z')
1327 field
= NumberField(z
**2 - 2, 'sqrt2', embedding
=RLF(2).sqrt())
1329 S
= _real_symmetric_basis(n
, field
)
1330 Qs
= _multiplication_table_from_matrix_basis(S
)
1332 fdeja
= super(RealSymmetricEJA
, self
)
1333 return fdeja
.__init
__(field
,
1339 def inner_product(self
, x
, y
):
1340 X
= x
.natural_representation()
1341 Y
= y
.natural_representation()
1342 return _real_symmetric_matrix_ip(X
,Y
)
1345 class ComplexHermitianEJA(FiniteDimensionalEuclideanJordanAlgebra
):
1347 The rank-n simple EJA consisting of complex Hermitian n-by-n
1348 matrices over the real numbers, the usual symmetric Jordan product,
1349 and the real-part-of-trace inner product. It has dimension `n^2` over
1354 sage: from mjo.eja.eja_algebra import ComplexHermitianEJA
1358 The dimension of this algebra is `n^2`::
1360 sage: set_random_seed()
1361 sage: n = ZZ.random_element(1,5)
1362 sage: J = ComplexHermitianEJA(n)
1363 sage: J.dimension() == n^2
1366 The Jordan multiplication is what we think it is::
1368 sage: set_random_seed()
1369 sage: n = ZZ.random_element(1,5)
1370 sage: J = ComplexHermitianEJA(n)
1371 sage: x = J.random_element()
1372 sage: y = J.random_element()
1373 sage: actual = (x*y).natural_representation()
1374 sage: X = x.natural_representation()
1375 sage: Y = y.natural_representation()
1376 sage: expected = (X*Y + Y*X)/2
1377 sage: actual == expected
1379 sage: J(expected) == x*y
1382 We can change the generator prefix::
1384 sage: ComplexHermitianEJA(2, prefix='z').gens()
1387 Our inner product satisfies the Jordan axiom::
1389 sage: set_random_seed()
1390 sage: n = ZZ.random_element(1,5)
1391 sage: J = ComplexHermitianEJA(n)
1392 sage: x = J.random_element()
1393 sage: y = J.random_element()
1394 sage: z = J.random_element()
1395 sage: (x*y).inner_product(z) == y.inner_product(x*z)
1398 Our basis is normalized with respect to the natural inner product::
1400 sage: set_random_seed()
1401 sage: n = ZZ.random_element(1,4)
1402 sage: J = ComplexHermitianEJA(n)
1403 sage: all( b.norm() == 1 for b in J.gens() )
1406 Left-multiplication operators are symmetric because they satisfy
1409 sage: set_random_seed()
1410 sage: n = ZZ.random_element(1,5)
1411 sage: x = ComplexHermitianEJA(n).random_element()
1412 sage: x.operator().matrix().is_symmetric()
1416 def __init__(self
, n
, field
=QQ
, **kwargs
):
1418 # We'll need sqrt(2) to normalize the basis, and this
1419 # winds up in the multiplication table, so the whole
1420 # algebra needs to be over the field extension.
1421 R
= PolynomialRing(field
, 'z')
1423 field
= NumberField(z
**2 - 2, 'sqrt2', embedding
=RLF(2).sqrt())
1424 S
= _complex_hermitian_basis(n
, field
)
1425 Qs
= _multiplication_table_from_matrix_basis(S
)
1427 fdeja
= super(ComplexHermitianEJA
, self
)
1428 return fdeja
.__init
__(field
,
1435 def inner_product(self
, x
, y
):
1436 X
= x
.natural_representation()
1437 Y
= y
.natural_representation()
1438 return _complex_hermitian_matrix_ip(X
,Y
)
1441 class QuaternionHermitianEJA(FiniteDimensionalEuclideanJordanAlgebra
):
1443 The rank-n simple EJA consisting of self-adjoint n-by-n quaternion
1444 matrices, the usual symmetric Jordan product, and the
1445 real-part-of-trace inner product. It has dimension `2n^2 - n` over
1450 sage: from mjo.eja.eja_algebra import QuaternionHermitianEJA
1454 The dimension of this algebra is `n^2`::
1456 sage: set_random_seed()
1457 sage: n = ZZ.random_element(1,5)
1458 sage: J = QuaternionHermitianEJA(n)
1459 sage: J.dimension() == 2*(n^2) - n
1462 The Jordan multiplication is what we think it is::
1464 sage: set_random_seed()
1465 sage: n = ZZ.random_element(1,5)
1466 sage: J = QuaternionHermitianEJA(n)
1467 sage: x = J.random_element()
1468 sage: y = J.random_element()
1469 sage: actual = (x*y).natural_representation()
1470 sage: X = x.natural_representation()
1471 sage: Y = y.natural_representation()
1472 sage: expected = (X*Y + Y*X)/2
1473 sage: actual == expected
1475 sage: J(expected) == x*y
1478 We can change the generator prefix::
1480 sage: QuaternionHermitianEJA(2, prefix='a').gens()
1481 (a0, a1, a2, a3, a4, a5)
1483 Our inner product satisfies the Jordan axiom::
1485 sage: set_random_seed()
1486 sage: n = ZZ.random_element(1,5)
1487 sage: J = QuaternionHermitianEJA(n)
1488 sage: x = J.random_element()
1489 sage: y = J.random_element()
1490 sage: z = J.random_element()
1491 sage: (x*y).inner_product(z) == y.inner_product(x*z)
1495 def __init__(self
, n
, field
=QQ
, **kwargs
):
1496 S
= _quaternion_hermitian_basis(n
, field
)
1497 Qs
= _multiplication_table_from_matrix_basis(S
)
1499 fdeja
= super(QuaternionHermitianEJA
, self
)
1500 return fdeja
.__init
__(field
,
1506 def inner_product(self
, x
, y
):
1507 # Since a+bi+cj+dk on the diagonal is represented as
1509 # a + bi +cj + dk = [ a b c d]
1514 # we'll quadruple-count the "a" entries if we take the trace of
1516 return _matrix_ip(x
,y
)/4
1519 class JordanSpinEJA(FiniteDimensionalEuclideanJordanAlgebra
):
1521 The rank-2 simple EJA consisting of real vectors ``x=(x0, x_bar)``
1522 with the usual inner product and jordan product ``x*y =
1523 (<x_bar,y_bar>, x0*y_bar + y0*x_bar)``. It has dimension `n` over
1528 sage: from mjo.eja.eja_algebra import JordanSpinEJA
1532 This multiplication table can be verified by hand::
1534 sage: J = JordanSpinEJA(4)
1535 sage: e0,e1,e2,e3 = J.gens()
1551 We can change the generator prefix::
1553 sage: JordanSpinEJA(2, prefix='B').gens()
1556 Our inner product satisfies the Jordan axiom::
1558 sage: set_random_seed()
1559 sage: n = ZZ.random_element(1,5)
1560 sage: J = JordanSpinEJA(n)
1561 sage: x = J.random_element()
1562 sage: y = J.random_element()
1563 sage: z = J.random_element()
1564 sage: (x*y).inner_product(z) == y.inner_product(x*z)
1568 def __init__(self
, n
, field
=QQ
, **kwargs
):
1569 V
= VectorSpace(field
, n
)
1570 mult_table
= [[V
.zero() for j
in range(n
)] for i
in range(n
)]
1580 z0
= x
.inner_product(y
)
1581 zbar
= y0
*xbar
+ x0
*ybar
1582 z
= V([z0
] + zbar
.list())
1583 mult_table
[i
][j
] = z
1585 # The rank of the spin algebra is two, unless we're in a
1586 # one-dimensional ambient space (because the rank is bounded by
1587 # the ambient dimension).
1588 fdeja
= super(JordanSpinEJA
, self
)
1589 return fdeja
.__init
__(field
, mult_table
, rank
=min(n
,2), **kwargs
)
1591 def inner_product(self
, x
, y
):
1592 return _usual_ip(x
,y
)