python双线性插值函数_如何在Python中实现双线性插值

这里有一个可重用的函数。它包括文档测试和数据验证:def bilinear_interpolation(x, y, points):

'''Interpolate (x,y) from values associated with four points.

The four points are a list of four triplets: (x, y, value).

The four points can be in any order. They should form a rectangle.

>>> bilinear_interpolation(12, 5.5,

... [(10, 4, 100),

... (20, 4, 200),

... (10, 6, 150),

... (20, 6, 300)])

165.0

'''

# See formula at: http://en.wikipedia.org/wiki/Bilinear_interpolation

points = sorted(points) # order points by x, then by y

(x1, y1, q11), (_x1, y2, q12), (x2, _y1, q21), (_x2, _y2, q22) = points

if x1 != _x1 or x2 != _x2 or y1 != _y1 or y2 != _y2:

raise ValueError('points do not form a rectangle')

if not x1 <= x <= x2 or not y1 <= y <= y2:

raise ValueError('(x, y) not within the rectangle')

return (q11 * (x2 - x) * (y2 - y) +

q21 * (x - x1) * (y2 - y) +

q12 * (x2 - x) * (y - y1) +

q22 * (x - x1) * (y - y1)

) / ((x2 - x1) * (y2 - y1) + 0.0)

您可以通过添加以下内容来运行测试代码:if __name__ == '__main__':

import doctest

doctest.testmod()

在数据集上运行插值会产生:>>> n = [(54.5, 17.041667, 31.993),

(54.5, 17.083333, 31.911),

(54.458333, 17.041667, 31.945),

(54.458333, 17.083333, 31.866),

]

>>> bilinear_interpolation(54.4786674627, 17.0470721369, n)

31.95798688313631

你可能感兴趣的:(python双线性插值函数)