-
Notifications
You must be signed in to change notification settings - Fork 59
/
RayClassGrp.jl
1443 lines (1318 loc) · 45 KB
/
RayClassGrp.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
add_verbosity_scope(:RayFacElem)
add_assertion_scope(:RayFacElem)
###############################################################################
#
# Map Type
#
###############################################################################
mutable struct MapRayClassGrp <: Map{FinGenAbGroup, FacElemMon{Hecke.AbsNumFieldOrderIdealSet{AbsSimpleNumField, AbsSimpleNumFieldElem}}, HeckeMap, MapRayClassGrp}
header::Hecke.MapHeader{FinGenAbGroup, FacElemMon{Hecke.AbsNumFieldOrderIdealSet{AbsSimpleNumField, AbsSimpleNumFieldElem}}}
defining_modulus::Tuple{AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, Vector{InfPlc{AbsSimpleNumField, AbsSimpleNumFieldEmbedding}}}
fact_mod::Dict{AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, Int} #The factorization of the finite part of the defining modulus
gens::Tuple{Vector{AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}}, Vector{FinGenAbGroupElem}}
#Dictionaries to cache preimages. Used in the action on the ray class group
prime_ideal_preimage_cache::Dict{AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, FinGenAbGroupElem}
prime_ideal_cache::Vector{AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}}
clgrpmap::MapClassGrp
powers::Vector{Tuple{AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}}}
groups_and_maps::Vector{Tuple{FinGenAbGroup, GrpAbFinGenToAbsOrdQuoRingMultMap}}
disc_log_inf_plc::Dict{InfPlc, FinGenAbGroupElem} #The infinite places and the corresponding discrete logarithm.
gens_mult_grp_disc_log::Vector{Tuple{AbsSimpleNumFieldOrderElem, FinGenAbGroupElem}}
function MapRayClassGrp()
z = new()
z.prime_ideal_preimage_cache = Dict{AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, FinGenAbGroupElem}()
return z
end
end
defining_modulus(mR) = mR.defining_modulus
################################################################################
#
# Function that stores the principal generators element of the powers
# of the generators in the class group map
#
################################################################################
function __assure_princ_gen(c::Hecke.ClassGrpCtx{sparse_matrix_type(ZZ)}, nquo::Int)
module_trafo_assure(c.M)
C = c.dl_data[3]
OK = order(c)
s = c.dl_data[1]
if length(c.dl_data) == 4
T = c.dl_data[4]
else
T = inv(c.dl_data[2])
c.dl_data = (s, c.dl_data[2], C, T)
end
RelHnf = c.M.basis
gens = c.FB.ideals
rels = vcat(c.R_gen, c.R_rel)
trafo = c.M.trafo
res = Tuple{FacElem{AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, AbsNumFieldOrderIdealSet{AbsSimpleNumField, AbsSimpleNumFieldElem}}, FacElem{AbsSimpleNumFieldElem, AbsSimpleNumField}}[]
diff = ppio(Int(C.snf[end]), nquo)[2]
diff_gens = ncols(T) - ngens(C)
for i = 1:ngens(C)
if nquo != -1
if is_coprime(C.snf[i], nquo)
continue
end
el = diff*sub(T, i+diff_gens:i+diff_gens, 1:ncols(T))
ex = Int(ppio(Int(C.snf[i]), nquo)[1])
else
el = sub(T, i+diff_gens:i+diff_gens, 1:ncols(T))
ex = Int(C.snf[i])
end
els_r = Tuple{Int, ZZRingElem}[]
DI = Dict{AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, ZZRingElem}()
for j = 1:ncols(el)
if !iszero(el[1, j])
add_to_key!(DI, gens[j+s-1], el[1, j])
push!(els_r, (j+s-1, ex*el[1, j]))
end
end
r = sparse_row(FlintZZ, els_r, sort = false)
sol, d = _solve_ut(RelHnf, r)
@assert isone(d)
rs = zeros(ZZRingElem, c.M.bas_gens.r + c.M.rel_gens.r)
for (p,v) in sol
rs[p] = v
end
for i in length(trafo):-1:1
apply_right!(rs, trafo[i])
end
e = FacElem(rels, rs)
e = reduce_mod_units([e], get_attribute(OK, :UnitGrpCtx))[1]
dd = e.fac
for i = dd.idxfloor:length(dd.vals)
if dd.slots[i] == 0x1 && iszero(dd.vals[i])
dd.count -= 1
dd.slots[i] = 0x0
end
end
I = FacElem(DI)
J, a = reduce_ideal(I)
inv!(a)
pow!(a, ex)
mul!(e, e, a)
push!(res, (FacElem(Dict(J => 1)), e))
end
return res
end
function _assure_princ_gen(mC::MapClassGrp)
if isdefined(mC, :princ_gens)
return nothing
end
C = domain(mC)
OK = order(codomain(mC))
K = nf(OK)
if order(domain(mC)) == 1
res1 = Vector{Tuple{FacElem{AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, AbsNumFieldOrderIdealSet{AbsSimpleNumField, AbsSimpleNumFieldElem}}, FacElem{AbsSimpleNumFieldElem, AbsSimpleNumField}}}()
if ngens(domain(mC)) == 1
push!(res1, (FacElem(Dict(ideal(OK, 1) => 1)), FacElem(Dict(K(1) => 1))))
end
mC.princ_gens = res1
return nothing
end
c = get_attribute(OK, :ClassGrpCtx)
if c !== nothing && isdefined(c, :dl_data)
res = __assure_princ_gen(c, mC.quo)
@hassert :RayFacElem 1 is_consistent(mC, res)
mC.princ_gens = res
return nothing
else
c = get_attribute(OK.lllO, :ClassGrpCtx)
reslll = __assure_princ_gen(c, mC.quo)
res = Vector{Tuple{FacElem{AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, AbsNumFieldOrderIdealSet{AbsSimpleNumField, AbsSimpleNumFieldElem}}, FacElem{AbsSimpleNumFieldElem, AbsSimpleNumField}}}(undef, length(reslll))
for i = 1:length(res)
fe = Dict{AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, ZZRingElem}()
for (k, v) in reslll[i][1]
fe[IdealSet(OK)(k)] = v
end
res[i] = (FacElem(fe), reslll[i][2])
end
@hassert :RayFacElem 1 is_consistent(mC, res)
mC.princ_gens = res
return nothing
end
end
function is_consistent(mC, res)
C = domain(mC)
OK = order(codomain(mC))
for i = 1:length(res)
I = evaluate(res[i][1]).num
if mC\I != C[i]
return false
end
e = evaluate(res[i][2])
J = ideal(OK, OK(e))
if I^Int(C.snf[i]) != J
return false
end
end
return true
end
################################################################################
#
# Class group as a ray class group
#
################################################################################
function class_as_ray_class(C::FinGenAbGroup, mC::MapClassGrp, exp_class::Function, m::AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, expo::Int)
O = order(m)
X = abelian_group(rels(C))
if expo != -1
Q, mQ = quo(C, expo, false)
local disclog1
let Q = Q, mC = mC, mQ = mQ, X = X
function disclog1(J::AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem})
return mQ(mC\(J))
end
function disclog1(J::FacElem{AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, AbsNumFieldOrderIdealSet{AbsSimpleNumField, AbsSimpleNumFieldElem}})
a = X[0]
for (f, k) in J.fac
a += k*disclog(f)
end
return a
end
end
local expo_map
let mQ = mQ, exp_class = exp_class
function expo_map(el::FinGenAbGroupElem)
@assert parent(el) === codomain(mQ)
return exp_class(mQ\el)
end
end
mp1 = Hecke.MapRayClassGrp()
mp1.header = Hecke.MapHeader(Q, FacElemMon(parent(m)), expo_map, disclog1)
mp1.fact_mod = Dict{AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, Int}()
mp1.defining_modulus = (m, InfPlc[])
mp1.clgrpmap = mC
return Q, mp1
end
local disclog
let X = X, mC = mC
function disclog(J::AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem})
return X((mC\J).coeff)
end
function disclog(J::FacElem{AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, AbsNumFieldOrderIdealSet{AbsSimpleNumField, AbsSimpleNumFieldElem}})
a = X[0]
for (f, k) in J.fac
a += k*disclog(f)
end
return a
end
end
mp = Hecke.MapRayClassGrp()
mp.header = Hecke.MapHeader(X, FacElemMon(parent(m)), exp_class, disclog)
mp.fact_mod = Dict{AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, Int}()
mp.defining_modulus = (m, InfPlc[])
mp.clgrpmap = mC
return X, mp
end
function empty_ray_class(m::AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem})
O = order(parent(m))
X = abelian_group(Int[])
local exp
let O = O, X = X
function exp(a::FinGenAbGroupElem)
@assert parent(a) === X
return FacElem(Dict(ideal(O,1) => ZZRingElem(1)))
end
end
local disclog
let X = X
function disclog(J::Union{AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, FacElem{AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}}})
return id(X)
end
end
mp = Hecke.MapRayClassGrp()
mp.header = Hecke.MapHeader(X, FacElemMon(parent(m)) , exp, disclog)
mp.defining_modulus = (m, InfPlc[])
return X,mp
end
##############################################################################
#
# Functions for the evaluation of factored elements
#
###############################################################################
#
# Multiple elements evaluation
#
function fac_elems_eval(p::AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, q::AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, elems::Vector{FacElem{AbsSimpleNumFieldElem, AbsSimpleNumField}}, exponent::ZZRingElem)
return _eval_quo(elems, p, q, exponent)
end
function _preproc(el::FacElem{AbsSimpleNumFieldElem, AbsSimpleNumField}, exponent::ZZRingElem)
K = base_ring(el)
OK = maximal_order(K)
Qx = parent(K.pol)
x = Dict{AbsSimpleNumFieldElem, ZZRingElem}()
for (f, k) in el
l = mod(k,exponent)
if iszero(l)
continue
end
if f in OK
add_to_key!(x, f, l)
else
d = denominator(f, OK)
add_to_key!(x, K(d), exponent-l)
n = d*f
c = numerator(content(Qx(n)))
if isone(c)
add_to_key!(x, n, l)
else
add_to_key!(x, divexact(n, c), l)
add_to_key!(x, K(c), l)
end
end
end
if !isempty(x)
return FacElem(x)
else
return FacElem(Dict(K(1)=> 1))
end
end
function _preproc(p::AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, el::FacElem{AbsSimpleNumFieldElem, AbsSimpleNumField}, exponent::ZZRingElem)
O = order(p)
K = nf(O)
Qx = parent(K.pol)
x = Dict{AbsSimpleNumFieldElem, ZZRingElem}()
P = minimum(p, copy = false)
for (f, k) in el
l = mod(k,exponent)
if iszero(l)
continue
end
n = numerator(f)
d = denominator(f)
if !isone(d)
com, uncom = ppio(d, P)
if !isone(uncom)
add_to_key!(x, K(mod(uncom, P)), exponent-l)
end
if !isone(com)
e, b = is_power(com)
add_to_key!(x, K(b), -e*l)
end
end
c = numerator(content(Qx(n)))
if isone(c)
add_to_key!(x, n, l)
else
add_to_key!(x, divexact(n, c), l)
com, uncom = ppio(c, P)
if !isone(uncom)
add_to_key!(x, K(mod(uncom, P)), l)
end
if !isone(com)
e, b = is_power(com)
add_to_key!(x, K(b), e*l)
end
end
end
if !isempty(x)
return FacElem(x)
else
return FacElem(Dict(K(1)=> 1))
end
end
function _preproc(p::AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, elems::Vector{FacElem{AbsSimpleNumFieldElem, AbsSimpleNumField}}, exponent::ZZRingElem)
newelems = FacElem{AbsSimpleNumFieldElem, AbsSimpleNumField}[_preproc(p, x, exponent) for x in elems]
return newelems
end
function _powermod(a::AbsSimpleNumFieldElem, i::Int, p::ZZRingElem)
if iszero(i)
return one(parent(a))
elseif isone(i)
b = mod(a, p)
return b
else
bit = ~((~UInt(0)) >> 1)
while (UInt(bit) & i) == 0
bit >>= 1
end
z = deepcopy(a)
bit >>= 1
while bit != 0
mul!(z, z, z)
z = mod(z, p)
if (UInt(bit) & i) != 0
mul!(z, z, a)
z = mod(z, p)
end
bit >>= 1
end
return z
end
end
function _ev_quo(Q, mQ, elems, p, exponent, multiplicity::Int)
el = elem_type(Q)[one(Q) for i = 1:length(elems)]
anti_uni = anti_uniformizer(p)
powers = Dict{Int, AbsSimpleNumFieldElem}()
powers[1] = anti_uni
O = order(p)
F, mF = residue_field(O, p)
for i = 1:length(elems)
J = elems[i]
vp = ZZRingElem(0)
for (f, k1) in J
k = mod(k1, exponent)
if iszero(k)
continue
end
if isinteger(f)
inte = numerator(coeff(f, 0))
vpp, np = remove(inte, minimum(p, copy = false))
mul!(el[i], el[i], Q(np)^k)
vp += vpp*k
continue
end
el1 = O(f, false)
if !iszero(mF(el1))
if !isone(k)
mul!(el[i], el[i], mQ(el1)^k)
else
mul!(el[i], el[i], mQ(el1))
end
continue
end
val = valuation(f, p)
if haskey(powers, val)
act_el = O(powers[val]*f, false)
else
exp_av = div(multiplicity*val, ramification_index(p))
anti_val = _powermod(anti_uni, val, minimum(p)^(exp_av+1))
powers[val] = anti_val
act_el = O(anti_val*f, false)
end
if !isone(k)
mul!(el[i], el[i], mQ(act_el)^k)
else
mul!(el[i], el[i], mQ(act_el))
end
end
vp = mod(vp, exponent)
if !iszero(vp)
if haskey(powers, ramification_index(p))
eli = minimum(p, copy = false)*powers[ramification_index(p)]
else
powers[ramification_index(p)] = _powermod(anti_uni, ramification_index(p), minimum(p)^(multiplicity+1))
eli = minimum(p, copy = false)*powers[ramification_index(p)]
end
if isone(vp)
mul!(el[i], el[i], mQ(O(eli, false)))
else
mul!(el[i], el[i], mQ(O(eli, false))^vp)
end
end
end
return AbsSimpleNumFieldOrderElem[mQ\el[i] for i=1:length(el)]
end
function _eval_quo(elems::Vector{FacElem{AbsSimpleNumFieldElem, AbsSimpleNumField}}, p::AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, q::AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, exponent::ZZRingElem)
O = order(p)
if p == q
if fits(Int, p.minimum)
Q, mQ = ResidueFieldSmall(O, p)
return _ev_quo(Q, mQ, elems, p, exponent, 1)
else
Q, mQ = residue_field(O, p)
return _ev_quo(Q, mQ, elems, p, exponent, 1)
end
else
Q, mQ = quo(O, q)
mult = Int(clog(norm(q), norm(p)))
return _ev_quo(Q, mQ, elems, p, exponent, mult)
end
end
################################################################################
#
# n-part of the class group
#
################################################################################
function is_coprime(a, b)
return isone(gcd(a, b))
end
function n_part_class_group(mC::Hecke.MapClassGrp, n::Integer)
O = order(codomain(mC))
C = domain(mC)
@assert is_snf(C)
K = nf(O)
if is_coprime(exponent(C), n)
G = abelian_group(ZZRingElem[])
local exp1
let O = O, G = G
function exp1(a::FinGenAbGroupElem)
@assert parent(a) === G
return ideal(O, one(O))
end
end
local disclog1
let G = G
function disclog1(I::AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem})
return G[0]
end
end
mp=Hecke.MapClassGrp()
mp.quo = n
mp.header=Hecke.MapHeader(G, mC.header.codomain, exp1, disclog1)
mp.princ_gens = Tuple{FacElem{AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, AbsNumFieldOrderIdealSet{AbsSimpleNumField, AbsSimpleNumFieldElem}}, FacElem{AbsSimpleNumFieldElem, AbsSimpleNumField}}[]
return G, mp
end
ind = findfirst(x -> !is_coprime(x, n), C.snf)
diff = ppio(exponent(C), ZZRingElem(n))[2]
invariants = ZZRingElem[ppio(x, ZZRingElem(n))[1] for x in C.snf[ind:end]]
#filter!(!isone, invariants)
G = abelian_group(invariants)
local exp2
let O = O, G = G
function exp2(a::FinGenAbGroupElem)
@assert parent(a) === G
new_coeff = zero_matrix(FlintZZ, 1, ngens(C))
for i = 1:ngens(G)
new_coeff[1, i+ind-1] = a[i]*diff
end
return mC(C(new_coeff))
end
end
local disclog2
let G = G, mC = mC, C = C, diff = diff
idiff = invmod(diff, exponent(G))
function disclog2(I::AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem})
if I.is_principal == 1
return id(G)
end
x=idiff*(mC\I)
y = zero_matrix(FlintZZ, 1, ngens(G))
for i=ind:ngens(C)
y[1,i-ind+1]=x.coeff[1,i]
end
return FinGenAbGroupElem(G, y)
end
end
mp = Hecke.MapClassGrp()
mp.header = Hecke.MapHeader(G, mC.header.codomain, exp2, disclog2)
mp.quo = Int(exponent(G))
if isdefined(mC, :princ_gens)
princ_gens = Vector{Tuple{FacElem{AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, AbsNumFieldOrderIdealSet{AbsSimpleNumField, AbsSimpleNumFieldElem}}, FacElem{AbsSimpleNumFieldElem, AbsSimpleNumField}}}(undef, length(mC.princ_gens))
for i = 1:length(princ_gens)
princ_gens[i] = (mC.princ_gens[ind+i-1][1]^diff, mC.princ_gens[ind+i-1][2])
end
mp.princ_gens = princ_gens
end
return G, mp
end
################################################################################
#
# Make positive
#
################################################################################
#makes the element x positive at all the embeddings adding a multiple of a
#TODO: Do this properly!
function make_positive(x::AbsSimpleNumFieldOrderElem, a::ZZRingElem)
els = conjugates_real(elem_in_nf(x))
m = ZZRingElem(0)
for i=1:length(els)
if is_positive(els[i])
continue
end
y = abs(lower_bound(els[i], ZZRingElem))
mu = div(y, a)
m = max(m, mu+1)
end
#@hassert :RayFacElem 1 is_coprime(ideal(parent(x),x), ideal(parent(x), a))
#@hassert :RayFacElem 1 is_coprime(ideal(parent(x),x+ZZRingElem(m)*a), ideal(parent(x), a))
@hassert :RayFacElem 1 is_totally_positive(x+m*a)
el_to_add = m*a
return x+el_to_add
end
###################################################################################
#
# Narrow Class Group
#
###################################################################################
@doc raw"""
narrow_class_group(O::AbsSimpleNumFieldOrder) -> FinGenAbGroup, Map
Computes the narrow (or strict) class group of $O$, ie. the group of invertable
ideals modulo principal ideals generated by elements that are
positive at all real places.
"""
function narrow_class_group(O::AbsSimpleNumFieldOrder)
@assert is_maximal_known_and_maximal(O)
K = nf(O)
plc = real_places(K)
return ray_class_group(ideal(O, 1), real_places(K))
end
###################################################################################
#
# Ray Class Group
#
###################################################################################
function ray_class_group(O::AbsSimpleNumFieldOrder, D::Dict{AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, Int}, inf_plc::Vector{<:InfPlc} = InfPlc[]; n_quo::Int = -1, GRH::Bool = true)
I = ideal(O, 1)
minI = ZZRingElem(1)
for (p, v) in D
q = p^v
I *= q
minI = lcm(minI, minimum(q))
end
I.minimum = minI
return ray_class_group(I, inf_plc, GRH = GRH, n_quo = n_quo, lp = D)
end
#
# We compute the group using the sequence U -> (O/m)^* _> Cl^m -> Cl -> 1
#
@doc raw"""
ray_class_group(m::AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, inf_plc::Vector{InfPlc}; n_quo::Int, lp::Dict{AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, Int}) -> FinGenAbGroup, MapRayClassGrp
Given an ideal $m$ and a set of infinite places of $K$,
this function returns the corresponding ray class group as an abstract group $\mathcal {Cl}_m$ and a map going
from the group into the group of ideals of $K$ that are coprime to $m$.
If `n_quo` is set, it will return the group modulo `n_quo`. The factorization of $m$ can be given with the keyword argument `lp`.
"""
function ray_class_group(m::AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, inf_plc::Vector{<:InfPlc} = Vector{InfPlc{AbsSimpleNumField, AbsSimpleNumFieldEmbedding}}(); GRH::Bool = true, n_quo::Int = -1, lp::Dict{AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, Int} = factor(m))
O = order(m)
K = nf(O)
C, mC = class_group(O, GRH = GRH)
expC = exponent(C)
diffC = ZZRingElem(1)
if n_quo != -1
C, mC = n_part_class_group(mC, n_quo)
diffC = divexact(expC, exponent(C))
expC = exponent(C)
end
if isone(m) && isempty(inf_plc)
local exp_c
let mC = mC
function exp_c(a::FinGenAbGroupElem)
return FacElem(Dict(mC(a) => 1))
end
end
return class_as_ray_class(C, mC, exp_c, m, n_quo)
end
_assure_princ_gen(mC)
exp_class, Kel = find_coprime_representatives(mC, m, lp)
if n_quo != -1
powers = Vector{Tuple{AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}}}()
quo_rings = Tuple{AbsSimpleNumFieldOrderQuoRing, Hecke.AbsOrdQuoMap{AbsNumFieldOrder{AbsSimpleNumField,AbsSimpleNumFieldElem},AbsNumFieldOrderIdeal{AbsSimpleNumField,AbsSimpleNumFieldElem},AbsSimpleNumFieldOrderElem}}[]
groups_and_maps = Tuple{FinGenAbGroup, Hecke.GrpAbFinGenToAbsOrdQuoRingMultMap{AbsNumFieldOrder{AbsSimpleNumField,AbsSimpleNumFieldElem},AbsNumFieldOrderIdeal{AbsSimpleNumField,AbsSimpleNumFieldElem},AbsSimpleNumFieldOrderElem}}[]
for (pp, vv) in lp
dtame = Dict{AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, Int}()
dwild = Dict{AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, Int}()
npp = norm(pp)
qq = ideal(O, 1)
if !is_coprime(npp-1, n_quo)
dtame[pp] = 1
qq = pp
end
if vv > 1 && !is_coprime(npp, n_quo)
dwild[pp] = vv
qq = pp^vv
end
if isempty(dtame) && isempty(dwild)
continue
end
push!(powers, (pp, qq))
Q, mQ = quo(O, qq)
if pp == qq
Q.factor = dtame
else
Q.factor = dwild
end
push!(quo_rings, (Q, mQ))
push!(groups_and_maps, _mult_grp_mod_n(quo_rings[end][1], dtame, dwild, n_quo))
end
else
powers = Tuple{AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}}[(p, p^v) for (p, v) in lp]
quo_rings = Tuple{AbsSimpleNumFieldOrderQuoRing, Hecke.AbsOrdQuoMap{AbsNumFieldOrder{AbsSimpleNumField,AbsSimpleNumFieldElem},AbsNumFieldOrderIdeal{AbsSimpleNumField,AbsSimpleNumFieldElem},AbsSimpleNumFieldOrderElem}}[quo(O, q) for (p, q) in powers]
groups_and_maps = Tuple{FinGenAbGroup, Hecke.GrpAbFinGenToAbsOrdQuoRingMultMap{AbsNumFieldOrder{AbsSimpleNumField,AbsSimpleNumFieldElem},AbsNumFieldOrderIdeal{AbsSimpleNumField,AbsSimpleNumFieldElem},AbsSimpleNumFieldOrderElem}}[_multgrp(x[1], true) for x in quo_rings]
end
if isempty(groups_and_maps)
nG = 0
expon = ZZRingElem(1)
else
nG = sum(ngens(x[1]) for x in groups_and_maps)
expon = lcm([exponent(x[1]) for x in groups_and_maps])
end
if n_quo == -1 || iseven(n_quo)
p = filter(is_real, inf_plc)
else
p = InfPlc[]
end
H, eH, lH = sign_map(O, _embedding.(p), m)
expon = lcm(expon, exponent(H))
U, mU = unit_group_fac_elem(O, GRH = GRH)
# We construct the relation matrix and evaluate units and relations with the class group in the quotient by m
# Then we compute the discrete logarithms
if n_quo == -1
R = zero_matrix(FlintZZ, ngens(C)+nG+ngens(H)+ngens(U), ngens(H)+ngens(C)+nG)
else
R = zero_matrix(FlintZZ, 2*(ngens(C)+nG+ngens(H))+ngens(U), ngens(C)+ngens(H)+nG)
for i = 1:ncols(R)
R[i+ngens(C)+nG+ngens(H)+ngens(U), i] = n_quo
end
end
for i=1:ngens(C)
R[i,i] = C.snf[i]
end
ind = 1
for s = 1:length(quo_rings)
G = groups_and_maps[s][1]
@assert is_snf(G)
for i = 1:ngens(G)
R[i+ngens(C)+ind-1, i+ngens(C)+ind-1] = G.snf[i]
end
ind += ngens(G)
end
for i = 1:ngens(H)
R[ngens(C)+nG+i, ngens(C)+nG+i] = 2
end
@vprintln :RayFacElem 1 "Collecting elements to be evaluated; first, units"
tobeeval1 = Vector{FacElem{AbsSimpleNumFieldElem, AbsSimpleNumField}}(undef, ngens(U)+ngens(C))
for i = 1:ngens(U)
tobeeval1[i] = mU(U[i])
end
for i = 1:ngens(C)
tobeeval1[i+ngens(U)] = mC.princ_gens[i][2]*(FacElem(Dict(Kel[i] => C.snf[i])))
end
tobeeval = _preproc(m, tobeeval1, expon)
ind = 1
for i = 1:length(groups_and_maps)
exp_q = gcd(expon, norm(powers[i][2])- divexact(norm(powers[i][2]), norm(powers[i][1])))
@vtime :RayFacElem 3 evals = fac_elems_eval(powers[i][1], powers[i][2], tobeeval, exp_q)
Q = quo_rings[i][1]
mG = groups_and_maps[i][2]
for j = 1:ngens(U)
a = (mG\Q(evals[j])).coeff
for s = 1:ncols(a)
R[j+nG+ngens(H)+ngens(C), ngens(C)+s+ind-1] = a[1, s]
end
end
for j = 1:ngens(C)
a = (mG\Q(evals[j+ngens(U)])).coeff
for s = 1:ncols(a)
R[j, ngens(C)+ind+s-1] = -a[1, s]
end
end
ind += ngens(groups_and_maps[i][1])
end
if !isempty(p)
for j = 1:ngens(U)
a = lH(tobeeval1[j]).coeff
for s = 1:ncols(a)
R[j+nG+ngens(C)+ngens(H), ngens(C)+ind-1+s] = a[1, s]
end
end
for j = 1:ngens(C)
a = lH(tobeeval1[j+ngens(U)]).coeff
for s = 1:ncols(a)
R[j, ngens(C)+ind-1+s] = -a[1, s]
end
end
end
X = abelian_group(R)
if n_quo != -1
X.exponent = n_quo
end
local disclog
let X = X, mC = mC, C = C, exp_class = exp_class, powers = powers, groups_and_maps = groups_and_maps, quo_rings = quo_rings, lH = lH, diffC = diffC, n_quo = n_quo, m = m, expon = expon
invd = invmod(ZZRingElem(diffC), expon)
# Discrete logarithm
function disclog(J::FacElem{AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, AbsNumFieldOrderIdealSet{AbsSimpleNumField, AbsSimpleNumFieldElem}})
@vprintln :RayFacElem 1 "Disc log of element $J"
a = id(X)
for (f, k) in J
a += k*disclog(f)
end
return a
end
function disclog(J::AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem})
@hassert :RayFacElem 1 is_coprime(J, m)
if isone(J)
@vprintln :RayFacElem 1 "J is one"
return id(X)
end
coeffs = zero_matrix(FlintZZ, 1, ngens(X))
if J.is_principal == 1 && isdefined(J, :princ_gen)
z = FacElem(Dict(J.princ_gen.elem_in_nf => diffC))
else
L = mC\J
for i = 1:ngens(C)
coeffs[1, i] = L[i]
end
@vprintln :RayFacElem 1 "Disc log of element J in the Class Group: $(L.coeff)"
s = exp_class(L)
inv!(s)
add_to_key!(s.fac, J, 1)
pow!(s, diffC)
@vprintln :RayFacElem 1 "This ideal is principal: $s"
z = principal_generator_fac_elem(s)
end
ii = 1
z1 = _preproc(m, z, expon)
for i = 1:length(powers)
P, Q = powers[i]
exponq = gcd(expon, norm(Q)-divexact(norm(Q), norm(P)))
el = fac_elems_eval(P, Q, FacElem{AbsSimpleNumFieldElem, AbsSimpleNumField}[z1], exponq)
y = (invd*(groups_and_maps[i][2]\quo_rings[i][1](el[1]))).coeff
for s = 1:ncols(y)
coeffs[1, ii-1+ngens(C)+s] = y[1, s]
end
ii += ngens(groups_and_maps[i][1])
end
if !isempty(p)
b = lH(z).coeff
for s = 1:ncols(b)
coeffs[1, ii-1+s+ngens(C)] = b[1, s]
end
end
return FinGenAbGroupElem(X, coeffs)
end
end
Dgens = Tuple{AbsSimpleNumFieldOrderElem, FinGenAbGroupElem}[]
ind = 1
#For the exponential map and other purposes, we need generators of the full multiplicative group
#In particular, we need the idempotents...
for i = 1:length(powers)
P, Q = powers[i]
mG = groups_and_maps[i][2]
J = ideal(O, 1)
minJ = ZZRingElem(1)
mins = ZZRingElem(1)
for (PP, vPP) in lp
if minimum(PP, copy = false) != minimum(P, copy = false)
mins = lcm(mins, minimum(PP, copy = false)^vPP)
continue
end
if PP != P
Jm = PP^vPP
J *= Jm
minJ = lcm(minJ, minimum(Jm))
end
end
J.minimum = minJ
i1, i2 = idempotents(Q, J)
if !isone(mins)
d, u1, v1 = gcdx(minimum(Q, copy = false), mins)
i1 = i1*(u1*minimum(Q, copy = false) + v1*mins) + u1*minimum(Q, copy = false) *i2
i2 = v1*mins*i2
end
gens = mG.generators
if isempty(p)
if haskey(mG.tame, P)
gens_tame = mG.tame[P].generators
for s = 1:length(gens_tame)
gens_tame[s] = gens_tame[s]*i2 + i1
end
mG.tame[P].generators = gens_tame
end
if haskey(mG.wild, P)
gens_wild = mG.wild[P].generators
for s = 1:length(gens_wild)
gens_wild[s] = gens_wild[s]*i2 + i1
end
mG.wild[P].generators = gens_wild
end
for s = 1:length(gens)
push!(Dgens, (gens[s].elem*i2+i1, X[ngens(C)+ind-1+s]))
end
else
if haskey(mG.tame, P)
gens_tame = mG.tame[P].generators
for s = 1:length(gens_tame)
gens_tame[s] = make_positive(gens_tame[s]*i2 + i1, minimum(m, copy = false))
end
mG.tame[P].generators = gens_tame
end
if haskey(mG.wild, P)
gens_wild = mG.wild[P].generators
for s = 1:length(gens_wild)
gens_wild[s] = make_positive(gens_wild[s]*i2 + i1, minimum(m, copy = false))
end
mG.wild[P].generators = gens_wild
end
for s = 1:length(gens)
elgen = make_positive(gens[s].elem*i2 + i1, minimum(m, copy = false))
push!(Dgens, (elgen, X[ngens(C)+ind-1+s]))
end
end
ind += length(gens)
end
local expo
let C = C, O = O, groups_and_maps = groups_and_maps, exp_class = exp_class, eH = eH, H = H, K = K, Dgens = Dgens, X = X, p = p
function expo(a::FinGenAbGroupElem)
@assert parent(a) === X
b = FinGenAbGroupElem(C, sub(a.coeff, 1:1, 1:ngens(C)))
res = exp_class(b)
for i = 1:nG
if !iszero(a.coeff[1, ngens(C)+i])
add_to_key!(res.fac, ideal(O, Dgens[i][1]), a.coeff[1, ngens(C)+i])
end
end
for i = 1:length(p)
if !iszero(a.coeff[i+nG+ngens(C)])
add_to_key!(res.fac, ideal(O, O(1+eH(H[i]))), 1)
end
end
return res
end
end
ind = 1
for i = 1:length(powers)
mG = groups_and_maps[i][2]
for (prim, mprim) in mG.tame
dis = zero_matrix(FlintZZ, 1, ngens(X))
to_be_c = mprim.disc_log.coeff
for i = 1:length(to_be_c)
dis[1, ind-1+i+ngens(C)] = to_be_c[1, i]
end
mprim.disc_log = FinGenAbGroupElem(X, dis)
end
ind += ngens(domain(mG))
end
disc_log_inf = Dict{InfPlc, FinGenAbGroupElem}()
for i = 1:length(p)
eldi = zero_matrix(FlintZZ, 1, ngens(X))
eldi[1, ngens(X) - length(p) + i] = 1
disc_log_inf[p[i]] = FinGenAbGroupElem(X, eldi)
end
mp = MapRayClassGrp()
mp.header = Hecke.MapHeader(X, FacElemMon(parent(m)), expo, disclog)
mp.fact_mod = lp
mp.defining_modulus = (m, inf_plc)
mp.powers = powers
mp.groups_and_maps = groups_and_maps
mp.disc_log_inf_plc = disc_log_inf
mp.gens_mult_grp_disc_log = Dgens
mp.clgrpmap = mC
return X, mp
end
##################################################################################
#
# Ray Class Group over QQ
#
##################################################################################
function ray_class_groupQQ(O::AbsSimpleNumFieldOrder, modulus::Int, inf_plc::Bool, n_quo::Int)
R=residue_ring(FlintZZ, modulus, cached=false)[1]
U, mU = unit_group_mod(R, n_quo)
U.exponent = n_quo
if inf_plc
function disc_log1(I::AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem})
@assert gcd(minimum(I),modulus)==1
i = Int(mod(I.minimum, modulus))
return mU\(R(i))
end
function expon1(a::FinGenAbGroupElem)
@assert parent(a) === domain(mU)
x=mU(a)
return FacElem(Dict{AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}, ZZRingElem}(ideal(O,lift(x)) => 1))
end
mp=Hecke.MapRayClassGrp()
mp.header = Hecke.MapHeader(U, FacElemMon(parent(ideal(O,1))) , expon1, disc_log1)
mp.defining_modulus = (ideal(O, modulus), real_places(nf(O)))
return U, mp
elseif isodd(n_quo)
function disc_log2(I::AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem})
@assert gcd(minimum(I),modulus)==1
i=Int(mod(I.minimum, modulus))
return mU\(R(i))