Skip to content

Commit 3270d7d

Browse files
authored
Fix ConstantSpace Multiplication for empty coefficients (#598)
* Fix ConstantSpace Multiplication for empty coefficients * Bump version to v0.9.16 * fix evaluate * Add test for zero coefficient
1 parent 28eb4ea commit 3270d7d

File tree

5 files changed

+26
-7
lines changed

5 files changed

+26
-7
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ApproxFunBase"
22
uuid = "fbd15aa5-315a-5a7d-a8a4-24992e37be05"
3-
version = "0.9.15"
3+
version = "0.9.16"
44

55
[deps]
66
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"

src/Spaces/ConstantSpace.jl

+14-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ iterate(f::Fun{SequenceSpace}, st) = f[st], st+1
66

77
getindex(f::Fun{SequenceSpace}, k::Integer) =
88
k ncoefficients(f) ? f.coefficients[k] : zero(cfstype(f))
9-
getindex(f::Fun{SequenceSpace},K::CartesianIndex{0}) = f[1]
9+
getindex(f::Fun{SequenceSpace},K::CartesianIndex{0}) = _first_or_zero(f)
1010
getindex(f::Fun{SequenceSpace},K) = cfstype(f)[f[k] for k in K]
1111

1212
length(f::Fun{SequenceSpace}) = ℵ₀
@@ -80,7 +80,10 @@ ones(S::ConstantSpace) = Fun(S,fill(1.0,1))
8080
ones(S::Union{AnyDomain,UnsetSpace}) = ones(ConstantSpace())
8181
zeros(S::AnyDomain) = zero(ConstantSpace())
8282
zero(S::UnsetSpace) = zero(ConstantSpace())
83-
evaluate(f::AbstractVector,::ConstantSpace,x...)=f[1]
83+
_first_or_zero(f::AbstractVector) = get(f, 1, zero(eltype(f)))
84+
function evaluate(f::AbstractVector,::ConstantSpace,x...)
85+
_first_or_zero(f)
86+
end
8487
evaluate(f::AbstractVector,::ZeroSpace,x...)=zero(eltype(f))
8588

8689

@@ -173,8 +176,15 @@ defaultMultiplication(f::Fun,b::ConstantSpace) = ConcreteMultiplication(f,b)
173176

174177
bandwidths(D::ConcreteMultiplication{CS1,CS2,T}) where {CS1<:ConstantSpace,CS2<:ConstantSpace,T} =
175178
0,0
176-
getindex(D::ConcreteMultiplication{CS1,CS2,T},k::Integer,j::Integer) where {CS1<:ConstantSpace,CS2<:ConstantSpace,T} =
177-
k==j==1 ? strictconvert(T,D.f.coefficients[1]) : one(T)
179+
180+
function getindex(D::ConcreteMultiplication{<:ConstantSpace,<:ConstantSpace,T},k::Integer,j::Integer) where {T}
181+
if k==j==1
182+
c = _first_or_zero(coefficients(D.f))
183+
strictconvert(T, c)
184+
else
185+
one(T)
186+
end
187+
end
178188

179189
rangespace(D::ConcreteMultiplication{CS1,CS2,T}) where {CS1<:ConstantSpace,CS2<:ConstantSpace,T} =
180190
D.space

src/show.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ function show(io::IO,s::SubSpace)
168168
show(io,s.indexes)
169169
end
170170

171-
show(io::IO,::ConstantSpace{AnyDomain}) = print(io,"ConstantSpace")
171+
show(io::IO,::ConstantSpace{AnyDomain}) = print(io,"ConstantSpace()")
172172
show(io::IO,S::ConstantSpace) = print(io,"ConstantSpace($(domain(S)))")
173173

174174
## Segment

test/SpacesTest.jl

+9
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,15 @@ using LinearAlgebra
315315
@test maxspace(ConstantSpace(Point(1)), ConstantSpace(AnyDomain())) == ConstantSpace(Point(1))
316316
@test maxspace(ConstantSpace(AnyDomain()), ConstantSpace(Point(2))) == ConstantSpace(Point(2))
317317
@test maxspace(ConstantSpace(AnyDomain()), ConstantSpace(AnyDomain())) == ConstantSpace(AnyDomain())
318+
319+
f = Fun(ConstantSpace(), Float64[])
320+
g = Fun(ConstantSpace(), Float64[0])
321+
@test f(0) == g(0) == 0
322+
323+
C = Multiplication(f, space(f))
324+
@test all(iszero, AbstractMatrix(C))
325+
C = Multiplication(g, space(g))
326+
@test all(iszero, AbstractMatrix(C))
318327
end
319328

320329
@testset "promotion" begin

test/show.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
end
1010
@testset "Space" begin
1111
@testset "ConstantSpace" begin
12-
@test contains(repr(ConstantSpace()), "ConstantSpace")
12+
@test repr(ConstantSpace()) == "ConstantSpace()"
1313
c = ConstantSpace(0..1)
1414
@test startswith(repr(c), "ConstantSpace")
1515
@test contains(repr(c), repr(domain(c)))

0 commit comments

Comments
 (0)