Skip to content

Commit b290668

Browse files
authored
First/last for ConstantSpace Fun (#631)
* First/last for ConstantSpace Fun * Fix test
1 parent 5b6c8d0 commit b290668

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/Spaces/ConstantSpace.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ ones(S::Union{AnyDomain,UnsetSpace}) = ones(ConstantSpace())
8181
zeros(S::AnyDomain) = zero(ConstantSpace())
8282
zero(S::UnsetSpace) = zero(ConstantSpace())
8383
_first_or_zero(f::AbstractVector) = get(f, 1, zero(eltype(f)))
84-
function evaluate(f::AbstractVector,::ConstantSpace,x...)
84+
function evaluate(f::AbstractVector, sp::ConstantSpace, x)
85+
x in domain(sp) || return zero(eltype(f))
8586
_first_or_zero(f)
8687
end
8788
evaluate(f::AbstractVector,::ZeroSpace,x...)=zero(eltype(f))
@@ -155,6 +156,9 @@ coefficients(f::AbstractVector, sp::ConstantSpace{<:Domain{<:Number}}, ts::Space
155156
coefficients(f::AbstractVector, sp::ConstantSpace, ts::Space) =
156157
f[1]*ones(ts).coefficients
157158

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

159163
########
160164
# Evaluation

test/SpacesTest.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,11 @@ using LinearAlgebra
317317
@test g >= f
318318
@test 1 < f < 3
319319
@test differentiate(f) == Fun(0, ConstantSpace(0..1))
320+
@test f(-1) == 0
321+
@test first(f) == last(f) == 2
322+
323+
f = Fun(2, ConstantSpace())
324+
@test first(f) == last(f) == 2
320325

321326
@test maxspace(ConstantSpace(Point(1)), ConstantSpace(Point(2))) == ConstantSpace(Point(1) Point(2))
322327
@test maxspace(ConstantSpace(Point(1)), ConstantSpace(AnyDomain())) == ConstantSpace(Point(1))

0 commit comments

Comments
 (0)