This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
quaternion_algebra.py
2984 lines (2365 loc) · 104 KB
/
quaternion_algebra.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Quaternion Algebras
AUTHORS:
- Jon Bobber (2009): rewrite
- William Stein (2009): rewrite
- Julian Rueth (2014-03-02): use UniqueFactory for caching
This code is partly based on Sage code by David Kohel from 2005.
TESTS:
Pickling test::
sage: Q.<i,j,k> = QuaternionAlgebra(QQ,-5,-2)
sage: Q == loads(dumps(Q))
True
"""
# ****************************************************************************
# Copyright (C) 2009 William Stein <wstein@gmail.com>
# Copyright (C) 2009 Jonathan Bober <jwbober@gmail.com>
# Copyright (C) 2014 Julian Rueth <julian.rueth@fsfe.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# https://www.gnu.org/licenses/
# ****************************************************************************
from __future__ import print_function, absolute_import
from sage.arith.all import (hilbert_conductor_inverse, hilbert_conductor,
factor, gcd, kronecker_symbol, valuation)
from sage.rings.all import RR, Integer
from sage.rings.integer_ring import ZZ
from sage.rings.rational import Rational
from sage.rings.finite_rings.finite_field_constructor import GF
from sage.rings.ring import Algebra
from sage.rings.ideal import Ideal_fractional
from sage.rings.rational_field import is_RationalField, QQ
from sage.rings.infinity import infinity
from sage.rings.number_field.number_field import is_NumberField
from sage.rings.power_series_ring import PowerSeriesRing
from sage.structure.category_object import normalize_names
from sage.structure.parent_gens import ParentWithGens
from sage.structure.parent import Parent
from sage.matrix.matrix_space import MatrixSpace
from sage.matrix.constructor import diagonal_matrix, matrix
from sage.structure.sequence import Sequence
from sage.structure.element import is_RingElement
from sage.structure.factory import UniqueFactory
from sage.modules.free_module import VectorSpace, FreeModule
from sage.modules.free_module_element import vector
from operator import itemgetter
from . import quaternion_algebra_element
from . import quaternion_algebra_cython
from sage.modular.modsym.p1list import P1List
from sage.misc.cachefunc import cached_method
from sage.categories.fields import Fields
from sage.categories.algebras import Algebras
_Fields = Fields()
########################################################
# Constructor
########################################################
class QuaternionAlgebraFactory(UniqueFactory):
r"""
There are three input formats:
- ``QuaternionAlgebra(a, b)``: quaternion algebra generated by ``i``, ``j``
subject to `i^2 = a`, `j^2 = b`, `j \cdot i = -i \cdot j`.
- ``QuaternionAlgebra(K, a, b)``: same as above but over a field ``K``.
Here, ``a`` and ``b`` are nonzero elements of a field (``K``) of
characteristic not 2, and we set `k = i \cdot j`.
- ``QuaternionAlgebra(D)``: a rational quaternion algebra with
discriminant ``D``, where `D > 1` is a squarefree integer.
EXAMPLES:
``QuaternionAlgebra(a, b)`` - return quaternion algebra over the
*smallest* field containing the nonzero elements ``a`` and ``b`` with
generators ``i``, ``j``, ``k`` with `i^2=a`, `j^2=b` and `j \cdot i =
-i \cdot j`::
sage: QuaternionAlgebra(-2,-3)
Quaternion Algebra (-2, -3) with base ring Rational Field
sage: QuaternionAlgebra(GF(5)(2), GF(5)(3))
Quaternion Algebra (2, 3) with base ring Finite Field of size 5
sage: QuaternionAlgebra(2, GF(5)(3))
Quaternion Algebra (2, 3) with base ring Finite Field of size 5
sage: QuaternionAlgebra(QQ[sqrt(2)](-1), -5)
Quaternion Algebra (-1, -5) with base ring Number Field in sqrt2 with defining polynomial x^2 - 2 with sqrt2 = 1.414213562373095?
sage: QuaternionAlgebra(sqrt(-1), sqrt(-3))
Quaternion Algebra (I, sqrt(-3)) with base ring Symbolic Ring
sage: QuaternionAlgebra(1r,1)
Quaternion Algebra (1, 1) with base ring Rational Field
Python ints, longs and floats may be passed to the
``QuaternionAlgebra(a, b)`` constructor, as may all pairs of nonzero
elements of a ring not of characteristic 2. The following tests address
the issues raised in :trac:`10601`::
sage: QuaternionAlgebra(1r,1)
Quaternion Algebra (1, 1) with base ring Rational Field
sage: QuaternionAlgebra(1,1.0r)
Quaternion Algebra (1.00000000000000, 1.00000000000000) with base ring Real Field with 53 bits of precision
sage: QuaternionAlgebra(0,0)
Traceback (most recent call last):
...
ValueError: a and b must be nonzero
sage: QuaternionAlgebra(GF(2)(1),1)
Traceback (most recent call last):
...
ValueError: a and b must be elements of a ring with characteristic not 2
sage: a = PermutationGroupElement([1,2,3])
sage: QuaternionAlgebra(a, a)
Traceback (most recent call last):
...
ValueError: a and b must be elements of a ring with characteristic not 2
``QuaternionAlgebra(K, a, b)`` - return quaternion algebra over the
field ``K`` with generators ``i``, ``j``, ``k`` with `i^2=a`, `j^2=b`
and `i \cdot j = -j \cdot i`::
sage: QuaternionAlgebra(QQ, -7, -21)
Quaternion Algebra (-7, -21) with base ring Rational Field
sage: QuaternionAlgebra(QQ[sqrt(2)], -2,-3)
Quaternion Algebra (-2, -3) with base ring Number Field in sqrt2 with defining polynomial x^2 - 2 with sqrt2 = 1.414213562373095?
``QuaternionAlgebra(D)`` - ``D`` is a squarefree integer; returns a
rational quaternion algebra of discriminant ``D``::
sage: QuaternionAlgebra(1)
Quaternion Algebra (-1, 1) with base ring Rational Field
sage: QuaternionAlgebra(2)
Quaternion Algebra (-1, -1) with base ring Rational Field
sage: QuaternionAlgebra(7)
Quaternion Algebra (-1, -7) with base ring Rational Field
sage: QuaternionAlgebra(2*3*5*7)
Quaternion Algebra (-22, 210) with base ring Rational Field
If the coefficients `a` and `b` in the definition of the quaternion
algebra are not integral, then a slower generic type is used for
arithmetic::
sage: type(QuaternionAlgebra(-1,-3).0)
<... 'sage.algebras.quatalg.quaternion_algebra_element.QuaternionAlgebraElement_rational_field'>
sage: type(QuaternionAlgebra(-1,-3/2).0)
<... 'sage.algebras.quatalg.quaternion_algebra_element.QuaternionAlgebraElement_generic'>
Make sure caching is sane::
sage: A = QuaternionAlgebra(2,3); A
Quaternion Algebra (2, 3) with base ring Rational Field
sage: B = QuaternionAlgebra(GF(5)(2),GF(5)(3)); B
Quaternion Algebra (2, 3) with base ring Finite Field of size 5
sage: A is QuaternionAlgebra(2,3)
True
sage: B is QuaternionAlgebra(GF(5)(2),GF(5)(3))
True
sage: Q = QuaternionAlgebra(2); Q
Quaternion Algebra (-1, -1) with base ring Rational Field
sage: Q is QuaternionAlgebra(QQ,-1,-1)
True
sage: Q is QuaternionAlgebra(-1,-1)
True
sage: Q.<ii,jj,kk> = QuaternionAlgebra(15); Q.variable_names()
('ii', 'jj', 'kk')
sage: QuaternionAlgebra(15).variable_names()
('i', 'j', 'k')
TESTS:
Verify that bug found when working on :trac:`12006` involving coercing
invariants into the base field is fixed::
sage: Q = QuaternionAlgebra(-1,-1); Q
Quaternion Algebra (-1, -1) with base ring Rational Field
sage: parent(Q._a)
Rational Field
sage: parent(Q._b)
Rational Field
"""
def create_key(self, arg0, arg1=None, arg2=None, names='i,j,k'):
"""
Create a key that uniquely determines a quaternion algebra.
TESTS::
sage: QuaternionAlgebra.create_key(-1,-1)
(Rational Field, -1, -1, ('i', 'j', 'k'))
"""
# QuaternionAlgebra(D)
if arg1 is None and arg2 is None:
K = QQ
D = Integer(arg0)
a, b = hilbert_conductor_inverse(D)
a = Rational(a)
b = Rational(b)
elif arg2 is None:
# If arg0 or arg1 are Python data types, coerce them
# to the relevant Sage types. This is a bit inelegant.
L = []
for a in [arg0,arg1]:
if is_RingElement(a):
L.append(a)
elif isinstance(a, int):
L.append(Integer(a))
elif isinstance(a, float):
L.append(RR(a))
else:
raise ValueError("a and b must be elements of a ring with characteristic not 2")
# QuaternionAlgebra(a, b)
v = Sequence(L)
K = v.universe().fraction_field()
a = K(v[0])
b = K(v[1])
# QuaternionAlgebra(K, a, b)
else:
K = arg0
if K not in _Fields:
raise TypeError("base ring of quaternion algebra must be a field")
a = K(arg1)
b = K(arg2)
if K.characteristic() == 2:
# Lameness!
raise ValueError("a and b must be elements of a ring with characteristic not 2")
if a == 0 or b == 0:
raise ValueError("a and b must be nonzero")
names = normalize_names(3, names)
return (K, a, b, names)
def create_object(self, version, key, **extra_args):
"""
Create the object from the key (extra arguments are ignored). This is
only called if the object was not found in the cache.
TESTS::
sage: QuaternionAlgebra.create_object("6.0", (QQ, -1, -1, ('i', 'j', 'k')))
Quaternion Algebra (-1, -1) with base ring Rational Field
"""
K, a, b, names = key
return QuaternionAlgebra_ab(K, a, b, names=names)
QuaternionAlgebra = QuaternionAlgebraFactory("QuaternionAlgebra")
########################################################
# Classes
########################################################
def is_QuaternionAlgebra(A):
"""
Return ``True`` if ``A`` is of the QuaternionAlgebra data type.
EXAMPLES::
sage: sage.algebras.quatalg.quaternion_algebra.is_QuaternionAlgebra(QuaternionAlgebra(QQ,-1,-1))
True
sage: sage.algebras.quatalg.quaternion_algebra.is_QuaternionAlgebra(ZZ)
False
"""
return isinstance(A, QuaternionAlgebra_abstract)
class QuaternionAlgebra_abstract(Algebra):
def _repr_(self):
"""
EXAMPLES::
sage: sage.algebras.quatalg.quaternion_algebra.QuaternionAlgebra_abstract(QQ)._repr_()
'Quaternion Algebra with base ring Rational Field'
"""
return "Quaternion Algebra with base ring %s" % self.base_ring()
def ngens(self):
"""
Return the number of generators of the quaternion algebra as a K-vector
space, not including 1.
This value is always 3: the algebra is spanned
by the standard basis `1`, `i`, `j`, `k`.
EXAMPLES::
sage: Q.<i,j,k> = QuaternionAlgebra(QQ,-5,-2)
sage: Q.ngens()
3
sage: Q.gens()
[i, j, k]
"""
return 3
def basis(self):
"""
Return the fixed basis of ``self``, which is `1`, `i`, `j`, `k`, where
`i`, `j`, `k` are the generators of ``self``.
EXAMPLES::
sage: Q.<i,j,k> = QuaternionAlgebra(QQ,-5,-2)
sage: Q.basis()
(1, i, j, k)
sage: Q.<xyz,abc,theta> = QuaternionAlgebra(GF(9,'a'),-5,-2)
sage: Q.basis()
(1, xyz, abc, theta)
The basis is cached::
sage: Q.basis() is Q.basis()
True
"""
try:
return self.__basis
except AttributeError:
self.__basis = tuple([self(1)] + list(self.gens()))
return self.__basis
@cached_method
def inner_product_matrix(self):
"""
Return the inner product matrix associated to ``self``.
This is the
Gram matrix of the reduced norm as a quadratic form on ``self``.
The standard basis `1`, `i`, `j`, `k` is orthogonal, so this matrix
is just the diagonal matrix with diagonal entries `2`, `2a`, `2b`,
`2ab`.
EXAMPLES::
sage: Q.<i,j,k> = QuaternionAlgebra(-5,-19)
sage: Q.inner_product_matrix()
[ 2 0 0 0]
[ 0 10 0 0]
[ 0 0 38 0]
[ 0 0 0 190]
"""
a, b = self._a, self._b
M = diagonal_matrix(self.base_ring(), [2, -2 * a, -2 * b, 2 * a * b])
M.set_immutable()
return M
def is_commutative(self):
"""
Return ``False`` always, since all quaternion algebras are
noncommutative.
EXAMPLES::
sage: Q.<i,j,k> = QuaternionAlgebra(QQ, -3,-7)
sage: Q.is_commutative()
False
"""
return False
def is_division_algebra(self):
"""
Return ``True`` if the quaternion algebra is a division algebra (i.e.
every nonzero element in ``self`` is invertible), and ``False`` if the
quaternion algebra is isomorphic to the 2x2 matrix algebra.
EXAMPLES::
sage: QuaternionAlgebra(QQ,-5,-2).is_division_algebra()
True
sage: QuaternionAlgebra(1).is_division_algebra()
False
sage: QuaternionAlgebra(2,9).is_division_algebra()
False
sage: QuaternionAlgebra(RR(2.),1).is_division_algebra()
Traceback (most recent call last):
...
NotImplementedError: base field must be rational numbers
"""
if not is_RationalField(self.base_ring()):
raise NotImplementedError("base field must be rational numbers")
return self.discriminant() != 1
def is_matrix_ring(self):
"""
Return ``True`` if the quaternion algebra is isomorphic to the 2x2
matrix ring, and ``False`` if ``self`` is a division algebra (i.e.
every nonzero element in ``self`` is invertible).
EXAMPLES::
sage: QuaternionAlgebra(QQ,-5,-2).is_matrix_ring()
False
sage: QuaternionAlgebra(1).is_matrix_ring()
True
sage: QuaternionAlgebra(2,9).is_matrix_ring()
True
sage: QuaternionAlgebra(RR(2.),1).is_matrix_ring()
Traceback (most recent call last):
...
NotImplementedError: base field must be rational numbers
"""
if not is_RationalField(self.base_ring()):
raise NotImplementedError("base field must be rational numbers")
return self.discriminant() == 1
def is_exact(self):
"""
Return ``True`` if elements of this quaternion algebra are represented
exactly, i.e. there is no precision loss when doing arithmetic. A
quaternion algebra is exact if and only if its base field is
exact.
EXAMPLES::
sage: Q.<i,j,k> = QuaternionAlgebra(QQ, -3, -7)
sage: Q.is_exact()
True
sage: Q.<i,j,k> = QuaternionAlgebra(Qp(7), -3, -7)
sage: Q.is_exact()
False
"""
return self.base_ring().is_exact()
def is_field(self, proof = True):
"""
Return ``False`` always, since all quaternion algebras are
noncommutative and all fields are commutative.
EXAMPLES::
sage: Q.<i,j,k> = QuaternionAlgebra(QQ, -3, -7)
sage: Q.is_field()
False
"""
return False
def is_finite(self):
"""
Return ``True`` if the quaternion algebra is finite as a set.
Algorithm: A quaternion algebra is finite if and only if the
base field is finite.
EXAMPLES::
sage: Q.<i,j,k> = QuaternionAlgebra(QQ, -3, -7)
sage: Q.is_finite()
False
sage: Q.<i,j,k> = QuaternionAlgebra(GF(5), -3, -7)
sage: Q.is_finite()
True
"""
return self.base_ring().is_finite()
def is_integral_domain(self, proof=True):
"""
Return ``False`` always, since all quaternion algebras are
noncommutative and integral domains are commutative (in Sage).
EXAMPLES::
sage: Q.<i,j,k> = QuaternionAlgebra(QQ, -3, -7)
sage: Q.is_integral_domain()
False
"""
return False
def is_noetherian(self):
"""
Return ``True`` always, since any quaternion algebra is a noetherian
ring (because it is a finitely generated module over a field).
EXAMPLES::
sage: Q.<i,j,k> = QuaternionAlgebra(QQ, -3, -7)
sage: Q.is_noetherian()
True
"""
return True
def order(self):
"""
Return the number of elements of the quaternion algebra, or
``+Infinity`` if the algebra is not finite.
EXAMPLES::
sage: Q.<i,j,k> = QuaternionAlgebra(QQ, -3, -7)
sage: Q.order()
+Infinity
sage: Q.<i,j,k> = QuaternionAlgebra(GF(5), -3, -7)
sage: Q.order()
625
"""
return (self.base_ring().order())**4
def random_element(self, *args, **kwds):
"""
Return a random element of this quaternion algebra.
The ``args`` and ``kwds`` are passed to the ``random_element`` method
of the base ring.
EXAMPLES::
sage: g = QuaternionAlgebra(QQ[sqrt(2)], -3, 7).random_element()
sage: g.parent() is QuaternionAlgebra(QQ[sqrt(2)], -3, 7)
True
sage: g = QuaternionAlgebra(-3, 19).random_element()
sage: g.parent() is QuaternionAlgebra(-3, 19)
True
sage: g = QuaternionAlgebra(GF(17)(2), 3).random_element()
sage: g.parent() is QuaternionAlgebra(GF(17)(2), 3)
True
Specify the numerator and denominator bounds::
sage: g = QuaternionAlgebra(-3,19).random_element(10^6, 10^6)
sage: for h in g:
....: assert h.numerator() in range(-10^6, 10^6 + 1)
....: assert h.denominator() in range(10^6 + 1)
sage: g = QuaternionAlgebra(-3,19).random_element(5, 4)
sage: for h in g:
....: assert h.numerator() in range(-5, 5 + 1)
....: assert h.denominator() in range(4 + 1)
"""
K = self.base_ring()
return self([K.random_element(*args, **kwds) for _ in range(4)])
@cached_method
def vector_space(self):
"""
Return the vector space associated to ``self`` with inner product given
by the reduced norm.
EXAMPLES::
sage: QuaternionAlgebra(-3,19).vector_space()
Ambient quadratic space of dimension 4 over Rational Field
Inner product matrix:
[ 2 0 0 0]
[ 0 6 0 0]
[ 0 0 -38 0]
[ 0 0 0 -114]
"""
return VectorSpace(self.base_ring(), 4, inner_product_matrix=self.inner_product_matrix())
class QuaternionAlgebra_ab(QuaternionAlgebra_abstract):
"""
The quaternion algebra of the form `(a, b/K)`, where `i^2=a`, `j^2 = b`,
and `j*i = -i*j`. ``K`` is a field not of characteristic 2 and ``a``,
``b`` are nonzero elements of ``K``.
See ``QuaternionAlgebra`` for many more examples.
INPUT:
- ``base_ring`` -- commutative ring
- ``a, b`` -- elements of ``base_ring``
- ``names`` -- string (optional, default 'i,j,k') names of the generators
EXAMPLES::
sage: QuaternionAlgebra(QQ, -7, -21) # indirect doctest
Quaternion Algebra (-7, -21) with base ring Rational Field
"""
def __init__(self, base_ring, a, b, names='i,j,k'):
"""
Create the quaternion algebra with `i^2 = a`, `j^2 = b`, and
`i*j = -j*i = k`.
TESTS:
Test making quaternion elements (using the element constructor)::
sage: Q.<i,j,k> = QuaternionAlgebra(QQ,-1,-2)
sage: a = Q(2/3); a
2/3
sage: type(a)
<... 'sage.algebras.quatalg.quaternion_algebra_element.QuaternionAlgebraElement_rational_field'>
sage: Q(a)
2/3
sage: Q([1,2,3,4])
1 + 2*i + 3*j + 4*k
sage: Q((1,2,3,4))
1 + 2*i + 3*j + 4*k
sage: Q(-3/5)
-3/5
sage: TestSuite(Q).run()
The base ring must be a field::
sage: Q.<ii,jj,kk> = QuaternionAlgebra(ZZ,-5,-19)
Traceback (most recent call last):
...
TypeError: base ring of quaternion algebra must be a field
"""
ParentWithGens.__init__(self, base_ring, names=names, category=Algebras(base_ring).Division())
self._a = a
self._b = b
if is_RationalField(base_ring) and a.denominator() == 1 and b.denominator() == 1:
self.Element = quaternion_algebra_element.QuaternionAlgebraElement_rational_field
elif is_NumberField(base_ring) and base_ring.degree() > 2 and base_ring.is_absolute() and \
a.denominator() == 1 and b.denominator() == 1 and base_ring.defining_polynomial().is_monic():
# This QuaternionAlgebraElement_number_field class is not
# designed to work with elements of a quadratic field. To
# do that, the main thing would be to implement
# __getitem__, etc. This would maybe give a factor of 2
# (or more?) speedup. Much care must be taken because the
# underlying representation of quadratic fields is a bit
# tricky.
self.Element = quaternion_algebra_element.QuaternionAlgebraElement_number_field
elif base_ring in _Fields:
self.Element = quaternion_algebra_element.QuaternionAlgebraElement_generic
else:
raise TypeError("base ring of quaternion algebra must be a field")
self._populate_coercion_lists_(coerce_list=[base_ring])
self._gens = [self([0,1,0,0]), self([0,0,1,0]), self([0,0,0,1])]
@cached_method
def maximal_order(self, take_shortcuts=True):
r"""
Return a maximal order in this quaternion algebra.
The algorithm used is from [Voi2012]_.
INPUT:
- ``take_shortcuts`` -- (default: ``True``) if the discriminant is
prime and the invariants of the algebra are of a nice form, use
Proposition 5.2 of [Piz1980]_.
OUTPUT:
A maximal order in this quaternion algebra.
EXAMPLES::
sage: QuaternionAlgebra(-1,-7).maximal_order()
Order of Quaternion Algebra (-1, -7) with base ring Rational Field with basis (1/2 + 1/2*j, 1/2*i + 1/2*k, j, k)
sage: QuaternionAlgebra(-1,-1).maximal_order().basis()
(1/2 + 1/2*i + 1/2*j + 1/2*k, i, j, k)
sage: QuaternionAlgebra(-1,-11).maximal_order().basis()
(1/2 + 1/2*j, 1/2*i + 1/2*k, j, k)
sage: QuaternionAlgebra(-1,-3).maximal_order().basis()
(1/2 + 1/2*j, 1/2*i + 1/2*k, j, k)
sage: QuaternionAlgebra(-3,-1).maximal_order().basis()
(1/2 + 1/2*i, 1/2*j - 1/2*k, i, -k)
sage: QuaternionAlgebra(-2,-5).maximal_order().basis()
(1/2 + 1/2*j + 1/2*k, 1/4*i + 1/2*j + 1/4*k, j, k)
sage: QuaternionAlgebra(-5,-2).maximal_order().basis()
(1/2 + 1/2*i - 1/2*k, 1/2*i + 1/4*j - 1/4*k, i, -k)
sage: QuaternionAlgebra(-17,-3).maximal_order().basis()
(1/2 + 1/2*j, 1/2*i + 1/2*k, -1/3*j - 1/3*k, k)
sage: QuaternionAlgebra(-3,-17).maximal_order().basis()
(1/2 + 1/2*i, 1/2*j - 1/2*k, -1/3*i + 1/3*k, -k)
sage: QuaternionAlgebra(-17*9,-3).maximal_order().basis()
(1, 1/3*i, 1/6*i + 1/2*j, 1/2 + 1/3*j + 1/18*k)
sage: QuaternionAlgebra(-2, -389).maximal_order().basis()
(1/2 + 1/2*j + 1/2*k, 1/4*i + 1/2*j + 1/4*k, j, k)
If you want bases containing 1, switch off ``take_shortcuts``::
sage: QuaternionAlgebra(-3,-89).maximal_order(take_shortcuts=False)
Order of Quaternion Algebra (-3, -89) with base ring Rational Field with basis (1, 1/2 + 1/2*i, j, 1/2 + 1/6*i + 1/2*j + 1/6*k)
sage: QuaternionAlgebra(1,1).maximal_order(take_shortcuts=False) # Matrix ring
Order of Quaternion Algebra (1, 1) with base ring Rational Field with basis (1, 1/2 + 1/2*i, j, 1/2*j + 1/2*k)
sage: QuaternionAlgebra(-22,210).maximal_order(take_shortcuts=False)
Order of Quaternion Algebra (-22, 210) with base ring Rational Field with basis (1, i, 1/2*i + 1/2*j, 1/2 + 17/22*i + 1/44*k)
sage: for d in ( m for m in range(1, 750) if is_squarefree(m) ): # long time (3s)
....: A = QuaternionAlgebra(d)
....: R = A.maximal_order(take_shortcuts=False)
....: assert A.discriminant() == R.discriminant()
We do not support number fields other than the rationals yet::
sage: K = QuadraticField(5)
sage: QuaternionAlgebra(K,-1,-1).maximal_order()
Traceback (most recent call last):
...
NotImplementedError: maximal order only implemented for rational quaternion algebras
"""
if self.base_ring() != QQ:
raise NotImplementedError("maximal order only implemented for rational quaternion algebras")
d_A = self.discriminant()
# The following only works over QQ if the discriminant is prime
# and if the invariants are of the special form
# (every quaternion algebra of prime discriminant has a representation
# of such a form though)
a, b = self.invariants()
if take_shortcuts and d_A.is_prime() and a in ZZ and b in ZZ:
a = ZZ(a)
b = ZZ(b)
i,j,k = self.gens()
# if necessary, try to swap invariants to match Pizer's paper
if (a != -1 and b == -1) or (b == -2) \
or (a != -1 and a != -2 and (-a) % 8 != 1):
a, b = b, a
i, j = j, i
k = i*j
basis = []
if (a,b) == (-1,-1):
basis = [(1+i+j+k)/2, i, j, k]
elif a == -1 and (-b).is_prime() and ((-b) % 4 == 3):
basis = [(1+j)/2, (i+k)/2, j, k]
elif a == -2 and (-b).is_prime() and ((-b) % 8 == 5):
basis = [(1+j+k)/2, (i+2*j+k)/4, j, k]
elif (-a).is_prime() and (-b).is_prime():
q = -b
p = -a
if q % 4 == 3 and kronecker_symbol(p,q) == -1:
a = 0
while (a*a*p + 1)%q != 0:
a += 1
basis = [(1+j)/2, (i+k)/2, -(j+a*k)/q, k]
if basis:
return self.quaternion_order(basis)
# The following code should always work (over QQ)
# Start with <1,i,j,k>
R = self.quaternion_order([1] + self.gens())
d_R = R.discriminant()
e_new_gens = []
# For each prime at which R is not yet maximal, make it bigger
for (p,p_val) in d_R.factor():
e = R.basis()
while self.quaternion_order(e).discriminant().valuation(p) > d_A.valuation(p):
# Compute a normalized basis at p
f = normalize_basis_at_p(list(e), p)
# Ensure the basis lies in R by clearing denominators
# (this may make the order smaller at q != p)
# Also saturate the basis (divide out p as far as possible)
V = self.base_ring()**4
A = matrix(self.base_ring(), 4, 4, [list(g) for g in e])
e_n = []
x_rows = A.solve_left(matrix([ V(vec.coefficient_tuple()) for (vec,val) in f ]), check=False).rows()
denoms = [ x.denominator() for x in x_rows ]
for i in range(4):
vec = f[i][0]
val = f[i][1]
v = (val/2).floor()
e_n.append(denoms[i] / p**(v) * vec)
# for e_n to become p-saturated we still need to sort by
# ascending valuation of the quadratic form
lst = sorted(zip(e_n, [f[m][1].mod(2) for m in range(4)]),
key=itemgetter(1))
e_n = list(next(zip(*lst)))
# Final step: Enlarge the basis at p
if p != 2:
# ensure that v_p(e_n[1]**2) = 0 by swapping basis elements
if ZZ(e_n[1]**2).valuation(p) != 0:
if ZZ(e_n[2]**2).valuation(p) == 0:
e_n[1], e_n[2] = e_n[2], e_n[1]
else:
e_n[1], e_n[3] = e_n[3], e_n[1]
a = ZZ(e_n[1]**2)
b = ZZ(e_n[2]**2)
if b.valuation(p) > 0: # if v_p(b) = 0, then already p-maximal
F = ZZ.quo(p)
if F(a).is_square():
x = F(a).sqrt().lift()
if (x**2 - a).mod(p**2) == 0: # make sure v_p(x**2 - a) = 1
x = x + p
g = 1/p*(x - e_n[1])*e_n[2]
e_n[2] = g
e_n[3] = e_n[1]*g
else: # p == 2
t = e_n[1].reduced_trace()
a = -e_n[1].reduced_norm()
b = ZZ(e_n[2]**2)
if t.valuation(p) == 0:
if b.valuation(p) > 0:
x = a
if (x**2 - t*x + a).mod(p**2) == 0: # make sure v_p(...) = 1
x = x + p
g = 1/p*(x - e_n[1])*e_n[2]
e_n[2] = g
e_n[3] = e_n[1]*g
else: # t.valuation(p) > 0
(y,z,w) = maxord_solve_aux_eq(a, b, p)
g = 1/p*(1 + y*e_n[1] + z*e_n[2] + w*e_n[1]*e_n[2])
h = (z*b)*e_n[1] - (y*a)*e_n[2]
e_n[1:4] = [g,h,g*h]
if (1 - a*y**2 - b*z**2 + a*b*w**2).valuation(2) > 2:
e_n = basis_for_quaternion_lattice(list(e) + e_n[1:], reverse=True)
# e_n now contains elements that locally at p give a bigger order,
# but the basis may be messed up at other primes (it might not even
# be an order). We will join them all together at the end
e = e_n
e_new_gens.extend(e[1:])
e_new = basis_for_quaternion_lattice(list(R.basis()) + e_new_gens, reverse=True)
return self.quaternion_order(e_new)
def invariants(self):
"""
Return the structural invariants `a`, `b` of this quaternion
algebra: ``self`` is generated by `i`, `j` subject to
`i^2 = a`, `j^2 = b` and `j*i = -i*j`.
EXAMPLES::
sage: Q.<i,j,k> = QuaternionAlgebra(15)
sage: Q.invariants()
(-3, 5)
sage: i^2
-3
sage: j^2
5
"""
return self._a, self._b
def __eq__(self, other):
"""
Compare self and other.
EXAMPLES::
sage: QuaternionAlgebra(-1,-7) == QuaternionAlgebra(-1,-7)
True
sage: QuaternionAlgebra(-1,-7) == QuaternionAlgebra(-1,-5)
False
"""
if not isinstance(other, QuaternionAlgebra_abstract):
return False
return (self.base_ring() == other.base_ring() and
(self._a, self._b) == (other._a, other._b))
def __ne__(self, other):
"""
Compare self and other.
EXAMPLES::
sage: QuaternionAlgebra(-1,-7) != QuaternionAlgebra(-1,-7)
False
sage: QuaternionAlgebra(-1,-7) != QuaternionAlgebra(-1,-5)
True
"""
return not self.__eq__(other)
def __hash__(self):
"""
Compute the hash of ``self``.
EXAMPLES::
sage: h1 = hash(QuaternionAlgebra(-1,-7))
sage: h2 = hash(QuaternionAlgebra(-1,-7))
sage: h3 = hash(QuaternionAlgebra(-1,-5))
sage: h1 == h2 and h1 != h3
True
"""
return hash((self.base_ring(), self._a, self._b))
def gen(self, i=0):
"""
Return the `i^{th}` generator of ``self``.
INPUT:
- ``i`` - integer (optional, default 0)
EXAMPLES::
sage: Q.<ii,jj,kk> = QuaternionAlgebra(QQ,-1,-2); Q
Quaternion Algebra (-1, -2) with base ring Rational Field
sage: Q.gen(0)
ii
sage: Q.gen(1)
jj
sage: Q.gen(2)
kk
sage: Q.gens()
[ii, jj, kk]
"""
return self._gens[i]
def _repr_(self):
"""
Print representation.
TESTS::
sage: Q.<i,j,k> = QuaternionAlgebra(QQ,-5,-2)
sage: type(Q)
<class 'sage.algebras.quatalg.quaternion_algebra.QuaternionAlgebra_ab_with_category'>
sage: Q._repr_()
'Quaternion Algebra (-5, -2) with base ring Rational Field'
sage: Q
Quaternion Algebra (-5, -2) with base ring Rational Field
sage: print(Q)
Quaternion Algebra (-5, -2) with base ring Rational Field
sage: str(Q)
'Quaternion Algebra (-5, -2) with base ring Rational Field'
"""
return "Quaternion Algebra (%r, %r) with base ring %s" % (self._a, self._b, self.base_ring())
def inner_product_matrix(self):
"""
Return the inner product matrix associated to ``self``, i.e. the
Gram matrix of the reduced norm as a quadratic form on ``self``.
The standard basis `1`, `i`, `j`, `k` is orthogonal, so this matrix
is just the diagonal matrix with diagonal entries `1`, `a`, `b`, `ab`.
EXAMPLES::
sage: Q.<i,j,k> = QuaternionAlgebra(-5,-19)
sage: Q.inner_product_matrix()
[ 2 0 0 0]
[ 0 10 0 0]
[ 0 0 38 0]
[ 0 0 0 190]
sage: R.<a,b> = QQ[]; Q.<i,j,k> = QuaternionAlgebra(Frac(R),a,b)
sage: Q.inner_product_matrix()
[ 2 0 0 0]
[ 0 -2*a 0 0]
[ 0 0 -2*b 0]
[ 0 0 0 2*a*b]
"""
a, b = self._a, self._b
return diagonal_matrix(self.base_ring(), [2, -2*a, -2*b, 2*a*b])
@cached_method
def discriminant(self):
"""
Given a quaternion algebra `A` defined over a number field,
return the discriminant of `A`, i.e. the
product of the ramified primes of `A`.
EXAMPLES::
sage: QuaternionAlgebra(210,-22).discriminant()
210
sage: QuaternionAlgebra(19).discriminant()
19
sage: F.<a> = NumberField(x^2-x-1)
sage: B.<i,j,k> = QuaternionAlgebra(F, 2*a,F(-1))
sage: B.discriminant()
Fractional ideal (2)
sage: QuaternionAlgebra(QQ[sqrt(2)],3,19).discriminant()
Fractional ideal (1)