CohomologyPersistence::add returns the dying cocycle dev
authorDmitriy Morozov <dmitriy@mrzv.org>
Thu, 07 Jan 2010 10:27:18 -0800
branchdev
changeset 187 25e468323d77
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
--- a/bindings/python/cohomology-persistence.cpp	Tue Jan 05 11:02:07 2010 -0800
+++ b/bindings/python/cohomology-persistence.cpp	Thu Jan 07 10:27:18 2010 -0800
@@ -30,20 +30,22 @@
 {
     dp::CohomPersistence::SimplexIndex      i;
     dp::CohomPersistence::Death             d;
+    dp::CohomPersistence::CocyclePtr        ccl;
 
     if (coefficients)
     {
-        boost::tie(i,d)                         = chp.add(bp::stl_input_iterator<int>(coefficients),
-                                                          bp::stl_input_iterator<dp::CohomPersistence::SimplexIndex>(bdry),
-                                                          bp::stl_input_iterator<dp::CohomPersistence::SimplexIndex>(),
-                                                          birth, store, dp::CohomPersistence::SimplexData(), image); 
+        boost::tie(i,d,ccl)                         = chp.add(bp::stl_input_iterator<int>(coefficients),
+                                                              bp::stl_input_iterator<dp::CohomPersistence::SimplexIndex>(bdry),
+                                                              bp::stl_input_iterator<dp::CohomPersistence::SimplexIndex>(),
+                                                              birth, store, dp::CohomPersistence::SimplexData(), image); 
     } else
     {
-        boost::tie(i,d)                         = chp.add(bp::stl_input_iterator<dp::CohomPersistence::SimplexIndex>(bdry),
-                                                          bp::stl_input_iterator<dp::CohomPersistence::SimplexIndex>(),
-                                                          birth, store, dp::CohomPersistence::SimplexData(), image); 
+        boost::tie(i,d,ccl)                         = chp.add(bp::stl_input_iterator<dp::CohomPersistence::SimplexIndex>(bdry),
+                                                              bp::stl_input_iterator<dp::CohomPersistence::SimplexIndex>(),
+                                                              birth, store, dp::CohomPersistence::SimplexData(), image); 
     }
 
+    // TODO: return ccl as well
     return bp::make_tuple(i,d);
 }
 
--- a/examples/alphashapes/alphashapes3d-cohomology.cpp	Tue Jan 05 11:02:07 2010 -0800
+++ b/examples/alphashapes/alphashapes3d-cohomology.cpp	Thu Jan 07 10:27:18 2010 -0800
@@ -19,6 +19,7 @@
 
 typedef     Persistence::SimplexIndex                               Index;
 typedef     Persistence::Death                                      Death;
+typedef     Persistence::CocyclePtr                                 CocyclePtr;
 
 namespace po = boost::program_options;
 
@@ -97,9 +98,9 @@
         for (AlphaSimplex3D::BoundaryIterator bcur  = s.boundary_begin(); bcur != s.boundary_end(); ++bcur)
             boundary.push_back(complex_map[*bcur]);
         
-        Index idx; Death d;
+        Index idx; Death d; CocyclePtr ccl;
         bool store = s.dimension() < 3;
-        boost::tie(idx, d)      = p.add(boundary.begin(), boundary.end(), boost::make_tuple(s.dimension(), s.value()), store);
+        boost::tie(idx, d, ccl)     = p.add(boundary.begin(), boundary.end(), boost::make_tuple(s.dimension(), s.value()), store);
         
         // c[*cur] = idx;
         if (store)
--- a/examples/cohomology/output.h	Tue Jan 05 11:02:07 2010 -0800
+++ b/examples/cohomology/output.h	Thu Jan 07 10:27:18 2010 -0800
@@ -51,13 +51,13 @@
     }
 }
 
-void output_cocycle(std::string cocycle_prefix, unsigned i, const SimplexVector& v, const Persistence::Cocycle& c, ZpField::Element prime)
+void output_cocycle(std::string cocycle_prefix, unsigned i, const SimplexVector& v, const BirthInfo& birth, const Persistence::ZColumn& zcol, ZpField::Element prime)
 {
     std::ostringstream istr; istr << '-' << i;
     std::string filename = cocycle_prefix + istr.str() + ".ccl";
     std::ofstream out(filename.c_str());
-    out << "# Cocycle born at " << c.birth.get<1>() << std::endl;
-    for (Persistence::ZColumn::const_iterator zcur = c.zcolumn.begin(); zcur != c.zcolumn.end(); ++zcur)
+    out << "# Cocycle born at " << birth.get<1>() << std::endl;
+    for (Persistence::ZColumn::const_iterator zcur = zcol.begin(); zcur != zcol.end(); ++zcur)
     {
         //const Smplx& s = **(zcur->si);
         out << (zcur->coefficient <= prime/2 ? zcur->coefficient : zcur->coefficient - prime) << " ";
--- a/examples/cohomology/rips-cohomology.cpp	Tue Jan 05 11:02:07 2010 -0800
+++ b/examples/cohomology/rips-cohomology.cpp	Thu Jan 07 10:27:18 2010 -0800
@@ -24,6 +24,7 @@
 typedef     CohomologyPersistence<Distances::DistanceType>          Persistence;
 typedef     Persistence::SimplexIndex                               Index;
 typedef     Persistence::Death                                      Death;
+typedef     Persistence::CocyclePtr                                 CocyclePtr;
 
 typedef     Rips<Distances>                                         Generator;
 typedef     Generator::Simplex                                      Smplx;
@@ -52,8 +53,8 @@
                                      bcur != cur->boundary_end();       ++bcur)
             boundary.push_back(c[*bcur]);
         
-        Index idx; Death d;
-        boost::tie(idx, d)      = p.add(boundary.begin(), boundary.end(), size(*cur));
+        Index idx; Death d; CocyclePtr ccl;
+        boost::tie(idx, d, ccl)     = p.add(boundary.begin(), boundary.end(), size(*cur));
         c[*cur] = idx;
         if (d && (size(*cur) - *d) > 0)
             std::cout << (cur->dimension() - 1) << " " << *d << " " << size(*cur) << std::endl;
--- a/examples/cohomology/rips-pairwise-cohomology.cpp	Tue Jan 05 11:02:07 2010 -0800
+++ b/examples/cohomology/rips-pairwise-cohomology.cpp	Thu Jan 07 10:27:18 2010 -0800
@@ -30,6 +30,7 @@
 typedef     CohomologyPersistence<BirthInfo, Wrapper<unsigned> >    Persistence;
 typedef     Persistence::SimplexIndex                               Index;
 typedef     Persistence::Death                                      Death;
+typedef     Persistence::CocyclePtr                                 CocyclePtr;
 
 typedef     Rips<PairDistances, Simplex<Vertex, Index> >            Generator;
 typedef     Generator::Simplex                                      Smplx;
@@ -115,9 +116,9 @@
         for (Smplx::BoundaryIterator bcur  = cur->boundary_begin(); bcur != cur->boundary_end(); ++bcur)
             boundary.push_back(map_of_v[*bcur]->data());
         
-        Index idx; Death d;
+        Index idx; Death d; CocyclePtr ccl;
         bool store = cur->dimension() < skeleton;
-        boost::tie(idx, d)      = p.add(boundary.begin(), boundary.end(), boost::make_tuple(cur->dimension(), size(*cur)), store, index_in_v[j]);
+        boost::tie(idx, d, ccl)     = p.add(boundary.begin(), boundary.end(), boost::make_tuple(cur->dimension(), size(*cur)), store, index_in_v[j]);
         
         // c[*cur] = idx;
         if (store)
@@ -160,7 +161,7 @@
         for (Persistence::Cocycles::const_iterator cur = p.begin(); cur != p.end(); ++cur)
         {
             if (cur->birth.get<0>() != 1) continue;
-            output_cocycle(cocycle_prefix, i, v, *cur, prime);
+            output_cocycle(cocycle_prefix, i, v, cur->birth, cur->zcolumn, prime);
             // std::cout << "Cocycle of dimension: " << cur->birth.get<0>() << " born at " << cur->birth.get<1>() << std::endl;
             ++i;
         }
--- a/examples/cohomology/rips-weighted-cohomology.cpp	Tue Jan 05 11:02:07 2010 -0800
+++ b/examples/cohomology/rips-weighted-cohomology.cpp	Thu Jan 07 10:27:18 2010 -0800
@@ -25,6 +25,7 @@
 typedef     CohomologyPersistence<BirthInfo, Wrapper<unsigned> >    Persistence;
 typedef     Persistence::SimplexIndex                               Index;
 typedef     Persistence::Death                                      Death;
+typedef     Persistence::CocyclePtr                                 CocyclePtr;
 
 typedef     WeightedRips<PairDistances, Simplex<Vertex, Index> >    Generator;
 typedef     Generator::Simplex                                      Smplx;
@@ -100,9 +101,9 @@
         for (Smplx::BoundaryIterator bcur  = cur->boundary_begin(); bcur != cur->boundary_end(); ++bcur)
             boundary.push_back(map_of_v[*bcur]->data());
         
-        Index idx; Death d;
+        Index idx; Death d; CocyclePtr ccl;
         bool store = cur->dimension() < skeleton;
-        boost::tie(idx, d)      = p.add(boundary.begin(), boundary.end(), boost::make_tuple(cur->dimension(), size(*cur)), store, index_in_v[j]);
+        boost::tie(idx, d, ccl)     = p.add(boundary.begin(), boundary.end(), boost::make_tuple(cur->dimension(), size(*cur)), store, index_in_v[j]);
         
         if (store)
             map_of_v[*cur]->data() = idx;
@@ -126,7 +127,7 @@
     for (Persistence::Cocycles::const_iterator cur = p.begin(); cur != p.end(); ++cur)
     {
         if (cur->birth.get<0>() != 1) continue;
-        output_cocycle(cocycle_prefix, i, v, *cur, prime);
+        output_cocycle(cocycle_prefix, i, v, cur->birth, cur->zcolumn, prime);
         // std::cout << "Cocycle of dimension: " << cur->birth.get<0>() << " born at " << cur->birth.get<1>() << std::endl;
         ++i;
     }
--- a/examples/cohomology/triangle-cohomology.cpp	Tue Jan 05 11:02:07 2010 -0800
+++ b/examples/cohomology/triangle-cohomology.cpp	Thu Jan 07 10:27:18 2010 -0800
@@ -20,6 +20,7 @@
 typedef         CohomologyPersistence<unsigned>     Persistence;
 typedef         Persistence::SimplexIndex           Index;
 typedef         Persistence::Death                  Death;
+typedef         Persistence::CocyclePtr             CocyclePtr;
 
 typedef         unsigned                            Vertex;
 typedef         Simplex<Vertex, double>             Smplx;
@@ -78,8 +79,8 @@
                                      bcur != cur->boundary_end();       ++bcur)
             boundary.push_back(c[*bcur]);
         
-        Index idx; Death d;
-        boost::tie(idx, d)      = p.add(boundary.begin(), boundary.end(), i++);
+        Index idx; Death d; CocyclePtr ccl;
+        boost::tie(idx, d, ccl)     = p.add(boundary.begin(), boundary.end(), i++);
         c[*cur] = idx;
         if (d)
             std::cout << (cur->dimension() - 1) << " " << *d << " " << (i-1) << std::endl;
--- a/include/topology/cohomology-persistence.h	Tue Jan 05 11:02:07 2010 -0800
+++ b/include/topology/cohomology-persistence.h	Thu Jan 07 10:27:18 2010 -0800
@@ -23,6 +23,9 @@
 #include <boost/intrusive/list.hpp>
 namespace bi = boost::intrusive;
 
+#include <boost/tuple/tuple.hpp>
+#include <boost/shared_ptr.hpp>
+
 
 template<class BirthInfo_, class SimplexData_ = Empty<>, class Field_ = ZpField>
 class CohomologyPersistence
@@ -55,16 +58,17 @@
         typedef             std::pair<CocycleIndex, FieldElement>                       CocycleCoefficientPair;
 
         typedef             boost::optional<BirthInfo>                                  Death;
-        typedef             std::pair<SimplexIndex, Death>                              IndexDeathPair;
+        typedef             boost::shared_ptr<ZColumn>                                  CocyclePtr;
+        typedef             boost::tuple<SimplexIndex, Death, CocyclePtr>               IndexDeathCocycle;
 
         // return either a SimplexIndex or a Death
         // BI = BoundaryIterator; it should dereference to a SimplexIndex
         template<class BI>
-        IndexDeathPair      add(BI begin, BI end, BirthInfo b, bool store = true, const SimplexData& sd = SimplexData(), bool image = true);
+        IndexDeathCocycle   add(BI begin, BI end, BirthInfo b, bool store = true, const SimplexData& sd = SimplexData(), bool image = true);
         
         // if sign needs to be specified explicitly, provide (parallel) coefficient_iter
         template<class BI, class CI>
-        IndexDeathPair      add(CI coefficient_iter, BI begin, BI end, BirthInfo b, bool store = true, const SimplexData& sd = SimplexData(), bool image = true);
+        IndexDeathCocycle   add(CI coefficient_iter, BI begin, BI end, BirthInfo b, bool store = true, const SimplexData& sd = SimplexData(), bool image = true);
 
         void                show_cocycles() const;
         CocycleIndex        begin()                                                     { return image_begin_; }
--- a/include/topology/cohomology-persistence.hpp	Tue Jan 05 11:02:07 2010 -0800
+++ b/include/topology/cohomology-persistence.hpp	Thu Jan 07 10:27:18 2010 -0800
@@ -15,6 +15,8 @@
 #include <boost/lambda/bind.hpp>
 namespace bl = boost::lambda;
 
+#include <boost/make_shared.hpp>
+
 #ifdef LOGGING
 static rlog::RLogChannel* rlCohomology =                DEF_CHANNEL("topology/cohomology",        rlog::Log_Debug);
 #endif
@@ -42,7 +44,7 @@
 
 template<class BirthInfo, class SimplexData, class Field>
 template<class BI>
-typename CohomologyPersistence<BirthInfo, SimplexData, Field>::IndexDeathPair
+typename CohomologyPersistence<BirthInfo, SimplexData, Field>::IndexDeathCocycle
 CohomologyPersistence<BirthInfo, SimplexData, Field>::
 add(BI begin, BI end, BirthInfo birth, bool store, const SimplexData& sd, bool image)
 {
@@ -56,7 +58,7 @@
 
 template<class BirthInfo, class SimplexData, class Field>
 template<class BI, class CI>
-typename CohomologyPersistence<BirthInfo, SimplexData, Field>::IndexDeathPair
+typename CohomologyPersistence<BirthInfo, SimplexData, Field>::IndexDeathCocycle
 CohomologyPersistence<BirthInfo, SimplexData, Field>::
 add(CI coefficient_iter, BI begin, BI end, BirthInfo birth, bool store, const SimplexData& sd, bool image)
 {
@@ -116,7 +118,8 @@
         if (!store)
         {
             simplices_.pop_back();
-            return std::make_pair(simplices_.end(), Death());
+            boost::shared_ptr<ZColumn> p = boost::make_shared<ZColumn>();
+            return boost::make_tuple(simplices_.end(), Death(), p);
         }
         
         signed order = 0;
@@ -150,7 +153,8 @@
         
         Count(cCohomologyElementCount);
 
-        return std::make_pair(si, Death());
+        boost::shared_ptr<ZColumn> p = boost::make_shared<ZColumn>();
+        return boost::make_tuple(si, Death(), p);
     }
 
     // Death
@@ -185,9 +189,11 @@
         cur->unlink();
     
     CountBy(cCohomologyElementCount, -z.first->zcolumn.size());
-    cocycles_.erase(candidates.front().first);
+    boost::shared_ptr<ZColumn> p = boost::make_shared<ZColumn>();
+    p->swap(z.first->zcolumn);
+    cocycles_.erase(z.first);
 
-    return std::make_pair(si, d);
+    return boost::make_tuple(si, d, p);
 }
         
 template<class BirthInfo, class SimplexData, class Field>