Added arbitrary coefficients in boundaries (to C++ and Python) + cleaned up CohomologyPersistence.add overloading in Python
3 from dionysus import Simplex, CohomologyPersistence, points_file, PairwiseDistances, ExplicitDistances, Rips, data_dim_cmp
4 from sys import argv, exit
7 def main(filename, skeleton, max, prime = 11):
8 points = [p for p in points_file(filename)]
9 print '#', time.asctime(), "Points read"
10 distances = PairwiseDistances(points)
11 distances = ExplicitDistances(distances) # speeds up generation of the Rips complex at the expense of memory usage
12 rips = Rips(distances)
13 print '#', time.asctime(), "Rips initialized"
16 rips.generate(skeleton, max, simplices.append)
17 print '#', time.asctime(), "Generated complex: %d simplices" % len(simplices)
19 # While this step is unnecessary (Filtration below can be passed rips.cmp),
20 # it greatly speeds up the running times
21 for s in simplices: s.data = rips.eval(s)
22 print '#', time.asctime(), simplices[0], '...', simplices[-1]
24 simplices.sort(data_dim_cmp)
25 print '#', time.asctime(), "Simplices sorted"
27 ch = CohomologyPersistence(prime)
31 i,d = ch.add([complex[sb] for sb in s.boundary], (s.dimension(), s.data), store = (s.dimension() < skeleton))
35 print dimension, birth, s.data
39 dimension, birth = ccl.birth
40 if dimension >= skeleton: continue
41 print dimension, birth, 'inf' # dimension, simplex data = birth
42 print "# Cocycle at (dim=%d, birth=%f)" % ccl.birth
44 print "# ", e.si.order, normalized(e.coefficient, prime)
46 def normalized(coefficient, prime):
47 if coefficient > prime/2:
48 return coefficient - prime
51 if __name__ == '__main__':
53 print "Usage: %s POINTS SKELETON MAX [PRIME=11]" % argv[0]
57 skeleton = int(argv[2])
59 prime = (len(argv) > 4 and argv[4]) or 11
61 main(filename, skeleton, max, prime)