Skip to content

Commit 042ee2e

Browse files
sglyonTomas Lycken
authored andcommitted
Type stable scale function. Closes #111 (#112)
* Type stable scale function. Closes #111 Also adds methods for eltype and ndims to AbstractInterpolation * Add `@inferred` to scaling tests
1 parent c57344f commit 042ee2e

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

src/Interpolations.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export
3434

3535
using WoodburyMatrices, Ratios, AxisAlgorithms
3636

37-
import Base: convert, size, getindex, gradient, scale, promote_rule
37+
import Base: convert, size, getindex, gradient, scale, promote_rule, ndims, eltype
3838

3939
abstract Flag
4040
abstract InterpolationType <: Flag
@@ -76,6 +76,12 @@ itptype(itp::AbstractInterpolation ) = itptype(typeof(itp))
7676
gridtype{T,N,IT<:DimSpec{InterpolationType},GT<:DimSpec{GridType}}(::Type{AbstractInterpolation{T,N,IT,GT}}) = GT
7777
gridtype{ITP<:AbstractInterpolation}(::Type{ITP}) = gridtype(super(ITP))
7878
gridtype(itp::AbstractInterpolation) = gridtype(typeof(itp))
79+
ndims{T,N,IT<:DimSpec{InterpolationType},GT<:DimSpec{GridType}}(::Type{AbstractInterpolation{T,N,IT,GT}}) = N
80+
ndims{ITP<:AbstractInterpolation}(::Type{ITP}) = ndims(super(ITP))
81+
ndims(itp::AbstractInterpolation) = ndims(typeof(itp))
82+
eltype{T,N,IT<:DimSpec{InterpolationType},GT<:DimSpec{GridType}}(::Type{AbstractInterpolation{T,N,IT,GT}}) = T
83+
eltype{ITP<:AbstractInterpolation}(::Type{ITP}) = eltype(super(ITP))
84+
eltype(itp::AbstractInterpolation) = eltype(typeof(itp))
7985
count_interp_dims{T<:AbstractInterpolation}(::Type{T}, N) = N
8086

8187
include("nointerp/nointerp.jl")

src/scaling/scaling.jl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ immutable ScaledInterpolation{T,N,ITPT,IT,GT,RT} <: AbstractInterpolationWrapper
66
itp::ITPT
77
ranges::RT
88
end
9-
ScaledInterpolation{T,ITPT,IT,GT,RT}(::Type{T}, N, itp::ITPT, ::Type{IT}, ::Type{GT}, ranges::RT) =
10-
ScaledInterpolation{T,N,ITPT,IT,GT,RT}(itp, ranges)
9+
@generated function ScaledInterpolation{ITPT,RT}(itp::ITPT, ranges::RT)
10+
T = eltype(itp)
11+
N = ndims(itp)
12+
IT = itptype(itp)
13+
GT = gridtype(itp)
14+
:(ScaledInterpolation{$T,$N,$ITPT,$IT,$GT,$RT}(itp, ranges))
15+
end
16+
1117
"""
12-
`scale(itp, xs, ys, ...)` scales an existing interpolation object to allow for indexing using other coordinate axes than unit ranges, by wrapping the interpolation object and transforming the indices from the provided axes onto unit ranges upon indexing.
18+
`scale(itp, xs, ys, ...)` scales an existing interpolation object to allow for indexing using other coordinate axes than unit ranges, by wrapping the interpolation object and transforming the indices from the provided axes onto unit ranges upon indexing.a
1319
1420
The parameters `xs` etc must be either ranges or linspaces, and there must be one coordinate range/linspace for each dimension of the interpolation object.
1521
@@ -25,7 +31,7 @@ function scale{T,N,IT,GT}(itp::AbstractInterpolation{T,N,IT,GT}, ranges::Range..
2531
end
2632
end
2733

28-
ScaledInterpolation(T,N,itp,IT,GT,ranges)
34+
ScaledInterpolation(itp, ranges)
2935
end
3036

3137
@generated function getindex{T,N,ITPT,IT<:DimSpec}(sitp::ScaledInterpolation{T,N,ITPT,IT}, xs::Number...)

test/scaling/scaling.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ using Base.Test
88

99
itp = interpolate(1:1.0:10, BSpline(Linear()), OnGrid())
1010

11-
sitp = scale(itp, -3:.5:1.5)
11+
sitp = @inferred(scale(itp, -3:.5:1.5))
1212

1313
for (x,y) in zip(-3:.05:1.5, 1:.1:10)
1414
@test_approx_eq sitp[x] y
@@ -24,7 +24,7 @@ ys = -4:.2:4
2424
zs = Float64[testfunction(x,y) for x in xs, y in ys]
2525

2626
itp2 = interpolate(zs, BSpline(Quadratic(Flat())), OnGrid())
27-
sitp2 = scale(itp2, xs, ys)
27+
sitp2 = @inferred scale(itp2, xs, ys)
2828

2929
for x in xs, y in ys
3030
@test_approx_eq testfunction(x,y) sitp2[x,y]
@@ -34,7 +34,7 @@ end
3434
xs = -pi:.1:pi
3535
ys = sin(xs)
3636
itp = interpolate(ys, BSpline(Linear()), OnGrid())
37-
sitp = scale(itp, xs)
37+
sitp = @inferred scale(itp, xs)
3838

3939
for x in -pi:.1:pi
4040
g = @inferred(gradient(sitp, x))[1]
@@ -46,13 +46,13 @@ end
4646
@inferred(getindex(sitp2, -3, 1))
4747
@inferred(getindex(sitp2, -3.4, 1))
4848

49-
sitp32 = scale(interpolate(Float32[testfunction(x,y) for x in -5:.5:5, y in -4:.2:4], BSpline(Quadratic(Flat())), OnGrid()), -5f0:.5f0:5f0, -4f0:.2f0:4f0)
49+
sitp32 = @inferred scale(interpolate(Float32[testfunction(x,y) for x in -5:.5:5, y in -4:.2:4], BSpline(Quadratic(Flat())), OnGrid()), -5f0:.5f0:5f0, -4f0:.2f0:4f0)
5050
@test typeof(@inferred(getindex(sitp32, -3.4f0, 1.2f0))) == Float32
5151

5252
# Iteration
5353
itp = interpolate(rand(3,3,3), BSpline(Quadratic(Flat())), OnCell())
5454
knots = map(d->1:100:201, 1:3)
55-
sitp = scale(itp, knots...)
55+
sitp = @inferred scale(itp, knots...)
5656
function foo!(dest, sitp)
5757
i = 0
5858
for s in eachvalue(sitp)

0 commit comments

Comments
 (0)