def get_vol(simplex):
# Compute the volume via the Cayley-Menger determinant
# <http://mathworld.wolfram.com/Cayley-MengerDeterminant.html>. One
# advantage is that it can compute the volume of the simplex indenpendent
# of the dimension of the space where it's embedded.
# compute all edge lengths
edges = numpy.subtract(simplex[:, None], simplex[None, :])
ei_dot_ej = numpy.einsum('...k,...k->...', edges, edges)
j = simplex.shape[0] - 1
a = numpy.empty((j+2, j+2) + ei_dot_ej.shape[2:])
a[1:, 1:] = ei_dot_ej
a[0, 1:] = 1.0
a[1:, 0] = 1.0
a[0, 0] = 0.0
a = numpy.moveaxis(a, (0, 1), (-2, -1))
det = numpy.linalg.det(a)
vol = numpy.sqrt((-1.0)**(j+1) / 2**j / math.factorial(j)**2 * det)
return vol
评论列表
文章目录