Interpolations.jl icon indicating copy to clipboard operation
Interpolations.jl copied to clipboard

MethodError bounds for CubicSplineInterpolation

Open hendri54 opened this issue 3 years ago • 4 comments

Calling bounds on a CubicSplineInterpolation throws a MethodError:

julia> xV = LinRange(-20.0, 10.0, 50);
julia> yV = LinRange(-10.0, 30.0, 40);
julia> zM = xV .+ xV .* yV';
julia> cubic1 = CubicSplineInterpolation((xV, yV), zM);
julia> bounds(cubic1)
ERROR: MethodError: no method matching lbounds(::Interpolations.Extrapolation{Float64, 2, ScaledInterpolation{Float64, 2, Interpolations.BSplineInterpolation{Float64, 2, OffsetArrays.OffsetMatrix{Float64, Matrix{Float64}}, BSpline{Cubic{Line{OnGrid}}}, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}, BSpline{Cubic{Line{OnGrid}}}, Tuple{LinRange{Float64}, LinRange{Float64}}}, BSpline{Cubic{Line{OnGrid}}}, Throw{Nothing}})
Closest candidates are:
  lbounds(::Interpolations.BSplineInterpolation) at /Users/lutz/.julia/packages/Interpolations/GIn2o/src/b-splines/b-splines.jl:133
  lbounds(::Interpolations.GriddedInterpolation) at /Users/lutz/.julia/packages/Interpolations/GIn2o/src/gridded/gridded.jl:92
  lbounds(::Interpolations.MonotonicInterpolation) at /Users/lutz/.julia/packages/Interpolations/GIn2o/src/monotonic/monotonic.jl:387
  ...
Stacktrace:
 [1] bounds(itp::Interpolations.Extrapolation{Float64, 2, ScaledInterpolation{Float64, 2, Interpolations.BSplineInterpolation{Float64, 2, OffsetArrays.OffsetMatrix{Float64, Matrix{Float64}}, BSpline{Cubic{Line{OnGrid}}}, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}, BSpline{Cubic{Line{OnGrid}}}, Tuple{LinRange{Float64}, LinRange{Float64}}}, BSpline{Cubic{Line{OnGrid}}}, Throw{Nothing}})
   @ Interpolations ~/.julia/packages/Interpolations/GIn2o/src/Interpolations.jl:146
 [2] top-level scope

Constructing what appears to be the same interpolation the long way round does not throw a MethodError:

julia> cubic2 = interpolate(zM, BSpline(Cubic(Line(OnGrid()))));
julia> cubic2 = scale(cubic2, xV, yV);
julia> cubic1(-10.5, 7.5)
-89.25
julia> cubic2(-10.5, 7.5)
-89.25
julia> bounds(cubic2)
((-20.0, 10.0), (-10.0, 30.0))

I thought both ways of constructing the CubicSplineInterpolation were the same. What am I missing?

hendri54 avatar May 21 '21 13:05 hendri54

In the first case, it seems to be producing an Extrapolation for some reason.

mkitti avatar May 21 '21 16:05 mkitti

Yes, but still throws a BoundsError when out of range. So the Extrapolation seems to be Throw in both cases.

hendri54 avatar May 21 '21 17:05 hendri54

Can you obtain the bounds via bounds(parent(cubic1))?

mkitti avatar May 21 '21 17:05 mkitti

Yes, that works.

hendri54 avatar May 21 '21 18:05 hendri54