I’m trying to convert a Python code to Julia that makes use of scipy.interpolate.interp1d, and I’m having difficulty finding the best way to do this in Julia. The interp1d function does 1D interpolation along a given dimension of multidimensional array.
I’ve been using Interpolations.jl but my approach does not seem the most elegant (see below). There is a very similar question asked three years ago, but the accepted answer does not provide a straightforward answer.
Here’s a simple example of what I’m looking for:
# Test data
x = [0., 2., 5., 10., 11.]
y = repeat(x .^ 2, outer=(1, 3, 3))
# We want to interpolate y (a 3D array) along the first dimension,
# so that the output is another 3D array
itp = interp1d(x, y; dims=1, k=1)
# Expected results
itp(2.0)
3×3 Matrix{Float64}:
4.0 4.0 4.0
4.0 4.0 4.0
4.0 4.0 4.0
itp(3)
3×3 Matrix{Float64}:
11.0 11.0 11.0
11.0 11.0 11.0
11.0 11.0 11.0
My data are all in irregular grids.
This seems to work, but I wonder if it is the best approach:
And it still seems that with Gridded the highest order interpolation supported is linear.
Anyone else has a better approach for this type of interpolation?
Thanks for the suggestion. I had looked at Interp1d.jl before, but it seems very alpha. Not registered package, no documentation, does not work in 3D or higher (only 1D or 2D) and does not even support linear interpolation (only nearest neighbour or previous).
It’s common in Julia to make your function work on 1-d arrays (vectors) and then just map it over slices of n-d arrays.
Suppose you already have interpolation working exactly as you need for vectors, implemented in func(vector). Then mapslices(func, A; dims=1) should work, or map(func, eachslice(...)) - depending on how you prefer the result to be shaped.