Python 从顶点开始的四面体边长(numpy)
我试图用NumPy计算四面体顶点的边长,但我能想到的最好办法是有点纠结:Python 从顶点开始的四面体边长(numpy),python,numpy,geometry,Python,Numpy,Geometry,我试图用NumPy计算四面体顶点的边长,但我能想到的最好办法是有点纠结: import numpy as np V = np.array([[ 1, 1, 1], [-1, -1, 1], [ 1, -1, -1], [-1, 1, -1]]) vertex_pair_indexes = np.array([[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]]) np.squeeze(np.linalg.norm((diff(V[vertex_p
import numpy as np
V = np.array([[ 1, 1, 1], [-1, -1, 1], [ 1, -1, -1], [-1, 1, -1]])
vertex_pair_indexes = np.array([[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]])
np.squeeze(np.linalg.norm((diff(V[vertex_pair_indexes], axis=1)), axis=2))
我可以更自然地重构它并避免调用
np.squence()
吗?除了使用itertools.compositions
生成所有顶点坐标对之外,还有其他方法吗?这里有一种方法可以简化计算:
In [42]: vertex_pair_indexes
Out[42]:
array([[0, 1],
[0, 2],
[0, 3],
[1, 2],
[1, 3],
[2, 3]])
In [43]: first = vertex_pair_indexes[:, 0]
In [44]: second = vertex_pair_indexes[:, 1]
In [45]: np.linalg.norm(V[first] - V[second], axis=-1)
Out[45]:
array([ 2.82842712, 2.82842712, 2.82842712, 2.82842712, 2.82842712,
2.82842712])
正如您所建议的,您可以使用itertools.compositions
来生成顶点对索引
,但是如果您只对四面体感兴趣,您也可以使用first=np.array([0,0,0,1,1,2])
和second=np.array([1,2,3,2,3])
来硬连接所需的顶点
或者,如果您不介意依赖scipy,可以使用:
In [70]: from scipy.spatial.distance import pdist
In [71]: pdist(V)
Out[71]:
array([ 2.82842712, 2.82842712, 2.82842712, 2.82842712, 2.82842712,
2.82842712])