<p>灵感来自<a href="https://stackoverflow.com/a/7345691/592289">here</a>,我想出了下面的片段。API经过优化,可多次重复使用同一个表:</p>
<pre><code>from bisect import bisect_left
class BilinearInterpolation(object):
""" Bilinear interpolation. """
def __init__(self, x_index, y_index, values):
self.x_index = x_index
self.y_index = y_index
self.values = values
def __call__(self, x, y):
# local lookups
x_index, y_index, values = self.x_index, self.y_index, self.values
i = bisect_left(x_index, x) - 1
j = bisect_left(y_index, y) - 1
x1, x2 = x_index[i:i + 2]
y1, y2 = y_index[j:j + 2]
z11, z12 = values[j][i:i + 2]
z21, z22 = values[j + 1][i:i + 2]
return (z11 * (x2 - x) * (y2 - y) +
z21 * (x - x1) * (y2 - y) +
z12 * (x2 - x) * (y - y1) +
z22 * (x - x1) * (y - y1)) / ((x2 - x1) * (y2 - y1))
</code></pre>
<p>你可以这样使用它:</p>
<pre><code>table = BilinearInterpolation(
x_index=(54.458333, 54.5),
y_index=(17.041667, 17.083333),
values=((31.945, 31.866), (31.993, 31.911))
)
print(table(54.4786674627, 17.0470721369))
# 31.957986883136307
</code></pre>
<p>此版本没有错误检查,如果尝试在索引边界(或索引边界以外)使用它,则会遇到问题。有关代码的完整版本(包括错误检查和可选外推),请查看<a href="https://github.com/pmav99/interpolation" rel="nofollow noreferrer">here</a>。</p>