author Dmitriy Morozov <>
Thu, 14 May 2009 17:43:19 -0700
changeset 140 9851fee5a33b
parent 134 c270826fd4a8
child 181 1ee6edc17cb6
permissions -rw-r--r--
In documentation put contents into sidebar

.. _triangle-example:

Triangle example

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

.. literalinclude:: ../../examples/triangle/
   :language: python

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

The complex must be sorted lexicographically to compute persistence using
:class:`StaticPersistence`, and it is accomplished via a helper comparison function :func:`vertex_cmp`, which compares simplices with respect to the lexicographic ordering::


A filtration ``f`` is initialized using the :class:`Filtration` class, which
takes a list of lexicographically sorted simplices and a comparison that defines
in what order the simplices should come in the filtration. In this case we use
:func:`data_cmp`, which simply compares simplices' :attr:``

:class:`StaticPersistence` is initialized with the filtration, and its method
:meth:`~StaticPersistence.pair_simplices` pairs the simplices of the

    p = StaticPersistence(f)

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. Note
``complex[f[p(i)]]``: ``p(i)`` gives the integer index of the iterator ``i`` in
the filtration ``f``; in turn ``f[p(i)]`` gives the index of the simplex in the
(lexicographically ordered) ``complex``. So the entire expression returns
individual simplices. Naturally, one could use this to access the
:attr:`` attribute of the simplices to output the actual
persistence diagram, as is done in the :ref:`alpha-shape-example` and the