【问题标题】:n-volume of a n-dimensional convex polyhedronn 维凸多面体的 n 体积
【发布时间】:2019-12-03 17:53:48
【问题描述】:

我希望找到 n 维凸多面体的 n 体积(2D 多面体的面积,3D 多面体的体积等)。是否有可以用python(甚至python包)编写的“通用”公式(或算法)?

编辑 - 1: 为了回答 norok2 的评论,多面体被定义为一组有序的顶点。

【问题讨论】:

  • 你的多面体是如何定义的?
  • 多面体被定义为一组有序的顶点。
  • 这样做的一种方法是找到(dis)方程形式的定界超平面,然后生成包含多面体的超立方体的随机样本,并将每个样本分类为多面体内部或外部,in/ out ratio乘以超立方体体积将为您提供一个很好的体积近似值。
  • 快速搜索monte carlo volume computation给出,例如sciencedirect.com/science/article/pii/S1877050911002092
  • 审查确切方法:citeseerx.ist.psu.edu/viewdoc/…

标签: python-2.7 area convex-hull


【解决方案1】:

使用 Python 包 polytope,可以按如下方式计算多面体的体积:

"""How to compute a polytope's volume."""
import numpy as np
import polytope

# constructing a convex polytope and computing its volume
vertices = np.array([[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]])
hull = polytope.qhull(vertices)
    # `hull` is an instance of the class `polytope.polytope.Polytope`
    # which is for representing a convex polytope
print(hull.volume)

# constructing a nonconvex polytope and computing its volume
vertices_1 = np.array([[0.0, 0.0], [0.0, 1.0], [2.0, 1.0]])
vertices_2 = np.array([[0.0, 1.0], [1.0, 1.0], [1.0, 2.0], [0.0, 2.0]])
hull_1 = polytope.qhull(vertices_1)  # convex hull of vertices in `vertices_1`
hull_2 = polytope.qhull(vertices_2)  # convex hull of vertices in `vertices_2`
nonconvex = hull_1.union(hull_2)  # construct union of convex polytopes
    # `nonconvex` is an instance of the class `polytope.polytope.Region`
    # which is for representing any polytope, including nonconvex ones,
    # and in this case can also be constructed with
    # `polytope.polytope.Region([hull_1, hull_2])`
print(nonconvex.volume)

polytope 使用随机算法计算体积,在函数polytope.polytope.volume 中实现。上面的代码还使用了类numpy.ndarraypolytope.polytope.Polytopepolytope.polytope.Region,以及函数numpy.arraypolytope.polytope.qhull和(间接)polytope.polytope.union

以上 Python 代码使用 polytope 0.2.3 版。

可以使用pipPyPI安装包polytope

pip install polytope

【讨论】:

    猜你喜欢
    • 2017-12-04
    • 1970-01-01
    • 2015-02-07
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    • 2018-05-07
    • 2015-03-09
    • 2023-03-26
    相关资源
    最近更新 更多