Skip to content

Fix ConstantSpace Multiplication for empty coefficients #598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ApproxFunBase"
uuid = "fbd15aa5-315a-5a7d-a8a4-24992e37be05"
version = "0.9.15"
version = "0.9.16"

[deps]
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"
Expand Down
18 changes: 14 additions & 4 deletions src/Spaces/ConstantSpace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ iterate(f::Fun{SequenceSpace}, st) = f[st], st+1

getindex(f::Fun{SequenceSpace}, k::Integer) =
k ≤ ncoefficients(f) ? f.coefficients[k] : zero(cfstype(f))
getindex(f::Fun{SequenceSpace},K::CartesianIndex{0}) = f[1]
getindex(f::Fun{SequenceSpace},K::CartesianIndex{0}) = _first_or_zero(f)
getindex(f::Fun{SequenceSpace},K) = cfstype(f)[f[k] for k in K]

length(f::Fun{SequenceSpace}) = ℵ₀
Expand Down Expand Up @@ -80,7 +80,10 @@ ones(S::ConstantSpace) = Fun(S,fill(1.0,1))
ones(S::Union{AnyDomain,UnsetSpace}) = ones(ConstantSpace())
zeros(S::AnyDomain) = zero(ConstantSpace())
zero(S::UnsetSpace) = zero(ConstantSpace())
evaluate(f::AbstractVector,::ConstantSpace,x...)=f[1]
_first_or_zero(f::AbstractVector) = get(f, 1, zero(eltype(f)))
function evaluate(f::AbstractVector,::ConstantSpace,x...)
_first_or_zero(f)
end
evaluate(f::AbstractVector,::ZeroSpace,x...)=zero(eltype(f))


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

bandwidths(D::ConcreteMultiplication{CS1,CS2,T}) where {CS1<:ConstantSpace,CS2<:ConstantSpace,T} =
0,0
getindex(D::ConcreteMultiplication{CS1,CS2,T},k::Integer,j::Integer) where {CS1<:ConstantSpace,CS2<:ConstantSpace,T} =
k==j==1 ? strictconvert(T,D.f.coefficients[1]) : one(T)

function getindex(D::ConcreteMultiplication{<:ConstantSpace,<:ConstantSpace,T},k::Integer,j::Integer) where {T}
if k==j==1
c = _first_or_zero(coefficients(D.f))
strictconvert(T, c)
else
one(T)
end
end

rangespace(D::ConcreteMultiplication{CS1,CS2,T}) where {CS1<:ConstantSpace,CS2<:ConstantSpace,T} =
D.space
Expand Down
2 changes: 1 addition & 1 deletion src/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ function show(io::IO,s::SubSpace)
show(io,s.indexes)
end

show(io::IO,::ConstantSpace{AnyDomain}) = print(io,"ConstantSpace")
show(io::IO,::ConstantSpace{AnyDomain}) = print(io,"ConstantSpace()")
show(io::IO,S::ConstantSpace) = print(io,"ConstantSpace($(domain(S)))")

## Segment
Expand Down
9 changes: 9 additions & 0 deletions test/SpacesTest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,15 @@ using LinearAlgebra
@test maxspace(ConstantSpace(Point(1)), ConstantSpace(AnyDomain())) == ConstantSpace(Point(1))
@test maxspace(ConstantSpace(AnyDomain()), ConstantSpace(Point(2))) == ConstantSpace(Point(2))
@test maxspace(ConstantSpace(AnyDomain()), ConstantSpace(AnyDomain())) == ConstantSpace(AnyDomain())

f = Fun(ConstantSpace(), Float64[])
g = Fun(ConstantSpace(), Float64[0])
@test f(0) == g(0) == 0

C = Multiplication(f, space(f))
@test all(iszero, AbstractMatrix(C))
C = Multiplication(g, space(g))
@test all(iszero, AbstractMatrix(C))
end

@testset "promotion" begin
Expand Down
2 changes: 1 addition & 1 deletion test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
end
@testset "Space" begin
@testset "ConstantSpace" begin
@test contains(repr(ConstantSpace()), "ConstantSpace")
@test repr(ConstantSpace()) == "ConstantSpace()"
c = ConstantSpace(0..1)
@test startswith(repr(c), "ConstantSpace")
@test contains(repr(c), repr(domain(c)))
Expand Down