CohomologyPersistence::add returns the dying cocycle dev
authorDmitriy Morozov <dmitriy@mrzv.org>
Thu Jan 07 10:27:18 2010 -0800 (2010-01-07)
branchdev
changeset 18725e468323d77
parent 186 6d81d6ae7a3b
child 188 23cb832e035d
CohomologyPersistence::add returns the dying cocycle
bindings/python/cohomology-persistence.cpp
examples/alphashapes/alphashapes3d-cohomology.cpp
examples/cohomology/output.h
examples/cohomology/rips-cohomology.cpp
examples/cohomology/rips-pairwise-cohomology.cpp
examples/cohomology/rips-weighted-cohomology.cpp
examples/cohomology/triangle-cohomology.cpp
include/topology/cohomology-persistence.h
include/topology/cohomology-persistence.hpp
     1.1 --- a/bindings/python/cohomology-persistence.cpp	Tue Jan 05 11:02:07 2010 -0800
     1.2 +++ b/bindings/python/cohomology-persistence.cpp	Thu Jan 07 10:27:18 2010 -0800
     1.3 @@ -30,20 +30,22 @@
     1.4  {
     1.5      dp::CohomPersistence::SimplexIndex      i;
     1.6      dp::CohomPersistence::Death             d;
     1.7 +    dp::CohomPersistence::CocyclePtr        ccl;
     1.8  
     1.9      if (coefficients)
    1.10      {
    1.11 -        boost::tie(i,d)                         = chp.add(bp::stl_input_iterator<int>(coefficients),
    1.12 -                                                          bp::stl_input_iterator<dp::CohomPersistence::SimplexIndex>(bdry),
    1.13 -                                                          bp::stl_input_iterator<dp::CohomPersistence::SimplexIndex>(),
    1.14 -                                                          birth, store, dp::CohomPersistence::SimplexData(), image); 
    1.15 +        boost::tie(i,d,ccl)                         = chp.add(bp::stl_input_iterator<int>(coefficients),
    1.16 +                                                              bp::stl_input_iterator<dp::CohomPersistence::SimplexIndex>(bdry),
    1.17 +                                                              bp::stl_input_iterator<dp::CohomPersistence::SimplexIndex>(),
    1.18 +                                                              birth, store, dp::CohomPersistence::SimplexData(), image); 
    1.19      } else
    1.20      {
    1.21 -        boost::tie(i,d)                         = chp.add(bp::stl_input_iterator<dp::CohomPersistence::SimplexIndex>(bdry),
    1.22 -                                                          bp::stl_input_iterator<dp::CohomPersistence::SimplexIndex>(),
    1.23 -                                                          birth, store, dp::CohomPersistence::SimplexData(), image); 
    1.24 +        boost::tie(i,d,ccl)                         = chp.add(bp::stl_input_iterator<dp::CohomPersistence::SimplexIndex>(bdry),
    1.25 +                                                              bp::stl_input_iterator<dp::CohomPersistence::SimplexIndex>(),
    1.26 +                                                              birth, store, dp::CohomPersistence::SimplexData(), image); 
    1.27      }
    1.28  
    1.29 +    // TODO: return ccl as well
    1.30      return bp::make_tuple(i,d);
    1.31  }
    1.32  
     2.1 --- a/examples/alphashapes/alphashapes3d-cohomology.cpp	Tue Jan 05 11:02:07 2010 -0800
     2.2 +++ b/examples/alphashapes/alphashapes3d-cohomology.cpp	Thu Jan 07 10:27:18 2010 -0800
     2.3 @@ -19,6 +19,7 @@
     2.4  
     2.5  typedef     Persistence::SimplexIndex                               Index;
     2.6  typedef     Persistence::Death                                      Death;
     2.7 +typedef     Persistence::CocyclePtr                                 CocyclePtr;
     2.8  
     2.9  namespace po = boost::program_options;
    2.10  
    2.11 @@ -97,9 +98,9 @@
    2.12          for (AlphaSimplex3D::BoundaryIterator bcur  = s.boundary_begin(); bcur != s.boundary_end(); ++bcur)
    2.13              boundary.push_back(complex_map[*bcur]);
    2.14          
    2.15 -        Index idx; Death d;
    2.16 +        Index idx; Death d; CocyclePtr ccl;
    2.17          bool store = s.dimension() < 3;
    2.18 -        boost::tie(idx, d)      = p.add(boundary.begin(), boundary.end(), boost::make_tuple(s.dimension(), s.value()), store);
    2.19 +        boost::tie(idx, d, ccl)     = p.add(boundary.begin(), boundary.end(), boost::make_tuple(s.dimension(), s.value()), store);
    2.20          
    2.21          // c[*cur] = idx;
    2.22          if (store)
     3.1 --- a/examples/cohomology/output.h	Tue Jan 05 11:02:07 2010 -0800
     3.2 +++ b/examples/cohomology/output.h	Thu Jan 07 10:27:18 2010 -0800
     3.3 @@ -51,13 +51,13 @@
     3.4      }
     3.5  }
     3.6  
     3.7 -void output_cocycle(std::string cocycle_prefix, unsigned i, const SimplexVector& v, const Persistence::Cocycle& c, ZpField::Element prime)
     3.8 +void output_cocycle(std::string cocycle_prefix, unsigned i, const SimplexVector& v, const BirthInfo& birth, const Persistence::ZColumn& zcol, ZpField::Element prime)
     3.9  {
    3.10      std::ostringstream istr; istr << '-' << i;
    3.11      std::string filename = cocycle_prefix + istr.str() + ".ccl";
    3.12      std::ofstream out(filename.c_str());
    3.13 -    out << "# Cocycle born at " << c.birth.get<1>() << std::endl;
    3.14 -    for (Persistence::ZColumn::const_iterator zcur = c.zcolumn.begin(); zcur != c.zcolumn.end(); ++zcur)
    3.15 +    out << "# Cocycle born at " << birth.get<1>() << std::endl;
    3.16 +    for (Persistence::ZColumn::const_iterator zcur = zcol.begin(); zcur != zcol.end(); ++zcur)
    3.17      {
    3.18          //const Smplx& s = **(zcur->si);
    3.19          out << (zcur->coefficient <= prime/2 ? zcur->coefficient : zcur->coefficient - prime) << " ";
     4.1 --- a/examples/cohomology/rips-cohomology.cpp	Tue Jan 05 11:02:07 2010 -0800
     4.2 +++ b/examples/cohomology/rips-cohomology.cpp	Thu Jan 07 10:27:18 2010 -0800
     4.3 @@ -24,6 +24,7 @@
     4.4  typedef     CohomologyPersistence<Distances::DistanceType>          Persistence;
     4.5  typedef     Persistence::SimplexIndex                               Index;
     4.6  typedef     Persistence::Death                                      Death;
     4.7 +typedef     Persistence::CocyclePtr                                 CocyclePtr;
     4.8  
     4.9  typedef     Rips<Distances>                                         Generator;
    4.10  typedef     Generator::Simplex                                      Smplx;
    4.11 @@ -52,8 +53,8 @@
    4.12                                       bcur != cur->boundary_end();       ++bcur)
    4.13              boundary.push_back(c[*bcur]);
    4.14          
    4.15 -        Index idx; Death d;
    4.16 -        boost::tie(idx, d)      = p.add(boundary.begin(), boundary.end(), size(*cur));
    4.17 +        Index idx; Death d; CocyclePtr ccl;
    4.18 +        boost::tie(idx, d, ccl)     = p.add(boundary.begin(), boundary.end(), size(*cur));
    4.19          c[*cur] = idx;
    4.20          if (d && (size(*cur) - *d) > 0)
    4.21              std::cout << (cur->dimension() - 1) << " " << *d << " " << size(*cur) << std::endl;
     5.1 --- a/examples/cohomology/rips-pairwise-cohomology.cpp	Tue Jan 05 11:02:07 2010 -0800
     5.2 +++ b/examples/cohomology/rips-pairwise-cohomology.cpp	Thu Jan 07 10:27:18 2010 -0800
     5.3 @@ -30,6 +30,7 @@
     5.4  typedef     CohomologyPersistence<BirthInfo, Wrapper<unsigned> >    Persistence;
     5.5  typedef     Persistence::SimplexIndex                               Index;
     5.6  typedef     Persistence::Death                                      Death;
     5.7 +typedef     Persistence::CocyclePtr                                 CocyclePtr;
     5.8  
     5.9  typedef     Rips<PairDistances, Simplex<Vertex, Index> >            Generator;
    5.10  typedef     Generator::Simplex                                      Smplx;
    5.11 @@ -115,9 +116,9 @@
    5.12          for (Smplx::BoundaryIterator bcur  = cur->boundary_begin(); bcur != cur->boundary_end(); ++bcur)
    5.13              boundary.push_back(map_of_v[*bcur]->data());
    5.14          
    5.15 -        Index idx; Death d;
    5.16 +        Index idx; Death d; CocyclePtr ccl;
    5.17          bool store = cur->dimension() < skeleton;
    5.18 -        boost::tie(idx, d)      = p.add(boundary.begin(), boundary.end(), boost::make_tuple(cur->dimension(), size(*cur)), store, index_in_v[j]);
    5.19 +        boost::tie(idx, d, ccl)     = p.add(boundary.begin(), boundary.end(), boost::make_tuple(cur->dimension(), size(*cur)), store, index_in_v[j]);
    5.20          
    5.21          // c[*cur] = idx;
    5.22          if (store)
    5.23 @@ -160,7 +161,7 @@
    5.24          for (Persistence::Cocycles::const_iterator cur = p.begin(); cur != p.end(); ++cur)
    5.25          {
    5.26              if (cur->birth.get<0>() != 1) continue;
    5.27 -            output_cocycle(cocycle_prefix, i, v, *cur, prime);
    5.28 +            output_cocycle(cocycle_prefix, i, v, cur->birth, cur->zcolumn, prime);
    5.29              // std::cout << "Cocycle of dimension: " << cur->birth.get<0>() << " born at " << cur->birth.get<1>() << std::endl;
    5.30              ++i;
    5.31          }
     6.1 --- a/examples/cohomology/rips-weighted-cohomology.cpp	Tue Jan 05 11:02:07 2010 -0800
     6.2 +++ b/examples/cohomology/rips-weighted-cohomology.cpp	Thu Jan 07 10:27:18 2010 -0800
     6.3 @@ -25,6 +25,7 @@
     6.4  typedef     CohomologyPersistence<BirthInfo, Wrapper<unsigned> >    Persistence;
     6.5  typedef     Persistence::SimplexIndex                               Index;
     6.6  typedef     Persistence::Death                                      Death;
     6.7 +typedef     Persistence::CocyclePtr                                 CocyclePtr;
     6.8  
     6.9  typedef     WeightedRips<PairDistances, Simplex<Vertex, Index> >    Generator;
    6.10  typedef     Generator::Simplex                                      Smplx;
    6.11 @@ -100,9 +101,9 @@
    6.12          for (Smplx::BoundaryIterator bcur  = cur->boundary_begin(); bcur != cur->boundary_end(); ++bcur)
    6.13              boundary.push_back(map_of_v[*bcur]->data());
    6.14          
    6.15 -        Index idx; Death d;
    6.16 +        Index idx; Death d; CocyclePtr ccl;
    6.17          bool store = cur->dimension() < skeleton;
    6.18 -        boost::tie(idx, d)      = p.add(boundary.begin(), boundary.end(), boost::make_tuple(cur->dimension(), size(*cur)), store, index_in_v[j]);
    6.19 +        boost::tie(idx, d, ccl)     = p.add(boundary.begin(), boundary.end(), boost::make_tuple(cur->dimension(), size(*cur)), store, index_in_v[j]);
    6.20          
    6.21          if (store)
    6.22              map_of_v[*cur]->data() = idx;
    6.23 @@ -126,7 +127,7 @@
    6.24      for (Persistence::Cocycles::const_iterator cur = p.begin(); cur != p.end(); ++cur)
    6.25      {
    6.26          if (cur->birth.get<0>() != 1) continue;
    6.27 -        output_cocycle(cocycle_prefix, i, v, *cur, prime);
    6.28 +        output_cocycle(cocycle_prefix, i, v, cur->birth, cur->zcolumn, prime);
    6.29          // std::cout << "Cocycle of dimension: " << cur->birth.get<0>() << " born at " << cur->birth.get<1>() << std::endl;
    6.30          ++i;
    6.31      }
     7.1 --- a/examples/cohomology/triangle-cohomology.cpp	Tue Jan 05 11:02:07 2010 -0800
     7.2 +++ b/examples/cohomology/triangle-cohomology.cpp	Thu Jan 07 10:27:18 2010 -0800
     7.3 @@ -20,6 +20,7 @@
     7.4  typedef         CohomologyPersistence<unsigned>     Persistence;
     7.5  typedef         Persistence::SimplexIndex           Index;
     7.6  typedef         Persistence::Death                  Death;
     7.7 +typedef         Persistence::CocyclePtr             CocyclePtr;
     7.8  
     7.9  typedef         unsigned                            Vertex;
    7.10  typedef         Simplex<Vertex, double>             Smplx;
    7.11 @@ -78,8 +79,8 @@
    7.12                                       bcur != cur->boundary_end();       ++bcur)
    7.13              boundary.push_back(c[*bcur]);
    7.14          
    7.15 -        Index idx; Death d;
    7.16 -        boost::tie(idx, d)      = p.add(boundary.begin(), boundary.end(), i++);
    7.17 +        Index idx; Death d; CocyclePtr ccl;
    7.18 +        boost::tie(idx, d, ccl)     = p.add(boundary.begin(), boundary.end(), i++);
    7.19          c[*cur] = idx;
    7.20          if (d)
    7.21              std::cout << (cur->dimension() - 1) << " " << *d << " " << (i-1) << std::endl;
     8.1 --- a/include/topology/cohomology-persistence.h	Tue Jan 05 11:02:07 2010 -0800
     8.2 +++ b/include/topology/cohomology-persistence.h	Thu Jan 07 10:27:18 2010 -0800
     8.3 @@ -23,6 +23,9 @@
     8.4  #include <boost/intrusive/list.hpp>
     8.5  namespace bi = boost::intrusive;
     8.6  
     8.7 +#include <boost/tuple/tuple.hpp>
     8.8 +#include <boost/shared_ptr.hpp>
     8.9 +
    8.10  
    8.11  template<class BirthInfo_, class SimplexData_ = Empty<>, class Field_ = ZpField>
    8.12  class CohomologyPersistence
    8.13 @@ -55,16 +58,17 @@
    8.14          typedef             std::pair<CocycleIndex, FieldElement>                       CocycleCoefficientPair;
    8.15  
    8.16          typedef             boost::optional<BirthInfo>                                  Death;
    8.17 -        typedef             std::pair<SimplexIndex, Death>                              IndexDeathPair;
    8.18 +        typedef             boost::shared_ptr<ZColumn>                                  CocyclePtr;
    8.19 +        typedef             boost::tuple<SimplexIndex, Death, CocyclePtr>               IndexDeathCocycle;
    8.20  
    8.21          // return either a SimplexIndex or a Death
    8.22          // BI = BoundaryIterator; it should dereference to a SimplexIndex
    8.23          template<class BI>
    8.24 -        IndexDeathPair      add(BI begin, BI end, BirthInfo b, bool store = true, const SimplexData& sd = SimplexData(), bool image = true);
    8.25 +        IndexDeathCocycle   add(BI begin, BI end, BirthInfo b, bool store = true, const SimplexData& sd = SimplexData(), bool image = true);
    8.26          
    8.27          // if sign needs to be specified explicitly, provide (parallel) coefficient_iter
    8.28          template<class BI, class CI>
    8.29 -        IndexDeathPair      add(CI coefficient_iter, BI begin, BI end, BirthInfo b, bool store = true, const SimplexData& sd = SimplexData(), bool image = true);
    8.30 +        IndexDeathCocycle   add(CI coefficient_iter, BI begin, BI end, BirthInfo b, bool store = true, const SimplexData& sd = SimplexData(), bool image = true);
    8.31  
    8.32          void                show_cocycles() const;
    8.33          CocycleIndex        begin()                                                     { return image_begin_; }
     9.1 --- a/include/topology/cohomology-persistence.hpp	Tue Jan 05 11:02:07 2010 -0800
     9.2 +++ b/include/topology/cohomology-persistence.hpp	Thu Jan 07 10:27:18 2010 -0800
     9.3 @@ -15,6 +15,8 @@
     9.4  #include <boost/lambda/bind.hpp>
     9.5  namespace bl = boost::lambda;
     9.6  
     9.7 +#include <boost/make_shared.hpp>
     9.8 +
     9.9  #ifdef LOGGING
    9.10  static rlog::RLogChannel* rlCohomology =                DEF_CHANNEL("topology/cohomology",        rlog::Log_Debug);
    9.11  #endif
    9.12 @@ -42,7 +44,7 @@
    9.13  
    9.14  template<class BirthInfo, class SimplexData, class Field>
    9.15  template<class BI>
    9.16 -typename CohomologyPersistence<BirthInfo, SimplexData, Field>::IndexDeathPair
    9.17 +typename CohomologyPersistence<BirthInfo, SimplexData, Field>::IndexDeathCocycle
    9.18  CohomologyPersistence<BirthInfo, SimplexData, Field>::
    9.19  add(BI begin, BI end, BirthInfo birth, bool store, const SimplexData& sd, bool image)
    9.20  {
    9.21 @@ -56,7 +58,7 @@
    9.22  
    9.23  template<class BirthInfo, class SimplexData, class Field>
    9.24  template<class BI, class CI>
    9.25 -typename CohomologyPersistence<BirthInfo, SimplexData, Field>::IndexDeathPair
    9.26 +typename CohomologyPersistence<BirthInfo, SimplexData, Field>::IndexDeathCocycle
    9.27  CohomologyPersistence<BirthInfo, SimplexData, Field>::
    9.28  add(CI coefficient_iter, BI begin, BI end, BirthInfo birth, bool store, const SimplexData& sd, bool image)
    9.29  {
    9.30 @@ -116,7 +118,8 @@
    9.31          if (!store)
    9.32          {
    9.33              simplices_.pop_back();
    9.34 -            return std::make_pair(simplices_.end(), Death());
    9.35 +            boost::shared_ptr<ZColumn> p = boost::make_shared<ZColumn>();
    9.36 +            return boost::make_tuple(simplices_.end(), Death(), p);
    9.37          }
    9.38          
    9.39          signed order = 0;
    9.40 @@ -150,7 +153,8 @@
    9.41          
    9.42          Count(cCohomologyElementCount);
    9.43  
    9.44 -        return std::make_pair(si, Death());
    9.45 +        boost::shared_ptr<ZColumn> p = boost::make_shared<ZColumn>();
    9.46 +        return boost::make_tuple(si, Death(), p);
    9.47      }
    9.48  
    9.49      // Death
    9.50 @@ -185,9 +189,11 @@
    9.51          cur->unlink();
    9.52      
    9.53      CountBy(cCohomologyElementCount, -z.first->zcolumn.size());
    9.54 -    cocycles_.erase(candidates.front().first);
    9.55 +    boost::shared_ptr<ZColumn> p = boost::make_shared<ZColumn>();
    9.56 +    p->swap(z.first->zcolumn);
    9.57 +    cocycles_.erase(z.first);
    9.58  
    9.59 -    return std::make_pair(si, d);
    9.60 +    return boost::make_tuple(si, d, p);
    9.61  }
    9.62          
    9.63  template<class BirthInfo, class SimplexData, class Field>