Interpolation in vector-valued multi-variate function

时光毁灭记忆、已成空白 提交于 2019-12-30 06:49:40

问题


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.


回答1:


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)


来源:https://stackoverflow.com/questions/12391747/interpolation-in-vector-valued-multi-variate-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!