Skip to content

Commit b2c626b

Browse files
authored
Evaluate Fun{<:PointSpace} using a Point (#633)
* Evaluate Fun{<:PointSpace} using a Point * Test sum for DiracSpace * Simpler isapprox Fix2 representation
1 parent b290668 commit b2c626b

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

src/Spaces/DiracSpace.jl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,20 @@ Fun(::typeof(identity), S::DiracSpace) = Fun(PointSpace(S.points),S.points)
7979
transform(S::PointSpace,v::AbstractVector,plan...) = v
8080
values(f::Fun{S}) where S<:PointSpace = coefficient(f,:)
8181

82-
function evaluate(f::AbstractVector,PS::PointSpace,x::Number)
83-
p = findfirst(y->isapprox(x,y),PS.points)
82+
first(f::Fun{<:PointSpace}) = coefficients(f)[1]
83+
last(f::Fun{<:PointSpace}) = coefficients(f)[end]
84+
85+
function evaluate(f::AbstractVector, PS::PointSpace, x)
86+
p = findfirst((convert(Number,x)), PS.points)
8487
if p === nothing
8588
zero(eltype(f))
8689
else
8790
f[p]
8891
end
8992
end
9093

91-
function evaluate(f::AbstractVector, PS::DiracSpace, x::Number)
92-
x domain(PS) && return zero(eltype(f))
93-
94-
p = findfirst(y->isapprox(x,y), PS.points)
94+
function evaluate(f::AbstractVector, PS::DiracSpace, x)
95+
p = findfirst((convert(Number,x)), PS.points)
9596
if p === nothing
9697
zero(eltype(f))
9798
else
@@ -100,7 +101,7 @@ function evaluate(f::AbstractVector, PS::DiracSpace, x::Number)
100101
end
101102

102103
Base.sum(f::Fun{DS}) where DS<:DiracSpace =
103-
sum(f.coefficients[1:dimension(space(f))])
104+
sum(@view f.coefficients[1:dimension(space(f))])
104105

105106
DiracDelta(x::Number)=Fun(DiracSpace(x),[1.])
106107
DiracDelta()=DiracDelta(0.)

test/SpacesTest.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
using ApproxFunBase
2-
using Test
32
using ApproxFunBase: PointSpace, HeavisideSpace, PiecewiseSegment, dimension, SVector, checkpoints, AnyDomain
4-
using StaticArrays
53
using BandedMatrices: rowrange, colrange, BandedMatrix
4+
using DomainSets: Point
65
using LinearAlgebra
6+
using StaticArrays
7+
using Test
78

89
@testset "Spaces" begin
910
@testset "PointSpace" begin
1011
@test eltype(domain(PointSpace([0,0.1,1])) ) == Float64
1112

1213
f = @inferred Fun(x->(x-0.1),PointSpace([0,0.1,1]))
1314
@test roots(f) == [0.1]
15+
@test first(f) == -0.1
16+
@test last(f) == 0.9
17+
@test f(Point(0)) == -0.1
1418

1519
a = @inferred Fun(exp, space(f))
1620
@test f/a == @inferred Fun(x->(x-0.1)*exp(-x),space(f))
@@ -212,6 +216,8 @@ using LinearAlgebra
212216
@test f(0.5) == 0
213217
@test f(5) == 0
214218
@test isinf(f(0))
219+
@test isinf(f(Point(0)))
220+
@test sum(f) == 6
215221
end
216222

217223
@testset "Derivative operator for HeavisideSpace" begin

0 commit comments

Comments
 (0)