Added image persistence functionality to CohomologyPersistence dev
authorDmitriy Morozov <dmitriy@mrzv.org>
Wed Oct 28 14:38:36 2009 -0700 (2009-10-28)
branchdev
changeset 172a6605dc232f2
parent 171 a172b960aaaa
child 173 5fd3f43e6fbf
Added image persistence functionality to CohomologyPersistence
bindings/python/cohomology-persistence.cpp
doc/python/cohomology-persistence.rst
include/topology/cohomology-persistence.h
include/topology/cohomology-persistence.hpp
     1.1 --- a/bindings/python/cohomology-persistence.cpp	Fri Oct 23 11:51:23 2009 -0700
     1.2 +++ b/bindings/python/cohomology-persistence.cpp	Wed Oct 28 14:38:36 2009 -0700
     1.3 @@ -40,6 +40,16 @@
     1.4      return bp::make_tuple(i,d);
     1.5  }
     1.6  
     1.7 +bp::tuple                                   chp_add_store_image(dp::CohomPersistence& chp, bp::object bdry, dp::BirthID birth, bool store, bool image)
     1.8 +{
     1.9 +    dp::CohomPersistence::SimplexIndex      i;
    1.10 +    dp::CohomPersistence::Death             d;
    1.11 +    boost::tie(i,d)                                 = chp.add(bp::stl_input_iterator<dp::CohomPersistence::SimplexIndex>(bdry),
    1.12 +                                                              bp::stl_input_iterator<dp::CohomPersistence::SimplexIndex>(),
    1.13 +                                                              birth, store, dp::CohomPersistence::SimplexData(), image); 
    1.14 +    return bp::make_tuple(i,d);
    1.15 +}
    1.16 +
    1.17  dp::CohomPersistence::ZColumn::const_iterator     
    1.18  cocycle_zcolumn_begin(dp::CohomPersistence::Cocycle& ccl)                   
    1.19  { return ccl.zcolumn.begin(); }
    1.20 @@ -72,6 +82,7 @@
    1.21          .def("__init__",        bp::make_constructor(&init_from_prime))
    1.22          .def("add",             &chp_add)
    1.23          .def("add",             &chp_add_store)
    1.24 +        .def("add",             &chp_add_store_image)
    1.25          
    1.26          .def("__iter__",        bp::range(&dp::CohomPersistence::begin, &dp::CohomPersistence::end))
    1.27      ;
     2.1 --- a/doc/python/cohomology-persistence.rst	Fri Oct 23 11:51:23 2009 -0700
     2.2 +++ b/doc/python/cohomology-persistence.rst	Wed Oct 28 14:38:36 2009 -0700
     2.3 @@ -11,7 +11,7 @@
     2.4          this point on all the computation will be performed with coefficients
     2.5          coming from :math:`\mathbb{Z}/prime \mathbb{Z}`.
     2.6  
     2.7 -    .. method:: add(boundary, birth, [store = True])
     2.8 +    .. method:: add(boundary, birth, [store = True, [image = True]])
     2.9          
    2.10          Adds a simplex with the given `boundary` to the complex, i.e. 
    2.11          :math:`K_{i+1} = K_i \cup \sigma` and `boundary` = :math:`\partial \sigma`.
    2.12 @@ -23,6 +23,13 @@
    2.13          classes of the dimension equal to the maximum-dimensional simplices of
    2.14          the complex since such classes will never die.
    2.15  
    2.16 +        The `image` parameter allows one to work with a case of a space 
    2.17 +        :math:`L \subseteq K` where the filtration of :math:`K` induces a
    2.18 +        filtration of :math:`L`. In this case, one may want to compute **image
    2.19 +        persistence** (i.e. the persistence of the sequences of the images given
    2.20 +        by the inclusion of :math:`L` in :math:`K`). `image` indicates whether
    2.21 +        the simplex added belongs to :math:`L` or not.
    2.22 +
    2.23          :returns: a pair (`i`, `d`). The first element is the index `i`. 
    2.24                    It is the internal representation of the newly added simplex,
    2.25                    and should be used later for removal or when constructing the
     3.1 --- a/include/topology/cohomology-persistence.h	Fri Oct 23 11:51:23 2009 -0700
     3.2 +++ b/include/topology/cohomology-persistence.h	Wed Oct 28 14:38:36 2009 -0700
     3.3 @@ -36,7 +36,7 @@
     3.4  
     3.5  
     3.6                              CohomologyPersistence(const Field& field = Field()):
     3.7 -                                field_(field)                                           {}
     3.8 +                                field_(field), image_begin_(cocycles_.end())            {}
     3.9  
    3.10  
    3.11          // An entry in a cocycle column
    3.12 @@ -60,7 +60,7 @@
    3.13          // return either a SimplexIndex or a Death
    3.14          // BI = BoundaryIterator; it should dereference to a SimplexIndex
    3.15          template<class BI>
    3.16 -        IndexDeathPair      add(BI begin, BI end, BirthInfo b, bool store = true, const SimplexData& sd = SimplexData());
    3.17 +        IndexDeathPair      add(BI begin, BI end, BirthInfo b, bool store = true, const SimplexData& sd = SimplexData(), bool image = true);
    3.18  
    3.19          void                show_cocycles() const;
    3.20          CocycleIndex        begin()                                                     { return cocycles_.begin(); }
    3.21 @@ -72,6 +72,7 @@
    3.22      private:
    3.23          Simplices           simplices_;
    3.24          Cocycles            cocycles_;
    3.25 +        CocycleIndex        image_begin_;
    3.26          Field               field_;
    3.27  };
    3.28          
    3.29 @@ -117,7 +118,7 @@
    3.30  
    3.31      ZColumn         zcolumn;
    3.32      BirthInfo       birth;
    3.33 -    unsigned        order;
    3.34 +    signed          order;
    3.35  
    3.36      bool            operator<(const Cocycle& other) const                       { return order > other.order; }
    3.37      bool            operator==(const Cocycle& other) const                      { return order == other.order; }
     4.1 --- a/include/topology/cohomology-persistence.hpp	Fri Oct 23 11:51:23 2009 -0700
     4.2 +++ b/include/topology/cohomology-persistence.hpp	Wed Oct 28 14:38:36 2009 -0700
     4.3 @@ -1,6 +1,7 @@
     4.4  #include <boost/utility.hpp>
     4.5  #include <queue>
     4.6  #include <vector>
     4.7 +#include <limits>
     4.8  
     4.9  #include <utilities/log.h>
    4.10  #include <utilities/indirect.h>
    4.11 @@ -20,7 +21,7 @@
    4.12  template<class BI>
    4.13  typename CohomologyPersistence<BirthInfo, SimplexData, Field>::IndexDeathPair
    4.14  CohomologyPersistence<BirthInfo, SimplexData, Field>::
    4.15 -add(BI begin, BI end, BirthInfo birth, bool store, const SimplexData& sd)
    4.16 +add(BI begin, BI end, BirthInfo birth, bool store, const SimplexData& sd, bool image)
    4.17  {
    4.18      // Create simplex representation
    4.19      simplices_.push_back(SHead(sd, simplices_.empty() ? 0 : (simplices_.back().order + 1)));
    4.20 @@ -73,21 +74,40 @@
    4.21      // Birth
    4.22      if (candidates.empty())
    4.23      {
    4.24 +        // rLog(rlCohomology, "  Birth occurred");
    4.25          if (!store)
    4.26          {
    4.27              simplices_.pop_back();
    4.28              return std::make_pair(simplices_.begin(), Death());         // TODO: shouldn't return front
    4.29          }
    4.30          
    4.31 -        unsigned order = cocycles_.empty() ? 0 : cocycles_.front().order + 1;
    4.32 -        cocycles_.push_front(Cocycle(birth, order));
    4.33 +        signed order = 0;
    4.34 +        if (image)
    4.35 +            if (image_begin_ == cocycles_.end())
    4.36 +                order = std::numeric_limits<signed>::min();
    4.37 +            else
    4.38 +                order = image_begin_->order + 1;
    4.39 +        else
    4.40 +            if (!cocycles_.empty() && cocycles_.front().order >= 0)     // we have something outside the image
    4.41 +                order = cocycles_.front().order + 1;
    4.42 +
    4.43 +        CocycleIndex nw;
    4.44 +        if (image)
    4.45 +        {
    4.46 +            image_begin_ = cocycles_.insert(image_begin_, Cocycle(birth, order));
    4.47 +            nw = image_begin_;
    4.48 +        } else
    4.49 +        {
    4.50 +            cocycles_.push_front(Cocycle(birth, order));
    4.51 +            nw = cocycles_.begin();
    4.52 +        }
    4.53          
    4.54 -        rLog(rlCohomology,  "Birth: %d", cocycles_.front().order);
    4.55 +        rLog(rlCohomology,  "Birth: %d", nw->order);
    4.56  
    4.57          // set up the cocycle
    4.58 -        ZColumn& cocycle = cocycles_.front().zcolumn;
    4.59 -        cocycle.push_back(SNode(si, field_.id(), cocycles_.begin()));
    4.60 -        si->row.push_back(cocycles_.front().zcolumn.front());
    4.61 +        ZColumn& cocycle = nw->zcolumn;
    4.62 +        cocycle.push_back(SNode(si, field_.id(), nw));
    4.63 +        si->row.push_back(cocycle.front());
    4.64          rLog(rlCohomology,  "  Cocyle: %d", si->order);
    4.65  
    4.66          return std::make_pair(si, Death());
    4.67 @@ -105,6 +125,12 @@
    4.68  
    4.69      CocycleCoefficientPair& z   = candidates.front();
    4.70      Death d                     = z.first->birth;
    4.71 +    rLog(rlCohomology, "  Order: %d", z.first->order);
    4.72 +    if (z.first->order >= 0)    // if death outside image
    4.73 +        d = Death();            // no death occurs outside the image
    4.74 +    else
    4.75 +        if (z.first == image_begin_)
    4.76 +            ++image_begin_;
    4.77  
    4.78      // add z to everything else in candidates
    4.79      for (typename Candidates::iterator cur  = boost::next(candidates.begin());