Skip to content

Commit

Permalink
Backport: Fix ConstantSpace Multiplication for empty coefficients (#599)
Browse files Browse the repository at this point in the history
* Backport: Fix ConstantSpace Multiplication for empty coefficients

* Bump version to v0.8.59

* Add tests
  • Loading branch information
jishnub authored Sep 12, 2023
1 parent f9bab60 commit 16cc267
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
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.8.58"
version = "0.8.59"

[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

2 comments on commit 16cc267

@jishnub
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/91325

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.8.59 -m "<description of version>" 16cc267345740fcb33aa692f9b178ce27eede228
git push origin v0.8.59

Please sign in to comment.