栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Python中执行双线性插值

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Python中执行双线性插值

这是您可以使用的可重用功能。它包括doctest和数据验证:

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/639518.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号