annotate pymf/vol.py @ 19:890cfe424f4a tip

added annotations
author mitian
date Fri, 11 Dec 2015 09:47:40 +0000
parents 26838b1f560f
children
rev   line source
mi@0 1 #!/usr/bin/python2.6
mi@0 2 #
mi@0 3 # Copyright (C) Christian Thurau, 2010.
mi@0 4 # Licensed under the GNU General Public License (GPL).
mi@0 5 # http://www.gnu.org/licenses/gpl.txt
mi@0 6 """
mi@0 7 PyMF functions for computing matrix/simplex volumes
mi@0 8
mi@0 9 cmdet(): Cayley-Menger Determinant
mi@0 10 simplex_volume(): Ordinary simplex volume
mi@0 11
mi@0 12 """
mi@0 13
mi@0 14
mi@0 15 import numpy as np
mi@0 16 try:
mi@0 17 from scipy.misc.common import factorial
mi@0 18 except:
mi@0 19 from scipy.misc import factorial
mi@0 20
mi@0 21 __all__ = ["cmdet", "simplex"]
mi@0 22
mi@0 23 def cmdet(d):
mi@0 24 # compute the CMD determinant of the euclidean distance matrix d
mi@0 25 # -> d should not be squared!
mi@0 26 D = np.ones((d.shape[0]+1,d.shape[0]+1))
mi@0 27 D[0,0] = 0.0
mi@0 28 D[1:,1:] = d**2
mi@0 29 j = np.float32(D.shape[0]-2)
mi@0 30 f1 = (-1.0)**(j+1) / ( (2**j) * ((factorial(j))**2))
mi@0 31 cmd = f1 * np.linalg.det(D)
mi@0 32 # sometimes, for very small values "cmd" might be negative ...
mi@0 33 return np.sqrt(np.abs(cmd))
mi@0 34
mi@0 35 def simplex(d):
mi@0 36 # compute the simplex volume using coordinates
mi@0 37 D = np.ones((d.shape[0]+1, d.shape[1]))
mi@0 38 D[1:,:] = d
mi@0 39 vol = np.abs(np.linalg.det(D)) / factorial(d.shape[1] - 1)
mi@0 40 return vol