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