1 from sage
.misc
.cachefunc
import cached_method
2 from sage
.combinat
.free_module
import CombinatorialFreeModule
3 from sage
.modules
.with_basis
.indexed_element
import IndexedFreeModuleElement
4 from sage
.rings
.all
import AA
6 from mjo
.matrix_algebra
import MatrixAlgebra
, MatrixAlgebraElement
8 class Octonion(IndexedFreeModuleElement
):
13 sage: from mjo.hurwitz import Octonions
18 sage: x = sum(O.gens())
20 e0 - e1 - e2 - e3 - e4 - e5 - e6 - e7
24 Conjugating twice gets you the original element::
27 sage: x = O.random_element()
28 sage: x.conjugate().conjugate() == x
32 from sage
.rings
.all
import ZZ
33 from sage
.matrix
.matrix_space
import MatrixSpace
34 C
= MatrixSpace(ZZ
,8).diagonal_matrix((1,-1,-1,-1,-1,-1,-1,-1))
35 return self
.parent().from_vector(C
*self
.to_vector())
39 Return the real part of this octonion.
41 The real part of an octonion is its projection onto the span
42 of the first generator. In other words, the "first dimension"
43 is real and the others are imaginary.
47 sage: from mjo.hurwitz import Octonions
52 sage: x = sum(O.gens())
58 This method is idempotent::
61 sage: x = O.random_element()
62 sage: x.real().real() == x.real()
66 return (self
+ self
.conjugate())/2
70 Return the imaginary part of this octonion.
72 The imaginary part of an octonion is its projection onto the
73 orthogonal complement of the span of the first generator. In
74 other words, the "first dimension" is real and the others are
79 sage: from mjo.hurwitz import Octonions
84 sage: x = sum(O.gens())
86 e1 + e2 + e3 + e4 + e5 + e6 + e7
90 This method is idempotent::
93 sage: x = O.random_element()
94 sage: x.imag().imag() == x.imag()
98 return (self
- self
.conjugate())/2
100 def _norm_squared(self
):
101 return (self
*self
.conjugate()).coefficient(0)
105 Return the norm of this octonion.
109 sage: from mjo.hurwitz import Octonions
113 sage: O = Octonions()
119 The norm is nonnegative and belongs to the base field::
121 sage: O = Octonions()
122 sage: n = O.random_element().norm()
123 sage: n >= 0 and n in O.base_ring()
126 The norm is homogeneous::
128 sage: O = Octonions()
129 sage: x = O.random_element()
130 sage: alpha = O.base_ring().random_element()
131 sage: (alpha*x).norm() == alpha.abs()*x.norm()
135 return self
._norm
_squared
().sqrt()
137 # The absolute value notation is typically used for complex numbers...
138 # and norm() isn't supported in AA, so this lets us use abs() in all
139 # of the division algebras we need.
144 Return the inverse of this element if it exists.
148 sage: from mjo.hurwitz import Octonions
152 sage: O = Octonions()
153 sage: x = sum(O.gens())
154 sage: x*x.inverse() == O.one()
159 sage: O = Octonions()
160 sage: O.one().inverse() == O.one()
165 sage: O = Octonions()
166 sage: x = O.random_element()
167 sage: x.is_zero() or ( x*x.inverse() == O.one() )
172 raise ValueError("zero is not invertible")
173 return self
.conjugate()/self
._norm
_squared
()
177 class Octonions(CombinatorialFreeModule
):
181 sage: from mjo.hurwitz import Octonions
186 Octonion algebra with base ring Algebraic Real Field
187 sage: Octonions(field=QQ)
188 Octonion algebra with base ring Rational Field
195 # Not associative, not commutative
196 from sage
.categories
.magmatic_algebras
import MagmaticAlgebras
197 category
= MagmaticAlgebras(field
).FiniteDimensional()
198 category
= category
.WithBasis().Unital()
200 super().__init
__(field
,
202 element_class
=Octonion
,
207 # The product of each basis element is plus/minus another
208 # basis element that can simply be looked up on
209 # https://en.wikipedia.org/wiki/Octonion
210 e0
, e1
, e2
, e3
, e4
, e5
, e6
, e7
= self
.gens()
211 self
._multiplication
_table
= (
212 (e0
, e1
, e2
, e3
, e4
, e5
, e6
, e7
),
213 (e1
,-e0
, e3
,-e2
, e5
,-e4
,-e7
, e6
),
214 (e2
,-e3
,-e0
, e1
, e6
, e7
,-e4
,-e5
),
215 (e3
, e2
,-e1
,-e0
, e7
,-e6
, e5
,-e4
),
216 (e4
,-e5
,-e6
,-e7
,-e0
, e1
, e2
, e3
),
217 (e5
, e4
,-e7
, e6
,-e1
,-e0
,-e3
, e2
),
218 (e6
, e7
, e4
,-e5
,-e2
, e3
,-e0
,-e1
),
219 (e7
,-e6
, e5
, e4
,-e3
,-e2
, e1
,-e0
),
222 def product_on_basis(self
, i
, j
):
223 return self
._multiplication
_table
[i
][j
]
227 Return the monomial index (basis element) corresponding to the
228 octonion unit element.
232 sage: from mjo.hurwitz import Octonions
236 This gives the correct unit element::
238 sage: O = Octonions()
239 sage: x = O.random_element()
240 sage: x*O.one() == x and O.one()*x == x
247 return ("Octonion algebra with base ring %s" % self
.base_ring())
249 def multiplication_table(self
):
251 Return a visual representation of this algebra's multiplication
252 table (on basis elements).
256 sage: from mjo.hurwitz import Octonions
260 The multiplication table is what Wikipedia says it is::
262 sage: Octonions().multiplication_table()
263 +----++----+-----+-----+-----+-----+-----+-----+-----+
264 | * || e0 | e1 | e2 | e3 | e4 | e5 | e6 | e7 |
265 +====++====+=====+=====+=====+=====+=====+=====+=====+
266 | e0 || e0 | e1 | e2 | e3 | e4 | e5 | e6 | e7 |
267 +----++----+-----+-----+-----+-----+-----+-----+-----+
268 | e1 || e1 | -e0 | e3 | -e2 | e5 | -e4 | -e7 | e6 |
269 +----++----+-----+-----+-----+-----+-----+-----+-----+
270 | e2 || e2 | -e3 | -e0 | e1 | e6 | e7 | -e4 | -e5 |
271 +----++----+-----+-----+-----+-----+-----+-----+-----+
272 | e3 || e3 | e2 | -e1 | -e0 | e7 | -e6 | e5 | -e4 |
273 +----++----+-----+-----+-----+-----+-----+-----+-----+
274 | e4 || e4 | -e5 | -e6 | -e7 | -e0 | e1 | e2 | e3 |
275 +----++----+-----+-----+-----+-----+-----+-----+-----+
276 | e5 || e5 | e4 | -e7 | e6 | -e1 | -e0 | -e3 | e2 |
277 +----++----+-----+-----+-----+-----+-----+-----+-----+
278 | e6 || e6 | e7 | e4 | -e5 | -e2 | e3 | -e0 | -e1 |
279 +----++----+-----+-----+-----+-----+-----+-----+-----+
280 | e7 || e7 | -e6 | e5 | e4 | -e3 | -e2 | e1 | -e0 |
281 +----++----+-----+-----+-----+-----+-----+-----+-----+
285 # Prepend the header row.
286 M
= [["*"] + list(self
.gens())]
288 # And to each subsequent row, prepend an entry that belongs to
289 # the left-side "header column."
290 M
+= [ [self
.monomial(i
)] + [ self
.monomial(i
)*self
.monomial(j
)
294 from sage
.misc
.table
import table
295 return table(M
, header_row
=True, header_column
=True, frame
=True)
301 class HurwitzMatrixAlgebraElement(MatrixAlgebraElement
):
304 Return the entrywise conjugate of this matrix.
308 sage: from mjo.hurwitz import ComplexMatrixAlgebra
312 sage: A = ComplexMatrixAlgebra(2, QQbar, ZZ)
313 sage: M = A([ [ I, 1 + 2*I],
323 entries
= [ [ self
[i
,j
].conjugate()
324 for j
in range(self
.ncols())]
325 for i
in range(self
.nrows()) ]
326 return self
.parent()._element
_constructor
_(entries
)
328 def conjugate_transpose(self
):
330 Return the conjugate-transpose of this matrix.
334 sage: from mjo.hurwitz import ComplexMatrixAlgebra
338 sage: A = ComplexMatrixAlgebra(2, QQbar, ZZ)
339 sage: M = A([ [ I, 2*I],
341 sage: M.conjugate_transpose()
347 sage: M.conjugate_transpose().to_vector()
348 (0, -1, 0, -3, 0, -2, 0, -4)
351 entries
= [ [ self
[j
,i
].conjugate()
352 for j
in range(self
.ncols())]
353 for i
in range(self
.nrows()) ]
354 return self
.parent()._element
_constructor
_(entries
)
356 def is_hermitian(self
):
361 sage: from mjo.hurwitz import (ComplexMatrixAlgebra,
362 ....: HurwitzMatrixAlgebra)
366 sage: A = ComplexMatrixAlgebra(2, QQbar, ZZ)
367 sage: M = A([ [ 0,I],
369 sage: M.is_hermitian()
374 sage: A = ComplexMatrixAlgebra(2, QQbar, ZZ)
375 sage: M = A([ [ 0,0],
377 sage: M.is_hermitian()
382 sage: A = HurwitzMatrixAlgebra(2, AA, QQ)
383 sage: M = A([ [1, 1],
385 sage: M.is_hermitian()
389 # A tiny bit faster than checking equality with the conjugate
391 return all( self
[i
,j
] == self
[j
,i
].conjugate()
392 for i
in range(self
.nrows())
393 for j
in range(i
+1) )
396 def is_skew_symmetric(self
):
398 Return whether or not this matrix is skew-symmetric.
402 sage: from mjo.hurwitz import (ComplexMatrixAlgebra,
403 ....: HurwitzMatrixAlgebra)
407 sage: A = ComplexMatrixAlgebra(2, QQbar, ZZ)
408 sage: M = A([ [ 0,I],
410 sage: M.is_skew_symmetric()
415 sage: A = ComplexMatrixAlgebra(2, QQbar, ZZ)
416 sage: M = A([ [ 0, 1+I],
418 sage: M.is_skew_symmetric()
423 sage: A = HurwitzMatrixAlgebra(2, AA, QQ)
424 sage: M = A([ [1, 1],
426 sage: M.is_skew_symmetric()
431 sage: A = ComplexMatrixAlgebra(2, QQbar, ZZ)
432 sage: M = A([ [2*I , 1 + I],
433 ....: [-1 + I, -2*I] ])
434 sage: M.is_skew_symmetric()
438 # A tiny bit faster than checking equality with the negation
440 return all( self
[i
,j
] == -self
[j
,i
]
441 for i
in range(self
.nrows())
442 for j
in range(i
+1) )
445 class HurwitzMatrixAlgebra(MatrixAlgebra
):
447 A class of matrix algebras whose entries come from a Hurwitz
450 For our purposes, we consider "a Hurwitz" algebra to be the real
451 or complex numbers, the quaternions, or the octonions. These are
452 typically also referred to as the Euclidean Hurwitz algebras, or
453 the normed division algebras.
455 By the Cayley-Dickson construction, each Hurwitz algebra is an
456 algebra over the real numbers, so we restrict the scalar field in
457 this case to be real. This also allows us to more accurately
458 produce the generators of the matrix algebra.
460 Element
= HurwitzMatrixAlgebraElement
462 def __init__(self
, n
, entry_algebra
, scalars
, **kwargs
):
463 from sage
.rings
.all
import RR
464 if not scalars
.is_subring(RR
):
465 # Not perfect, but it's what we're using.
466 raise ValueError("scalar field is not real")
468 super().__init
__(n
, entry_algebra
, scalars
, **kwargs
)
470 def entry_algebra_gens(self
):
472 Return a tuple of the generators of (that is, a basis for) the
473 entries of this matrix algebra.
475 This works around the inconsistency in the ``gens()`` methods
476 of the real/complex numbers, quaternions, and octonions.
480 sage: from mjo.hurwitz import Octonions, HurwitzMatrixAlgebra
484 The inclusion of the unit element is inconsistent across
485 (subalgebras of) Hurwitz algebras::
491 sage: QuaternionAlgebra(AA,1,-1).gens()
493 sage: Octonions().gens()
494 (e0, e1, e2, e3, e4, e5, e6, e7)
496 The unit element is always returned by this method, so the
497 sets of generators have cartinality 1,2,4, and 8 as you'd
500 sage: HurwitzMatrixAlgebra(2, AA, AA).entry_algebra_gens()
502 sage: HurwitzMatrixAlgebra(2, QQbar, AA).entry_algebra_gens()
504 sage: Q = QuaternionAlgebra(AA,-1,-1)
505 sage: HurwitzMatrixAlgebra(2, Q, AA).entry_algebra_gens()
507 sage: O = Octonions()
508 sage: HurwitzMatrixAlgebra(2, O, AA).entry_algebra_gens()
509 (e0, e1, e2, e3, e4, e5, e6, e7)
512 gs
= self
.entry_algebra().gens()
513 one
= self
.entry_algebra().one()
517 return (one
,) + tuple(gs
)
521 class OctonionMatrixAlgebra(HurwitzMatrixAlgebra
):
523 The algebra of ``n``-by-``n`` matrices with octonion entries over
524 (a subfield of) the real numbers.
526 The usual matrix spaces in SageMath don't support octonion entries
527 because they assume that the entries of the matrix come from a
528 commutative and associative ring, and the octonions are neither.
532 sage: from mjo.hurwitz import Octonions, OctonionMatrixAlgebra
536 sage: OctonionMatrixAlgebra(3)
537 Module of 3 by 3 matrices with entries in Octonion algebra with base
538 ring Algebraic Real Field over the scalar ring Algebraic Real Field
542 sage: OctonionMatrixAlgebra(3,scalars=QQ)
543 Module of 3 by 3 matrices with entries in Octonion algebra with
544 base ring Rational Field over the scalar ring Rational Field
548 sage: O = Octonions(RR)
549 sage: A = OctonionMatrixAlgebra(1,O)
551 Module of 1 by 1 matrices with entries in Octonion algebra with
552 base ring Real Field with 53 bits of precision over the scalar
553 ring Algebraic Real Field
555 +---------------------+
556 | 1.00000000000000*e0 |
557 +---------------------+
559 (+---------------------+
560 | 1.00000000000000*e0 |
561 +---------------------+,
562 +---------------------+
563 | 1.00000000000000*e1 |
564 +---------------------+,
565 +---------------------+
566 | 1.00000000000000*e2 |
567 +---------------------+,
568 +---------------------+
569 | 1.00000000000000*e3 |
570 +---------------------+,
571 +---------------------+
572 | 1.00000000000000*e4 |
573 +---------------------+,
574 +---------------------+
575 | 1.00000000000000*e5 |
576 +---------------------+,
577 +---------------------+
578 | 1.00000000000000*e6 |
579 +---------------------+,
580 +---------------------+
581 | 1.00000000000000*e7 |
582 +---------------------+)
586 sage: A = OctonionMatrixAlgebra(2)
587 sage: e0,e1,e2,e3,e4,e5,e6,e7 = A.entry_algebra().gens()
588 sage: A([ [e0+e4, e1+e5],
589 ....: [e2-e6, e3-e7] ])
590 +---------+---------+
591 | e0 + e4 | e1 + e5 |
592 +---------+---------+
593 | e2 - e6 | e3 - e7 |
594 +---------+---------+
598 sage: A1 = OctonionMatrixAlgebra(1,scalars=QQ)
599 sage: A2 = OctonionMatrixAlgebra(1,scalars=QQ)
600 sage: cartesian_product([A1,A2])
601 Module of 1 by 1 matrices with entries in Octonion algebra with
602 base ring Rational Field over the scalar ring Rational Field (+)
603 Module of 1 by 1 matrices with entries in Octonion algebra with
604 base ring Rational Field over the scalar ring Rational Field
608 sage: A = OctonionMatrixAlgebra(ZZ.random_element(10))
609 sage: x = A.random_element()
610 sage: x*A.one() == x and A.one()*x == x
614 def __init__(self
, n
, entry_algebra
=None, scalars
=AA
, **kwargs
):
615 if entry_algebra
is None:
616 entry_algebra
= Octonions(field
=scalars
)
622 class QuaternionMatrixAlgebra(HurwitzMatrixAlgebra
):
624 The algebra of ``n``-by-``n`` matrices with quaternion entries over
625 (a subfield of) the real numbers.
627 The usual matrix spaces in SageMath don't support quaternion entries
628 because they assume that the entries of the matrix come from a
629 commutative ring, and the quaternions are not commutative.
633 sage: from mjo.hurwitz import QuaternionMatrixAlgebra
637 sage: QuaternionMatrixAlgebra(3)
638 Module of 3 by 3 matrices with entries in Quaternion
639 Algebra (-1, -1) with base ring Algebraic Real Field
640 over the scalar ring Algebraic Real Field
644 sage: QuaternionMatrixAlgebra(3,scalars=QQ)
645 Module of 3 by 3 matrices with entries in Quaternion
646 Algebra (-1, -1) with base ring Rational Field over
647 the scalar ring Rational Field
651 sage: Q = QuaternionAlgebra(RDF, -1, -1)
652 sage: A = QuaternionMatrixAlgebra(1,Q)
654 Module of 1 by 1 matrices with entries in Quaternion Algebra
655 (-1.0, -1.0) with base ring Real Double Field over the scalar
656 ring Algebraic Real Field
677 sage: A = QuaternionMatrixAlgebra(2)
678 sage: i,j,k = A.entry_algebra().gens()
679 sage: A([ [1+i, j-2],
689 sage: A1 = QuaternionMatrixAlgebra(1,scalars=QQ)
690 sage: A2 = QuaternionMatrixAlgebra(2,scalars=QQ)
691 sage: cartesian_product([A1,A2])
692 Module of 1 by 1 matrices with entries in Quaternion Algebra
693 (-1, -1) with base ring Rational Field over the scalar ring
694 Rational Field (+) Module of 2 by 2 matrices with entries in
695 Quaternion Algebra (-1, -1) with base ring Rational Field over
696 the scalar ring Rational Field
700 sage: A = QuaternionMatrixAlgebra(ZZ.random_element(10))
701 sage: x = A.random_element()
702 sage: x*A.one() == x and A.one()*x == x
706 def __init__(self
, n
, entry_algebra
=None, scalars
=AA
, **kwargs
):
707 if entry_algebra
is None:
708 # The -1,-1 gives us the "usual" definition of quaternion
709 from sage
.algebras
.quatalg
.quaternion_algebra
import (
712 entry_algebra
= QuaternionAlgebra(scalars
,-1,-1)
713 super().__init
__(n
, entry_algebra
, scalars
, **kwargs
)
715 def _entry_algebra_element_to_vector(self
, entry
):
720 sage: from mjo.hurwitz import QuaternionMatrixAlgebra
724 sage: A = QuaternionMatrixAlgebra(2)
725 sage: u = A.entry_algebra().one()
726 sage: A._entry_algebra_element_to_vector(u)
728 sage: i,j,k = A.entry_algebra().gens()
729 sage: A._entry_algebra_element_to_vector(i)
731 sage: A._entry_algebra_element_to_vector(j)
733 sage: A._entry_algebra_element_to_vector(k)
737 from sage
.modules
.free_module
import FreeModule
738 d
= len(self
.entry_algebra_gens())
739 V
= FreeModule(self
.entry_algebra().base_ring(), d
)
740 return V(entry
.coefficient_tuple())
742 class ComplexMatrixAlgebra(HurwitzMatrixAlgebra
):
744 The algebra of ``n``-by-``n`` matrices with complex entries over
745 (a subfield of) the real numbers.
747 These differ from the usual complex matrix spaces in SageMath
748 because the scalar field is real (and not assumed to be the same
749 as the space from which the entries are drawn). The space of
750 `1`-by-`1` complex matrices will have dimension two, for example.
754 sage: from mjo.hurwitz import ComplexMatrixAlgebra
758 sage: ComplexMatrixAlgebra(3)
759 Module of 3 by 3 matrices with entries in Algebraic Field
760 over the scalar ring Algebraic Real Field
764 sage: ComplexMatrixAlgebra(3,scalars=QQ)
765 Module of 3 by 3 matrices with entries in Algebraic Field
766 over the scalar ring Rational Field
770 sage: A = ComplexMatrixAlgebra(1,CC)
772 Module of 1 by 1 matrices with entries in Complex Field with
773 53 bits of precision over the scalar ring Algebraic Real Field
779 (+------------------+
781 +------------------+,
782 +--------------------+
783 | 1.00000000000000*I |
784 +--------------------+)
788 sage: A = ComplexMatrixAlgebra(2)
789 sage: (I,) = A.entry_algebra().gens()
800 sage: A1 = ComplexMatrixAlgebra(1,scalars=QQ)
801 sage: A2 = ComplexMatrixAlgebra(2,scalars=QQ)
802 sage: cartesian_product([A1,A2])
803 Module of 1 by 1 matrices with entries in Algebraic Field over
804 the scalar ring Rational Field (+) Module of 2 by 2 matrices with
805 entries in Algebraic Field over the scalar ring Rational Field
809 sage: A = ComplexMatrixAlgebra(ZZ.random_element(10))
810 sage: x = A.random_element()
811 sage: x*A.one() == x and A.one()*x == x
815 def __init__(self
, n
, entry_algebra
=None, scalars
=AA
, **kwargs
):
816 if entry_algebra
is None:
817 from sage
.rings
.all
import QQbar
818 entry_algebra
= QQbar
819 super().__init
__(n
, entry_algebra
, scalars
, **kwargs
)
821 def _entry_algebra_element_to_vector(self
, entry
):
826 sage: from mjo.hurwitz import ComplexMatrixAlgebra
830 sage: A = ComplexMatrixAlgebra(2, QQbar, QQ)
831 sage: A._entry_algebra_element_to_vector(QQbar(1))
833 sage: A._entry_algebra_element_to_vector(QQbar(I))
837 from sage
.modules
.free_module
import FreeModule
838 d
= len(self
.entry_algebra_gens())
839 V
= FreeModule(self
.entry_algebra().base_ring(), d
)
840 return V((entry
.real(), entry
.imag()))