-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathStandardBasis.jl
1825 lines (1512 loc) · 60.7 KB
/
StandardBasis.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
using LinearAlgebra
using OffsetArrays, StaticArrays
import Polynomials: indeterminate, ⟒
## Test standard basis polynomials with (nearly) the same tests
# compare upto trailing zeros
function upto_tz(as, bs)
n,m = findlast.(!iszero, (as,bs))
n == m || return false
isnothing(n) && return true
for i in 1:n
!(as[i] ≈ bs[i]) && return false
end
true
end
upto_z(as, bs) = upto_tz(filter(!iszero,as), filter(!iszero,bs))
# compare upto trailing zeros infix operator
==ᵗ⁰(a,b) = upto_tz(a,b)
==ᵗᶻ(a,b) = upto_z(a,b)
==ᵟ(a,b) = (a == b)
==ᵟ(a::FactoredPolynomial, b::FactoredPolynomial) = a ≈ b
_isimmutable(::Type{P}) where {P <: Union{ImmutablePolynomial, FactoredPolynomial}} = true
_isimmutable(P) = false
Ps = (ImmutablePolynomial, Polynomial, SparsePolynomial, LaurentPolynomial, FactoredPolynomial, Polynomials.SparseVectorPolynomial)
@testset "Construction" begin
@testset for coeff in Any[
Int64[1, 1, 1, 1],
Float32[1, -4, 2],
ComplexF64[1 - 1im, 2 + 3im],
[3 // 4, -2 // 1, 1 // 1]
]
@testset for P in Ps
p = P(coeff)
@test coeffs(p) ==ᵗ⁰ coeff
@test degree(p) == length(coeff) - 1
@test indeterminate(p) == :x
P == Polynomial && @test length(p) == length(coeff)
P == Polynomial && @test size(p) == size(coeff)
P == Polynomial && @test size(p, 1) == size(coeff, 1)
P == Polynomial && @test typeof(p).parameters[2] == eltype(coeff)
if !(eltype(coeff) <: Real && P == FactoredPolynomial) # roots may be complex
@test eltype(p) == eltype(coeff)
end
@test all([-200, -0.3, 1, 48.2] .∈ Polynomials.domain(p))
## issue #316
@test_throws InexactError P{Int,:x}([1+im, 1])
@test_throws InexactError P{Int}([1+im, 1], :x)
@test_throws InexactError P{Int,:x}(1+im)
@test_throws InexactError P{Int}(1+im)
## issue #395
v = [1,2,3]
@test P(v) == P(v,:x) == P(v,'x') == P(v,"x") == P(v, Polynomials.Var(:x))
## issue #452
ps = (1, 1.0)
P != FactoredPolynomial && @test eltype(P(ps)) == eltype(promote(ps...))
## issue 464
Polynomials.@variable z # not exported
@test z^2 + 2 == Polynomial([2,0,1], :z)
end
end
end
@testset "Mapdomain" begin
@testset for P in Ps
x = -30:20
mx = mapdomain(P, x)
@test mx == x
x = 0.5:0.01:0.6
mx = mapdomain(P, x)
@test mx == x
end
end
# Custom offset vector type to test constructors
struct ZVector{T,A<:AbstractVector{T}} <: AbstractVector{T}
x :: A
offset :: Int
function ZVector(x::AbstractVector)
offset = firstindex(x)
new{eltype(x),typeof(x)}(x, offset)
end
end
Base.parent(z::ZVector) = z.x
Base.size(z::ZVector) = size(parent(z))
Base.axes(z::ZVector) = (OffsetArrays.IdentityUnitRange(0:size(z,1)-1),)
Base.getindex(z::ZVector, I::Int) = parent(z)[I + z.offset]
@testset "Other Construction" begin
@testset for P in Ps
# Leading 0s
p = P([1, 2, 0, 0])
@test coeffs(p) ==ᵗ⁰ [1, 2]
P == Polynomial && @test length(p) == 2
# different type
p = P{Float64}(ones(Int32, 4))
@test coeffs(p) ==ᵗ⁰ ones(Float64, 4)
p = P(30)
@test coeffs(p) ==ᵗ⁰ [30]
p = zero(P{Int})
@test coeffs(p) ==ᵗ⁰ [0]
p = one(P{Int})
@test coeffs(p) ==ᵗ⁰ [1]
pNULL = P(Int[])
@test iszero(pNULL)
P != LaurentPolynomial && @test degree(pNULL) == -1
p0 = P([0])
@test iszero(p0)
P != LaurentPolynomial && @test degree(p0) == -1
# P(2) is 2 (not 2p₀) convert(Polynomial, P(s::Number)) = Polynomial(s)
@test convert(Polynomial, P(2)) ≈ Polynomial(2)
@test P(2) ≈ 2*one(P)
# variable(), P() to generate `x` in given basis
@test degree(variable(P)) == 1
@test variable(P)(1) == 1
@test degree(P()) == 1
@test P()(1) == 1
@test variable(P, :y) == P(:y)
# test degree, isconstant
P != LaurentPolynomial && @test degree(zero(P)) == -1
@test degree(one(P)) == 0
@test degree(P(1)) == 0
@test degree(P([1])) == 0
@test degree(P(:x)) == 1
@test degree(variable(P)) == 1
@test degree(Polynomials.basis(P,5)) == 5
@test Polynomials.isconstant(P(1))
@test !Polynomials.isconstant(variable(P))
end
end
@testset "Non-number type" begin
conv = Polynomials.conv
@testset "T=Polynomial{Int,:y}" begin
@testset for P in (Polynomial,)
T = P{Int, :y}
a,b,c = T([1]), T([1,2]), T([1,2,3])
p = P([a,b,c])
q = P([a,b])
s = 2
d = c
# scalar product
@test s*p == P([s*cᵢ for cᵢ ∈ [a,b,c]])
@test p*s == P([cᵢ*s for cᵢ ∈ [a,b,c]])
@test_throws ArgumentError d*p == P([d*cᵢ for cᵢ ∈ [a,b,c]]) # can't fix
@test_throws ArgumentError p*d == P([cᵢ*d for cᵢ ∈ [a,b,c]]) # can't fix
# poly add
@test +p == p
@test p + q == P([a+a,b+b,c])
@test p - q == P([a-a,b-b,c])
@test p - p == P([0*a])
# poly mult
@test p * q == P(conv([a,b,c], [a,b]))
@test q * p == P(conv([a,b], [a,b, c]))
# poly powers
@test p^2 == p * p
# evaluation
@test p(s) == a + b * s + c * s * s
@test p(c) == a + b * c + c * c * c
# ∂, ∫
@test derivative(p) == P([b, 2c])
@test integrate(p) == P([0*a, a, b/2, c/3])
# matrix element
pq = [p q]
@test pq[1] == p
@test pq[2] == q
# implicit promotion
@test p + s == P([a+s, b, c])
@test_throws Union{ArgumentError, MethodError} p + d == P([a+d, b, c]) # can't fix
@test p + P([d]) == P([a+d,b,c])
ps = [p s]
@test ps[1] == p
@test ps[2] == s
end
end
@testset "T=Matrix (2x2)" begin
@testset for P ∈ (Polynomial, ImmutablePolynomial)
a,b,c = [1 0; 1 1], [1 0; 2 1], [1 0; 3 1]
p = P([a,b,c])
q = P([a,b])
s = 2
d = [4 1; 1 0]
# scalar product
@test s*p == P([s*cᵢ for cᵢ ∈ [a,b,c]])
@test p*s == P([cᵢ*s for cᵢ ∈ [a,b,c]])
@test d*p == P([d*cᵢ for cᵢ ∈ [a,b,c]])
@test p*d == P([cᵢ*d for cᵢ ∈ [a,b,c]])
# poly add
@test +p == p
@test p + q == P([a+a,b+b,c])
@test p - q == P([a-a,b-b,c])
# poly mult
@test p * q == P(conv([a,b,c], [a,b]))
@test q * p == P(conv([a,b], [a,b, c]))
# poly powers
@test p^2 == p * p
# evaluation
@test p(s) == a + b * s + c * s * s
@test p(c) == a + b * c + c * c * c
# ∂, ∫
@test derivative(p) == P([b, 2c])
@test integrate(p) == P([0*a, a, b/2, c/3])
# matrix element
@test [p q][1] == p
@test [p q][2] == q
# implicit promotion
@test_throws MethodError p + s == P([a+s, b, c]) # OK, no a + s
@test p + d == P([a+d, b, c])
@test p + P([d]) == P([a+d,b,c])
if VERSION < v"1.10"
@test_throws MethodError [p s][1] == p # no promotion T(s)
@test_throws MethodError [p s][2] == s
end
end
end
@testset "T=Vector{Int}" begin
@testset for P ∈ (Polynomial, ImmutablePolynomial)
a,b,c = [1,0,0], [1,1,0], [1,1,1]
p = P([a,b,c])
q = P([a,b])
s = 2
d = [1,2,3]
# scalar product
@test s*p == P([s*cᵢ for cᵢ ∈ [a,b,c]])
@test p*s == P([cᵢ*s for cᵢ ∈ [a,b,c]])
@test_throws MethodError d*p == P([d*cᵢ for cᵢ ∈ [a,b,c]]) # Ok, no * for T
@test_throws MethodError p*d == P([cᵢ*d for cᵢ ∈ [a,b,c]]) # Ok, no * for T
# poly add
@test +p == p
@test p + q == P([a+a,b+b,c])
@test p - q == P([a-a,b-b,c])
# poly mult
@test_throws MethodError p * q == P(conv([a,b,c], [a,b])) # Ok, no * for T
@test_throws MethodError q * p == P(conv([a,b], [a,b, c])) # Ok, no * for T
# poly powers
VERSION >= v"1.8.0" && (@test_throws r"Error" p^2) # (Ok, no * for T) # XXX ArgumentError, not MethodError being thrown on nightly
@test_throws MethodError p * p
# evaluation
@test p(s) == a + b * s + c * s * s
@test_throws MethodError p(c) == a + b * c + c * c * c # OK, no b * c
# ∂, ∫
@test derivative(p) == P([b, 2c])
@test integrate(p) == P([0*a, a, b/2, c/3])
# matrix element
@test [p q][1] == p
@test [p q][2] == q
# implicit promotion
@test_throws MethodError p + s == P([a+s, b, c]) # OK, no a + s
@test p + d == P([a+d, b, c])
@test p + P([d]) == P([a+d,b,c])
# For Immutable + v1.10 this [p s] doesn't error, but is
# error prone
#@test_throws MethodError [p s][1] == p # no promotion T(s)
#@test_throws MethodError [p s][2] == s
end
end
end
@testset "OffsetVector" begin
as = ones(3:4)
bs = parent(as)
@testset for P in Ps
# LaurentPolynomial accepts OffsetArrays; others throw warning
if P ∈ (LaurentPolynomial,)
@test LaurentPolynomial(as) == LaurentPolynomial(bs, 3)
else
@test P(as) == P(bs)
@test P{eltype(as)}(as) == P{eltype(as)}(bs)
# (Or throw an error?)
# @test_throws ArgumentError P(as)
# @test P{eltype(as)}(as) == P{eltype(as)}(bs)
end
end
a = [1,1]
b = OffsetVector(a, axes(a))
c = ZVector(a)
d = ZVector(b)
@testset for P in Ps
if P == LaurentPolynomial && continue
@test P(a) == P(b) == P(c) == P(d)
end
end
end
@testset "Arithmetic" begin
@testset for P in Ps
pNULL = P(Int[])
p0 = P([0])
p1 = P([1,0,0,0,0,0,0,0,0,0,0,0,0,0])
p2 = P([1,1,0,0])
p3 = P([1,2,1,0,0,0,0])
p4 = P([1,3,3,1,0,0])
p5 = P([1,4,6,4,1,0,0,0,0,0,0,0,0,0,0,0,0,0])
pN = P([276,3,87,15,24,0])
pR = P([3 // 4, -2 // 1, 1 // 1])
# type stability of the default constructor without variable name
if !(P ∈ (LaurentPolynomial, ImmutablePolynomial, FactoredPolynomial))
@inferred P([1, 2, 3])
@inferred P([1,2,3], Polynomials.Var(:x))
end
@test p3 == P([1,2,1])
@test pN * 10 == P([2760, 30, 870, 150, 240])
@test pN / 10.0 ≈ P([27.6, 0.3, 8.7, 1.5, 2.4])
@test 10 * pNULL + pN ==ᵟ pN
@test 10 * p0 + pN ==ᵟ pN
@test p5 + 2 * p1 == P([3,4,6,4,1])
@test 10 * pNULL - pN ==ᵟ -pN
@test p0 - pN ==ᵟ -pN
@test p5 - 2 * p1 == P([-1,4,6,4,1])
@test p2 * p2 * p2 == p4
@test p2^4 == p5
@test pNULL^3 == pNULL
@test pNULL * pNULL == pNULL
# if P === Polynomial
# # type stability of multiplication
# @inferred 10 * pNULL
# @inferred 10 * p0
# @inferred p2 * p2
# @inferred p2 * p2
# end
@test pNULL + 2 == p0 + 2 == 2 + p0 == P([2])
@test p2 - 2 == -2 + p2 == P([-1,1])
@test 2 - p2 == P([1,-1])
end
# test inferrability
@testset "Inferrability" for P ∈ (ImmutablePolynomial, LaurentPolynomial, SparsePolynomial, Polynomial)
x = [1,2,3]
T, S = Float64, Int
@testset "constructors" begin
x = [1,2,3]
T, S = Float64, Int
if P == ImmutablePolynomial
x = (1,2,3)
@inferred P{T,:x,4}(x) == P{T,:x,4}(x)
@inferred P{S,:x,4}(x) == P{S,:x,4}(x)
@inferred P{T,:x,3}(x) == P{T,:x,3}(x)
@inferred P{S,:x,3}(x) == P{S,:x,3}(x)
end
@inferred P{T,:x}(x) == P{T,:x}(x)
@inferred P{S,:x}(x) == P{S,:x}(x)
@inferred P{T}(x) == P{T}(x)
@inferred P{S}(x) == P{S}(x)
@inferred P(x) == P(x)
end
@testset "arithmetic" begin
for p ∈ (P(x), zero(P))
q = P(x)^2
@inferred -p
@inferred p + 2
@inferred p * 2
@inferred 2 * p
@inferred p/2
@inferred p + q
@inferred p * q
P != ImmutablePolynomial && @inferred p^2
P != ImmutablePolynomial && @inferred p^3
end
end
@testset "integrate/differentiation" begin
p = P(x)
@inferred integrate(p)
@inferred derivative(p)
end
end
@testset "generic arithmetics" begin
P = Polynomial
# define a set algebra
struct Setalg # not a number
content::Vector{Int}
end
Base.:(+)(a::Setalg, b::Setalg) = Setalg(a.content ∪ b.content)
Base.:(*)(a::Setalg, b::Setalg) = Setalg(vec([x * y for x in a.content, y in b.content]))
Base.zero(::Setalg) = Setalg(Int[])
Base.one(::Setalg) = Setalg(Int[1])
Base.zero(::Type{Setalg}) = Setalg(Int[])
Base.one(::Type{Setalg}) = Setalg(Int[1])
Base.:(==)(a::Setalg, b::Setalg) = a.content == b.content
a = Setalg([1])
b = Setalg([4,2])
pNULL = P(Setalg[])
p0 = P([a])
p1 = P([a, b, b])
@test pNULL * p0 == pNULL
@test pNULL * p1 == pNULL
@test p0 * p1 == p1
end
@testset for P in Ps # ensure promotion of scalar +,*,/
p = P([1,2,3])
@test p + 0.5 ==ᵟ P([1.5, 2.0, 3.0])
@test p / 2 == P([1/2, 1.0, 3/2])
@test p * 0.5 == P([1/2, 1.0, 3/2])
end
# ensure promotion of +,*; issue 215
@testset for P in Ps
p,q = P([1,2,3]), P(im, :θ)
@test p+q == P([1+im, 2, 3])
@test p*q ==ᵟ P(im*[1,2,3])
end
@testset "Laurent" begin
P = LaurentPolynomial
x = variable(P)
x⁻ = inv(x)
p = P([1,2,3], -4)
@test p + 4 == P([1,2,3,0,4],-4)
p = P([1,2,3], 4)
@test p + 4 == P([4,0,0,0,1,2,3])
@test P([1,2,3],-4) + P([1,2,3]) == P([1,2,3,0,1,2,3],-4)
# Laurent polynomials and scalar operations
cs = [1,2,3,4]
p = LaurentPolynomial(cs, -3)
@test p*3 == LaurentPolynomial(cs .* 3, -3)
@test 3*p == LaurentPolynomial(3 .* cs, -3)
# LaurentPolynomial has an inverse for monomials
x = variable(LaurentPolynomial)
@test Polynomials.isconstant(x * inv(x))
@test_throws ArgumentError inv(x + x^2)
end
# issue #395
@testset for P ∈ Ps
P ∈ (FactoredPolynomial, ImmutablePolynomial) && continue
p = P([2,1], :s)
@inferred -p # issue #395
@inferred 2p
@inferred p + p
@inferred p * p
@inferred p^3
end
# evaluation at special cases (degree 0,1; evaluate at 0)
@testset for P ∈ Ps
for T ∈ (Int, Float16, Float64, Complex{Float64})
p₀ = zero(P{T,:X})
@test p₀(0) == zero(T) == Polynomials.constantterm(p₀)
@test p₀(1) == zero(T)
p₁ = P(T[2])
@test p₁(0) == T(2) == Polynomials.constantterm(p₁)
@test p₁(1) == T(2)
end
end
# evaluation at special cases different number types
@testset for P ∈ Ps
P ∈ (SparsePolynomial, Polynomials.SparseVectorPolynomial, FactoredPolynomial) && continue
# vector coefficients
v₀, v₁ = [1,1,1], [1,2,3]
p₁ = P([v₀])
@test p₁(0) == v₀ == Polynomials.constantterm(p₁)
P != ImmutablePolynomial && @test_throws MethodError (0 * p₁)(0) # no zero(Vector{Int}) # XXX
p₂ = P([v₀, v₁])
@test p₂(0) == v₀ == Polynomials.constantterm(p₂)
@test p₂(2) == v₀ + 2v₁
# matrix arguments
# for matrices like pₒI + p₁X + p₂X² + ⋯
p = P([1])
x = [1 2; 3 4]
@test p(x) == 1*I
@test (0p)(x) == 0*I
p = P([1,2])
@test p(x) == 1*I + 2*x
p = P([1,2,3])
@test p(x) == 1*I + 2*x + 3x^2
end
# p - p requires a zero
@testset for P ∈ Ps
P ∈ (LaurentPolynomial, SparsePolynomial, Polynomials.SparseVectorPolynomial, FactoredPolynomial) && continue
for v ∈ ([1,2,3],
[[1,2,3],[1,2,3]],
[[1 2;3 4], [3 4; 5 6]]
)
p = P(v)
@test p - p == 0*p
end
end
# issue #495, (scalar div fix)
𝐐 = Rational{Int}
v = Polynomial{𝐐}([0//1])
@test eltype(integrate(v)) == 𝐐
end
@testset "Divrem" begin
@testset for P in Ps
P == FactoredPolynomial && continue
p0 = P([0])
p1 = P([1])
p2 = P([5, 6, -3, 2 ,4])
p3 = P([7, -3, 2, 6])
p4 = p2 * p3
pN = P([276,3,87,15,24,0])
pR = P([3 // 4, -2 // 1, 1 // 1])
@test all(divrem(p4, p2) .==ᵟ (p3, zero(p3)))
@test p3 % p2 ==ᵟ p3
@test all((map(abs, coeffs(p2 ÷ p3 - P([1 / 9,2 / 3])))) .< eps())
@test all(divrem(p0, p1) .==ᵟ (p0, p0))
@test all(divrem(p1, p1) .==ᵟ (p1, p0))
@test all(divrem(p2, p2) .==ᵟ (p1, p0))
@test all(divrem(pR, pR) .==ᵟ (one(pR), zero(pR)))
@test_throws DivideError p1 ÷ p0
@test_throws DivideError divrem(p0, p0)
# issue #235
num = P([0.8581454436924945, 0.249671302254737, 0.8048498901050951, 0.1922713965697087]) # degree 3 polynomial
den = P([0.9261520696359462, 0.07141031902098072, 0.378071465860349]) # degree 2 polynomial
q, r = divrem(num,den) # expected degrees: degree(q) = degree(num)-degree(den) = 1, degree(r) = degree(den)-1 = 1
@test num ≈ den*q+r # true
@test degree(q) == 1 # true
degree(r) < degree(den)
end
end
@testset "Comparisons" begin
@testset for P in Ps
pX = P([1, 2, 3, 4, 5])
pS1 = P([1, 2, 3, 4, 5], "s")
pS2 = P([1, 2, 3, 4, 5], 's')
pS3 = P([1, 2, 3, 4, 5], :s)
@test pX != pS1
@test pS1 == pS2
@test pS1 == pS3
@test indeterminate(pS1 + pS1) == indeterminate(pS1)
@test indeterminate(pS1 - pS1) == indeterminate(pS1)
@test indeterminate(pS1 * pS1) == indeterminate(pS1)
@test indeterminate(pS1 ÷ pS1) == indeterminate(pS1)
@test indeterminate(pS1 % pS1) == indeterminate(pS1)
@test_throws ArgumentError pS1 + pX
@test_throws ArgumentError pS1 - pX
@test_throws ArgumentError pS1 * pX
@test_throws ArgumentError pS1 ÷ pX
@test_throws ArgumentError pS1 % pX
# Testing copying.
pcpy1 = P([1,2,3,4,5], :y)
pcpy2 = copy(pcpy1)
@test pcpy1 == pcpy2
# Check for isequal
p1 = P([1.0, -0.0, 5.0, Inf])
p2 = P([1.0, 0.0, 5.0, Inf])
!(P ∈ (FactoredPolynomial, SparsePolynomial, Polynomials.SparseVectorPolynomial)) && (@test p1 == p2 && !isequal(p1, p2)) # SparsePolynomial doesn't store -0.0, 0.0.
p3 = P([0, NaN])
@test p3 === p3 && p3 ≠ p3 && isequal(p3, p3)
p = fromroots(P, [1,2,3])
q = fromroots(P, [1,2,3])
@test hash(p) == hash(q)
p1s = P([1,2], :s)
p1x = P([1,2], :x)
p2s = P([1], :s)
@test p1s == p1s
@test p1s ≠ p1x
@test p1s ≠ p2s
@test_throws ArgumentError p1s ≈ p1x
@test p1s ≉ p2s
@test p1s ≈ P([1,2.], :s)
@test p2s ≈ 1.0 ≈ p2s
@test p2s == 1.0 == p2s
@test p2s ≠ 2.0 ≠ p2s
@test p1s ≠ 2.0 ≠ p1s
@test nnz(map(P, sparse(1.0I, 5, 5))) == 5
@test P([0.5]) + 2 == P([2.5])
@test 2 - P([0.5]) == P([1.5])
# check ≈ for P matches usage for Vector{T} (possibly padded with trailing zeros)
@test (P([NaN]) ≈ P([NaN])) == ([NaN] ≈ [NaN]) # false
@test (P([NaN]) ≈ NaN) == (false)
@test (P([Inf]) ≈ P([Inf])) == ([Inf] ≈ [Inf]) # true
@test (P([Inf]) ≈ Inf) == (true)
!(P <: FactoredPolynomial) && @test (P([1,Inf]) ≈ P([0,Inf])) == ([1,Inf] ≈ [0,Inf]) # false
!(P <: FactoredPolynomial) && @test (P([1,NaN,Inf]) ≈ P([0,NaN, Inf])) == ([1,NaN,Inf] ≈ [0,NaN, Inf]) #false
@test (P([eps(), eps()]) ≈ P([0,0])) == ([eps(), eps()] ≈ [0,0]) # false
@test (P([1,eps(), 1]) ≈ P([1,0,1])) == ([1,eps(), 1] ≈ [1,0,1]) # true
!(P <: FactoredPolynomial) && @test (P([1,2]) ≈ P([1,2,eps()])) == ([1,2,0] ≈ [1,2,eps()])
# NaN poisons comparison
@test !(P([NaN, 1.0, 2.0]) ≈ P([NaN, 1.0, 2.0]))
# check how ==, ===, isapprox ignore variable mismatch when constants are involved, issue #217, issue #219
@test zero(P, :x) == zero(P, :y)
@test one(P, :x) == one(P, :y)
@test !(variable(P, :x) == variable(P,:y))
@test !(zero(P, :x) === zero(P, :y))
@test !(one(P, :x) === one(P, :y))
@test !(variable(P, :x) === variable(P,:y))
@test zero(P, :x) ≈ zero(P, :y)
@test one(P, :x) ≈ one(P, :y)
@test (variable(P, :x) ≈ variable(P, :x))
@test_throws ArgumentError variable(P, :x) ≈ variable(P, :y)
end
## Issue 408
p = Polynomial([1,2,3])
q = ChebyshevT([1,2,3])
@test p != q
end
@testset "Fitting" begin
@testset for P in Ps
P <: FactoredPolynomial && continue
xs = range(0, stop = π, length = 10)
ys = sin.(xs)
p = fit(P, xs, ys)
y_fit = p.(xs)
abs_error = abs.(y_fit .- ys)
@test maximum(abs_error) <= 0.03
p = fit(P, xs, ys, 2)
y_fit = p.(xs)
abs_error = abs.(y_fit .- ys)
@test maximum(abs_error) <= 0.03
# Test weighted
@testset for W in [1, ones(size(xs)), diagm(0 => ones(size(xs)))]
p = fit(P, xs, ys, 2, weights = W)
@test p.(xs) ≈ y_fit
end
# Getting error on passing Real arrays to polyfit #146
xx = Real[20.0, 30.0, 40.0]
yy = Real[15.7696, 21.4851, 28.2463]
fit(P, xx, yy, 2)
# issue #214 -- should error
@test_throws ArgumentError fit(Polynomial, rand(2,2), rand(2,2))
# issue #268 -- inexacterror
@test fit(P, 1:4, 1:4, var=:x) ≈ variable(P{Float64}, :x)
@test fit(P, 1:4, 1:4, 1, var=:x) ≈ variable(P{Float64}, :x)
# issue #467, fit specific degrees only
p = fit(P, xs, ys, 1:2:9)
@test norm(p.(xs) - ys) ≤ 1e-4
# issue 467: with constants
p = fit(P, xs, ys, 3:2:9, Dict(1 => 1))
@test norm(p.(xs) - ys) ≤ 1e-3
end
f(x) = 1/(1 + 25x^2)
N = 250; xs = [cos(j*pi/N) for j in N:-1:0];
q = fit(ArnoldiFit, xs, f.(xs));
@test maximum(abs, q(x) - f(x) for x ∈ range(-1,stop=1,length=500)) < 10eps()
q = fit(ArnoldiFit, xs, f.(xs), 100);
@test maximum(abs, q(x) - f(x) for x ∈ range(-1,stop=1,length=500)) < sqrt(eps())
# test default (issue #228)
fit(1:3, rand(3))
# weight test (PR #291)
# we specify w^2.
x = range(0, stop=pi, length=30)
y = sin.(x)
wts = 1 ./ sqrt.(1 .+ x)
# cs = numpy.polynomial.polynomial.polyfit(x, y, 4, w=wts)
cs = [0.0006441172319036863, 0.985961582190304, 0.04999233434370933, -0.23162369757680354, 0.036864056406570644]
@test maximum(abs, coeffs(fit(x, y, 4, weights=wts.^2)) - cs) <= sqrt(eps())
end
@testset "Values" begin
@testset for P in Ps
pNULL = P(Int[])
p0 = P([0])
p1 = P([1,0,0,0,0,0,0,0,0,0,0,0,0,0])
p2 = P([1,1,0,0])
p3 = P([1,2,1,0,0,0,0])
p4 = P([1,3,3,1,0,0])
p5 = P([1,4,6,4,1,0,0,0,0,0,0,0,0,0,0,0,0,0])
pN = P([276,3,87,15,24,0])
pR = P([3 // 4, -2 // 1, 1 // 1])
@test fromroots(P, Int[])(2.) == 1.
@test pN(-.125) ≈ 276.9609375
@test pNULL(10) == 0
@test p0(-10) == 0
@test fromroots(P, [1 // 2, 3 // 2])(1 // 2) == 0 // 1
# Check for Inf/NaN operations
p1 = P([Inf, Inf])
p2 = P([0, Inf])
@test p1(Inf) == Inf
if !(P <: FactoredPolynomial)
@test isnan(p1(-Inf))
@test isnan(p1(0))
@test p2(-Inf) == -Inf
end
# issue #189
p = P([0,1,2,3])
A = [0 1; 0 0];
@test p(A) ==ᵟ A + 2A^2 + 3A^3
# issue #209
ps = [P([0,1]), P([0,0,1])]
@test Polynomials.evalpoly.(1/2, ps) ≈ [p(1/2) for p in ps]
end
# constant polynomials and type
Ts = (Int, Float32, Float64, Complex{Int}, Complex{Float64})
@testset for P in (Polynomial, ImmutablePolynomial, SparsePolynomial)
@testset for T in Ts
@testset for S in Ts
c = 2
p = P{T}(c)
x = one(S)
y = p(x)
@test y === c * one(T)*one(S)
q = P{T}([c,c])
@test typeof(q(x)) == typeof(p(x))
end
end
end
@testset for P in Ps
p = P(1)
x = [1 0; 0 1]
y = p(x)
@test y ≈ x
# Issue #208 and type of output
p1=P([1//1])
p2=P([0, 0.9])
p3=p1(p2)
@test isa(p3, P)
@test eltype(p3) == eltype(p2)
end
# compensated_horner
# polynomial evaluation for polynomials with large condition numbers
@testset for P in (Polynomial, ImmutablePolynomial, SparsePolynomial)
x = variable(P{Float64})
f(x) = (x - 1)^20
p = f(x)
e₁ = abs( (f(4/3) - p(4/3))/ p(4/3) )
e₂ = abs( (f(4/3) - Polynomials.compensated_horner(coeffs(p), 4/3))/ p(4/3) )
λ = cond(p, 4/3)
u = eps()/2
@test λ > 1/u
@test e₁ <= 2 * 20 * u * λ
@test e₁ > u^(1/4)
@test e₂ <= u + u^2 * λ * 100
end
end
@testset "Conversion" begin
X = :x
@testset for P in Ps
if !(P ∈ (ImmutablePolynomial,))
p = P([0,one(Float64)])
@test P{Complex{Float64},X} == typeof(p + 1im)
@test P{Complex{Float64},X} == typeof(1im - p)
@test P{Complex{Float64},X} == typeof(p * 1im)
else
p = P([0,one(Float64)])
N=2
@test P{Complex{Float64},X,N} == typeof(p + 1im)
@test P{Complex{Float64},X,N} == typeof(1im - p)
@test P{Complex{Float64},X,N} == typeof(p * 1im)
end
end
# unnecessary copy in convert #65
p1 = Polynomial([1,2])
p2 = convert(Polynomial{Int}, p1)
p2[3] = 3
@test p1[3] == 3
# issue #287
p = LaurentPolynomial([1], -5)
@test p ≈ convert(LaurentPolynomial{Float64}, p)
# issue #358 `P(p::AbstractPolynomial)` should be `convert(P, p)` not `P(pᵢ for pᵢ ∈ p))`
x² = Polynomial([0,0,1], :x)
@testset for P ∈ (ImmutablePolynomial, SparsePolynomial, ChebyshevT)
@test P(x²) == convert(P, x²)
Q = P{Float64}
@test Q(x²) == convert(Q, x²)
end
# preserve eltype in SparsePolynomial
s = SparsePolynomial(Dict(1=>3, 2=>4))
s2 = SparsePolynomial(s)
@test s2 isa typeof(s)
@test s2 == s
s3 = SparsePolynomial{Float64}(s)
@test s3 isa SparsePolynomial{Float64,indeterminate(s)}
# conversions between pairs of polynomial types
c = [1:5;]
Psexact = (ImmutablePolynomial, Polynomial, SparsePolynomial, LaurentPolynomial)
# @testset for P1 in Ps
# p = P1(c)
# @testset for P2 in Psexact
# #@test convert(P2, p) == p
# @test convert(P2, p) ≈ p
# end
# @test convert(FactoredPolynomial, p) ≈ p
# end
@testset for P1 in Psexact
for P2 ∈ Psexact
p = P1(c)
@test convert(P2, p) == p
end
end
@testset for P2 ∈ Psexact
convert(FactoredPolynomial, P2(c)) ≈ P2(c)
end
# reinterpret coefficients
for P in (ImmutablePolynomial, Polynomial, SparsePolynomial, LaurentPolynomial, FactoredPolynomial)
for T in (Float64, Rational)
xs = [1,2,3]
p = fromroots(P,xs)
@test Polynomials.copy_with_eltype(T, p) == fromroots(P, T.(xs))
@test Polynomials.copy_with_eltype(T, Val(:u), p) == fromroots(P, T.(xs); var=:u)
P == ImmutablePolynomial && continue
@inferred Polynomials.copy_with_eltype(T, Val(:u), p)
@inferred Polynomials.copy_with_eltype(T, p)
end
end
end
@testset "Roots" begin
@testset for P in Ps
pNULL = P(Int[])
p0 = P([0])
p1 = P([1,0,0,0,0,0,0,0,0,0,0,0,0,0])
p2 = P([1,1,0,0])
p3 = P([1,2,1,0,0,0,0])
p4 = P([1,3,3,1,0,0])
p5 = P([1,4,6,4,1,0,0,0,0,0,0,0,0,0,0,0,0,0])
pN = P([276,3,87,15,24,0])
pR = P([3 // 4, -2 // 1, 1 // 1])
# From roots
r = [2, 3]
@test fromroots(r) ==ᵟ Polynomial([6, -5, 1])
p = fromroots(P, r)
@test p ==ᵟ P([6, -5, 1])
@test sort(roots(p)) ≈ r
@test roots(p0) == roots(p1) == roots(pNULL) == []
@test eltype(roots(p0)) == eltype(roots(p1)) == eltype(roots(pNULL))
!(P <: FactoredPolynomial) && @test eltype(roots(pNULL)) == Float64
@test P == LaurentPolynomial ? roots(variable(P)) == [0.0] : roots(P([0,1,0])) == [0.0]
@test roots(p2) == [-1]
if !(P <: FactoredPolynomial)
a_roots = [c for c in coeffs(copy(pN))]
@test all(map(abs, sort(roots(fromroots(a_roots))) - sort(a_roots)) .< 1e6)
end
@test length(roots(p5)) == 4
@test roots(pNULL) == []
@test sort(roots(pR)) == [1 // 2, 3 // 2]
@test sort(roots(LaurentPolynomial([24,10,-15,0,1],-2,:z)))≈[-4.0,-1.0,2.0,3.0]
A = [1 0; 0 1]
@test fromroots(A) == Polynomial(Float64[1, -2, 1])
p = fromroots(P, A)
@test p == P(Float64[1, -2, 1])
@test roots(p) ≈ sort!(eigvals(A), rev = true)
x = variable()
plarge = 8.362779449448982e41 - 2.510840694154672e57x + 4.2817430781178795e44x^2 - 1.6225927682921337e31x^3 + 1.0x^4 # #120
@test length(roots(plarge)) == 4
@test begin
a = P([1,1,1])*P([1,0.5,1])*P([1,1]) # two complex conjugate pole pairs and one real pole
r = roots(a)
b = fromroots(r)
(b ≈ a) & isreal(coeffs(b)) # the coeff should be real
end
p = Polynomial([1; zeros(99); -1])
@test fromroots(P, roots(p)) * p[end] ≈ p
end
end
@testset "multroot" begin
@testset for P in (Polynomial, ImmutablePolynomial, SparsePolynomial, LaurentPolynomial)
rts = [1.0, sqrt(2), sqrt(3)]
ls = [2, 3, 4]
x = variable(P{Float64})
p = prod((x-z)^l for (z,l) in zip(rts, ls))
out = Polynomials.Multroot.multroot(p)
@test all(out.values .≈ rts)
@test all(out.multiplicities .≈ ls)