mi@0: #!/usr/bin/python2.6 mi@0: # mi@0: # Copyright (C) Christian Thurau, 2010. mi@0: # Licensed under the GNU General Public License (GPL). mi@0: # http://www.gnu.org/licenses/gpl.txt mi@0: """ mi@0: PyMF functions for computing matrix/simplex volumes mi@0: mi@0: cmdet(): Cayley-Menger Determinant mi@0: simplex_volume(): Ordinary simplex volume mi@0: mi@0: """ mi@0: mi@0: mi@0: import numpy as np mi@0: try: mi@0: from scipy.misc.common import factorial mi@0: except: mi@0: from scipy.misc import factorial mi@0: mi@0: __all__ = ["cmdet", "simplex"] mi@0: mi@0: def cmdet(d): mi@0: # compute the CMD determinant of the euclidean distance matrix d mi@0: # -> d should not be squared! mi@0: D = np.ones((d.shape[0]+1,d.shape[0]+1)) mi@0: D[0,0] = 0.0 mi@0: D[1:,1:] = d**2 mi@0: j = np.float32(D.shape[0]-2) mi@0: f1 = (-1.0)**(j+1) / ( (2**j) * ((factorial(j))**2)) mi@0: cmd = f1 * np.linalg.det(D) mi@0: # sometimes, for very small values "cmd" might be negative ... mi@0: return np.sqrt(np.abs(cmd)) mi@0: mi@0: def simplex(d): mi@0: # compute the simplex volume using coordinates mi@0: D = np.ones((d.shape[0]+1, d.shape[1])) mi@0: D[1:,:] = d mi@0: vol = np.abs(np.linalg.det(D)) / factorial(d.shape[1] - 1) mi@0: return vol