gpt4 book ai didi

python - 从顶点计算四面体边长(numpy)

转载 作者:行者123 更新时间:2023-12-01 04:55:31 24 4
gpt4 key购买 nike

我正在尝试使用 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_pair_indexes], axis=1)), axis=2))

我可以更自然地重构它并避免 np.squeeze() 调用吗?除了使用 itertools.combinations 来生成所有顶点坐标对之外,还有其他方法吗?

最佳答案

这是简化计算的一种方法:

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.combinations来生成vertex_pair_indexes,但是如果您只对四面体感兴趣,您也可以硬连线所需的具有 first = np.array([0, 0, 0, 1, 1, 2])second = np.array([1, 2, 3, 2, 3, 3])

<小时/>

或者,如果您不介意对 scipy 的依赖,则可以使用 scipy.spatial.distance.pdist :

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])

关于python - 从顶点计算四面体边长(numpy),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27496093/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com