Skip to content

Commit 0deea6f

Browse files
committed
empty sparse polynomial
Change firstindex and lastindex for sparse polynomials to span the entire range of indices
1 parent 6a91055 commit 0deea6f

File tree

3 files changed

+60
-49
lines changed

3 files changed

+60
-49
lines changed

src/polynomials/SparsePolynomial.jl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,6 @@ function Base.setindex!(p::SparsePolynomial, value::Number, idx::Int)
127127
return p
128128
end
129129

130-
131-
Base.firstindex(p::SparsePolynomial) = sort!(collect(keys(p.coeffs)), by=x->x[1])[1]
132-
Base.lastindex(p::SparsePolynomial) = sort!(collect(keys(p.coeffs)), by=x->x[1])[end]
133-
Base.eachindex(p::SparsePolynomial) = sort!(collect(keys(p.coeffs)), by=x->x[1])
134-
135130
# pairs iterates only over non-zero
136131
# inherits order for underlying dictionary
137132
function Base.iterate(v::PolynomialKeys{SparsePolynomial{T,X}}, state...) where {T,X}

test/ChebyshevT.jl

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
@testset "Construction" for coeff in [
2-
Int64[1, 1, 1, 1],
3-
Float32[1, -4, 2],
4-
ComplexF64[1 - 1im, 2 + 3im],
5-
[3 // 4, -2 // 1, 1 // 1]
6-
]
7-
p = ChebyshevT(coeff)
8-
@test p.coeffs == coeff
9-
@test coeffs(p) == coeff
10-
@test degree(p) == length(coeff) - 1
11-
@test Polynomials.indeterminate(p) == :x
12-
@test length(p) == length(coeff)
13-
@test size(p) == size(coeff)
14-
@test size(p, 1) == size(coeff, 1)
15-
@test typeof(p).parameters[1] == eltype(coeff)
16-
@test eltype(p) == eltype(coeff)
1+
@testset "Construction" begin
2+
@testset for coeff in Any[
3+
Int64[1, 1, 1, 1],
4+
Float32[1, -4, 2],
5+
ComplexF64[1 - 1im, 2 + 3im],
6+
[3 // 4, -2 // 1, 1 // 1]
7+
]
8+
9+
p = ChebyshevT(coeff)
10+
@test p.coeffs == coeff
11+
@test coeffs(p) == coeff
12+
@test degree(p) == length(coeff) - 1
13+
@test Polynomials.indeterminate(p) == :x
14+
@test length(p) == length(coeff)
15+
@test size(p) == size(coeff)
16+
@test size(p, 1) == size(coeff, 1)
17+
@test typeof(p).parameters[1] == eltype(coeff)
18+
@test eltype(p) == eltype(coeff)
19+
end
1720
end
1821

1922
@testset "Other Construction" begin

test/StandardBasis.jl

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,36 +28,39 @@ Ps = (ImmutablePolynomial, Polynomial, SparsePolynomial, LaurentPolynomial, Fact
2828
isimmutable(p::P) where {P} = P <: ImmutablePolynomial
2929
isimmutable(::Type{<:ImmutablePolynomial}) = true
3030

31-
@testset "Construction" for coeff in [
32-
Int64[1, 1, 1, 1],
33-
Float32[1, -4, 2],
34-
ComplexF64[1 - 1im, 2 + 3im],
35-
[3 // 4, -2 // 1, 1 // 1]
36-
]
31+
@testset "Construction" begin
32+
@testset for coeff in Any[
33+
Int64[1, 1, 1, 1],
34+
Float32[1, -4, 2],
35+
ComplexF64[1 - 1im, 2 + 3im],
36+
[3 // 4, -2 // 1, 1 // 1]
37+
]
38+
39+
@testset for P in Ps
40+
p = P(coeff)
41+
@test coeffs(p) ==ᵗ⁰ coeff
42+
@test degree(p) == length(coeff) - 1
43+
@test indeterminate(p) == :x
44+
P == Polynomial && @test length(p) == length(coeff)
45+
P == Polynomial && @test size(p) == size(coeff)
46+
P == Polynomial && @test size(p, 1) == size(coeff, 1)
47+
P == Polynomial && @test typeof(p).parameters[1] == eltype(coeff)
48+
if P != FactoredPolynomial
49+
@test eltype(p) == eltype(coeff)
50+
end
51+
@test all([-200, -0.3, 1, 48.2] .∈ Polynomials.domain(p))
3752

38-
for P in Ps
39-
p = P(coeff)
40-
@test coeffs(p) ==ᵗ⁰ coeff
41-
@test degree(p) == length(coeff) - 1
42-
@test indeterminate(p) == :x
43-
P == Polynomial && @test length(p) == length(coeff)
44-
P == Polynomial && @test size(p) == size(coeff)
45-
P == Polynomial && @test size(p, 1) == size(coeff, 1)
46-
P == Polynomial && @test typeof(p).parameters[1] == eltype(coeff)
47-
@test eltype(p) == eltype(coeff)
48-
@test all([-200, -0.3, 1, 48.2] .∈ Polynomials.domain(p))
49-
50-
## issue #316
51-
@test_throws InexactError P{Int,:x}([1+im, 1])
52-
@test_throws InexactError P{Int}([1+im, 1], :x)
53-
@test_throws InexactError P{Int,:x}(1+im)
54-
@test_throws InexactError P{Int}(1+im)
55-
56-
## issue #395
57-
v = [1,2,3]
58-
@test P(v) == P(v,:x) == P(v,'x') == P(v,"x") == P(v, Polynomials.Var(:x))
59-
end
53+
## issue #316
54+
@test_throws InexactError P{Int,:x}([1+im, 1])
55+
@test_throws InexactError P{Int}([1+im, 1], :x)
56+
@test_throws InexactError P{Int,:x}(1+im)
57+
@test_throws InexactError P{Int}(1+im)
6058

59+
## issue #395
60+
v = [1,2,3]
61+
@test P(v) == P(v,:x) == P(v,'x') == P(v,"x") == P(v, Polynomials.Var(:x))
62+
end
63+
end
6164
end
6265

6366
@testset "Mapdomain" begin
@@ -1493,3 +1496,13 @@ end
14931496
@test c == MA.operate(*, A, b)
14941497
@test 0 == @allocated MA.buffered_operate!(buffer, MA.add_mul, c, A, b)
14951498
end
1499+
1500+
@testset "empty SparsePolynomial" begin
1501+
p = SparsePolynomial(Float64[0])
1502+
@test eltype(p) == Float64
1503+
@test eltype(keys(p)) == Int
1504+
@test eltype(values(p)) == Float64
1505+
@test collect(p) == Float64[]
1506+
@test collect(keys(p)) == Int[]
1507+
@test collect(values(p)) == Float64[]
1508+
end

0 commit comments

Comments
 (0)