Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions src/Interpolations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,6 @@ size{T,N}(itp::AbstractInterpolation{T,N}) = ntuple(i->size(itp,i), N)::NTuple{N
size(exp::AbstractExtrapolation, d) = size(exp.itp, d)
gridtype{T,N,IT,GT}(itp::AbstractInterpolation{T,N,IT,GT}) = GT

# Fix display issues by defining odd combinations of dimensionality and index
function getindex{T}(itp::AbstractInterpolation{T,1}, x::Real, y::Real)
if y != 1
throw(ArgumentError("Cannot index into 1D interpolation with two indices unless y == 1 (y == $y)"))
end
itp[x]
end

@inline gradient{T,N}(itp::AbstractInterpolation{T,N}, xs...) = gradient!(Array(T,N), itp, xs...)

include("b-splines/b-splines.jl")
Expand Down
28 changes: 2 additions & 26 deletions src/b-splines/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ using Base.Cartesian

import Base.getindex

Base.linearindexing{T<:AbstractInterpolation}(::Type{T}) = Base.LinearSlow()

function getindex_impl{T,N,TCoefs,IT<:DimSpec{BSpline},GT<:DimSpec{GridType},Pad}(itp::Type{BSplineInterpolation{T,N,TCoefs,IT,GT,Pad}})
meta = Expr(:meta, :inline)
quote
Expand All @@ -21,32 +23,6 @@ function getindex_impl{T,N,TCoefs,IT<:DimSpec{BSpline},GT<:DimSpec{GridType},Pad
end
end

# Resolve ambiguity with general array indexing,
# getindex{T,N}(A::AbstractArray{T,N}, I::AbstractArray{T,N})
function getindex{T,N}(itp::BSplineInterpolation{T,N}, I::AbstractArray{T,N})
error("Array indexing is not defined for interpolation objects.")
end

# Resolve ambiguity with colon indexing for 1D interpolations
# getindex{T}(A::AbstractArray{T,1}, C::Colon)
function getindex{T}(itp::BSplineInterpolation{T,1}, c::Colon)
error("Colon indexing is not supported for interpolation objects")
end

# Resolve ambiguity with indexing with Real indices
# getindex{T,N}(A::AbstractArray{T,N}, x::Real...)
@generated function getindex{T,N,TCoefs,IT<:BSpline}(itp::BSplineInterpolation{T,N,TCoefs,IT}, xs::Real...)
getindex_impl(itp)
end

# Linear indexing is supported only for 1D interpolations
@generated function getindex{T,N}(itp::BSplineInterpolation{T,N}, xs::Real)
if N > 1
error("Linear indexing is not supported for interpolation objects")
end
getindex_impl(itp)
end

@generated function getindex{T,N}(itp::BSplineInterpolation{T,N}, xs...)
getindex_impl(itp)
end
Expand Down
12 changes: 6 additions & 6 deletions src/extrapolation/constant.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ ConstantExtrapolation{T,ITP,IT,GT}(::Type{T}, N, itp::ITP, ::Type{IT}, ::Type{GT
extrapolate{T,N,IT,GT}(itp::AbstractInterpolation{T,N,IT,GT}, ::Type{Flat}) =
ConstantExtrapolation(T,N,itp,IT,GT)

function extrap_prep{T,ITP,IT}(exp::Type{ConstantExtrapolation{T,1,ITP,IT,OnGrid}}, xs)
:(xs = clamp(xs, 1, size(exp,1)))
function extrap_prep{T,ITP,IT}(exp::Type{ConstantExtrapolation{T,1,ITP,IT,OnGrid}}, x)
:(x = clamp(x, 1, size(exp,1)))
end
function extrap_prep{T,ITP,IT}(exp::Type{ConstantExtrapolation{T,1,ITP,IT,OnCell}}, xs)
:(xs = clamp(xs, .5, size(exp,1)+.5))
function extrap_prep{T,ITP,IT}(exp::Type{ConstantExtrapolation{T,1,ITP,IT,OnCell}}, x)
:(x = clamp(x, .5, size(exp,1)+.5))
end
function extrap_prep{T,N,ITP,IT}(exp::Type{ConstantExtrapolation{T,N,ITP,IT,OnGrid}}, xs...)
:(@nexprs $N d->(@show xs[d] = clamp(xs[d], 1, size(exp,d))))
:(@nexprs $N d->(xs[d] = clamp(xs[d], 1, size(exp,d))))
end
function extrap_prep{T,N,ITP,IT}(exp::Type{ConstantExtrapolation{T,N,ITP,IT,OnCell}}, xs...)
:(@nexprs $N d->(@show xs[d] = clamp(xs[d], .5, size(exp,d)+.5)))
:(@nexprs $N d->(xs[d] = clamp(xs[d], .5, size(exp,d)+.5)))
end
38 changes: 7 additions & 31 deletions src/extrapolation/indexing.jl
Original file line number Diff line number Diff line change
@@ -1,37 +1,13 @@
function extrap_impl{T<:AbstractExtrapolation}(exp::Type{T}, xs...)
@generated function getindex{T}(exp::AbstractExtrapolation{T,1}, x)
quote
$(extrap_prep(exp, xs...))
exp.itp[xs...]
end
end


# Resolve ambiguity with general array indexing,
# getindex{T,N}(A::AbstractArray{T,N}, I::AbstractArray{T,N})
function getindex{T,N}(exp::AbstractExtrapolation{T,N}, I::AbstractArray{T,N})
error("Array indexing is not defined for interpolation objects.")
end

# Resolve ambiguity with colon indexing for 1D interpolations
# getindex{T}(A::AbstractArray{T,1}, C::Colon)
function getindex{T}(exp::AbstractExtrapolation{T,1}, c::Colon)
error("Colon indexing is not supported for interpolation objects")
end

# Resolve ambiguity with indexing with Real indices
# getindex{T,N}(A::AbstractArray{T,N}, x::Real...)
@generated function getindex{T,N,ITP,GT}(exp::AbstractExtrapolation{T,N,ITP,GT}, xs::Real...)
:($extrap_impl(exp, xs...))
end

# Linear indexing is supported only for 1D interpolations
@generated function getindex{T,N}(exp::AbstractExtrapolation{T,N}, xs::Real)
if N > 1
error("Linear indexing is not supported for interpolation objects")
$(extrap_prep(exp, x))
exp.itp[x]
end
:($(extrap_impl(exp, xs)))
end

@generated function getindex{T,N,ITP,GT}(exp::AbstractExtrapolation{T,N,ITP,GT}, xs...)
:($(extrap_impl(exp, xs...)))
quote
$(extrap_prep(exp, xs...))
exp.itp[xs...]
end
end
3 changes: 1 addition & 2 deletions test/extrapolation/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ itpg = interpolate(A, BSpline(Linear), OnGrid)
expg = extrapolate(itpg, Flat)

@test expg[-3] == expg[-4.5] == expg[0.9] == expg[1.0] == A[1]
#@test
expg[10.1] == expg[11]# == expg[148.298452] == A[end]
@test expg[10.1] == expg[11] == expg[148.298452] == A[end]

end
2 changes: 0 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ include("gradient.jl")
# Tests copied from Grid.jl's old test suite
#include("grid.jl")

println("all tests passed")

end

nothing