-
Notifications
You must be signed in to change notification settings - Fork 63
/
FunctionField.jl
1377 lines (1168 loc) · 37.4 KB
/
FunctionField.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
###############################################################################
#
# FunctionField.jl : Generic univariate function fields (algebraic extension
# of rational function field)
#
###############################################################################
###############################################################################
#
# Rational arithmetic : equivalent of _fmpq in Flint for k(x)
#
###############################################################################
function _rat_add(n1::PolyRingElem{T}, d1::PolyRingElem{T},
n2::PolyRingElem{T}, d2::PolyRingElem{T}) where T <: FieldElement
if d1 == d2
rnum = n1 + n2
if isone(d1)
rden = deepcopy(d1)
else
gd = gcd(rnum, d1)
if isone(gd)
rden = deepcopy(d1)
else
rnum = divexact(rnum, gd)
rden = divexact(d1, gd)
end
end
elseif isone(d1)
rnum = n1*d2 + n2
rden = deepcopy(d2)
elseif isone(d2)
rnum = n1 + n2*d1
rden = deepcopy(d1)
else
gd = gcd(d1, d2)
if isone(gd)
rnum = n1*d2 + n2*d1
rden = d1*d2
else
q1 = divexact(d1, gd)
q2 = divexact(d2, gd)
rnum = q1*n2 + q2*n1
t = gcd(rnum, gd)
if isone(t)
rden = q2*d1
else
rnum = divexact(rnum, t)
gd = divexact(d1, t)
rden = gd*q2
end
end
end
return rnum, rden
end
function _rat_sub(n1::PolyRingElem{T}, d1::PolyRingElem{T},
n2::PolyRingElem{T}, d2::PolyRingElem{T}) where T <: FieldElement
if d1 == d2
rnum = n1 - n2
if isone(d1)
rden = deepcopy(d1)
else
gd = gcd(rnum, d1)
if isone(gd)
rden = deepcopy(d1)
else
rnum = divexact(rnum, gd)
rden = divexact(d1, gd)
end
end
elseif isone(d1)
rnum = n1*d2 - n2
rden = deepcopy(d2)
elseif isone(d2)
rnum = n1 - n2*d1
rden = deepcopy(d1)
else
gd = gcd(d1, d2)
if isone(gd)
rnum = n1*d2 - n2*d1
rden = d1*d2
else
q1 = divexact(d1, gd)
q2 = divexact(d2, gd)
rnum = q2*n1 - q1*n2
t = gcd(rnum, gd)
if isone(t)
rden = q2*d1
else
rnum = divexact(rnum, t)
gd = divexact(d1, t)
rden = gd*q2
end
end
end
return rnum, rden
end
function _rat_mul(n1::PolyRingElem{T}, d1::PolyRingElem{T},
n2::PolyRingElem{T}, d2::PolyRingElem{T}) where T <: FieldElement
if d1 == d2
n = n1*n2
d = d1*d2
elseif isone(d1)
gd = gcd(n1, d2)
if isone(gd)
n = n1*n2
d = deepcopy(d2)
else
n = divexact(n1, gd)*n2
d = divexact(d2, gd)
end
elseif isone(d2)
gd = gcd(n2, d1)
if isone(gd)
n = n2*n1
d = deepcopy(d1)
else
n = divexact(n2, gd)*n1
d = divexact(d1, gd)
end
else
g1 = gcd(n1, d2)
g2 = gcd(n2, d1)
if !isone(g1)
n1 = divexact(n1, g1)
d2 = divexact(d2, g1)
end
if !isone(g2)
n2 = divexact(n2, g2)
d1 = divexact(d1, g2)
end
n = n1*n2
d = d1*d2
end
return n, d
end
function _rat_canonicalise(n::PolyRingElem{T}, d::PolyRingElem{T}) where
T <: FieldElement
g = gcd(n, d)
if !isone(g)
n = divexact(n, g)
d = divexact(d, g)
end
u = canonical_unit(d)
if !isone(u)
n = divexact(n, u)
d = divexact(d, u)
end
return n, d
end
###############################################################################
#
# Rational polynomial arithmetic : equivalent of _fmpq_poly in Flint for k(x)
#
###############################################################################
function _rat_poly_add(poly1::Poly{S}, den1::S,
poly2::Poly{S}, den2::S) where
{T <: FieldElement, S <: PolyRingElem{T}}
R = base_ring(poly1)
if den1 == den2
rpoly = poly1 + poly2
if isone(den1)
rden = deepcopy(den1)
else
d = content(rpoly)
if !isone(d)
d = gcd(d, den1)
end
if isone(d)
rden = deepcopy(den1)
else
rpoly = divexact(rpoly, d)
rden = divexact(den1, d)
end
end
return rpoly, rden
end
d = gcd(den1, den2)
if isone(d)
rpoly = poly1*den2
t = poly2*den1
rpoly = add!(rpoly, t)
rden = den1*den2
else
den11 = divexact(den1, d)
den22 = divexact(den2, d)
rpoly = poly1*den22
t = poly2*den11
rpoly = add!(rpoly, t)
if iszero(rpoly)
rden = one(R)
else
c = content(rpoly)
if !isone(c)
c = gcd(c, d)
end
if isone(c)
rden = den1*den22
else
rpoly = divexact(rpoly, c)
den11 = divexact(den1, c)
rden = den11*den22
end
end
end
return rpoly, rden
end
function _rat_poly_sub(poly1::Poly{S}, den1::S,
poly2::Poly{S}, den2::S) where
{T <: FieldElement, S <: PolyRingElem{T}}
R = base_ring(poly1)
if den1 == den2
rpoly = poly1 - poly2
if isone(den1)
rden = deepcopy(den1)
else
d = content(rpoly)
if !isone(d)
d = gcd(d, den1)
end
if isone(d)
rden = deepcopy(den1)
else
rpoly = divexact(rpoly, d)
rden = divexact(den1, d)
end
end
return rpoly, rden
end
d = gcd(den1, den2)
if isone(d)
rpoly = poly1*den2 - poly2*den1
rden = den1*den2
else
den11 = divexact(den1, d)
den22 = divexact(den2, d)
rpoly = poly1*den22 - poly2*den11
if iszero(rpoly)
rden = one(R)
else
c = content(rpoly)
if !isone(c)
c = gcd(c, d)
end
if isone(c)
rden = den1*den22
else
rpoly = divexact(rpoly, c)
den11 = divexact(den1, c)
rden = den11*den22
end
end
end
return rpoly, rden
end
function _rat_poly_mul(poly1::Poly{S}, den1::S,
poly2::Poly{S}, den2::S) where
{T <: FieldElement, S <: PolyRingElem{T}}
R = base_ring(poly1)
if !isone(den2)
gcd1 = content(poly1)
gcd1 = gcd(gcd1, den2)
else
gcd1 = one(R)
end
if !isone(den1)
gcd2 = content(poly2)
gcd2 = gcd(gcd2, den1)
else
gcd2 = one(R)
end
rpoly = poly1*poly2
rden = den1*den2
if !isone(gcd1) || !isone(gcd2)
g = gcd1*gcd2
rpoly = divexact(rpoly, g)
rden = divexact(rden, g)
end
return rpoly, rden
end
function _rat_poly_canonicalise(poly::Poly{S}, den::S) where
{T <: FieldElement, S <: PolyRingElem{T}}
R = base_ring(poly)
if isone(den)
return poly, den
end
if isone(-den)
return -poly, one(R)
end
if length(poly) == 0
return poly, one(R)
end
g = content(poly)
g = gcd(g, den)
if !isone(g)
poly = divexact(poly, g)
den = divexact(den, g)
end
return poly, den
end
function _rat_poly_rem(poly1::Poly{S}, den1::S,
poly2::Poly{S}, den2::S) where
{T <: FieldElement, S <: PolyRingElem{T}}
R = base_ring(poly1)
len1 = length(poly1)
len2 = length(poly2)
if len1 < len2
rpoly = deepcopy(poly1)
rden = deepcopy(den1)
return rpoly, rden
end
if len2 == 1
rpoly = zero(parent(poly1))
rden = one(R)
return rpoly, rden
end
lenq = len1 - len2 + 1
rpoly = pseudorem(poly1, poly2)
lead = leading_coefficient(poly2)
if isone(lead)
rden = deepcopy(den1)
elseif isone(-lead)
rden = -den1
rpoly = -rpoly
else
rden = den1*lead^lenq
end
return _rat_poly_canonicalise(rpoly, rden)
end
function _rat_poly_gcdx(a::Poly{U}, den_a::U,
b::Poly{U}, den_b::U) where
{T <: FieldElement, U <: PolyRingElem{T}}
S = parent(a)
R = parent(den_a)
lena = length(a)
lenb = length(b)
if lena == 0 && lenb == 0
return zero(S), one(R), zero(S), one(R), zero(S), one(R)
end
if lena == 0
c = leading_coefficient(b)
g, den_g = _rat_poly_canonicalise(b, c)
t, den_t = _rat_poly_canonicalise(S(den_b), c)
return g, den_g, zero(S), one(R), t, den_t
end
if lenb == 0
c = leading_coefficient(a)
g, den_g = _rat_poly_canonicalise(a, c)
s, den_s = _rat_poly_canonicalise(S(den_a), c)
return g, den_g, s, den_s, zero(S), one(R)
end
if lena == 1
c = leading_coefficient(a)
s, den_s = _rat_poly_canonicalise(S(den_a), c)
return one(S), one(R), s, den_s, zero(S), one(R)
end
if lenb == 1
c = leading_coefficient(b)
t, den_t = _rat_poly_canonicalise(S(den_b), c)
return one(S), one(R), zero(S), one(R), t, den_t
end
ca = content(a)
cb = content(b)
if !isone(ca)
a = divexact(a, ca)
end
if !isone(cb)
b = divexact(b, cb)
end
g = gcd(a, b)
if length(g) > 1
a = divexact(a, g)
b = divexact(b, g)
end
den_g, s, t = resx(a, b)
den_g *= leading_coefficient(g)
s *= den_a
den_s = ca*den_g
t *= den_b
den_t = cb*den_g
s, den_s = _rat_poly_canonicalise(s, den_s)
t, den_t = _rat_poly_canonicalise(t, den_t)
return g, leading_coefficient(g), s, den_s, t, den_t
end
function _rat_poly_resultant(a::Poly{U}, den_a::U,
b::Poly{U}, den_b::U) where
{T <: FieldElement, U <: PolyRingElem{T}}
lena = length(a)
lenb = length(b)
R = parent(den_a)
if lena == 0 || lenb == 0
return zero(R), one(R)
end
ca = content(a)
cb = content(b)
a = divexact(a, ca)
b = divexact(b, cb)
g = gcd(a, b)
if length(g) > 1 # not coprime, resultant is zero
return zero(R), one(R)
end
rnum = resultant(a, b)
if !isone(ca)
rnum *= ca^(lenb - 1)
end
if !isone(cb)
rnum *= cb^(lena - 1)
end
rden = den_a^(lenb - 1)*den_b^(lena - 1)
return _rat_canonicalise(rnum, rden)
end
# convert a polynomial over a rational function field to
# a numerator and denominator
function _rat_poly(p::Poly{RationalFunctionFieldElem{T, U}}, var=parent(p).S; cached::Bool=true) where {T <: FieldElement, U <: PolyRingElem}
K = base_ring(p)
R = base_ring(fraction_field(K))
S = elem_type(R)
par = PolyRing{S}(R, var, cached)
len = length(p)
if len == 0
rpol = Poly{S}(R())
rpol.parent = par
return rpol, R()
end
d = one(R)
for i = 1:len
d = lcm(d, denominator(coeff(p, i - 1), false))
end
V = Vector{S}(undef, len)
for i = 1:len
c = coeff(p, i - 1)
den_i = denominator(c, false)
if den_i == d
V[i] = deepcopy(numerator(c, false))
else
V[i] = divexact(d, den_i)*numerator(c, false)
end
end
rpol = Poly{S}(V)
rpol.parent = par
return rpol, d
end
###############################################################################
#
# Data type and parent object methods
#
###############################################################################
parent_type(::Type{FunctionFieldElem{T}}) where T <: FieldElement = FunctionField{T}
elem_type(::Type{FunctionField{T}}) where T <: FieldElement = FunctionFieldElem{T}
function base_ring_type(::Type{FunctionField{T}}) where T <: FieldElement
U = dense_poly_type(T)
return RationalFunctionField{T, U}
end
base_ring(R::FunctionField{T}) where T <: FieldElement = R.base_ring::base_ring_type(R)
# For consistency with number fields in Hecke.jl
@doc raw"""
base_field(R::FunctionField)
Return the rational function field that the field `R` is an extension of.
Synonymous with `base_ring`.
"""
base_field(R::FunctionField) = base_ring(R)
parent(a::FunctionFieldElem) = a.parent
function is_exact_type(a::Type{T}) where {S <: FieldElement, T <: FunctionFieldElem{S}}
return is_exact_type(S)
end
@doc raw"""
var(R::FunctionField)
Return the variable name of the generator of the function field `R` as a
symbol.
"""
var(R::FunctionField) = R.S
@doc raw"""
characteristic(R::FunctionField)
Return the characteristic of the underlying rational function field.
"""
characteristic(R::FunctionField) = characteristic(base_ring(R))
###############################################################################
#
# Basic manipulation
#
###############################################################################
@doc raw"""
defining_polynomial(R::FunctionField)
modulus(R::FunctionField)
Return the original polynomial that was used to define the function field `R`.
"""
defining_polynomial(R::FunctionField) = R.pol
modulus(R::FunctionField) = defining_polynomial(R)
function power_precomp(R::FunctionField{T}, n::Int) where T <: FieldElement
return R.powers[n + 1]::Poly{dense_poly_type(T)}
end
function power_precomp_den(R::FunctionField{T}, n::Int) where T <: FieldElement
return R.powers_den[n + 1]::dense_poly_type(T)
end
function trace_precomp(R::FunctionField{T}, n::Int) where T <: FieldElement
return R.traces[n + 1]::dense_poly_type(T)
end
function trace_precomp_den(R::FunctionField{T}) where T <: FieldElement
return R.traces_den::dense_poly_type(T)
end
@doc raw"""
Base.numerator(R::FunctionField{T}, canonicalise::Bool=true) where T <: FieldElement
Base.denominator(R::FunctionField{T}, canonicalise::Bool=true) where T <: FieldElement
Thinking of elements of the rational function field as fractions, put the
defining polynomial of the function field over a common denominator
and return the numerator/denominator respectively. Note that the resulting
polynomials belong to a different ring than the original defining polynomial.
The `canonicalise` is ignored, but exists for compatibility with the Generic
interface.
"""
function Base.numerator(R::FunctionField{T},
canonicalise::Bool=true) where T <: FieldElement
# only used for type assert, so no need to canonicalise
return R.num::Poly{dense_poly_type(T)}
end
function Base.denominator(R::FunctionField{T},
canonicalise::Bool=true) where T <: FieldElement
# only used for type assert, so no need to canonicalise
return R.den::dense_poly_type(T)
end
@doc raw"""
Base.numerator(a::FunctionFieldElem{T}, canonicalise::Bool=true) where T <: FieldElement
Base.denominator(a::FunctionFieldElem{T}, canonicalise::Bool=true) where T <: FieldElement
Return the numerator and denominator of the function field element `a`.
Note that elements are stored in fraction free form so that the denominator
is a common denominator for the coefficients of the element `a`.
If `canonicalise` is set to `true` the fraction is first canonicalised.
"""
function Base.numerator(a::FunctionFieldElem{T},
canonicalise::Bool=true) where T <: FieldElement
anum = a.num::Poly{dense_poly_type(T)}
aden = a.den::dense_poly_type(T)
if canonicalise
u = canonical_unit(aden)
return divexact(anum, u)
else
return anum
end
end
function Base.denominator(a::FunctionFieldElem{T},
canonicalise::Bool=true) where T <: FieldElement
aden = a.den::dense_poly_type(T)
if canonicalise
u = canonical_unit(aden)
return divexact(aden, u)
else
return aden
end
end
@doc raw"""
degree(S::FunctionField)
Return the degree of the defining polynomial of the function field, i.e. the
degree of the extension that the function field makes of the underlying
rational function field.
"""
degree(S::FunctionField) = degree(numerator(S))
zero(S::FunctionField) = S()
one(S::FunctionField) = S(1)
@doc raw"""
gen(S::FunctionField{T}) where T <: FieldElement
Return the generator of the function field returned by the function field
constructor.
"""
function gen(S::FunctionField{T}) where T <: FieldElement
if degree(S) == 1
return S(-coeff(modulus(S), 0)//coeff(modulus(S), 1))
else
return FunctionFieldElem{T}(S, deepcopy(power_precomp(S, 1)),
deepcopy(power_precomp_den(S, 1)))
end
end
iszero(a::FunctionFieldElem) = iszero(numerator(a, false))
isone(a::FunctionFieldElem) = numerator(a, false) == denominator(a, false)
is_unit(a::FunctionFieldElem) = !iszero(a)
@doc raw"""
is_gen(a::FunctionFieldElem)
Return `true` if `a` is the generator of the function field returned by the
function field constructor.
"""
function is_gen(a::FunctionFieldElem)
S = parent(a)
if degree(S) == 1
return a == S(-coeff(modulus(S), 0)//coeff(modulus(S), 1))
else
return is_gen(numerator(a, false)) && isone(denominator(a, false)) ||
is_gen(numerator(a, true)) && isone(denominator(a, true))
end
end
@doc raw"""
coeff(a::FunctionFieldElem, n::Int)
Return the degree `n` coefficient of the element `a` in its polynomial
representation in terms of the generator of the function field. The
coefficient is returned as an element of the underlying rational function
field.
"""
function coeff(a::FunctionFieldElem, n::Int)
R = base_ring(a)
n = coeff(numerator(a, false), n)
d = denominator(a, false)
return R(n//d)
end
@doc raw"""
num_coeff(a::FunctionFieldElem, n::Int)
Return the degree `n` coefficient of the numerator of the element `a` (in its
polynomial representation in terms of the generator of the function field,
rationalised as per `numerator/denominator` described above). The coefficient
will be an polynomial over the `base_ring` of the underlying rational function
field.
"""
function num_coeff(a::FunctionFieldElem, n::Int)
return coeff(numerator(a, false), n)
end
function deepcopy_internal(a::FunctionFieldElem, dict::IdDict)
S = parent(a)
return S(deepcopy_internal(numerator(a, false), dict),
deepcopy_internal(denominator(a, false), dict))
end
function Base.hash(a::FunctionFieldElem, h::UInt)
b = 0x52fd76bf2694aa02%UInt
b = xor(hash(denominator(a, false), h),
xor(hash(numerator(a, false), h), b))
return b
end
function _rat_poly(a::FunctionFieldElem)
return numerator(a, false), denominator(a, false)
end
###############################################################################
#
# AbstractString I/O
#
###############################################################################
function AbstractAlgebra.expressify(a::FunctionFieldElem; context = nothing)
n = numerator(a, true)
d = denominator(a, true)
if isone(d)
return expressify(n; context)
else
return Expr(:call, ://, expressify(n; context), expressify(d; context))
end
end
@enable_all_show_via_expressify FunctionFieldElem
function show(io::IO, R::FunctionField)
@show_name(io, R)
@show_special(io, R)
print(terse(pretty(io)), "Function Field over ", Lowercase(),
base_ring(base_ring(R)), " with defining polynomial ",
numerator(R))
end
###############################################################################
#
# Unary operators
#
###############################################################################
function -(a::FunctionFieldElem)
R = parent(a)
return R(-numerator(a, false), denominator(a, false))
end
###############################################################################
#
# Binary operators
#
###############################################################################
function +(a::FunctionFieldElem{T}, b::FunctionFieldElem{T}) where T <: FieldElement
check_parent(a, b)
R = parent(a)
n1, d1 = _rat_poly(a)
n2, d2 = _rat_poly(b)
return R(_rat_poly_add(n1, d1, n2, d2)...)
end
function -(a::FunctionFieldElem{T}, b::FunctionFieldElem{T}) where T <: FieldElement
check_parent(a, b)
R = parent(a)
n1, d1 = _rat_poly(a)
n2, d2 = _rat_poly(b)
return R(_rat_poly_sub(n1, d1, n2, d2)...)
end
function *(a::FunctionFieldElem{T}, b::FunctionFieldElem{T}) where T <: FieldElement
check_parent(a, b)
R = parent(a)
n1, d1 = _rat_poly(a)
n2, d2 = _rat_poly(b)
z = R(_rat_poly_mul(n1, d1, n2, d2)...)
return reduce!(z)
end
###############################################################################
#
# Ad hoc binary operators
#
###############################################################################
function *(a::FunctionFieldElem, b::Union{Integer, Rational})
R = parent(a)
num = numerator(a, false)*b
return R(_rat_poly_canonicalise(num, denominator(a, false))...)
end
*(a::Union{Integer, Rational}, b::FunctionFieldElem) = b*a
function *(a::FunctionFieldElem{T}, b::T) where T <: FieldElem
R = parent(a)
num = numerator(a, false)*b
return R(_rat_poly_canonicalise(num, denominator(a, false))...)
end
*(a::T, b::FunctionFieldElem{T}) where T <: FieldElem = b*a
function *(a::FunctionFieldElem{T}, b::RationalFunctionFieldElem{T, U}) where {T <: FieldElement, U <: PolyRingElem}
parent(b) != base_ring(a) && error("Could not coerce element")
R = parent(a)
num = numerator(a, false)*numerator(b, false)
den = denominator(a, false)*denominator(b, false)
return R(_rat_poly_canonicalise(num, den)...)
end
*(a::RationalFunctionFieldElem{T, U}, b::FunctionFieldElem{T}) where {T <: FieldElement, U <: PolyRingElem} = b*a
function +(a::FunctionFieldElem{T}, b::RationalFunctionFieldElem{T, U}) where {T <: FieldElement, U <: PolyRingElem}
parent(b) != base_ring(a) && error("Unable to coerce element")
return a + parent(a)(b)
end
+(a::RationalFunctionFieldElem{T, U}, b::FunctionFieldElem{T}) where {T <: FieldElement, U <: PolyRingElem} = b + a
+(a::FunctionFieldElem, b::Union{Integer, Rational}) = a + base_ring(a)(b)
+(a::Union{Integer, Rational}, b::FunctionFieldElem) = b + a
function -(a::FunctionFieldElem{T}, b::RationalFunctionFieldElem{T, U}) where {T <: FieldElement, U <: PolyRingElem}
parent(b) != base_ring(a) && error("Unable to coerce element")
return a - parent(a)(b)
end
function -(a::RationalFunctionFieldElem{T, U}, b::FunctionFieldElem{T}) where {T <: FieldElement, U <: PolyRingElem}
parent(a) != base_ring(b) && error("Unable to coerce element")
return parent(b)(a) - b
end
-(a::FunctionFieldElem, b::Union{Integer, Rational}) = a - base_ring(a)(b)
-(a::Union{Integer, Rational}, b::FunctionFieldElem) = base_ring(b)(a) - b
###############################################################################
#
# Powering
#
###############################################################################
function ^(a::FunctionFieldElem{T}, b::Int) where T <: FieldElement
b < 0 && error("Not implemented")
R = parent(a)
if is_gen(a) && b < 2*length(numerator(R)) - 3 # special case powers of generator
return R(deepcopy(power_precomp(R, b)),
deepcopy(power_precomp_den(R, b)))
elseif b == 0
return one(R)
elseif iszero(a)
return zero(R)
elseif length(numerator(a, false)) == 1
return R(coeff(a, 0)^b)
elseif b == 1
return deepcopy(a)
else
bit = ~((~UInt(0)) >> 1)
while (UInt(bit) & b) == 0
bit >>= 1
end
z = a
bit >>= 1
while bit != 0
z = z*z
if (UInt(bit) & b) != 0
z *= a
end
bit >>= 1
end
return z
end
end
###############################################################################
#
# Comparison
#
###############################################################################
function ==(a::FunctionFieldElem{T}, b::FunctionFieldElem{T}) where T <: FieldElement
check_parent(a, b)
aden = denominator(a, true)
bden = denominator(b, true)
if aden != bden
return false
end
anum = numerator(a, true)
bnum = numerator(b, true)
return anum == bnum
end
###############################################################################
#
# Ad hoc comparison
#
###############################################################################
function ==(a::FunctionFieldElem{T}, b::RationalFunctionFieldElem{T, U}) where {T <: FieldElement, U <: PolyRingElem}
parent(b) != base_ring(a) && error("Unable to coerce element")
if iszero(a) && iszero(b)
return true
elseif length(numerator(a, false)) != 1
return false
end
return a == parent(a)(b)
end
==(a::RationalFunctionFieldElem{T, U}, b::FunctionFieldElem{T}) where {T <: FieldElement, U <: PolyRingElem} = b == a
==(a::FunctionFieldElem, b::Union{Integer, Rational}) = a == base_ring(a)(b)
==(a::Union{Integer, Rational}, b::FunctionFieldElem) = b == a
###############################################################################
#
# Inversion
#
###############################################################################
function Base.inv(a::FunctionFieldElem)
R = parent(a)
anum = numerator(a, false)
aden = denominator(a, false)
G, G_den, S, S_den, T, T_den =
_rat_poly_gcdx(anum, aden, numerator(R), denominator(R))
return R(S, S_den)
end
###############################################################################
#
# Exact division
#
###############################################################################
function divexact(a::FunctionFieldElem{T},
b::FunctionFieldElem{T}; check::Bool=true) where T <: FieldElement
return a*inv(b)
end
###############################################################################
#
# Ad hoc exact division
#
###############################################################################
function divexact(a::FunctionFieldElem,
b::Union{Rational, Integer}; check::Bool=true)
S = parent(a)
anum = numerator(a, false)
aden = denominator(a, false)
R = parent(aden)
rnum, rden = _rat_poly_canonicalise(anum, R(b))
return S(rnum, rden*aden)
end
function divexact(a::FunctionFieldElem{T},
b::T; check::Bool=true) where T <: FieldElem
S = parent(a)
anum = numerator(a, false)
aden = denominator(a, false)
R = parent(aden)
rnum, rden = _rat_poly_canonicalise(anum, R(b))
return S(rnum, rden*aden)