# Triangle example¶

Simple example of a filtered triangle is given in `examples/triangle/triangle.cpp`. Its equivalent in Python appears in `examples/triangle/triangle.py`, and we describe it next.

```from dionysus import Simplex, Filtration, StaticPersistence, \
vertex_cmp, data_cmp, data_dim_cmp \

complex = [Simplex((0,),        0),                 # A
Simplex((1,),        1),                 # B
Simplex((2,),        2),                 # C
Simplex((0,1),       2.5),               # AB
Simplex((1,2),       2.9),               # BC
Simplex((0,2),       3.5),               # CA
Simplex((0,1,2),     5)]                 # ABC

print "Complex:", complex
print "Vertex: ", sorted(complex, vertex_cmp)
print "Data:   ", sorted(complex, data_cmp)

f = Filtration(complex, data_cmp)
print "Complex in the filtration order:", ', '.join((str(s) for s in f))

p = StaticPersistence(f)
print "Persistence initialized"
p.pair_simplices(True)
print "Simplices paired"

smap = p.make_simplex_map(f)
for i in p:
print i.sign(), i.pair().sign()
print "%s (%d) - %s (%d)" % (smap[i], i.sign(), smap[i.pair()], i.pair().sign())
print "Cycle (%d):" % len(i.cycle), " + ".join((str(smap[ii]) for ii in i.cycle))

print "Number of unpaired simplices:", len([i for i in p if i.unpaired()])
```

After the necessary imports, the `complex` is setup explicitly as a list of simplices. Each `Simplex` constructor takes an iterable sequence of vertices, and optionally a data value.

A filtration `f` is initialized using the `Filtration` class, which takes a list of simplices (or anything iterable) and a comparison that defines in what order the simplices should come in the filtration. In this case we use `data_cmp()`, which simply compares simplices’ `data` attributes.

`StaticPersistence` is initialized with the filtration, and its method `pair_simplices()` pairs the simplices of the filtration:

```p = StaticPersistence(f)
p.pair_simplices()
```

Subsequently, we iterate over `p` to access a representation of each simplex in the filtration order. We output each simplex, its sign, and its pair. The auxilliary `smap = p.make_simplex_map(f)` remaps the indices of `StaticPersistence` into the simplices in the filtration. Naturally, one could use this to access the `data` attribute of the simplices to output the actual persistence diagram, as is done in the Alpha shape example and the Rips complex example.