Ben*_*min 3 python numpy delaunay matplotlib scipy
当比较scipy(0.9.0)和matplotlib(1.0.1)Delaunay三角测量程序时,我注意到一个无法解释的行为.我的点是存储在的UTM坐标numpy.array([[easting, northing], [easting, northing], [easting, northing]])
.Scipy的边缘缺少一些我的观点,而matplotlib就在那里.有没有修复,或者我做错了什么?
import scipy
import numpy
from scipy.spatial import Delaunay
import matplotlib.delaunay
def delaunay_edges(points):
d = scipy.spatial.Delaunay(points)
s = d.vertices
return numpy.vstack((s[:,:2], s[:,1:], s[:,::-2]))
def delaunay_edges_matplotlib(points):
cens, edges, tri, neig = matplotlib.delaunay.delaunay(points[:,0], points[:,1])
return edges
points = numpy.array([[500000.25, 6220000.25],[500000.5, 6220000.5],[500001.0, 6220001.0],[500002.0, 6220003.0],[500003.0, 6220005.0]])
edges1 = delaunay_edges(points)
edges2 = delaunay_edges_matplotlib(points)
numpy.unique(edges1).shape # Some points missing, presumably nearby ones
numpy.unique(edges2).shape # Includes all points
Run Code Online (Sandbox Code Playgroud)
这种行为scipy.spatial.Delaunay
可能与浮点运算的不精确性有关.
您可能知道,scipy.spatial.Delaunay
使用C qhull
库来计算Delaunay三角剖分.Qhull
在其又是执行Quickhull
算法,它是在由作者所述细节此纸(1).您也可能知道计算机中使用的浮点运算是使用IEEE 754标准执行的(例如,您可以在维基百科中阅读它).根据该标准,每个有限数最简单地由三个整数描述:s
=符号(零或一),c
=有效数(或'系数'),q
=指数.用于表示这些整数的位数因数据类型而异.因此,显而易见的是,数字轴上浮点数分布的密度不是恒定的 - 数字越大,它们的分布越松散.即使使用Google计算器也可以看到 - 您可以从3333333333333334中减去3333333333333333并获得0.这是因为3333333333333333和3333333333333334都四舍五入到相同的浮点数.
现在,了解舍入错误,我们可能想要阅读题为" 使用impresicion进行复制 "的论文(1)的第4章.在本章中,描述了一种处理舍入误差的算法:
Quickhull partitions a point and determines its horizon facets by computing
whether the point is above or below a hyperplane. We have assumed that
computations return consistent results ... With floating-point arithmetic, we
cannot prevent errors from occurring, but we can repair the damage after
processing a point. We use brute force: if adjacent facets are nonconvex, one of
the facets is merged into a neighbor. Quickhull merges the facet that minimizes
the maximum distance of a vertex to the neighbor.
Run Code Online (Sandbox Code Playgroud)
这就是可能发生的事情 - Quickhull
无法区分两个附近的点,因此它合并了两个方面,从而成功地消除了其中一个方面.例如,要消除这些错误,您可以尝试移动坐标原点:
co = points[0]
points = points - co
edges1 = delaunay_edges(points)
edges2 = delaunay_edges_matplotlib(points)
print numpy.unique(edges1)
>>> [0 1 2 3 4]
print numpy.unique(edges2)
>>> [0 1 2 3 4]
Run Code Online (Sandbox Code Playgroud)
这将计算移动到浮点数相当密集的区域.
归档时间: |
|
查看次数: |
912 次 |
最近记录: |