scipy.spatial.HalfspaceIntersection¶
- class scipy.spatial.HalfspaceIntersection(halfspaces, interior_point, incremental=False, qhull_options=None)¶
N维的半空间交点。
0.19.0 新版功能.
- 参数
- halfspacesndarray浮子,形状(nineq,ndim+1)
格式为Ax+b<=0的堆叠不等式 [A; b]
- interior_pointNdarray of Floor,Shape(ndim,)
在由半空格定义的区域内清晰地指向。也称为可行点,可以通过线性规划得到。
- incremental布尔值,可选
允许以增量方式添加新的半空格。这会占用一些额外的资源。
- qhull_options字符串,可选
要传递给Qhull的其他选项。有关详细信息,请参阅Qhull手册。(默认值:ndim>4为“qx”,否则为“”)选项“H”始终启用。
- 加薪
- QhullError
当Qhull遇到错误条件时引发,例如未启用要解决的选项时的几何退化。
- ValueError
如果将不兼容的数组作为输入给定,则引发。
注意事项
交点是使用 Qhull library 。这将重现Qhull的“qHalf”功能。
参考文献
- Qhull
- 1
首页--期刊主要分类--期刊细介绍--期刊题录与文摘--期刊详细文摘内容
示例
形成某些多边形的平面的半空间相交
>>> from scipy.spatial import HalfspaceIntersection >>> halfspaces = np.array([[-1, 0., 0.], ... [0., -1., 0.], ... [2., 1., -4.], ... [-0.5, 1., -2.]]) >>> feasible_point = np.array([0.5, 0.5]) >>> hs = HalfspaceIntersection(halfspaces, feasible_point)
将半空格打印为填充区域和交点:
>>> import matplotlib.pyplot as plt >>> fig = plt.figure() >>> ax = fig.add_subplot(1, 1, 1, aspect='equal') >>> xlim, ylim = (-1, 3), (-1, 3) >>> ax.set_xlim(xlim) >>> ax.set_ylim(ylim) >>> x = np.linspace(-1, 3, 100) >>> symbols = ['-', '+', 'x', '*'] >>> signs = [0, 0, -1, -1] >>> fmt = {"color": None, "edgecolor": "b", "alpha": 0.5} >>> for h, sym, sign in zip(halfspaces, symbols, signs): ... hlist = h.tolist() ... fmt["hatch"] = sym ... if h[1]== 0: ... ax.axvline(-h[2]/h[0], label='{}x+{}y+{}=0'.format(*hlist)) ... xi = np.linspace(xlim[sign], -h[2]/h[0], 100) ... ax.fill_between(xi, ylim[0], ylim[1], **fmt) ... else: ... ax.plot(x, (-h[2]-h[0]*x)/h[1], label='{}x+{}y+{}=0'.format(*hlist)) ... ax.fill_between(x, (-h[2]-h[0]*x)/h[1], ylim[sign], **fmt) >>> x, y = zip(*hs.intersections) >>> ax.plot(x, y, 'o', markersize=8)
默认情况下,qhull不提供计算内点的方法。这可以很容易地使用线性规划来计算。考虑形式的半空间 \(Ax + b \leq 0\) ,求解线性规划:
\[ \begin{align}\begin{aligned}最大\:Y\\S.T.ax+y| |A_i| |\leq-b\end{aligned}\end{align} \]使用 \(A_i\) 为A的行,即每个平面的法线。
将产生一个点x,该点在凸多面体内部最远。准确地说,它是内嵌在多面体中的半径为y的最大超球面的中心。该点称为多面体的切比雪夫中心(请参见 [1] 4.3.1,第148-149页)。Qhull输出的方程总是归一化的。
>>> from scipy.optimize import linprog >>> from matplotlib.patches import Circle >>> norm_vector = np.reshape(np.linalg.norm(halfspaces[:, :-1], axis=1), ... (halfspaces.shape[0], 1)) >>> c = np.zeros((halfspaces.shape[1],)) >>> c[-1] = -1 >>> A = np.hstack((halfspaces[:, :-1], norm_vector)) >>> b = - halfspaces[:, -1:] >>> res = linprog(c, A_ub=A, b_ub=b, bounds=(None, None)) >>> x = res.x[:-1] >>> y = res.x[-1] >>> circle = Circle(x, radius=y, alpha=0.3) >>> ax.add_patch(circle) >>> plt.legend(bbox_to_anchor=(1.6, 1.0)) >>> plt.show()
- 属性
- halfspaces双倍形状的ndarray(nineq,ndim+1)
输入半空格。
- interior_point :ndarray of floats, shape (ndim,)
输入内点。
- intersections双倍形状的ndarray(ninter,ndim)
所有半空间的交点。
- dual_points双倍形状的ndarray(nineq,ndim)
输入半空间的对偶点。
- dual_facetsINT列表列表
形成对偶凸壳的(不一定是单纯的)面的点的指数。
- dual_vertices整数的ndarray,形状(nverts,)
形成对偶凸壳顶点的半空间的指数。对于二维凸壳,顶点按逆时针顺序排列。对于其他维度,它们按输入顺序排列。
- dual_equations双倍形状的ndarray(nfacet,ndim+1)
[法线,偏移] 形成对偶面的超平面方程(请参见 Qhull documentation 有关更多信息,请参见)。
- dual_area浮动
对偶凸壳的面积
- dual_volume浮动
对偶凸壳的体积
方法:
add_halfspaces
\(半空格[, restart] )处理一组附加的新半空格。
close
\()完成增量处理。