Skip to content

Commit

Permalink
Add fallbacks with old interface
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasaschan committed Oct 5, 2015
1 parent 4a72de0 commit 21a0233
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 7 deletions.
10 changes: 10 additions & 0 deletions src/Interpolations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ count_interp_dims{T<:AbstractInterpolation}(::Type{T}, N) = N
:(gradient!(Array($Tg,$n), itp, $(xargs...)))
end

"""
`construct_instance(::Type)` constructs an instance of the provided type using
zero-argument constructors, but taking care to map tuple types to the corresponding
instances, i.e. Tuple{S,T} -> (S(), T()).
This is intended solely for internal compatibility between the old interface,
which used types, and the new, which uses instances.
"""
construct_instance{T}(::Type{T}) = T()
construct_instance{T<:Tuple}(::Type{T}) = ntuple(i -> construct_instance(T.parameters[i]), length(T.parameters))::T

include("nointerp/nointerp.jl")
include("b-splines/b-splines.jl")
include("gridded/gridded.jl")
Expand Down
32 changes: 29 additions & 3 deletions src/b-splines/b-splines.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export
abstract Degree{N}

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

bsplinetype{D<:Degree}(::Type{BSpline{D}}) = D
Expand Down Expand Up @@ -49,10 +50,22 @@ count_interp_dims{T,N,TCoefs,IT<:DimSpec{InterpolationType},GT<:DimSpec{GridType
end
end

interpolate{TWeights,TCoefs,IT<:DimSpec{BSpline},GT<:DimSpec{GridType}}(::Type{TWeights}, ::Type{TCoefs}, A, ::Type{IT}, ::Type{GT}) =
interpolate(TWeights, TCoefs, A, construct_instance(IT), construct_instance(GT))
interpolate{TWeights,TCoefs,IT<:DimSpec{BSpline},GT<:DimSpec{GridType}}(::Type{TWeights}, ::Type{TCoefs}, A, it::IT, ::Type{GT}) =
interpolate(TWeights, TCoefs, A, it, construct_instance(GT))
interpolate{TWeights,TCoefs,IT<:DimSpec{BSpline},GT<:DimSpec{GridType}}(::Type{TWeights}, ::Type{TCoefs}, A, ::Type{IT}, gt::GT) =
interpolate(TWeights, TCoefs, A, construct_instance(IT), gt)
function interpolate{TWeights,TCoefs,IT<:DimSpec{BSpline},GT<:DimSpec{GridType}}(::Type{TWeights}, ::Type{TCoefs}, A, it::IT, gt::GT)
Apad, Pad = prefilter(TWeights, TCoefs, A, IT, GT)
BSplineInterpolation(TWeights, Apad, it, gt, Pad)
end
interpolate{IT<:DimSpec{BSpline}, GT<:DimSpec{GridType}}(A::AbstractArray, ::Type{IT}, ::Type{GT}) =
interpolate(A, construct_instance(IT), construct_instance(GT))
interpolate{IT<:DimSpec{BSpline}, GT<:DimSpec{GridType}}(A::AbstractArray, it::IT, ::Type{GT}) =
interpolate(A, it, construct_instance(GT))
interpolate{IT<:DimSpec{BSpline}, GT<:DimSpec{GridType}}(A::AbstractArray, ::Type{IT}, gt::GT) =
interpolate(A, construct_instance(IT), gt)
function interpolate{IT<:DimSpec{BSpline},GT<:DimSpec{GridType}}(A::AbstractArray, it::IT, gt::GT)
interpolate(tweight(A), tcoef(A), A, it, gt)
end
Expand All @@ -68,10 +81,23 @@ tcoef(A::AbstractArray{Float32}) = Float32
tcoef(A::AbstractArray{Rational{Int}}) = Rational{Int}
tcoef{T<:Integer}(A::AbstractArray{T}) = typeof(float(zero(T)))

interpolate!{TWeights,IT<:DimSpec{BSpline},GT<:DimSpec{GridType}}(::Type{TWeights}, A, it::IT, gt::GT) = BSplineInterpolation(TWeights, prefilter!(TWeights, A, IT, GT), it, gt, Val{0}())
function interpolate!{IT<:DimSpec{BSpline},GT<:DimSpec{GridType}}(A::AbstractArray, it::IT, gt::GT)
interpolate!{TWeights,IT<:DimSpec{BSpline},GT<:DimSpec{GridType}}(::Type{TWeights}, A, ::Type{IT}, ::Type{GT}) =
interpolate!(TWeights, A, construct_instance(IT), construct_instance(GT))
interpolate!{TWeights,IT<:DimSpec{BSpline},GT<:DimSpec{GridType}}(::Type{TWeights}, A, it::IT, ::Type{GT}) =
interpolate!(TWeights, A, it, construct_instance(GT))
interpolate!{TWeights,IT<:DimSpec{BSpline},GT<:DimSpec{GridType}}(::Type{TWeights}, A, ::Type{IT}, gt::GT) =
interpolate!(TWeights, A, construct_instance(IT), gt)
interpolate!{TWeights,IT<:DimSpec{BSpline},GT<:DimSpec{GridType}}(::Type{TWeights}, A, it::IT, gt::GT) =
BSplineInterpolation(TWeights, prefilter!(TWeights, A, IT, GT), it, gt, Val{0}())

interpolate!{IT<:DimSpec{BSpline},GT<:DimSpec{GridType}}(A::AbstractArray, ::Type{IT}, ::Type{GT}) =
interpolate!(A, construct_instance(IT), construct_instance(GT))
interpolate!{IT<:DimSpec{BSpline},GT<:DimSpec{GridType}}(A::AbstractArray, it::IT, ::Type{GT}) =
interpolate!(A, it, construct_instance(GT))
interpolate!{IT<:DimSpec{BSpline},GT<:DimSpec{GridType}}(A::AbstractArray, ::Type{IT}, gt::GT) =
interpolate!(A, construct_instance(IT), gt)
interpolate!{IT<:DimSpec{BSpline},GT<:DimSpec{GridType}}(A::AbstractArray, it::IT, gt::GT) =
interpolate!(tweight(A), A, it, gt)
end

include("constant.jl")
include("linear.jl")
Expand Down
1 change: 1 addition & 0 deletions src/b-splines/quadratic.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
immutable Quadratic{BC<:BoundaryCondition} <: Degree{2} end
Quadratic{BC<:BoundaryCondition}(::Type{BC}) = Quadratic(BC())
Quadratic{BC<:BoundaryCondition}(::BC) = Quadratic{BC}()

function define_indices_d{BC}(::Type{BSpline{Quadratic{BC}}}, d, pad)
Expand Down
17 changes: 13 additions & 4 deletions src/gridded/gridded.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export Gridded

immutable Gridded{D<:Degree} <: InterpolationType end
Gridded{D<:Degree}(::Type{D}) = Gridded(D())
Gridded{D<:Degree}(::D) = Gridded{D}()

griddedtype{D<:Degree}(::Type{Gridded{D}}) = D
Expand Down Expand Up @@ -48,17 +49,25 @@ iextract{T<:GridType}(::Type{T}, d) = T
end
end

interpolate{TWeights,TCoefs,Tel,N,IT<:DimSpec{Gridded}}(::Type{TWeights}, ::Type{TCoefs}, knots::NTuple{N,GridIndex}, A::AbstractArray{Tel,N}, ::Type{IT}) =
interpolate(TWeights, TCoefs, knots, A, construct_instance(IT))
function interpolate{TWeights,TCoefs,Tel,N,IT<:DimSpec{Gridded}}(::Type{TWeights}, ::Type{TCoefs}, knots::NTuple{N,GridIndex}, A::AbstractArray{Tel,N}, it::IT)
GriddedInterpolation(TWeights, knots, A, it, Val{0}())
end
interpolate{Tel,N,IT<:DimSpec{Gridded}}(knots::NTuple{N,GridIndex}, A::AbstractArray{Tel,N}, ::Type{IT}) =
interpolate(knots, A, construct_instance(IT))
function interpolate{Tel,N,IT<:DimSpec{Gridded}}(knots::NTuple{N,GridIndex}, A::AbstractArray{Tel,N}, it::IT)
interpolate(tweight(A), tcoef(A), knots, A, it)
end

interpolate!{TWeights,Tel,N,IT<:DimSpec{Gridded}}(::Type{TWeights}, knots::NTuple{N,GridIndex}, A::AbstractArray{Tel,N}, it::IT) = GriddedInterpolation(TWeights, knots, A, it, Val{0}())
function interpolate!{Tel,N,IT<:DimSpec{Gridded}}(knots::NTuple{N,GridIndex}, A::AbstractArray{Tel,N}, it::IT)
interpolate!(tweight(A), tcoef(A), knots, A, it)
end
interpolate!{TWeights,Tel,N,IT<:DimSpec{Gridded}}(::Type{TWeights}, knots::NTuple{N,GridIndex}, A::AbstractArray{Tel,N}, it::IT) =
GriddedInterpolation(TWeights, knots, A, it, Val{0}())
interpolate!{TWeights,Tel,N,IT<:DimSpec{Gridded}}(::Type{TWeights}, knots::NTuple{N,GridIndex}, A::AbstractArray{Tel,N}, ::Type{IT}) =
interpolate!(TWeights, knots, A, construct_instance(IT))
interpolate!{Tel,N,IT<:DimSpec{Gridded}}(knots::NTuple{N,GridIndex}, A::AbstractArray{Tel,N}, ::Type{IT}) =
interpolate!(knots, A, construct_instance(IT))
interpolate!{Tel,N,IT<:DimSpec{Gridded}}(knots::NTuple{N,GridIndex}, A::AbstractArray{Tel,N}, it::IT) =
interpolate!(tweight(A), knots, A, it)

include("constant.jl")
include("linear.jl")
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ include("gridded/runtests.jl")
# test interpolation with specific types
include("typing.jl")

# test instantiating interpolation objects using types
# instead of interfaces
include("type-instantiation.jl")

# Tests copied from Grid.jl's old test suite
#include("grid.jl")

Expand Down
78 changes: 78 additions & 0 deletions test/type-instantiation.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
module TypeInstantiationTests

using Interpolations, Base.Test

# NO DIMSPECS
# tests that we forward types correctly to the instance constructors
const A1 = rand(15)
const A2 = rand(15, 10)

# B-splines
for GT in (OnCell, OnGrid)
# ...without boundary conditions
for D in (Constant, Linear)
@inferred(interpolate(A1, BSpline(D), GT))
@inferred(interpolate(A1, BSpline(D), GT()))
@inferred(interpolate(A1, BSpline(D()), GT))
@inferred(interpolate(A2, BSpline(D), GT))
@inferred(interpolate(A2, BSpline(D), GT()))
@inferred(interpolate(A2, BSpline(D()), GT))

@inferred(interpolate!(copy(A1), BSpline(D), GT))
@inferred(interpolate!(copy(A1), BSpline(D), GT()))
@inferred(interpolate!(copy(A1), BSpline(D()), GT))
@inferred(interpolate!(copy(A2), BSpline(D), GT))
@inferred(interpolate!(copy(A2), BSpline(D), GT()))
@inferred(interpolate!(copy(A2), BSpline(D()), GT))
end

# Quadratic
for BC in (Flat, Line, Periodic, Reflect, Free)
if BC != InPlace
@inferred(interpolate(A1, BSpline(Quadratic(BC)), GT))
@inferred(interpolate(A1, BSpline(Quadratic(BC())), GT))
@inferred(interpolate(A1, BSpline(Quadratic(BC)), GT()))
@inferred(interpolate(A2, BSpline(Quadratic(BC)), GT))
@inferred(interpolate(A2, BSpline(Quadratic(BC())), GT))
@inferred(interpolate(A2, BSpline(Quadratic(BC)), GT()))
end

@inferred(interpolate!(copy(A1), BSpline(Quadratic(BC)), GT))
@inferred(interpolate!(copy(A1), BSpline(Quadratic(BC())), GT))
@inferred(interpolate!(copy(A1), BSpline(Quadratic(BC)), GT()))
@inferred(interpolate!(copy(A2), BSpline(Quadratic(BC)), GT))
@inferred(interpolate!(copy(A2), BSpline(Quadratic(BC())), GT))
@inferred(interpolate!(copy(A2), BSpline(Quadratic(BC)), GT()))
end
end

# Gridded
const knots1 = (sort(rand(15)),)
const knots2 = (sort(rand(15)), sort(rand(10)))
for D in (Constant, Linear)
@inferred(interpolate(knots1, A1, Gridded(D)))
@inferred(interpolate(knots2, A2, Gridded(D)))

@inferred(interpolate!(knots1, copy(A1), Gridded(D)))
@inferred(interpolate!(knots2, copy(A2), Gridded(D)))
end

# DIMSPECS

# test that constructing dimspecs work
for T in (
Tuple{OnGrid,OnCell},
Tuple{BSpline{Linear},BSpline{Quadratic{Free}}}
)
@test isa(@inferred(Interpolations.construct_instance(T)), T)
end
# sample one dimspec for each interpolation constructor to see that it
# calls the construct_instance() function correctly
@inferred(interpolate(A2, Tuple{BSpline{Linear},BSpline{Constant}}, Tuple{OnGrid,OnCell}))
@inferred(interpolate!(copy(A2), Tuple{BSpline{Linear},BSpline{Constant}}, Tuple{OnGrid,OnCell}))
@inferred(interpolate(A2, Tuple{BSpline{Linear},BSpline{Constant}}, (OnGrid(),OnCell())))
@inferred(interpolate!(copy(A2), Tuple{BSpline{Linear},BSpline{Constant}}, (OnGrid(),OnCell())))
@inferred(interpolate(A2, (BSpline(Linear()),BSpline(Constant())), Tuple{OnGrid,OnCell}))
@inferred(interpolate!(copy(A2), (BSpline(Linear()),BSpline(Constant())), Tuple{OnGrid,OnCell}))

end

0 comments on commit 21a0233

Please sign in to comment.