Persistent_cohomology_interface.h
1 /* This file is part of the Gudhi Library. The Gudhi library
2  * (Geometric Understanding in Higher Dimensions) is a generic C++
3  * library for computational topology.
4  *
5  * Author(s): Vincent Rouvreau
6  *
7  * Copyright (C) 2016 Inria
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef INCLUDE_PERSISTENT_COHOMOLOGY_INTERFACE_H_
24 #define INCLUDE_PERSISTENT_COHOMOLOGY_INTERFACE_H_
25 
26 #include <gudhi/Persistent_cohomology.h>
27 
28 #include <vector>
29 #include <utility> // for std::pair
30 #include <algorithm> // for sort
31 
32 namespace Gudhi {
33 
34 template<class FilteredComplex>
35 class Persistent_cohomology_interface : public
36 persistent_cohomology::Persistent_cohomology<FilteredComplex, persistent_cohomology::Field_Zp> {
37  private:
38  /*
39  * Compare two intervals by dimension, then by length.
40  */
41  struct cmp_intervals_by_dim_then_length {
42  explicit cmp_intervals_by_dim_then_length(FilteredComplex * sc)
43  : sc_(sc) { }
44 
45  template<typename Persistent_interval>
46  bool operator()(const Persistent_interval & p1, const Persistent_interval & p2) {
47  if (sc_->dimension(get < 0 > (p1)) == sc_->dimension(get < 0 > (p2)))
48  return (sc_->filtration(get < 1 > (p1)) - sc_->filtration(get < 0 > (p1))
49  > sc_->filtration(get < 1 > (p2)) - sc_->filtration(get < 0 > (p2)));
50  else
51  return (sc_->dimension(get < 0 > (p1)) > sc_->dimension(get < 0 > (p2)));
52  }
53  FilteredComplex* sc_;
54  };
55 
56  public:
57  Persistent_cohomology_interface(FilteredComplex* stptr)
58  : persistent_cohomology::Persistent_cohomology<FilteredComplex, persistent_cohomology::Field_Zp>(*stptr),
59  stptr_(stptr) { }
60 
61  Persistent_cohomology_interface(FilteredComplex* stptr, bool persistence_dim_max)
62  : persistent_cohomology::Persistent_cohomology<FilteredComplex,
63  persistent_cohomology::Field_Zp>(*stptr, persistence_dim_max),
64  stptr_(stptr) { }
65 
66  std::vector<std::pair<int, std::pair<double, double>>> get_persistence(int homology_coeff_field,
67  double min_persistence) {
68  persistent_cohomology::Persistent_cohomology<FilteredComplex,
69  persistent_cohomology::Field_Zp>::init_coefficients(homology_coeff_field);
70  persistent_cohomology::Persistent_cohomology<FilteredComplex,
71  persistent_cohomology::Field_Zp>::compute_persistent_cohomology(min_persistence);
72 
73  // Custom sort and output persistence
74  cmp_intervals_by_dim_then_length cmp(stptr_);
75  auto persistent_pairs = persistent_cohomology::Persistent_cohomology<FilteredComplex,
76  persistent_cohomology::Field_Zp>::get_persistent_pairs();
77  std::sort(std::begin(persistent_pairs), std::end(persistent_pairs), cmp);
78 
79  std::vector<std::pair<int, std::pair<double, double>>> persistence;
80  for (auto pair : persistent_pairs) {
81  persistence.push_back(std::make_pair(stptr_->dimension(get<0>(pair)),
82  std::make_pair(stptr_->filtration(get<0>(pair)),
83  stptr_->filtration(get<1>(pair)))));
84  }
85  return persistence;
86  }
87 
88  std::vector<std::pair<std::vector<int>, std::vector<int>>> persistence_pairs() {
89  auto pairs = persistent_cohomology::Persistent_cohomology<FilteredComplex,
90  persistent_cohomology::Field_Zp>::get_persistent_pairs();
91 
92  std::vector<std::pair<std::vector<int>, std::vector<int>>> persistence_pairs;
93  persistence_pairs.reserve(pairs.size());
94  for (auto pair : pairs) {
95  std::vector<int> birth;
96  if (get<0>(pair) != stptr_->null_simplex()) {
97  for (auto vertex : stptr_->simplex_vertex_range(get<0>(pair))) {
98  birth.push_back(vertex);
99  }
100  }
101 
102  std::vector<int> death;
103  if (get<1>(pair) != stptr_->null_simplex()) {
104  for (auto vertex : stptr_->simplex_vertex_range(get<1>(pair))) {
105  death.push_back(vertex);
106  }
107  }
108 
109  persistence_pairs.push_back(std::make_pair(birth, death));
110  }
111  return persistence_pairs;
112  }
113 
114  private:
115  // A copy
116  FilteredComplex* stptr_;
117 };
118 
119 } // namespace Gudhi
120 
121 #endif // INCLUDE_PERSISTENT_COHOMOLOGY_INTERFACE_H_
Computes the persistent cohomology of a filtered complex.
Definition: Persistent_cohomology.h:64
Definition: SimplicialComplexForAlpha.h:26
Structure representing the coefficient field .
Definition: Field_Zp.h:38
The concept FilteredComplex describes the requirements for a type to implement a filtered cell comple...
Definition: FilteredComplex.h:28
GUDHI  Version 2.3.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : GPL v3 Generated on Fri Oct 18 2019 18:40:54 for GUDHI by Doxygen 1.8.13