Skip to content

First/last for ConstantSpace Fun #631

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 2 commits into from
Feb 3, 2024
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
6 changes: 5 additions & 1 deletion src/Spaces/ConstantSpace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ ones(S::Union{AnyDomain,UnsetSpace}) = ones(ConstantSpace())
zeros(S::AnyDomain) = zero(ConstantSpace())
zero(S::UnsetSpace) = zero(ConstantSpace())
_first_or_zero(f::AbstractVector) = get(f, 1, zero(eltype(f)))
function evaluate(f::AbstractVector,::ConstantSpace,x...)
function evaluate(f::AbstractVector, sp::ConstantSpace, x)
x in domain(sp) || return zero(eltype(f))
_first_or_zero(f)
end
evaluate(f::AbstractVector,::ZeroSpace,x...)=zero(eltype(f))
Expand Down Expand Up @@ -155,6 +156,9 @@ coefficients(f::AbstractVector, sp::ConstantSpace{<:Domain{<:Number}}, ts::Space
coefficients(f::AbstractVector, sp::ConstantSpace, ts::Space) =
f[1]*ones(ts).coefficients

# a Fun{<:ConstantSpace} corresponds to a constant value within the domain, irrespective of the domain
first(f::Fun{<:ConstantSpace}) = convert(Number, f)
last(f::Fun{<:ConstantSpace}) = convert(Number, f)

########
# Evaluation
Expand Down
5 changes: 5 additions & 0 deletions test/SpacesTest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@ using LinearAlgebra
@test g >= f
@test 1 < f < 3
@test differentiate(f) == Fun(0, ConstantSpace(0..1))
@test f(-1) == 0
@test first(f) == last(f) == 2

f = Fun(2, ConstantSpace())
@test first(f) == last(f) == 2

@test maxspace(ConstantSpace(Point(1)), ConstantSpace(Point(2))) == ConstantSpace(Point(1) ∪ Point(2))
@test maxspace(ConstantSpace(Point(1)), ConstantSpace(AnyDomain())) == ConstantSpace(Point(1))
Expand Down