JuliaMath / Interpolations.jl

Fast, continuous interpolation of discrete datasets in Julia
http://juliamath.github.io/Interpolations.jl/
Other
512 stars 106 forks source link

Interpolation (BSpline) with input matrix X and output vector Y #387

Open gregorykimmel opened 3 years ago

gregorykimmel commented 3 years ago

Using Interpolations

x = rand(50,5)*2 .- 1 # Fake input data
y = rand(50)  # Fake output data

itp = interpolate((x,),y, BSpline(Linear()))

ERROR: MethodError: no method matching interpolate(::Tuple{Array{Float64,1}}, ::Array{Float64,1}, ::BSpline{Linear})
Closest candidates are:
  interpolate(::Tuple{Vararg{Union{AbstractArray{T,1}, Tuple} where T,N}}, ::AbstractArray{Tel,N}, ::IT) where
{Tel, N, IT<:Union{NoInterp, Tuple{Vararg{Union{NoInterp, Gridded},N} where N}, Gridded}} 
at .../.julia/packages/Interpolations/XUr6v/src/gridded/gridded.jl:83

Is there any way to do this? I would like to end up with a function itp that can take numbers from the matrix x e.g. itp(0.1,0.3,0.01,-0.4,0.5) and return a value that resulted from the interpolation (assuming that vector was not a row in the data x)

mkitti commented 3 years ago

This might be possible if you sorted x and gave each column as a distinct element of the tuple. I'm assuming x represents a 5D space?

gregorykimmel commented 3 years ago

Is this the only way? We could sort by a particular column, but in general the other columns would not be sorted as well. A sample of the actual x

 0.8   1.3   0.5  0.6   0.5
 1.2   1.0  -0.4  0.4   0.4
 0.6   0.7  -0.5  0.1  -0.1
 0.2   0.9   1.1  0.1   0.1
 1.5   2.1   1.3  3.1   0.2
 1.9  -0.1   0.3  1.1   1.8
 0.9   0.0   0.0  0.0   0.8
 1.1   1.3  -0.8  0.2   0.3

with y

 0.346573590279973
 0.277258872223978
 0.154032706791099
 0.198042051588556
 0.346573590279973
 0.693147180559945
 0.462098120373297
 0.346573590279973

I am getting the impression that interpolation is difficult to do in this setting, but I am wondering why this package in R seems to be able to do it https://cran.r-project.org/web/packages/chebpol/chebpol.pdf. In particular the part on scattered data.

mkitti commented 3 years ago

I'm still rather confused exactly how you want to process x. Each row is a vector? (Julia is column major, so vectors are usually columns). Then you have some mapping from the vectors to a single value. How do we interdigitate over the vectors? What is the metric of the vector space? Or is this some sparse sampling of a space?

There are a few other packages that handle Chebyshev polynomials, although I'm not really sure where they are with multidimensional capabilities. https://github.com/JuliaApproximation/ApproxFun.jl https://juliamath.github.io/Polynomials.jl/dev/polynomials/chebyshev/

mkitti commented 3 years ago

Could you give me some example calculations? How would you compute itp(0.1,0.3,0.01,-0.4,0.5) with your example x and y?

gregorykimmel commented 3 years ago

Perhaps this will also be useful in regards to the R package in terms of the functionality I'm hoping to use in Julia: https://cran.r-project.org/web/packages/chebpol/vignettes/chebpol.pdf. In particular the last few pages (beginning at the end of page 7) discusses the two solvers available for scattered data describes an example.