Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interpolation in vector-valued multi-variate function

In Python, I'm trying to construct a routine that interpolates in vector-valued data in a multi-dimensional (5+) parameter space. i.e. I have a function that takes a number of input variables and returns a number of output variables. At the moment, there is one call for each element of the vector. The data is in a columned file, so I retrieve it with

import numpy
[x_data,y_data,f1_data,f2_data] = numpy.loadtxt('data',unpack=True)

Then, I instantiate individual interpolators using SciPy's functions, like

from scipy import interpolate
f1 = interpolate.LinearNDInterpolator((x_data,y_data),f1_data)
f2 = interpolate.LinearNDInterpolator((x_data,y_data),f2_data)
...

Now, when I make the interpolation call, I have to interpolate for each value f1, f2, etc. even though really it should be achievable as one operation. And I'm guessing that making one interpolation should be faster than making 5 or more.

Is there a way to construct a vector- (or array-) valued interpolator?

I tried constructing the interpolator with

f = interpolate.LinearNDInterpolator((x_data,y_data),(f1_data,f2_data,...))

but it returns the error

ValueError: different number of values and points

I've also read this question and answer but it's about a vector-valued function of a scalar, which can apparently be handled by interp1d.

like image 620
Warrick Avatar asked Sep 12 '12 15:09

Warrick


1 Answers

scipy.interpolate.LinearNDInterpolator expects to receive its data in row-major order: for example in your case, the first argument needs to be an array of pairs, not a pair of arrays. Since you transposed your data when you loaded it, you'll have to transpose it back again before passing it to LinearNDInterpolator. Try something like:

points = numpy.array((x, y)).T
values = numpy.array((f1, f2)).T
f = interpolate.LinearNDInterpolator(points, values)
like image 54
Gareth Rees Avatar answered Sep 28 '22 08:09

Gareth Rees