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

Interpolation of whole matrices with Cubic, Quadratic etc.

Open rafaqz opened this issue 5 years ago • 1 comments

Currently this only works with Linear:

julia> data = [[1 2; 3 4], [5 6; 7 8]]
2-element Array{Array{Int64,2},1}:
 [1 2; 3 4]
 [5 6; 7 8]

julia> itp = interpolate(data, BSpline(Linear()))
2-element interpolate(::Array{Array{Int64,2},1}, BSpline(Linear())) with element type Array{Float64,2}:
 [1 2; 3 4]
 [5 6; 7 8]

julia> itp[1.0]
2×2 Array{Float64,2}:
 1.0  2.0
 3.0  4.0

julia> itp[1.1]
2×2 Array{Float64,2}:
 1.4  2.4
 3.4  4.4

julia> itp = interpolate(data, BSpline(Cubic(Line(OnGrid()))))
ERROR: MethodError: no method matching zero(::Type{Array{Int64,2}})
Closest candidates are:
  zero(::Type{Pkg.Resolve.FieldValue}) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/Pkg/src/Resolve/fieldvalues.jl:38
  zero(::Type{DateTime}) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/Dates/src/types.jl:404
  zero(::Type{Measures.Length{:mm,Float64}}) at /home/raf/.julia/packages/Plots/M1wcx/src/layouts.jl:12
  ...
Stacktrace:
 [1] copy_with_padding(::Type{Array{Int64,2}}, ::Array{Array{Int64,2},1}, ::BSpline{Cubic{Line{OnGrid}}}) at /home/raf/.julia/packages/Interpolations/TBuvH/src/b-splines/prefiltering.jl:27
 [2] prefilter(::Type{Float64}, ::Type{Array{Int64,2}}, ::Array{Array{Int64,2},1}, ::BSpline{Cubic{Line{OnGrid}}}) at /home/raf/.julia/packages/Interpolations/TBuvH/src/b-splines/prefiltering.jl:39
 [3] interpolate(::Type{Float64}, ::Type{Array{Int64,2}}, ::Array{Array{Int64,
2},1}, ::BSpline{Cubic{Line{OnGrid}}}) at /home/raf/.julia/packages/Interpolations/TBuvH/src/b-splines/b-splines.jl:160
 [4] interpolate(::Array{Array{Int64,2},1}, ::BSpline{Cubic{Line{OnGrid}}}) at /home/raf/.julia/packages/Interpolations/TBuvH/src/b-splines/b-splines.jl:179
 [5] top-level scope at REPL[40]:1

Looks like it will need some work in prefiltering to use zero on the array A rather than the type TC, when you have a matrix:

https://github.com/JuliaMath/Interpolations.jl/blob/eb5690069a8c5cbe97b6ac2e44f58ea1567bee8d/src/b-splines/prefiltering.jl#L19-L31

I will be able to do this at some stage, just wondering how feasble it is and if I will break other things doing it.

rafaqz avatar Sep 16 '20 10:09 rafaqz

Note that this does already work for static matrices:

julia> using StaticArrays

julia> data = SMatrix{2,2,Float64}.([[1 2; 3 4], [5 6; 7 8]])
2-element Array{SArray{Tuple{2,2},Float64,2,4},1}:
 [1.0 2.0; 3.0 4.0]
 [5.0 6.0; 7.0 8.0]

julia> itp = interpolate(data, BSpline(Cubic(Line(OnGrid()))))
2-element interpolate(OffsetArray(::Array{SArray{Tuple{2,2},Float64,2,4},1}, 0:3), BSpline(Cubic(Line(OnGrid())))) with element type SArray{Tuple{2,2},Float64,2,4}:
 [0.9999999999999999 1.9999999999999998; 3.0 4.0]
 [5.0 6.0; 6.999999999999999 8.0]

julia> itp(1.1)
2×2 SArray{Tuple{2,2},Float64,2,4} with indices SOneTo(2)×SOneTo(2):
 1.4  2.4
 3.4  4.4

dkarrasch avatar Sep 16 '20 12:09 dkarrasch