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
/
number_field.py
11135 lines (9008 loc) · 394 KB
/
number_field.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
# -*- coding: utf-8 -*-
r"""
Number Fields
AUTHORS:
- William Stein (2004, 2005): initial version
- Steven Sivek (2006-05-12): added support for relative extensions
- William Stein (2007-09-04): major rewrite and documentation
- Robert Bradshaw (2008-10): specified embeddings into ambient fields
- Simon King (2010-05): Improve coercion from GAP
- Jeroen Demeyer (2010-07, 2011-04): Upgrade PARI (:trac:`9343`, :trac:`10430`, :trac:`11130`)
- Robert Harron (2012-08): added is_CM(), complex_conjugation(), and
maximal_totally_real_subfield()
- Christian Stump (2012-11): added conversion to universal cyclotomic field
- Julian Rueth (2014-04-03): absolute number fields are unique parents
- Vincent Delecroix (2015-02): comparisons/floor/ceil using embeddings
- Kiran Kedlaya (2016-05): relative number fields hash based on relative polynomials
- Peter Bruin (2016-06): make number fields fully satisfy unique representation
- John Jones (2017-07): improve check for is_galois(), add is_abelian(), building on work in patch by Chris Wuthrich
.. note::
Unlike in PARI/GP, class group computations *in Sage* do *not* by default
assume the Generalized Riemann Hypothesis. To do class groups computations
not provably correctly you must often pass the flag ``proof=False`` to
functions or call the function ``proof.number_field(False)``. It can easily
take 1000's of times longer to do computations with ``proof=True`` (the
default).
This example follows one in the Magma reference manual::
sage: K.<y> = NumberField(x^4 - 420*x^2 + 40000)
sage: z = y^5/11; z
420/11*y^3 - 40000/11*y
sage: R.<y> = PolynomialRing(K)
sage: f = y^2 + y + 1
sage: L.<a> = K.extension(f); L
Number Field in a with defining polynomial y^2 + y + 1 over its base field
sage: KL.<b> = NumberField([x^4 - 420*x^2 + 40000, x^2 + x + 1]); KL
Number Field in b0 with defining polynomial x^4 - 420*x^2 + 40000 over its base field
We do some arithmetic in a tower of relative number fields::
sage: K.<cuberoot2> = NumberField(x^3 - 2)
sage: L.<cuberoot3> = K.extension(x^3 - 3)
sage: S.<sqrt2> = L.extension(x^2 - 2)
sage: S
Number Field in sqrt2 with defining polynomial x^2 - 2 over its base field
sage: sqrt2 * cuberoot3
cuberoot3*sqrt2
sage: (sqrt2 + cuberoot3)^5
(20*cuberoot3^2 + 15*cuberoot3 + 4)*sqrt2 + 3*cuberoot3^2 + 20*cuberoot3 + 60
sage: cuberoot2 + cuberoot3
cuberoot3 + cuberoot2
sage: cuberoot2 + cuberoot3 + sqrt2
sqrt2 + cuberoot3 + cuberoot2
sage: (cuberoot2 + cuberoot3 + sqrt2)^2
(2*cuberoot3 + 2*cuberoot2)*sqrt2 + cuberoot3^2 + 2*cuberoot2*cuberoot3 + cuberoot2^2 + 2
sage: cuberoot2 + sqrt2
sqrt2 + cuberoot2
sage: a = S(cuberoot2); a
cuberoot2
sage: a.parent()
Number Field in sqrt2 with defining polynomial x^2 - 2 over its base field
.. warning::
Doing arithmetic in towers of relative fields that depends on
canonical coercions is currently VERY SLOW. It is much better to
explicitly coerce all elements into a common field, then do
arithmetic with them there (which is quite fast).
"""
#*****************************************************************************
# Copyright (C) 2004, 2005, 2006, 2007 William Stein <wstein@gmail.com>
# 2014 Julian Rueth <julian.rueth@fsfe.org>
#
# Distributed under the terms of the GNU General Public License (GPL)
# as published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************
from __future__ import absolute_import, print_function
from six.moves import range
from six import integer_types
from sage.structure.parent_gens import localvars
from sage.misc.cachefunc import cached_method
import sage.libs.ntl.all as ntl
import sage.interfaces.gap
import sage.rings.complex_field
from sage.rings.polynomial.polynomial_element import is_Polynomial
import sage.rings.real_mpfr
import sage.rings.real_mpfi
import sage.rings.complex_double
import sage.rings.real_double
import sage.rings.real_lazy
from sage.rings.finite_rings.integer_mod import mod
from sage.misc.fast_methods import WithEqualityById
from sage.misc.functional import is_odd, lift
from sage.misc.misc_c import prod
from sage.categories.homset import End
from sage.rings.all import Infinity
import sage.rings.ring
from sage.misc.latex import latex_variable_name
from sage.misc.misc import union
from .unit_group import UnitGroup
from .class_group import ClassGroup
from .class_group import SClassGroup
from sage.structure.element import is_Element
from sage.structure.sequence import Sequence
from sage.structure.category_object import normalize_names
import sage.structure.parent_gens
from sage.structure.proof.proof import get_flag
from . import maps
from . import structure
from . import number_field_morphisms
from itertools import count
from builtins import zip
def is_NumberFieldHomsetCodomain(codomain):
"""
Returns whether ``codomain`` is a valid codomain for a number
field homset. This is used by NumberField._Hom_ to determine
whether the created homsets should be a
:class:`sage.rings.number_field.morphism.NumberFieldHomset`.
EXAMPLES:
This currently accepts any parent (CC, RR, ...) in :class:`Fields`::
sage: from sage.rings.number_field.number_field import is_NumberFieldHomsetCodomain
sage: is_NumberFieldHomsetCodomain(QQ)
True
sage: is_NumberFieldHomsetCodomain(NumberField(x^2 + 1, 'x'))
True
sage: is_NumberFieldHomsetCodomain(ZZ)
False
sage: is_NumberFieldHomsetCodomain(3)
False
sage: is_NumberFieldHomsetCodomain(MatrixSpace(QQ, 2))
False
sage: is_NumberFieldHomsetCodomain(InfinityRing)
False
Question: should, for example, QQ-algebras be accepted as well?
Caveat: Gap objects are not (yet) in :class:`Fields`, and therefore
not accepted as number field homset codomains::
sage: is_NumberFieldHomsetCodomain(gap.Rationals)
False
"""
from sage.categories.fields import Fields
return codomain in Fields()
from sage.rings.number_field.morphism import RelativeNumberFieldHomomorphism_from_abs
def proof_flag(t):
"""
Used for easily determining the correct proof flag to use.
Returns t if t is not None, otherwise returns the system-wide
proof-flag for number fields (default: True).
EXAMPLES::
sage: from sage.rings.number_field.number_field import proof_flag
sage: proof_flag(True)
True
sage: proof_flag(False)
False
sage: proof_flag(None)
True
sage: proof_flag("banana")
'banana'
"""
return get_flag(t, "number_field")
import weakref
from sage.misc.latex import latex
import sage.arith.all as arith
import sage.rings.rational_field as rational_field
import sage.rings.integer_ring as integer_ring
import sage.rings.infinity as infinity
from sage.rings.rational import Rational
from sage.rings.integer import Integer
import sage.rings.polynomial.polynomial_element as polynomial_element
import sage.rings.complex_field
import sage.groups.abelian_gps.abelian_group
import sage.rings.complex_interval_field
from sage.structure.parent_gens import ParentWithGens
from sage.structure.factory import UniqueFactory
from . import number_field_element
from . import number_field_element_quadratic
from .number_field_ideal import is_NumberFieldIdeal, NumberFieldFractionalIdeal
from sage.libs.pari.all import pari, pari_gen
from sage.rings.rational_field import QQ
from sage.rings.integer_ring import ZZ
RIF = sage.rings.real_mpfi.RealIntervalField()
CIF = sage.rings.complex_interval_field.ComplexIntervalField()
from sage.rings.real_double import RDF
from sage.rings.complex_double import CDF
from sage.rings.real_lazy import RLF, CLF
def NumberField(polynomial, name=None, check=True, names=None, embedding=None, latex_name=None, assume_disc_small=False, maximize_at_primes=None, structure=None):
r"""
Return *the* number field (or tower of number fields) defined by the
irreducible ``polynomial``.
INPUT:
- ``polynomial`` - a polynomial over `\QQ` or a number field, or a list
of such polynomials.
- ``name`` - a string or a list of strings, the names of the generators
- ``check`` - a boolean (default: ``True``); do type checking and
irreducibility checking.
- ``embedding`` - ``None``, an element, or a list of elements, the
images of the generators in an ambient field (default: ``None``)
- ``latex_name`` - ``None``, a string, or a list of strings (default:
``None``), how the generators are printed for latex output
- ``assume_disc_small`` -- a boolean (default: ``False``); if ``True``,
assume that no square of a prime greater than PARI's primelimit
(which should be 500000); only applies for absolute fields at
present.
- ``maximize_at_primes`` -- ``None`` or a list of primes (default:
``None``); if not ``None``, then the maximal order is computed by
maximizing only at the primes in this list, which completely avoids
having to factor the discriminant, but of course can lead to wrong
results; only applies for absolute fields at present.
- ``structure`` -- ``None``, a list or an instance of
:class:`structure.NumberFieldStructure` (default: ``None``),
internally used to pass in additional structural information, e.g.,
about the field from which this field is created as a subfield.
EXAMPLES::
sage: z = QQ['z'].0
sage: K = NumberField(z^2 - 2,'s'); K
Number Field in s with defining polynomial z^2 - 2
sage: s = K.0; s
s
sage: s*s
2
sage: s^2
2
Constructing a relative number field::
sage: K.<a> = NumberField(x^2 - 2)
sage: R.<t> = K[]
sage: L.<b> = K.extension(t^3+t+a); L
Number Field in b with defining polynomial t^3 + t + a over its base field
sage: L.absolute_field('c')
Number Field in c with defining polynomial x^6 + 2*x^4 + x^2 - 2
sage: a*b
a*b
sage: L(a)
a
sage: L.lift_to_base(b^3 + b)
-a
Constructing another number field::
sage: k.<i> = NumberField(x^2 + 1)
sage: R.<z> = k[]
sage: m.<j> = NumberField(z^3 + i*z + 3)
sage: m
Number Field in j with defining polynomial z^3 + i*z + 3 over its base field
Number fields are globally unique::
sage: K.<a> = NumberField(x^3 - 5)
sage: a^3
5
sage: L.<a> = NumberField(x^3 - 5)
sage: K is L
True
Equality of number fields depends on the variable name of the
defining polynomial::
sage: x = polygen(QQ, 'x'); y = polygen(QQ, 'y')
sage: k.<a> = NumberField(x^2 + 3)
sage: m.<a> = NumberField(y^2 + 3)
sage: k
Number Field in a with defining polynomial x^2 + 3
sage: m
Number Field in a with defining polynomial y^2 + 3
sage: k == m
False
In case of conflict of the generator name with the name given by the preparser, the name given by the preparser takes precedence::
sage: K.<b> = NumberField(x^2 + 5, 'a'); K
Number Field in b with defining polynomial x^2 + 5
One can also define number fields with specified embeddings, may be used
for arithmetic and deduce relations with other number fields which would
not be valid for an abstract number field. ::
sage: K.<a> = NumberField(x^3-2, embedding=1.2)
sage: RR.coerce_map_from(K)
Composite map:
From: Number Field in a with defining polynomial x^3 - 2
To: Real Field with 53 bits of precision
Defn: Generic morphism:
From: Number Field in a with defining polynomial x^3 - 2
To: Real Lazy Field
Defn: a -> 1.259921049894873?
then
Conversion via _mpfr_ method map:
From: Real Lazy Field
To: Real Field with 53 bits of precision
sage: RR(a)
1.25992104989487
sage: 1.1 + a
2.35992104989487
sage: b = 1/(a+1); b
1/3*a^2 - 1/3*a + 1/3
sage: RR(b)
0.442493334024442
sage: L.<b> = NumberField(x^6-2, embedding=1.1)
sage: L(a)
b^2
sage: a + b
b^2 + b
Note that the image only needs to be specified to enough precision
to distinguish roots, and is exactly computed to any needed
precision::
sage: RealField(200)(a)
1.2599210498948731647672106072782283505702514647015079800820
One can embed into any other field::
sage: K.<a> = NumberField(x^3-2, embedding=CC.gen()-0.6)
sage: CC(a)
-0.629960524947436 + 1.09112363597172*I
sage: L = Qp(5)
sage: f = polygen(L)^3 - 2
sage: K.<a> = NumberField(x^3-2, embedding=f.roots()[0][0])
sage: a + L(1)
4 + 2*5^2 + 2*5^3 + 3*5^4 + 5^5 + 4*5^6 + 2*5^8 + 3*5^9 + 4*5^12 + 4*5^14 + 4*5^15 + 3*5^16 + 5^17 + 5^18 + 2*5^19 + O(5^20)
sage: L.<b> = NumberField(x^6-x^2+1/10, embedding=1)
sage: K.<a> = NumberField(x^3-x+1/10, embedding=b^2)
sage: a+b
b^2 + b
sage: CC(a) == CC(b)^2
True
sage: K.coerce_embedding()
Generic morphism:
From: Number Field in a with defining polynomial x^3 - x + 1/10
To: Number Field in b with defining polynomial x^6 - x^2 + 1/10
Defn: a -> b^2
The ``QuadraticField`` and ``CyclotomicField`` constructors
create an embedding by default unless otherwise specified::
sage: K.<zeta> = CyclotomicField(15)
sage: CC(zeta)
0.913545457642601 + 0.406736643075800*I
sage: L.<sqrtn3> = QuadraticField(-3)
sage: K(sqrtn3)
2*zeta^5 + 1
sage: sqrtn3 + zeta
2*zeta^5 + zeta + 1
Comparison depends on the (real) embedding specified (or the one selected by default).
Note that the codomain of the embedding must be `QQbar` or `AA` for this to work
(see :trac:`20184`)::
sage: N.<g> = NumberField(x^3+2,embedding=1)
sage: 1 < g
False
sage: g > 1
False
sage: RR(g)
-1.25992104989487
If no embedding is specified or is complex, the comparison is not returning something
meaningful.::
sage: N.<g> = NumberField(x^3+2)
sage: 1 < g
False
sage: g > 1
True
Since SageMath 6.9, number fields may be defined by polynomials
that are not necessarily integral or monic. The only notable
practical point is that in the PARI interface, a monic integral
polynomial defining the same number field is computed and used::
sage: K.<a> = NumberField(2*x^3 + x + 1)
sage: K.pari_polynomial()
x^3 - x^2 - 2
Elements and ideals may be converted to and from PARI as follows::
sage: pari(a)
Mod(-1/2*y^2 + 1/2*y, y^3 - y^2 - 2)
sage: K(pari(a))
a
sage: I = K.ideal(a); I
Fractional ideal (a)
sage: I.pari_hnf()
[1, 0, 0; 0, 1, 0; 0, 0, 1/2]
sage: K.ideal(I.pari_hnf())
Fractional ideal (a)
Here is an example where the field has non-trivial class group::
sage: L.<b> = NumberField(3*x^2 - 1/5)
sage: L.pari_polynomial()
x^2 - 15
sage: J = L.primes_above(2)[0]; J
Fractional ideal (2, 15*b + 1)
sage: J.pari_hnf()
[2, 1; 0, 1]
sage: L.ideal(J.pari_hnf())
Fractional ideal (2, 15*b + 1)
An example involving a variable name that defines a function in
PARI::
sage: theta = polygen(QQ, 'theta')
sage: M.<z> = NumberField([theta^3 + 4, theta^2 + 3]); M
Number Field in z0 with defining polynomial theta^3 + 4 over its base field
TESTS::
sage: x = QQ['x'].gen()
sage: y = ZZ['y'].gen()
sage: K = NumberField(x^3 + x + 3, 'a'); K
Number Field in a with defining polynomial x^3 + x + 3
sage: K.defining_polynomial().parent()
Univariate Polynomial Ring in x over Rational Field
::
sage: L = NumberField(y^3 + y + 3, 'a'); L
Number Field in a with defining polynomial y^3 + y + 3
sage: L.defining_polynomial().parent()
Univariate Polynomial Ring in y over Rational Field
::
sage: W1 = NumberField(x^2+1,'a')
sage: K.<x> = CyclotomicField(5)[]
sage: W.<a> = NumberField(x^2 + 1); W
Number Field in a with defining polynomial x^2 + 1 over its base field
The following has been fixed in :trac:`8800`::
sage: P.<x> = QQ[]
sage: K.<a> = NumberField(x^3-5,embedding=0)
sage: L.<b> = K.extension(x^2+a)
sage: F, R = L.construction()
sage: F(R) == L # indirect doctest
True
Check that :trac:`11670` has been fixed::
sage: K.<a> = NumberField(x^2 - x - 1)
sage: loads(dumps(K)) is K
True
sage: K.<a> = NumberField(x^3 - x - 1)
sage: loads(dumps(K)) is K
True
sage: K.<a> = CyclotomicField(7)
sage: loads(dumps(K)) is K
True
Another problem that was found while working on :trac:`11670`,
``maximize_at_primes`` and ``assume_disc_small`` were lost when pickling::
sage: K.<a> = NumberField(x^3-2, assume_disc_small=True, maximize_at_primes=[2], latex_name='\\alpha', embedding=2^(1/3))
sage: L = loads(dumps(K))
sage: L._assume_disc_small
True
sage: L._maximize_at_primes
(2,)
It is an error not to specify the generator::
sage: K = NumberField(x^2-2)
Traceback (most recent call last):
...
TypeError: You must specify the name of the generator.
"""
if names is not None:
name = names
if isinstance(polynomial, (list,tuple)):
return NumberFieldTower(polynomial, names=name, check=check, embeddings=embedding, latex_names=latex_name, assume_disc_small=assume_disc_small, maximize_at_primes=maximize_at_primes, structures=structure)
return NumberField_version2(polynomial=polynomial, name=name, check=check, embedding=embedding, latex_name=latex_name, assume_disc_small=assume_disc_small, maximize_at_primes=maximize_at_primes, structure=structure)
class NumberFieldFactory(UniqueFactory):
r"""
Factory for number fields.
This should usually not be called directly, use :meth:`NumberField`
instead.
INPUT:
- ``polynomial`` - a polynomial over `\QQ` or a number field.
- ``name`` - a string (default: ``'a'``), the name of the generator
- ``check`` - a boolean (default: ``True``); do type checking and
irreducibility checking.
- ``embedding`` - ``None`` or an element, the images of the generator
in an ambient field (default: ``None``)
- ``latex_name`` - ``None`` or a string (default: ``None``), how the
generator is printed for latex output
- ``assume_disc_small`` -- a boolean (default: ``False``); if ``True``,
assume that no square of a prime greater than PARI's primelimit
(which should be 500000); only applies for absolute fields at
present.
- ``maximize_at_primes`` -- ``None`` or a list of primes (default:
``None``); if not ``None``, then the maximal order is computed by
maximizing only at the primes in this list, which completely avoids
having to factor the discriminant, but of course can lead to wrong
results; only applies for absolute fields at present.
- ``structure`` -- ``None`` or an instance of
:class:`structure.NumberFieldStructure` (default: ``None``),
internally used to pass in additional structural information, e.g.,
about the field from which this field is created as a subfield.
TESTS::
sage: from sage.rings.number_field.number_field import NumberFieldFactory
sage: nff = NumberFieldFactory("number_field_factory")
sage: R.<x> = QQ[]
sage: nff(x^2 + 1, name='a', check=False, embedding=None, latex_name=None, assume_disc_small=False, maximize_at_primes=None, structure=None)
Number Field in a with defining polynomial x^2 + 1
Pickling preserves the ``structure()`` of a number field::
sage: K.<a> = QuadraticField(2)
sage: L.<b> = K.change_names()
sage: M = loads(dumps(L))
sage: M.structure()
(Isomorphism given by variable name change map:
From: Number Field in b with defining polynomial x^2 - 2
To: Number Field in a with defining polynomial x^2 - 2,
Isomorphism given by variable name change map:
From: Number Field in a with defining polynomial x^2 - 2
To: Number Field in b with defining polynomial x^2 - 2)
"""
def create_key_and_extra_args(self, polynomial, name, check, embedding, latex_name, assume_disc_small, maximize_at_primes, structure):
r"""
Create a unique key for the number field specified by the parameters.
TESTS::
sage: from sage.rings.number_field.number_field import NumberFieldFactory
sage: nff = NumberFieldFactory("number_field_factory")
sage: R.<x> = QQ[]
sage: nff.create_key_and_extra_args(x^2+1, name='a', check=False, embedding=None, latex_name=None, assume_disc_small=False, maximize_at_primes=None, structure=None)
((Rational Field, x^2 + 1, ('a',), None, 'a', None, False, None),
{'check': False})
"""
if name is None:
raise TypeError("You must specify the name of the generator.")
name = normalize_names(1, name)
if not is_Polynomial(polynomial):
try:
polynomial = polynomial.polynomial(QQ)
except (AttributeError, TypeError):
raise TypeError("polynomial (=%s) must be a polynomial." % polynomial)
# convert polynomial to a polynomial over a field
polynomial = polynomial.change_ring(polynomial.base_ring().fraction_field())
# normalize embedding
if isinstance(embedding, (list,tuple)):
if len(embedding) != 1:
raise TypeError("embedding must be a list of length 1")
embedding = embedding[0]
if embedding is not None:
x = number_field_morphisms.root_from_approx(polynomial, embedding)
embedding = (x.parent(), x)
# normalize latex_name
if isinstance(latex_name, (list, tuple)):
if len(latex_name) != 1:
raise TypeError("latex_name must be a list of length 1")
latex_name = latex_name[0]
if latex_name is None:
latex_name = latex_variable_name(name[0])
if maximize_at_primes is not None:
maximize_at_primes = tuple(maximize_at_primes)
# normalize structure
if isinstance(structure, (list, tuple)):
if len(structure) != 1:
raise TypeError("structure must be a list of length 1")
structure = structure[0]
return (polynomial.base_ring(), polynomial, name, embedding, latex_name, maximize_at_primes, assume_disc_small, structure), {"check":check}
def create_object(self, version, key, check):
r"""
Create the unique number field defined by ``key``.
TESTS::
sage: from sage.rings.number_field.number_field import NumberFieldFactory
sage: nff = NumberFieldFactory("number_field_factory")
sage: R.<x> = QQ[]
sage: nff.create_object(None, (QQ, x^2 + 1, ('a',), None, None, None, False, None), check=False)
Number Field in a with defining polynomial x^2 + 1
"""
base, polynomial, name, embedding, latex_name, maximize_at_primes, assume_disc_small, structure = key
if isinstance(base, NumberField_generic):
from sage.rings.number_field.number_field_rel import NumberField_relative
# Relative number fields do not support embeddings.
return NumberField_relative(base, polynomial, name[0], latex_name,
check=check, embedding=None,
structure=structure)
if polynomial.degree() == 2:
return NumberField_quadratic(polynomial, name, latex_name, check, embedding, assume_disc_small=assume_disc_small, maximize_at_primes=maximize_at_primes, structure=structure)
else:
return NumberField_absolute(polynomial, name, latex_name, check, embedding, assume_disc_small=assume_disc_small, maximize_at_primes=maximize_at_primes, structure=structure)
NumberField_version2 = NumberFieldFactory("sage.rings.number_field.number_field.NumberField_version2")
def NumberFieldTower(polynomials, names, check=True, embeddings=None, latex_names=None, assume_disc_small=False, maximize_at_primes=None, structures=None):
"""
Create the tower of number fields defined by the polynomials in the list
``polynomials``.
INPUT:
- ``polynomials`` - a list of polynomials. Each entry must be polynomial
which is irreducible over the number field generated by the roots of the
following entries.
- ``names`` - a list of strings or a string, the names of the generators of
the relative number fields. If a single string, then names are generated
from that string.
- ``check`` - a boolean (default: ``True``), whether to check that the
polynomials are irreducible
- ``embeddings`` - a list of elements or ``None`` (default: ``None``),
embeddings of the relative number fields in an ambient field.
- ``latex_names`` - a list of strings or ``None`` (default: ``None``), names
used to print the generators for latex output.
- ``assume_disc_small`` -- a boolean (default: ``False``); if ``True``,
assume that no square of a prime greater than PARI's primelimit
(which should be 500000); only applies for absolute fields at
present.
- ``maximize_at_primes`` -- ``None`` or a list of primes (default:
``None``); if not ``None``, then the maximal order is computed by
maximizing only at the primes in this list, which completely avoids
having to factor the discriminant, but of course can lead to wrong
results; only applies for absolute fields at present.
- ``structures`` -- ``None`` or a list (default: ``None``), internally used
to provide additional information about the number field such as the
field from which it was created.
OUTPUT:
Returns the relative number field generated by a root of the first entry of
``polynomials`` over the relative number field generated by root of the
second entry of ``polynomials`` ... over the number field over which the
last entry of ``polynomials`` is defined.
EXAMPLES::
sage: k.<a,b,c> = NumberField([x^2 + 1, x^2 + 3, x^2 + 5]); k # indirect doctest
Number Field in a with defining polynomial x^2 + 1 over its base field
sage: a^2
-1
sage: b^2
-3
sage: c^2
-5
sage: (a+b+c)^2
(2*b + 2*c)*a + 2*c*b - 9
The Galois group is a product of 3 groups of order 2::
sage: k.galois_group(type="pari")
Galois group PARI group [8, 1, 3, "E(8)=2[x]2[x]2"] of degree 8 of the Number Field in a with defining polynomial x^2 + 1 over its base field
Repeatedly calling base_field allows us to descend the internally
constructed tower of fields::
sage: k.base_field()
Number Field in b with defining polynomial x^2 + 3 over its base field
sage: k.base_field().base_field()
Number Field in c with defining polynomial x^2 + 5
sage: k.base_field().base_field().base_field()
Rational Field
In the following example the second polynomial is reducible over
the first, so we get an error::
sage: v = NumberField([x^3 - 2, x^3 - 2], names='a')
Traceback (most recent call last):
...
ValueError: defining polynomial (x^3 - 2) must be irreducible
We mix polynomial parent rings::
sage: k.<y> = QQ[]
sage: m = NumberField([y^3 - 3, x^2 + x + 1, y^3 + 2], 'beta')
sage: m
Number Field in beta0 with defining polynomial y^3 - 3 over its base field
sage: m.base_field ()
Number Field in beta1 with defining polynomial x^2 + x + 1 over its base field
A tower of quadratic fields::
sage: K.<a> = NumberField([x^2 + 3, x^2 + 2, x^2 + 1])
sage: K
Number Field in a0 with defining polynomial x^2 + 3 over its base field
sage: K.base_field()
Number Field in a1 with defining polynomial x^2 + 2 over its base field
sage: K.base_field().base_field()
Number Field in a2 with defining polynomial x^2 + 1
A bigger tower of quadratic fields::
sage: K.<a2,a3,a5,a7> = NumberField([x^2 + p for p in [2,3,5,7]]); K
Number Field in a2 with defining polynomial x^2 + 2 over its base field
sage: a2^2
-2
sage: a3^2
-3
sage: (a2+a3+a5+a7)^3
((6*a5 + 6*a7)*a3 + 6*a7*a5 - 47)*a2 + (6*a7*a5 - 45)*a3 - 41*a5 - 37*a7
The function can also be called by name::
sage: NumberFieldTower([x^2 + 1, x^2 + 2], ['a','b'])
Number Field in a with defining polynomial x^2 + 1 over its base field
"""
try:
names = normalize_names(len(polynomials), names)
except IndexError:
names = normalize_names(1, names)
if len(polynomials) > 1:
names = ['%s%s'%(names[0], i) for i in range(len(polynomials))]
if embeddings is None:
embeddings = [None] * len(polynomials)
if latex_names is None:
latex_names = [None] * len(polynomials)
if structures is None:
structures = [None] * len(polynomials)
if not isinstance(polynomials, (list, tuple)):
raise TypeError("polynomials must be a list or tuple")
if len(polynomials) == 0:
return QQ
if len(polynomials) == 1:
return NumberField(polynomials[0], names=names, check=check, embedding=embeddings[0], latex_name=latex_names[0], assume_disc_small=assume_disc_small, maximize_at_primes=maximize_at_primes, structure=structures[0])
# create the relative number field defined by f over the tower defined by polynomials[1:]
f = polynomials[0]
name = names[0]
w = NumberFieldTower(polynomials[1:], names=names[1:], check=check, embeddings=embeddings[1:], latex_names=latex_names[1:], assume_disc_small=assume_disc_small, maximize_at_primes=maximize_at_primes, structures=structures[1:])
var = f.variable_name() if is_Polynomial(f) else 'x'
R = w[var] # polynomial ring
return w.extension(R(f), name, check=check, embedding=embeddings[0], structure=structures[0]) # currently, extension does not accept assume_disc_small, or maximize_at_primes
def QuadraticField(D, name='a', check=True, embedding=True, latex_name='sqrt', **args):
r"""
Return a quadratic field obtained by adjoining a square root of
`D` to the rational numbers, where `D` is not a
perfect square.
INPUT:
- ``D`` - a rational number
- ``name`` - variable name (default: 'a')
- ``check`` - bool (default: True)
- ``embedding`` - bool or square root of D in an
ambient field (default: True)
- ``latex_name`` - latex variable name (default: \sqrt{D})
OUTPUT: A number field defined by a quadratic polynomial. Unless
otherwise specified, it has an embedding into `\RR` or
`\CC` by sending the generator to the positive
or upper-half-plane root.
EXAMPLES::
sage: QuadraticField(3, 'a')
Number Field in a with defining polynomial x^2 - 3
sage: K.<theta> = QuadraticField(3); K
Number Field in theta with defining polynomial x^2 - 3
sage: RR(theta)
1.73205080756888
sage: QuadraticField(9, 'a')
Traceback (most recent call last):
...
ValueError: D must not be a perfect square.
sage: QuadraticField(9, 'a', check=False)
Number Field in a with defining polynomial x^2 - 9
Quadratic number fields derive from general number fields.
::
sage: from sage.rings.number_field.number_field import is_NumberField
sage: type(K)
<class 'sage.rings.number_field.number_field.NumberField_quadratic_with_category'>
sage: is_NumberField(K)
True
Quadratic number fields are cached::
sage: QuadraticField(-11, 'a') is QuadraticField(-11, 'a')
True
By default, quadratic fields come with a nice latex representation::
sage: K.<a> = QuadraticField(-7)
sage: latex(K)
\Bold{Q}(\sqrt{-7})
sage: latex(a)
\sqrt{-7}
sage: latex(1/(1+a))
-\frac{1}{8} \sqrt{-7} + \frac{1}{8}
sage: K.latex_variable_name()
'\\sqrt{-7}'
We can provide our own name as well::
sage: K.<a> = QuadraticField(next_prime(10^10), latex_name=r'\sqrt{D}')
sage: 1+a
a + 1
sage: latex(1+a)
\sqrt{D} + 1
sage: latex(QuadraticField(-1, 'a', latex_name=None).gen())
a
The name of the generator does not interfere with Sage preparser, see :trac:`1135`::
sage: K1 = QuadraticField(5, 'x')
sage: K2.<x> = QuadraticField(5)
sage: K3.<x> = QuadraticField(5, 'x')
sage: K1 is K2
True
sage: K1 is K3
True
sage: K1
Number Field in x with defining polynomial x^2 - 5
Note that, in presence of two different names for the generator,
the name given by the preparser takes precedence::
sage: K4.<y> = QuadraticField(5, 'x'); K4
Number Field in y with defining polynomial x^2 - 5
sage: K1 == K4
False
TESTS::
sage: QuadraticField(-11, 'a') is QuadraticField(-11, 'a', latex_name='Z')
False
sage: QuadraticField(-11, 'a') is QuadraticField(-11, 'a', latex_name=None)
False
"""
D = QQ(D)
if check:
if D.is_square():
raise ValueError("D must not be a perfect square.")
R = QQ['x']
f = R([-D, 0, 1])
if embedding is True:
if D > 0:
embedding = RLF(D).sqrt()
else:
embedding = CLF(D).sqrt()
if latex_name == 'sqrt':
latex_name = r'\sqrt{%s}' % D
return NumberField(f, name, check=False, embedding=embedding, latex_name=latex_name, **args)
def is_AbsoluteNumberField(x):
"""
Return True if x is an absolute number field.
EXAMPLES::
sage: from sage.rings.number_field.number_field import is_AbsoluteNumberField
sage: is_AbsoluteNumberField(NumberField(x^2+1,'a'))
True
sage: is_AbsoluteNumberField(NumberField([x^3 + 17, x^2+1],'a'))
False
The rationals are a number field, but they're not of the absolute
number field class.
::
sage: is_AbsoluteNumberField(QQ)
False
"""
return isinstance(x, NumberField_absolute)
def is_QuadraticField(x):
r"""
Return True if x is of the quadratic *number* field type.
EXAMPLES::
sage: from sage.rings.number_field.number_field import is_QuadraticField
sage: is_QuadraticField(QuadraticField(5,'a'))
True
sage: is_QuadraticField(NumberField(x^2 - 5, 'b'))
True
sage: is_QuadraticField(NumberField(x^3 - 5, 'b'))
False
A quadratic field specially refers to a number field, not a finite
field::
sage: is_QuadraticField(GF(9,'a'))
False
"""
return isinstance(x, NumberField_quadratic)
class CyclotomicFieldFactory(UniqueFactory):
r"""
Return the `n`-th cyclotomic field, where n is a positive integer,
or the universal cyclotomic field if ``n==0``.
For the documentation of the universal cyclotomic field, see
:class:`~sage.rings.universal_cyclotomic_field.UniversalCyclotomicField`.
INPUT:
- ``n`` - a nonnegative integer, default:``0``
- ``names`` - name of generator (optional - defaults to zetan)
- ``bracket`` - Defines the brackets in the case of ``n==0``, and
is ignored otherwise. Can be any even length string, with ``"()"`` being the default.
- ``embedding`` - bool or n-th root of unity in an
ambient field (default True)
EXAMPLES:
If called without a parameter, we get the :class:`universal cyclotomic
field<sage.rings.universal_cyclotomic_field.UniversalCyclotomicField>`::
sage: CyclotomicField()
Universal Cyclotomic Field