-
Notifications
You must be signed in to change notification settings - Fork 112
/
NumberField.jl
380 lines (318 loc) · 9.76 KB
/
NumberField.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
@testset "Number field" begin
Qx, x = FlintQQ["x"]
k, _ = number_field(x^2 + 1)
ku, u = k["u1", "u2"]
Ik = ideal(ku, [u[1]^3 + u[2]^3 - 3, u[1]^5 + u[2]^5 - 5])
Qy, y = FlintQQ["y1", "y2"]
IQ = ideal(Qy, [y[1]^3 + y[2]^3 - 3, y[1]^5 + y[2]^5 - 5])
for (Bk, Pk, I) in [(k, ku, Ik), (FlintQQ, Qy, IQ)]
gg = gens(Pk)
@test_throws ErrorException number_field(ideal([gg[1]]))
K, = @inferred number_field(I, [:a1, :a2])
@assert symbols(K) == [:a1, :a2]
@test_throws ArgumentError number_field(I, [:a1])
@test_throws ArgumentError number_field(I, [:a1, :a2, :a3])
K, = @inferred number_field(I, ["a1", "a2"])
@test symbols(K) == [:a1, :a2]
@test_throws ArgumentError number_field(I, ["a1"])
@test_throws ArgumentError number_field(I, ["a1", "a2", "a3"])
K, = @inferred number_field(I, "a")
@test symbols(K) == [:a1, :a2]
K, = @inferred number_field(I)
K, a = @inferred number_field(I, :a)
@test symbols(K) == [:a1, :a2]
# parent and element type
@inferred parent_type(a[1])
@inferred elem_type(K)
@test parent_type(elem_type(typeof(K))) == typeof(K)
@test elem_type(parent_type(a[1])) == typeof(a[1])
# field access
@test Bk == @inferred base_field(K)
@test Pk == @inferred Oscar.polynomial_ring(K)
@test I == @inferred Oscar.defining_ideal(K)
@test 12 == @inferred degree(K)
@test K.(gens(Pk)) == @inferred gens(K)
@test K(gen(Pk, 1)) == @inferred gen(K, 1)
@test K(gen(Pk, 2)) == @inferred gen(K, 2)
@test gen(Pk, 1) == @inferred Hecke.data(a[1])
@test K === @inferred parent(a[1])
@test 2 == @inferred ngens(K)
@test [:a1, :a2] == @inferred symbols(K)
# string i/o
s = sprint(show, "text/plain", K)
@test s isa String
s = sprint(show, "text/plain", a[1])
@test s isa String
@test (@inferred check_parent(a[1], a[1])) === nothing
KK, aa = number_field(I, [:a1, :a2])
@test_throws ArgumentError check_parent(a[1], aa[1])
# Ring interface
b = @inferred one(K)
@test @inferred isone(b)
b = @inferred zero(K)
@test @inferred iszero(b)
@test a[1] == @inferred canonical_unit(a[1])
# Arithmetic
for i in 1:10
b = rand(K, -2:2)
c = rand(K, -2:2)
d = rand(K, -2:2)
e = @inferred b + c
@test e == c + b
f = @inferred e * d
@test f == d * e
@test d * b + d * c == f
e = @inferred b - c
f = @inferred e * d
@test d * b - d * c == f
@test one(K) * b == b
@test zero(K) * b == zero(K)
@test iszero(b + @inferred(-b))
@test isone(@inferred b^0)
@test b == b^1
@test b * b == b^2
@test b^10 == reduce(*, [b for j in 1:10])
@test b^11 == reduce(*, [b for j in 1:11])
while iszero(b)
b = rand(K, -2:2)
end
bi = @inferred inv(b)
@test isone(bi * b)
e = @inferred divexact(c, b)
@test e * b == c
e = @inferred c//b
@test e * b == c
end
b = zero(K)
@test iszero(b^1)
@test_throws ArgumentError inv(b)
# Ad hoc arithmetic
for i in 1:10
b = rand(K, -1:1)
for R in Any[Bk, Int32, Int, BigInt, ZZRingElem,
Base.Rational{Int}, Base.Rational{BigInt}, QQFieldElem]
@test b == @inferred (b + R(0))
@test b == @inferred (R(0) + b)
@test b == @inferred (b * R(1))
@test b == @inferred (R(1) * b)
@test b == @inferred b//R(1)
@test b == @inferred divexact(b, R(1))
if !iszero(b)
@test inv(b) == @inferred R(1)//b
@test inv(b) == @inferred divexact(R(1), b)
end
end
end
b = rand(K, -1:1)
bb = deepcopy(b)
@test parent(bb) === K
@test b !== bb
# In place operations
for i in 1:10
b = rand(K, -2:2)
c = rand(K, -2:2)
d = rand(k, -2:2)
e = zero(K)
@inferred mul!(e, b, c)
@test e == b * c
@inferred add!(e, b, c)
@test e == b + c
e = deepcopy(b)
@inferred addeq!(b, c)
@test b == e + c
end
# comparison
for i in 1:10
b = rand(K, -2:2)
@test (@inferred b == b)
@test (@inferred b != (b + 1))
for R in Any[Bk, Int32, Int, BigInt, ZZRingElem,
Base.Rational{Int}, Base.Rational{BigInt}, QQFieldElem]
o = one(K)
@test (@inferred o == R(1))
@test (@inferred o != R(0))
@test (@inferred R(1) == o)
@test (@inferred R(0) != o)
end
end
# parent call overloading
b = @inferred K()
@test parent(b) === K
b = @inferred K(gen(Pk, 1))
@test parent(b) === K
PP, _x = Bk["x1", "x2", "x3"]
@test_throws ErrorException K(_x[1])
for R in Any[Bk, Int, BigInt, ZZRingElem,
Base.Rational{Int}, Base.Rational{BigInt}, QQFieldElem]
b = @inferred K(R(1))
end
# denominator
if Bk === FlintQQ
b = rand(K, -2:2)
d = @inferred denominator(b)
@test d isa ZZRingElem
end
# basis
B = @inferred basis(K)
@test length(B) == degree(K)
BA = @inferred absolute_basis(K)
@test length(BA) == absolute_degree(K)
for i in 1:10
b = rand(K, -2:2)
v = @inferred coordinates(b)
@test b == sum(v[i] * B[i] for i in 1:degree(K))
v = @inferred absolute_coordinates(b)
@test length(v) == absolute_degree(K)
@test b == sum(v[i] * BA[i] for i in 1:length(BA))
end
# basis matrix
if Bk == FlintQQ
for i in 1:10
BB = [rand(K, -2:2) for j in 1:rand(1:10)]
M = @inferred basis_matrix(BB, Hecke.FakeFmpqMat)
@test nrows(M) == length(BB)
@test ncols(M) == degree(K)
for n in 1:nrows(M)
@test BB[n] == sum(M[n, m] * B[m] for m in 1:length(B))
end
end
end
# representation matrix
for i in 1:10
b = rand(K, -2:2)
M = @inferred representation_matrix(b)
@test nrows(M) == degree(K)
@test ncols(M) == degree(K)
@test (M isa dense_matrix_type(Bk))
for n in 1:length(B)
@test b * B[n] == sum(M[n, m] * B[m] for m in 1:length(B))
end
if Bk == FlintQQ
M, d = @inferred representation_matrix_q(b)
@test nrows(M) == degree(K)
@test ncols(M) == degree(K)
@test (M isa ZZMatrix)
for n in 1:length(B)
@test b * B[n] == sum(M[n, m]//d * B[m] for m in 1:length(B))
c = @inferred Oscar.Hecke.elem_from_mat_row(K, M, n, d)
@test b * B[n] == c
end
# elem_to_mat_row!
c = rand(-10:10)
while iszero(c)
c = rand(-10:10)
end
b = b//c
MM = zero_matrix(FlintZZ, nrows(M), ncols(M))
dd = ZZRingElem()
j = rand(1:nrows(MM))
Oscar.Hecke.elem_to_mat_row!(MM, j, dd, b)
@test b == sum(MM[j, m]//dd * B[m] for m in 1:length(B))
end
end
# minpoly and charpoly
for i in 1:10
b = rand(K, -2:2)
f = @inferred minpoly(b)
@test iszero(f(b))
@test is_irreducible(f)
f = @inferred charpoly(b)
@test iszero(f(b))
@test degree(f) == degree(K)
end
# trace and norm
for i in 1:10
b = rand(K, -2:2)
f = @inferred charpoly(b)
@test iszero(f(b))
@test degree(f) == degree(K)
t = @inferred tr(b)
@test parent(t) === Bk
@test t == -coeff(f, degree(K) - 1)
t = @inferred norm(b)
@test parent(t) === Bk
@test t == (isodd(degree(K)) ? -1 : 1) * coeff(f, 0)
b = rand(Bk, -10:10)
t = @inferred tr(K(b))
@test t == degree(K) * b
t = @inferred norm(K(b))
@test t == b^degree(K)
end
# (Maximal) order needs some adjustments on the Hecke side
#@test_broken maximal_order(K)
# Random
b = @inferred rand(K, -1:1)
@test parent(b) === K
z = @inferred Oscar.primitive_element(K)
@test degree(minpoly(z)) == degree(K)
# simple extension
Ks, KstoK = simple_extension(K)
for i in 1:10
b = rand(Ks, -10:10)
c = rand(Ks, -10:10)
@inferred KstoK(b)
@test parent(KstoK(b)) === K
@test KstoK(b + c) == KstoK(b) + KstoK(c)
@test KstoK(b * c) == KstoK(b) * KstoK(c)
@test KstoK\(KstoK(b)) == b
b = rand(K, -10:10)
c = rand(K, -10:10)
@inferred KstoK\(b)
@test parent(KstoK\(b)) === Ks
@test KstoK\(b + c) == (KstoK\b) + (KstoK\c)
@test KstoK\(b * c) == (KstoK\b) * (KstoK\c)
@test KstoK(KstoK\(b)) == b
end
# simple extension
if Bk == FlintQQ
Ks, KstoK = simple_extension(K, simplify = true)
else
Ks, KstoK = simple_extension(K)
end
for i in 1:10
b = rand(Ks, -10:10)
c = rand(Ks, -10:10)
@inferred KstoK(b)
@test parent(KstoK(b)) === K
@test KstoK(b + c) == KstoK(b) + KstoK(c)
@test KstoK(b * c) == KstoK(b) * KstoK(c)
if Bk == FlintQQ
@test KstoK\(KstoK(b)) == b
b = rand(K, -10:10)
c = rand(K, -10:10)
@inferred KstoK\(b)
@test parent(KstoK\(b)) === Ks
@test KstoK\(b + c) == (KstoK\b) + (KstoK\c)
@test KstoK\(b * c) == (KstoK\b) * (KstoK\c)
@test KstoK(KstoK\(b)) == b
end
end
# map
f = hom(K, K, gens(K))
@test K === @inferred domain(f)
@test K === @inferred codomain(f)
for i in 1:10
b = rand(K, -10:10)
@test b == @inferred f(b)
end
f = id_hom(K)
@test f * f == f
g = hom(K, K, [gen(K, 2), gen(K, 1)])
@test K === @inferred domain(g)
@test K === @inferred codomain(g)
for i in 1:10
b = rand(K, -10:10)
@test b == @inferred g(g(b))
end
for i in 1:10
b = rand(K, -10:10)
@test b == @inferred g\(g(b))
end
@test @inferred f == f
@test @inferred g == g
@test @inferred f != g
@test f == @inferred g * g
f = id_hom(K)
@test f * f == f
end
end