-
Notifications
You must be signed in to change notification settings - Fork 63
/
Poly.jl
3437 lines (3101 loc) · 92.3 KB
/
Poly.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
###############################################################################
#
# Poly.jl : Univariate polynomials
#
###############################################################################
###############################################################################
#
# Data type and parent object methods
#
###############################################################################
base_ring_type(::Type{<:PolyRing{T}}) where T<:RingElement = parent_type(T)
coefficient_ring(R::PolyRing) = base_ring(R)
dense_poly_type(::Type{T}) where T<:RingElement = Generic.Poly{T}
function is_domain_type(::Type{T}) where {S <: RingElement, T <: PolyRingElem{S}}
return is_domain_type(S)
end
function is_exact_type(a::Type{T}) where {S <: RingElement, T <: PolyRingElem{S}}
return is_exact_type(S)
end
@doc raw"""
var(a::PolyRing)
Return the internal name of the generator of the polynomial ring. Note that
this is returned as a `Symbol` not a `String`.
"""
var(a::PolyRing)
@doc raw"""
symbols(a::PolyRing)
Return an array of the variable names for the polynomial ring. Note that
this is returned as an array of `Symbol` not `String`.
"""
symbols(a::PolyRing) = [var(a)]
@doc raw"""
number_of_variables(a::PolyRing)
Return the number of variables of the polynomial ring, which is 1.
"""
number_of_variables(a::PolyRing) = 1
characteristic(a::PolyRing) = characteristic(base_ring(a))
Base.copy(a::PolyRingElem) = deepcopy(a)
###############################################################################
#
# Basic manipulation
#
###############################################################################
function Base.hash(a::PolyRingElem, h::UInt)
b = 0x53dd43cd511044d1%UInt
for i in 0:length(a) - 1
b = xor(b, xor(hash(coeff(a, i), h), h))
b = (b << 1) | (b >> (sizeof(Int)*8 - 1))
end
return b
end
@doc raw"""
length(a::PolynomialElem)
Return the length of the polynomial. The length of a univariate polynomial is
defined to be the number of coefficients in its dense representation, including
zero coefficients. Thus naturally the zero polynomial has length zero and
additionally for nonzero polynomials the length is one more than the degree.
(Note that the leading coefficient will always be nonzero.)
"""
length(a::PolynomialElem)
@doc raw"""
degree(a::PolynomialElem)
Return the degree of the given polynomial. This is defined to be one less
than the length, even for constant polynomials.
"""
degree(a::PolynomialElem) = length(a) - 1
@doc raw"""
is_constant(a::PolynomialElem)
Return `true` if `a` is a degree zero polynomial or the zero polynomial, i.e.
a constant polynomial.
"""
function is_constant(a::PolynomialElem)
return length(a) <= 1
end
@doc raw"""
modulus(a::PolyRingElem{T}) where {T <: ResElem}
Return the modulus of the coefficients of the given polynomial.
"""
modulus(a::PolyRingElem{T}) where {T <: ResElem} = modulus(base_ring(a))
@doc raw"""
leading_coefficient(a::PolynomialElem)
Return the leading coefficient of the given polynomial. This will be the
nonzero coefficient of the term with highest degree unless the polynomial
in the zero polynomial, in which case a zero coefficient is returned.
"""
function leading_coefficient(a::PolynomialElem)
return length(a) == 0 ? zero(base_ring(a)) : coeff(a, length(a) - 1)
end
@doc raw"""
trailing_coefficient(a::PolynomialElem)
Return the trailing coefficient of the given polynomial. This will be the
nonzero coefficient of the term with lowest degree unless the polynomial
is the zero polynomial, in which case a zero coefficient is returned.
"""
function trailing_coefficient(a::PolynomialElem)
if iszero(a)
return zero(base_ring(a))
else
for i = 1:length(a)
c = coeff(a, i - 1)
if !iszero(c)
return c
end
end
return coeff(a, length(a) - 1)
end
end
@doc raw"""
constant_coefficient(a::PolynomialElem)
Return the constant coefficient of the given polynomial. If the polynomial is
the zero polynomial, the function will return zero.
"""
function constant_coefficient(a::PolynomialElem)
if iszero(a)
return zero(base_ring(a))
end
return coeff(a, 0)
end
@doc raw"""
tail(a::PolynomialElem)
Return the tail of the given polynomial, i.e. the polynomial without its
leading term (if any).
"""
function tail(a::PolynomialElem)
return iszero(a) ? zero(parent(a)) : truncate(a, length(a) - 1)
end
@doc raw"""
set_coefficient!(c::PolynomialElem{T}, n::Int, a::T) where T <: RingElement
set_coefficient!(c::PolynomialElem{T}, n::Int, a::U) where {T <: RingElement, U <: Integer}
Set the coefficient of degree $n$ to $a$.
"""
function set_coefficient!(c::PolynomialElem{T}, n::Int, a::T) where T <: RingElement
return setcoeff!(c, n, a) # merely acts as generic fallback
end
function set_coefficient!(c::PolynomialElem{T}, n::Int, a::U) where {T <: RingElement, U <: Integer}
return setcoeff!(c, n, base_ring(c)(a)) # merely acts as generic fallback
end
function set_coefficient!(c::PolynomialElem{T}, n::Int, a::T) where T <: Integer
return setcoeff!(c, n, a) # merely acts as generic fallback
end
zero(R::PolyRing) = R(zero(base_ring(R)))
one(R::PolyRing) = R(one(base_ring(R)))
@doc raw"""
gen(R::PolyRing)
Return the generator of the given polynomial ring.
"""
gen(R::PolyRing) = R([zero(base_ring(R)), one(base_ring(R))])
@doc raw"""
gens(R::PolyRing)
Return an array containing the generator of the given polynomial ring.
"""
gens(R::PolyRing) = [gen(R)]
number_of_generators(R::PolyRing) = 1
iszero(a::PolynomialElem) = length(a) == 0
isone(a::PolynomialElem) = length(a) == 1 && isone(coeff(a, 0))
@doc raw"""
is_gen(a::PolynomialElem)
Return `true` if the given polynomial is the constant generator of its
polynomial ring, otherwise return `false`.
"""
function is_gen(a::PolynomialElem)
return length(a) <= 2 && isone(coeff(a, 1)) && iszero(coeff(a, 0))
end
@doc raw"""
is_monic(a::PolynomialElem)
Return `true` if the given polynomial is monic, i.e. has leading coefficient
equal to one, otherwise return `false`.
"""
function is_monic(a::PolynomialElem)
return isone(leading_coefficient(a))
end
function is_unit(a::PolynomialElem)
if length(a) <= 1
return is_unit(coeff(a, 0))
elseif is_domain_type(elem_type(coefficient_ring(a)))
return false
elseif !is_unit(coeff(a, 0)) || is_unit(coeff(a, length(a) - 1))
return false
else
throw(NotImplementedError(:is_unit, a))
end
end
is_zero_divisor(a::PolynomialElem) = is_zero_divisor(content(a))
function is_zero_divisor_with_annihilator(a::PolyRingElem{T}) where T <: RingElement
f, b = is_zero_divisor_with_annihilator(content(a))
return f, parent(a)(b)
end
###############################################################################
#
# Monomial and term
#
###############################################################################
@doc raw"""
is_term(a::PolynomialElem)
Return `true` if the given polynomial has one term.
"""
function is_term(a::PolynomialElem)
if iszero(a)
return false
end
for i = 1:length(a) - 1
if !iszero(coeff(a, i - 1))
return false
end
end
return true
end
is_term_recursive(a::T) where T <: RingElement = true
@doc raw"""
is_term_recursive(a::PolynomialElem)
Return `true` if the given polynomial has one term. This function is
recursive, with all scalar types returning true.
"""
function is_term_recursive(a::PolynomialElem)
if !is_term_recursive(leading_coefficient(a))
return false
end
for i = 1:length(a) - 1
if !iszero(coeff(a, i - 1))
return false
end
end
return true
end
@doc raw"""
is_monomial(a::PolynomialElem)
Return `true` if the given polynomial is a monomial.
"""
function is_monomial(a::PolynomialElem)
return is_one(leading_coefficient(a)) && is_term(a)
end
is_monomial_recursive(a::T) where T <: RingElement = isone(a)
@doc raw"""
is_monomial_recursive(a::PolynomialElem)
Return `true` if the given polynomial is a monomial. This function is
recursive, with all scalar types returning true.
"""
function is_monomial_recursive(a::PolynomialElem)
if !is_monomial_recursive(leading_coefficient(a))
return false
end
for i = 1:length(a) - 1
if !iszero(coeff(a, i - 1))
return false
end
end
return true
end
###############################################################################
#
# Similar and zero
#
###############################################################################
function similar(x::PolyRingElem, R::Ring, s::VarName=var(parent(x)); cached::Bool=true)
TT = elem_type(R)
V = Vector{TT}(undef, 0)
p = Generic.Poly{TT}(V)
# Default similar is supposed to return a polynomial
if base_ring(x) === R && Symbol(s) == var(parent(x)) && x isa Generic.Poly{TT}
# steal parent in case it is not cached
p.parent = parent(x)
else
p.parent = Generic.PolyRing{TT}(R, Symbol(s), cached)
end
p = set_length!(p, 0)
return p
end
similar(x::PolyRingElem, var::VarName=var(parent(x)); cached::Bool=true) =
similar(x, base_ring(x), Symbol(var); cached)
zero(p::PolyRingElem, R::Ring, var::VarName=var(parent(p)); cached::Bool=true) =
similar(p, R, var; cached=cached)
zero(p::PolyRingElem, var::VarName=var(parent(p)); cached::Bool=true) =
similar(p, base_ring(p), var; cached=cached)
###############################################################################
#
# polynomial constructor
#
###############################################################################
function polynomial(R::Ring, arr::Vector{T}, var::VarName=:x; cached::Bool=true) where T
TT = elem_type(R)
coeffs = T == Any && length(arr) == 0 ? elem_type(R)[] : map(R, arr)
p = Generic.Poly{TT}(coeffs)
# Default is supposed to return a polynomial
p.parent = Generic.PolyRing{TT}(R, Symbol(var), cached)
return p
end
###############################################################################
#
# Iterators
#
###############################################################################
@doc raw"""
exponent_vectors(a::PolyRingElem)
Return an iterator for the exponent vectors of the given polynomial. The
exponent vectors will have length 1 and may correspond to terms with zero
coefficient but will not give exponents higher than the degree.
"""
function exponent_vectors(a::PolyRingElem)
return Generic.MPolyExponentVectors(a)
end
struct PolyCoeffs{T <: RingElement}
f::T
end
function coefficients(f::PolyRingElem)
return PolyCoeffs(f)
end
function Base.iterate(PC::PolyCoeffs{<:PolyRingElem}, st::Int = -1)
st += 1
if st > degree(PC.f)
return nothing
else
return coeff(PC.f, st), st
end
end
function Base.iterate(PCR::Iterators.Reverse{<:PolyCoeffs{<:PolyRingElem}},
st::Int = degree(PCR.itr.f) + 1)
st -= 1
if st < 0
return nothing
else
return coeff(PCR.itr.f, st), st
end
end
Base.IteratorEltype(M::PolyRingElem) = Base.HasEltype()
Base.eltype(M::PolyRingElem{T}) where {T} = T
Base.eltype(M::PolyCoeffs) = Base.eltype(M.f)
Base.eltype(M::Iterators.Reverse{<:PolyCoeffs}) = Base.eltype(M.itr.f)
Base.eltype(M::Iterators.Take{<:PolyCoeffs}) = Base.eltype(M.xs.f)
Base.eltype(M::Iterators.Take{<:Iterators.Reverse{<:PolyCoeffs}}) = Base.eltype(M.xs.itr.f)
Base.IteratorSize(M::PolyCoeffs{<:PolyRingElem}) = Base.HasLength()
Base.length(M::PolyCoeffs{<:PolyRingElem}) = length(M.f)
function Base.lastindex(a::PolyCoeffs{<:PolyRingElem})
return degree(a.f)
end
function Base.getindex(a::PolyCoeffs{<:PolyRingElem}, i::Int)
return coeff(a.f, i)
end
function Base.getindex(a::Iterators.Reverse{<:PolyCoeffs{<:PolyRingElem}}, i::Int)
return coeff(a.itr.f, degree(a.itr.f) - i)
end
###############################################################################
#
# Canonicalisation
#
###############################################################################
canonical_unit(x::PolynomialElem) = canonical_unit(leading_coefficient(x))
###############################################################################
#
# String I/O
#
###############################################################################
function expressify(@nospecialize(a::Union{PolynomialElem, NCPolyRingElem}),
x = var(parent(a)); context = nothing)
sum = Expr(:call, :+)
for k in degree(a):-1:0
c = coeff(a, k)
if !iszero(c)
xk = k < 1 ? 1 : k == 1 ? x : Expr(:call, :^, x, k)
if isone(c)
push!(sum.args, Expr(:call, :*, xk))
else
push!(sum.args, Expr(:call, :*, expressify(c, context = context), xk))
end
end
end
return sum
end
@enable_all_show_via_expressify Union{PolynomialElem, NCPolyRingElem}
function show(io::IO, p::PolyRing)
@show_name(io, p)
@show_special(io, p)
if is_terse(io)
print(io, "Univariate polynomial ring")
else
io = pretty(io)
print(io, "Univariate polynomial ring in ", var(p), " over ")
print(terse(io), Lowercase(), base_ring(p))
end
end
###############################################################################
#
# Unary operations
#
###############################################################################
function -(a::PolynomialElem)
len = length(a)
z = parent(a)()
fit!(z, len)
for i = 1:len
z = setcoeff!(z, i - 1, -coeff(a, i - 1))
end
z = set_length!(z, len)
return z
end
###############################################################################
#
# Binary operations
#
###############################################################################
function +(a::PolyRingElem{T}, b::PolyRingElem{T}) where T <: RingElement
check_parent(a, b)
lena = length(a)
lenb = length(b)
lenz = max(lena, lenb)
z = parent(a)()
fit!(z, lenz)
i = 1
while i <= min(lena, lenb)
z = setcoeff!(z, i - 1, coeff(a, i - 1) + coeff(b, i - 1))
i += 1
end
while i <= lena
z = setcoeff!(z, i - 1, deepcopy(coeff(a, i - 1)))
i += 1
end
while i <= lenb
z = setcoeff!(z, i - 1, deepcopy(coeff(b, i - 1)))
i += 1
end
z = set_length!(z, normalise(z, i - 1))
return z
end
function -(a::PolyRingElem{T}, b::PolyRingElem{T}) where T <: RingElement
check_parent(a, b)
lena = length(a)
lenb = length(b)
lenz = max(lena, lenb)
z = parent(a)()
fit!(z, lenz)
i = 1
while i <= min(lena, lenb)
z = setcoeff!(z, i - 1, coeff(a, i - 1) - coeff(b, i - 1))
i += 1
end
while i <= lena
z = setcoeff!(z, i - 1, deepcopy(coeff(a, i - 1)))
i += 1
end
while i <= lenb
z = setcoeff!(z, i - 1, -coeff(b, i - 1))
i += 1
end
z = set_length!(z, normalise(z, i - 1))
return z
end
@doc raw"""
mul_karatsuba(a::PolyRingElem{T}, b::PolyRingElem{T}) where T <: RingElement
Return $a \times b$ using the Karatsuba algorithm.
"""
function mul_karatsuba(a::PolyRingElem{T}, b::PolyRingElem{T}) where T <: RingElement
# we assume len(a) != 0 != lenb and parent(a) == parent(b)
lena = length(a)
lenb = length(b)
m = div(max(lena, lenb) + 1, 2)
if m < lena
a1 = shift_right(a, m)
a0 = truncate(a, m)
else
return a*truncate(b, m) + shift_left(a*shift_right(b, m), m)
end
if a !== b
if m < lenb
b1 = shift_right(b, m)
b0 = truncate(b, m)
else
return b*truncate(a, m) + shift_left(b*shift_right(a, m), m)
end
else
b1 = a1
b0 = a0
end
z0 = a0*b0
z2 = a1*b1
if a !== b
z1 = (a1 + a0)*(b1 + b0) - z2 - z0
else
s = a1 + a0
z1 = s*s - z2 - z0
end
r = parent(a)()
fit!(r, lena + lenb - 1)
for i = 1:length(z0)
r = setcoeff!(r, i - 1, coeff(z0, i - 1))
end
for i = length(z0) + 1:2m
r = setcoeff!(r, i - 1, base_ring(a)())
end
for i = 1:length(z2)
r = setcoeff!(r, 2m + i - 1, coeff(z2, i - 1))
end
for i = 1:length(z1)
u = coeff(r, i + m - 1)
u = add!(u, coeff(z1, i - 1))
setcoeff!(r, i + m - 1, u)
end
# necessary for finite characteristic
r = set_length!(r, normalise(r, length(r)))
return r
end
function mul_ks(a::PolyRingElem{T}, b::PolyRingElem{T}) where {T <: PolyRingElem}
lena = length(a)
lenb = length(b)
if lena == 0 || lenb == 0
return parent(a)()
end
maxa = 0
nza = 0
for i = 1:lena
lenc = length(coeff(a, i - 1))
maxa = max(lenc, maxa)
nza += (lenc == 0 ? 0 : 1)
end
if a !== b
maxb = 0
nzb = 0
for i = 1:lenb
lenc = length(coeff(b, i - 1))
maxb = max(lenc, maxb)
nzb += (lenc == 0 ? 0 : 1)
end
else
maxb = maxa
nzb = nza
end
if nza*nzb < 4*max(lena, lenb)
return mul_classical(a, b)
end
m = maxa + maxb - 1
z = base_ring(base_ring(a))()
A1 = Vector{elem_type(base_ring(base_ring(a)))}(undef, m*lena)
for i = 1:lena
c = coeff(a, i - 1)
for j = 1:length(c)
A1[(i - 1)*m + j] = coeff(c, j - 1)
end
for j = length(c) + 1:m
A1[(i - 1)*m + j] = z
end
end
ksa = base_ring(a)(A1)
if a !== b
A2 = Vector{elem_type(base_ring(base_ring(a)))}(undef, m*lenb)
for i = 1:lenb
c = coeff(b, i - 1)
for j = 1:length(c)
A2[(i - 1)*m + j] = coeff(c, j - 1)
end
for j = length(c) + 1:m
A2[(i - 1)*m + j] = z
end
end
ksb = base_ring(b)(A2)
else
ksb = ksa
end
p = ksa*ksb
r = parent(a)()
lenr = lena + lenb - 1
fit!(r, lenr)
for i = 1:lenr
u = coeff(r, i - 1)
fit!(u, m)
for j = 1:m
u = setcoeff!(u, j - 1, coeff(p, (i - 1)*m + j - 1))
end
setcoeff!(r, i - 1, u)
end
r = set_length!(r, normalise(r, lenr))
return r
end
function mul_classical(a::PolyRingElem{T}, b::PolyRingElem{T}) where T <: RingElement
lena = length(a)
lenb = length(b)
if lena == 0 || lenb == 0
return parent(a)()
end
R = base_ring(a)
t = R()
lenz = lena + lenb - 1
d = Vector{T}(undef, lenz)
for i = 1:lena
d[i] = mul_red!(R(), coeff(a, i - 1), coeff(b, 0), false)
end
for i = 2:lenb
d[lena + i - 1] = mul_red!(R(), coeff(a, lena - 1), coeff(b, i - 1), false)
end
for i = 1:lena - 1
for j = 2:lenb
t = mul_red!(t, coeff(a, i - 1), coeff(b, j - 1), false)
d[i + j - 1] = add!(d[i + j - 1], t)
end
end
for i = 1:lenz
d[i] = reduce!(d[i])
end
z = parent(a)(d)
z = set_length!(z, normalise(z, lenz))
return z
end
function use_karamul(a::PolyRingElem{T}, b::PolyRingElem{T}) where T <: RingElement
return false
end
function *(a::PolyRingElem{T}, b::PolyRingElem{T}) where T <: RingElement
check_parent(a, b)
# karatsuba recurses into * so check lengths are > 1
if use_karamul(a, b) && length(a) > 1 && length(b) > 1
return mul_karatsuba(a, b)
else
return mul_classical(a, b)
end
end
###############################################################################
#
# Ad hoc binary operators
#
###############################################################################
function *(a::T, b::PolyRingElem{T}) where {T <: RingElem}
len = length(b)
z = parent(b)()
fit!(z, len)
for i = 1:len
z = setcoeff!(z, i - 1, a*coeff(b, i - 1))
end
z = set_length!(z, normalise(z, len))
return z
end
function *(a::Union{Integer, Rational, AbstractFloat}, b::PolynomialElem)
len = length(b)
z = parent(b)()
fit!(z, len)
for i = 1:len
z = setcoeff!(z, i - 1, a*coeff(b, i - 1))
end
z = set_length!(z, normalise(z, len))
return z
end
*(a::PolyRingElem{T}, b::T) where {T <: RingElem} = b*a
*(a::PolynomialElem, b::Union{Integer, Rational, AbstractFloat}) = b*a
###############################################################################
#
# Powering
#
###############################################################################
function pow_multinomial(a::PolyRingElem{T}, e::Int) where T <: RingElement
e < 0 && throw(DomainError(e, "exponent must be >= 0"))
lena = length(a)
lenz = (lena - 1) * e + 1
res = Vector{T}(undef, lenz)
for k = 1:lenz
res[k] = base_ring(a)()
end
d = base_ring(a)()
first = coeff(a, 0)
res[1] = first ^ e
for k = 1 : lenz - 1
u = -k
for i = 1 : min(k, lena - 1)
t = coeff(a, i) * res[(k - i) + 1]
u += e + 1
res[k + 1] = add!(res[k + 1], t * u)
end
d = add!(d, first)
res[k + 1] = divexact(res[k + 1], d)
end
z = parent(a)(res)
return z
end
@doc raw"""
^(a::PolyRingElem{T}, b::Int) where T <: RingElement
Return $a^b$. We require $b \geq 0$.
"""
function ^(a::PolyRingElem{T}, b::Int) where T <: RingElement
b < 0 && throw(DomainError(b, "exponent must be >= 0"))
# special case powers of x for constructing polynomials efficiently
R = parent(a)
if is_gen(a)
z = R()
fit!(z, b + 1)
z = setcoeff!(z, b, deepcopy(coeff(a, 1)))
for i = 1:b
z = setcoeff!(z, i - 1, deepcopy(coeff(a, 0)))
end
z = set_length!(z, b + 1)
return z
elseif b == 0
return one(R)
elseif length(a) == 0
return zero(R)
elseif length(a) == 1
return R(coeff(a, 0)^b)
elseif b == 1
return deepcopy(a)
else
if T <: FieldElement && characteristic(base_ring(R)) == 0
zn = 0
while iszero(coeff(a, zn))
zn += 1
end
if length(a) - zn < 8 && b > 4
f = shift_right(a, zn)
return shift_left(pow_multinomial(f, b), zn*b)
end
end
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
###############################################################################
#
# Comparisons
#
###############################################################################
@doc raw"""
==(x::PolyRingElem{T}, y::PolyRingElem{T}) where T <: RingElement
Return `true` if $x == y$ arithmetically, otherwise return `false`. Recall
that power series to different precisions may still be arithmetically
equal to the minimum of the two precisions.
"""
function ==(x::PolyRingElem{T}, y::PolyRingElem{T}) where T <: RingElement
b = check_parent(x, y, false)
!b && return false
if length(x) != length(y)
return false
else
for i = 1:length(x)
if coeff(x, i - 1) != coeff(y, i - 1)
return false
end
end
end
return true
end
@doc raw"""
isequal(x::PolyRingElem{T}, y::PolyRingElem{T}) where T <: RingElement
Return `true` if $x == y$ exactly, otherwise return `false`. This function is
useful in cases where the coefficients of the polynomial are inexact, e.g.
power series. Only if the power series are precisely the same, to the same
precision, are they declared equal by this function.
"""
function isequal(x::PolyRingElem{T}, y::PolyRingElem{T}) where T <: RingElement
if parent(x) != parent(y)
return false
end
if length(x) != length(y)
return false
end
for i = 1:length(x)
if !isequal(coeff(x, i - 1), coeff(y, i - 1))
return false
end
end
return true
end
###############################################################################
#
# Ad hoc comparisons
#
###############################################################################
@doc raw"""
==(x::PolyRingElem{T}, y::T) where {T <: RingElem}
Return `true` if $x == y$.
"""
==(x::PolyRingElem{T}, y::T) where T <: RingElem = ((length(x) == 0 && iszero(y))
|| (length(x) == 1 && coeff(x, 0) == y))
@doc raw"""
==(x::PolynomialElem, y::Union{Integer, Rational, AbstractFloat})
Return `true` if $x == y$ arithmetically, otherwise return `false`.
"""
==(x::PolynomialElem, y::Union{Integer, Rational, AbstractFloat}) = ((length(x) == 0 && iszero(base_ring(x)(y)))
|| (length(x) == 1 && coeff(x, 0) == y))
@doc raw"""
==(x::T, y::PolyRingElem{T}) where T <: RingElem = y == x
Return `true` if $x = y$.
"""
==(x::T, y::PolyRingElem{T}) where T <: RingElem = y == x
@doc raw"""
==(x::Union{Integer, Rational, AbstractFloat}, y::PolyRingElem)
Return `true` if $x == y$ arithmetically, otherwise return `false`.
"""
==(x::Union{Integer, Rational, AbstractFloat}, y::PolyRingElem) = y == x
###############################################################################
#
# Approximation
#
###############################################################################
function Base.isapprox(f::PolynomialElem, g::PolynomialElem; atol::Real=sqrt(eps()))
check_parent(f, g)
nmin = min(length(f), length(g))
i = 1
while i <= nmin
if !isapprox(coeff(f, i - 1), coeff(g, i - 1); atol=atol)
return false
end
i += 1
end
while i <= length(f)
if !isapprox(coeff(f, i - 1), 0; atol=atol)
return false
end
i += 1
end
while i <= length(g)
if !isapprox(coeff(g, i - 1), 0; atol=atol)
return false
end
i += 1
end
return true
end
function Base.isapprox(f::PolynomialElem{T}, g::T; atol::Real=sqrt(eps())) where T
return isapprox(f, parent(f)(g); atol=atol)
end
function Base.isapprox(f::T, g::PolynomialElem{T}; atol::Real=sqrt(eps())) where T
return isapprox(parent(g)(f), g; atol=atol)
end
###############################################################################
#
# Truncation
#
###############################################################################
@doc raw"""
truncate(a::PolynomialElem, n::Int)
Return $a$ truncated to $n$ terms, i.e. the remainder upon division by $x^n$.
"""
function truncate(a::PolynomialElem, n::Int)
lena = length(a)
if lena <= n
return a
end
lenz = min(lena, n)
z = parent(a)()
fit!(z, lenz)
for i = 1:lenz
z = setcoeff!(z, i - 1, coeff(a, i - 1))
end
z = set_length!(z, normalise(z, lenz))
return z
end
@doc raw"""
mullow(a::PolyRingElem{T}, b::PolyRingElem{T}, n::Int) where T <: RingElement
Return $a\times b$ truncated to $n$ terms.
"""
function mullow(a::PolyRingElem{T}, b::PolyRingElem{T}, n::Int) where T <: RingElement
check_parent(a, b)
lena = length(a)
lenb = length(b)
if lena == 0 || lenb == 0
return zero(parent(a))
end
if n < 0