Skip to content

Commit ac0d522

Browse files
authored
Merge pull request #536 from JuliaMath/mk/pipe_chaining
Allow for BSpline type construction via chained pipes
2 parents 1338ee2 + 8fc0764 commit ac0d522

File tree

7 files changed

+37
-2
lines changed

7 files changed

+37
-2
lines changed

src/Interpolations.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ struct InPlaceQ{GT<:Union{GridType,Nothing}} <: BoundaryCondition gt::GT end
109109
const Natural = Line
110110

111111
(::Type{BC})() where BC<:BoundaryCondition = BC(nothing)
112+
for BC in (:Throw, :Flat, :Line, :Free, :Reflect, :InPlace, :InPlaceQ, :Periodic)
113+
eval(quote
114+
$BC(::Type{GT}) where GT <: GridType = $BC{GT}(GT())
115+
$BC{GT}(::Type{GT}) where GT <: GridType = $BC{GT}(GT())
116+
end)
117+
end
112118
function Base.show(io::IO, bc::BoundaryCondition)
113119
print(io, nameof(typeof(bc)), '(')
114120
bc.gt === nothing || show(io, bc.gt)

src/b-splines/b-splines.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export
99

1010
abstract type Degree{N} <: Flag end
1111
abstract type DegreeBC{N} <: Degree{N} end # degree type supporting a BoundaryCondition
12+
(::Type{D})(::Type{BC}) where {D <: Degree, BC <: BoundaryCondition} = D(BC())
1213

1314
"""
1415
BSpline(degree)
@@ -21,6 +22,7 @@ struct BSpline{D<:Degree} <: InterpolationType
2122
end
2223

2324
BSpline(::Type{D}) where D <: Degree = BSpline(D())
25+
BSpline() = Linear |> BSpline
2426

2527
bsplinetype(::Type{BSpline{D}}) where {D<:Degree} = D
2628
bsplinetype(::BS) where {BS<:BSpline} = bsplinetype(BS)

src/b-splines/constant.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ struct Constant{T<:ConstantInterpType,BC<:Union{Throw{OnGrid},Periodic{OnCell}}}
3737
end
3838

3939
# Default to Nearest and Throw{OnGrid}
40-
Constant(args...) = Constant{Nearest}(args...)
40+
Constant() = Constant{Nearest}()
41+
Constant(bc::BC) where {BC <: BoundaryCondition} = Constant{Nearest}(bc)
42+
Constant(::Type{T}) where T <: ConstantInterpType = Constant{T}()
4143
Constant{T}() where {T<:ConstantInterpType} = Constant{T,Throw{OnGrid}}(Throw(OnGrid()))
4244
Constant{T}(bc::BC) where {T<:ConstantInterpType,BC<:BoundaryCondition} = Constant{T,BC}(bc)
4345
Constant{T}(::Periodic{Nothing}) where {T<:ConstantInterpType} = Constant{T,Periodic{OnCell}}(Periodic(OnCell()))
44-
Constant(::Type{T}) where T <: ConstantInterpType = Constant{T}()
4546

4647
function Base.show(io::IO, deg::Constant)
4748
print(io, nameof(typeof(deg)), '{', typeof(deg).parameters[1], '}', '(')

src/b-splines/cubic.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ end
55
(deg::Cubic)(gt::GridType) = Cubic(deg.bc(gt))
66
# Default constructor to match cubic_spline_interpolation
77
Cubic() = Cubic(Line(OnGrid()))
8+
Cubic{BC}(::Type{BC}) where BC <: BoundaryCondition = Cubic{BC}(BC())
9+
Cubic{BC}(::Type{BC2}) where {BC <: BoundaryCondition, BC2 <: BoundaryCondition} = throw(ArgumentError("Argument must match type parameter"))
810

911
"""
1012
Cubic(bc::BoundaryCondition)

src/b-splines/quadratic.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ struct Quadratic{BC<:BoundaryCondition} <: DegreeBC{2}
33
end
44
# Default constructor
55
Quadratic() = Quadratic(Line(OnGrid()))
6+
Quadratic{BC}(::Type{BC}) where BC <: BoundaryCondition = Quadratic{BC}(BC())
7+
Quadratic{BC}(::Type{BC2}) where {BC <: BoundaryCondition, BC2 <: BoundaryCondition} = throw(ArgumentError("Argument must match type parameter"))
68
(deg::Quadratic)(gt::GridType) = Quadratic(deg.bc(gt))
79

810

test/pipes.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Test
2+
using Interpolations
3+
4+
@testset "Pipes" begin
5+
for BC in (Throw, Flat, Line, Free, Reflect, InPlace, InPlaceQ, Periodic)
6+
for GT in (OnGrid, OnCell)
7+
@test BC(GT) == BC(GT())
8+
@test BC{GT}(GT) == BC{GT}(GT())
9+
@test GT |> BC == BC(GT())
10+
for D in (Cubic, Quadratic)
11+
@test GT |> BC |> D == D(BC(GT()))
12+
@test GT |> BC |> D{BC{GT}} == D(BC(GT()))
13+
@test D{BC}(BC) == D{BC}(BC())
14+
end
15+
end
16+
@test Linear(Throw{OnGrid}) == Linear(Throw{OnGrid}())
17+
end
18+
@test BSpline() == BSpline(Linear())
19+
@test_throws ArgumentError Cubic{Throw}(Flat)
20+
@test_throws ArgumentError Quadratic{Throw}(Flat)
21+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const isci = get(ENV, "CI", "") in ("true", "True")
5050
include("convenience-constructors.jl")
5151
include("readme-examples.jl")
5252
include("iterate.jl")
53+
include("pipes.jl")
5354

5455
# Chain rules interaction
5556
include("chainrules.jl")

0 commit comments

Comments
 (0)