examples/cohomology/cocycle.py
 author Dmitriy Morozov Mon, 10 Aug 2009 16:48:13 -0700 branch dev changeset 160 1b30e507e8aa parent 146 4e27f1f7c169 child 165 c3c3c53dfc08 permissions -rwxr-xr-x
Fixed bugs in cocycle.py introduced when rips-pairwise-cohomology.py was originally added
```
#!/usr/bin/env python

from    cvxopt          import spmatrix, matrix
from    cvxopt.blas     import copy
from    lsqr            import lsqr
from    sys             import argv, exit
import  os.path

def smooth(boundary_list, cocycle_list, vertices):
vertices = [v for v in vertices]
dimension = max((max(d, d) for d in boundary_list))
dimension += 1

# NB: D is a coboundary matrix; 1 and 2 below are transposed
D = spmatrix([d for d in boundary_list],
[d for d in boundary_list],
[d for d in boundary_list], (dimension, dimension))

z = spmatrix([zz for zz in cocycle_list],
[zz for zz in cocycle_list],
[0     for zz in cocycle_list], (dimension, 1))

v1 = D * z
# print "D^2 is zero:", not bool(D*D)
# print "D*z is zero:", not bool(v1)
z = matrix(z)

def Dfun(x,y,trans = 'N'):
if trans == 'N':
copy(D * x, y)
elif trans == 'T':
copy(D.T * x, y)
else:
assert False, "Unexpected trans parameter"

tol = 1e-10
show = False
maxit = None
solution = lsqr(Dfun, matrix(z), show = show, atol = tol, btol = tol, itnlim = maxit)

v = z - D*solution

# print sum(v**2)
# assert sum((D*v)**2) < tol and sum((D.T*v)**2) < tol, "Expected a harmonic cocycle"
if not (sum((D*v)**2) < tol and sum((D.T*v)**2) < tol):
print "Expected a harmonic cocycle:", sum((D*v)**2), sum((D.T*v)**2)

values = [None]*len(vertices)
for i in xrange(len(vertices)):
values[vertices[i]] = solution[i]
return values

list = []
with open(filename) as fp:
if line.startswith('#'): continue
list.append(map(int, line.split()))
return list

if __name__ == '__main__':
if len(argv) < 4:
print "Usage: %s BOUNDARY COCYCLE VERTEXMAP" % argv
exit()

boundary_filename = argv
cocycle_filename = argv
vertexmap_filename = argv