]> gitweb.michael.orlitzky.com - sage.d.git/blob - mjo/eja/euclidean_jordan_algebra.py
eja: rewrite the operator class again to eliminate VectorSpaceMorphisms.
[sage.d.git] / mjo / eja / euclidean_jordan_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 sage.categories.finite_dimensional_algebras_with_basis import FiniteDimensionalAlgebrasWithBasis
9 from sage.categories.map import Map
10 from sage.structure.element import is_Matrix
11 from sage.structure.category_object import normalize_names
12
13 from sage.algebras.finite_dimensional_algebras.finite_dimensional_algebra import FiniteDimensionalAlgebra
14 from sage.algebras.finite_dimensional_algebras.finite_dimensional_algebra_element import FiniteDimensionalAlgebraElement
15
16
17 class FiniteDimensionalEuclideanJordanAlgebraOperator(Map):
18 def __init__(self, domain_eja, codomain_eja, mat):
19 if not (
20 isinstance(domain_eja, FiniteDimensionalEuclideanJordanAlgebra) and
21 isinstance(codomain_eja, FiniteDimensionalEuclideanJordanAlgebra) ):
22 raise ValueError('(co)domains must be finite-dimensional Euclidean '
23 'Jordan algebras')
24
25 F = domain_eja.base_ring()
26 if not (F == codomain_eja.base_ring()):
27 raise ValueError("domain and codomain must have the same base ring")
28
29 # We need to supply something here to avoid getting the
30 # default Homset of the parent FiniteDimensionalAlgebra class,
31 # which messes up e.g. equality testing.
32 parent = Hom(domain_eja, codomain_eja, VectorSpaces(F))
33
34 # The Map initializer will set our parent to a homset, which
35 # is explicitly NOT what we want, because these ain't algebra
36 # homomorphisms.
37 super(FiniteDimensionalEuclideanJordanAlgebraOperator,self).__init__(parent)
38
39 # Keep a matrix around to do all of the real work. It would
40 # be nice if we could use a VectorSpaceMorphism instead, but
41 # those use row vectors that we don't want to accidentally
42 # expose to our users.
43 self._matrix = mat
44
45
46 def _call_(self, x):
47 """
48 Allow this operator to be called only on elements of an EJA.
49
50 EXAMPLES::
51
52 sage: J = JordanSpinEJA(3)
53 sage: x = J.linear_combination(zip(range(len(J.gens())), J.gens()))
54 sage: id = identity_matrix(J.base_ring(), J.dimension())
55 sage: f = FiniteDimensionalEuclideanJordanAlgebraOperator(J,J,id)
56 sage: f(x) == x
57 True
58
59 """
60 return self.codomain()(self.matrix()*x.vector())
61
62
63 def _add_(self, other):
64 """
65 Add the ``other`` EJA operator to this one.
66
67 EXAMPLES:
68
69 When we add two EJA operators, we get another one back::
70
71 sage: J = RealSymmetricEJA(2)
72 sage: id = identity_matrix(J.base_ring(), J.dimension())
73 sage: f = FiniteDimensionalEuclideanJordanAlgebraOperator(J,J,id)
74 sage: g = FiniteDimensionalEuclideanJordanAlgebraOperator(J,J,id)
75 sage: f + g
76 Linear operator between finite-dimensional Euclidean Jordan
77 algebras represented by the matrix:
78 [2 0 0]
79 [0 2 0]
80 [0 0 2]
81 Domain: Euclidean Jordan algebra of degree 3 over Rational Field
82 Codomain: Euclidean Jordan algebra of degree 3 over Rational Field
83
84 If you try to add two identical vector space operators but on
85 different EJAs, that should blow up::
86
87 sage: J1 = RealSymmetricEJA(2)
88 sage: J2 = JordanSpinEJA(3)
89 sage: id = identity_matrix(QQ, 3)
90 sage: f = FiniteDimensionalEuclideanJordanAlgebraOperator(J1,J1,id)
91 sage: g = FiniteDimensionalEuclideanJordanAlgebraOperator(J2,J2,id)
92 sage: f + g
93 Traceback (most recent call last):
94 ...
95 TypeError: unsupported operand parent(s) for +: ...
96
97 """
98 return FiniteDimensionalEuclideanJordanAlgebraOperator(
99 self.domain(),
100 self.codomain(),
101 self.matrix() + other.matrix())
102
103
104 def _composition_(self, other, homset):
105 """
106 Compose two EJA operators to get another one (and NOT a formal
107 composite object) back.
108
109 EXAMPLES::
110
111 sage: J1 = JordanSpinEJA(3)
112 sage: J2 = RealCartesianProductEJA(2)
113 sage: J3 = RealSymmetricEJA(1)
114 sage: mat1 = matrix(QQ, [[1,2,3],
115 ....: [4,5,6]])
116 sage: mat2 = matrix(QQ, [[7,8]])
117 sage: g = FiniteDimensionalEuclideanJordanAlgebraOperator(J1,
118 ....: J2,
119 ....: mat1)
120 sage: f = FiniteDimensionalEuclideanJordanAlgebraOperator(J2,
121 ....: J3,
122 ....: mat2)
123 sage: f*g
124 Linear operator between finite-dimensional Euclidean Jordan
125 algebras represented by the matrix:
126 [39 54 69]
127 Domain: Euclidean Jordan algebra of degree 3 over Rational Field
128 Codomain: Euclidean Jordan algebra of degree 1 over Rational Field
129
130 """
131 return FiniteDimensionalEuclideanJordanAlgebraOperator(
132 other.domain(),
133 self.codomain(),
134 self.matrix()*other.matrix())
135
136
137 def __eq__(self, other):
138 if self.domain() != other.domain():
139 return False
140 if self.codomain() != other.codomain():
141 return False
142 if self.matrix() != other.matrix():
143 return False
144 return True
145
146 def __invert__(self):
147 """
148 Invert this EJA operator.
149
150 EXAMPLES::
151
152 sage: J = RealSymmetricEJA(2)
153 sage: id = identity_matrix(J.base_ring(), J.dimension())
154 sage: f = FiniteDimensionalEuclideanJordanAlgebraOperator(J,J,id)
155 sage: ~f
156 Linear operator between finite-dimensional Euclidean Jordan
157 algebras represented by the matrix:
158 [1 0 0]
159 [0 1 0]
160 [0 0 1]
161 Domain: Euclidean Jordan algebra of degree 3 over Rational Field
162 Codomain: Euclidean Jordan algebra of degree 3 over Rational Field
163
164 """
165 return FiniteDimensionalEuclideanJordanAlgebraOperator(
166 self.codomain(),
167 self.domain(),
168 ~self.matrix())
169
170
171 def __mul__(self, other):
172 """
173 Compose two EJA operators, or scale myself by an element of the
174 ambient vector space.
175
176 We need to override the real ``__mul__`` function to prevent the
177 coercion framework from throwing an error when it fails to convert
178 a base ring element into a morphism.
179
180 EXAMPLES:
181
182 We can scale an operator on a rational algebra by a rational number::
183
184 sage: J = RealSymmetricEJA(2)
185 sage: e0,e1,e2 = J.gens()
186 sage: x = 2*e0 + 4*e1 + 16*e2
187 sage: x.operator()
188 Linear operator between finite-dimensional Euclidean Jordan algebras
189 represented by the matrix:
190 [ 2 4 0]
191 [ 2 9 2]
192 [ 0 4 16]
193 Domain: Euclidean Jordan algebra of degree 3 over Rational Field
194 Codomain: Euclidean Jordan algebra of degree 3 over Rational Field
195 sage: x.operator()*(1/2)
196 Linear operator between finite-dimensional Euclidean Jordan algebras
197 represented by the matrix:
198 [ 1 2 0]
199 [ 1 9/2 1]
200 [ 0 2 8]
201 Domain: Euclidean Jordan algebra of degree 3 over Rational Field
202 Codomain: Euclidean Jordan algebra of degree 3 over Rational Field
203
204 """
205 if other in self.codomain().base_ring():
206 return FiniteDimensionalEuclideanJordanAlgebraOperator(
207 self.domain(),
208 self.codomain(),
209 self.matrix()*other)
210
211 # This should eventually delegate to _composition_ after performing
212 # some sanity checks for us.
213 mor = super(FiniteDimensionalEuclideanJordanAlgebraOperator,self)
214 return mor.__mul__(other)
215
216
217 def _neg_(self):
218 """
219 Negate this EJA operator.
220
221 EXAMPLES::
222
223 sage: J = RealSymmetricEJA(2)
224 sage: id = identity_matrix(J.base_ring(), J.dimension())
225 sage: f = FiniteDimensionalEuclideanJordanAlgebraOperator(J,J,id)
226 sage: -f
227 Linear operator between finite-dimensional Euclidean Jordan
228 algebras represented by the matrix:
229 [-1 0 0]
230 [ 0 -1 0]
231 [ 0 0 -1]
232 Domain: Euclidean Jordan algebra of degree 3 over Rational Field
233 Codomain: Euclidean Jordan algebra of degree 3 over Rational Field
234
235 """
236 return FiniteDimensionalEuclideanJordanAlgebraOperator(
237 self.domain(),
238 self.codomain(),
239 -self.matrix())
240
241
242 def __pow__(self, n):
243 """
244 Raise this EJA operator to the power ``n``.
245
246 TESTS:
247
248 Ensure that we get back another EJA operator that can be added,
249 subtracted, et cetera::
250
251 sage: J = RealSymmetricEJA(2)
252 sage: id = identity_matrix(J.base_ring(), J.dimension())
253 sage: f = FiniteDimensionalEuclideanJordanAlgebraOperator(J,J,id)
254 sage: f^0 + f^1 + f^2
255 Linear operator between finite-dimensional Euclidean Jordan
256 algebras represented by the matrix:
257 [3 0 0]
258 [0 3 0]
259 [0 0 3]
260 Domain: Euclidean Jordan algebra of degree 3 over Rational Field
261 Codomain: Euclidean Jordan algebra of degree 3 over Rational Field
262
263 """
264 if (n == 1):
265 return self
266 elif (n == 0):
267 # Raising a vector space morphism to the zero power gives
268 # you back a special IdentityMorphism that is useless to us.
269 rows = self.codomain().dimension()
270 cols = self.domain().dimension()
271 mat = matrix.identity(self.base_ring(), rows, cols)
272 else:
273 mat = self.matrix()**n
274
275 return FiniteDimensionalEuclideanJordanAlgebraOperator(
276 self.domain(),
277 self.codomain(),
278 mat)
279
280
281 def _repr_(self):
282 r"""
283
284 A text representation of this linear operator on a Euclidean
285 Jordan Algebra.
286
287 EXAMPLES::
288
289 sage: J = JordanSpinEJA(2)
290 sage: id = identity_matrix(J.base_ring(), J.dimension())
291 sage: FiniteDimensionalEuclideanJordanAlgebraOperator(J,J,id)
292 Linear operator between finite-dimensional Euclidean Jordan
293 algebras represented by the matrix:
294 [1 0]
295 [0 1]
296 Domain: Euclidean Jordan algebra of degree 2 over Rational Field
297 Codomain: Euclidean Jordan algebra of degree 2 over Rational Field
298
299 """
300 msg = ("Linear operator between finite-dimensional Euclidean Jordan "
301 "algebras represented by the matrix:\n",
302 "{!r}\n",
303 "Domain: {}\n",
304 "Codomain: {}")
305 return ''.join(msg).format(self.matrix(),
306 self.domain(),
307 self.codomain())
308
309
310 def _sub_(self, other):
311 """
312 Subtract ``other`` from this EJA operator.
313
314 EXAMPLES::
315
316 sage: J = RealSymmetricEJA(2)
317 sage: id = identity_matrix(J.base_ring(),J.dimension())
318 sage: f = FiniteDimensionalEuclideanJordanAlgebraOperator(J,J,id)
319 sage: f - (f*2)
320 Linear operator between finite-dimensional Euclidean Jordan
321 algebras represented by the matrix:
322 [-1 0 0]
323 [ 0 -1 0]
324 [ 0 0 -1]
325 Domain: Euclidean Jordan algebra of degree 3 over Rational Field
326 Codomain: Euclidean Jordan algebra of degree 3 over Rational Field
327
328 """
329 return (self + (-other))
330
331
332 def matrix(self):
333 """
334 Return the matrix representation of this operator with respect
335 to the default bases of its (co)domain.
336
337 EXAMPLES::
338
339 sage: J = RealSymmetricEJA(2)
340 sage: mat = matrix(J.base_ring(), J.dimension(), range(9))
341 sage: f = FiniteDimensionalEuclideanJordanAlgebraOperator(J,J,mat)
342 sage: f.matrix()
343 [0 1 2]
344 [3 4 5]
345 [6 7 8]
346
347 """
348 return self._matrix
349
350
351 class FiniteDimensionalEuclideanJordanAlgebra(FiniteDimensionalAlgebra):
352 @staticmethod
353 def __classcall_private__(cls,
354 field,
355 mult_table,
356 names='e',
357 assume_associative=False,
358 category=None,
359 rank=None,
360 natural_basis=None):
361 n = len(mult_table)
362 mult_table = [b.base_extend(field) for b in mult_table]
363 for b in mult_table:
364 b.set_immutable()
365 if not (is_Matrix(b) and b.dimensions() == (n, n)):
366 raise ValueError("input is not a multiplication table")
367 mult_table = tuple(mult_table)
368
369 cat = FiniteDimensionalAlgebrasWithBasis(field)
370 cat.or_subcategory(category)
371 if assume_associative:
372 cat = cat.Associative()
373
374 names = normalize_names(n, names)
375
376 fda = super(FiniteDimensionalEuclideanJordanAlgebra, cls)
377 return fda.__classcall__(cls,
378 field,
379 mult_table,
380 assume_associative=assume_associative,
381 names=names,
382 category=cat,
383 rank=rank,
384 natural_basis=natural_basis)
385
386
387 def __init__(self,
388 field,
389 mult_table,
390 names='e',
391 assume_associative=False,
392 category=None,
393 rank=None,
394 natural_basis=None):
395 """
396 EXAMPLES:
397
398 By definition, Jordan multiplication commutes::
399
400 sage: set_random_seed()
401 sage: J = random_eja()
402 sage: x = J.random_element()
403 sage: y = J.random_element()
404 sage: x*y == y*x
405 True
406
407 """
408 self._rank = rank
409 self._natural_basis = natural_basis
410 self._multiplication_table = mult_table
411 fda = super(FiniteDimensionalEuclideanJordanAlgebra, self)
412 fda.__init__(field,
413 mult_table,
414 names=names,
415 category=category)
416
417
418 def _repr_(self):
419 """
420 Return a string representation of ``self``.
421 """
422 fmt = "Euclidean Jordan algebra of degree {} over {}"
423 return fmt.format(self.degree(), self.base_ring())
424
425
426 def _a_regular_element(self):
427 """
428 Guess a regular element. Needed to compute the basis for our
429 characteristic polynomial coefficients.
430 """
431 gs = self.gens()
432 z = self.sum( (i+1)*gs[i] for i in range(len(gs)) )
433 if not z.is_regular():
434 raise ValueError("don't know a regular element")
435 return z
436
437
438 @cached_method
439 def _charpoly_basis_space(self):
440 """
441 Return the vector space spanned by the basis used in our
442 characteristic polynomial coefficients. This is used not only to
443 compute those coefficients, but also any time we need to
444 evaluate the coefficients (like when we compute the trace or
445 determinant).
446 """
447 z = self._a_regular_element()
448 V = self.vector_space()
449 V1 = V.span_of_basis( (z**k).vector() for k in range(self.rank()) )
450 b = (V1.basis() + V1.complement().basis())
451 return V.span_of_basis(b)
452
453
454 @cached_method
455 def _charpoly_coeff(self, i):
456 """
457 Return the coefficient polynomial "a_{i}" of this algebra's
458 general characteristic polynomial.
459
460 Having this be a separate cached method lets us compute and
461 store the trace/determinant (a_{r-1} and a_{0} respectively)
462 separate from the entire characteristic polynomial.
463 """
464 (A_of_x, x, xr, detA) = self._charpoly_matrix_system()
465 R = A_of_x.base_ring()
466 if i >= self.rank():
467 # Guaranteed by theory
468 return R.zero()
469
470 # Danger: the in-place modification is done for performance
471 # reasons (reconstructing a matrix with huge polynomial
472 # entries is slow), but I don't know how cached_method works,
473 # so it's highly possible that we're modifying some global
474 # list variable by reference, here. In other words, you
475 # probably shouldn't call this method twice on the same
476 # algebra, at the same time, in two threads
477 Ai_orig = A_of_x.column(i)
478 A_of_x.set_column(i,xr)
479 numerator = A_of_x.det()
480 A_of_x.set_column(i,Ai_orig)
481
482 # We're relying on the theory here to ensure that each a_i is
483 # indeed back in R, and the added negative signs are to make
484 # the whole charpoly expression sum to zero.
485 return R(-numerator/detA)
486
487
488 @cached_method
489 def _charpoly_matrix_system(self):
490 """
491 Compute the matrix whose entries A_ij are polynomials in
492 X1,...,XN, the vector ``x`` of variables X1,...,XN, the vector
493 corresponding to `x^r` and the determinent of the matrix A =
494 [A_ij]. In other words, all of the fixed (cachable) data needed
495 to compute the coefficients of the characteristic polynomial.
496 """
497 r = self.rank()
498 n = self.dimension()
499
500 # Construct a new algebra over a multivariate polynomial ring...
501 names = ['X' + str(i) for i in range(1,n+1)]
502 R = PolynomialRing(self.base_ring(), names)
503 J = FiniteDimensionalEuclideanJordanAlgebra(R,
504 self._multiplication_table,
505 rank=r)
506
507 idmat = identity_matrix(J.base_ring(), n)
508
509 W = self._charpoly_basis_space()
510 W = W.change_ring(R.fraction_field())
511
512 # Starting with the standard coordinates x = (X1,X2,...,Xn)
513 # and then converting the entries to W-coordinates allows us
514 # to pass in the standard coordinates to the charpoly and get
515 # back the right answer. Specifically, with x = (X1,X2,...,Xn),
516 # we have
517 #
518 # W.coordinates(x^2) eval'd at (standard z-coords)
519 # =
520 # W-coords of (z^2)
521 # =
522 # W-coords of (standard coords of x^2 eval'd at std-coords of z)
523 #
524 # We want the middle equivalent thing in our matrix, but use
525 # the first equivalent thing instead so that we can pass in
526 # standard coordinates.
527 x = J(vector(R, R.gens()))
528 l1 = [column_matrix(W.coordinates((x**k).vector())) for k in range(r)]
529 l2 = [idmat.column(k-1).column() for k in range(r+1, n+1)]
530 A_of_x = block_matrix(R, 1, n, (l1 + l2))
531 xr = W.coordinates((x**r).vector())
532 return (A_of_x, x, xr, A_of_x.det())
533
534
535 @cached_method
536 def characteristic_polynomial(self):
537 """
538
539 .. WARNING::
540
541 This implementation doesn't guarantee that the polynomial
542 denominator in the coefficients is not identically zero, so
543 theoretically it could crash. The way that this is handled
544 in e.g. Faraut and Koranyi is to use a basis that guarantees
545 the denominator is non-zero. But, doing so requires knowledge
546 of at least one regular element, and we don't even know how
547 to do that. The trade-off is that, if we use the standard basis,
548 the resulting polynomial will accept the "usual" coordinates. In
549 other words, we don't have to do a change of basis before e.g.
550 computing the trace or determinant.
551
552 EXAMPLES:
553
554 The characteristic polynomial in the spin algebra is given in
555 Alizadeh, Example 11.11::
556
557 sage: J = JordanSpinEJA(3)
558 sage: p = J.characteristic_polynomial(); p
559 X1^2 - X2^2 - X3^2 + (-2*t)*X1 + t^2
560 sage: xvec = J.one().vector()
561 sage: p(*xvec)
562 t^2 - 2*t + 1
563
564 """
565 r = self.rank()
566 n = self.dimension()
567
568 # The list of coefficient polynomials a_1, a_2, ..., a_n.
569 a = [ self._charpoly_coeff(i) for i in range(n) ]
570
571 # We go to a bit of trouble here to reorder the
572 # indeterminates, so that it's easier to evaluate the
573 # characteristic polynomial at x's coordinates and get back
574 # something in terms of t, which is what we want.
575 R = a[0].parent()
576 S = PolynomialRing(self.base_ring(),'t')
577 t = S.gen(0)
578 S = PolynomialRing(S, R.variable_names())
579 t = S(t)
580
581 # Note: all entries past the rth should be zero. The
582 # coefficient of the highest power (x^r) is 1, but it doesn't
583 # appear in the solution vector which contains coefficients
584 # for the other powers (to make them sum to x^r).
585 if (r < n):
586 a[r] = 1 # corresponds to x^r
587 else:
588 # When the rank is equal to the dimension, trying to
589 # assign a[r] goes out-of-bounds.
590 a.append(1) # corresponds to x^r
591
592 return sum( a[k]*(t**k) for k in range(len(a)) )
593
594
595 def inner_product(self, x, y):
596 """
597 The inner product associated with this Euclidean Jordan algebra.
598
599 Defaults to the trace inner product, but can be overridden by
600 subclasses if they are sure that the necessary properties are
601 satisfied.
602
603 EXAMPLES:
604
605 The inner product must satisfy its axiom for this algebra to truly
606 be a Euclidean Jordan Algebra::
607
608 sage: set_random_seed()
609 sage: J = random_eja()
610 sage: x = J.random_element()
611 sage: y = J.random_element()
612 sage: z = J.random_element()
613 sage: (x*y).inner_product(z) == y.inner_product(x*z)
614 True
615
616 """
617 if (not x in self) or (not y in self):
618 raise TypeError("arguments must live in this algebra")
619 return x.trace_inner_product(y)
620
621
622 def natural_basis(self):
623 """
624 Return a more-natural representation of this algebra's basis.
625
626 Every finite-dimensional Euclidean Jordan Algebra is a direct
627 sum of five simple algebras, four of which comprise Hermitian
628 matrices. This method returns the original "natural" basis
629 for our underlying vector space. (Typically, the natural basis
630 is used to construct the multiplication table in the first place.)
631
632 Note that this will always return a matrix. The standard basis
633 in `R^n` will be returned as `n`-by-`1` column matrices.
634
635 EXAMPLES::
636
637 sage: J = RealSymmetricEJA(2)
638 sage: J.basis()
639 Family (e0, e1, e2)
640 sage: J.natural_basis()
641 (
642 [1 0] [0 1] [0 0]
643 [0 0], [1 0], [0 1]
644 )
645
646 ::
647
648 sage: J = JordanSpinEJA(2)
649 sage: J.basis()
650 Family (e0, e1)
651 sage: J.natural_basis()
652 (
653 [1] [0]
654 [0], [1]
655 )
656
657 """
658 if self._natural_basis is None:
659 return tuple( b.vector().column() for b in self.basis() )
660 else:
661 return self._natural_basis
662
663
664 def rank(self):
665 """
666 Return the rank of this EJA.
667 """
668 if self._rank is None:
669 raise ValueError("no rank specified at genesis")
670 else:
671 return self._rank
672
673 def vector_space(self):
674 """
675 Return the vector space that underlies this algebra.
676
677 EXAMPLES::
678
679 sage: J = RealSymmetricEJA(2)
680 sage: J.vector_space()
681 Vector space of dimension 3 over Rational Field
682
683 """
684 return self.zero().vector().parent().ambient_vector_space()
685
686
687 class Element(FiniteDimensionalAlgebraElement):
688 """
689 An element of a Euclidean Jordan algebra.
690 """
691
692 def __dir__(self):
693 """
694 Oh man, I should not be doing this. This hides the "disabled"
695 methods ``left_matrix`` and ``matrix`` from introspection;
696 in particular it removes them from tab-completion.
697 """
698 return filter(lambda s: s not in ['left_matrix', 'matrix'],
699 dir(self.__class__) )
700
701
702 def __init__(self, A, elt=None):
703 """
704 EXAMPLES:
705
706 The identity in `S^n` is converted to the identity in the EJA::
707
708 sage: J = RealSymmetricEJA(3)
709 sage: I = identity_matrix(QQ,3)
710 sage: J(I) == J.one()
711 True
712
713 This skew-symmetric matrix can't be represented in the EJA::
714
715 sage: J = RealSymmetricEJA(3)
716 sage: A = matrix(QQ,3, lambda i,j: i-j)
717 sage: J(A)
718 Traceback (most recent call last):
719 ...
720 ArithmeticError: vector is not in free module
721
722 """
723 # Goal: if we're given a matrix, and if it lives in our
724 # parent algebra's "natural ambient space," convert it
725 # into an algebra element.
726 #
727 # The catch is, we make a recursive call after converting
728 # the given matrix into a vector that lives in the algebra.
729 # This we need to try the parent class initializer first,
730 # to avoid recursing forever if we're given something that
731 # already fits into the algebra, but also happens to live
732 # in the parent's "natural ambient space" (this happens with
733 # vectors in R^n).
734 try:
735 FiniteDimensionalAlgebraElement.__init__(self, A, elt)
736 except ValueError:
737 natural_basis = A.natural_basis()
738 if elt in natural_basis[0].matrix_space():
739 # Thanks for nothing! Matrix spaces aren't vector
740 # spaces in Sage, so we have to figure out its
741 # natural-basis coordinates ourselves.
742 V = VectorSpace(elt.base_ring(), elt.nrows()**2)
743 W = V.span( _mat2vec(s) for s in natural_basis )
744 coords = W.coordinates(_mat2vec(elt))
745 FiniteDimensionalAlgebraElement.__init__(self, A, coords)
746
747 def __pow__(self, n):
748 """
749 Return ``self`` raised to the power ``n``.
750
751 Jordan algebras are always power-associative; see for
752 example Faraut and Koranyi, Proposition II.1.2 (ii).
753
754 .. WARNING:
755
756 We have to override this because our superclass uses row vectors
757 instead of column vectors! We, on the other hand, assume column
758 vectors everywhere.
759
760 EXAMPLES::
761
762 sage: set_random_seed()
763 sage: x = random_eja().random_element()
764 sage: x.operator_matrix()*x.vector() == (x^2).vector()
765 True
766
767 A few examples of power-associativity::
768
769 sage: set_random_seed()
770 sage: x = random_eja().random_element()
771 sage: x*(x*x)*(x*x) == x^5
772 True
773 sage: (x*x)*(x*x*x) == x^5
774 True
775
776 We also know that powers operator-commute (Koecher, Chapter
777 III, Corollary 1)::
778
779 sage: set_random_seed()
780 sage: x = random_eja().random_element()
781 sage: m = ZZ.random_element(0,10)
782 sage: n = ZZ.random_element(0,10)
783 sage: Lxm = (x^m).operator_matrix()
784 sage: Lxn = (x^n).operator_matrix()
785 sage: Lxm*Lxn == Lxn*Lxm
786 True
787
788 """
789 A = self.parent()
790 if n == 0:
791 return A.one()
792 elif n == 1:
793 return self
794 else:
795 return A( (self.operator_matrix()**(n-1))*self.vector() )
796
797
798 def apply_univariate_polynomial(self, p):
799 """
800 Apply the univariate polynomial ``p`` to this element.
801
802 A priori, SageMath won't allow us to apply a univariate
803 polynomial to an element of an EJA, because we don't know
804 that EJAs are rings (they are usually not associative). Of
805 course, we know that EJAs are power-associative, so the
806 operation is ultimately kosher. This function sidesteps
807 the CAS to get the answer we want and expect.
808
809 EXAMPLES::
810
811 sage: R = PolynomialRing(QQ, 't')
812 sage: t = R.gen(0)
813 sage: p = t^4 - t^3 + 5*t - 2
814 sage: J = RealCartesianProductEJA(5)
815 sage: J.one().apply_univariate_polynomial(p) == 3*J.one()
816 True
817
818 TESTS:
819
820 We should always get back an element of the algebra::
821
822 sage: set_random_seed()
823 sage: p = PolynomialRing(QQ, 't').random_element()
824 sage: J = random_eja()
825 sage: x = J.random_element()
826 sage: x.apply_univariate_polynomial(p) in J
827 True
828
829 """
830 if len(p.variables()) > 1:
831 raise ValueError("not a univariate polynomial")
832 P = self.parent()
833 R = P.base_ring()
834 # Convert the coeficcients to the parent's base ring,
835 # because a priori they might live in an (unnecessarily)
836 # larger ring for which P.sum() would fail below.
837 cs = [ R(c) for c in p.coefficients(sparse=False) ]
838 return P.sum( cs[k]*(self**k) for k in range(len(cs)) )
839
840
841 def characteristic_polynomial(self):
842 """
843 Return the characteristic polynomial of this element.
844
845 EXAMPLES:
846
847 The rank of `R^3` is three, and the minimal polynomial of
848 the identity element is `(t-1)` from which it follows that
849 the characteristic polynomial should be `(t-1)^3`::
850
851 sage: J = RealCartesianProductEJA(3)
852 sage: J.one().characteristic_polynomial()
853 t^3 - 3*t^2 + 3*t - 1
854
855 Likewise, the characteristic of the zero element in the
856 rank-three algebra `R^{n}` should be `t^{3}`::
857
858 sage: J = RealCartesianProductEJA(3)
859 sage: J.zero().characteristic_polynomial()
860 t^3
861
862 The characteristic polynomial of an element should evaluate
863 to zero on that element::
864
865 sage: set_random_seed()
866 sage: x = RealCartesianProductEJA(3).random_element()
867 sage: p = x.characteristic_polynomial()
868 sage: x.apply_univariate_polynomial(p)
869 0
870
871 """
872 p = self.parent().characteristic_polynomial()
873 return p(*self.vector())
874
875
876 def inner_product(self, other):
877 """
878 Return the parent algebra's inner product of myself and ``other``.
879
880 EXAMPLES:
881
882 The inner product in the Jordan spin algebra is the usual
883 inner product on `R^n` (this example only works because the
884 basis for the Jordan algebra is the standard basis in `R^n`)::
885
886 sage: J = JordanSpinEJA(3)
887 sage: x = vector(QQ,[1,2,3])
888 sage: y = vector(QQ,[4,5,6])
889 sage: x.inner_product(y)
890 32
891 sage: J(x).inner_product(J(y))
892 32
893
894 The inner product on `S^n` is `<X,Y> = trace(X*Y)`, where
895 multiplication is the usual matrix multiplication in `S^n`,
896 so the inner product of the identity matrix with itself
897 should be the `n`::
898
899 sage: J = RealSymmetricEJA(3)
900 sage: J.one().inner_product(J.one())
901 3
902
903 Likewise, the inner product on `C^n` is `<X,Y> =
904 Re(trace(X*Y))`, where we must necessarily take the real
905 part because the product of Hermitian matrices may not be
906 Hermitian::
907
908 sage: J = ComplexHermitianEJA(3)
909 sage: J.one().inner_product(J.one())
910 3
911
912 Ditto for the quaternions::
913
914 sage: J = QuaternionHermitianEJA(3)
915 sage: J.one().inner_product(J.one())
916 3
917
918 TESTS:
919
920 Ensure that we can always compute an inner product, and that
921 it gives us back a real number::
922
923 sage: set_random_seed()
924 sage: J = random_eja()
925 sage: x = J.random_element()
926 sage: y = J.random_element()
927 sage: x.inner_product(y) in RR
928 True
929
930 """
931 P = self.parent()
932 if not other in P:
933 raise TypeError("'other' must live in the same algebra")
934
935 return P.inner_product(self, other)
936
937
938 def operator_commutes_with(self, other):
939 """
940 Return whether or not this element operator-commutes
941 with ``other``.
942
943 EXAMPLES:
944
945 The definition of a Jordan algebra says that any element
946 operator-commutes with its square::
947
948 sage: set_random_seed()
949 sage: x = random_eja().random_element()
950 sage: x.operator_commutes_with(x^2)
951 True
952
953 TESTS:
954
955 Test Lemma 1 from Chapter III of Koecher::
956
957 sage: set_random_seed()
958 sage: J = random_eja()
959 sage: u = J.random_element()
960 sage: v = J.random_element()
961 sage: lhs = u.operator_commutes_with(u*v)
962 sage: rhs = v.operator_commutes_with(u^2)
963 sage: lhs == rhs
964 True
965
966 """
967 if not other in self.parent():
968 raise TypeError("'other' must live in the same algebra")
969
970 A = self.operator_matrix()
971 B = other.operator_matrix()
972 return (A*B == B*A)
973
974
975 def det(self):
976 """
977 Return my determinant, the product of my eigenvalues.
978
979 EXAMPLES::
980
981 sage: J = JordanSpinEJA(2)
982 sage: e0,e1 = J.gens()
983 sage: x = sum( J.gens() )
984 sage: x.det()
985 0
986
987 ::
988
989 sage: J = JordanSpinEJA(3)
990 sage: e0,e1,e2 = J.gens()
991 sage: x = sum( J.gens() )
992 sage: x.det()
993 -1
994
995 TESTS:
996
997 An element is invertible if and only if its determinant is
998 non-zero::
999
1000 sage: set_random_seed()
1001 sage: x = random_eja().random_element()
1002 sage: x.is_invertible() == (x.det() != 0)
1003 True
1004
1005 """
1006 P = self.parent()
1007 r = P.rank()
1008 p = P._charpoly_coeff(0)
1009 # The _charpoly_coeff function already adds the factor of
1010 # -1 to ensure that _charpoly_coeff(0) is really what
1011 # appears in front of t^{0} in the charpoly. However,
1012 # we want (-1)^r times THAT for the determinant.
1013 return ((-1)**r)*p(*self.vector())
1014
1015
1016 def inverse(self):
1017 """
1018 Return the Jordan-multiplicative inverse of this element.
1019
1020 ALGORITHM:
1021
1022 We appeal to the quadratic representation as in Koecher's
1023 Theorem 12 in Chapter III, Section 5.
1024
1025 EXAMPLES:
1026
1027 The inverse in the spin factor algebra is given in Alizadeh's
1028 Example 11.11::
1029
1030 sage: set_random_seed()
1031 sage: n = ZZ.random_element(1,10)
1032 sage: J = JordanSpinEJA(n)
1033 sage: x = J.random_element()
1034 sage: while not x.is_invertible():
1035 ....: x = J.random_element()
1036 sage: x_vec = x.vector()
1037 sage: x0 = x_vec[0]
1038 sage: x_bar = x_vec[1:]
1039 sage: coeff = ~(x0^2 - x_bar.inner_product(x_bar))
1040 sage: inv_vec = x_vec.parent()([x0] + (-x_bar).list())
1041 sage: x_inverse = coeff*inv_vec
1042 sage: x.inverse() == J(x_inverse)
1043 True
1044
1045 TESTS:
1046
1047 The identity element is its own inverse::
1048
1049 sage: set_random_seed()
1050 sage: J = random_eja()
1051 sage: J.one().inverse() == J.one()
1052 True
1053
1054 If an element has an inverse, it acts like one::
1055
1056 sage: set_random_seed()
1057 sage: J = random_eja()
1058 sage: x = J.random_element()
1059 sage: (not x.is_invertible()) or (x.inverse()*x == J.one())
1060 True
1061
1062 The inverse of the inverse is what we started with::
1063
1064 sage: set_random_seed()
1065 sage: J = random_eja()
1066 sage: x = J.random_element()
1067 sage: (not x.is_invertible()) or (x.inverse().inverse() == x)
1068 True
1069
1070 The zero element is never invertible::
1071
1072 sage: set_random_seed()
1073 sage: J = random_eja().zero().inverse()
1074 Traceback (most recent call last):
1075 ...
1076 ValueError: element is not invertible
1077
1078 """
1079 if not self.is_invertible():
1080 raise ValueError("element is not invertible")
1081
1082 return (~self.quadratic_representation())(self)
1083
1084
1085 def is_invertible(self):
1086 """
1087 Return whether or not this element is invertible.
1088
1089 We can't use the superclass method because it relies on
1090 the algebra being associative.
1091
1092 ALGORITHM:
1093
1094 The usual way to do this is to check if the determinant is
1095 zero, but we need the characteristic polynomial for the
1096 determinant. The minimal polynomial is a lot easier to get,
1097 so we use Corollary 2 in Chapter V of Koecher to check
1098 whether or not the paren't algebra's zero element is a root
1099 of this element's minimal polynomial.
1100
1101 TESTS:
1102
1103 The identity element is always invertible::
1104
1105 sage: set_random_seed()
1106 sage: J = random_eja()
1107 sage: J.one().is_invertible()
1108 True
1109
1110 The zero element is never invertible::
1111
1112 sage: set_random_seed()
1113 sage: J = random_eja()
1114 sage: J.zero().is_invertible()
1115 False
1116
1117 """
1118 zero = self.parent().zero()
1119 p = self.minimal_polynomial()
1120 return not (p(zero) == zero)
1121
1122
1123 def is_nilpotent(self):
1124 """
1125 Return whether or not some power of this element is zero.
1126
1127 The superclass method won't work unless we're in an
1128 associative algebra, and we aren't. However, we generate
1129 an assocoative subalgebra and we're nilpotent there if and
1130 only if we're nilpotent here (probably).
1131
1132 TESTS:
1133
1134 The identity element is never nilpotent::
1135
1136 sage: set_random_seed()
1137 sage: random_eja().one().is_nilpotent()
1138 False
1139
1140 The additive identity is always nilpotent::
1141
1142 sage: set_random_seed()
1143 sage: random_eja().zero().is_nilpotent()
1144 True
1145
1146 """
1147 # The element we're going to call "is_nilpotent()" on.
1148 # Either myself, interpreted as an element of a finite-
1149 # dimensional algebra, or an element of an associative
1150 # subalgebra.
1151 elt = None
1152
1153 if self.parent().is_associative():
1154 elt = FiniteDimensionalAlgebraElement(self.parent(), self)
1155 else:
1156 V = self.span_of_powers()
1157 assoc_subalg = self.subalgebra_generated_by()
1158 # Mis-design warning: the basis used for span_of_powers()
1159 # and subalgebra_generated_by() must be the same, and in
1160 # the same order!
1161 elt = assoc_subalg(V.coordinates(self.vector()))
1162
1163 # Recursive call, but should work since elt lives in an
1164 # associative algebra.
1165 return elt.is_nilpotent()
1166
1167
1168 def is_regular(self):
1169 """
1170 Return whether or not this is a regular element.
1171
1172 EXAMPLES:
1173
1174 The identity element always has degree one, but any element
1175 linearly-independent from it is regular::
1176
1177 sage: J = JordanSpinEJA(5)
1178 sage: J.one().is_regular()
1179 False
1180 sage: e0, e1, e2, e3, e4 = J.gens() # e0 is the identity
1181 sage: for x in J.gens():
1182 ....: (J.one() + x).is_regular()
1183 False
1184 True
1185 True
1186 True
1187 True
1188
1189 """
1190 return self.degree() == self.parent().rank()
1191
1192
1193 def degree(self):
1194 """
1195 Compute the degree of this element the straightforward way
1196 according to the definition; by appending powers to a list
1197 and figuring out its dimension (that is, whether or not
1198 they're linearly dependent).
1199
1200 EXAMPLES::
1201
1202 sage: J = JordanSpinEJA(4)
1203 sage: J.one().degree()
1204 1
1205 sage: e0,e1,e2,e3 = J.gens()
1206 sage: (e0 - e1).degree()
1207 2
1208
1209 In the spin factor algebra (of rank two), all elements that
1210 aren't multiples of the identity are regular::
1211
1212 sage: set_random_seed()
1213 sage: n = ZZ.random_element(1,10)
1214 sage: J = JordanSpinEJA(n)
1215 sage: x = J.random_element()
1216 sage: x == x.coefficient(0)*J.one() or x.degree() == 2
1217 True
1218
1219 """
1220 return self.span_of_powers().dimension()
1221
1222
1223 def left_matrix(self):
1224 """
1225 Our parent class defines ``left_matrix`` and ``matrix``
1226 methods whose names are misleading. We don't want them.
1227 """
1228 raise NotImplementedError("use operator_matrix() instead")
1229
1230 matrix = left_matrix
1231
1232
1233 def minimal_polynomial(self):
1234 """
1235 Return the minimal polynomial of this element,
1236 as a function of the variable `t`.
1237
1238 ALGORITHM:
1239
1240 We restrict ourselves to the associative subalgebra
1241 generated by this element, and then return the minimal
1242 polynomial of this element's operator matrix (in that
1243 subalgebra). This works by Baes Proposition 2.3.16.
1244
1245 TESTS:
1246
1247 The minimal polynomial of the identity and zero elements are
1248 always the same::
1249
1250 sage: set_random_seed()
1251 sage: J = random_eja()
1252 sage: J.one().minimal_polynomial()
1253 t - 1
1254 sage: J.zero().minimal_polynomial()
1255 t
1256
1257 The degree of an element is (by one definition) the degree
1258 of its minimal polynomial::
1259
1260 sage: set_random_seed()
1261 sage: x = random_eja().random_element()
1262 sage: x.degree() == x.minimal_polynomial().degree()
1263 True
1264
1265 The minimal polynomial and the characteristic polynomial coincide
1266 and are known (see Alizadeh, Example 11.11) for all elements of
1267 the spin factor algebra that aren't scalar multiples of the
1268 identity::
1269
1270 sage: set_random_seed()
1271 sage: n = ZZ.random_element(2,10)
1272 sage: J = JordanSpinEJA(n)
1273 sage: y = J.random_element()
1274 sage: while y == y.coefficient(0)*J.one():
1275 ....: y = J.random_element()
1276 sage: y0 = y.vector()[0]
1277 sage: y_bar = y.vector()[1:]
1278 sage: actual = y.minimal_polynomial()
1279 sage: t = PolynomialRing(J.base_ring(),'t').gen(0)
1280 sage: expected = t^2 - 2*y0*t + (y0^2 - norm(y_bar)^2)
1281 sage: bool(actual == expected)
1282 True
1283
1284 The minimal polynomial should always kill its element::
1285
1286 sage: set_random_seed()
1287 sage: x = random_eja().random_element()
1288 sage: p = x.minimal_polynomial()
1289 sage: x.apply_univariate_polynomial(p)
1290 0
1291
1292 """
1293 V = self.span_of_powers()
1294 assoc_subalg = self.subalgebra_generated_by()
1295 # Mis-design warning: the basis used for span_of_powers()
1296 # and subalgebra_generated_by() must be the same, and in
1297 # the same order!
1298 elt = assoc_subalg(V.coordinates(self.vector()))
1299
1300 # We get back a symbolic polynomial in 'x' but want a real
1301 # polynomial in 't'.
1302 p_of_x = elt.operator_matrix().minimal_polynomial()
1303 return p_of_x.change_variable_name('t')
1304
1305
1306 def natural_representation(self):
1307 """
1308 Return a more-natural representation of this element.
1309
1310 Every finite-dimensional Euclidean Jordan Algebra is a
1311 direct sum of five simple algebras, four of which comprise
1312 Hermitian matrices. This method returns the original
1313 "natural" representation of this element as a Hermitian
1314 matrix, if it has one. If not, you get the usual representation.
1315
1316 EXAMPLES::
1317
1318 sage: J = ComplexHermitianEJA(3)
1319 sage: J.one()
1320 e0 + e5 + e8
1321 sage: J.one().natural_representation()
1322 [1 0 0 0 0 0]
1323 [0 1 0 0 0 0]
1324 [0 0 1 0 0 0]
1325 [0 0 0 1 0 0]
1326 [0 0 0 0 1 0]
1327 [0 0 0 0 0 1]
1328
1329 ::
1330
1331 sage: J = QuaternionHermitianEJA(3)
1332 sage: J.one()
1333 e0 + e9 + e14
1334 sage: J.one().natural_representation()
1335 [1 0 0 0 0 0 0 0 0 0 0 0]
1336 [0 1 0 0 0 0 0 0 0 0 0 0]
1337 [0 0 1 0 0 0 0 0 0 0 0 0]
1338 [0 0 0 1 0 0 0 0 0 0 0 0]
1339 [0 0 0 0 1 0 0 0 0 0 0 0]
1340 [0 0 0 0 0 1 0 0 0 0 0 0]
1341 [0 0 0 0 0 0 1 0 0 0 0 0]
1342 [0 0 0 0 0 0 0 1 0 0 0 0]
1343 [0 0 0 0 0 0 0 0 1 0 0 0]
1344 [0 0 0 0 0 0 0 0 0 1 0 0]
1345 [0 0 0 0 0 0 0 0 0 0 1 0]
1346 [0 0 0 0 0 0 0 0 0 0 0 1]
1347
1348 """
1349 B = self.parent().natural_basis()
1350 W = B[0].matrix_space()
1351 return W.linear_combination(zip(self.vector(), B))
1352
1353
1354 def operator(self):
1355 """
1356 Return the left-multiplication-by-this-element
1357 operator on the ambient algebra.
1358
1359 TESTS::
1360
1361 sage: set_random_seed()
1362 sage: J = random_eja()
1363 sage: x = J.random_element()
1364 sage: y = J.random_element()
1365 sage: x.operator()(y) == x*y
1366 True
1367 sage: y.operator()(x) == x*y
1368 True
1369
1370 """
1371 P = self.parent()
1372 return FiniteDimensionalEuclideanJordanAlgebraOperator(
1373 P,P,
1374 self.operator_matrix() )
1375
1376
1377
1378 def operator_matrix(self):
1379 """
1380 Return the matrix that represents left- (or right-)
1381 multiplication by this element in the parent algebra.
1382
1383 We implement this ourselves to work around the fact that
1384 our parent class represents everything with row vectors.
1385
1386 EXAMPLES:
1387
1388 Test the first polarization identity from my notes, Koecher Chapter
1389 III, or from Baes (2.3)::
1390
1391 sage: set_random_seed()
1392 sage: J = random_eja()
1393 sage: x = J.random_element()
1394 sage: y = J.random_element()
1395 sage: Lx = x.operator_matrix()
1396 sage: Ly = y.operator_matrix()
1397 sage: Lxx = (x*x).operator_matrix()
1398 sage: Lxy = (x*y).operator_matrix()
1399 sage: bool(2*Lx*Lxy + Ly*Lxx == 2*Lxy*Lx + Lxx*Ly)
1400 True
1401
1402 Test the second polarization identity from my notes or from
1403 Baes (2.4)::
1404
1405 sage: set_random_seed()
1406 sage: J = random_eja()
1407 sage: x = J.random_element()
1408 sage: y = J.random_element()
1409 sage: z = J.random_element()
1410 sage: Lx = x.operator_matrix()
1411 sage: Ly = y.operator_matrix()
1412 sage: Lz = z.operator_matrix()
1413 sage: Lzy = (z*y).operator_matrix()
1414 sage: Lxy = (x*y).operator_matrix()
1415 sage: Lxz = (x*z).operator_matrix()
1416 sage: bool(Lx*Lzy + Lz*Lxy + Ly*Lxz == Lzy*Lx + Lxy*Lz + Lxz*Ly)
1417 True
1418
1419 Test the third polarization identity from my notes or from
1420 Baes (2.5)::
1421
1422 sage: set_random_seed()
1423 sage: J = random_eja()
1424 sage: u = J.random_element()
1425 sage: y = J.random_element()
1426 sage: z = J.random_element()
1427 sage: Lu = u.operator_matrix()
1428 sage: Ly = y.operator_matrix()
1429 sage: Lz = z.operator_matrix()
1430 sage: Lzy = (z*y).operator_matrix()
1431 sage: Luy = (u*y).operator_matrix()
1432 sage: Luz = (u*z).operator_matrix()
1433 sage: Luyz = (u*(y*z)).operator_matrix()
1434 sage: lhs = Lu*Lzy + Lz*Luy + Ly*Luz
1435 sage: rhs = Luyz + Ly*Lu*Lz + Lz*Lu*Ly
1436 sage: bool(lhs == rhs)
1437 True
1438
1439 Ensure that our operator's ``matrix`` method agrees with
1440 this implementation::
1441
1442 sage: set_random_seed()
1443 sage: J = random_eja()
1444 sage: x = J.random_element()
1445 sage: x.operator().matrix() == x.operator_matrix()
1446 True
1447
1448 """
1449 fda_elt = FiniteDimensionalAlgebraElement(self.parent(), self)
1450 return fda_elt.matrix().transpose()
1451
1452
1453 def quadratic_representation(self, other=None):
1454 """
1455 Return the quadratic representation of this element.
1456
1457 EXAMPLES:
1458
1459 The explicit form in the spin factor algebra is given by
1460 Alizadeh's Example 11.12::
1461
1462 sage: set_random_seed()
1463 sage: n = ZZ.random_element(1,10)
1464 sage: J = JordanSpinEJA(n)
1465 sage: x = J.random_element()
1466 sage: x_vec = x.vector()
1467 sage: x0 = x_vec[0]
1468 sage: x_bar = x_vec[1:]
1469 sage: A = matrix(QQ, 1, [x_vec.inner_product(x_vec)])
1470 sage: B = 2*x0*x_bar.row()
1471 sage: C = 2*x0*x_bar.column()
1472 sage: D = identity_matrix(QQ, n-1)
1473 sage: D = (x0^2 - x_bar.inner_product(x_bar))*D
1474 sage: D = D + 2*x_bar.tensor_product(x_bar)
1475 sage: Q = block_matrix(2,2,[A,B,C,D])
1476 sage: Q == x.quadratic_representation().matrix()
1477 True
1478
1479 Test all of the properties from Theorem 11.2 in Alizadeh::
1480
1481 sage: set_random_seed()
1482 sage: J = random_eja()
1483 sage: x = J.random_element()
1484 sage: y = J.random_element()
1485 sage: Lx = x.operator()
1486 sage: Lxx = (x*x).operator()
1487 sage: Qx = x.quadratic_representation()
1488 sage: Qy = y.quadratic_representation()
1489 sage: Qxy = x.quadratic_representation(y)
1490 sage: Qex = J.one().quadratic_representation(x)
1491 sage: n = ZZ.random_element(10)
1492 sage: Qxn = (x^n).quadratic_representation()
1493
1494 Property 1:
1495
1496 sage: 2*Qxy == (x+y).quadratic_representation() - Qx - Qy
1497 True
1498
1499 Property 2 (multiply on the right for :trac:`28272`):
1500
1501 sage: alpha = QQ.random_element()
1502 sage: (alpha*x).quadratic_representation() == Qx*(alpha^2)
1503 True
1504
1505 Property 3:
1506
1507 sage: not x.is_invertible() or ( Qx(x.inverse()) == x )
1508 True
1509
1510 sage: not x.is_invertible() or (
1511 ....: ~Qx
1512 ....: ==
1513 ....: x.inverse().quadratic_representation() )
1514 True
1515
1516 sage: Qxy(J.one()) == x*y
1517 True
1518
1519 Property 4:
1520
1521 sage: not x.is_invertible() or (
1522 ....: x.quadratic_representation(x.inverse())*Qx
1523 ....: == Qx*x.quadratic_representation(x.inverse()) )
1524 True
1525
1526 sage: not x.is_invertible() or (
1527 ....: x.quadratic_representation(x.inverse())*Qx
1528 ....: ==
1529 ....: 2*x.operator()*Qex - Qx )
1530 True
1531
1532 sage: 2*x.operator()*Qex - Qx == Lxx
1533 True
1534
1535 Property 5:
1536
1537 sage: Qy(x).quadratic_representation() == Qy*Qx*Qy
1538 True
1539
1540 Property 6:
1541
1542 sage: Qxn == (Qx)^n
1543 True
1544
1545 Property 7:
1546
1547 sage: not x.is_invertible() or (
1548 ....: Qx*x.inverse().operator() == Lx )
1549 True
1550
1551 Property 8:
1552
1553 sage: not x.operator_commutes_with(y) or (
1554 ....: Qx(y)^n == Qxn(y^n) )
1555 True
1556
1557 """
1558 if other is None:
1559 other=self
1560 elif not other in self.parent():
1561 raise TypeError("'other' must live in the same algebra")
1562
1563 L = self.operator()
1564 M = other.operator()
1565 return ( L*M + M*L - (self*other).operator() )
1566
1567
1568 def span_of_powers(self):
1569 """
1570 Return the vector space spanned by successive powers of
1571 this element.
1572 """
1573 # The dimension of the subalgebra can't be greater than
1574 # the big algebra, so just put everything into a list
1575 # and let span() get rid of the excess.
1576 #
1577 # We do the extra ambient_vector_space() in case we're messing
1578 # with polynomials and the direct parent is a module.
1579 V = self.parent().vector_space()
1580 return V.span( (self**d).vector() for d in xrange(V.dimension()) )
1581
1582
1583 def subalgebra_generated_by(self):
1584 """
1585 Return the associative subalgebra of the parent EJA generated
1586 by this element.
1587
1588 TESTS::
1589
1590 sage: set_random_seed()
1591 sage: x = random_eja().random_element()
1592 sage: x.subalgebra_generated_by().is_associative()
1593 True
1594
1595 Squaring in the subalgebra should be the same thing as
1596 squaring in the superalgebra::
1597
1598 sage: set_random_seed()
1599 sage: x = random_eja().random_element()
1600 sage: u = x.subalgebra_generated_by().random_element()
1601 sage: u.operator_matrix()*u.vector() == (u**2).vector()
1602 True
1603
1604 """
1605 # First get the subspace spanned by the powers of myself...
1606 V = self.span_of_powers()
1607 F = self.base_ring()
1608
1609 # Now figure out the entries of the right-multiplication
1610 # matrix for the successive basis elements b0, b1,... of
1611 # that subspace.
1612 mats = []
1613 for b_right in V.basis():
1614 eja_b_right = self.parent()(b_right)
1615 b_right_rows = []
1616 # The first row of the right-multiplication matrix by
1617 # b1 is what we get if we apply that matrix to b1. The
1618 # second row of the right multiplication matrix by b1
1619 # is what we get when we apply that matrix to b2...
1620 #
1621 # IMPORTANT: this assumes that all vectors are COLUMN
1622 # vectors, unlike our superclass (which uses row vectors).
1623 for b_left in V.basis():
1624 eja_b_left = self.parent()(b_left)
1625 # Multiply in the original EJA, but then get the
1626 # coordinates from the subalgebra in terms of its
1627 # basis.
1628 this_row = V.coordinates((eja_b_left*eja_b_right).vector())
1629 b_right_rows.append(this_row)
1630 b_right_matrix = matrix(F, b_right_rows)
1631 mats.append(b_right_matrix)
1632
1633 # It's an algebra of polynomials in one element, and EJAs
1634 # are power-associative.
1635 #
1636 # TODO: choose generator names intelligently.
1637 return FiniteDimensionalEuclideanJordanAlgebra(F, mats, assume_associative=True, names='f')
1638
1639
1640 def subalgebra_idempotent(self):
1641 """
1642 Find an idempotent in the associative subalgebra I generate
1643 using Proposition 2.3.5 in Baes.
1644
1645 TESTS::
1646
1647 sage: set_random_seed()
1648 sage: J = random_eja()
1649 sage: x = J.random_element()
1650 sage: while x.is_nilpotent():
1651 ....: x = J.random_element()
1652 sage: c = x.subalgebra_idempotent()
1653 sage: c^2 == c
1654 True
1655
1656 """
1657 if self.is_nilpotent():
1658 raise ValueError("this only works with non-nilpotent elements!")
1659
1660 V = self.span_of_powers()
1661 J = self.subalgebra_generated_by()
1662 # Mis-design warning: the basis used for span_of_powers()
1663 # and subalgebra_generated_by() must be the same, and in
1664 # the same order!
1665 u = J(V.coordinates(self.vector()))
1666
1667 # The image of the matrix of left-u^m-multiplication
1668 # will be minimal for some natural number s...
1669 s = 0
1670 minimal_dim = V.dimension()
1671 for i in xrange(1, V.dimension()):
1672 this_dim = (u**i).operator_matrix().image().dimension()
1673 if this_dim < minimal_dim:
1674 minimal_dim = this_dim
1675 s = i
1676
1677 # Now minimal_matrix should correspond to the smallest
1678 # non-zero subspace in Baes's (or really, Koecher's)
1679 # proposition.
1680 #
1681 # However, we need to restrict the matrix to work on the
1682 # subspace... or do we? Can't we just solve, knowing that
1683 # A(c) = u^(s+1) should have a solution in the big space,
1684 # too?
1685 #
1686 # Beware, solve_right() means that we're using COLUMN vectors.
1687 # Our FiniteDimensionalAlgebraElement superclass uses rows.
1688 u_next = u**(s+1)
1689 A = u_next.operator_matrix()
1690 c_coordinates = A.solve_right(u_next.vector())
1691
1692 # Now c_coordinates is the idempotent we want, but it's in
1693 # the coordinate system of the subalgebra.
1694 #
1695 # We need the basis for J, but as elements of the parent algebra.
1696 #
1697 basis = [self.parent(v) for v in V.basis()]
1698 return self.parent().linear_combination(zip(c_coordinates, basis))
1699
1700
1701 def trace(self):
1702 """
1703 Return my trace, the sum of my eigenvalues.
1704
1705 EXAMPLES::
1706
1707 sage: J = JordanSpinEJA(3)
1708 sage: x = sum(J.gens())
1709 sage: x.trace()
1710 2
1711
1712 ::
1713
1714 sage: J = RealCartesianProductEJA(5)
1715 sage: J.one().trace()
1716 5
1717
1718 TESTS:
1719
1720 The trace of an element is a real number::
1721
1722 sage: set_random_seed()
1723 sage: J = random_eja()
1724 sage: J.random_element().trace() in J.base_ring()
1725 True
1726
1727 """
1728 P = self.parent()
1729 r = P.rank()
1730 p = P._charpoly_coeff(r-1)
1731 # The _charpoly_coeff function already adds the factor of
1732 # -1 to ensure that _charpoly_coeff(r-1) is really what
1733 # appears in front of t^{r-1} in the charpoly. However,
1734 # we want the negative of THAT for the trace.
1735 return -p(*self.vector())
1736
1737
1738 def trace_inner_product(self, other):
1739 """
1740 Return the trace inner product of myself and ``other``.
1741
1742 TESTS:
1743
1744 The trace inner product is commutative::
1745
1746 sage: set_random_seed()
1747 sage: J = random_eja()
1748 sage: x = J.random_element(); y = J.random_element()
1749 sage: x.trace_inner_product(y) == y.trace_inner_product(x)
1750 True
1751
1752 The trace inner product is bilinear::
1753
1754 sage: set_random_seed()
1755 sage: J = random_eja()
1756 sage: x = J.random_element()
1757 sage: y = J.random_element()
1758 sage: z = J.random_element()
1759 sage: a = QQ.random_element();
1760 sage: actual = (a*(x+z)).trace_inner_product(y)
1761 sage: expected = ( a*x.trace_inner_product(y) +
1762 ....: a*z.trace_inner_product(y) )
1763 sage: actual == expected
1764 True
1765 sage: actual = x.trace_inner_product(a*(y+z))
1766 sage: expected = ( a*x.trace_inner_product(y) +
1767 ....: a*x.trace_inner_product(z) )
1768 sage: actual == expected
1769 True
1770
1771 The trace inner product satisfies the compatibility
1772 condition in the definition of a Euclidean Jordan algebra::
1773
1774 sage: set_random_seed()
1775 sage: J = random_eja()
1776 sage: x = J.random_element()
1777 sage: y = J.random_element()
1778 sage: z = J.random_element()
1779 sage: (x*y).trace_inner_product(z) == y.trace_inner_product(x*z)
1780 True
1781
1782 """
1783 if not other in self.parent():
1784 raise TypeError("'other' must live in the same algebra")
1785
1786 return (self*other).trace()
1787
1788
1789 class RealCartesianProductEJA(FiniteDimensionalEuclideanJordanAlgebra):
1790 """
1791 Return the Euclidean Jordan Algebra corresponding to the set
1792 `R^n` under the Hadamard product.
1793
1794 Note: this is nothing more than the Cartesian product of ``n``
1795 copies of the spin algebra. Once Cartesian product algebras
1796 are implemented, this can go.
1797
1798 EXAMPLES:
1799
1800 This multiplication table can be verified by hand::
1801
1802 sage: J = RealCartesianProductEJA(3)
1803 sage: e0,e1,e2 = J.gens()
1804 sage: e0*e0
1805 e0
1806 sage: e0*e1
1807 0
1808 sage: e0*e2
1809 0
1810 sage: e1*e1
1811 e1
1812 sage: e1*e2
1813 0
1814 sage: e2*e2
1815 e2
1816
1817 """
1818 @staticmethod
1819 def __classcall_private__(cls, n, field=QQ):
1820 # The FiniteDimensionalAlgebra constructor takes a list of
1821 # matrices, the ith representing right multiplication by the ith
1822 # basis element in the vector space. So if e_1 = (1,0,0), then
1823 # right (Hadamard) multiplication of x by e_1 picks out the first
1824 # component of x; and likewise for the ith basis element e_i.
1825 Qs = [ matrix(field, n, n, lambda k,j: 1*(k == j == i))
1826 for i in xrange(n) ]
1827
1828 fdeja = super(RealCartesianProductEJA, cls)
1829 return fdeja.__classcall_private__(cls, field, Qs, rank=n)
1830
1831 def inner_product(self, x, y):
1832 return _usual_ip(x,y)
1833
1834
1835 def random_eja():
1836 """
1837 Return a "random" finite-dimensional Euclidean Jordan Algebra.
1838
1839 ALGORITHM:
1840
1841 For now, we choose a random natural number ``n`` (greater than zero)
1842 and then give you back one of the following:
1843
1844 * The cartesian product of the rational numbers ``n`` times; this is
1845 ``QQ^n`` with the Hadamard product.
1846
1847 * The Jordan spin algebra on ``QQ^n``.
1848
1849 * The ``n``-by-``n`` rational symmetric matrices with the symmetric
1850 product.
1851
1852 * The ``n``-by-``n`` complex-rational Hermitian matrices embedded
1853 in the space of ``2n``-by-``2n`` real symmetric matrices.
1854
1855 * The ``n``-by-``n`` quaternion-rational Hermitian matrices embedded
1856 in the space of ``4n``-by-``4n`` real symmetric matrices.
1857
1858 Later this might be extended to return Cartesian products of the
1859 EJAs above.
1860
1861 TESTS::
1862
1863 sage: random_eja()
1864 Euclidean Jordan algebra of degree...
1865
1866 """
1867
1868 # The max_n component lets us choose different upper bounds on the
1869 # value "n" that gets passed to the constructor. This is needed
1870 # because e.g. R^{10} is reasonable to test, while the Hermitian
1871 # 10-by-10 quaternion matrices are not.
1872 (constructor, max_n) = choice([(RealCartesianProductEJA, 6),
1873 (JordanSpinEJA, 6),
1874 (RealSymmetricEJA, 5),
1875 (ComplexHermitianEJA, 4),
1876 (QuaternionHermitianEJA, 3)])
1877 n = ZZ.random_element(1, max_n)
1878 return constructor(n, field=QQ)
1879
1880
1881
1882 def _real_symmetric_basis(n, field=QQ):
1883 """
1884 Return a basis for the space of real symmetric n-by-n matrices.
1885 """
1886 # The basis of symmetric matrices, as matrices, in their R^(n-by-n)
1887 # coordinates.
1888 S = []
1889 for i in xrange(n):
1890 for j in xrange(i+1):
1891 Eij = matrix(field, n, lambda k,l: k==i and l==j)
1892 if i == j:
1893 Sij = Eij
1894 else:
1895 # Beware, orthogonal but not normalized!
1896 Sij = Eij + Eij.transpose()
1897 S.append(Sij)
1898 return tuple(S)
1899
1900
1901 def _complex_hermitian_basis(n, field=QQ):
1902 """
1903 Returns a basis for the space of complex Hermitian n-by-n matrices.
1904
1905 TESTS::
1906
1907 sage: set_random_seed()
1908 sage: n = ZZ.random_element(1,5)
1909 sage: all( M.is_symmetric() for M in _complex_hermitian_basis(n) )
1910 True
1911
1912 """
1913 F = QuadraticField(-1, 'I')
1914 I = F.gen()
1915
1916 # This is like the symmetric case, but we need to be careful:
1917 #
1918 # * We want conjugate-symmetry, not just symmetry.
1919 # * The diagonal will (as a result) be real.
1920 #
1921 S = []
1922 for i in xrange(n):
1923 for j in xrange(i+1):
1924 Eij = matrix(field, n, lambda k,l: k==i and l==j)
1925 if i == j:
1926 Sij = _embed_complex_matrix(Eij)
1927 S.append(Sij)
1928 else:
1929 # Beware, orthogonal but not normalized! The second one
1930 # has a minus because it's conjugated.
1931 Sij_real = _embed_complex_matrix(Eij + Eij.transpose())
1932 S.append(Sij_real)
1933 Sij_imag = _embed_complex_matrix(I*Eij - I*Eij.transpose())
1934 S.append(Sij_imag)
1935 return tuple(S)
1936
1937
1938 def _quaternion_hermitian_basis(n, field=QQ):
1939 """
1940 Returns a basis for the space of quaternion Hermitian n-by-n matrices.
1941
1942 TESTS::
1943
1944 sage: set_random_seed()
1945 sage: n = ZZ.random_element(1,5)
1946 sage: all( M.is_symmetric() for M in _quaternion_hermitian_basis(n) )
1947 True
1948
1949 """
1950 Q = QuaternionAlgebra(QQ,-1,-1)
1951 I,J,K = Q.gens()
1952
1953 # This is like the symmetric case, but we need to be careful:
1954 #
1955 # * We want conjugate-symmetry, not just symmetry.
1956 # * The diagonal will (as a result) be real.
1957 #
1958 S = []
1959 for i in xrange(n):
1960 for j in xrange(i+1):
1961 Eij = matrix(Q, n, lambda k,l: k==i and l==j)
1962 if i == j:
1963 Sij = _embed_quaternion_matrix(Eij)
1964 S.append(Sij)
1965 else:
1966 # Beware, orthogonal but not normalized! The second,
1967 # third, and fourth ones have a minus because they're
1968 # conjugated.
1969 Sij_real = _embed_quaternion_matrix(Eij + Eij.transpose())
1970 S.append(Sij_real)
1971 Sij_I = _embed_quaternion_matrix(I*Eij - I*Eij.transpose())
1972 S.append(Sij_I)
1973 Sij_J = _embed_quaternion_matrix(J*Eij - J*Eij.transpose())
1974 S.append(Sij_J)
1975 Sij_K = _embed_quaternion_matrix(K*Eij - K*Eij.transpose())
1976 S.append(Sij_K)
1977 return tuple(S)
1978
1979
1980 def _mat2vec(m):
1981 return vector(m.base_ring(), m.list())
1982
1983 def _vec2mat(v):
1984 return matrix(v.base_ring(), sqrt(v.degree()), v.list())
1985
1986 def _multiplication_table_from_matrix_basis(basis):
1987 """
1988 At least three of the five simple Euclidean Jordan algebras have the
1989 symmetric multiplication (A,B) |-> (AB + BA)/2, where the
1990 multiplication on the right is matrix multiplication. Given a basis
1991 for the underlying matrix space, this function returns a
1992 multiplication table (obtained by looping through the basis
1993 elements) for an algebra of those matrices. A reordered copy
1994 of the basis is also returned to work around the fact that
1995 the ``span()`` in this function will change the order of the basis
1996 from what we think it is, to... something else.
1997 """
1998 # In S^2, for example, we nominally have four coordinates even
1999 # though the space is of dimension three only. The vector space V
2000 # is supposed to hold the entire long vector, and the subspace W
2001 # of V will be spanned by the vectors that arise from symmetric
2002 # matrices. Thus for S^2, dim(V) == 4 and dim(W) == 3.
2003 field = basis[0].base_ring()
2004 dimension = basis[0].nrows()
2005
2006 V = VectorSpace(field, dimension**2)
2007 W = V.span( _mat2vec(s) for s in basis )
2008
2009 # Taking the span above reorders our basis (thanks, jerk!) so we
2010 # need to put our "matrix basis" in the same order as the
2011 # (reordered) vector basis.
2012 S = tuple( _vec2mat(b) for b in W.basis() )
2013
2014 Qs = []
2015 for s in S:
2016 # Brute force the multiplication-by-s matrix by looping
2017 # through all elements of the basis and doing the computation
2018 # to find out what the corresponding row should be. BEWARE:
2019 # these multiplication tables won't be symmetric! It therefore
2020 # becomes REALLY IMPORTANT that the underlying algebra
2021 # constructor uses ROW vectors and not COLUMN vectors. That's
2022 # why we're computing rows here and not columns.
2023 Q_rows = []
2024 for t in S:
2025 this_row = _mat2vec((s*t + t*s)/2)
2026 Q_rows.append(W.coordinates(this_row))
2027 Q = matrix(field, W.dimension(), Q_rows)
2028 Qs.append(Q)
2029
2030 return (Qs, S)
2031
2032
2033 def _embed_complex_matrix(M):
2034 """
2035 Embed the n-by-n complex matrix ``M`` into the space of real
2036 matrices of size 2n-by-2n via the map the sends each entry `z = a +
2037 bi` to the block matrix ``[[a,b],[-b,a]]``.
2038
2039 EXAMPLES::
2040
2041 sage: F = QuadraticField(-1,'i')
2042 sage: x1 = F(4 - 2*i)
2043 sage: x2 = F(1 + 2*i)
2044 sage: x3 = F(-i)
2045 sage: x4 = F(6)
2046 sage: M = matrix(F,2,[[x1,x2],[x3,x4]])
2047 sage: _embed_complex_matrix(M)
2048 [ 4 -2| 1 2]
2049 [ 2 4|-2 1]
2050 [-----+-----]
2051 [ 0 -1| 6 0]
2052 [ 1 0| 0 6]
2053
2054 TESTS:
2055
2056 Embedding is a homomorphism (isomorphism, in fact)::
2057
2058 sage: set_random_seed()
2059 sage: n = ZZ.random_element(5)
2060 sage: F = QuadraticField(-1, 'i')
2061 sage: X = random_matrix(F, n)
2062 sage: Y = random_matrix(F, n)
2063 sage: actual = _embed_complex_matrix(X) * _embed_complex_matrix(Y)
2064 sage: expected = _embed_complex_matrix(X*Y)
2065 sage: actual == expected
2066 True
2067
2068 """
2069 n = M.nrows()
2070 if M.ncols() != n:
2071 raise ValueError("the matrix 'M' must be square")
2072 field = M.base_ring()
2073 blocks = []
2074 for z in M.list():
2075 a = z.real()
2076 b = z.imag()
2077 blocks.append(matrix(field, 2, [[a,b],[-b,a]]))
2078
2079 # We can drop the imaginaries here.
2080 return block_matrix(field.base_ring(), n, blocks)
2081
2082
2083 def _unembed_complex_matrix(M):
2084 """
2085 The inverse of _embed_complex_matrix().
2086
2087 EXAMPLES::
2088
2089 sage: A = matrix(QQ,[ [ 1, 2, 3, 4],
2090 ....: [-2, 1, -4, 3],
2091 ....: [ 9, 10, 11, 12],
2092 ....: [-10, 9, -12, 11] ])
2093 sage: _unembed_complex_matrix(A)
2094 [ 2*i + 1 4*i + 3]
2095 [ 10*i + 9 12*i + 11]
2096
2097 TESTS:
2098
2099 Unembedding is the inverse of embedding::
2100
2101 sage: set_random_seed()
2102 sage: F = QuadraticField(-1, 'i')
2103 sage: M = random_matrix(F, 3)
2104 sage: _unembed_complex_matrix(_embed_complex_matrix(M)) == M
2105 True
2106
2107 """
2108 n = ZZ(M.nrows())
2109 if M.ncols() != n:
2110 raise ValueError("the matrix 'M' must be square")
2111 if not n.mod(2).is_zero():
2112 raise ValueError("the matrix 'M' must be a complex embedding")
2113
2114 F = QuadraticField(-1, 'i')
2115 i = F.gen()
2116
2117 # Go top-left to bottom-right (reading order), converting every
2118 # 2-by-2 block we see to a single complex element.
2119 elements = []
2120 for k in xrange(n/2):
2121 for j in xrange(n/2):
2122 submat = M[2*k:2*k+2,2*j:2*j+2]
2123 if submat[0,0] != submat[1,1]:
2124 raise ValueError('bad on-diagonal submatrix')
2125 if submat[0,1] != -submat[1,0]:
2126 raise ValueError('bad off-diagonal submatrix')
2127 z = submat[0,0] + submat[0,1]*i
2128 elements.append(z)
2129
2130 return matrix(F, n/2, elements)
2131
2132
2133 def _embed_quaternion_matrix(M):
2134 """
2135 Embed the n-by-n quaternion matrix ``M`` into the space of real
2136 matrices of size 4n-by-4n by first sending each quaternion entry
2137 `z = a + bi + cj + dk` to the block-complex matrix
2138 ``[[a + bi, c+di],[-c + di, a-bi]]`, and then embedding those into
2139 a real matrix.
2140
2141 EXAMPLES::
2142
2143 sage: Q = QuaternionAlgebra(QQ,-1,-1)
2144 sage: i,j,k = Q.gens()
2145 sage: x = 1 + 2*i + 3*j + 4*k
2146 sage: M = matrix(Q, 1, [[x]])
2147 sage: _embed_quaternion_matrix(M)
2148 [ 1 2 3 4]
2149 [-2 1 -4 3]
2150 [-3 4 1 -2]
2151 [-4 -3 2 1]
2152
2153 Embedding is a homomorphism (isomorphism, in fact)::
2154
2155 sage: set_random_seed()
2156 sage: n = ZZ.random_element(5)
2157 sage: Q = QuaternionAlgebra(QQ,-1,-1)
2158 sage: X = random_matrix(Q, n)
2159 sage: Y = random_matrix(Q, n)
2160 sage: actual = _embed_quaternion_matrix(X)*_embed_quaternion_matrix(Y)
2161 sage: expected = _embed_quaternion_matrix(X*Y)
2162 sage: actual == expected
2163 True
2164
2165 """
2166 quaternions = M.base_ring()
2167 n = M.nrows()
2168 if M.ncols() != n:
2169 raise ValueError("the matrix 'M' must be square")
2170
2171 F = QuadraticField(-1, 'i')
2172 i = F.gen()
2173
2174 blocks = []
2175 for z in M.list():
2176 t = z.coefficient_tuple()
2177 a = t[0]
2178 b = t[1]
2179 c = t[2]
2180 d = t[3]
2181 cplx_matrix = matrix(F, 2, [[ a + b*i, c + d*i],
2182 [-c + d*i, a - b*i]])
2183 blocks.append(_embed_complex_matrix(cplx_matrix))
2184
2185 # We should have real entries by now, so use the realest field
2186 # we've got for the return value.
2187 return block_matrix(quaternions.base_ring(), n, blocks)
2188
2189
2190 def _unembed_quaternion_matrix(M):
2191 """
2192 The inverse of _embed_quaternion_matrix().
2193
2194 EXAMPLES::
2195
2196 sage: M = matrix(QQ, [[ 1, 2, 3, 4],
2197 ....: [-2, 1, -4, 3],
2198 ....: [-3, 4, 1, -2],
2199 ....: [-4, -3, 2, 1]])
2200 sage: _unembed_quaternion_matrix(M)
2201 [1 + 2*i + 3*j + 4*k]
2202
2203 TESTS:
2204
2205 Unembedding is the inverse of embedding::
2206
2207 sage: set_random_seed()
2208 sage: Q = QuaternionAlgebra(QQ, -1, -1)
2209 sage: M = random_matrix(Q, 3)
2210 sage: _unembed_quaternion_matrix(_embed_quaternion_matrix(M)) == M
2211 True
2212
2213 """
2214 n = ZZ(M.nrows())
2215 if M.ncols() != n:
2216 raise ValueError("the matrix 'M' must be square")
2217 if not n.mod(4).is_zero():
2218 raise ValueError("the matrix 'M' must be a complex embedding")
2219
2220 Q = QuaternionAlgebra(QQ,-1,-1)
2221 i,j,k = Q.gens()
2222
2223 # Go top-left to bottom-right (reading order), converting every
2224 # 4-by-4 block we see to a 2-by-2 complex block, to a 1-by-1
2225 # quaternion block.
2226 elements = []
2227 for l in xrange(n/4):
2228 for m in xrange(n/4):
2229 submat = _unembed_complex_matrix(M[4*l:4*l+4,4*m:4*m+4])
2230 if submat[0,0] != submat[1,1].conjugate():
2231 raise ValueError('bad on-diagonal submatrix')
2232 if submat[0,1] != -submat[1,0].conjugate():
2233 raise ValueError('bad off-diagonal submatrix')
2234 z = submat[0,0].real() + submat[0,0].imag()*i
2235 z += submat[0,1].real()*j + submat[0,1].imag()*k
2236 elements.append(z)
2237
2238 return matrix(Q, n/4, elements)
2239
2240
2241 # The usual inner product on R^n.
2242 def _usual_ip(x,y):
2243 return x.vector().inner_product(y.vector())
2244
2245 # The inner product used for the real symmetric simple EJA.
2246 # We keep it as a separate function because e.g. the complex
2247 # algebra uses the same inner product, except divided by 2.
2248 def _matrix_ip(X,Y):
2249 X_mat = X.natural_representation()
2250 Y_mat = Y.natural_representation()
2251 return (X_mat*Y_mat).trace()
2252
2253
2254 class RealSymmetricEJA(FiniteDimensionalEuclideanJordanAlgebra):
2255 """
2256 The rank-n simple EJA consisting of real symmetric n-by-n
2257 matrices, the usual symmetric Jordan product, and the trace inner
2258 product. It has dimension `(n^2 + n)/2` over the reals.
2259
2260 EXAMPLES::
2261
2262 sage: J = RealSymmetricEJA(2)
2263 sage: e0, e1, e2 = J.gens()
2264 sage: e0*e0
2265 e0
2266 sage: e1*e1
2267 e0 + e2
2268 sage: e2*e2
2269 e2
2270
2271 TESTS:
2272
2273 The degree of this algebra is `(n^2 + n) / 2`::
2274
2275 sage: set_random_seed()
2276 sage: n = ZZ.random_element(1,5)
2277 sage: J = RealSymmetricEJA(n)
2278 sage: J.degree() == (n^2 + n)/2
2279 True
2280
2281 The Jordan multiplication is what we think it is::
2282
2283 sage: set_random_seed()
2284 sage: n = ZZ.random_element(1,5)
2285 sage: J = RealSymmetricEJA(n)
2286 sage: x = J.random_element()
2287 sage: y = J.random_element()
2288 sage: actual = (x*y).natural_representation()
2289 sage: X = x.natural_representation()
2290 sage: Y = y.natural_representation()
2291 sage: expected = (X*Y + Y*X)/2
2292 sage: actual == expected
2293 True
2294 sage: J(expected) == x*y
2295 True
2296
2297 """
2298 @staticmethod
2299 def __classcall_private__(cls, n, field=QQ):
2300 S = _real_symmetric_basis(n, field=field)
2301 (Qs, T) = _multiplication_table_from_matrix_basis(S)
2302
2303 fdeja = super(RealSymmetricEJA, cls)
2304 return fdeja.__classcall_private__(cls,
2305 field,
2306 Qs,
2307 rank=n,
2308 natural_basis=T)
2309
2310 def inner_product(self, x, y):
2311 return _matrix_ip(x,y)
2312
2313
2314 class ComplexHermitianEJA(FiniteDimensionalEuclideanJordanAlgebra):
2315 """
2316 The rank-n simple EJA consisting of complex Hermitian n-by-n
2317 matrices over the real numbers, the usual symmetric Jordan product,
2318 and the real-part-of-trace inner product. It has dimension `n^2` over
2319 the reals.
2320
2321 TESTS:
2322
2323 The degree of this algebra is `n^2`::
2324
2325 sage: set_random_seed()
2326 sage: n = ZZ.random_element(1,5)
2327 sage: J = ComplexHermitianEJA(n)
2328 sage: J.degree() == n^2
2329 True
2330
2331 The Jordan multiplication is what we think it is::
2332
2333 sage: set_random_seed()
2334 sage: n = ZZ.random_element(1,5)
2335 sage: J = ComplexHermitianEJA(n)
2336 sage: x = J.random_element()
2337 sage: y = J.random_element()
2338 sage: actual = (x*y).natural_representation()
2339 sage: X = x.natural_representation()
2340 sage: Y = y.natural_representation()
2341 sage: expected = (X*Y + Y*X)/2
2342 sage: actual == expected
2343 True
2344 sage: J(expected) == x*y
2345 True
2346
2347 """
2348 @staticmethod
2349 def __classcall_private__(cls, n, field=QQ):
2350 S = _complex_hermitian_basis(n)
2351 (Qs, T) = _multiplication_table_from_matrix_basis(S)
2352
2353 fdeja = super(ComplexHermitianEJA, cls)
2354 return fdeja.__classcall_private__(cls,
2355 field,
2356 Qs,
2357 rank=n,
2358 natural_basis=T)
2359
2360 def inner_product(self, x, y):
2361 # Since a+bi on the diagonal is represented as
2362 #
2363 # a + bi = [ a b ]
2364 # [ -b a ],
2365 #
2366 # we'll double-count the "a" entries if we take the trace of
2367 # the embedding.
2368 return _matrix_ip(x,y)/2
2369
2370
2371 class QuaternionHermitianEJA(FiniteDimensionalEuclideanJordanAlgebra):
2372 """
2373 The rank-n simple EJA consisting of self-adjoint n-by-n quaternion
2374 matrices, the usual symmetric Jordan product, and the
2375 real-part-of-trace inner product. It has dimension `2n^2 - n` over
2376 the reals.
2377
2378 TESTS:
2379
2380 The degree of this algebra is `n^2`::
2381
2382 sage: set_random_seed()
2383 sage: n = ZZ.random_element(1,5)
2384 sage: J = QuaternionHermitianEJA(n)
2385 sage: J.degree() == 2*(n^2) - n
2386 True
2387
2388 The Jordan multiplication is what we think it is::
2389
2390 sage: set_random_seed()
2391 sage: n = ZZ.random_element(1,5)
2392 sage: J = QuaternionHermitianEJA(n)
2393 sage: x = J.random_element()
2394 sage: y = J.random_element()
2395 sage: actual = (x*y).natural_representation()
2396 sage: X = x.natural_representation()
2397 sage: Y = y.natural_representation()
2398 sage: expected = (X*Y + Y*X)/2
2399 sage: actual == expected
2400 True
2401 sage: J(expected) == x*y
2402 True
2403
2404 """
2405 @staticmethod
2406 def __classcall_private__(cls, n, field=QQ):
2407 S = _quaternion_hermitian_basis(n)
2408 (Qs, T) = _multiplication_table_from_matrix_basis(S)
2409
2410 fdeja = super(QuaternionHermitianEJA, cls)
2411 return fdeja.__classcall_private__(cls,
2412 field,
2413 Qs,
2414 rank=n,
2415 natural_basis=T)
2416
2417 def inner_product(self, x, y):
2418 # Since a+bi+cj+dk on the diagonal is represented as
2419 #
2420 # a + bi +cj + dk = [ a b c d]
2421 # [ -b a -d c]
2422 # [ -c d a -b]
2423 # [ -d -c b a],
2424 #
2425 # we'll quadruple-count the "a" entries if we take the trace of
2426 # the embedding.
2427 return _matrix_ip(x,y)/4
2428
2429
2430 class JordanSpinEJA(FiniteDimensionalEuclideanJordanAlgebra):
2431 """
2432 The rank-2 simple EJA consisting of real vectors ``x=(x0, x_bar)``
2433 with the usual inner product and jordan product ``x*y =
2434 (<x_bar,y_bar>, x0*y_bar + y0*x_bar)``. It has dimension `n` over
2435 the reals.
2436
2437 EXAMPLES:
2438
2439 This multiplication table can be verified by hand::
2440
2441 sage: J = JordanSpinEJA(4)
2442 sage: e0,e1,e2,e3 = J.gens()
2443 sage: e0*e0
2444 e0
2445 sage: e0*e1
2446 e1
2447 sage: e0*e2
2448 e2
2449 sage: e0*e3
2450 e3
2451 sage: e1*e2
2452 0
2453 sage: e1*e3
2454 0
2455 sage: e2*e3
2456 0
2457
2458 """
2459 @staticmethod
2460 def __classcall_private__(cls, n, field=QQ):
2461 Qs = []
2462 id_matrix = identity_matrix(field, n)
2463 for i in xrange(n):
2464 ei = id_matrix.column(i)
2465 Qi = zero_matrix(field, n)
2466 Qi.set_row(0, ei)
2467 Qi.set_column(0, ei)
2468 Qi += diagonal_matrix(n, [ei[0]]*n)
2469 # The addition of the diagonal matrix adds an extra ei[0] in the
2470 # upper-left corner of the matrix.
2471 Qi[0,0] = Qi[0,0] * ~field(2)
2472 Qs.append(Qi)
2473
2474 # The rank of the spin algebra is two, unless we're in a
2475 # one-dimensional ambient space (because the rank is bounded by
2476 # the ambient dimension).
2477 fdeja = super(JordanSpinEJA, cls)
2478 return fdeja.__classcall_private__(cls, field, Qs, rank=min(n,2))
2479
2480 def inner_product(self, x, y):
2481 return _usual_ip(x,y)