-
Notifications
You must be signed in to change notification settings - Fork 126
/
AlgClosureFp.jl
334 lines (271 loc) · 9.35 KB
/
AlgClosureFp.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
###############################################################################
#
# Algebraic closure of finite fields
#
###############################################################################
# This is an implementation of the algebraic closure of finite fields,
# which is modelled as the union of finite fields.
module AlgClosureFp
using ..Oscar
import Base: +, -, *, //, ==, deepcopy_internal, hash, isone, iszero, one,
parent, show, zero
import ..Oscar: base_field, base_ring, characteristic, data, degree, divexact,
elem_type, embedding, has_preimage, IntegerUnion, is_unit, map_entries,
minpoly, parent_type, promote_rule, roots
struct AlgClosure{T} <: AbstractAlgebra.Field
# T <: FinField
k::T
fld::Dict{Int, FinField} # Cache for the finite fields
function AlgClosure(k::T) where T <: FinField
return new{T}(k, Dict{Int, FinField}(degree(k) => k))
end
end
function show(io::IO, A::AlgClosure)
print(io, "Algebraic Closure of $(A.k)")
end
base_field(A::AlgClosure) = A.k
base_ring(A::AlgClosure) = A.k
characteristic(k::AlgClosure) = characteristic(base_field(k))
struct AlgClosureElem{T} <: FieldElem
# T <: FinField
data::FinFieldElem
parent::AlgClosure{T}
end
elem_type(::Type{AlgClosure{T}}) where T = AlgClosureElem{T}
elem_type(::AlgClosure{T}) where T = AlgClosureElem{T}
parent_type(::AlgClosureElem{T}) where T = AlgClosure{T}
parent_type(::Type{AlgClosureElem{T}}) where T = AlgClosure{T}
function show(io::IO, a::AlgClosureElem)
print(io, data(a))
end
function deepcopy_internal(a::AlgClosureElem, d::IdDict)
return AlgClosureElem(deepcopy_internal(data(a), d), parent(a))
end
(A::AlgClosure)(a::IntegerUnion) = AlgClosureElem(A.k(a), A)
(A::AlgClosure)(a::AlgClosureElem) = a
(A::AlgClosure)() = A(0)
function (A::AlgClosure)(a::FinFieldElem)
@assert characteristic(parent(a)) == characteristic(A)
if haskey(A.fld, degree(parent(a)))
@assert A.fld[degree(parent(a))] == parent(a)
end
return AlgClosureElem(a, A)
end
zero(A::AlgClosure) = AlgClosureElem(zero(base_field(A)), A)
one(A::AlgClosure) = AlgClosureElem(one(base_field(A)), A)
parent(a::AlgClosureElem) = a.parent
data(a::AlgClosureElem) = a.data
function check_parent(a::AlgClosureElem, b::AlgClosureElem)
parent(a) == parent(b) || error("incompatible elements")
end
#TODO: Guarantee to return a field of the same type as `base_ring(A)`?
# (Then `Nemo.fpField` cannot be supported as `base_ring(A)`)
@doc raw"""
ext_of_degree(A::AlgClosure, d::Int)
Return a finite field `F` of order `p^d`
where `p` is the characteristic of `K`.
This field is compatible with `A` in the sense that `A(x)` returns the
element of `A` that corresponds to the element `x` of `F`.
"""
function ext_of_degree(A::AlgClosure, d::Int)
if haskey(A.fld, d)
return A.fld[d]
end
k = base_ring(A)
if isa(k, Nemo.fpField) || isa(k, fqPolyRepField)
K = GF(Int(characteristic(k)), d, cached = false)
elseif isa(k, FqField)
K = Nemo._GF(characteristic(k), d, cached = false)
else
K = GF(characteristic(k), d, cached = false)
end
A.fld[d] = K
return K
end
function op(f::Function, a::AlgClosureElem, b::AlgClosureElem)
check_parent(a, b)
ad = data(a)
bd = data(b)
if parent(ad) == parent(bd)
return f(ad,bd)
end
l = lcm(degree(parent(ad)), degree(parent(bd)))
k = ext_of_degree(parent(a), l)
embed(parent(ad), k)
embed(parent(bd), k)
return f(k(ad), k(bd))
end
#T the following belongs to Nemo and should be moved there
function Oscar.embed(k::Nemo.fpField, K::fqPolyRepField)
@assert characteristic(K) == characteristic(k)
end
+(a::AlgClosureElem, b::AlgClosureElem) = AlgClosureElem(op(+, a, b), parent(a))
-(a::AlgClosureElem, b::AlgClosureElem) = AlgClosureElem(op(-, a, b), parent(a))
#TODO: do we really want to support different types here? (implies different parents)
*(a::AlgClosureElem{T}, b::AlgClosureElem{S}) where S where T = AlgClosureElem(op(*, a, b), parent(a))
//(a::AlgClosureElem, b::AlgClosureElem) = AlgClosureElem(op(//, a, b), parent(a))
divexact(a::AlgClosureElem, b::AlgClosureElem; check = true) = AlgClosureElem(op(divexact, a, b), parent(a))
==(a::AlgClosureElem, b::AlgClosureElem) = op(==, a, b)
iszero(a::AlgClosureElem) = iszero(data(a))
isone(a::AlgClosureElem) = isone(data(a))
-(a::AlgClosureElem) = AlgClosureElem(-data(a), parent(a))
################################################################################
#
# Ad hoc binary operations
#
################################################################################
*(a::IntegerUnion, b::AlgClosureElem) = AlgClosureElem(data(b)*a, parent(b))
*(a::AlgClosureElem, b::IntegerUnion) = b*a
+(a::IntegerUnion, b::AlgClosureElem) = AlgClosureElem(data(b) + a, parent(b))
+(a::AlgClosureElem, b::IntegerUnion) = b + a
-(a::IntegerUnion, b::AlgClosureElem) = AlgClosureElem(-(a, data(b)), parent(b))
-(a::AlgClosureElem, b::IntegerUnion) = AlgClosureElem(-(data(a), b), parent(a))
//(a::AlgClosureElem, b::Integer) = AlgClosureElem(data(a)//b, parent(a))
//(a::AlgClosureElem, b::ZZRingElem) = AlgClosureElem(data(a)//b, parent(a))
is_unit(a::AlgClosureElem) = !iszero(a)
###############################################################################
#
# Functions for computing roots
#
###############################################################################
function roots(a::AlgClosureElem, b::Int)
ad = data(a)
kx, x = polynomial_ring(parent(ad), cached = false)
f = x^b-ad
lf = factor(f)
d = mapreduce(degree, lcm, keys(lf.fac), init = 1)
d = lcm(d, degree(parent(ad)))
K = ext_of_degree(parent(a), d)
r = roots(K, f)
return [AlgClosureElem(x, parent(a)) for x = r]
end
function roots(a::Generic.Poly{AlgClosureElem{T}}) where T
A = base_ring(a)
b = minimize(FinField, collect(coefficients(a)))
kx, x = polynomial_ring(parent(b[1]), cached = false)
f = kx(b)
lf = factor(f)
d = mapreduce(degree, lcm, keys(lf.fac), init = 1)
d = lcm(d, degree(parent(b[1])))
K = ext_of_degree(A, d)
r = roots(K, f)
return [AlgClosureElem(x, A) for x = r]
end
#TODO: Does this really make sense?
# K = algebraic_closure(GF(3, 1))
# F2 = ext_of_degree(K, 2)
# a = gen(F2); fa = minpoly(a); fa(a) # works
# c = K(a); fc = minpoly(c); fc(c) # does not work
function minpoly(a::AlgClosureElem)
return minpoly(data(a))
end
#TODO: Move to Nemo.
function minpoly(a::fpFieldElem)
kx, x = polynomial_ring(parent(a), cached = false)
return x-a
end
# Note: We want the degree of the smallest finite field that contains `a`.
function degree(a::AlgClosureElem)
#TODO: via Frobenius? as a fixed s.th.?
return degree(minpoly(data(a)))
end
function minimize(a::AlgClosureElem)
f = minpoly(a)
k = ext_of_degree(parent(a), degree(f))
embed(k, parent(data(a)))
return AlgClosureElem(k(data(a)), parent(a))
end
function minimize(::Type{FinField}, a::AlgClosureElem)
return data(minimize(a))
end
function minimize(::Type{FinField}, a::AbstractArray{<:AlgClosureElem})
if length(a) == 0
return a
end
@assert all(x->parent(x) == parent(a[1]), a)
da = map(degree, a)
l = reduce(lcm, da)
k = ext_of_degree(parent(a[1]), l)
b = elem_type(k)[]
for i = eachindex(a)
if da[i] < l
embed(parent(data(a[i])), k)
push!(b, k(data(a[i])))
elseif da[i] == l
push!(b, data(a[i]))
else
embed(k, parent(data(a[i])))
push!(b, k(data(a[i])))
end
end
return b
end
function (F::FinField)(a::AlgClosureElem)
b = minimize(FinField, a)
embed(parent(b), F)
return F(b)
end
function hash(a::AlgClosureElem, u::UInt)
b = minimize(a)
return hash(data(b), u)
end
function map_entries(K::FinField, M::MatElem{<:AlgClosureElem})
N = zero_matrix(K, nrows(M), ncols(M))
for i=1:nrows(M)
for j=1:ncols(M)
embed(parent(data(M[i,j])), K)
N[i,j] = K(data(M[i,j]))
end
end
return N
end
################################################################################
#
# Creation of the field
#
################################################################################
@doc raw"""
algebraic_closure(F::FinField)
Let `F` be a prime field of order `p`.
Return a field `K` that is the union of finite fields of oder `p^d`,
for all positive integers `d`.
The degree `d` extension of `F` can be obtained as `ext_of_degree(K, d)`.
`K` is cached in `F`, and the fields returned by `ext_of_degree` are
cached in `K`.
# Examples
```jldoctest; setup = :(using Oscar)
julia> K = algebraic_closure(GF(3, 1));
julia> F2 = ext_of_degree(K, 2);
julia> F3 = ext_of_degree(K, 3);
julia> x = K(gen(F2)) + K(gen(F3));
julia> degree(x)
6
```
"""
function algebraic_closure(F::T) where T <: FinField
@req is_prime(order(F)) "only for finite prime fields"
return get_attribute!(F, :algebraic_closure) do
return AlgClosure(F)
end::AlgClosure{T}
end
function embedding(k::T, K::AlgClosure{T}) where T <: FinField
@req characteristic(k) == characteristic(K) "incompatible characteristics"
f = x::FinFieldElem -> K(x)
finv = x::AlgClosureElem{T} -> k(x)
return MapFromFunc(k, K, f, finv)
end
function has_preimage(mp::MapFromFunc{T, AlgClosure{S}}, elm::AlgClosureElem{S}) where T <: FinField where S <: FinField
F = domain(mp)
mod(degree(F), degree(elm)) != 0 && return false, zero(F)
return true, preimage(mp, elm)
end
end # AlgClosureFp
import .AlgClosureFp:
AlgClosure,
AlgClosureElem,
algebraic_closure,
ext_of_degree
export AlgClosure,
AlgClosureElem,
algebraic_closure,
ext_of_degree