Skip to content

Commit 0063063

Browse files
committed
Add fallbacks with old interface
1 parent 95235f2 commit 0063063

File tree

7 files changed

+101
-20
lines changed

7 files changed

+101
-20
lines changed

src/Interpolations.jl

+11-10
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ using WoodburyMatrices, Ratios, AxisAlgorithms
3232

3333
import Base: convert, size, getindex, gradient, scale, promote_rule
3434

35-
abstract InterpolationType
35+
abstract Flag
36+
abstract InterpolationType <: Flag
3637
immutable NoInterp <: InterpolationType end
37-
abstract GridType
38+
abstract GridType <: Flag
3839
immutable OnGrid <: GridType end
3940
immutable OnCell <: GridType end
4041

@@ -44,16 +45,16 @@ abstract AbstractInterpolation{T,N,IT<:DimSpec{InterpolationType},GT<:DimSpec{Gr
4445
abstract AbstractInterpolationWrapper{T,N,ITPT,IT,GT} <: AbstractInterpolation{T,N,IT,GT}
4546
abstract AbstractExtrapolation{T,N,ITPT,IT,GT} <: AbstractInterpolationWrapper{T,N,ITPT,IT,GT}
4647

47-
abstract BoundaryCondition
48-
immutable Flat <: BoundaryCondition end
49-
immutable Line <: BoundaryCondition end
50-
immutable Free <: BoundaryCondition end
51-
immutable Periodic <: BoundaryCondition end
52-
immutable Reflect <: BoundaryCondition end
53-
immutable InPlace <: BoundaryCondition end
48+
immutable Throw <: Flag end
49+
immutable Flat <: Flag end
50+
immutable Line <: Flag end
51+
immutable Free <: Flag end
52+
immutable Periodic <: Flag end
53+
immutable Reflect <: Flag end
54+
immutable InPlace <: Flag end
5455
# InPlaceQ is exact for an underlying quadratic. This is nice for ground-truth testing
5556
# of in-place (unpadded) interpolation.
56-
immutable InPlaceQ <: BoundaryCondition end
57+
immutable InPlaceQ <: Flag end
5758
typealias Natural Line
5859

5960
# TODO: size might have to be faster?

src/b-splines/b-splines.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export
66
Linear,
77
Quadratic
88

9-
abstract Degree{N}
9+
abstract Degree{N} <: Flag
1010

1111
immutable BSpline{D<:Degree} <: InterpolationType end
1212
BSpline{D<:Degree}(::D) = BSpline{D}()

src/b-splines/quadratic.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
immutable Quadratic{BC<:BoundaryCondition} <: Degree{2} end
2-
Quadratic{BC<:BoundaryCondition}(::BC) = Quadratic{BC}()
1+
immutable Quadratic{BC<:Flag} <: Degree{2} end
2+
Quadratic{BC<:Flag}(::BC) = Quadratic{BC}()
33

44
function define_indices_d{BC}(::Type{BSpline{Quadratic{BC}}}, d, pad)
55
symix, symixm, symixp = symbol("ix_",d), symbol("ixm_",d), symbol("ixp_",d)
@@ -73,7 +73,7 @@ function index_gen{Q<:Quadratic,IT<:DimSpec{BSpline}}(::Type{BSpline{Q}}, ::Type
7373
end
7474
end
7575

76-
padding{BC<:BoundaryCondition}(::Type{BSpline{Quadratic{BC}}}) = Val{1}()
76+
padding{BC<:Flag}(::Type{BSpline{Quadratic{BC}}}) = Val{1}()
7777
padding(::Type{BSpline{Quadratic{Periodic}}}) = Val{0}()
7878

7979
function inner_system_diags{T,Q<:Quadratic}(::Type{T}, n::Int, ::Type{Q})

src/extrapolation/extrapolation.jl

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
export Throw
2-
31
type Extrapolation{T,N,ITPT,IT,GT,ET} <: AbstractInterpolationWrapper{T,N,ITPT,IT,GT}
42
itp::ITPT
53
end
64
Extrapolation{T,ITPT,IT,GT,ET}(::Type{T}, N, itp::ITPT, ::Type{IT}, ::Type{GT}, et::ET) =
75
Extrapolation{T,N,ITPT,IT,GT,ET}(itp)
86

7+
# DimSpec{Flag} is not enough for extrapolation dispatch, since we allow nested tuples
8+
# However, no tuples should be nested deeper than this; the first level is for different
9+
# schemes in different dimensions, and the second level is for different schemes in
10+
# different directions.
11+
typealias ExtrapDimSpec Union{Flag,Tuple{Vararg{Union{Flag,NTuple{2,Flag}}}}}
12+
913
"""
1014
`extrapolate(itp, scheme)` adds extrapolation behavior to an interpolation object, according to the provided scheme.
1115
@@ -21,7 +25,7 @@ You can also combine schemes in tuples. For example, the scheme `(Linear(), Flat
2125
2226
Finally, you can specify different extrapolation behavior in different direction. `((Linear(),Flat()), Flat())` will extrapolate linearly in the first dimension if the index is too small, but use constant etrapolation if it is too large, and always use constant extrapolation in the second dimension.
2327
"""
24-
extrapolate{T,N,IT,GT}(itp::AbstractInterpolation{T,N,IT,GT}, et) =
28+
extrapolate{T,N,IT,GT,ET<:ExtrapDimSpec}(itp::AbstractInterpolation{T,N,IT,GT}, et::ET) =
2529
Extrapolation(T,N,itp,IT,GT,et)
2630

2731
include("throw.jl")

src/extrapolation/throw.jl

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
immutable Throw end
2-
31
function extrap_prep{N,d}(::Type{Throw}, ::Val{N}, ::Val{d})
42
xsym = symbol("xs_", d)
53
:(lbound(etp, $d) <= $xsym <= ubound(etp, $d) || throw(BoundsError()))

test/extrapolation/runtests.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ etpg = extrapolate(itpg, Flat())
1414
@test etpg[-3] == etpg[-4.5] == etpg[0.9] == etpg[1.0] == A[1]
1515
@test etpg[10.1] == etpg[11] == etpg[148.298452] == A[end]
1616

17-
etpf = @inferred(extrapolate(itpg, FillValue(NaN)))
17+
etpf = @inferred(extrapolate(itpg, NaN))
1818

1919
@test @inferred(size(etpf)) == (xmax,)
2020
@test isnan(@inferred(getindex(etpf, -2.5)))

test/type-instantiation.jl

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
module TypeInstantiationTests
2+
3+
using Interpolations, Base.Test
4+
5+
# NO DIMSPECS
6+
# tests that we forward types correctly to the instance constructors
7+
const A1 = rand(15)
8+
const A2 = rand(15, 10)
9+
10+
# B-splines
11+
for GT in (OnCell, OnGrid)
12+
# ...without boundary conditions
13+
for D in (Constant, Linear)
14+
@inferred(interpolate(A1, BSpline(D), GT))
15+
@inferred(interpolate(A1, BSpline(D), GT()))
16+
@inferred(interpolate(A1, BSpline(D()), GT))
17+
@inferred(interpolate(A2, BSpline(D), GT))
18+
@inferred(interpolate(A2, BSpline(D), GT()))
19+
@inferred(interpolate(A2, BSpline(D()), GT))
20+
21+
@inferred(interpolate!(copy(A1), BSpline(D), GT))
22+
@inferred(interpolate!(copy(A1), BSpline(D), GT()))
23+
@inferred(interpolate!(copy(A1), BSpline(D()), GT))
24+
@inferred(interpolate!(copy(A2), BSpline(D), GT))
25+
@inferred(interpolate!(copy(A2), BSpline(D), GT()))
26+
@inferred(interpolate!(copy(A2), BSpline(D()), GT))
27+
end
28+
29+
# Quadratic
30+
for BC in (Flat, Line, Periodic, Reflect, Free)
31+
if BC != InPlace
32+
@inferred(interpolate(A1, BSpline(Quadratic(BC)), GT))
33+
@inferred(interpolate(A1, BSpline(Quadratic(BC())), GT))
34+
@inferred(interpolate(A1, BSpline(Quadratic(BC)), GT()))
35+
@inferred(interpolate(A2, BSpline(Quadratic(BC)), GT))
36+
@inferred(interpolate(A2, BSpline(Quadratic(BC())), GT))
37+
@inferred(interpolate(A2, BSpline(Quadratic(BC)), GT()))
38+
end
39+
40+
@inferred(interpolate!(copy(A1), BSpline(Quadratic(BC)), GT))
41+
@inferred(interpolate!(copy(A1), BSpline(Quadratic(BC())), GT))
42+
@inferred(interpolate!(copy(A1), BSpline(Quadratic(BC)), GT()))
43+
@inferred(interpolate!(copy(A2), BSpline(Quadratic(BC)), GT))
44+
@inferred(interpolate!(copy(A2), BSpline(Quadratic(BC())), GT))
45+
@inferred(interpolate!(copy(A2), BSpline(Quadratic(BC)), GT()))
46+
end
47+
end
48+
49+
# Gridded
50+
const knots1 = (sort(rand(15)),)
51+
const knots2 = (sort(rand(15)), sort(rand(10)))
52+
for D in (Constant, Linear)
53+
@inferred(interpolate(knots1, A1, Gridded(D)))
54+
@inferred(interpolate(knots2, A2, Gridded(D)))
55+
56+
@inferred(interpolate!(knots1, copy(A1), Gridded(D)))
57+
@inferred(interpolate!(knots2, copy(A2), Gridded(D)))
58+
end
59+
60+
# DIMSPECS
61+
62+
# test that constructing dimspecs work
63+
for T in (
64+
Tuple{OnGrid,OnCell},
65+
Tuple{BSpline{Linear},BSpline{Quadratic{Free}}}
66+
)
67+
@test isa(@inferred(Interpolations.construct_instance(T)), T)
68+
end
69+
# sample one dimspec for each interpolation constructor to see that it
70+
# calls the construct_instance() function correctly
71+
@inferred(interpolate(A2, Tuple{BSpline{Linear},BSpline{Constant}}, Tuple{OnGrid,OnCell}))
72+
@inferred(interpolate!(copy(A2), Tuple{BSpline{Linear},BSpline{Constant}}, Tuple{OnGrid,OnCell}))
73+
@inferred(interpolate(A2, Tuple{BSpline{Linear},BSpline{Constant}}, (OnGrid(),OnCell())))
74+
@inferred(interpolate!(copy(A2), Tuple{BSpline{Linear},BSpline{Constant}}, (OnGrid(),OnCell())))
75+
@inferred(interpolate(A2, (BSpline(Linear()),BSpline(Constant())), Tuple{OnGrid,OnCell}))
76+
@inferred(interpolate!(copy(A2), (BSpline(Linear()),BSpline(Constant())), Tuple{OnGrid,OnCell}))
77+
78+
end

0 commit comments

Comments
 (0)