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
/
universal_cyclotomic_field.py
2105 lines (1579 loc) · 65.3 KB
/
universal_cyclotomic_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
r"""
The universal cyclotomic field (UCF)
Implementation of the universal cyclotomic field using the :meth:`Zumbroich basis<UniversalCyclotomicField.zumbroich_basis_indices>`.
The universal cyclotomic field is the smallest subfield of the complex field
containing all roots of unity.
REFERENCES:
.. [Bre97] T. Breuer "Integral bases for subfields of cyclotomic fields" AAECC 8, 279--289 (1997).
AUTHORS:
- Christian Stump
.. NOTE::
- This function behaves exactly like the *Cyclotomics* in *GAP*.
- The universal cyclotomic field is used to work with non-crystallographic
reflection groups. E.g., to work with elements as matrices, computing
*reflecting hyperplanes*, and *characters*.
- To multiply matrices over the universal cyclotomic field, it is still
*much* faster to coerce it to a cyclotomic field and to the
computation there.
.. TODO::
- implementation of matrices over the universal cyclotomic field.
- speed improvements of the cythonized methods.
- speed improvements for scalar multiples.
- Remove the inheritance from Field and FieldElement as soon as
the methods ``is_field(proof=True)`` is implemented in the Fields category.
EXAMPLES:
The universal cyclotomic field is constructed using::
sage: UCF = UniversalCyclotomicField(); UCF
Universal Cyclotomic Field
One can as well construct it through :func:`~sage.ring.number_field.number_field.CyclotomicField`::
sage: UCF = CyclotomicField(); UCF
Universal Cyclotomic Field
The cyclotomics themselves are accessable through::
sage: UCF.gen(5)
E(5)
sage: UCF.gen(5,2)
E(5)^2
or the alias::
sage: UCF.gen(5)
E(5)
sage: UCF.gen(5,2)
E(5)^2
One can as well access the universal cyclotomic field using::
sage: UCF.<E> = UniversalCyclotomicField();
sage: E(5)
E(5)
Other names are supported as well::
sage: UCF.<zeta> = UniversalCyclotomicField();
sage: zeta(5)
zeta(5)
As are other bracketings::
sage: UCF.<E> = UniversalCyclotomicField(bracket='');
sage: E(5)
E5
sage: UCF.<E> = UniversalCyclotomicField(bracket="[]");
sage: E(5)
E[5]
sage: UCF.<E> = UniversalCyclotomicField(bracket="(ABCXYZ)");
sage: E(5)
E(ABC5XYZ)
We use the generator "E" and the standard bracketing throughout this file::
sage: UCF.<E> = UniversalCyclotomicField();
Some very first examples::
sage: E(2)
-1
sage: E(3)
E(3)
sage: E(6)
-E(3)^2
Equality and inequality checks::
sage: E(6,2) == E(6)^2 == E(3)
True
sage: E(6)^2 != E(3)
False
Addition and multiplication::
sage: E(2) * E(3)
-E(3)
sage: f = E(2) + E(3); f
2*E(3) + E(3)^2
Inverses::
sage: f^-1
1/3*E(3) + 2/3*E(3)^2
sage: f.inverse()
1/3*E(3) + 2/3*E(3)^2
sage: f * f.inverse()
1
Complex conjugation::
sage: f.conjugate()
E(3) + 2*E(3)^2
Galois conjugation::
sage: f.galois_conjugates()
[2*E(3) + E(3)^2, E(3) + 2*E(3)^2]
sage: f.norm_of_galois_extension()
3
Coercion to the algebraic field :class:`QQbar<sage.rings.qqbar.AlgebraicField>`::
sage: QQbar(E(3))
-0.500000000000000? + 0.866025403784439?*I
sage: QQbar(f)
-1.500000000000000? + 0.866025403784439?*I
Partial conversion to the real algebraic field :class:`AA<sage.rings.qqbar.AlgebraicRealField>`::
sage: AA(E(5)+E(5).conjugate())
0.618033988749895?
sage: AA(E(5))
Traceback (most recent call last):
...
TypeError: No conversion of E(5) to the real algebraic field AA.
One can as well define the universal cyclotomic field without any embedding::
sage: UCF.<E> = UniversalCyclotomicField(embedding=None); UCF
Universal Cyclotomic Field
sage: UCF.<E> = UniversalCyclotomicField(embedding=False); UCF
Universal Cyclotomic Field
sage: QQbar(E(5))
Traceback (most recent call last):
...
TypeError: Illegal initializer for algebraic number
Conversion to :class:`CyclotomicField<sage.rings.number_field.number_field.CyclotomicField>`:
.. WARNING::
This is only possible if ``self`` has the standard embedding
::
sage: UCF.<E> = UniversalCyclotomicField()
sage: E(5).to_cyclotomic_field()
zeta5
sage: f = E(2) + E(3)
sage: f.to_cyclotomic_field()
zeta3 - 1
sage: CF = CyclotomicField(5)
sage: CF(E(5))
zeta5
sage: CF = CyclotomicField(7)
sage: CF(E(5))
Traceback (most recent call last):
...
TypeError: The element E(5) cannot be converted to Cyclotomic Field of order 7 and degree 6
sage: CF = CyclotomicField(10)
sage: CF(E(5))
zeta10^2
Conversions to and from GAP::
sage: a = gap('E(6)'); a
-E(3)^2
sage: a.parent()
Gap
sage: b = UCF.from_gap(a); b
-E(3)^2
sage: b.parent()
Universal Cyclotomic Field
sage: gap(b)
-E(3)^2
Conversions to and from the *cyclotomic field*::
sage: a = E(6).to_cyclotomic_field(); a
zeta3 + 1
sage: UCF.from_cyclotomic_field(a)
-E(3)^2
One can also do basic arithmetics with matrices over the universal cyclotomic field::
sage: m = matrix(2,[E(3),1,1,E(4)]); m
[E(3) 1]
[ 1 E(4)]
sage: m.parent()
Full MatrixSpace of 2 by 2 dense matrices over Universal Cyclotomic Field
sage: m^2
[ -E(3) E(12)^4 - E(12)^7 - E(12)^11]
[E(12)^4 - E(12)^7 - E(12)^11 0]
sage: -m
[-E(3) -1]
[ -1 -E(4)]
And compute its *characteristic polynomial*, *echelon form*, *pivots*, and thus its *rank*::
sage: m.charpoly()
x^2 + (-E(12)^4 + E(12)^7 + E(12)^11)*x + E(12)^4 + E(12)^7 + E(12)^8
sage: m.echelon_form()
[1 0]
[0 1]
sage: m.pivots()
(0, 1)
sage: m.rank()
2
The eigenvalues do not (yet) work::
sage: m.eigenvalues() # not implemented
...
NotImplementedError:
A long real life test. Computing ``N3`` is much faster than computing
``N2`` which is again 3 times faster than computing ``N1``::
sage: W = gap3.ComplexReflectionGroup(14) #optional - gap3 # long time
sage: UC = W.UnipotentCharacters() #optional - gap3 # long time
sage: UCF.<E> = UniversalCyclotomicField(); #optional - gap3 # long time
sage: M = matrix(UCF,UC.families[2].fourierMat) #optional - gap3 # long time
sage: N1 = M*M #optional - gap3 # long time
sage: N2 = UCF._matrix_mult(M,M) #optional - gap3 # long time
sage: CF = CyclotomicField(24) #optional - gap3 # long time
sage: M = matrix(CF,M) #optional - gap3 # long time
sage: N3 = matrix(UCF,M*M) #optional - gap3 # long time
sage: N1 == N2 == N3 #optional - gap3 # long time
True
TESTS:
As an indication that everything works, we start with a test that we
obtain the same answers as in GAP::
sage: all(str(E(n,k)).translate(None,' ') == gap.execute('E('+str(n)+')^'+str(k)).translate(None,'\n ') for n in range(1,15) for k in range(n))
True
The following didn't work first::
sage: str(-E(9)^4-E(9)^7).translate(None,' ') == gap.execute('-E(9)^4-E(9)^7').translate(None,'\n ')
True
sage: str(-E(9)^5-E(9)^8).translate(None,' ') == gap.execute('-E(9)^5-E(9)^8').translate(None,'\n ')
True
"""
#*****************************************************************************
# Copyright (C) 2012 Christian Stump <christian.stump@univie.ac.at>
#
# Distributed under the terms of the GNU General Public License (GPL)
# http://www.gnu.org/licenses/
#*****************************************************************************
from sage.misc.cachefunc import cached_method
from random import randint, randrange, sample, choice
import sage.structure.parent_base
from sage.structure.unique_representation import UniqueRepresentation
from sage.structure.element import FieldElement, Element
from sage.structure.parent import Parent
from sage.structure.element_wrapper import ElementWrapper
from sage.structure.sage_object import have_same_parent
from sage.categories.morphism import SetMorphism
from sage.categories.sets_with_partial_maps import SetsWithPartialMaps
from sage.categories.sets_cat import Sets
from sage.categories.homset import Hom
from sage.rings.all import ZZ, QQ
from sage.rings.ring import Field
from sage.rings.qqbar import QQbar, AA
from sage.rings.number_field.number_field import CyclotomicField
from sage.rings.integer import GCD_list, LCM_list
from sage.rings.real_mpfr import RealField, mpfr_prec_min
from sage.rings.complex_field import ComplexField
from sage.rings.complex_double import CDF
from sage.rings.real_lazy import RLF, CLF
from sage.combinat.dict_addition import dict_linear_combination, dict_addition
from sage.rings.universal_cyclotomic_field.universal_cyclotomic_field_c import \
ZumbroichBasisCython, push_down_cython, ZumbroichDecomposition, galois_conjugates_cython, \
push_to_higher_field, dict_multiplication, dict_vector_multiplication
class UniversalCyclotomicField(UniqueRepresentation, Field):
r"""
The *universal cyclotomic field*, which is the smallest field containing
the rational numbers together with all roots of unity.
Its elements are represented as linear combinations of the so-called
*Zumbroich basis*.
EXAMPLES::
sage: UCF.<E> = UniversalCyclotomicField(); UCF
Universal Cyclotomic Field
sage: E(12)
-E(12)^7
sage: E(12) in UCF
True
One also has access to the universal cyclotomic field using the function :func:`CyclotomicField`::
sage: UCF = CyclotomicField(); UCF
Universal Cyclotomic Field
One can also construct a vector space over the universal cyclotomic field::
sage: UCF^3
Vector space of dimension 3 over Universal Cyclotomic Field
"""
def __init__(self,names="E",bracket="()",embedding=True):
r"""
:param names: The string name for the root of unity
:type names: optional, default:"E"
:param bracket: The bracket used in string representations. Can be any even length string
:type bracket: optional, default:"()"
:param embedding: The given embedding of ``self`` into the complex numbers. Can be
- ``True``: The standard embedding
- ``None`` or ``False``: No embedding
- An indexable `f(n) \in \mathbb{N}` such that `0 \leq f(n) < n` sending the generator ``E(n)`` to `e^{ 2 \pi i k / n }`.
:param embedding: optional, default:``True``
.. WARNING::
The optional argument ``embedding`` is not checked for consistency.
TESTS::
sage: F = UniversalCyclotomicField()
sage: TestSuite(F).run()
"""
# the data is stored as linear combinations of elements in the Zumbroich Basis
from sage.combinat.free_module import CombinatorialFreeModule
from sage.categories.fields import Fields
from sage.categories.algebras import Algebras
# getting the optional argument "names" right
if names is None:
names = "E"
if not isinstance(names,str):
if not isinstance(names,(list,tuple)):
raise ValueError("The given name %s is not valid."%names)
if len(names) != 1:
raise ValueError("The given name %s is not valid."%names)
names = names[0]
if not isinstance(names,str):
raise ValueError("The given name %s is not valid."%names)
# getting the optional argument "bracket" right
if isinstance(bracket,str) and len(bracket) % 2 == 0:
bracket_len = len(bracket)
bracket = (bracket[:bracket_len/2],bracket[bracket_len/2:])
else:
raise ValueError("The given bracket %s is not a string of even length."%bracket)
self._data = CombinatorialFreeModule(QQ, ZumbroichBasisIndices(),prefix=names,bracket=bracket)
Parent.__init__(self, base = QQ, category = (Fields(), Algebras(QQ)))
self._has_standard_embedding = False
if embedding not in [False,None]:
if embedding is True:
embedding = lambda n: QQbar.zeta(n)
self._has_standard_embedding = True
P = get_parent_of_embedding(embedding)
# embedding from self into P
def on_basis(x):
return embedding(x[0])**(int(x[1]))
mor = self._data.module_morphism(on_basis, codomain=P)
H = SetMorphism(Hom(self, QQbar), lambda z: mor(z.value))
self.register_embedding(H)
# define partial conversions to AA, if possible
SetMorphism(
Hom(self, AA, SetsWithPartialMaps()),
lambda elem: elem._real_()
).register_as_conversion()
# string representations of elements
def repr_term(m,type="repr"):
if m[1] == 0:
return '1'
elif 2*m[1] == m[0]:
return '-1'
elif m[1] == 1:
if type == "repr":
return '%s%s%s%s'%(names,bracket[0],m[0],bracket[1])
elif type == "latex":
return '\\zeta_{%s}'%m[0]
else:
if type == "repr":
return '%s%s%s%s^%s'%(names,bracket[0],m[0],bracket[1],m[1])
elif type == "latex":
return '\\zeta_{%s}^{%s}'%(m[0],m[1])
self._data._repr_term = lambda m: repr_term(m,type="repr")
self._data._latex_term = lambda m: repr_term(m,type="latex")
# setting zero and one
self._zero = self._from_dict({}, remove_zeros=False)
self._one = self._from_dict({(1,0):QQ(1)}, remove_zeros=False)
@cached_method
def gen(self,n, k=1):
r"""
Returns `\zeta^k` living in :class:`UniversalCyclotomicField`, where `\zeta` denotes the primitive `n`-th root of unity `\zeta = exp(2 \pi i / n)`.
:param n: positive integer.
:param k: positive integer.
:type n: integer
:type k: integer; optional, default ``1``
.. NOTE::
- For the mathematical description of the Zumbroich basis and the
algorithmic behind, see [Bre97]_.
- This function behaves exactly like the *Cyclotomics* in *GAP*.
EXAMPLES::
sage: UCF.<E> = UniversalCyclotomicField()
sage: E(3) # indirect doctest
E(3)
sage: E(6) # indirect doctest
-E(3)^2
sage: E(12) # indirect doctest
-E(12)^7
sage: E(6,2) # indirect doctest
E(3)
sage: E(6)^2 # indirect doctest
E(3)
"""
if n != ZZ(n) or not n > 0 or k != ZZ(k):
raise TypeError('The argument for a root of unity is not correct.')
else:
g = GCD_list([n,k])
n = ZZ(n/g)
k = ZZ(k/g) % n
return self._from_dict(push_down_cython(n, ZumbroichDecomposition(n,k)), coerce=True, remove_zeros=False)
def _first_ngens(self,n):
r"""
Returns the method :meth:`gen` if ``n=1``, and raises an error otherwise.
This method is needed to make the following work::
sage: UCF.<E> = UniversalCyclotomicField() # indirect doctest
"""
if n == 1:
return (self.gen,)
else:
raise ValueError("This ring has only a single generator method.")
def _element_constructor_(self, arg):
r"""
The only way to get here is if there was no coercion found.
In this case, we give other parents the option to define a conversion
using the method ``_universal_cyclotomic_field_``, or raise a TypeError otherwise.
TESTS::
sage: UCF = UniversalCyclotomicField()
sage: UCF(CC(5)) # indirect doctest
Traceback (most recent call last):
...
TypeError: No coercion to the universal cyclotomic field found for the input 5.00000000000000 with parent Complex Field with 53 bits of precision.
sage: p = Permutation([2,1])
sage: UCF(p)
Traceback (most recent call last):
...
TypeError: No coercion to the universal cyclotomic field found for the input [2, 1] with parent Standard permutations.
"""
if hasattr(arg,"_universal_cyclotomic_field_"):
return arg._universal_cyclotomic_field_()
elif isinstance(arg,(sage.interfaces.gap3.GAP3Element,sage.interfaces.gap.GapElement)):
return self.from_gap(arg)
error_str = "No coercion to the universal cyclotomic field found for the input %s"%str(arg)
if hasattr(arg,"parent"):
error_str ="%s with parent %s."%(error_str,str(arg.parent()))
else:
error_str ="%s."%(error_str)
raise TypeError(error_str)
def _coerce_map_from_(self, other):
r"""
If ``self`` has the standard embedding and
- ``other`` is a cyclotomic number field: returns
the coercion thereof, taking non-standard embeddings
of ``other.parent()`` into account.
- ``other`` is also a universal cyclotomic field with the
standard embedding: returns the obvious morphism
EXAMPLES::
sage: UCF = UniversalCyclotomicField()
sage: zeta = CyclotomicField(5).gen()
sage: UCF(zeta) # indirect doctest
E(5)
sage: zeta = CyclotomicField(5,embedding=CC(exp(2*pi*I/5))).gen()
sage: UCF(zeta) # indirect doctest
E(5)
sage: zeta = CyclotomicField(5,embedding=CC(exp(4*pi*I/5))).gen()
sage: UCF(zeta) # indirect doctest
E(5)^2
sage: UCF.<E> = UniversalCyclotomicField();
sage: UCF2.<E2> = UniversalCyclotomicField();
sage: UCF2(E(5))
E2(5)
sage: UCF = UniversalCyclotomicField(embedding=None)
sage: UCF(zeta) # indirect doctest
Traceback (most recent call last):
...
TypeError: No coercion to the universal cyclotomic field found for the input zeta5 with parent Cyclotomic Field of order 5 and degree 4.
"""
from sage.rings.number_field.number_field import NumberField_cyclotomic
from sage.rings.number_field.number_field_morphisms import NumberFieldEmbedding
if self._has_standard_embedding:
if isinstance(other,NumberField_cyclotomic):
return NumberFieldEmbedding(other, self, self.from_cyclotomic_field(other.gen()))
elif isinstance(other,UniversalCyclotomicField) and other._has_standard_embedding:
return SetMorphism(Hom(other,self), lambda z: self._from_dict(z._dict_()))
def __pow__(self,n):
r"""
Returns the ``n``-th power of self as a vector space.
EXAMPLES::
sage: UCF = UniversalCyclotomicField()
sage: UCF^3
Vector space of dimension 3 over Universal Cyclotomic Field
"""
from sage.modules.free_module import VectorSpace
return VectorSpace(self,n)
def is_finite(self):
r"""
Returns False as ``self`` is not finite.
EXAMPLES::
sage: UCF = UniversalCyclotomicField()
sage: UCF.is_finite()
False
"""
return False
def is_subring(self,other):
r"""
Returns True if ``self`` is a subring of ``other``.
.. WARNING::
Currently, it is only checked if ``self is other``!
EXAMPLES::
sage: UCF = UniversalCyclotomicField()
sage: UCF.is_subring(UCF)
True
sage: UCF.is_subring(CC)
False
"""
return other is self
def _repr_(self):
r"""
Returns the string representation of ``self``.
EXAMPLES::
sage: UCF = UniversalCyclotomicField()
sage: UCF._repr_()
'Universal Cyclotomic Field'
"""
return "Universal Cyclotomic Field"
def _gap_init_(self):
r"""
Returns gap string representation of ``self``.
EXAMPLES::
sage: UCF = UniversalCyclotomicField()
sage: UCF._gap_init_()
'Cyclotomics'
"""
return 'Cyclotomics'
def degree(self):
r"""
Returns the *degree* of ``self`` as a field extension over the Rationals.
EXAMPLES::
sage: UCF = UniversalCyclotomicField()
sage: UCF.degree()
+Infinity
"""
from sage.rings.infinity import infinity
return infinity
def characteristic(self):
r"""
Returns ``0`` which is the *characteristic* of ``self``.
EXAMPLES::
sage: UCF = UniversalCyclotomicField()
sage: UCF.characteristic()
0
"""
return ZZ(0)
def prime_subfield(self):
r"""
Returns `\QQ` which is the *prime subfield* of ``self``.
EXAMPLES::
sage: UCF = UniversalCyclotomicField()
sage: UCF.prime_subfield()
Rational Field
"""
return QQ
def is_prime_field(self):
r"""
Returns False since ``self`` is not a prime field.
EXAMPLES::
sage: UCF = UniversalCyclotomicField()
sage: UCF.is_prime_field()
False
"""
return False
def an_element(self, order = 3):
r"""
Returns an element of ``self`` of order ``order``.
:param order: a positive integer.
:type order: integer; optional, default:``3``
EXAMPLES::
sage: UCF = UniversalCyclotomicField()
sage: UniversalCyclotomicField().an_element()
E(3)
sage: UniversalCyclotomicField().an_element(order=6)
-E(3)^2
sage: UniversalCyclotomicField().an_element(order=10)
-E(5)^3
"""
return self.gen(order)
def random_element(self, order=None):
r"""
Returns a (often non-trivial) pseudo-random element of ``self``.
:param order:
:type order: integer or None; optional, default:``None``
EXAMPLES::
sage: UCF = UniversalCyclotomicField()
sage: UCF.random_element() # random
3*E(7)^2 + E(7)^3 + 2*E(7)^4 - 5*E(7)^5
sage: UCF.random_element(order=4) # random
-3*E(4)
sage: UCF.random_element(order=12) # random
E(12)^7 - 4*E(12)^8 + E(12)^11
"""
F = self
seq = [-5,-4,-3,-2,-1,1,2,3,4,5]
if order is not None:
n = order
else:
n = randint(1, 17)
B = ZumbroichBasisIndices().indices(n)
# TODO: could this be written in a more conceptual way
# by having appropriate constructors?
k = randrange(len(B))
B = sample(B, k)
dict_in_basis = {}
for key in B:
dict_in_basis[ key.value ] = QQ(choice(seq))
return F._from_dict(push_down_cython(n,dict_in_basis), remove_zeros=False)
def _from_dict(self, D, coerce=True, remove_zeros=True):
r"""
Returns the element in ``self`` from the given dictionary ``D``.
:param D: a dictionary with keys being elements in the Zumbroich basis and values being Rationals.
:param coerce: if True, the values are coerced to the Rationals.
:type coerce: Boolean; optional, default:``False``
:param remove_zeros: if True, zeros are removed from the dict first. Should be ``True`` unless it is clear that ``D`` doesn't contain zeros.
:type remove_zeros: Boolean; optional, default:``True``
EXAMPLES::
sage: UCF = UniversalCyclotomicField()
sage: D = dict([((1,0),2)])
sage: UCF._from_dict(D)
2
sage: D = dict([((1,0),2)],remove_zeros=False)
sage: UCF._from_dict(D)
2
sage: D = dict([((1,0),2),((3,1),1),((3,2),0)])
sage: UCF._from_dict(D)
2 + E(3)
"""
if coerce:
for X,a in D.iteritems():
D[X] = QQ(a)
elem = self.element_class(self, self._data._from_dict(D, remove_zeros=remove_zeros))
return elem
def from_base_ring(self,coeff):
r"""
Returns the base ring element ``coeff`` as an element in ``self``.
:param coeff: A rational number.
EXAMPLES::
sage: UCF = UniversalCyclotomicField()
sage: x = UCF.from_base_ring(2); x
2
sage: x.parent()
Universal Cyclotomic Field
"""
return self._from_dict({ (1,0):coeff })
def zero(self):
r"""
Returns the zero in ``self``.
EXAMPLES::
sage: UCF = UniversalCyclotomicField()
sage: UCF.zero()
0
"""
return self._zero
def one(self):
r"""
Returns the one in ``self``.
EXAMPLES::
sage: UCF = UniversalCyclotomicField()
sage: UCF.one()
1
"""
return self._one
def monomial(self, mon, check=True):
r"""
Returns the monomial in ``self`` associated to ``mon`` in the Zumbroich basis.
:param mon: an element in the Zumbroich basis
:param check: if True, the monomial is checked to be in the Zumbroich basis
:type check: Boolean; optional, default:``True``
EXAMPLES::
sage: UCF = UniversalCyclotomicField()
sage: UCF.monomial((1,0))
1
sage: UCF.monomial((4,2))
Traceback (most recent call last):
...
ValueError: The given data is not a monomial of the universal cyclotomic field.
"""
if check:
if not mon in ZumbroichBasisIndices():
raise ValueError("The given data is not a monomial of the universal cyclotomic field.")
return self._from_dict({ mon : QQ(1) }, remove_zeros=False)
def sum(self, L):
r"""
Returns the sum of all elements (which must be coerceable into ``self``) in ``L``.
:param L: list or tuple of elements in ``self``
.. NOTE::
Faster than the usual sum as operated directly on dictionaries, as all steps are done together.
EXAMPLES::
sage: UCF.<E> = UniversalCyclotomicField()
sage: UCF.sum([ E(i) for i in range(1,5) ])
E(12)^4 - E(12)^7 - E(12)^11
"""
l = LCM_list([ other.field_order() for other in L ])
large_dict_list = [ push_to_higher_field(other.value._monomial_coefficients, other.field_order(), l) for other in L ]
return self._from_dict(push_down_cython(l,dict_addition(large_dict_list)), remove_zeros=False)
def _matrix_mult(self,M1,M2,order=None):
r"""
Returns the product ``M1`` `\times` ``M2`` of the two matrices ``M1`` and ``M2`` over ``self``.
.. WARNING::
This method is not for public use, but only to provide a quick test how fast we can multiply matrices.
EXAMPLES::
sage: UCF.<E> = UniversalCyclotomicField()
sage: M = matrix(UCF,[[E(3),E(4)],[E(5),E(6)]]); M
[ E(3) E(4)]
[ E(5) -E(3)^2]
sage: M2 = UCF._matrix_mult(M,M); M2
[-E(60)^4 - E(60)^7 - E(60)^16 - E(60)^28 - E(60)^47 - E(60)^52 E(12)^7 - E(12)^11]
[ E(15)^8 - E(15)^13 -E(60)^7 - E(60)^8 - E(60)^32 - E(60)^44 - E(60)^47 - E(60)^56]
sage: M2 == M*M
True
"""
from sage.matrix.all import zero_matrix
if not M1.nrows() == M2.ncols():
raise ValueError("The given matrices cannot be multiplied.")
dim1, dim, dim2 = M1.ncols(), M1.nrows(), M2.nrows()
m_rows = M1.rows()
m_cols = M2.columns()
rows,cols = [],[]
for i in xrange(dim):
rows.append(tuple(x.value._monomial_coefficients for x in m_rows[i]))
cols.append(tuple(x.value._monomial_coefficients for x in m_cols[i]))
M_new = zero_matrix(self,dim1,dim2)
if order:
n = order
else:
LCM = [ x.field_order() for x in set(M1.list()).union(M2.list()) ]
n = LCM_list(LCM)
for i in xrange(dim1):
for j in xrange(dim2):
M_new[i,j] = self._from_dict(push_down_cython(n,dict_vector_multiplication(n,rows[i],cols[j])))
return M_new
def zumbroich_basis_indices(self, n):
r"""
Returns the indices of the *Zumbroich basis* of order ``n``.
The Zumbroich basis is a linear basis of the universal cyclotomic field
that behaves very well with considering an primitive `d`-th root of unity
as a (non primitive) `kd`-th root. See [Bre97]_ for further details.
:param n: positive integer
OUTPUT:
- a set of tuples `(n,k)` of all elements in the Zumbroich basis of order `n`.
EXAMPLES::
sage: UCF = UniversalCyclotomicField()
sage: UCF.zumbroich_basis_indices(8)
set([(8, 1), (8, 3), (8, 0), (8, 2)])
"""
return ZumbroichBasisIndices().indices(n)
def zumbroich_basis(self,n):
r"""
Returns the *Zumbroich basis* of order ``n``.
The Zumbroich basis is a linear basis of the universal cyclotomic field
that behaves very well with considering an primitive `d`-th root of unity
as a (non primitive) `kd`-th root. See [Bre97]_ for further details.
:param n: positive integer
OUTPUT:
- the set of elements in the universal cyclotomic field forming the Zumbroich basis of order `n`.
EXAMPLES::
sage: UCF = UniversalCyclotomicField()
sage: UCF.zumbroich_basis(8)
set([E(8)^3, 1, E(4), E(8)])
sage: UCF.zumbroich_basis(9)
set([E(9)^2, E(3)^2, E(9)^5, E(9)^4, E(3), E(9)^7])
"""
return set(self.gen(n,k) for n,k in self.zumbroich_basis_indices(n))
def from_gap(self, elem):
r"""
Returns the element in ``self`` obtained from the gap by executing ``string``.
:param string: string representing an element in the universal cyclotomic field
EXAMPLES::
sage: UCF = UniversalCyclotomicField()
sage: UCF.from_gap(gap("-E(3)^2"))
-E(3)^2
sage: UCF = UniversalCyclotomicField()
sage: UCF.from_gap(gap("E(3)^2"))
E(3)^2
sage: UCF.from_gap(gap("1/6*E(3)")) # testing a former bug
1/6*E(3)
"""
if not isinstance(elem,(sage.interfaces.gap3.GAP3Element,sage.interfaces.gap.GapElement)):
raise ValueError("The input %s is not a GAP object."%elem)
if hasattr(elem,"sage"):
try:
return self(elem.sage())
except NotImplementedError:
pass
string = str(elem)
terms = string.replace('\n','').replace('-','+-').split('+')
if terms[0] == '':
del terms[0]
for i in range(len(terms)):
if '^' in terms[i]:
terms[i] = terms[i].replace(')^',',') + ')'