沿曲线评估 scipy 2D 插值的输出 [英] Evaluate the output from scipy 2D interpolation along a curve
问题描述
我有从网格点 x, y
处的二维函数 f
采样的数据 z
,如 z = f(x, y)
.
I have data z
sampled from a 2D function f
at grid points x, y
, as in z = f(x, y)
.
很容易通过 f = interp2d(x, y, z
) 用 scipy.interp2d
插入 f
.
It is easy to interpolate f
with scipy.interp2d
via f = interp2d(x, y, z
).
然而,评估 f(x, y)
会返回一个完整的 2D 网格,就像我已经完成一样
However, evaluating f(x, y)
returns an entire 2D grid as if I had done
xx, yy = np.meshgrid(x, y)
f(xx, yy)
我想要的行为是简单地返回值 [f(x[i], y[i]) for i in range(len(x))]
,我认为这是numpy 中几乎任何其他方法的行为.
The behaviour I want is to simply return the values [f(x[i], y[i]) for i in range(len(x))]
, which I believe is the behaviour for pretty much any other method in numpy.
我想要这种行为的原因是我正在寻找沿着 f
的表面在时间"上由 (t, u(t))
.
The reason I want this behaviour is that I'm looking for the path traced out along the surface of f
over "time" by the pair (t, u(t))
.
同样令人惊讶的是,np.diag(f(t, u(t)))
与 np.array([f(ti, u(ti))] 对于 ti 不同在 t])
中,所以我不清楚如何从通过 interp2d
返回的内容获得路径 f(t, u(t))
.
It is also surprising that np.diag(f(t, u(t)))
differs from np.array([f(ti, u(ti)) for ti in t])
, so It's not clear to me how to get at the path f(t, u(t))
from what is returned via interp2d
.
关于 diag
,我只是觉得我们应该有 np.diag(f(t, u(t))) == np.array([f(ti), u(ti)) for ti in t])
,但事实并非如此.
About diag
, I just thought it seemed we should have np.diag(f(t, u(t))) == np.array([f(ti, u(ti)) for ti in t])
, but that isn't the case.
完整示例:
def f(t, u):
return (t**2) * np.exp((u**2) / (1 + u**2))
x = np.linspace(0, 1, 250)
xx, yy = np.meshgrid(x, x)
z = f(xx, yy)
f = scipy.interpolate.interp2d(x, y, z)
print(f(x, y))
print(np.array([f(xi, yi)[0] for xi, yi in zip(x, y)]))
我希望两个 print
语句的输出相同.
I would like the output of both print
statements to be the same.
推荐答案
interp2d
方法返回一个对象,该对象的调用方法期望 x、y 向量是矩形网格的坐标.您没有从返回数组的对角线上获得所需值的原因是它首先对 x, y 进行排序.
The method interp2d
returns an object whose call method expects the x, y vectors to be coordinates of a rectangular grid. And the reason you don't get the desired values from the diagonal of the returned array is that it sorts x, y first.
但是有一种解决方法,我也在在 B 样条基础上查询二元样条上的多个点 中使用了它.执行后
But there is a workaround, which I also used in Querying multiple points on a bivariate spline in the B-spline basis. After executing
import scipy.interpolate as si
f = si.interp2d(x, y, z)
评估 f 不是通过调用它,而是通过传递它的 tck
属性,然后是你的 x、y 坐标,到内部的 bispeu
方法.像这样:
evaluate f not by calling it, but by passing its tck
properties, followed by your x, y coordinates, to internal bispeu
method. Like this:
print(si.dfitpack.bispeu(f.tck[0], f.tck[1], f.tck[2], f.tck[3], f.tck[4], x, y)[0])
以上返回与慢循环相同
print(np.array([f(xi, yi)[0] for xi, yi in zip(x, y)]))
说明
对象 f
秘密地是 1 阶 B 样条.样条参数(节点、系数、阶数)包含在其 tck
属性中,可以使用直接通过低阶例程达到预期的效果.
Explanation
The object f
is secretly a B-spline of order 1. The spline parameters (knots, coefficients, order) are contained in its tck
property and can be used directly by lower-order routines to the desired effect.
(理想情况下,f
的调用方法将有一个布尔参数 grid
,我们将其设置为 False 以使其知道我们不需要网格评估.唉,它没有实现.)
(Ideally, the call method of f
would have a Boolean parameter grid
which we'd set to False to let it know we don't want grid evaluation. Alas, it's not implemented.)
这篇关于沿曲线评估 scipy 2D 插值的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!