Hi all,
I am trying to perform linear interpolation on some gridded data using the Interpolations.jl package. Suppose that I have 2 variables (x,y) and a function z=f(x,y). I want to interpolate a vector of values V={ (x1,y1), (x2,y2), … (xm,ym) } at once, without using a loop across the m points.
Suppose that I have the following:
using Interpolations
x = collect(linspace(0,1,10)) #x grid
y = collect(linspace(0,1,10)) #y grid
function fxy(x,y)
z = x.^2+y.^2
return z
end
z = zeros(length(x), length(y))
for i_x=1:length(x)
for i_y=1:length(y)
z[i_x,i_y] = fxy(x[i_x],y[i_y])
end
end
I want to interpolate a vector of data points at once, without the use of a loop across the different points. To be clear:
#Create interpolation object
FF = interpolate((x,y),z,Gridded(Linear()))
#I want to interpolate fxy on the following points:
xy_data = [collect(linspace(1,5,5)) collect(linspace(5,10,5))] #Object that I want to interpolate
#This is the output that I want (a 5x1 vector with the values of fxy at each of the data points)
output_desired = zeros(size(xy_data,1))
for i=1:size(xy_data,1)
output_desired[i] = FF[xy_data[i,1], xy_data[i,2]]
end
I cannot find a way of obtaining the same result without the use of a loop.
For my application, I need to repeat these interpolations many times as the interpolation object will be inside a non-linear solver. So performance is key and I need to avoid looping across the M points of the xy_data vector.
For example, the following will interpolate all possible combinations of (x,y) in the xy_data matrix (it returns a 5x5 matrix)
output_matrix = FF[xy_data[:,1], xy_data[:,2]]
The above line is in fact faster than using the loop across points. But I just want the diagonal of output_matrix.
Is there any way to do this?
Thanks,