Skip to content

Commit 49a3936

Browse files
authored
boundary enum in Evaluation (#452)
* boundary enum in Evaluation * bump version to v0.8.15 * fix dispatch
1 parent 37f8d24 commit 49a3936

File tree

3 files changed

+47
-31
lines changed

3 files changed

+47
-31
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ApproxFunBase"
22
uuid = "fbd15aa5-315a-5a7d-a8a4-24992e37be05"
3-
version = "0.8.14"
3+
version = "0.8.15"
44

55
[deps]
66
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"

src/Operators/functionals/Evaluation.jl

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ export Evaluation,ivp,bvp,Dirichlet,Neumann
55
abstract type Evaluation{T}<:Operator{T} end
66

77
@functional Evaluation
8+
evaluation_point(C::Evaluation) = C.x
9+
10+
@enum Boundary RightEndPoint=1 LeftEndPoint=-1
11+
12+
isleftendpoint(::typeof(leftendpoint)) = true
13+
isrightendpoint(::typeof(rightendpoint)) = true
14+
isrightendpoint(x::Boundary) = x == RightEndPoint
15+
isleftendpoint(x::Boundary) = x == LeftEndPoint
816

917
# M = leftendpoint/rightendpoint if endpoint
1018
struct ConcreteEvaluation{S,M,OT,T} <: Evaluation{T}
@@ -33,17 +41,27 @@ end
3341

3442
Evaluation(sp::Space,x,order) = Evaluation(rangetype(sp),sp,x,order)
3543

36-
Evaluation(d::Space,x::Union{Number,typeof(leftendpoint),typeof(rightendpoint)}) = Evaluation(d,x,0)
37-
Evaluation(::Type{T},d::Space,n...) where {T} = error("Override Evaluation for $(typeof(d))")
38-
Evaluation(::Type{T},d,n...) where {T} = Evaluation(T,Space(d),n...)
39-
Evaluation(S::Space,n...) = error("Override Evaluation for $(typeof(S))")
40-
Evaluation(d,n...) = Evaluation(Space(d),n...)
41-
Evaluation(x::Union{Number,typeof(leftendpoint),typeof(rightendpoint)}) = Evaluation(UnsetSpace(),x,0)
42-
Evaluation(x::Union{Number,typeof(leftendpoint),typeof(rightendpoint)},k::Integer) =
43-
Evaluation(UnsetSpace(),x,k)
44-
45-
rangespace(E::ConcreteEvaluation{<:AmbiguousSpace}) = ConstantSpace()
46-
rangespace(E::ConcreteEvaluation) = ConstantSpace(Point(E.x))
44+
const SpecialEvalPtType = Union{typeof(leftendpoint),typeof(rightendpoint),Boundary}
45+
const EvalPtType = Union{Number,SpecialEvalPtType}
46+
47+
error_space(d::Space) = error("Override Evaluation for $(typeof(d))")
48+
error_space(d) = nothing
49+
50+
Evaluation(d::Space,x::EvalPtType) = Evaluation(d,x,0)
51+
Evaluation(::Type{T},d,n...) where {T} = (error_space(d); Evaluation(T,Space(d),n...))
52+
Evaluation(d,n...) = (error_space(d); Evaluation(Space(d),n...))
53+
Evaluation(x::EvalPtType,k::Integer=0) = Evaluation(UnsetSpace(),x,k)
54+
55+
_rangespace_eval(E::ConcreteEvaluation, ::AmbiguousSpace, ::SpecialEvalPtType) = UnsetSpace()
56+
_rangespace_eval(E::ConcreteEvaluation, ::AmbiguousSpace, ::Any) = ConstantSpace()
57+
_rangespace_eval(E::ConcreteEvaluation, ::Space, ::Any) = ConstantSpace(Point(E.x))
58+
function _rangespace_eval(E::ConcreteEvaluation, ::Space, ::SpecialEvalPtType)
59+
d = domain(domainspace(E))
60+
isambiguous(d) && return ConstantSpace()
61+
dop = boundaryfn(E.x)
62+
return ConstantSpace(Point(dop(d)))
63+
end
64+
rangespace(E::ConcreteEvaluation) = _rangespace_eval(E, E.space, evaluation_point(E))
4765

4866

4967
function convert(::Type{Operator{T}},E::ConcreteEvaluation) where T
@@ -57,31 +75,22 @@ end
5775

5876

5977
## default getindex
78+
_eval(f, x) = f(x)
79+
_eval(f, x::SpecialEvalPtType) = boundaryevalfn(x)(f)
6080
function getindex(D::ConcreteEvaluation,k::Integer)
6181
T = prectype(domainspace(D))
6282
f = Fun(D.space, [zeros(T,k-1); one(T)])
6383
df = differentiate(f,D.order)
64-
v = df(D.x)
84+
v = _eval(df, D.x)
6585
strictconvert(eltype(D), v)
6686
end
6787

68-
#special leftendpoint/rightendpoint overrides
69-
for (dop, fop) in ((:leftendpoint,:first), (:rightendpoint,:last))
70-
@eval begin
71-
rangespace(E::ConcreteEvaluation{<:AmbiguousSpace,typeof($dop)}) = UnsetSpace()
72-
function rangespace(E::ConcreteEvaluation{<:Any,typeof($dop)})
73-
d = domain(domainspace(E))
74-
isambiguous(d) && return ConstantSpace()
75-
return ConstantSpace(Point($dop(d)))
76-
end
77-
function getindex(D::ConcreteEvaluation{<:Any,typeof($dop)},k::Integer)
78-
P = prectype(domainspace(D))
79-
R = eltype(D)
80-
R($fop(differentiate(Fun(D.space,[zeros(P,k-1);one(P)]),D.order)))
81-
end
82-
end
83-
end
84-
88+
boundaryfn(x::typeof(rightendpoint)) = x
89+
boundaryfn(x::typeof(leftendpoint)) = x
90+
boundaryfn(x::Boundary) = isleftendpoint(x) ? leftendpoint : rightendpoint
91+
boundaryevalfn(::typeof(rightendpoint)) = last
92+
boundaryevalfn(::typeof(leftendpoint)) = first
93+
boundaryevalfn(x::Boundary) = isleftendpoint(x) ? first : last
8594

8695

8796

@@ -132,7 +141,7 @@ ivp(d,k) = [ldiffbc(d,i) for i=0:k-1]
132141
bvp(d,k) = vcat([ldiffbc(d,i) for i=0:div(k,2)-1],
133142
[rdiffbc(d,i) for i=0:div(k,2)-1])
134143

135-
periodic(d,k) = [Evaluation(d,leftendpoint,i)-Evaluation(d,rightendpoint,i) for i=0:k]
144+
periodic(d,k) = [ldiffbc(d,i) - rdiffbc(d,i) for i=0:k]
136145

137146
# shorthand for second order
138147
ivp(d) = ivp(d,2)

test/runtests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,5 +667,12 @@ end
667667
@test collect(it) == collect(b)
668668
end
669669

670+
@testset "Evaluation left/rightendpoint" begin
671+
@test ApproxFunBase.isleftendpoint(ApproxFunBase.LeftEndPoint)
672+
@test ApproxFunBase.isrightendpoint(ApproxFunBase.RightEndPoint)
673+
@test ApproxFunBase.isleftendpoint(leftendpoint)
674+
@test ApproxFunBase.isrightendpoint(rightendpoint)
675+
end
676+
670677
@time include("ETDRK4Test.jl")
671678
include("show.jl")

0 commit comments

Comments
 (0)