Skip to content

Commit c564934

Browse files
committed
fixed getindex_return_type
1 parent c630c9c commit c564934

File tree

4 files changed

+32
-24
lines changed

4 files changed

+32
-24
lines changed

src/b-splines/indexing.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Base.Cartesian
2+
using DualNumbers
23

34
import Base.getindex
45

@@ -79,14 +80,13 @@ function gradient_impl{T,N,TCoefs,IT<:DimSpec{BSpline},GT<:DimSpec{GridType},Pad
7980
end
8081
end
8182

82-
function getindex_return_type{T,N,TCoefs,IT<:DimSpec{BSpline},GT<:DimSpec{GridType},Pad}(::Type{BSplineInterpolation{T,N,TCoefs,IT,GT,Pad}}, argtypes)
83-
Tret = eltype(TCoefs)
84-
for a in argtypes
85-
Tret = Base.promote_op(@functorize(*), Tret, a) # the macro is used to support julia 0.4
86-
end
87-
Tret
88-
end
83+
# there is a Heisenbug, when Base.promote_op is inlined into getindex_return_type
84+
# thats why we use this @noinline fence
85+
@noinline _promote_mul(a,b) = Base.promote_op(@functorize(*), a, b)
8986

87+
@noinline function getindex_return_type{T,N,TCoefs,IT<:DimSpec{BSpline},GT<:DimSpec{GridType},Pad}(::Type{BSplineInterpolation{T,N,TCoefs,IT,GT,Pad}}, argtypes)
88+
reduce(_promote_mul, eltype(TCoefs), argtypes)
89+
end
9090

9191
@generated function gradient!{T,N}(g::AbstractVector, itp::BSplineInterpolation{T,N}, xs::Number...)
9292
length(xs) == N || error("Can only be called with $N indexes")

src/extrapolation/filled.jl

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,28 @@ end
1414
"""
1515
extrapolate{T,N,IT,GT}(itp::AbstractInterpolation{T,N,IT,GT}, fillvalue) = FilledExtrapolation(itp, convert(eltype(itp), fillvalue))
1616

17+
function getindex_impl{T,N,ITP,IT,GT,FT}(fitp::Type{FilledExtrapolation{T,N,ITP,IT,GT,FT}}, args)
18+
n = length(args)
19+
n == N || return error("Must index $(N)-dimensional interpolation objects with $(nindexes(N))")
20+
21+
Tret = FT<:Number ? getindex_return_type(ITP, args) : FT
22+
meta = Expr(:meta, :inline)
23+
quote
24+
$meta
25+
# Check to see if we're in the extrapolation region, i.e.,
26+
# out-of-bounds in an index
27+
@nexprs $N d->((args[d] < lbound(fitp,d) || args[d] > ubound(fitp, d))) && return convert($Tret, fitp.fillvalue)::$Tret
28+
# In the interpolation region
29+
return convert($Tret, getindex(fitp.itp,args...))::$Tret
30+
end
31+
end
32+
33+
1734
@generated function getindex{T,N,ITP,IT,GT,FT}(fitp::FilledExtrapolation{T,N,ITP,IT,GT,FT}, args::Number...)
18-
n = length(args)
19-
n == N || return error("Must index $(N)-dimensional interpolation objects with $(nindexes(N))")
20-
Tret = FT<:Number ? getindex_return_type(ITP, args) : FT
21-
meta = Expr(:meta, :inline)
22-
quote
23-
$meta
24-
# Check to see if we're in the extrapolation region, i.e.,
25-
# out-of-bounds in an index
26-
@nexprs $N d->((args[d] < lbound(fitp,d) || args[d] > ubound(fitp, d)) && return convert($Tret, fitp.fillvalue))
27-
# In the interpolation region
28-
return getindex(fitp.itp,args...)
29-
end
35+
getindex_impl(fitp, args)
3036
end
3137

3238
getindex{T}(fitp::FilledExtrapolation{T,1}, x::Number, y::Int) = y == 1 ? fitp[x] : throw(BoundsError())
3339

3440
lbound(etp::FilledExtrapolation, d) = lbound(etp.itp, d)
35-
ubound(etp::FilledExtrapolation, d) = ubound(etp.itp, d)
41+
ubound(etp::FilledExtrapolation, d) = ubound(etp.itp, d)

test/extrapolation/runtests.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module ExtrapTests
33
using Base.Test, DualNumbers
44
using Interpolations
55

6+
67
f(x) = sin((x-3)*2pi/9 - 1)
78
xmax = 10
89
A = Float64[f(x) for x in 1:xmax]
@@ -27,7 +28,8 @@ etpf = @inferred(extrapolate(itpg, NaN))
2728
@test_throws BoundsError etpf[2.5,2]
2829
@test_throws ErrorException etpf[2.5,2,1] # this will probably become a BoundsError someday
2930

30-
@test_broken isa(@inferred(getindex(etpf, dual(-2.5,1))), Dual)
31+
x = @inferred(getindex(etpf, dual(-2.5,1)))
32+
@test isa(x, Dual)
3133

3234
etpl = extrapolate(itpg, Linear())
3335
k_lo = A[2] - A[1]

test/runtests.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ module RunTests
33
using Base.Test
44
using Interpolations
55

6-
# b-spline interpolation tests
7-
include("b-splines/runtests.jl")
86

97
# extrapolation tests
108
include("extrapolation/runtests.jl")
9+
# b-spline interpolation tests
10+
include("b-splines/runtests.jl")
1111

1212
# scaling tests
1313
include("scaling/runtests.jl")
@@ -28,4 +28,4 @@ include("issues/runtests.jl")
2828

2929
end
3030

31-
nothing
31+
nothing

0 commit comments

Comments
 (0)