Skip to content

Commit 646e28f

Browse files
author
Tomas Lycken
committed
Merge pull request #48 from tlycken/simplify-indexing
Simplify indexing
2 parents ba01661 + c297bab commit 646e28f

6 files changed

Lines changed: 16 additions & 75 deletions

File tree

src/Interpolations.jl

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,6 @@ size{T,N}(itp::AbstractInterpolation{T,N}) = ntuple(i->size(itp,i), N)::NTuple{N
5454
size(exp::AbstractExtrapolation, d) = size(exp.itp, d)
5555
gridtype{T,N,IT,GT}(itp::AbstractInterpolation{T,N,IT,GT}) = GT
5656

57-
# Fix display issues by defining odd combinations of dimensionality and index
58-
function getindex{T}(itp::AbstractInterpolation{T,1}, x::Real, y::Real)
59-
if y != 1
60-
throw(ArgumentError("Cannot index into 1D interpolation with two indices unless y == 1 (y == $y)"))
61-
end
62-
itp[x]
63-
end
64-
6557
@inline gradient{T,N}(itp::AbstractInterpolation{T,N}, xs...) = gradient!(Array(T,N), itp, xs...)
6658

6759
include("b-splines/b-splines.jl")

src/b-splines/indexing.jl

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ using Base.Cartesian
22

33
import Base.getindex
44

5+
Base.linearindexing{T<:AbstractInterpolation}(::Type{T}) = Base.LinearSlow()
6+
57
function getindex_impl{T,N,TCoefs,IT<:DimSpec{BSpline},GT<:DimSpec{GridType},Pad}(itp::Type{BSplineInterpolation{T,N,TCoefs,IT,GT,Pad}})
68
meta = Expr(:meta, :inline)
79
quote
@@ -21,32 +23,6 @@ function getindex_impl{T,N,TCoefs,IT<:DimSpec{BSpline},GT<:DimSpec{GridType},Pad
2123
end
2224
end
2325

24-
# Resolve ambiguity with general array indexing,
25-
# getindex{T,N}(A::AbstractArray{T,N}, I::AbstractArray{T,N})
26-
function getindex{T,N}(itp::BSplineInterpolation{T,N}, I::AbstractArray{T,N})
27-
error("Array indexing is not defined for interpolation objects.")
28-
end
29-
30-
# Resolve ambiguity with colon indexing for 1D interpolations
31-
# getindex{T}(A::AbstractArray{T,1}, C::Colon)
32-
function getindex{T}(itp::BSplineInterpolation{T,1}, c::Colon)
33-
error("Colon indexing is not supported for interpolation objects")
34-
end
35-
36-
# Resolve ambiguity with indexing with Real indices
37-
# getindex{T,N}(A::AbstractArray{T,N}, x::Real...)
38-
@generated function getindex{T,N,TCoefs,IT<:BSpline}(itp::BSplineInterpolation{T,N,TCoefs,IT}, xs::Real...)
39-
getindex_impl(itp)
40-
end
41-
42-
# Linear indexing is supported only for 1D interpolations
43-
@generated function getindex{T,N}(itp::BSplineInterpolation{T,N}, xs::Real)
44-
if N > 1
45-
error("Linear indexing is not supported for interpolation objects")
46-
end
47-
getindex_impl(itp)
48-
end
49-
5026
@generated function getindex{T,N}(itp::BSplineInterpolation{T,N}, xs...)
5127
getindex_impl(itp)
5228
end

src/extrapolation/constant.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ ConstantExtrapolation{T,ITP,IT,GT}(::Type{T}, N, itp::ITP, ::Type{IT}, ::Type{GT
77
extrapolate{T,N,IT,GT}(itp::AbstractInterpolation{T,N,IT,GT}, ::Type{Flat}) =
88
ConstantExtrapolation(T,N,itp,IT,GT)
99

10-
function extrap_prep{T,ITP,IT}(exp::Type{ConstantExtrapolation{T,1,ITP,IT,OnGrid}}, xs)
11-
:(xs = clamp(xs, 1, size(exp,1)))
10+
function extrap_prep{T,ITP,IT}(exp::Type{ConstantExtrapolation{T,1,ITP,IT,OnGrid}}, x)
11+
:(x = clamp(x, 1, size(exp,1)))
1212
end
13-
function extrap_prep{T,ITP,IT}(exp::Type{ConstantExtrapolation{T,1,ITP,IT,OnCell}}, xs)
14-
:(xs = clamp(xs, .5, size(exp,1)+.5))
13+
function extrap_prep{T,ITP,IT}(exp::Type{ConstantExtrapolation{T,1,ITP,IT,OnCell}}, x)
14+
:(x = clamp(x, .5, size(exp,1)+.5))
1515
end
1616
function extrap_prep{T,N,ITP,IT}(exp::Type{ConstantExtrapolation{T,N,ITP,IT,OnGrid}}, xs...)
17-
:(@nexprs $N d->(@show xs[d] = clamp(xs[d], 1, size(exp,d))))
17+
:(@nexprs $N d->(xs[d] = clamp(xs[d], 1, size(exp,d))))
1818
end
1919
function extrap_prep{T,N,ITP,IT}(exp::Type{ConstantExtrapolation{T,N,ITP,IT,OnCell}}, xs...)
20-
:(@nexprs $N d->(@show xs[d] = clamp(xs[d], .5, size(exp,d)+.5)))
20+
:(@nexprs $N d->(xs[d] = clamp(xs[d], .5, size(exp,d)+.5)))
2121
end

src/extrapolation/indexing.jl

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,13 @@
1-
function extrap_impl{T<:AbstractExtrapolation}(exp::Type{T}, xs...)
1+
@generated function getindex{T}(exp::AbstractExtrapolation{T,1}, x)
22
quote
3-
$(extrap_prep(exp, xs...))
4-
exp.itp[xs...]
5-
end
6-
end
7-
8-
9-
# Resolve ambiguity with general array indexing,
10-
# getindex{T,N}(A::AbstractArray{T,N}, I::AbstractArray{T,N})
11-
function getindex{T,N}(exp::AbstractExtrapolation{T,N}, I::AbstractArray{T,N})
12-
error("Array indexing is not defined for interpolation objects.")
13-
end
14-
15-
# Resolve ambiguity with colon indexing for 1D interpolations
16-
# getindex{T}(A::AbstractArray{T,1}, C::Colon)
17-
function getindex{T}(exp::AbstractExtrapolation{T,1}, c::Colon)
18-
error("Colon indexing is not supported for interpolation objects")
19-
end
20-
21-
# Resolve ambiguity with indexing with Real indices
22-
# getindex{T,N}(A::AbstractArray{T,N}, x::Real...)
23-
@generated function getindex{T,N,ITP,GT}(exp::AbstractExtrapolation{T,N,ITP,GT}, xs::Real...)
24-
:($extrap_impl(exp, xs...))
25-
end
26-
27-
# Linear indexing is supported only for 1D interpolations
28-
@generated function getindex{T,N}(exp::AbstractExtrapolation{T,N}, xs::Real)
29-
if N > 1
30-
error("Linear indexing is not supported for interpolation objects")
3+
$(extrap_prep(exp, x))
4+
exp.itp[x]
315
end
32-
:($(extrap_impl(exp, xs)))
336
end
347

358
@generated function getindex{T,N,ITP,GT}(exp::AbstractExtrapolation{T,N,ITP,GT}, xs...)
36-
:($(extrap_impl(exp, xs...)))
9+
quote
10+
$(extrap_prep(exp, xs...))
11+
exp.itp[xs...]
12+
end
3713
end

test/extrapolation/runtests.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ itpg = interpolate(A, BSpline(Linear), OnGrid)
1212
expg = extrapolate(itpg, Flat)
1313

1414
@test expg[-3] == expg[-4.5] == expg[0.9] == expg[1.0] == A[1]
15-
#@test
16-
expg[10.1] == expg[11]# == expg[148.298452] == A[end]
15+
@test expg[10.1] == expg[11] == expg[148.298452] == A[end]
1716

1817
end

test/runtests.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ include("gradient.jl")
1818
# Tests copied from Grid.jl's old test suite
1919
#include("grid.jl")
2020

21-
println("all tests passed")
22-
2321
end
2422

2523
nothing

0 commit comments

Comments
 (0)