-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathNfOrd.jl
1435 lines (1273 loc) · 41.8 KB
/
NfOrd.jl
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
################################################################################
#
# NfOrd/NfOrd.jl : Orders in absolute number fields
#
# This file is part of Hecke.
#
# Copyright (c) 2015, 2016: Claus Fieker, Tommy Hofmann
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#
# Copyright (C) 2015, 2016, 2017 Tommy Hofmann
#
################################################################################
export ==, +, basis, basis_matrix, basis_mat_inv, contains_equation_order,
discriminant, degree, gen_index, EquationOrder, index,
is_equation_order, is_index_divisor, lll, lll_basis, nf,
minkowski_matrix, norm_change_const, Order, parent, different,
signature, trace_matrix, codifferent, ramified_primes,
reduced_discriminant
################################################################################
#
# Make NfOrd fully working Nemo ring
#
################################################################################
Nemo.parent_type(::Type{NfAbsOrdElem{S, T}}) where {S, T} = NfAbsOrd{S, T}
Nemo.elem_type(::NfAbsOrd{S, T}) where {S, T} = NfAbsOrdElem{S, T}
Nemo.elem_type(::Type{NfAbsOrd{S, T}}) where {S, T} = NfAbsOrdElem{S, T}
ideal_type(::NfAbsOrd{S, T}) where {S, T} = NfAbsOrdIdl{S, T}
ideal_type(::Type{NfAbsOrd{S, T}}) where {S, T} = NfAbsOrdIdl{S, T}
fractional_ideal_type(::NfAbsOrd{S, T}) where {S, T} = NfOrdFracIdl
fractional_ideal_type(::Type{NfAbsOrd{S, T}}) where {S, T} = NfOrdFracIdl
Nemo.base_ring(::NfAbsOrd) = FlintZZ
@doc raw"""
parent(O::NfAbsOrd) -> NfOrdSet
Returns the parent of $\mathcal O$, that is, the set of orders of the ambient
number field.
"""
parent(O::NfOrd) = NfAbsOrdSet(nf(O), false)
function contains_equation_order(O::NfAbsOrd)
if isdefined(O, :index)
return true
end
if isdefined(O, :basis_mat_inv)
return isone(O.basis_mat_inv.den)
end
return isinteger(gen_index(O))
end
################################################################################
#
# "Assure" functions for fields
#
################################################################################
function assure_has_basis(O::NfAbsOrd)
if isdefined(O, :basis_ord)
return nothing
end
b = O.basis_nf
d = degree(O)
B = Vector{elem_type(O)}(undef, d)
for i in 1:length(b)
v = ZZRingElem[ZZRingElem(0) for j in 1:d]
one!(v[i])
B[i] = NfAbsOrdElem(O, b[i], v)
end
O.basis_ord = B
return nothing
end
function Base.getindex(O::NfAbsOrd, i::Int)
if iszero(i)
return zero(O)
end
assure_has_basis(O)
@assert i <= degree(O) && i > 0 "Index must be a positive integer smaller than the dimension"
return O.basis_ord[i]
end
function assure_has_basis_matrix(O::NfAbsOrd)
if isdefined(O, :basis_matrix)
return nothing
end
A = O.basis_nf#::Vector{elem_type(nf(O))}
O.basis_matrix = FakeFmpqMat(basis_matrix(A))
return nothing
end
function assure_has_basis_mat_inv(O::NfAbsOrd)
if isdefined(O, :basis_mat_inv)
return nothing
end
M = basis_matrix(O, copy = false)
if isdefined(O, :index) && is_lower_triangular(M.num)
#The order contains the equation order and the matrix is lower triangular
#The inverse is lower triangular and it has denominator 1
#to exploit this, I call can_solve
I = solve_lt(M.num, scalar_matrix(FlintZZ, nrows(M), M.den))
O.basis_mat_inv = FakeFmpqMat(I)
return nothing
end
O.basis_mat_inv = inv(basis_matrix(O, copy = false))
return nothing
end
################################################################################
#
# Basis
#
################################################################################
@inline function basis_ord(O::NfAbsOrd; copy::Bool = true)
assure_has_basis(O)
if copy
res = O.basis_ord::Vector{elem_type(O)}
return deepcopy(res)::Vector{elem_type(O)}
else
return O.basis_ord::Vector{elem_type(O)}
end
end
@doc raw"""
basis(O::NfAbsOrd) -> Vector{NfAbsOrdElem}
Returns the $\mathbf Z$-basis of $\mathcal O$.
"""
@inline basis(O::NfAbsOrd; copy::Bool = true) = basis_ord(O, copy = copy)
@doc raw"""
basis(O::NfOrd, K::AnticNumberField) -> Vector{nf_elem}
Returns the $\mathbf Z$-basis elements of $\mathcal O$ as elements of the
ambient number field.
"""
function basis(O::NfOrd, K::AnticNumberField; copy::Bool = true)
nf(O) != K && error()
if copy
return deepcopy(O.basis_nf)
else
return O.basis_nf
end
end
function basis(O::NfAbsOrd{NfAbsNS, NfAbsNSElem}, K::NfAbsNS; copy::Bool = true)
nf(O) != K && error()
if copy
return deepcopy(O.basis_nf)
else
return O.basis_nf
end
end
################################################################################
#
# (Inverse) basis matrix
#
################################################################################
@doc raw"""
basis_matrix(O::NfAbsOrd) -> FakeFmpqMat
Returns the basis matrix of $\mathcal O$ with respect to the basis
of the ambient number field.
"""
function basis_matrix(O::NfAbsOrd; copy::Bool = true)
assure_has_basis_matrix(O)
if copy
return deepcopy(O.basis_matrix)
else
return O.basis_matrix
end
end
@doc raw"""
basis_mat_inv(O::NfAbsOrd) -> FakeFmpqMat
Returns the inverse of the basis matrix of $\mathcal O$.
"""
function basis_mat_inv(O::NfAbsOrd; copy::Bool = true)
assure_has_basis_mat_inv(O)
if copy
return deepcopy(O.basis_mat_inv)
else
return O.basis_mat_inv
end
end
################################################################################
#
# String I/O
#
################################################################################
function show(io::IO, S::NfOrdSet)
print(io, "Set of orders of the number field ")
print(io, S.nf)
end
function extra_name(O::NfAbsOrd)
set_name!(O)
s = get_attribute(O, :name)
s !== nothing && return
set_name!(nf(O))
s = get_attribute(nf(O), :name)
if s !== nothing
set_name!(O, "O_$s")
end
return get_attribute(O, :name)
end
function show(io::IO, O::NfAbsOrd)
@show_name(io, O)
@show_special(io, O)
if is_maximal_known_and_maximal(O)
show_maximal(io, O)
else
show_gen(io, O)
end
end
function show_gen(io::IO, O::NfAbsOrd)
print(io, "Order of ")
println(io, nf(O))
print(io, "with Z-basis ")
print(io, basis(O, copy = false))
end
function show_maximal(io::IO, O::NfAbsOrd)
print(io, "Maximal order of $(nf(O)) \nwith basis $(O.basis_nf)")
end
################################################################################
#
# Discriminant
#
################################################################################
function assure_has_discriminant(O::NfAbsOrd)
if isdefined(O, :disc)
return nothing
else
if is_equation_order(O) && is_simple(nf(O)) && is_defining_polynomial_nice(nf(O))
O.disc = numerator(discriminant(nf(O).pol))
else
O.disc = det(trace_matrix(O, copy = false))
end
end
return nothing
end
@doc raw"""
discriminant(O::NfOrd) -> ZZRingElem
Returns the discriminant of $\mathcal O$.
"""
function discriminant(O::NfAbsOrd)
assure_has_discriminant(O)
return O.disc
end
#TODO: compute differently in equation orders, this is the rres...
@doc raw"""
reduced_discriminant(O::NfOrd) -> ZZRingElem
Returns the reduced discriminant, that is, the largest elementary divisor of
the trace matrix of $\mathcal O$.
"""
function reduced_discriminant(O::NfOrd)
if is_equation_order(O)
Zx = polynomial_ring(FlintZZ, cached = false)[1]
f = Zx(nf(O).pol)
return rres(f, derivative(f))
end
return maximal_elementary_divisor(trace_matrix(O, copy = false))
end
################################################################################
#
# (Generalized) index
#
################################################################################
@doc raw"""
gen_index(O::NfOrd) -> QQFieldElem
Returns the generalized index of $\mathcal O$ with respect to the equation
order of the ambient number field.
"""
function gen_index(O::NfAbsOrd)
if isdefined(O, :gen_index)
return deepcopy(O.gen_index)
else
#TODO: Remove once the determinant checks if a matrix is upper/lower triangular.
if is_lower_triangular(basis_matrix(O, copy = false).num)
return basis_matrix(O, copy = false).den^degree(O)//prod_diagonal(basis_matrix(O, copy = false).num)
end
O.gen_index = inv(det(basis_matrix(O, copy = false)))
return deepcopy(O.gen_index)
end
end
@doc raw"""
index(O::NfOrd) -> ZZRingElem
Assuming that the order $\mathcal O$ contains the equation order
$\mathbf Z[\alpha]$ of the ambient number field, this function returns the
index $[ \mathcal O : \mathbf Z]$.
"""
function index(O::NfAbsOrd; copy::Bool = false)
if !isdefined(O, :index)
i = gen_index(O)
!isone(denominator(i)) && error("Order does not contain the equation order")
O.index = abs(numerator(i))
end
if copy
return deepcopy(O.index)
else
return O.index
end
end
################################################################################
#
# Index divisor
#
################################################################################
@doc raw"""
is_index_divisor(O::NfOrd, d::ZZRingElem) -> Bool
is_index_divisor(O::NfOrd, d::Int) -> Bool
Returns whether $d$ is a divisor of the index of $\mathcal O$. It is assumed
that $\mathcal O$ contains the equation order of the ambient number field.
"""
function is_index_divisor(O::NfAbsOrd, d::IntegerUnion)
i = index(O, copy = false)
return iszero(i % d)
end
################################################################################
#
# Ramified Primes
#
################################################################################
@doc raw"""
ramified_primes(O::NfAbsOrd) -> Vector{ZZRingElem}
Returns the list of prime numbers that divide $\operatorname{disc}(\mathcal O)$.
"""
function ramified_primes(O::NfAbsOrd)
return collect(keys(factor(discriminant(O)).fac))
end
################################################################################
#
# Deepcopy
#
################################################################################
function Base.deepcopy_internal(O::NfAbsOrd{S, T}, dict::IdDict) where {S, T}
z = NfAbsOrd{S, T}(O.nf)
for x in fieldnames(typeof(O))
if x != :nf && isdefined(O, x)
setfield!(z, x, Base.deepcopy_internal(getfield(O, x), dict))
end
end
if isdefined(z, :basis_ord)
# Until now we have parent(basis(x)) !== z
for b in z.basis_ord
b.parent = z
end
end
return z
end
################################################################################
#
# Minkowski matrix
#
################################################################################
@doc raw"""
minkowski_matrix(O::NfAbsOrd, abs_tol::Int = 64) -> arb_mat
Returns the Minkowski matrix of $\mathcal O$. Thus if $\mathcal O$ has degree
$d$, then the result is a matrix in $\operatorname{Mat}_{d\times d}(\mathbf
R)$. The entries of the matrix are real balls of type `arb` with radius less
then `2^-abs_tol`.
"""
function minkowski_matrix(O::NfAbsOrd, abs_tol::Int = 64)
if isdefined(O, :minkowski_matrix) && O.minkowski_matrix[2] >= abs_tol
A = deepcopy(O.minkowski_matrix[1])
else
M = minkowski_matrix(O.basis_nf, abs_tol)
O.minkowski_matrix = (M, abs_tol)
A = deepcopy(M)
end
return A
end
function minkowski_matrix(B::Vector{S}, abs_tol::Int = 64) where S <: NumFieldElem
K = parent(B[1])
T = Vector{Vector{arb}}(undef, length(B))
for i in 1:length(B)
T[i] = minkowski_map(B[i], abs_tol)
end
p = maximum(Int[ precision(parent(T[i][j])) for i in 1:length(B), j in 1:absolute_degree(K) ])
M = zero_matrix(ArbField(p, cached = false), length(B), absolute_degree(K))
for i in 1:length(B)
for j in 1:absolute_degree(K)
M[i, j] = T[i][j]
end
end
return M
end
@doc raw"""
minkowski_gram_mat_scaled(O::NfAbsOrd, prec::Int = 64) -> ZZMatrix
Let $c$ be the Minkowski matrix as computed by `minkowski_matrix` with precision $p$.
This function computes $d = round(c 2^p)$ and returns $round(d d^t/2^p)$.
"""
function minkowski_gram_mat_scaled(O::NfAbsOrd, prec::Int = 64)
if isdefined(O, :minkowski_gram_mat_scaled) && O.minkowski_gram_mat_scaled[2] >= prec
A = deepcopy(O.minkowski_gram_mat_scaled[1])
shift!(A, prec - O.minkowski_gram_mat_scaled[2])
else
c = minkowski_matrix(O, prec+2)
d = zero_matrix(FlintZZ, degree(O), degree(O))
A = zero_matrix(FlintZZ, degree(O), degree(O))
round_scale!(d, c, prec)
ccall((:fmpz_mat_gram, libflint), Nothing, (Ref{ZZMatrix}, Ref{ZZMatrix}), A, d)
shift!(A, -prec)
O.minkowski_gram_mat_scaled = (A, prec)
A = deepcopy(A)
end
# to ensure pos. definitenes, we add n to the diag.
for i=1:degree(O)
fmpz_mat_entry_add_ui!(A, i, i, UInt(nrows(A)))
end
return A
end
function minkowski_gram_mat_scaled(B::Vector{T}, prec::Int = 64) where T <: NumFieldElem
K = parent(B[1])
c = minkowski_matrix(B, prec+2)
d = zero_matrix(FlintZZ, length(B), absolute_degree(K))
A = zero_matrix(FlintZZ, length(B), length(B))
round_scale!(d, c, prec)
ccall((:fmpz_mat_gram, libflint), Nothing, (Ref{ZZMatrix}, Ref{ZZMatrix}), A, d)
shift!(A, -prec)
return A
end
################################################################################
#
# Inclusion of number field elements
#
################################################################################
# Check if a number field element is contained in O
# In this case, the second return value is the coefficient vector with respect
# to the basis of O
function _check_elem_in_order(a::T, O::NfAbsOrd{S, T},
short::Type{Val{U}} = Val{false}) where {S, T, U}
assure_has_basis_mat_inv(O)
t = O.tcontain
elem_to_mat_row!(t.num, 1, t.den, a)
t = mul!(t, t, O.basis_mat_inv)
if short == Val{true}
return isone(t.den)
elseif short == Val{false}
if !isone(t.den)
return false, Vector{ZZRingElem}()
else
v = Vector{ZZRingElem}(undef, degree(O))
for i in 1:degree(O)
v[i] = t.num[1, i]
end
return true, v
end
end
end
function in(a::NfAbsNSElem, O::NfAbsOrd)
@assert parent(a) == nf(O)
return _check_elem_in_order(a, O, Val{true})
end
@doc raw"""
in(a::NumFieldElem, O::NumFieldOrd) -> Bool
Checks whether $a$ lies in $\mathcal O$.
"""
function in(a::nf_elem, O::NfOrd)
@assert parent(a) == nf(O)
if is_defining_polynomial_nice(nf(O)) && contains_equation_order(O)
d = denominator!(O.tcontain_fmpz, a)
if isone(d)
return true
end
exp_index = basis_matrix(O, copy = false).den
if !divisible(exp_index, d)
return false
end
M = basis_mat_inv(O, copy = false)
d2 = ppio(M.den, d)[1]
t = O.tcontain
elem_to_mat_row!(t.num, 1, t.den, a)
d = mul!(d, d, d2)
if fits(Int, d)
R = residue_ring(FlintZZ, Int(d), cached = false)
return _check_containment(R, M.num, t.num)
else
R1 = residue_ring(FlintZZ, d, cached = false)
return _check_containment(R1, M.num, t.num)
end
end
return _check_elem_in_order(a, O, Val{true})
end
function _check_containment(R, M, t)
M1 = map_entries(R, M)
t1 = map_entries(R, t)
mul!(t1, t1, M1)
return iszero(t1)
end
################################################################################
#
# Denominator in an order
#
################################################################################
@doc raw"""
denominator(a::nf_elem, O::NfOrd) -> ZZRingElem
Returns the smallest positive integer $k$ such that $k \cdot a$ is contained in
$\mathcal O$.
"""
function denominator(a::NfAbsNSElem, O::NfAbsOrd)
M = O.tcontain
elem_to_mat_row!(M.num, 1, M.den, a)
M = mul!(M, M, basis_mat_inv(O, copy = false))
return deepcopy(M.den)
end
function denominator(a::nf_elem, O::NfOrd)
@assert parent(a) == nf(O)
if is_defining_polynomial_nice(nf(O)) && contains_equation_order(O)
d = denominator(a)
if isone(d)
return d
end
d1, d2 = ppio(d, index(O))
if isone(d1)
return d2
end
a1 = d2*a
a1 = mod(a1, d1)
M = basis_mat_inv(O, copy = false)
d3 = ppio(M.den, d1)[1]
M1 = mod(M.num, d1*d3)
t = O.tcontain
elem_to_mat_row!(t.num, 1, t.den, a1)
mul!(t.num, t.num, M1)
c = gcd(content(t.num), d1*d3)
c1 = divexact(d1*d3, c)
return d2*c1
end
M = O.tcontain
elem_to_mat_row!(M.num, 1, M.den, a)
M = mul!(M, M, basis_mat_inv(O, copy = false))
return deepcopy(M.den)
end
function integral_split(a::nf_elem, O::NfOrd)
assure_has_basis_mat_inv(O)
M = O.tcontain
elem_to_mat_row!(M.num, 1, M.den, a)
M = mul!(M, M, O.basis_mat_inv)
return M.den, M.num
end
##################################3#############################################
#
# Norm change constant
#
################################################################################
# For x = \sum_i x_i omega_i let |x|_1 = \sqrt(x_1^2 + ... + x_d^2).
# And let |x|_2 = sqrt(T_2(x))
# Then there exist c1, c2 such that
# |x|_2^2 <= c1 |x|_2^2, |x|_1^2 <= c2 |x|_1^2
# A suitable pair (c1, c2) can be determined using the Minkowski map/matrix
#
# Reference
# Fieker, Friedrichs
# On Reconstruction of Algebraic Numbers
# (in particular p. 288)
#
# TODO: Make this rigorous using interval arithmetic. The only problem is that
# the characteristic polynomial might not be squarefree.
@doc raw"""
norm_change_const(O::NfOrd) -> (Float64, Float64)
Returns $(c_1, c_2) \in \mathbf R_{>0}^2$ such that for all
$x = \sum_{i=1}^d x_i \omega_i \in \mathcal O$ we have
$T_2(x) \leq c_1 \cdot \sum_i^d x_i^2$
and
$\sum_i^d x_i^2 \leq c_2 \cdot T_2(x)$,
where $(\omega_i)_i$ is the $\mathbf Z$-basis of $\mathcal O$.
"""
function norm_change_const(O::NfOrd; cached::Bool = true)
if cached && isdefined(O, :norm_change_const)
return O.norm_change_const::Tuple{BigFloat, BigFloat}
end
z = _norm_change_const(O.basis_nf)
O.norm_change_const = z
return z::Tuple{BigFloat, BigFloat}
end
function _norm_change_const(v::Vector{nf_elem})
d = degree(parent(v[1]))
M = minkowski_matrix(v, 64)
# I don't think we have to swap rows,
# since permutation matrices are orthogonal
#r1, r2 = signature(O)
#for i in 2:2:r2
# swap_rows!(M, r1 + i, r1 + 2*r2 - i + 1)
#end
M = M*transpose(M)
N = Symmetric([ Float64(M[i, j]) for i in 1:nrows(M), j in 1:ncols(M) ])
#forcing N to really be Symmetric helps julia - apparently
if any(!isfinite, N)
fl1 = true
else
r = sort(LinearAlgebra.eigvals(N))
fl1 = false
for ind = 1:length(r)
if isnan(r[ind])
fl1 = true
break
end
end
end
if fl1 || !(r[1] > 0)
# more complicated methods are called for...
m = ceil(Int, log(d)/log(2))
m += m%2
@assert iseven(m)
l_max = root(tr(M^m), m) #an upper bound within a factor of 2
#according to a paper by Victor Pan
#https://doi.org/10.1016/0898-1221(90)90236-D
#formula (1) and discussion
pr = 128
l_min = l_max
if isodd(d) d+=1; end
while true
try
M = inv(M)
l_min = root(tr(M^d), d) #as above...
if isfinite(l_min)
z = (BigFloat(l_max), BigFloat(l_min))
return z::Tuple{BigFloat, BigFloat}
end
catch e
# should verify the correct error
if !(e isa ErrorException)
rethrow(e)
end
finally
M = minkowski_matrix(v, pr)
M = M*transpose(M)
pr *= 2
end
end
end
@assert r[1]>0
z = (BigFloat(r[end]), BigFloat(inv(r[1])))
return z::Tuple{BigFloat, BigFloat}
end
################################################################################
#
# Construction of orders
#
################################################################################
#the one WITHOUT K is there for rel-ext, so this is for parity
function Order(a::Vector{T}; check::Bool = true, isbasis::Bool = false,
cached::Bool = false) where {T <: NumFieldElem{QQFieldElem}}
return Order(parent(a[1]), a, check = check, isbasis = isbasis, cached = cached)
end
@doc raw"""
Order(a::Vector{nf_elem}; check::Bool = true, cached::Bool = true, isbasis::Bool = false) -> NfOrd
Order(K::AnticNumberField, a::Vector{nf_elem}; check::Bool = true, cached::Bool = true, isbasis::Bool = false) -> NfOrd
Returns the order generated by $a$. If `check` is set, it is checked
whether $a$ defines an order, in particular the integrality of the elements is
checked by computing minimal polynomials. If `isbasis` is set, then elements are
assumed to form a $\mathbf{Z}$-basis. If `cached` is set, then the constructed
order is cached for future use.
"""
function Order(K::S, a::Vector{T}; check::Bool = true, isbasis::Bool = false,
cached::Bool = false) where {S <: NumField{QQFieldElem}, T <: NumFieldElem{QQFieldElem}}
@assert all(x->K == parent(x), a)
if isbasis
if check
b, bmat, bmat_inv, _ = defines_order(K, a)
if !b
error("The elements do not define an order")
else
return NfAbsOrd(K, bmat, bmat_inv, deepcopy(a), cached)
end
else
return NfAbsOrd(deepcopy(a), cached)
end
else
return _order(K, a, cached = cached, check = check)
end
end
function Order(K, a::Vector; check::Bool = true, isbasis::Bool = false,
cached::Bool = true)
local b::Vector{elem_type(K)}
try
b = map(K, a)
b = convert(Vector{elem_type(K)}, b)
catch
error("Cannot coerce elements from array into the number field")
end
return Order(K, b, check = check, cached = cached, isbasis = isbasis)
end
@doc raw"""
Order(K::AnticNumberField, A::FakeFmpqMat; check::Bool = true) -> NfOrd
Returns the order which has basis matrix $A$ with respect to the power basis
of $K$. If `check` is set, it is checked whether $A$ defines an order.
"""
function Order(K::S, a::FakeFmpqMat; check::Bool = true,
cached::Bool = false) where {S <: NumField{QQFieldElem}}
if check
b, ainv, d = defines_order(K, a)
if !b
error("The basis matrix does not define an order")
else
return NfAbsOrd(K, deepcopy(a), ainv, d, cached)
end
else
return NfAbsOrd(K, deepcopy(a), cached)
end
end
@doc raw"""
Order(K::AnticNumberField, A::QQMatrix; check::Bool = true) -> NfOrd
Returns the order which has basis matrix $A$ with respect to the power basis
of $K$. If `check` is set, it is checked whether $A$ defines an order.
"""
function Order(K::S, a::QQMatrix; check::Bool = true,
cached::Bool = true) where {S <: Union{AnticNumberField, NfAbsNS}}
return Order(K, FakeFmpqMat(a), cached = cached, check = check)
end
@doc raw"""
Order(K::AnticNumberField, A::ZZMatrix, check::Bool = true) -> NfOrd
Returns the order which has basis matrix $A$ with respect to the power basis
of $K$. If `check` is set, it is checked whether $A$ defines an order.
"""
function Order(K::S, a::ZZMatrix, check::Bool = true,
cached::Bool = true) where {S}
return Order(K, FakeFmpqMat(a), check = check, cached = cached)
end
################################################################################
#
# Extension of orders
#
################################################################################
function extend(O::NfAbsOrd, elts::Vector{T}; check::Bool = true, cached::Bool = true) where {T}
# check = true <=> elts[i] is checked to be integral
return _order(nf(O), elts; cached = cached, check = check, extends = O)
end
function extend(O::NfRelOrd, elts::Vector{T}; check::Bool = true, cached::Bool = true) where {T}
throw(NotImplemented())
end
################################################################################
#
# Any order
#
################################################################################
#Based on an idea of Lenstra. More details in
#https://www.sciencedirect.com/science/article/pii/S0019357701800392
#https://www.impan.pl/pl/wydawnictwa/czasopisma-i-serie-wydawnicze/acta-arithmetica/all/120/3/82018/decomposition-of-primes-in-non-maximal-orders
#: Denis Simon: The index of nonmonic polynomials
# Indag Math, 2001
#: Denis Simon, Ilaria Del Corso, Roberto Dvornicich:
# Decomposition of primes in non-maximal orders,
# Acta Arithmetica 120 (2005), 231-244
#
@doc raw"""
any_order(K::number_field)
Return some order in $K$. In case the defining polynomial for $K$
is monic and integral, this just returns the equation order.
In the other case $\mathbb Z[\alpha]\cap \mathbb Z[1/\alpha]$
is returned.
"""
function any_order(K::AnticNumberField)
f = K.pol
de = denominator(f)
g = f * de
if is_monic(g)
return equation_order(K)
else
d = degree(g)
M = zero_matrix(FlintZZ, d, d)
M[1, 1] = 1
for i in 2:d
for j in i:-1:2
M[i, j] = numerator(coeff(g, d - (i - j)))
end
end
@hassert :NfOrd 1 defines_order(K, FakeFmpqMat(M))[1]
z = NfAbsOrd{AnticNumberField, nf_elem}(K, FakeFmpqMat(M))
z.is_equation_order = false
return z
end
end
function any_order(K::NfAbsNS)
normalized_gens = Vector{NfAbsNSElem}(undef, ngens(K))
g = gens(K)
for i in 1:ngens(K)
f = denominator(K.pol[i]) * K.pol[i]
if isone(coeff(f, 1))
normalized_gens[i] = g[i]
else
normalized_gens[i] = coeff(f, 1) * g[i]
end
end
b = Vector{NfAbsNSElem}(undef, degree(K))
ind = 1
it = cartesian_product_iterator([1:degrees(K)[i] for i in 1:ngens(K)], inplace = true)
for i in it
b[ind] = prod(normalized_gens[j]^(i[j] - 1) for j=1:length(i))
ind += 1
end
return Order(K, b, check = false, cached = false)
end
################################################################################
#
# Equation order
#
################################################################################
equation_order(K, cached::Bool = false) = EquationOrder(K, cached)
@doc raw"""
EquationOrder(K::number_field) -> NumFieldOrd
equation_order(K::number_field) -> NumFieldOrd
Returns the equation order of the number field $K$.
"""
function EquationOrder(K::NumField{QQFieldElem}, cached::Bool = true)
if cached
return get_attribute!(K, :equation_order) do
return __equation_order(K)
end::order_type(K)
else
return __equation_order(K)
end
end
# If the numerator of the defining polynomial is not monic, then we construct
# the order as described in exercise 15, chapter 15 of Cohen's first book.
# This is due to H. Lenstra.
function __equation_order(K::AnticNumberField)
f = K.pol
if isone(denominator(f) * leading_coefficient(f))
M = FakeFmpqMat(identity_matrix(FlintZZ, degree(K)))
Minv = FakeFmpqMat(identity_matrix(FlintZZ, degree(K)))
z = NfAbsOrd{AnticNumberField, nf_elem}(K, M, Minv, basis(K), false)
z.is_equation_order = true
return z
else
error("Primitive element must be integral")
end
end
function __equation_order(K::NfAbsNS)
for f in K.pol
if !isone(denominator(f) * coeff(f, 1))
error("Generators must be integral")
end
end
M = FakeFmpqMat(identity_matrix(FlintZZ, degree(K)))
Minv = FakeFmpqMat(identity_matrix(FlintZZ, degree(K)))
z = NfAbsOrd{NfAbsNS, NfAbsNSElem}(K, M, Minv, basis(K), false)
z.is_equation_order = true
return z
end
@doc raw"""
EquationOrder(f::ZZPolyRingElem; cached::Bool = true, check::Bool = true) -> NfOrd
equation_order(f::ZZPolyRingElem; cached::Bool = true, check::Bool = true) -> NfOrd
Returns the equation order defined by the monic polynomial $f$.
"""
function EquationOrder(f::ZZPolyRingElem; cached::Bool = true, check::Bool = true)
is_monic(f) || error("polynomial must be monic")
K = number_field(f, cached = cached, check = check)[1]
return EquationOrder(K)
end
equation_order(f::ZZPolyRingElem; cached::Bool = true, check::Bool = true) = EquationOrder(f, cached = cached, check = check)
@doc raw"""
EquationOrder(f::QQPolyRingElem; cached::Bool = true, check::Bool = true) -> NfOrd
equation_order(f::QQPolyRingElem; cached::Bool = true, check::Bool = true) -> NfOrd
Returns the equation order defined by the monic integral polynomial $f$.
"""
function EquationOrder(f::QQPolyRingElem; cached::Bool = true, check::Bool = true)
is_monic(f) || error("polynomial must be integral and monic")
isone(denominator(f)) || error("polynomial must be integral and monic")
K = number_field(f, cached = cached, check = check)[1]
return EquationOrder(K)
end
equation_order(f::QQPolyRingElem; cached::Bool = true, check::Bool = true) = EquationOrder(f, cached = cached, check = check)
@doc raw"""
equation_order(M::NfOrd) -> NfOrd
The equation order of the number field.
"""
equation_order(M::NfAbsOrd) = equation_order(nf(M))
function _order(K::S, elt::Vector{T}; cached::Bool = true, check::Bool = true, extends = nothing) where {S <: NumField{QQFieldElem}, T}
elt = unique(elt)
n = degree(K)
if extends !== nothing
extended_order::order_type(K) = extends