]> gitweb.michael.orlitzky.com - sage.d.git/blob - mjo/eja/eja_algebra.py
eja: check that the multiplication table is square with check=True.
[sage.d.git] / mjo / eja / eja_algebra.py
1 """
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.
6 """
7
8 from itertools import repeat
9
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, AA, QQbar, RR, RLF, CLF,
21 PolynomialRing,
22 QuadraticField)
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
27
28 class FiniteDimensionalEuclideanJordanAlgebra(CombinatorialFreeModule):
29
30 def _coerce_map_from_base_ring(self):
31 """
32 Disable the map from the base ring into the algebra.
33
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.
37
38 SETUP::
39
40 sage: from mjo.eja.eja_algebra import random_eja
41
42 TESTS::
43
44 sage: set_random_seed()
45 sage: J = random_eja()
46 sage: J(1)
47 Traceback (most recent call last):
48 ...
49 ValueError: not a naturally-represented algebra element
50
51 """
52 return None
53
54 def __init__(self,
55 field,
56 mult_table,
57 prefix='e',
58 category=None,
59 natural_basis=None,
60 check=True):
61 """
62 SETUP::
63
64 sage: from mjo.eja.eja_algebra import (
65 ....: FiniteDimensionalEuclideanJordanAlgebra,
66 ....: JordanSpinEJA,
67 ....: random_eja)
68
69 EXAMPLES:
70
71 By definition, Jordan multiplication commutes::
72
73 sage: set_random_seed()
74 sage: J = random_eja()
75 sage: x,y = J.random_elements(2)
76 sage: x*y == y*x
77 True
78
79 TESTS:
80
81 The ``field`` we're given must be real with ``check=True``::
82
83 sage: JordanSpinEJA(2,QQbar)
84 Traceback (most recent call last):
85 ...
86 ValueError: field is not real
87
88 The multiplication table must be square with ``check=True``::
89
90 sage: FiniteDimensionalEuclideanJordanAlgebra(QQ,((),()))
91 Traceback (most recent call last):
92 ...
93 ValueError: multiplication table is not square
94
95 """
96 if check:
97 if not field.is_subring(RR):
98 # Note: this does return true for the real algebraic
99 # field, and any quadratic field where we've specified
100 # a real embedding.
101 raise ValueError('field is not real')
102
103 self._natural_basis = natural_basis
104
105 if category is None:
106 category = MagmaticAlgebras(field).FiniteDimensional()
107 category = category.WithBasis().Unital()
108
109 # The multiplication table had better be square
110 n = len(mult_table)
111 if check:
112 if not all( len(l) == n for l in mult_table ):
113 raise ValueError("multiplication table is not square")
114
115 fda = super(FiniteDimensionalEuclideanJordanAlgebra, self)
116 fda.__init__(field,
117 range(n),
118 prefix=prefix,
119 category=category)
120 self.print_options(bracket='')
121
122 # The multiplication table we're given is necessarily in terms
123 # of vectors, because we don't have an algebra yet for
124 # anything to be an element of. However, it's faster in the
125 # long run to have the multiplication table be in terms of
126 # algebra elements. We do this after calling the superclass
127 # constructor so that from_vector() knows what to do.
128 self._multiplication_table = [
129 list(map(lambda x: self.from_vector(x), ls))
130 for ls in mult_table
131 ]
132
133 if check:
134 if not self._is_commutative():
135 raise ValueError("algebra is not commutative")
136 if not self._is_jordanian():
137 raise ValueError("Jordan identity does not hold")
138 if not self._inner_product_is_associative():
139 raise ValueError("inner product is not associative")
140
141 def _element_constructor_(self, elt):
142 """
143 Construct an element of this algebra from its natural
144 representation.
145
146 This gets called only after the parent element _call_ method
147 fails to find a coercion for the argument.
148
149 SETUP::
150
151 sage: from mjo.eja.eja_algebra import (JordanSpinEJA,
152 ....: HadamardEJA,
153 ....: RealSymmetricEJA)
154
155 EXAMPLES:
156
157 The identity in `S^n` is converted to the identity in the EJA::
158
159 sage: J = RealSymmetricEJA(3)
160 sage: I = matrix.identity(QQ,3)
161 sage: J(I) == J.one()
162 True
163
164 This skew-symmetric matrix can't be represented in the EJA::
165
166 sage: J = RealSymmetricEJA(3)
167 sage: A = matrix(QQ,3, lambda i,j: i-j)
168 sage: J(A)
169 Traceback (most recent call last):
170 ...
171 ArithmeticError: vector is not in free module
172
173 TESTS:
174
175 Ensure that we can convert any element of the two non-matrix
176 simple algebras (whose natural representations are their usual
177 vector representations) back and forth faithfully::
178
179 sage: set_random_seed()
180 sage: J = HadamardEJA.random_instance()
181 sage: x = J.random_element()
182 sage: J(x.to_vector().column()) == x
183 True
184 sage: J = JordanSpinEJA.random_instance()
185 sage: x = J.random_element()
186 sage: J(x.to_vector().column()) == x
187 True
188
189 """
190 msg = "not a naturally-represented algebra element"
191 if elt == 0:
192 # The superclass implementation of random_element()
193 # needs to be able to coerce "0" into the algebra.
194 return self.zero()
195 elif elt in self.base_ring():
196 # Ensure that no base ring -> algebra coercion is performed
197 # by this method. There's some stupidity in sage that would
198 # otherwise propagate to this method; for example, sage thinks
199 # that the integer 3 belongs to the space of 2-by-2 matrices.
200 raise ValueError(msg)
201
202 natural_basis = self.natural_basis()
203 basis_space = natural_basis[0].matrix_space()
204 if elt not in basis_space:
205 raise ValueError(msg)
206
207 # Thanks for nothing! Matrix spaces aren't vector spaces in
208 # Sage, so we have to figure out its natural-basis coordinates
209 # ourselves. We use the basis space's ring instead of the
210 # element's ring because the basis space might be an algebraic
211 # closure whereas the base ring of the 3-by-3 identity matrix
212 # could be QQ instead of QQbar.
213 V = VectorSpace(basis_space.base_ring(), elt.nrows()*elt.ncols())
214 W = V.span_of_basis( _mat2vec(s) for s in natural_basis )
215 coords = W.coordinate_vector(_mat2vec(elt))
216 return self.from_vector(coords)
217
218 @staticmethod
219 def _max_test_case_size():
220 """
221 Return an integer "size" that is an upper bound on the size of
222 this algebra when it is used in a random test
223 case. Unfortunately, the term "size" is quite vague -- when
224 dealing with `R^n` under either the Hadamard or Jordan spin
225 product, the "size" refers to the dimension `n`. When dealing
226 with a matrix algebra (real symmetric or complex/quaternion
227 Hermitian), it refers to the size of the matrix, which is
228 far less than the dimension of the underlying vector space.
229
230 We default to five in this class, which is safe in `R^n`. The
231 matrix algebra subclasses (or any class where the "size" is
232 interpreted to be far less than the dimension) should override
233 with a smaller number.
234 """
235 return 5
236
237 def _repr_(self):
238 """
239 Return a string representation of ``self``.
240
241 SETUP::
242
243 sage: from mjo.eja.eja_algebra import JordanSpinEJA
244
245 TESTS:
246
247 Ensure that it says what we think it says::
248
249 sage: JordanSpinEJA(2, field=AA)
250 Euclidean Jordan algebra of dimension 2 over Algebraic Real Field
251 sage: JordanSpinEJA(3, field=RDF)
252 Euclidean Jordan algebra of dimension 3 over Real Double Field
253
254 """
255 fmt = "Euclidean Jordan algebra of dimension {} over {}"
256 return fmt.format(self.dimension(), self.base_ring())
257
258 def product_on_basis(self, i, j):
259 return self._multiplication_table[i][j]
260
261 def _is_commutative(self):
262 r"""
263 Whether or not this algebra's multiplication table is commutative.
264
265 This method should of course always return ``True``, unless
266 this algebra was constructed with ``check=False`` and passed
267 an invalid multiplication table.
268 """
269 return all( self.product_on_basis(i,j) == self.product_on_basis(i,j)
270 for i in range(self.dimension())
271 for j in range(self.dimension()) )
272
273 def _is_jordanian(self):
274 r"""
275 Whether or not this algebra's multiplication table respects the
276 Jordan identity `(x^{2})(xy) = x(x^{2}y)`.
277
278 We only check one arrangement of `x` and `y`, so for a
279 ``True`` result to be truly true, you should also check
280 :meth:`_is_commutative`. This method should of course always
281 return ``True``, unless this algebra was constructed with
282 ``check=False`` and passed an invalid multiplication table.
283 """
284 return all( (self.monomial(i)**2)*(self.monomial(i)*self.monomial(j))
285 ==
286 (self.monomial(i))*((self.monomial(i)**2)*self.monomial(j))
287 for i in range(self.dimension())
288 for j in range(self.dimension()) )
289
290 def _inner_product_is_associative(self):
291 r"""
292 Return whether or not this algebra's inner product `B` is
293 associative; that is, whether or not `B(xy,z) = B(x,yz)`.
294
295 This method should of course always return ``True``, unless
296 this algebra was constructed with ``check=False`` and passed
297 an invalid multiplication table.
298 """
299 for i in range(self.dimension()):
300 for j in range(self.dimension()):
301 for k in range(self.dimension()):
302 x = self.monomial(i)
303 y = self.monomial(j)
304 z = self.monomial(k)
305 if (x*y).inner_product(z) != x.inner_product(y*z):
306 return False
307
308 return True
309
310 @cached_method
311 def characteristic_polynomial_of(self):
312 """
313 Return the algebra's "characteristic polynomial of" function,
314 which is itself a multivariate polynomial that, when evaluated
315 at the coordinates of some algebra element, returns that
316 element's characteristic polynomial.
317
318 The resulting polynomial has `n+1` variables, where `n` is the
319 dimension of this algebra. The first `n` variables correspond to
320 the coordinates of an algebra element: when evaluated at the
321 coordinates of an algebra element with respect to a certain
322 basis, the result is a univariate polynomial (in the one
323 remaining variable ``t``), namely the characteristic polynomial
324 of that element.
325
326 SETUP::
327
328 sage: from mjo.eja.eja_algebra import JordanSpinEJA, TrivialEJA
329
330 EXAMPLES:
331
332 The characteristic polynomial in the spin algebra is given in
333 Alizadeh, Example 11.11::
334
335 sage: J = JordanSpinEJA(3)
336 sage: p = J.characteristic_polynomial_of(); p
337 X1^2 - X2^2 - X3^2 + (-2*t)*X1 + t^2
338 sage: xvec = J.one().to_vector()
339 sage: p(*xvec)
340 t^2 - 2*t + 1
341
342 By definition, the characteristic polynomial is a monic
343 degree-zero polynomial in a rank-zero algebra. Note that
344 Cayley-Hamilton is indeed satisfied since the polynomial
345 ``1`` evaluates to the identity element of the algebra on
346 any argument::
347
348 sage: J = TrivialEJA()
349 sage: J.characteristic_polynomial_of()
350 1
351
352 """
353 r = self.rank()
354 n = self.dimension()
355
356 # The list of coefficient polynomials a_0, a_1, a_2, ..., a_(r-1).
357 a = self._charpoly_coefficients()
358
359 # We go to a bit of trouble here to reorder the
360 # indeterminates, so that it's easier to evaluate the
361 # characteristic polynomial at x's coordinates and get back
362 # something in terms of t, which is what we want.
363 S = PolynomialRing(self.base_ring(),'t')
364 t = S.gen(0)
365 if r > 0:
366 R = a[0].parent()
367 S = PolynomialRing(S, R.variable_names())
368 t = S(t)
369
370 return (t**r + sum( a[k]*(t**k) for k in range(r) ))
371
372
373 def inner_product(self, x, y):
374 """
375 The inner product associated with this Euclidean Jordan algebra.
376
377 Defaults to the trace inner product, but can be overridden by
378 subclasses if they are sure that the necessary properties are
379 satisfied.
380
381 SETUP::
382
383 sage: from mjo.eja.eja_algebra import random_eja
384
385 EXAMPLES:
386
387 Our inner product is "associative," which means the following for
388 a symmetric bilinear form::
389
390 sage: set_random_seed()
391 sage: J = random_eja()
392 sage: x,y,z = J.random_elements(3)
393 sage: (x*y).inner_product(z) == y.inner_product(x*z)
394 True
395
396 """
397 X = x.natural_representation()
398 Y = y.natural_representation()
399 return self.natural_inner_product(X,Y)
400
401
402 def is_trivial(self):
403 """
404 Return whether or not this algebra is trivial.
405
406 A trivial algebra contains only the zero element.
407
408 SETUP::
409
410 sage: from mjo.eja.eja_algebra import (ComplexHermitianEJA,
411 ....: TrivialEJA)
412
413 EXAMPLES::
414
415 sage: J = ComplexHermitianEJA(3)
416 sage: J.is_trivial()
417 False
418
419 ::
420
421 sage: J = TrivialEJA()
422 sage: J.is_trivial()
423 True
424
425 """
426 return self.dimension() == 0
427
428
429 def multiplication_table(self):
430 """
431 Return a visual representation of this algebra's multiplication
432 table (on basis elements).
433
434 SETUP::
435
436 sage: from mjo.eja.eja_algebra import JordanSpinEJA
437
438 EXAMPLES::
439
440 sage: J = JordanSpinEJA(4)
441 sage: J.multiplication_table()
442 +----++----+----+----+----+
443 | * || e0 | e1 | e2 | e3 |
444 +====++====+====+====+====+
445 | e0 || e0 | e1 | e2 | e3 |
446 +----++----+----+----+----+
447 | e1 || e1 | e0 | 0 | 0 |
448 +----++----+----+----+----+
449 | e2 || e2 | 0 | e0 | 0 |
450 +----++----+----+----+----+
451 | e3 || e3 | 0 | 0 | e0 |
452 +----++----+----+----+----+
453
454 """
455 M = list(self._multiplication_table) # copy
456 for i in range(len(M)):
457 # M had better be "square"
458 M[i] = [self.monomial(i)] + M[i]
459 M = [["*"] + list(self.gens())] + M
460 return table(M, header_row=True, header_column=True, frame=True)
461
462
463 def natural_basis(self):
464 """
465 Return a more-natural representation of this algebra's basis.
466
467 Every finite-dimensional Euclidean Jordan Algebra is a direct
468 sum of five simple algebras, four of which comprise Hermitian
469 matrices. This method returns the original "natural" basis
470 for our underlying vector space. (Typically, the natural basis
471 is used to construct the multiplication table in the first place.)
472
473 Note that this will always return a matrix. The standard basis
474 in `R^n` will be returned as `n`-by-`1` column matrices.
475
476 SETUP::
477
478 sage: from mjo.eja.eja_algebra import (JordanSpinEJA,
479 ....: RealSymmetricEJA)
480
481 EXAMPLES::
482
483 sage: J = RealSymmetricEJA(2)
484 sage: J.basis()
485 Finite family {0: e0, 1: e1, 2: e2}
486 sage: J.natural_basis()
487 (
488 [1 0] [ 0 0.7071067811865475?] [0 0]
489 [0 0], [0.7071067811865475? 0], [0 1]
490 )
491
492 ::
493
494 sage: J = JordanSpinEJA(2)
495 sage: J.basis()
496 Finite family {0: e0, 1: e1}
497 sage: J.natural_basis()
498 (
499 [1] [0]
500 [0], [1]
501 )
502
503 """
504 if self._natural_basis is None:
505 M = self.natural_basis_space()
506 return tuple( M(b.to_vector()) for b in self.basis() )
507 else:
508 return self._natural_basis
509
510
511 def natural_basis_space(self):
512 """
513 Return the matrix space in which this algebra's natural basis
514 elements live.
515
516 Generally this will be an `n`-by-`1` column-vector space,
517 except when the algebra is trivial. There it's `n`-by-`n`
518 (where `n` is zero), to ensure that two elements of the
519 natural basis space (empty matrices) can be multiplied.
520 """
521 if self.is_trivial():
522 return MatrixSpace(self.base_ring(), 0)
523 elif self._natural_basis is None or len(self._natural_basis) == 0:
524 return MatrixSpace(self.base_ring(), self.dimension(), 1)
525 else:
526 return self._natural_basis[0].matrix_space()
527
528
529 @staticmethod
530 def natural_inner_product(X,Y):
531 """
532 Compute the inner product of two naturally-represented elements.
533
534 For example in the real symmetric matrix EJA, this will compute
535 the trace inner-product of two n-by-n symmetric matrices. The
536 default should work for the real cartesian product EJA, the
537 Jordan spin EJA, and the real symmetric matrices. The others
538 will have to be overridden.
539 """
540 return (X.conjugate_transpose()*Y).trace()
541
542
543 @cached_method
544 def one(self):
545 """
546 Return the unit element of this algebra.
547
548 SETUP::
549
550 sage: from mjo.eja.eja_algebra import (HadamardEJA,
551 ....: random_eja)
552
553 EXAMPLES::
554
555 sage: J = HadamardEJA(5)
556 sage: J.one()
557 e0 + e1 + e2 + e3 + e4
558
559 TESTS:
560
561 The identity element acts like the identity::
562
563 sage: set_random_seed()
564 sage: J = random_eja()
565 sage: x = J.random_element()
566 sage: J.one()*x == x and x*J.one() == x
567 True
568
569 The matrix of the unit element's operator is the identity::
570
571 sage: set_random_seed()
572 sage: J = random_eja()
573 sage: actual = J.one().operator().matrix()
574 sage: expected = matrix.identity(J.base_ring(), J.dimension())
575 sage: actual == expected
576 True
577
578 """
579 # We can brute-force compute the matrices of the operators
580 # that correspond to the basis elements of this algebra.
581 # If some linear combination of those basis elements is the
582 # algebra identity, then the same linear combination of
583 # their matrices has to be the identity matrix.
584 #
585 # Of course, matrices aren't vectors in sage, so we have to
586 # appeal to the "long vectors" isometry.
587 oper_vecs = [ _mat2vec(g.operator().matrix()) for g in self.gens() ]
588
589 # Now we use basis linear algebra to find the coefficients,
590 # of the matrices-as-vectors-linear-combination, which should
591 # work for the original algebra basis too.
592 A = matrix.column(self.base_ring(), oper_vecs)
593
594 # We used the isometry on the left-hand side already, but we
595 # still need to do it for the right-hand side. Recall that we
596 # wanted something that summed to the identity matrix.
597 b = _mat2vec( matrix.identity(self.base_ring(), self.dimension()) )
598
599 # Now if there's an identity element in the algebra, this should work.
600 coeffs = A.solve_right(b)
601 return self.linear_combination(zip(self.gens(), coeffs))
602
603
604 def peirce_decomposition(self, c):
605 """
606 The Peirce decomposition of this algebra relative to the
607 idempotent ``c``.
608
609 In the future, this can be extended to a complete system of
610 orthogonal idempotents.
611
612 INPUT:
613
614 - ``c`` -- an idempotent of this algebra.
615
616 OUTPUT:
617
618 A triple (J0, J5, J1) containing two subalgebras and one subspace
619 of this algebra,
620
621 - ``J0`` -- the algebra on the eigenspace of ``c.operator()``
622 corresponding to the eigenvalue zero.
623
624 - ``J5`` -- the eigenspace (NOT a subalgebra) of ``c.operator()``
625 corresponding to the eigenvalue one-half.
626
627 - ``J1`` -- the algebra on the eigenspace of ``c.operator()``
628 corresponding to the eigenvalue one.
629
630 These are the only possible eigenspaces for that operator, and this
631 algebra is a direct sum of them. The spaces ``J0`` and ``J1`` are
632 orthogonal, and are subalgebras of this algebra with the appropriate
633 restrictions.
634
635 SETUP::
636
637 sage: from mjo.eja.eja_algebra import random_eja, RealSymmetricEJA
638
639 EXAMPLES:
640
641 The canonical example comes from the symmetric matrices, which
642 decompose into diagonal and off-diagonal parts::
643
644 sage: J = RealSymmetricEJA(3)
645 sage: C = matrix(QQ, [ [1,0,0],
646 ....: [0,1,0],
647 ....: [0,0,0] ])
648 sage: c = J(C)
649 sage: J0,J5,J1 = J.peirce_decomposition(c)
650 sage: J0
651 Euclidean Jordan algebra of dimension 1...
652 sage: J5
653 Vector space of degree 6 and dimension 2...
654 sage: J1
655 Euclidean Jordan algebra of dimension 3...
656 sage: J0.one().natural_representation()
657 [0 0 0]
658 [0 0 0]
659 [0 0 1]
660 sage: orig_df = AA.options.display_format
661 sage: AA.options.display_format = 'radical'
662 sage: J.from_vector(J5.basis()[0]).natural_representation()
663 [ 0 0 1/2*sqrt(2)]
664 [ 0 0 0]
665 [1/2*sqrt(2) 0 0]
666 sage: J.from_vector(J5.basis()[1]).natural_representation()
667 [ 0 0 0]
668 [ 0 0 1/2*sqrt(2)]
669 [ 0 1/2*sqrt(2) 0]
670 sage: AA.options.display_format = orig_df
671 sage: J1.one().natural_representation()
672 [1 0 0]
673 [0 1 0]
674 [0 0 0]
675
676 TESTS:
677
678 Every algebra decomposes trivially with respect to its identity
679 element::
680
681 sage: set_random_seed()
682 sage: J = random_eja()
683 sage: J0,J5,J1 = J.peirce_decomposition(J.one())
684 sage: J0.dimension() == 0 and J5.dimension() == 0
685 True
686 sage: J1.superalgebra() == J and J1.dimension() == J.dimension()
687 True
688
689 The decomposition is into eigenspaces, and its components are
690 therefore necessarily orthogonal. Moreover, the identity
691 elements in the two subalgebras are the projections onto their
692 respective subspaces of the superalgebra's identity element::
693
694 sage: set_random_seed()
695 sage: J = random_eja()
696 sage: x = J.random_element()
697 sage: if not J.is_trivial():
698 ....: while x.is_nilpotent():
699 ....: x = J.random_element()
700 sage: c = x.subalgebra_idempotent()
701 sage: J0,J5,J1 = J.peirce_decomposition(c)
702 sage: ipsum = 0
703 sage: for (w,y,z) in zip(J0.basis(), J5.basis(), J1.basis()):
704 ....: w = w.superalgebra_element()
705 ....: y = J.from_vector(y)
706 ....: z = z.superalgebra_element()
707 ....: ipsum += w.inner_product(y).abs()
708 ....: ipsum += w.inner_product(z).abs()
709 ....: ipsum += y.inner_product(z).abs()
710 sage: ipsum
711 0
712 sage: J1(c) == J1.one()
713 True
714 sage: J0(J.one() - c) == J0.one()
715 True
716
717 """
718 if not c.is_idempotent():
719 raise ValueError("element is not idempotent: %s" % c)
720
721 # Default these to what they should be if they turn out to be
722 # trivial, because eigenspaces_left() won't return eigenvalues
723 # corresponding to trivial spaces (e.g. it returns only the
724 # eigenspace corresponding to lambda=1 if you take the
725 # decomposition relative to the identity element).
726 trivial = FiniteDimensionalEuclideanJordanSubalgebra(self, ())
727 J0 = trivial # eigenvalue zero
728 J5 = VectorSpace(self.base_ring(), 0) # eigenvalue one-half
729 J1 = trivial # eigenvalue one
730
731 for (eigval, eigspace) in c.operator().matrix().right_eigenspaces():
732 if eigval == ~(self.base_ring()(2)):
733 J5 = eigspace
734 else:
735 gens = tuple( self.from_vector(b) for b in eigspace.basis() )
736 subalg = FiniteDimensionalEuclideanJordanSubalgebra(self, gens)
737 if eigval == 0:
738 J0 = subalg
739 elif eigval == 1:
740 J1 = subalg
741 else:
742 raise ValueError("unexpected eigenvalue: %s" % eigval)
743
744 return (J0, J5, J1)
745
746
747 def random_element(self, thorough=False):
748 r"""
749 Return a random element of this algebra.
750
751 Our algebra superclass method only returns a linear
752 combination of at most two basis elements. We instead
753 want the vector space "random element" method that
754 returns a more diverse selection.
755
756 INPUT:
757
758 - ``thorough`` -- (boolean; default False) whether or not we
759 should generate irrational coefficients for the random
760 element when our base ring is irrational; this slows the
761 algebra operations to a crawl, but any truly random method
762 should include them
763
764 """
765 # For a general base ring... maybe we can trust this to do the
766 # right thing? Unlikely, but.
767 V = self.vector_space()
768 v = V.random_element()
769
770 if self.base_ring() is AA:
771 # The "random element" method of the algebraic reals is
772 # stupid at the moment, and only returns integers between
773 # -2 and 2, inclusive:
774 #
775 # https://trac.sagemath.org/ticket/30875
776 #
777 # Instead, we implement our own "random vector" method,
778 # and then coerce that into the algebra. We use the vector
779 # space degree here instead of the dimension because a
780 # subalgebra could (for example) be spanned by only two
781 # vectors, each with five coordinates. We need to
782 # generate all five coordinates.
783 if thorough:
784 v *= QQbar.random_element().real()
785 else:
786 v *= QQ.random_element()
787
788 return self.from_vector(V.coordinate_vector(v))
789
790 def random_elements(self, count, thorough=False):
791 """
792 Return ``count`` random elements as a tuple.
793
794 INPUT:
795
796 - ``thorough`` -- (boolean; default False) whether or not we
797 should generate irrational coefficients for the random
798 elements when our base ring is irrational; this slows the
799 algebra operations to a crawl, but any truly random method
800 should include them
801
802 SETUP::
803
804 sage: from mjo.eja.eja_algebra import JordanSpinEJA
805
806 EXAMPLES::
807
808 sage: J = JordanSpinEJA(3)
809 sage: x,y,z = J.random_elements(3)
810 sage: all( [ x in J, y in J, z in J ])
811 True
812 sage: len( J.random_elements(10) ) == 10
813 True
814
815 """
816 return tuple( self.random_element(thorough)
817 for idx in range(count) )
818
819 @classmethod
820 def random_instance(cls, field=AA, **kwargs):
821 """
822 Return a random instance of this type of algebra.
823
824 Beware, this will crash for "most instances" because the
825 constructor below looks wrong.
826 """
827 if cls is TrivialEJA:
828 # The TrivialEJA class doesn't take an "n" argument because
829 # there's only one.
830 return cls(field)
831
832 n = ZZ.random_element(cls._max_test_case_size() + 1)
833 return cls(n, field, **kwargs)
834
835 @cached_method
836 def _charpoly_coefficients(self):
837 r"""
838 The `r` polynomial coefficients of the "characteristic polynomial
839 of" function.
840 """
841 n = self.dimension()
842 var_names = [ "X" + str(z) for z in range(1,n+1) ]
843 R = PolynomialRing(self.base_ring(), var_names)
844 vars = R.gens()
845 F = R.fraction_field()
846
847 def L_x_i_j(i,j):
848 # From a result in my book, these are the entries of the
849 # basis representation of L_x.
850 return sum( vars[k]*self.monomial(k).operator().matrix()[i,j]
851 for k in range(n) )
852
853 L_x = matrix(F, n, n, L_x_i_j)
854
855 r = None
856 if self.rank.is_in_cache():
857 r = self.rank()
858 # There's no need to pad the system with redundant
859 # columns if we *know* they'll be redundant.
860 n = r
861
862 # Compute an extra power in case the rank is equal to
863 # the dimension (otherwise, we would stop at x^(r-1)).
864 x_powers = [ (L_x**k)*self.one().to_vector()
865 for k in range(n+1) ]
866 A = matrix.column(F, x_powers[:n])
867 AE = A.extended_echelon_form()
868 E = AE[:,n:]
869 A_rref = AE[:,:n]
870 if r is None:
871 r = A_rref.rank()
872 b = x_powers[r]
873
874 # The theory says that only the first "r" coefficients are
875 # nonzero, and they actually live in the original polynomial
876 # ring and not the fraction field. We negate them because
877 # in the actual characteristic polynomial, they get moved
878 # to the other side where x^r lives.
879 return -A_rref.solve_right(E*b).change_ring(R)[:r]
880
881 @cached_method
882 def rank(self):
883 r"""
884 Return the rank of this EJA.
885
886 This is a cached method because we know the rank a priori for
887 all of the algebras we can construct. Thus we can avoid the
888 expensive ``_charpoly_coefficients()`` call unless we truly
889 need to compute the whole characteristic polynomial.
890
891 SETUP::
892
893 sage: from mjo.eja.eja_algebra import (HadamardEJA,
894 ....: JordanSpinEJA,
895 ....: RealSymmetricEJA,
896 ....: ComplexHermitianEJA,
897 ....: QuaternionHermitianEJA,
898 ....: random_eja)
899
900 EXAMPLES:
901
902 The rank of the Jordan spin algebra is always two::
903
904 sage: JordanSpinEJA(2).rank()
905 2
906 sage: JordanSpinEJA(3).rank()
907 2
908 sage: JordanSpinEJA(4).rank()
909 2
910
911 The rank of the `n`-by-`n` Hermitian real, complex, or
912 quaternion matrices is `n`::
913
914 sage: RealSymmetricEJA(4).rank()
915 4
916 sage: ComplexHermitianEJA(3).rank()
917 3
918 sage: QuaternionHermitianEJA(2).rank()
919 2
920
921 TESTS:
922
923 Ensure that every EJA that we know how to construct has a
924 positive integer rank, unless the algebra is trivial in
925 which case its rank will be zero::
926
927 sage: set_random_seed()
928 sage: J = random_eja()
929 sage: r = J.rank()
930 sage: r in ZZ
931 True
932 sage: r > 0 or (r == 0 and J.is_trivial())
933 True
934
935 Ensure that computing the rank actually works, since the ranks
936 of all simple algebras are known and will be cached by default::
937
938 sage: J = HadamardEJA(4)
939 sage: J.rank.clear_cache()
940 sage: J.rank()
941 4
942
943 ::
944
945 sage: J = JordanSpinEJA(4)
946 sage: J.rank.clear_cache()
947 sage: J.rank()
948 2
949
950 ::
951
952 sage: J = RealSymmetricEJA(3)
953 sage: J.rank.clear_cache()
954 sage: J.rank()
955 3
956
957 ::
958
959 sage: J = ComplexHermitianEJA(2)
960 sage: J.rank.clear_cache()
961 sage: J.rank()
962 2
963
964 ::
965
966 sage: J = QuaternionHermitianEJA(2)
967 sage: J.rank.clear_cache()
968 sage: J.rank()
969 2
970 """
971 return len(self._charpoly_coefficients())
972
973
974 def vector_space(self):
975 """
976 Return the vector space that underlies this algebra.
977
978 SETUP::
979
980 sage: from mjo.eja.eja_algebra import RealSymmetricEJA
981
982 EXAMPLES::
983
984 sage: J = RealSymmetricEJA(2)
985 sage: J.vector_space()
986 Vector space of dimension 3 over...
987
988 """
989 return self.zero().to_vector().parent().ambient_vector_space()
990
991
992 Element = FiniteDimensionalEuclideanJordanAlgebraElement
993
994
995 class HadamardEJA(FiniteDimensionalEuclideanJordanAlgebra):
996 """
997 Return the Euclidean Jordan Algebra corresponding to the set
998 `R^n` under the Hadamard product.
999
1000 Note: this is nothing more than the Cartesian product of ``n``
1001 copies of the spin algebra. Once Cartesian product algebras
1002 are implemented, this can go.
1003
1004 SETUP::
1005
1006 sage: from mjo.eja.eja_algebra import HadamardEJA
1007
1008 EXAMPLES:
1009
1010 This multiplication table can be verified by hand::
1011
1012 sage: J = HadamardEJA(3)
1013 sage: e0,e1,e2 = J.gens()
1014 sage: e0*e0
1015 e0
1016 sage: e0*e1
1017 0
1018 sage: e0*e2
1019 0
1020 sage: e1*e1
1021 e1
1022 sage: e1*e2
1023 0
1024 sage: e2*e2
1025 e2
1026
1027 TESTS:
1028
1029 We can change the generator prefix::
1030
1031 sage: HadamardEJA(3, prefix='r').gens()
1032 (r0, r1, r2)
1033
1034 """
1035 def __init__(self, n, field=AA, **kwargs):
1036 V = VectorSpace(field, n)
1037 mult_table = [ [ V.gen(i)*(i == j) for j in range(n) ]
1038 for i in range(n) ]
1039
1040 fdeja = super(HadamardEJA, self)
1041 fdeja.__init__(field, mult_table, **kwargs)
1042 self.rank.set_cache(n)
1043
1044 def inner_product(self, x, y):
1045 """
1046 Faster to reimplement than to use natural representations.
1047
1048 SETUP::
1049
1050 sage: from mjo.eja.eja_algebra import HadamardEJA
1051
1052 TESTS:
1053
1054 Ensure that this is the usual inner product for the algebras
1055 over `R^n`::
1056
1057 sage: set_random_seed()
1058 sage: J = HadamardEJA.random_instance()
1059 sage: x,y = J.random_elements(2)
1060 sage: X = x.natural_representation()
1061 sage: Y = y.natural_representation()
1062 sage: x.inner_product(y) == J.natural_inner_product(X,Y)
1063 True
1064
1065 """
1066 return x.to_vector().inner_product(y.to_vector())
1067
1068
1069 def random_eja(field=AA):
1070 """
1071 Return a "random" finite-dimensional Euclidean Jordan Algebra.
1072
1073 SETUP::
1074
1075 sage: from mjo.eja.eja_algebra import random_eja
1076
1077 TESTS::
1078
1079 sage: random_eja()
1080 Euclidean Jordan algebra of dimension...
1081
1082 """
1083 classname = choice([TrivialEJA,
1084 HadamardEJA,
1085 JordanSpinEJA,
1086 RealSymmetricEJA,
1087 ComplexHermitianEJA,
1088 QuaternionHermitianEJA])
1089 return classname.random_instance(field=field)
1090
1091
1092
1093
1094 class MatrixEuclideanJordanAlgebra(FiniteDimensionalEuclideanJordanAlgebra):
1095 @staticmethod
1096 def _max_test_case_size():
1097 # Play it safe, since this will be squared and the underlying
1098 # field can have dimension 4 (quaternions) too.
1099 return 2
1100
1101 def __init__(self, field, basis, normalize_basis=True, **kwargs):
1102 """
1103 Compared to the superclass constructor, we take a basis instead of
1104 a multiplication table because the latter can be computed in terms
1105 of the former when the product is known (like it is here).
1106 """
1107 # Used in this class's fast _charpoly_coefficients() override.
1108 self._basis_normalizers = None
1109
1110 # We're going to loop through this a few times, so now's a good
1111 # time to ensure that it isn't a generator expression.
1112 basis = tuple(basis)
1113
1114 if len(basis) > 1 and normalize_basis:
1115 # We'll need sqrt(2) to normalize the basis, and this
1116 # winds up in the multiplication table, so the whole
1117 # algebra needs to be over the field extension.
1118 R = PolynomialRing(field, 'z')
1119 z = R.gen()
1120 p = z**2 - 2
1121 if p.is_irreducible():
1122 field = field.extension(p, 'sqrt2', embedding=RLF(2).sqrt())
1123 basis = tuple( s.change_ring(field) for s in basis )
1124 self._basis_normalizers = tuple(
1125 ~(self.natural_inner_product(s,s).sqrt()) for s in basis )
1126 basis = tuple(s*c for (s,c) in zip(basis,self._basis_normalizers))
1127
1128 Qs = self.multiplication_table_from_matrix_basis(basis)
1129
1130 fdeja = super(MatrixEuclideanJordanAlgebra, self)
1131 fdeja.__init__(field, Qs, natural_basis=basis, **kwargs)
1132 return
1133
1134
1135 @cached_method
1136 def _charpoly_coefficients(self):
1137 r"""
1138 Override the parent method with something that tries to compute
1139 over a faster (non-extension) field.
1140 """
1141 if self._basis_normalizers is None:
1142 # We didn't normalize, so assume that the basis we started
1143 # with had entries in a nice field.
1144 return super(MatrixEuclideanJordanAlgebra, self)._charpoly_coefficients()
1145 else:
1146 basis = ( (b/n) for (b,n) in zip(self.natural_basis(),
1147 self._basis_normalizers) )
1148
1149 # Do this over the rationals and convert back at the end.
1150 # Only works because we know the entries of the basis are
1151 # integers.
1152 J = MatrixEuclideanJordanAlgebra(QQ,
1153 basis,
1154 normalize_basis=False)
1155 a = J._charpoly_coefficients()
1156
1157 # Unfortunately, changing the basis does change the
1158 # coefficients of the characteristic polynomial, but since
1159 # these are really the coefficients of the "characteristic
1160 # polynomial of" function, everything is still nice and
1161 # unevaluated. It's therefore "obvious" how scaling the
1162 # basis affects the coordinate variables X1, X2, et
1163 # cetera. Scaling the first basis vector up by "n" adds a
1164 # factor of 1/n into every "X1" term, for example. So here
1165 # we simply undo the basis_normalizer scaling that we
1166 # performed earlier.
1167 #
1168 # The a[0] access here is safe because trivial algebras
1169 # won't have any basis normalizers and therefore won't
1170 # make it to this "else" branch.
1171 XS = a[0].parent().gens()
1172 subs_dict = { XS[i]: self._basis_normalizers[i]*XS[i]
1173 for i in range(len(XS)) }
1174 return tuple( a_i.subs(subs_dict) for a_i in a )
1175
1176
1177 @staticmethod
1178 def multiplication_table_from_matrix_basis(basis):
1179 """
1180 At least three of the five simple Euclidean Jordan algebras have the
1181 symmetric multiplication (A,B) |-> (AB + BA)/2, where the
1182 multiplication on the right is matrix multiplication. Given a basis
1183 for the underlying matrix space, this function returns a
1184 multiplication table (obtained by looping through the basis
1185 elements) for an algebra of those matrices.
1186 """
1187 # In S^2, for example, we nominally have four coordinates even
1188 # though the space is of dimension three only. The vector space V
1189 # is supposed to hold the entire long vector, and the subspace W
1190 # of V will be spanned by the vectors that arise from symmetric
1191 # matrices. Thus for S^2, dim(V) == 4 and dim(W) == 3.
1192 if len(basis) == 0:
1193 return []
1194
1195 field = basis[0].base_ring()
1196 dimension = basis[0].nrows()
1197
1198 V = VectorSpace(field, dimension**2)
1199 W = V.span_of_basis( _mat2vec(s) for s in basis )
1200 n = len(basis)
1201 mult_table = [[W.zero() for j in range(n)] for i in range(n)]
1202 for i in range(n):
1203 for j in range(n):
1204 mat_entry = (basis[i]*basis[j] + basis[j]*basis[i])/2
1205 mult_table[i][j] = W.coordinate_vector(_mat2vec(mat_entry))
1206
1207 return mult_table
1208
1209
1210 @staticmethod
1211 def real_embed(M):
1212 """
1213 Embed the matrix ``M`` into a space of real matrices.
1214
1215 The matrix ``M`` can have entries in any field at the moment:
1216 the real numbers, complex numbers, or quaternions. And although
1217 they are not a field, we can probably support octonions at some
1218 point, too. This function returns a real matrix that "acts like"
1219 the original with respect to matrix multiplication; i.e.
1220
1221 real_embed(M*N) = real_embed(M)*real_embed(N)
1222
1223 """
1224 raise NotImplementedError
1225
1226
1227 @staticmethod
1228 def real_unembed(M):
1229 """
1230 The inverse of :meth:`real_embed`.
1231 """
1232 raise NotImplementedError
1233
1234
1235 @classmethod
1236 def natural_inner_product(cls,X,Y):
1237 Xu = cls.real_unembed(X)
1238 Yu = cls.real_unembed(Y)
1239 tr = (Xu*Yu).trace()
1240
1241 if tr in RLF:
1242 # It's real already.
1243 return tr
1244
1245 # Otherwise, try the thing that works for complex numbers; and
1246 # if that doesn't work, the thing that works for quaternions.
1247 try:
1248 return tr.vector()[0] # real part, imag part is index 1
1249 except AttributeError:
1250 # A quaternions doesn't have a vector() method, but does
1251 # have coefficient_tuple() method that returns the
1252 # coefficients of 1, i, j, and k -- in that order.
1253 return tr.coefficient_tuple()[0]
1254
1255
1256 class RealMatrixEuclideanJordanAlgebra(MatrixEuclideanJordanAlgebra):
1257 @staticmethod
1258 def real_embed(M):
1259 """
1260 The identity function, for embedding real matrices into real
1261 matrices.
1262 """
1263 return M
1264
1265 @staticmethod
1266 def real_unembed(M):
1267 """
1268 The identity function, for unembedding real matrices from real
1269 matrices.
1270 """
1271 return M
1272
1273
1274 class RealSymmetricEJA(RealMatrixEuclideanJordanAlgebra):
1275 """
1276 The rank-n simple EJA consisting of real symmetric n-by-n
1277 matrices, the usual symmetric Jordan product, and the trace inner
1278 product. It has dimension `(n^2 + n)/2` over the reals.
1279
1280 SETUP::
1281
1282 sage: from mjo.eja.eja_algebra import RealSymmetricEJA
1283
1284 EXAMPLES::
1285
1286 sage: J = RealSymmetricEJA(2)
1287 sage: e0, e1, e2 = J.gens()
1288 sage: e0*e0
1289 e0
1290 sage: e1*e1
1291 1/2*e0 + 1/2*e2
1292 sage: e2*e2
1293 e2
1294
1295 In theory, our "field" can be any subfield of the reals::
1296
1297 sage: RealSymmetricEJA(2, RDF)
1298 Euclidean Jordan algebra of dimension 3 over Real Double Field
1299 sage: RealSymmetricEJA(2, RR)
1300 Euclidean Jordan algebra of dimension 3 over Real Field with
1301 53 bits of precision
1302
1303 TESTS:
1304
1305 The dimension of this algebra is `(n^2 + n) / 2`::
1306
1307 sage: set_random_seed()
1308 sage: n_max = RealSymmetricEJA._max_test_case_size()
1309 sage: n = ZZ.random_element(1, n_max)
1310 sage: J = RealSymmetricEJA(n)
1311 sage: J.dimension() == (n^2 + n)/2
1312 True
1313
1314 The Jordan multiplication is what we think it is::
1315
1316 sage: set_random_seed()
1317 sage: J = RealSymmetricEJA.random_instance()
1318 sage: x,y = J.random_elements(2)
1319 sage: actual = (x*y).natural_representation()
1320 sage: X = x.natural_representation()
1321 sage: Y = y.natural_representation()
1322 sage: expected = (X*Y + Y*X)/2
1323 sage: actual == expected
1324 True
1325 sage: J(expected) == x*y
1326 True
1327
1328 We can change the generator prefix::
1329
1330 sage: RealSymmetricEJA(3, prefix='q').gens()
1331 (q0, q1, q2, q3, q4, q5)
1332
1333 Our natural basis is normalized with respect to the natural inner
1334 product unless we specify otherwise::
1335
1336 sage: set_random_seed()
1337 sage: J = RealSymmetricEJA.random_instance()
1338 sage: all( b.norm() == 1 for b in J.gens() )
1339 True
1340
1341 Since our natural basis is normalized with respect to the natural
1342 inner product, and since we know that this algebra is an EJA, any
1343 left-multiplication operator's matrix will be symmetric because
1344 natural->EJA basis representation is an isometry and within the EJA
1345 the operator is self-adjoint by the Jordan axiom::
1346
1347 sage: set_random_seed()
1348 sage: x = RealSymmetricEJA.random_instance().random_element()
1349 sage: x.operator().matrix().is_symmetric()
1350 True
1351
1352 We can construct the (trivial) algebra of rank zero::
1353
1354 sage: RealSymmetricEJA(0)
1355 Euclidean Jordan algebra of dimension 0 over Algebraic Real Field
1356
1357 """
1358 @classmethod
1359 def _denormalized_basis(cls, n, field):
1360 """
1361 Return a basis for the space of real symmetric n-by-n matrices.
1362
1363 SETUP::
1364
1365 sage: from mjo.eja.eja_algebra import RealSymmetricEJA
1366
1367 TESTS::
1368
1369 sage: set_random_seed()
1370 sage: n = ZZ.random_element(1,5)
1371 sage: B = RealSymmetricEJA._denormalized_basis(n,QQ)
1372 sage: all( M.is_symmetric() for M in B)
1373 True
1374
1375 """
1376 # The basis of symmetric matrices, as matrices, in their R^(n-by-n)
1377 # coordinates.
1378 S = []
1379 for i in range(n):
1380 for j in range(i+1):
1381 Eij = matrix(field, n, lambda k,l: k==i and l==j)
1382 if i == j:
1383 Sij = Eij
1384 else:
1385 Sij = Eij + Eij.transpose()
1386 S.append(Sij)
1387 return S
1388
1389
1390 @staticmethod
1391 def _max_test_case_size():
1392 return 4 # Dimension 10
1393
1394
1395 def __init__(self, n, field=AA, **kwargs):
1396 basis = self._denormalized_basis(n, field)
1397 super(RealSymmetricEJA, self).__init__(field, basis, **kwargs)
1398 self.rank.set_cache(n)
1399
1400
1401 class ComplexMatrixEuclideanJordanAlgebra(MatrixEuclideanJordanAlgebra):
1402 @staticmethod
1403 def real_embed(M):
1404 """
1405 Embed the n-by-n complex matrix ``M`` into the space of real
1406 matrices of size 2n-by-2n via the map the sends each entry `z = a +
1407 bi` to the block matrix ``[[a,b],[-b,a]]``.
1408
1409 SETUP::
1410
1411 sage: from mjo.eja.eja_algebra import \
1412 ....: ComplexMatrixEuclideanJordanAlgebra
1413
1414 EXAMPLES::
1415
1416 sage: F = QuadraticField(-1, 'I')
1417 sage: x1 = F(4 - 2*i)
1418 sage: x2 = F(1 + 2*i)
1419 sage: x3 = F(-i)
1420 sage: x4 = F(6)
1421 sage: M = matrix(F,2,[[x1,x2],[x3,x4]])
1422 sage: ComplexMatrixEuclideanJordanAlgebra.real_embed(M)
1423 [ 4 -2| 1 2]
1424 [ 2 4|-2 1]
1425 [-----+-----]
1426 [ 0 -1| 6 0]
1427 [ 1 0| 0 6]
1428
1429 TESTS:
1430
1431 Embedding is a homomorphism (isomorphism, in fact)::
1432
1433 sage: set_random_seed()
1434 sage: n_max = ComplexMatrixEuclideanJordanAlgebra._max_test_case_size()
1435 sage: n = ZZ.random_element(n_max)
1436 sage: F = QuadraticField(-1, 'I')
1437 sage: X = random_matrix(F, n)
1438 sage: Y = random_matrix(F, n)
1439 sage: Xe = ComplexMatrixEuclideanJordanAlgebra.real_embed(X)
1440 sage: Ye = ComplexMatrixEuclideanJordanAlgebra.real_embed(Y)
1441 sage: XYe = ComplexMatrixEuclideanJordanAlgebra.real_embed(X*Y)
1442 sage: Xe*Ye == XYe
1443 True
1444
1445 """
1446 n = M.nrows()
1447 if M.ncols() != n:
1448 raise ValueError("the matrix 'M' must be square")
1449
1450 # We don't need any adjoined elements...
1451 field = M.base_ring().base_ring()
1452
1453 blocks = []
1454 for z in M.list():
1455 a = z.list()[0] # real part, I guess
1456 b = z.list()[1] # imag part, I guess
1457 blocks.append(matrix(field, 2, [[a,b],[-b,a]]))
1458
1459 return matrix.block(field, n, blocks)
1460
1461
1462 @staticmethod
1463 def real_unembed(M):
1464 """
1465 The inverse of _embed_complex_matrix().
1466
1467 SETUP::
1468
1469 sage: from mjo.eja.eja_algebra import \
1470 ....: ComplexMatrixEuclideanJordanAlgebra
1471
1472 EXAMPLES::
1473
1474 sage: A = matrix(QQ,[ [ 1, 2, 3, 4],
1475 ....: [-2, 1, -4, 3],
1476 ....: [ 9, 10, 11, 12],
1477 ....: [-10, 9, -12, 11] ])
1478 sage: ComplexMatrixEuclideanJordanAlgebra.real_unembed(A)
1479 [ 2*I + 1 4*I + 3]
1480 [ 10*I + 9 12*I + 11]
1481
1482 TESTS:
1483
1484 Unembedding is the inverse of embedding::
1485
1486 sage: set_random_seed()
1487 sage: F = QuadraticField(-1, 'I')
1488 sage: M = random_matrix(F, 3)
1489 sage: Me = ComplexMatrixEuclideanJordanAlgebra.real_embed(M)
1490 sage: ComplexMatrixEuclideanJordanAlgebra.real_unembed(Me) == M
1491 True
1492
1493 """
1494 n = ZZ(M.nrows())
1495 if M.ncols() != n:
1496 raise ValueError("the matrix 'M' must be square")
1497 if not n.mod(2).is_zero():
1498 raise ValueError("the matrix 'M' must be a complex embedding")
1499
1500 # If "M" was normalized, its base ring might have roots
1501 # adjoined and they can stick around after unembedding.
1502 field = M.base_ring()
1503 R = PolynomialRing(field, 'z')
1504 z = R.gen()
1505 if field is AA:
1506 # Sage doesn't know how to embed AA into QQbar, i.e. how
1507 # to adjoin sqrt(-1) to AA.
1508 F = QQbar
1509 else:
1510 F = field.extension(z**2 + 1, 'I', embedding=CLF(-1).sqrt())
1511 i = F.gen()
1512
1513 # Go top-left to bottom-right (reading order), converting every
1514 # 2-by-2 block we see to a single complex element.
1515 elements = []
1516 for k in range(n/2):
1517 for j in range(n/2):
1518 submat = M[2*k:2*k+2,2*j:2*j+2]
1519 if submat[0,0] != submat[1,1]:
1520 raise ValueError('bad on-diagonal submatrix')
1521 if submat[0,1] != -submat[1,0]:
1522 raise ValueError('bad off-diagonal submatrix')
1523 z = submat[0,0] + submat[0,1]*i
1524 elements.append(z)
1525
1526 return matrix(F, n/2, elements)
1527
1528
1529 @classmethod
1530 def natural_inner_product(cls,X,Y):
1531 """
1532 Compute a natural inner product in this algebra directly from
1533 its real embedding.
1534
1535 SETUP::
1536
1537 sage: from mjo.eja.eja_algebra import ComplexHermitianEJA
1538
1539 TESTS:
1540
1541 This gives the same answer as the slow, default method implemented
1542 in :class:`MatrixEuclideanJordanAlgebra`::
1543
1544 sage: set_random_seed()
1545 sage: J = ComplexHermitianEJA.random_instance()
1546 sage: x,y = J.random_elements(2)
1547 sage: Xe = x.natural_representation()
1548 sage: Ye = y.natural_representation()
1549 sage: X = ComplexHermitianEJA.real_unembed(Xe)
1550 sage: Y = ComplexHermitianEJA.real_unembed(Ye)
1551 sage: expected = (X*Y).trace().real()
1552 sage: actual = ComplexHermitianEJA.natural_inner_product(Xe,Ye)
1553 sage: actual == expected
1554 True
1555
1556 """
1557 return RealMatrixEuclideanJordanAlgebra.natural_inner_product(X,Y)/2
1558
1559
1560 class ComplexHermitianEJA(ComplexMatrixEuclideanJordanAlgebra):
1561 """
1562 The rank-n simple EJA consisting of complex Hermitian n-by-n
1563 matrices over the real numbers, the usual symmetric Jordan product,
1564 and the real-part-of-trace inner product. It has dimension `n^2` over
1565 the reals.
1566
1567 SETUP::
1568
1569 sage: from mjo.eja.eja_algebra import ComplexHermitianEJA
1570
1571 EXAMPLES:
1572
1573 In theory, our "field" can be any subfield of the reals::
1574
1575 sage: ComplexHermitianEJA(2, RDF)
1576 Euclidean Jordan algebra of dimension 4 over Real Double Field
1577 sage: ComplexHermitianEJA(2, RR)
1578 Euclidean Jordan algebra of dimension 4 over Real Field with
1579 53 bits of precision
1580
1581 TESTS:
1582
1583 The dimension of this algebra is `n^2`::
1584
1585 sage: set_random_seed()
1586 sage: n_max = ComplexHermitianEJA._max_test_case_size()
1587 sage: n = ZZ.random_element(1, n_max)
1588 sage: J = ComplexHermitianEJA(n)
1589 sage: J.dimension() == n^2
1590 True
1591
1592 The Jordan multiplication is what we think it is::
1593
1594 sage: set_random_seed()
1595 sage: J = ComplexHermitianEJA.random_instance()
1596 sage: x,y = J.random_elements(2)
1597 sage: actual = (x*y).natural_representation()
1598 sage: X = x.natural_representation()
1599 sage: Y = y.natural_representation()
1600 sage: expected = (X*Y + Y*X)/2
1601 sage: actual == expected
1602 True
1603 sage: J(expected) == x*y
1604 True
1605
1606 We can change the generator prefix::
1607
1608 sage: ComplexHermitianEJA(2, prefix='z').gens()
1609 (z0, z1, z2, z3)
1610
1611 Our natural basis is normalized with respect to the natural inner
1612 product unless we specify otherwise::
1613
1614 sage: set_random_seed()
1615 sage: J = ComplexHermitianEJA.random_instance()
1616 sage: all( b.norm() == 1 for b in J.gens() )
1617 True
1618
1619 Since our natural basis is normalized with respect to the natural
1620 inner product, and since we know that this algebra is an EJA, any
1621 left-multiplication operator's matrix will be symmetric because
1622 natural->EJA basis representation is an isometry and within the EJA
1623 the operator is self-adjoint by the Jordan axiom::
1624
1625 sage: set_random_seed()
1626 sage: x = ComplexHermitianEJA.random_instance().random_element()
1627 sage: x.operator().matrix().is_symmetric()
1628 True
1629
1630 We can construct the (trivial) algebra of rank zero::
1631
1632 sage: ComplexHermitianEJA(0)
1633 Euclidean Jordan algebra of dimension 0 over Algebraic Real Field
1634
1635 """
1636
1637 @classmethod
1638 def _denormalized_basis(cls, n, field):
1639 """
1640 Returns a basis for the space of complex Hermitian n-by-n matrices.
1641
1642 Why do we embed these? Basically, because all of numerical linear
1643 algebra assumes that you're working with vectors consisting of `n`
1644 entries from a field and scalars from the same field. There's no way
1645 to tell SageMath that (for example) the vectors contain complex
1646 numbers, while the scalar field is real.
1647
1648 SETUP::
1649
1650 sage: from mjo.eja.eja_algebra import ComplexHermitianEJA
1651
1652 TESTS::
1653
1654 sage: set_random_seed()
1655 sage: n = ZZ.random_element(1,5)
1656 sage: field = QuadraticField(2, 'sqrt2')
1657 sage: B = ComplexHermitianEJA._denormalized_basis(n, field)
1658 sage: all( M.is_symmetric() for M in B)
1659 True
1660
1661 """
1662 R = PolynomialRing(field, 'z')
1663 z = R.gen()
1664 F = field.extension(z**2 + 1, 'I')
1665 I = F.gen()
1666
1667 # This is like the symmetric case, but we need to be careful:
1668 #
1669 # * We want conjugate-symmetry, not just symmetry.
1670 # * The diagonal will (as a result) be real.
1671 #
1672 S = []
1673 for i in range(n):
1674 for j in range(i+1):
1675 Eij = matrix(F, n, lambda k,l: k==i and l==j)
1676 if i == j:
1677 Sij = cls.real_embed(Eij)
1678 S.append(Sij)
1679 else:
1680 # The second one has a minus because it's conjugated.
1681 Sij_real = cls.real_embed(Eij + Eij.transpose())
1682 S.append(Sij_real)
1683 Sij_imag = cls.real_embed(I*Eij - I*Eij.transpose())
1684 S.append(Sij_imag)
1685
1686 # Since we embedded these, we can drop back to the "field" that we
1687 # started with instead of the complex extension "F".
1688 return ( s.change_ring(field) for s in S )
1689
1690
1691 def __init__(self, n, field=AA, **kwargs):
1692 basis = self._denormalized_basis(n,field)
1693 super(ComplexHermitianEJA,self).__init__(field, basis, **kwargs)
1694 self.rank.set_cache(n)
1695
1696
1697 class QuaternionMatrixEuclideanJordanAlgebra(MatrixEuclideanJordanAlgebra):
1698 @staticmethod
1699 def real_embed(M):
1700 """
1701 Embed the n-by-n quaternion matrix ``M`` into the space of real
1702 matrices of size 4n-by-4n by first sending each quaternion entry `z
1703 = a + bi + cj + dk` to the block-complex matrix ``[[a + bi,
1704 c+di],[-c + di, a-bi]]`, and then embedding those into a real
1705 matrix.
1706
1707 SETUP::
1708
1709 sage: from mjo.eja.eja_algebra import \
1710 ....: QuaternionMatrixEuclideanJordanAlgebra
1711
1712 EXAMPLES::
1713
1714 sage: Q = QuaternionAlgebra(QQ,-1,-1)
1715 sage: i,j,k = Q.gens()
1716 sage: x = 1 + 2*i + 3*j + 4*k
1717 sage: M = matrix(Q, 1, [[x]])
1718 sage: QuaternionMatrixEuclideanJordanAlgebra.real_embed(M)
1719 [ 1 2 3 4]
1720 [-2 1 -4 3]
1721 [-3 4 1 -2]
1722 [-4 -3 2 1]
1723
1724 Embedding is a homomorphism (isomorphism, in fact)::
1725
1726 sage: set_random_seed()
1727 sage: n_max = QuaternionMatrixEuclideanJordanAlgebra._max_test_case_size()
1728 sage: n = ZZ.random_element(n_max)
1729 sage: Q = QuaternionAlgebra(QQ,-1,-1)
1730 sage: X = random_matrix(Q, n)
1731 sage: Y = random_matrix(Q, n)
1732 sage: Xe = QuaternionMatrixEuclideanJordanAlgebra.real_embed(X)
1733 sage: Ye = QuaternionMatrixEuclideanJordanAlgebra.real_embed(Y)
1734 sage: XYe = QuaternionMatrixEuclideanJordanAlgebra.real_embed(X*Y)
1735 sage: Xe*Ye == XYe
1736 True
1737
1738 """
1739 quaternions = M.base_ring()
1740 n = M.nrows()
1741 if M.ncols() != n:
1742 raise ValueError("the matrix 'M' must be square")
1743
1744 F = QuadraticField(-1, 'I')
1745 i = F.gen()
1746
1747 blocks = []
1748 for z in M.list():
1749 t = z.coefficient_tuple()
1750 a = t[0]
1751 b = t[1]
1752 c = t[2]
1753 d = t[3]
1754 cplxM = matrix(F, 2, [[ a + b*i, c + d*i],
1755 [-c + d*i, a - b*i]])
1756 realM = ComplexMatrixEuclideanJordanAlgebra.real_embed(cplxM)
1757 blocks.append(realM)
1758
1759 # We should have real entries by now, so use the realest field
1760 # we've got for the return value.
1761 return matrix.block(quaternions.base_ring(), n, blocks)
1762
1763
1764
1765 @staticmethod
1766 def real_unembed(M):
1767 """
1768 The inverse of _embed_quaternion_matrix().
1769
1770 SETUP::
1771
1772 sage: from mjo.eja.eja_algebra import \
1773 ....: QuaternionMatrixEuclideanJordanAlgebra
1774
1775 EXAMPLES::
1776
1777 sage: M = matrix(QQ, [[ 1, 2, 3, 4],
1778 ....: [-2, 1, -4, 3],
1779 ....: [-3, 4, 1, -2],
1780 ....: [-4, -3, 2, 1]])
1781 sage: QuaternionMatrixEuclideanJordanAlgebra.real_unembed(M)
1782 [1 + 2*i + 3*j + 4*k]
1783
1784 TESTS:
1785
1786 Unembedding is the inverse of embedding::
1787
1788 sage: set_random_seed()
1789 sage: Q = QuaternionAlgebra(QQ, -1, -1)
1790 sage: M = random_matrix(Q, 3)
1791 sage: Me = QuaternionMatrixEuclideanJordanAlgebra.real_embed(M)
1792 sage: QuaternionMatrixEuclideanJordanAlgebra.real_unembed(Me) == M
1793 True
1794
1795 """
1796 n = ZZ(M.nrows())
1797 if M.ncols() != n:
1798 raise ValueError("the matrix 'M' must be square")
1799 if not n.mod(4).is_zero():
1800 raise ValueError("the matrix 'M' must be a quaternion embedding")
1801
1802 # Use the base ring of the matrix to ensure that its entries can be
1803 # multiplied by elements of the quaternion algebra.
1804 field = M.base_ring()
1805 Q = QuaternionAlgebra(field,-1,-1)
1806 i,j,k = Q.gens()
1807
1808 # Go top-left to bottom-right (reading order), converting every
1809 # 4-by-4 block we see to a 2-by-2 complex block, to a 1-by-1
1810 # quaternion block.
1811 elements = []
1812 for l in range(n/4):
1813 for m in range(n/4):
1814 submat = ComplexMatrixEuclideanJordanAlgebra.real_unembed(
1815 M[4*l:4*l+4,4*m:4*m+4] )
1816 if submat[0,0] != submat[1,1].conjugate():
1817 raise ValueError('bad on-diagonal submatrix')
1818 if submat[0,1] != -submat[1,0].conjugate():
1819 raise ValueError('bad off-diagonal submatrix')
1820 z = submat[0,0].real()
1821 z += submat[0,0].imag()*i
1822 z += submat[0,1].real()*j
1823 z += submat[0,1].imag()*k
1824 elements.append(z)
1825
1826 return matrix(Q, n/4, elements)
1827
1828
1829 @classmethod
1830 def natural_inner_product(cls,X,Y):
1831 """
1832 Compute a natural inner product in this algebra directly from
1833 its real embedding.
1834
1835 SETUP::
1836
1837 sage: from mjo.eja.eja_algebra import QuaternionHermitianEJA
1838
1839 TESTS:
1840
1841 This gives the same answer as the slow, default method implemented
1842 in :class:`MatrixEuclideanJordanAlgebra`::
1843
1844 sage: set_random_seed()
1845 sage: J = QuaternionHermitianEJA.random_instance()
1846 sage: x,y = J.random_elements(2)
1847 sage: Xe = x.natural_representation()
1848 sage: Ye = y.natural_representation()
1849 sage: X = QuaternionHermitianEJA.real_unembed(Xe)
1850 sage: Y = QuaternionHermitianEJA.real_unembed(Ye)
1851 sage: expected = (X*Y).trace().coefficient_tuple()[0]
1852 sage: actual = QuaternionHermitianEJA.natural_inner_product(Xe,Ye)
1853 sage: actual == expected
1854 True
1855
1856 """
1857 return RealMatrixEuclideanJordanAlgebra.natural_inner_product(X,Y)/4
1858
1859
1860 class QuaternionHermitianEJA(QuaternionMatrixEuclideanJordanAlgebra):
1861 """
1862 The rank-n simple EJA consisting of self-adjoint n-by-n quaternion
1863 matrices, the usual symmetric Jordan product, and the
1864 real-part-of-trace inner product. It has dimension `2n^2 - n` over
1865 the reals.
1866
1867 SETUP::
1868
1869 sage: from mjo.eja.eja_algebra import QuaternionHermitianEJA
1870
1871 EXAMPLES:
1872
1873 In theory, our "field" can be any subfield of the reals::
1874
1875 sage: QuaternionHermitianEJA(2, RDF)
1876 Euclidean Jordan algebra of dimension 6 over Real Double Field
1877 sage: QuaternionHermitianEJA(2, RR)
1878 Euclidean Jordan algebra of dimension 6 over Real Field with
1879 53 bits of precision
1880
1881 TESTS:
1882
1883 The dimension of this algebra is `2*n^2 - n`::
1884
1885 sage: set_random_seed()
1886 sage: n_max = QuaternionHermitianEJA._max_test_case_size()
1887 sage: n = ZZ.random_element(1, n_max)
1888 sage: J = QuaternionHermitianEJA(n)
1889 sage: J.dimension() == 2*(n^2) - n
1890 True
1891
1892 The Jordan multiplication is what we think it is::
1893
1894 sage: set_random_seed()
1895 sage: J = QuaternionHermitianEJA.random_instance()
1896 sage: x,y = J.random_elements(2)
1897 sage: actual = (x*y).natural_representation()
1898 sage: X = x.natural_representation()
1899 sage: Y = y.natural_representation()
1900 sage: expected = (X*Y + Y*X)/2
1901 sage: actual == expected
1902 True
1903 sage: J(expected) == x*y
1904 True
1905
1906 We can change the generator prefix::
1907
1908 sage: QuaternionHermitianEJA(2, prefix='a').gens()
1909 (a0, a1, a2, a3, a4, a5)
1910
1911 Our natural basis is normalized with respect to the natural inner
1912 product unless we specify otherwise::
1913
1914 sage: set_random_seed()
1915 sage: J = QuaternionHermitianEJA.random_instance()
1916 sage: all( b.norm() == 1 for b in J.gens() )
1917 True
1918
1919 Since our natural basis is normalized with respect to the natural
1920 inner product, and since we know that this algebra is an EJA, any
1921 left-multiplication operator's matrix will be symmetric because
1922 natural->EJA basis representation is an isometry and within the EJA
1923 the operator is self-adjoint by the Jordan axiom::
1924
1925 sage: set_random_seed()
1926 sage: x = QuaternionHermitianEJA.random_instance().random_element()
1927 sage: x.operator().matrix().is_symmetric()
1928 True
1929
1930 We can construct the (trivial) algebra of rank zero::
1931
1932 sage: QuaternionHermitianEJA(0)
1933 Euclidean Jordan algebra of dimension 0 over Algebraic Real Field
1934
1935 """
1936 @classmethod
1937 def _denormalized_basis(cls, n, field):
1938 """
1939 Returns a basis for the space of quaternion Hermitian n-by-n matrices.
1940
1941 Why do we embed these? Basically, because all of numerical
1942 linear algebra assumes that you're working with vectors consisting
1943 of `n` entries from a field and scalars from the same field. There's
1944 no way to tell SageMath that (for example) the vectors contain
1945 complex numbers, while the scalar field is real.
1946
1947 SETUP::
1948
1949 sage: from mjo.eja.eja_algebra import QuaternionHermitianEJA
1950
1951 TESTS::
1952
1953 sage: set_random_seed()
1954 sage: n = ZZ.random_element(1,5)
1955 sage: B = QuaternionHermitianEJA._denormalized_basis(n,QQ)
1956 sage: all( M.is_symmetric() for M in B )
1957 True
1958
1959 """
1960 Q = QuaternionAlgebra(QQ,-1,-1)
1961 I,J,K = Q.gens()
1962
1963 # This is like the symmetric case, but we need to be careful:
1964 #
1965 # * We want conjugate-symmetry, not just symmetry.
1966 # * The diagonal will (as a result) be real.
1967 #
1968 S = []
1969 for i in range(n):
1970 for j in range(i+1):
1971 Eij = matrix(Q, n, lambda k,l: k==i and l==j)
1972 if i == j:
1973 Sij = cls.real_embed(Eij)
1974 S.append(Sij)
1975 else:
1976 # The second, third, and fourth ones have a minus
1977 # because they're conjugated.
1978 Sij_real = cls.real_embed(Eij + Eij.transpose())
1979 S.append(Sij_real)
1980 Sij_I = cls.real_embed(I*Eij - I*Eij.transpose())
1981 S.append(Sij_I)
1982 Sij_J = cls.real_embed(J*Eij - J*Eij.transpose())
1983 S.append(Sij_J)
1984 Sij_K = cls.real_embed(K*Eij - K*Eij.transpose())
1985 S.append(Sij_K)
1986
1987 # Since we embedded these, we can drop back to the "field" that we
1988 # started with instead of the quaternion algebra "Q".
1989 return ( s.change_ring(field) for s in S )
1990
1991
1992 def __init__(self, n, field=AA, **kwargs):
1993 basis = self._denormalized_basis(n,field)
1994 super(QuaternionHermitianEJA,self).__init__(field, basis, **kwargs)
1995 self.rank.set_cache(n)
1996
1997
1998 class BilinearFormEJA(FiniteDimensionalEuclideanJordanAlgebra):
1999 r"""
2000 The rank-2 simple EJA consisting of real vectors ``x=(x0, x_bar)``
2001 with the half-trace inner product and jordan product ``x*y =
2002 (x0*y0 + <B*x_bar,y_bar>, x0*y_bar + y0*x_bar)`` where ``B`` is a
2003 symmetric positive-definite "bilinear form" matrix. It has
2004 dimension `n` over the reals, and reduces to the ``JordanSpinEJA``
2005 when ``B`` is the identity matrix of order ``n-1``.
2006
2007 SETUP::
2008
2009 sage: from mjo.eja.eja_algebra import (BilinearFormEJA,
2010 ....: JordanSpinEJA)
2011
2012 EXAMPLES:
2013
2014 When no bilinear form is specified, the identity matrix is used,
2015 and the resulting algebra is the Jordan spin algebra::
2016
2017 sage: J0 = BilinearFormEJA(3)
2018 sage: J1 = JordanSpinEJA(3)
2019 sage: J0.multiplication_table() == J0.multiplication_table()
2020 True
2021
2022 TESTS:
2023
2024 We can create a zero-dimensional algebra::
2025
2026 sage: J = BilinearFormEJA(0)
2027 sage: J.basis()
2028 Finite family {}
2029
2030 We can check the multiplication condition given in the Jordan, von
2031 Neumann, and Wigner paper (and also discussed on my "On the
2032 symmetry..." paper). Note that this relies heavily on the standard
2033 choice of basis, as does anything utilizing the bilinear form matrix::
2034
2035 sage: set_random_seed()
2036 sage: n = ZZ.random_element(5)
2037 sage: M = matrix.random(QQ, max(0,n-1), algorithm='unimodular')
2038 sage: B = M.transpose()*M
2039 sage: J = BilinearFormEJA(n, B=B)
2040 sage: eis = VectorSpace(M.base_ring(), M.ncols()).basis()
2041 sage: V = J.vector_space()
2042 sage: sis = [ J.from_vector(V([0] + (M.inverse()*ei).list()))
2043 ....: for ei in eis ]
2044 sage: actual = [ sis[i]*sis[j]
2045 ....: for i in range(n-1)
2046 ....: for j in range(n-1) ]
2047 sage: expected = [ J.one() if i == j else J.zero()
2048 ....: for i in range(n-1)
2049 ....: for j in range(n-1) ]
2050 sage: actual == expected
2051 True
2052 """
2053 def __init__(self, n, field=AA, B=None, **kwargs):
2054 if B is None:
2055 self._B = matrix.identity(field, max(0,n-1))
2056 else:
2057 self._B = B
2058
2059 V = VectorSpace(field, n)
2060 mult_table = [[V.zero() for j in range(n)] for i in range(n)]
2061 for i in range(n):
2062 for j in range(n):
2063 x = V.gen(i)
2064 y = V.gen(j)
2065 x0 = x[0]
2066 xbar = x[1:]
2067 y0 = y[0]
2068 ybar = y[1:]
2069 z0 = x0*y0 + (self._B*xbar).inner_product(ybar)
2070 zbar = y0*xbar + x0*ybar
2071 z = V([z0] + zbar.list())
2072 mult_table[i][j] = z
2073
2074 # The rank of this algebra is two, unless we're in a
2075 # one-dimensional ambient space (because the rank is bounded
2076 # by the ambient dimension).
2077 fdeja = super(BilinearFormEJA, self)
2078 fdeja.__init__(field, mult_table, **kwargs)
2079 self.rank.set_cache(min(n,2))
2080
2081 def inner_product(self, x, y):
2082 r"""
2083 Half of the trace inner product.
2084
2085 This is defined so that the special case of the Jordan spin
2086 algebra gets the usual inner product.
2087
2088 SETUP::
2089
2090 sage: from mjo.eja.eja_algebra import BilinearFormEJA
2091
2092 TESTS:
2093
2094 Ensure that this is one-half of the trace inner-product when
2095 the algebra isn't just the reals (when ``n`` isn't one). This
2096 is in Faraut and Koranyi, and also my "On the symmetry..."
2097 paper::
2098
2099 sage: set_random_seed()
2100 sage: n = ZZ.random_element(2,5)
2101 sage: M = matrix.random(QQ, max(0,n-1), algorithm='unimodular')
2102 sage: B = M.transpose()*M
2103 sage: J = BilinearFormEJA(n, B=B)
2104 sage: x = J.random_element()
2105 sage: y = J.random_element()
2106 sage: x.inner_product(y) == (x*y).trace()/2
2107 True
2108
2109 """
2110 xvec = x.to_vector()
2111 xbar = xvec[1:]
2112 yvec = y.to_vector()
2113 ybar = yvec[1:]
2114 return x[0]*y[0] + (self._B*xbar).inner_product(ybar)
2115
2116
2117 class JordanSpinEJA(BilinearFormEJA):
2118 """
2119 The rank-2 simple EJA consisting of real vectors ``x=(x0, x_bar)``
2120 with the usual inner product and jordan product ``x*y =
2121 (<x,y>, x0*y_bar + y0*x_bar)``. It has dimension `n` over
2122 the reals.
2123
2124 SETUP::
2125
2126 sage: from mjo.eja.eja_algebra import JordanSpinEJA
2127
2128 EXAMPLES:
2129
2130 This multiplication table can be verified by hand::
2131
2132 sage: J = JordanSpinEJA(4)
2133 sage: e0,e1,e2,e3 = J.gens()
2134 sage: e0*e0
2135 e0
2136 sage: e0*e1
2137 e1
2138 sage: e0*e2
2139 e2
2140 sage: e0*e3
2141 e3
2142 sage: e1*e2
2143 0
2144 sage: e1*e3
2145 0
2146 sage: e2*e3
2147 0
2148
2149 We can change the generator prefix::
2150
2151 sage: JordanSpinEJA(2, prefix='B').gens()
2152 (B0, B1)
2153
2154 TESTS:
2155
2156 Ensure that we have the usual inner product on `R^n`::
2157
2158 sage: set_random_seed()
2159 sage: J = JordanSpinEJA.random_instance()
2160 sage: x,y = J.random_elements(2)
2161 sage: X = x.natural_representation()
2162 sage: Y = y.natural_representation()
2163 sage: x.inner_product(y) == J.natural_inner_product(X,Y)
2164 True
2165
2166 """
2167 def __init__(self, n, field=AA, **kwargs):
2168 # This is a special case of the BilinearFormEJA with the identity
2169 # matrix as its bilinear form.
2170 return super(JordanSpinEJA, self).__init__(n, field, **kwargs)
2171
2172
2173 class TrivialEJA(FiniteDimensionalEuclideanJordanAlgebra):
2174 """
2175 The trivial Euclidean Jordan algebra consisting of only a zero element.
2176
2177 SETUP::
2178
2179 sage: from mjo.eja.eja_algebra import TrivialEJA
2180
2181 EXAMPLES::
2182
2183 sage: J = TrivialEJA()
2184 sage: J.dimension()
2185 0
2186 sage: J.zero()
2187 0
2188 sage: J.one()
2189 0
2190 sage: 7*J.one()*12*J.one()
2191 0
2192 sage: J.one().inner_product(J.one())
2193 0
2194 sage: J.one().norm()
2195 0
2196 sage: J.one().subalgebra_generated_by()
2197 Euclidean Jordan algebra of dimension 0 over Algebraic Real Field
2198 sage: J.rank()
2199 0
2200
2201 """
2202 def __init__(self, field=AA, **kwargs):
2203 mult_table = []
2204 fdeja = super(TrivialEJA, self)
2205 # The rank is zero using my definition, namely the dimension of the
2206 # largest subalgebra generated by any element.
2207 fdeja.__init__(field, mult_table, **kwargs)
2208 self.rank.set_cache(0)
2209
2210
2211 class DirectSumEJA(FiniteDimensionalEuclideanJordanAlgebra):
2212 r"""
2213 The external (orthogonal) direct sum of two other Euclidean Jordan
2214 algebras. Essentially the Cartesian product of its two factors.
2215 Every Euclidean Jordan algebra decomposes into an orthogonal
2216 direct sum of simple Euclidean Jordan algebras, so no generality
2217 is lost by providing only this construction.
2218
2219 SETUP::
2220
2221 sage: from mjo.eja.eja_algebra import (HadamardEJA,
2222 ....: RealSymmetricEJA,
2223 ....: DirectSumEJA)
2224
2225 EXAMPLES::
2226
2227 sage: J1 = HadamardEJA(2)
2228 sage: J2 = RealSymmetricEJA(3)
2229 sage: J = DirectSumEJA(J1,J2)
2230 sage: J.dimension()
2231 8
2232 sage: J.rank()
2233 5
2234
2235 """
2236 def __init__(self, J1, J2, field=AA, **kwargs):
2237 n1 = J1.dimension()
2238 n2 = J2.dimension()
2239 n = n1+n2
2240 V = VectorSpace(field, n)
2241 mult_table = [ [ V.zero() for j in range(n) ]
2242 for i in range(n) ]
2243 for i in range(n1):
2244 for j in range(n1):
2245 p = (J1.monomial(i)*J1.monomial(j)).to_vector()
2246 mult_table[i][j] = V(p.list() + [field.zero()]*n2)
2247
2248 for i in range(n2):
2249 for j in range(n2):
2250 p = (J2.monomial(i)*J2.monomial(j)).to_vector()
2251 mult_table[n1+i][n1+j] = V([field.zero()]*n1 + p.list())
2252
2253 fdeja = super(DirectSumEJA, self)
2254 fdeja.__init__(field, mult_table, **kwargs)
2255 self.rank.set_cache(J1.rank() + J2.rank())