Skip to content

why is BSpline() only working for regular grids? #131

@floswald

Description

@floswald

I was wondering how hard it would be to support irregular grids for BSpline interpolation. I am aware that you have Gridded for this case, however, I often end up missing features of Gridded which BSpline supports. for example, I can't linearly extrapolate a 2D gridded interpolation, whereas I can do that with a BSpline(Quadratic(Free())). For many of my applications, irregular grids are necessary.
If performance is worse on irregular grids, so be it.

julia> xmax, ymax = 8,8
(8,8)

julia> g(x, y) = (x^2 + 3x - 8) * (-2y^2 + y + 1)
g (generic function with 1 method)

# extrapolate from a 2D BSpline on regular grid

itp2g = interpolate(Float64[g(x,y) for x in 1:xmax, y in 1:ymax], (BSpline(Quadratic(Free())), BSpline(Linear())), OnGrid())
8×8 Interpolations.BSplineInterpolation{Float64,2,Array{Float64,2},Tuple{Interpolations.BSpline{Interpolations.Quadratic{Interpolations.Free}},Interpolations.BSpline{Interpolations.Linear}},Interpolations.OnGrid,(1,0)}:
 0.0    20.0     56.0    108.0    176.0    260.0    360.0    476.0
 0.0   -10.0    -28.0    -54.0    -88.0   -130.0   -180.0   -238.0
 0.0   -50.0   -140.0   -270.0   -440.0   -650.0   -900.0  -1190.0
 0.0  -100.0   -280.0   -540.0   -880.0  -1300.0  -1800.0  -2380.0
 0.0  -160.0   -448.0   -864.0  -1408.0  -2080.0  -2880.0  -3808.0
 0.0  -230.0   -644.0  -1242.0  -2024.0  -2990.0  -4140.0  -5474.0
 0.0  -310.0   -868.0  -1674.0  -2728.0  -4030.0  -5580.0  -7378.0
 0.0  -400.0  -1120.0  -2160.0  -3520.0  -5200.0  -7200.0  -9520.0

julia> ext = extrapolate(itp2g,(Linear(),Flat()))
8×8 Interpolations.Extrapolation{Float64,2,Interpolations.BSplineInterpolation{Float64,2,Array{Float64,2},Tuple{Interpolations.BSpline{Interpolations.Quadratic{Interpolations.Free}},Interpolations.BSpline{Interpolations.Linear}},Interpolations.OnGrid,(1,0)},Tuple{Interpolations.BSpline{Interpolations.Quadratic{Interpolations.Free}},Interpolations.BSpline{Interpolations.Linear}},Interpolations.OnGrid,Tuple{Interpolations.Linear,Interpolations.Flat}}:
 0.0    20.0     56.0    108.0    176.0    260.0    360.0    476.0
 0.0   -10.0    -28.0    -54.0    -88.0   -130.0   -180.0   -238.0
 0.0   -50.0   -140.0   -270.0   -440.0   -650.0   -900.0  -1190.0
 0.0  -100.0   -280.0   -540.0   -880.0  -1300.0  -1800.0  -2380.0
 0.0  -160.0   -448.0   -864.0  -1408.0  -2080.0  -2880.0  -3808.0
 0.0  -230.0   -644.0  -1242.0  -2024.0  -2990.0  -4140.0  -5474.0
 0.0  -310.0   -868.0  -1674.0  -2728.0  -4030.0  -5580.0  -7378.0
 0.0  -400.0  -1120.0  -2160.0  -3520.0  -5200.0  -7200.0  -9520.0

julia> ext[8,9]
-9520.000000000002

julia> ext[9,8]
-11781.0

# extrapolate from a 2D gridded interpolation (misses gradient implementation

itp2g = interpolate((1:xmax,1:ymax),Float64[g(x,y) for x in 1:xmax, y in 1:ymax], (Gridded(Linear()),Gridded(Linear())))
8×8 Interpolations.GriddedInterpolation{Float64,2,Float64,Tuple{Interpolations.Gridded{Interpolations.Linear},Interpolations.Gridded{Interpolations.Linear}},Tuple{Array{Int64,1},Array{Int64,1}},0}:
 0.0    20.0     56.0    108.0    176.0    260.0    360.0    476.0
 0.0   -10.0    -28.0    -54.0    -88.0   -130.0   -180.0   -238.0
 0.0   -50.0   -140.0   -270.0   -440.0   -650.0   -900.0  -1190.0
 0.0  -100.0   -280.0   -540.0   -880.0  -1300.0  -1800.0  -2380.0
 0.0  -160.0   -448.0   -864.0  -1408.0  -2080.0  -2880.0  -3808.0
 0.0  -230.0   -644.0  -1242.0  -2024.0  -2990.0  -4140.0  -5474.0
 0.0  -310.0   -868.0  -1674.0  -2728.0  -4030.0  -5580.0  -7378.0
 0.0  -400.0  -1120.0  -2160.0  -3520.0  -5200.0  -7200.0  -9520.0

julia> e = extrapolate(itp2g,(Linear(),Linear())
       )
WARNING: imported binding for e overwritten in module Main
8×8 Interpolations.Extrapolation{Float64,2,Interpolations.GriddedInterpolation{Float64,2,Float64,Tuple{Interpolations.Gridded{Interpolations.Linear},Interpolations.Gridded{Interpolations.Linear}},Tuple{Array{Int64,1},Array{Int64,1}},0},Tuple{Interpolations.Gridded{Interpolations.Linear},Interpolations.Gridded{Interpolations.Linear}},Interpolations.OnGrid,Tuple{Interpolations.Linear,Interpolations.Linear}}:
 0.0    20.0     56.0    108.0    176.0    260.0    360.0    476.0
 0.0   -10.0    -28.0    -54.0    -88.0   -130.0   -180.0   -238.0
 0.0   -50.0   -140.0   -270.0   -440.0   -650.0   -900.0  -1190.0
 0.0  -100.0   -280.0   -540.0   -880.0  -1300.0  -1800.0  -2380.0
 0.0  -160.0   -448.0   -864.0  -1408.0  -2080.0  -2880.0  -3808.0
 0.0  -230.0   -644.0  -1242.0  -2024.0  -2990.0  -4140.0  -5474.0
 0.0  -310.0   -868.0  -1674.0  -2728.0  -4030.0  -5580.0  -7378.0
 0.0  -400.0  -1120.0  -2160.0  -3520.0  -5200.0  -7200.0  -9520.0

julia> e[-1,1]
ERROR: MethodError: no method matching gradient_coefficients(::Type{Tuple{Interpolations.Gridded{Interpolations.Linear},Interpolations.Gridded{Interpolations.Linear}}}, ::Int64, ::Int64)


# create an invalid BSpline (on irregular grid)

itp2g = interpolate(Float64[g(x,y) for x in vcat(0.0,linspace(4.0,xmax,xmax-1)), y in 1:ymax], (BSpline(Quadratic(Free())), BSpline(Linear())), OnGrid())
8×8 Interpolations.BSplineInterpolation{Float64,2,Array{Float64,2},Tuple{Interpolations.BSpline{Interpolations.Quadratic{Interpolations.Free}},Interpolations.BSpline{Interpolations.Linear}},Interpolations.OnGrid,(1,0)}:
 0.0    40.0      112.0      216.0    352.0     520.0     720.0    952.0 
 0.0  -100.0     -280.0     -540.0   -880.0   -1300.0   -1800.0  -2380.0 
 0.0  -138.889   -388.889   -750.0  -1222.22  -1805.56  -2500.0  -3305.56
 0.0  -182.222   -510.222   -984.0  -1603.56  -2368.89  -3280.0  -4336.89
 0.0  -230.0     -644.0    -1242.0  -2024.0   -2990.0   -4140.0  -5474.0 
 0.0  -282.222   -790.222  -1524.0  -2483.56  -3668.89  -5080.0  -6716.89
 0.0  -338.889   -948.889  -1830.0  -2982.22  -4405.56  -6100.0  -8065.56
 0.0  -400.0    -1120.0    -2160.0  -3520.0   -5200.0   -7200.0  -9520.0 

julia> vcat(0.0,linspace(4.0,xmax,xmax-1))
8-element Array{Float64,1}:
 0.0    
 4.0    
 4.66667
 5.33333
 6.0    
 6.66667
 7.33333
 8.0    

julia> itp2g[1,1]
0.0

julia> itp2g[8,2]
-400.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions