author Dmitriy Morozov <>
Sun, 24 Nov 2019 13:35:45 -0800
changeset 298 02e680f6d987
parent 134 c270826fd4a8
permissions -rw-r--r--
Add .build.yml

.. _triangle-zigzag-example:

Triangle zigzag example

Simple example of a filtered triangle where simplices are first inserted in a
given order, and then removed in the reverse order is in
:sfile:`examples/triangle/triangle-zigzag.cpp`. Its Python equivalent
(:sfile:`examples/triangle/`) is described next.

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

Unlike the :ref:`triangle-example`, here we use :class:`ZigzagPersistence` to
compute the pairings, and therefore need to store the internal representations
of the simplicies used by the class. These representation are stored in the
dictionary ``complex``, which maps the simplices to their representations for

The first for loop processes the simplices sorted with respect to
:func:`data_cmp`. :meth:`ZigzagPersistence.add` invoked within the loop accepts
the boundary of the newly added cell in its internal representation, which is
computed by looking up each simplex in the dictionary ``complex``:
``[complex[ss] for ss in s.boundary]``. If there is a birth, the value to be
associated with the newly created class is ``b`` (which in this case is simply a
counter).  :meth:`~ZigzagPersistence.add` returns a pair ``(i,d)``. The former
is an internal representation of the newly added cell, which we immediately
record with ``complex[s] = i``. The latter is an indicator of whether a death
has occurred, which happens iff ``d is not None``, in which case ``d`` is the
birth value passed to :meth:`~ZigzagPersistence.add` whenever the class that
just died was born. If the death occurred, then we outut the interval ``(d,

The second for loop removes simplices in the reverse order of their insertion.
:meth:`~ZigzagPersistence.remove` takes the index of the cells to be removed
(looked up in the ``complex`` dictionary: ``complex[s]``), and takes a birth
value in case a class is born. It return only a death indicator (which again is
``None`` if no death occurred).