-
Notifications
You must be signed in to change notification settings - Fork 120
/
GaloisGrp.jl
2956 lines (2596 loc) · 89.3 KB
/
GaloisGrp.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
module GaloisGrp
using Oscar, Random
import Base: ^, +, -, *, ==
import Oscar: Hecke, AbstractAlgebra, GAP, extension_field, isinteger
using Oscar: SLPolyRing, SLPoly, SLPolynomialRing, CycleType
import Oscar: pretty, LowercaseOff
export cauchy_ideal
export elementary_symmetric
export fixed_field
export galois_group
export galois_ideal
export galois_quotient
export power_sum
export slpoly_ring
export to_elementary_symmetric
export upper_bound
export valuation_of_roots
import Hecke: orbit, fixed_field, extension_field
function __init__()
GAP.Packages.load("ferret"; install=true)
Hecke.add_verbosity_scope(:GaloisGroup)
Hecke.add_verbosity_scope(:GaloisInvariant)
Hecke.add_assertion_scope(:GaloisInvariant)
end
"""
A poor mans version of a (range of) tropical rings.
Actually, not even a ring. Defined in terms of operations
- `mul` used for multiplication
- `add` for addition
- `pow` for powering with Int exponent
- `map` to create elements from (large) integers/ ZZRingElem
- `name` is only used for printing.
"""
struct BoundRing{T} <: AbstractAlgebra.Ring
mul#::(T,T) -> T
add#::(T,T) -> T
pow#::(T, Int) -> T
map#:: R -> T
name::String
end
function Base.show(io::IO, b::BoundRing{T}) where {T}
print(io, "$(b.name) for type $T")
end
struct BoundRingElem{T} <: AbstractAlgebra.RingElem
val::T
p::BoundRing # the parent
end
function Base.show(io::IO, b::BoundRingElem)
print(io, "(x <= $(b.val))")
end
function check_parent(a::BoundRingElem, b::BoundRingElem)
parent(a) == parent(b) || error("Elements must have same parent")
return true
end
Base.:(==)(a::BoundRingElem, b::BoundRingElem) = check_parent(a, b) && a.val == b.val
function +(a::BoundRingElem, b::BoundRingElem)
check_parent(a, b)
c = BoundRingElem(a.p.add(a.val, b.val), a.p)
# @show a, "+", b, "=", c
return c
end
-(a::BoundRingElem, b::BoundRingElem) = check_parent(a, b) && BoundRingElem(a.p.add(a.val, b.val), a.p)
function *(a::BoundRingElem, b::BoundRingElem)
check_parent(a, b)
c = BoundRingElem(a.p.mul(a.val, b.val), a.p)
# @show a, "*", b, ":=", c
return c
end
function *(a::ZZRingElem, b::BoundRingElem)
c = BoundRingElem(b.p.mul(b.p.map(a), b.val), b.p)
# @show a, ":*", b, ":=", c
return c
end
function ^(a::BoundRingElem, b::Int)
c = BoundRingElem(a.p.pow(a.val, b), a.p)
# @show a, ":^", b, ":=", c
return c
end
-(a::BoundRingElem) = a
Oscar.parent(a::BoundRingElem) = a.p
value(a::BoundRingElem) = a.val
Base.isless(a::BoundRingElem, b::BoundRingElem) = check_parent(a, b) && isless(value(a), value(b))
Oscar.parent_type(::Type{BoundRingElem{T}}) where T = BoundRing{T}
Oscar.elem_type(::Type{BoundRing{T}}) where T = BoundRingElem{T}
(R::BoundRing{T})(a::ZZRingElem) where T = BoundRingElem{T}(R.map(abs(a)), R)
(R::BoundRing{T})(a::Integer) where T = BoundRingElem{T}(ZZRingElem(a), R)
(R::BoundRing{T})() where T = BoundRingElem{T}(ZZRingElem(0), R)
(R::BoundRing{T})(a::T) where T = BoundRingElem{T}(a, R)
(R::BoundRing{T})(a::BoundRingElem{T}) where T = a
Oscar.one(R::BoundRing) = R(1)
Oscar.zero(R::BoundRing) = R(0)
"""
Intended to be used to get upper bounds under evaluation in function fields (non
Archimedean valuaton, degree)
Operations are:
- `a+b := max(a, b)`
- `ab := a+b`
"""
function max_ring()
return BoundRing{ZZRingElem}( (x,y) -> x+y, (x,y) -> max(x, y), (x,y) -> y*x, x->x, "max-ring")
end
"""
Normal ring
"""
function add_ring(;type::Type=ZZRingElem)
return BoundRing{type}( (x,y) -> x*y, (x,y) -> x+y, (x,y) -> x^y, x->abs(x), "add-ring")
end
#roots rt are power series sum a_n x^n
#we have |a_n| <= r^-n B (n+1)^k for B = x[1], k = x[2]
#and deg(rt) <= x[3] (infinite valuation)
function qt_ring()
return BoundRing{Tuple{ZZRingElem, Int, QQFieldElem}}( (x,y) -> (x[1]*y[1], x[2]+y[2]+1, x[3]+y[3]),
(x,y) -> (x[1]+y[1], max(x[2], y[2]), max(x[3], y[3])),
(x,y) -> (x[1]^y, y*x[2]+y-1, y*x[3]),
x -> _coerce_qt(x), "qt-ring")
end
_coerce_qt(x::ZZRingElem) = (abs(x), 0, QQFieldElem(0))
_coerce_qt(x::Integer) = (ZZRingElem(x), 0, QQFieldElem(0))
function _coerce_qt(x::ZZPolyRingElem)
if iszero(x)
return (ZZRingElem(0), 0, QQFieldElem(0))
end
return (maximum(abs, coefficients(x))*(degree(x)+1), 0, QQFieldElem(0))
end
(R::BoundRing{Tuple{ZZRingElem, Int, QQFieldElem}})(a::Tuple{ZZRingElem, Int, QQFieldElem}) = BoundRingElem(a, R)
(R::BoundRing{Tuple{ZZRingElem, Int, QQFieldElem}})(a::Integer) = BoundRingElem(R.map(a), R)
(R::BoundRing{Tuple{ZZRingElem, Int, QQFieldElem}})(a::ZZRingElem) = BoundRingElem(R.map(a), R)
(R::BoundRing{Tuple{ZZRingElem, Int, QQFieldElem}})(a::ZZPolyRingElem) = BoundRingElem(R.map(a), R)
"""
An slpoly evaluated at `cost_ring` elements `0` will count the number
of multiplications involved. A measure of the cost of evaluation at more
interesting scalars.
Operations:
- `xy := x+y+1`
- `x+y := x+y`
- `x^y := x+2*log_2(y)`
- all constants are mapped to `0`
"""
function cost_ring()
return BoundRing{ZZRingElem}( (x,y) -> x+y+1, (x,y) -> x+y, (x,y) -> x+2*nbits(y), x->0, "cost-ring")
end
"""
An slpoly evaluated at `degree_ring` elements `1` will bound the total degree
from above.
Operations:
- `xy := x+y`
- `x+y := max(x,y)`
- `x^y := yx`
- all constants are mapped to `0`
"""
function degree_ring()
return BoundRing{ZZRingElem}( (x,y) -> x+y, (x,y) -> max(x, y), (x,y) -> y*x, x->0, "degree-ring")
end
@doc raw"""
cost(I::SLPoly)
Counts the number of multiplications to evaluate `I`, optionally
a Tschirnhaus transformation (`ZZPolyRingElem`) can be passed in as well.
"""
function cost(I::SLPoly)
n = ngens(parent(I))
C = cost_ring()
return value(evaluate(I, [C(0) for i = 1:n]))
end
function cost(I::SLPoly, ts::ZZPolyRingElem)
n = ngens(parent(I))
C = cost_ring()
return value(evaluate(I, [C(0) for i = 1:n]))+n*degree(ts)
end
@doc raw"""
total_degree(I::SLPoly)
Determines an upper bound for the total degree of `I`.
"""
function total_degree(I::SLPoly)
n = ngens(parent(I))
C = degree_ring()
return value(evaluate(I, [C(1) for i = 1:n]))
end
Oscar.mul!(a::BoundRingElem, b::BoundRingElem, c::BoundRingElem) = b*c
Oscar.addeq!(a::BoundRingElem, b::BoundRingElem) = a+b
#my 1st invariant!!!
@doc raw"""
sqrt_disc(a::Vector)
The product of differences ``a[i] - a[j]`` for all indices ``i<j``.
"""
function sqrt_disc(a::Vector)
if length(a) == 1
return one(parent(a[1]))
end
return prod([a[i] - a[j] for i = 1:length(a)-1 for j = i+1:length(a)])
end
@doc raw"""
elementary_symmetric(g::Vector, i::Int)
Evaluates the `i`-th elementary symmetric polynomial at the values in `g`.
The `i`-th elementary symmetric polynomial is the sum over all
products of `i` distinct variables.
"""
function elementary_symmetric(g::Vector, i::Int)
return sum(prod(g[i] for i = s) for s = Hecke.subsets(Set(1:length(g)), i))
end
@doc raw"""
power_sum(g::Vector, i::Int)
Evaluates the `i`-th power sums at the values in `g`, ie. the sum
of the `i`-th power of the values.
"""
function power_sum(g::Vector, i::Int)
return sum(a^i for a = g)
end
@doc raw"""
discriminant(g::Vector)
Compute the product of all differences of distinct elements in the array.
"""
function Oscar.discriminant(g::Vector{<:RingElem})
return prod(a-b for a = g for b = g if a!=b)
end
function slpoly_ring(R::AbstractAlgebra.Ring, n::Int; cached::Bool = false)
return SLPolynomialRing(R, [ Symbol("x_$i") for i=1:n], cached = cached)
end
function slpoly_ring(R::AbstractAlgebra.Ring, p::Pair{Symbol, <:AbstractVector{Int}}...; cached::Bool = false)
return SLPolynomialRing(R, p..., cached = cached)
end
function (R::SLPolyRing)(a::SLPoly)
parent(a) == R && return a
error("wrong parent")
end
@doc raw"""
roots_upper_bound(f::ZZPolyRingElem) -> ZZRingElem
An upper upper_bound for the absolute value of the complex roots of the input.
Uses the Cauchy bound.
"""
function Nemo.roots_upper_bound(f::ZZPolyRingElem)
a = coeff(f, degree(f))
b = ceil(ZZRingElem, abs(coeff(f, degree(f)-1)//a))
for i=0:degree(f)-2
b = max(b, iroot(ceil(ZZRingElem, abs(coeff(f, i)//a)), degree(f)-i)+1)
end
return 2*b
return max(ZZRingElem(1), maximum([ceil(ZZRingElem, abs(coeff(f, i)//a)) for i=0:degree(f)]))
end
function Nemo.roots_upper_bound(f::QQPolyRingElem)
a = coeff(f, degree(f))
return max(ZZRingElem(1), maximum([ceil(ZZRingElem, abs(coeff(f, i)//a)) for i=0:degree(f)]))
end
#roots are sums of m distinct roots of f
#from https://doi.org/10.2307/2153295
#Symmetric Functions, m-Sets, and Galois Groups
#by David Casperson and John McKay
@doc raw"""
msum_poly(f::PolyRingElem, m::Int)
Compute the polynomial with roots sums of `m` roots of `f` using
resultants.
"""
function msum_poly(f::PolyRingElem, m::Int)
f = divexact(f, leading_coefficient(f))
N = binomial(degree(f), m)
p = Hecke.polynomial_to_power_sums(f, N)
p = vcat([degree(f)*one(base_ring(f))], p)
S, a = power_series_ring(base_ring(f), N+1, "a")
Hfs = S([p[i]//factorial(ZZRingElem(i-1)) for i=1:length(p)], N+1, N+1, 0)
H = [S(1), Hfs]
for i=2:m
push!(H, 1//i*sum((-1)^(h+1)*Hfs(h*a)*H[i-h+1] for h=1:i))
end
p = [coeff(H[end], i)*factorial(ZZRingElem(i)) for i=0:N]
return Hecke.power_sums_to_polynomial(p[2:end])
end
@doc raw"""
A `GaloisCtx`, is the context object used for the computation of Galois
groups of (univariate) polynomials. It contains
- the polynomial
- an object that can compute the roots in a fixed order up to a given
precision. Currently, this is a q-adic field, that is an unramified
extension of the p-adic numbers.
- a upper bound on the _size_ of the roots, currently an upper bound on the
complex absolute value.
- at the end, the Galois group
This is constructed implicitly while computing a Galois group and returned
together with the group.
Not type stable, not sure what to do about it:
Depends on type of
- `f`
However, `f` is hardly ever used.
"""
mutable struct GaloisCtx{T}
f::PolyRingElem
C::T # a suitable root context
B::BoundRingElem # a "bound" on the roots, might be "anything"
G::PermGroup
rt_num::Dict{Int, Int}
chn::Vector{Tuple{PermGroup, SLPoly, ZZPolyRingElem, Vector{PermGroupElem}}}
start::Tuple{Int, Vector{Vector{Vector{Int}}}} # data for the starting group:
# if start[1] == 1: start[2] is a list of the block systems used
# == 2 start[2] is a list of the orbits of the msum-poly, ie. a list of a list of pairs
# where the pairs are Vector{Int} of length 2
data::Any #whatever else is needed in special cases
#= the descent chain, recording
- the group
- the invariant
- the tschirnhaus transformation
- the cosets used
should probably also record if the step was proven or not
the starting group and the block systems used to get them
=#
prime::Any #=can be
- ZZRingElem/ Int: prime number, used over Q
- AbsSimpleNumFieldOrderIdeal : prime ideal , used over NfAbs
- (Int, Int): evaluation point, prime number used over Q(t)
=#
function GaloisCtx(f::ZZPolyRingElem, ::AcbField)
r = new{ComplexRootCtx}()
r.f = f
r.C = ComplexRootCtx(f)
r.B = add_ring()(leading_coefficient(f)*roots_upper_bound(f))
r.chn = Tuple{PermGroup, SLPoly, ZZPolyRingElem, Vector{PermGroupElem}}[]
return r
end
function GaloisCtx(f::QQPolyRingElem, x::AcbField)
return GaloisCtx(numerator(f), x)
end
function GaloisCtx(f::ZZPolyRingElem, p::Int)
r = new{Hecke.qAdicRootCtx}()
r.prime = p
r.f = f
r.C = Hecke.qAdicRootCtx(f, p, splitting_field = true)
r.B = add_ring()(leading_coefficient(f)*roots_upper_bound(f))
r.chn = Tuple{PermGroup, SLPoly, ZZPolyRingElem, Vector{PermGroupElem}}[]
return r
end
function GaloisCtx(f::ZZPolyRingElem, field::Union{Nothing, AbsSimpleNumField})
r = new{SymbolicRootCtx}()
r.f = f
r.C = SymbolicRootCtx(f, field)
r.B = add_ring()(leading_coefficient(f)*roots_upper_bound(f))
r.chn = Tuple{PermGroup, SLPoly, ZZPolyRingElem, Vector{PermGroupElem}}[]
return r
end
function GaloisCtx(f::QQPolyRingElem, p::Int)
d = mapreduce(denominator, lcm, coefficients(f))
return GaloisCtx(Hecke.Globals.Zx(d*f), p)
end
#=
Roots in F_q[[t]] for q = p^d
- needs to be tweaked to do Q_q[[t]
- need q-lifting as well as t-lifting
- possibly also mul_ks for Q_q[[t]] case
- needs merging in Hecke
=#
function GaloisCtx(f::ZZMPolyRingElem, shft::Int, p::Int, d::Int)
f = evaluate(f, [gen(parent(f), 1), gen(parent(f), 2)+shft])
#f(x, T+t), the roots are power series in T over qAdic(p, d)
#so basically for f in Qq<<(T+t)>>
@assert ngens(parent(f)) == 2
Qq, _ = QadicField(p, d, 10)
F, mF = residue_field(Qq)
H = Hecke.MPolyFact.HenselCtxFqRelSeries(f, F)
SQq, _ = power_series_ring(Qq, 2, "s", cached = false)
SQqt, _ = polynomial_ring(SQq, "t", cached = false)
mc(f) = map_coefficients(x->map_coefficients(y->setprecision(preimage(mF, y), 1), x, parent = SQq), f, parent = SQqt)
HQ = Hecke.MPolyFact.HenselCtxFqRelSeries(H.f, map(mc, H.lf), map(mc, H.cf), H.n)
r = new{Hecke.MPolyFact.HenselCtxFqRelSeries{AbstractAlgebra.Generic.RelSeries{QadicFieldElem}}}()
r.prime = (shft, p)
Qt, t = rational_function_field(QQ, "t", cached = false)
Qts, s = polynomial_ring(Qt, "s", cached = false)
r.f = evaluate(f, [s, Qts(t)])
r.C = HQ
r.chn = Tuple{PermGroup, SLPoly, ZZPolyRingElem, Vector{PermGroupElem}}[]
vl = roots_upper_bound(f)
r.B = qt_ring()(vl[1])
r.data = Any[vl[2], shft, false] #false: not simulating C
@assert typeof(r.data[3]) == Bool
return r
end
function GaloisCtx(T::Type)
r = new{T}()
r.chn = Tuple{PermGroup, SLPoly, ZZPolyRingElem, Vector{PermGroupElem}}[]
return r
end
end
function Oscar.prime(C::GaloisCtx{Hecke.MPolyFact.HenselCtxFqRelSeries{Generic.RelSeries{QadicFieldElem}}})
return prime(base_ring(base_ring(C.C.lf[1])))
end
function bound_to_precision(G::GaloisCtx{T}, B::BoundRingElem{Tuple{ZZRingElem, Int, QQFieldElem}}, extra=(0, 0)) where {T}
if isa(extra, Int)
extra = (extra, min(2, div(extra, 3)))
end
C, k, d = B.val
r = G.data[1]
#so power series prec need to be floor(Int, d)
n = floor(ZZRingElem, d+1)
#padic: we ne |a_i| for i=0:n and |a_i| <= C (i+1)^k/r^i
#and then log_p()
#according to the Qt file, a_i is maximal around k/log(r) -1
if G.data[3] #we're simulating CC, so we don't care about the p-adics (too much)
b = max(C, ZZRingElem(10)^10)
elseif isone(r)
b = C*(n+1)^k
else
c = max(1, floor(Int, k/log(r)-1))
if n<c
b = C*(n+1)^k//r^n
else
b = max(C*(c+1)^k//r^c, C*(c+2)^k//r^(c+1))
end
end
b = max(b, ZZRingElem(1))
return (clog(floor(ZZRingElem, b), prime(G))+extra[1], Int(n)+extra[2])
end
function bound_to_precision(G::GaloisCtx{T}, B::BoundRingElem{ZZRingElem}, extra::Int = 5) where {T}
return clog(B.val, G.C.p)+extra
end
mutable struct ComplexRootCtx
f::ZZPolyRingElem
pr::Int
rt::Vector{AcbFieldElem}
function ComplexRootCtx(f::ZZPolyRingElem)
@assert is_monic(f)
rt = roots(AcbField(20), f)
return new(f, 20, rt)
end
function ComplexRootCtx(f::QQPolyRingElem)
return ComplexRootCtx(numerator(f))
end
end
function Base.show(io::IO, GC::GaloisCtx{ComplexRootCtx})
print(pretty(io), LowercaseOff(), "Galois context for $(GC.f) using complex roots")
end
function Hecke.roots(C::GaloisCtx{ComplexRootCtx}, pr::Int = 10; raw::Bool = false)
if C.C.pr >= pr
return C.C.rt
end
rt = roots(AcbField(pr), C.C.f)
C.C.pr = pr
n = length(rt)
for i=1:n
C.C.rt[i] = rt[argmin([abs(C.C.rt[i] - rt[x]) for x = 1:n])]
end
return C.C.rt
end
function isinteger(GC::GaloisCtx{ComplexRootCtx}, B::BoundRingElem, e)
if abs(imag(e)) > 1e-10
return false, ZZRingElem(0)
end
r = round(ZZRingElem, real(e))
if abs(real(e)-r) > 1e-10
return false, ZZRingElem(0)
else
return true, r
end
end
function bound_to_precision(G::GaloisCtx{ComplexRootCtx}, B::BoundRingElem{ZZRingElem}, extra::Int = 5)
return 2*clog(B.val, 2) + 10
end
function map_coeff(G::GaloisCtx{ComplexRootCtx}, a::QQFieldElem)
return parent(G.C.rt[1])(a)
end
function Hecke.MPolyFact.block_system(a::Vector{AcbFieldElem}, eps = 1e-9)
b = Dict{Int, Vector{Int}}()
for i=1:length(a)
cb = collect(keys(b))
fl = findfirst(x->abs(a[i] - a[x]) < eps, cb)
if fl === nothing
b[i] = [i]
else
push!(b[cb[fl]], i)
end
end
bs = sort(collect(values(b)), lt = (a,b) -> isless(a[1], b[1]))
return bs
end
mutable struct SymbolicRootCtx
f::ZZPolyRingElem
rt::Vector{AbsSimpleNumFieldElem}
function SymbolicRootCtx(f::ZZPolyRingElem, ::Nothing)
@assert is_monic(f)
_, rt = splitting_field(f, do_roots = true)
return new(f, rt)
end
function SymbolicRootCtx(f::ZZPolyRingElem, field::AbsSimpleNumField)
@assert is_monic(f)
rt = roots(f, field)
return new(f, rt)
end
function SymbolicRootCtx(f::QQPolyRingElem)
return SymbolicRootCtx(numerator(f), nothing)
end
end
function Base.show(io::IO, GC::GaloisCtx{SymbolicRootCtx})
print(pretty(io), LowercaseOff(), "Galois context for $(GC.f) using symbolic roots")
end
function Hecke.roots(C::GaloisCtx{SymbolicRootCtx}, ::Int; raw::Bool = false)
return C.C.rt
end
function isinteger(GC::GaloisCtx{SymbolicRootCtx}, B::BoundRingElem, e)
if Oscar.is_integer(e)
return true, ZZ(e)
else
return false, ZZRingElem(0)
end
end
function bound_to_precision(G::GaloisCtx{SymbolicRootCtx}, B::BoundRingElem{ZZRingElem}, extra::Int = 5)
return 1
end
function map_coeff(G::GaloisCtx{SymbolicRootCtx}, a::QQFieldElem)
return parent(G.C.rt[1])(a)
end
function Nemo.roots_upper_bound(f::ZZMPolyRingElem, t::Int = 0)
@assert nvars(parent(f)) == 2
Qs, s = rational_function_field(FlintQQ, "t", cached = false)
Qsx, x = polynomial_ring(Qs, cached = false)
F = evaluate(f, [x, Qsx(s)])
dis = numerator(discriminant(F))
@assert !iszero(dis(t))
rt = roots(AcbField(20), dis)
r = Hecke.lower_bound(minimum([abs(x-t) for x = rt]), ZZRingElem)
@assert r > 0
ff = map_coefficients(abs, f)
C = roots_upper_bound(Hecke.Globals.Zx(map(x->evaluate(x, ZZRingElem[r, 0]), coefficients(ff, 2))))
C1 = maximum(map(x->evaluate(x, ZZRingElem[r, 0]), coefficients(ff, 2)))
#the infinite valuation... need Newton
vl = valuations_of_roots(F)
return (C+1, 0, maximum(x[1] for x = vl)), r
end
function Base.show(io::IO, GC::GaloisCtx{Hecke.qAdicRootCtx})
print(pretty(io), LowercaseOff(), "Galois context for $(GC.f) and prime $(GC.C.p)")
end
function Base.show(io::IO, GC::GaloisCtx{<:Hecke.MPolyFact.HenselCtxFqRelSeries})
print(pretty(io), LowercaseOff(), "Galois context for $(GC.f)")
end
#TODO: change pr to be a "bound_ring_elem": in the Qt case this has to handle
# both power series prec as well as q-adic...
@doc raw"""
roots(G::GaloisCtx, pr::Int)
The roots of the polynomial used to define the Galois context in the fixed order
used in the algorithm. The roots are returned up to a precision of `pr`
p-adic digits, thus they are correct modulo ``p^{pr}``
For non-monic polynomials the roots are scaled by the leading coefficient.
If `raw` is set to true, the scaling is omitted.
The bound in the `GaloisCtx` is also adjusted.
"""
function Hecke.roots(G::GaloisCtx{Hecke.qAdicRootCtx}, pr::Int=5; raw::Bool = false)
a = Hecke.roots(G.C, pr)::Vector{QadicFieldElem}
b = Hecke.expand(a, all = true, flat = false, degs = Hecke.degrees(G.C.H))::Vector{QadicFieldElem}
if isdefined(G, :rt_num)
b = [b[G.rt_num[i]] for i=1:length(G.rt_num)]
end
if raw
return b
else
return leading_coefficient(G.f) .* b
end
end
function Hecke.setprecision(a::Generic.RelSeries, p::Int)
b = parent(a)(a.coeffs, min(length(a.coeffs), p), p+valuation(a), valuation(a))
end
#TODO: does not really work: the polynomial is not truncated, only
# the entry for precision is updated: 1 + ... + s^40 + O(s^2)
function Hecke.setprecision!(G::GaloisCtx{<:Hecke.MPolyFact.HenselCtxFqRelSeries}, pr::Tuple{Int, Int})
c = coeff(G.C.lf[1], 0)
@assert precision(c) >= pr[2]
@assert precision(coeff(c, 0)) >= pr[1]
for f = G.C.lf
Hecke.set_precision!(f, pr[2])
end
for f = G.C.cf
Hecke.set_precision!(f, pr[2])
end
end
function Hecke.roots(G::GaloisCtx{<:Hecke.MPolyFact.HenselCtxFqRelSeries}, pr::Tuple{Int, Int} = (5, 2); raw::Bool = false)
C = G.C
while precision(C)[1] < pr[1]
Hecke.MPolyFact.lift_q(C)
end
#TODO: truncate precision where necessary, working with an insane
# series precision is costly
while precision(C)[2] < pr[2]
Hecke.MPolyFact.lift(C)
end
rt = [-coeff(x, 0) for x = C.lf[1:C.n]]
rt = map(y->map_coefficients(x->setprecision(x, pr[1]), setprecision(y, pr[2]), parent = parent(y)), rt)
if isdefined(G, :rt_num)
rt = [rt[G.rt_num[i]] for i=1:length(G.rt_num)]
end
return rt
end
@doc raw"""
upper_bound(G::GaloisCtx, f...)
Given a `GaloisCtx` and some multivariate function, upper_bound the image of `f`
upon evaluation at the roots implicit in `G`.
`f` can be
- a multivariate polynomial or straight-line polynomial (strictly: any object
allowing `evaluate`
- `elementary_symmetric` or `power_sum`, in which case more arguments are
needed: the array with the values and the index.
`upper_bound(G, power_sum, A, i)` is equivalent to `upper_bound(G, power_sum(A, i))`
but more efficient.
In every case a univariate polynomial (over the integers) can be added, it
will act as a Tschirnhaus-transformation, ie. the roots (bounds) implicit
in `G` will first be transformed.
"""
function upper_bound end
function upper_bound(G::GaloisCtx, f)
# @show :eval, f, G.B, degree(G.f)
return Oscar.evaluate(f, [G.B for i=1:degree(G.f)])
end
function upper_bound(G::GaloisCtx, f, ts::ZZPolyRingElem)
B = ts(G.B)
return Oscar.evaluate(f, [B for i=1:degree(G.f)])
end
function upper_bound(G::GaloisCtx, ::typeof(elementary_symmetric), A::Vector, i::Int, ts::ZZPolyRingElem = gen(Oscar.Hecke.Globals.Zx))
if ts != gen(Hecke.Globals.Zx)
A = [ts(x) for x = A]
end
B = [upper_bound(G, x) for x = A]
n = length(B)
b = sort(B)
return parent(B[1])(binomial(n, i))*prod(b[max(1, n-i+1):end])
end
function upper_bound(G::GaloisCtx, ::typeof(power_sum), A::Vector, i::Int, ts::ZZPolyRingElem = gen(Oscar.Hecke.Globals.Zx))
if ts != gen(Hecke.Globals.Zx)
A = [ts(x) for x = A]
end
B = [upper_bound(G, x)^i for x = A]
return sum(B)
end
function upper_bound(G::GaloisCtx, ::typeof(elementary_symmetric), i::Int, ts::ZZPolyRingElem = gen(Oscar.Hecke.Globals.Zx))
if ts != gen(Hecke.Globals.Zx)
b = ts(G.B)
else
b = G.B
end
n = degree(G.f)
return parent(b)(binomial(n, i))*b^i
end
function upper_bound(G::GaloisCtx, ::typeof(power_sum), i::Int, ts::ZZPolyRingElem = gen(Oscar.Hecke.Globals.Zx))
if ts != gen(Hecke.Globals.Zx)
b = ts(G.B)
else
b = G.B
end
return parent(b)(degree(G.f))*b^i
end
function Hecke.orbit(G::Oscar.PermGroup, f::MPolyRingElem)
s = Set([f])
while true
n = Set(x^g for x = s for g = gens(G))
sn = length(s)
union!(s, n)
if length(s) == sn
break
end
end
return s
end
function Hecke.evaluate(I::SLPoly, p, a::Vector)
return evaluate(I, [a[p(i)] for i=1:length(a)])
end
probable_orbit(G::Oscar.PermGroup, f::MPolyRingElem) = orbit(G, f)
"""
`slprogram`s can be compiled into "normal" Julia functions, but there is
some overhead in the compilation itself. By default, apparently nothing is
compiled, so we allow to force this there.
`isPoly` allows the use of inplace operations, as `SLPoly`s result
in programs where intermediate results are used only once.
"""
function compile!(f::SLPoly)
if !isdefined(f.slprogram, :f)
Oscar.StraightLinePrograms.compile!(f.slprogram, isPoly = true)
end
end
#one cannot compare (==) slpoly, no hash either..
#(cannot be done, thus comparison is indirect via evaluation)
#I assume algorithm can be improved (TODO: Max?)
function probable_orbit(G::Oscar.PermGroup, f::SLPoly; limit::Int = typemax(Int))
n = ngens(parent(f))
F = GF(next_prime(2^50))
p = [rand(F) for i=1:n]
s = [f]
v = Set([evaluate(f, p)])
while true
nw = []
for g = gens(G)
for h = s
z = evaluate(h^g, p)
if !(z in v)
push!(nw, h^g)
push!(v, z)
if length(s) + length(nw) > limit
append!(s, nw)
return s
end
end
end
end
if length(nw) == 0
return s
end
append!(s, nw)
end
end
#TODO:
#- Bessere Abstraktion um mehr Grundkoerper/ Ringe zu erlauben
#- Bessere Teilkpoerper: ich brauche "nur" maximale
#- sanity-checks
#- "datenbank" fuer Beispiele
#a gimmick, not used in galois groups
@doc raw"""
to_elementary_symmetric(f)
For a multivariate symmetric polynomial `f`, (i.e. `f` is invariant under
permutation of the variables), compute a new polynomial `g` s.th.
`g` evaluated at the elementary symmetric polynomials recovers `f`.
This is using a rather elementary algorithm.
# Examples
We recover the Newton-Girard formulas:
```jldoctest
julia> R, x = polynomial_ring(QQ, 3);
julia> d = power_sum(x, 3)
x1^3 + x2^3 + x3^3
julia> g = to_elementary_symmetric(d)
x1^3 - 3*x1*x2 + 3*x3
julia> evaluate(g, [elementary_symmetric(x, i) for i=1:3])
x1^3 + x2^3 + x3^3
```
"""
function to_elementary_symmetric(f)
S = parent(f)
n = ngens(S)
if n == 1 || is_constant(f)
return f
end
T = polynomial_ring(base_ring(S), n-1)[1]
g1 = to_elementary_symmetric(evaluate(f, vcat(gens(T), [T(0)])))
es = [elementary_symmetric(gens(S), i) for i=1:n-1]
f = f - evaluate(g1, es)
h = divexact(f, elementary_symmetric(gens(S), n))
g2 = to_elementary_symmetric(h)
g1 = evaluate(g1, gens(S)[1:n-1])
return g1 + gen(S, n)*g2
end
function ^(f::SLPoly, p::Oscar.PermGroupElem)
#TODO: replace by making the permutation of the input an internal
# operation.
g = gens(parent(f))
h = typeof(f)[]
for i=1:ngens(parent(f))
push!(h, g[p(i)])
end
e = evaluate(f, h)
if typeof(e) != typeof(f)
@show "bad case"
return f
end
return e
end
@doc raw"""
isprobably_invariant(g, p) -> Bool
For a multivariate function, mainly an `SLPoly`, test if this is
likely to be invariant under the permutation `p`. Due to the representation
of `SLPoly`s as trees, it is not possible to test this exactly. Instead
`p` is evaluated at random elements in a large finite field.
"""
function isprobably_invariant(g, p)
R = parent(g)
k = GF(next_prime(2^20))
n = ngens(R)
lp = [rand(k) for i=1:n]
return evaluate(g, lp) == evaluate(g^p, lp)
end
#TODO: think about the order of arguments!
function isprobably_invariant(p, G::PermGroup)
R = parent(p)
k = GF(next_prime(2^20))
n = ngens(R)
lp = [rand(k) for i=1:n]
gp = evaluate(p, lp)
return all(x->gp == evaluate(p^x, lp), gens(G))
end
function set_orbit(G::PermGroup, H::PermGroup)
#from Elsenhans
#https://math.uni-paderborn.de/fileadmin/mathematik/AG-Computeralgebra/inv_transfer_5_homepage.pdf
# http://dblp.uni-trier.de/db/journals/jsc/jsc79.html#Elsenhans17
# https://doi.org/10.1016/j.jsc.2016.02.005
l = representative.(low_index_subgroup_classes(H, 2*degree(G)^2))
S, g = slpoly_ring(ZZ, degree(G), cached = false)
sort!(l, lt = (a,b) -> isless(order(b), order(a)))
for U = l
O = orbits(U)
for o in O
#TODO: should use orbits of Set(o)...
f = sum(g[collect(o)])
oH = probable_orbit(H, f)
oG = probable_orbit(G, f, limit = length(oH)+5)
if length(oH) < length(oG)
for i = 1:length(o)
I = sum(x^i for x = oH)
if isprobably_invariant(I, H) &&
!isprobably_invariant(I, G)
@vprint :GaloisInvariant 2 "SetOrbit won\n"
return true, I
end
end
f = prod(g[collect(o)])
oH = probable_orbit(H, f)
I = sum(oH)
if isprobably_invariant(I, H) &&
!isprobably_invariant(I, G)
@vprint :GaloisInvariant 2 "SetOrbit won - final attempt\n"
return true, I
end
end
end
end
return false, g[1]
end
@doc raw"""
invariant(G::PermGroup, H::PermGroup)
For a permutation group `G` and a maximal subgroup `H`, find
a (multivariate (`SLPoly`)) `f` with `G`-stabilizer `H`, i.e.
`f` is invariant under all permutations in `H`, but not invariant under
any other element of `G`.
"""
function invariant(G::PermGroup, H::PermGroup)
@vprint :GaloisInvariant 1 "Searching G-relative H-invariant\n"
@vprint :GaloisInvariant 2 "that is a $G-relative $H-invariant\n"
S, g = slpoly_ring(ZZ, degree(G), cached = false)
if is_transitive(G) && !is_transitive(H)
@vprint :GaloisInvariant 2 "top group transitive, bottom not\n"
return sum(probable_orbit(H, g[1]))
end
if !is_transitive(G)
@vprint :GaloisInvariant 2 "both groups are intransitive\n"
OG = [sort(collect(x)) for x = orbits(G)]
OH = [sort(collect(x)) for x = orbits(H)]
d = setdiff(OH, OG)
if length(d) > 0
@vprint :GaloisInvariant 2 "groups have different orbits\n"
return sum(probable_orbit(H, g[d[1][1]]))
end
#OH == OG
for o = OH
h = action_homomorphism(G, o)
hG = image(h)[1]
hH = image(h, H)[1]
if order(hG) > order(hH)
@vprint :GaloisInvariant 2 "differ on action on $o, recursing\n"
@hassert :GaloisInvariant 0 is_maximal_subgroup(hH, hG)
I = invariant(hG, hH)
return evaluate(I, g[collect(o)])
end
end
@vprint :GaloisInvariant 2 "going transitive...\n"
#creating transitive version.
gs = Oscar.gset(G, [[x[1] for x = OG]])
os = Oscar.orbits(gs)
@assert length(os) == 1
os = os[1]
h = Oscar.action_homomorphism(os)
GG = h(G)[1]
HH = h(H)[1]
I = invariant(GG, HH)
ex = 1
while true
J = evaluate(I, [sum(g[o])^ex for o = collect(os)])
if !isprobably_invariant(J, G)
I = J
break
end
ex += 1
end
@hassert :GaloisInvariant 2 isprobably_invariant(I, H)
@hassert :GaloisInvariant 2 !isprobably_invariant(I, G)
return I
end
if is_primitive(G) && is_primitive(H)
if isodd(G) && iseven(H)
@vprint :GaloisInvariant 3 "using sqrt_disc\n"
return sqrt_disc(g)
end