examples/pl-functions/cube.py
author Dmitriy Morozov <dmitriy@mrzv.org>
Sun, 03 Jun 2012 16:17:27 -0700
branchdev
changeset 253 6dc64e820477
parent 191 2d8fba6d1d58
permissions -rw-r--r--
Added adaptor for StaticCohomologyPersistence and ImagePersistence

def log2(x):
    i = 0
    while x:
        x >>= 1
        i += 1
    return i-1        

class Cube:
    def __init__(self, vertices):
        self.vertices = vertices

    def dimension(self):
        return log2(len(self.vertices))

    def boundary(self):
        for i in xrange(self.dimension()):
            for side in [0,1]:
                vertices = []
                for idx in xrange(len(self.vertices)/2):
                    # Insert i-th bit equal to side
                    v = (idx & ~(2**i-1)) << 1
                    if side: v |= 2**i
                    v |= (idx & (2**i - 1))
                    vertices.append(self.vertices[v])
                yield Cube(vertices)    

    def __hash__(self):
        return hash(tuple(self.vertices))

    def __eq__(self, other):
        return self.vertices == other.vertices

    def __repr__(self):
        return " ".join(map(str, self.vertices))


if __name__ == '__main__':
    c = Cube(['a', 'b', 'c', 'd'])
    print c
    for sb in c.boundary(): print sb