multipers 2.2.3__cp310-cp310-win_amd64.whl → 2.3.1__cp310-cp310-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of multipers might be problematic. Click here for more details.

Files changed (182) hide show
  1. multipers/__init__.py +33 -31
  2. multipers/_signed_measure_meta.py +430 -430
  3. multipers/_slicer_meta.py +211 -212
  4. multipers/data/MOL2.py +458 -458
  5. multipers/data/UCR.py +18 -18
  6. multipers/data/graphs.py +466 -466
  7. multipers/data/immuno_regions.py +27 -27
  8. multipers/data/pytorch2simplextree.py +90 -90
  9. multipers/data/shape3d.py +101 -101
  10. multipers/data/synthetic.py +113 -111
  11. multipers/distances.py +198 -198
  12. multipers/filtration_conversions.pxd.tp +84 -84
  13. multipers/filtrations/__init__.py +18 -0
  14. multipers/{ml/convolutions.py → filtrations/density.py} +563 -520
  15. multipers/filtrations/filtrations.py +289 -0
  16. multipers/filtrations.pxd +224 -224
  17. multipers/function_rips.cp310-win_amd64.pyd +0 -0
  18. multipers/function_rips.pyx +105 -105
  19. multipers/grids.cp310-win_amd64.pyd +0 -0
  20. multipers/grids.pyx +350 -350
  21. multipers/gudhi/Persistence_slices_interface.h +132 -132
  22. multipers/gudhi/Simplex_tree_interface.h +239 -245
  23. multipers/gudhi/Simplex_tree_multi_interface.h +516 -561
  24. multipers/gudhi/cubical_to_boundary.h +59 -59
  25. multipers/gudhi/gudhi/Bitmap_cubical_complex.h +450 -450
  26. multipers/gudhi/gudhi/Bitmap_cubical_complex_base.h +1070 -1070
  27. multipers/gudhi/gudhi/Bitmap_cubical_complex_periodic_boundary_conditions_base.h +579 -579
  28. multipers/gudhi/gudhi/Debug_utils.h +45 -45
  29. multipers/gudhi/gudhi/Fields/Multi_field.h +484 -484
  30. multipers/gudhi/gudhi/Fields/Multi_field_operators.h +455 -455
  31. multipers/gudhi/gudhi/Fields/Multi_field_shared.h +450 -450
  32. multipers/gudhi/gudhi/Fields/Multi_field_small.h +531 -531
  33. multipers/gudhi/gudhi/Fields/Multi_field_small_operators.h +507 -507
  34. multipers/gudhi/gudhi/Fields/Multi_field_small_shared.h +531 -531
  35. multipers/gudhi/gudhi/Fields/Z2_field.h +355 -355
  36. multipers/gudhi/gudhi/Fields/Z2_field_operators.h +376 -376
  37. multipers/gudhi/gudhi/Fields/Zp_field.h +420 -420
  38. multipers/gudhi/gudhi/Fields/Zp_field_operators.h +400 -400
  39. multipers/gudhi/gudhi/Fields/Zp_field_shared.h +418 -418
  40. multipers/gudhi/gudhi/Flag_complex_edge_collapser.h +337 -337
  41. multipers/gudhi/gudhi/Matrix.h +2107 -2107
  42. multipers/gudhi/gudhi/Multi_critical_filtration.h +1038 -1038
  43. multipers/gudhi/gudhi/Multi_persistence/Box.h +171 -171
  44. multipers/gudhi/gudhi/Multi_persistence/Line.h +282 -282
  45. multipers/gudhi/gudhi/Off_reader.h +173 -173
  46. multipers/gudhi/gudhi/One_critical_filtration.h +1433 -1431
  47. multipers/gudhi/gudhi/Persistence_matrix/Base_matrix.h +769 -769
  48. multipers/gudhi/gudhi/Persistence_matrix/Base_matrix_with_column_compression.h +686 -686
  49. multipers/gudhi/gudhi/Persistence_matrix/Boundary_matrix.h +842 -842
  50. multipers/gudhi/gudhi/Persistence_matrix/Chain_matrix.h +1350 -1350
  51. multipers/gudhi/gudhi/Persistence_matrix/Id_to_index_overlay.h +1105 -1105
  52. multipers/gudhi/gudhi/Persistence_matrix/Position_to_index_overlay.h +859 -859
  53. multipers/gudhi/gudhi/Persistence_matrix/RU_matrix.h +910 -910
  54. multipers/gudhi/gudhi/Persistence_matrix/allocators/entry_constructors.h +139 -139
  55. multipers/gudhi/gudhi/Persistence_matrix/base_pairing.h +230 -230
  56. multipers/gudhi/gudhi/Persistence_matrix/base_swap.h +211 -211
  57. multipers/gudhi/gudhi/Persistence_matrix/boundary_cell_position_to_id_mapper.h +60 -60
  58. multipers/gudhi/gudhi/Persistence_matrix/boundary_face_position_to_id_mapper.h +60 -60
  59. multipers/gudhi/gudhi/Persistence_matrix/chain_pairing.h +136 -136
  60. multipers/gudhi/gudhi/Persistence_matrix/chain_rep_cycles.h +190 -190
  61. multipers/gudhi/gudhi/Persistence_matrix/chain_vine_swap.h +616 -616
  62. multipers/gudhi/gudhi/Persistence_matrix/columns/chain_column_extra_properties.h +150 -150
  63. multipers/gudhi/gudhi/Persistence_matrix/columns/column_dimension_holder.h +106 -106
  64. multipers/gudhi/gudhi/Persistence_matrix/columns/column_utilities.h +219 -219
  65. multipers/gudhi/gudhi/Persistence_matrix/columns/entry_types.h +327 -327
  66. multipers/gudhi/gudhi/Persistence_matrix/columns/heap_column.h +1140 -1140
  67. multipers/gudhi/gudhi/Persistence_matrix/columns/intrusive_list_column.h +934 -934
  68. multipers/gudhi/gudhi/Persistence_matrix/columns/intrusive_set_column.h +934 -934
  69. multipers/gudhi/gudhi/Persistence_matrix/columns/list_column.h +980 -980
  70. multipers/gudhi/gudhi/Persistence_matrix/columns/naive_vector_column.h +1092 -1092
  71. multipers/gudhi/gudhi/Persistence_matrix/columns/row_access.h +192 -192
  72. multipers/gudhi/gudhi/Persistence_matrix/columns/set_column.h +921 -921
  73. multipers/gudhi/gudhi/Persistence_matrix/columns/small_vector_column.h +1093 -1093
  74. multipers/gudhi/gudhi/Persistence_matrix/columns/unordered_set_column.h +1012 -1012
  75. multipers/gudhi/gudhi/Persistence_matrix/columns/vector_column.h +1244 -1244
  76. multipers/gudhi/gudhi/Persistence_matrix/matrix_dimension_holders.h +186 -186
  77. multipers/gudhi/gudhi/Persistence_matrix/matrix_row_access.h +164 -164
  78. multipers/gudhi/gudhi/Persistence_matrix/ru_pairing.h +156 -156
  79. multipers/gudhi/gudhi/Persistence_matrix/ru_rep_cycles.h +376 -376
  80. multipers/gudhi/gudhi/Persistence_matrix/ru_vine_swap.h +540 -540
  81. multipers/gudhi/gudhi/Persistent_cohomology/Field_Zp.h +118 -118
  82. multipers/gudhi/gudhi/Persistent_cohomology/Multi_field.h +173 -173
  83. multipers/gudhi/gudhi/Persistent_cohomology/Persistent_cohomology_column.h +128 -128
  84. multipers/gudhi/gudhi/Persistent_cohomology.h +745 -745
  85. multipers/gudhi/gudhi/Points_off_io.h +171 -171
  86. multipers/gudhi/gudhi/Simple_object_pool.h +69 -69
  87. multipers/gudhi/gudhi/Simplex_tree/Simplex_tree_iterators.h +463 -463
  88. multipers/gudhi/gudhi/Simplex_tree/Simplex_tree_node_explicit_storage.h +83 -83
  89. multipers/gudhi/gudhi/Simplex_tree/Simplex_tree_siblings.h +106 -106
  90. multipers/gudhi/gudhi/Simplex_tree/Simplex_tree_star_simplex_iterators.h +277 -277
  91. multipers/gudhi/gudhi/Simplex_tree/hooks_simplex_base.h +62 -62
  92. multipers/gudhi/gudhi/Simplex_tree/indexing_tag.h +27 -27
  93. multipers/gudhi/gudhi/Simplex_tree/serialization_utils.h +62 -62
  94. multipers/gudhi/gudhi/Simplex_tree/simplex_tree_options.h +157 -157
  95. multipers/gudhi/gudhi/Simplex_tree.h +2794 -2794
  96. multipers/gudhi/gudhi/Simplex_tree_multi.h +152 -163
  97. multipers/gudhi/gudhi/distance_functions.h +62 -62
  98. multipers/gudhi/gudhi/graph_simplicial_complex.h +104 -104
  99. multipers/gudhi/gudhi/persistence_interval.h +253 -253
  100. multipers/gudhi/gudhi/persistence_matrix_options.h +170 -170
  101. multipers/gudhi/gudhi/reader_utils.h +367 -367
  102. multipers/gudhi/mma_interface_coh.h +256 -255
  103. multipers/gudhi/mma_interface_h0.h +223 -231
  104. multipers/gudhi/mma_interface_matrix.h +291 -282
  105. multipers/gudhi/naive_merge_tree.h +536 -575
  106. multipers/gudhi/scc_io.h +310 -289
  107. multipers/gudhi/truc.h +957 -888
  108. multipers/io.cp310-win_amd64.pyd +0 -0
  109. multipers/io.pyx +714 -711
  110. multipers/ml/accuracies.py +90 -90
  111. multipers/ml/invariants_with_persistable.py +79 -79
  112. multipers/ml/kernels.py +176 -176
  113. multipers/ml/mma.py +713 -714
  114. multipers/ml/one.py +472 -472
  115. multipers/ml/point_clouds.py +352 -346
  116. multipers/ml/signed_measures.py +1589 -1589
  117. multipers/ml/sliced_wasserstein.py +461 -461
  118. multipers/ml/tools.py +113 -113
  119. multipers/mma_structures.cp310-win_amd64.pyd +0 -0
  120. multipers/mma_structures.pxd +127 -127
  121. multipers/mma_structures.pyx +4 -8
  122. multipers/mma_structures.pyx.tp +1083 -1085
  123. multipers/multi_parameter_rank_invariant/diff_helpers.h +84 -93
  124. multipers/multi_parameter_rank_invariant/euler_characteristic.h +97 -97
  125. multipers/multi_parameter_rank_invariant/function_rips.h +322 -322
  126. multipers/multi_parameter_rank_invariant/hilbert_function.h +769 -769
  127. multipers/multi_parameter_rank_invariant/persistence_slices.h +148 -148
  128. multipers/multi_parameter_rank_invariant/rank_invariant.h +369 -369
  129. multipers/multiparameter_edge_collapse.py +41 -41
  130. multipers/multiparameter_module_approximation/approximation.h +2298 -2295
  131. multipers/multiparameter_module_approximation/combinatory.h +129 -129
  132. multipers/multiparameter_module_approximation/debug.h +107 -107
  133. multipers/multiparameter_module_approximation/format_python-cpp.h +286 -286
  134. multipers/multiparameter_module_approximation/heap_column.h +238 -238
  135. multipers/multiparameter_module_approximation/images.h +79 -79
  136. multipers/multiparameter_module_approximation/list_column.h +174 -174
  137. multipers/multiparameter_module_approximation/list_column_2.h +232 -232
  138. multipers/multiparameter_module_approximation/ru_matrix.h +347 -347
  139. multipers/multiparameter_module_approximation/set_column.h +135 -135
  140. multipers/multiparameter_module_approximation/structure_higher_dim_barcode.h +36 -36
  141. multipers/multiparameter_module_approximation/unordered_set_column.h +166 -166
  142. multipers/multiparameter_module_approximation/utilities.h +403 -419
  143. multipers/multiparameter_module_approximation/vector_column.h +223 -223
  144. multipers/multiparameter_module_approximation/vector_matrix.h +331 -331
  145. multipers/multiparameter_module_approximation/vineyards.h +464 -464
  146. multipers/multiparameter_module_approximation/vineyards_trajectories.h +649 -649
  147. multipers/multiparameter_module_approximation.cp310-win_amd64.pyd +0 -0
  148. multipers/multiparameter_module_approximation.pyx +218 -217
  149. multipers/pickle.py +90 -53
  150. multipers/plots.py +342 -334
  151. multipers/point_measure.cp310-win_amd64.pyd +0 -0
  152. multipers/point_measure.pyx +322 -320
  153. multipers/simplex_tree_multi.cp310-win_amd64.pyd +0 -0
  154. multipers/simplex_tree_multi.pxd +133 -133
  155. multipers/simplex_tree_multi.pyx +115 -48
  156. multipers/simplex_tree_multi.pyx.tp +1947 -1935
  157. multipers/slicer.cp310-win_amd64.pyd +0 -0
  158. multipers/slicer.pxd +301 -120
  159. multipers/slicer.pxd.tp +218 -214
  160. multipers/slicer.pyx +1570 -507
  161. multipers/slicer.pyx.tp +931 -914
  162. multipers/tensor/tensor.h +672 -672
  163. multipers/tensor.pxd +13 -13
  164. multipers/test.pyx +44 -44
  165. multipers/tests/__init__.py +57 -57
  166. multipers/torch/diff_grids.py +217 -217
  167. multipers/torch/rips_density.py +310 -304
  168. {multipers-2.2.3.dist-info → multipers-2.3.1.dist-info}/LICENSE +21 -21
  169. {multipers-2.2.3.dist-info → multipers-2.3.1.dist-info}/METADATA +21 -11
  170. multipers-2.3.1.dist-info/RECORD +182 -0
  171. {multipers-2.2.3.dist-info → multipers-2.3.1.dist-info}/WHEEL +1 -1
  172. multipers/tests/test_diff_helper.py +0 -73
  173. multipers/tests/test_hilbert_function.py +0 -82
  174. multipers/tests/test_mma.py +0 -83
  175. multipers/tests/test_point_clouds.py +0 -49
  176. multipers/tests/test_python-cpp_conversion.py +0 -82
  177. multipers/tests/test_signed_betti.py +0 -181
  178. multipers/tests/test_signed_measure.py +0 -89
  179. multipers/tests/test_simplextreemulti.py +0 -221
  180. multipers/tests/test_slicer.py +0 -221
  181. multipers-2.2.3.dist-info/RECORD +0 -189
  182. {multipers-2.2.3.dist-info → multipers-2.3.1.dist-info}/top_level.txt +0 -0
@@ -1,745 +1,745 @@
1
- /* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
2
- * See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
3
- * Author(s): Clément Maria
4
- *
5
- * Copyright (C) 2014 Inria
6
- *
7
- * Modification(s):
8
- * - YYYY/MM Author: Description of the modification
9
- */
10
-
11
- #ifndef PERSISTENT_COHOMOLOGY_H_
12
- #define PERSISTENT_COHOMOLOGY_H_
13
-
14
- #include <gudhi/Persistent_cohomology/Persistent_cohomology_column.h>
15
- #include <gudhi/Persistent_cohomology/Field_Zp.h>
16
- #include <gudhi/Simple_object_pool.h>
17
-
18
- #include <boost/intrusive/set.hpp>
19
- #include <boost/pending/disjoint_sets.hpp>
20
- #include <boost/intrusive/list.hpp>
21
-
22
- #include <iostream>
23
- #include <map>
24
- #include <unordered_map>
25
- #include <utility>
26
- #include <list>
27
- #include <vector>
28
- #include <set>
29
- #include <fstream> // std::ofstream
30
- #include <limits> // for numeric_limits<>
31
- #include <tuple>
32
- #include <algorithm>
33
- #include <string>
34
- #include <stdexcept> // for std::out_of_range
35
-
36
- namespace Gudhi {
37
-
38
- namespace persistent_cohomology {
39
-
40
- /** \brief Computes the persistent cohomology of a filtered complex.
41
- *
42
- * \ingroup persistent_cohomology
43
- *
44
- * The computation is implemented with a Compressed Annotation Matrix
45
- * (CAM)\cite DBLP:conf/esa/BoissonnatDM13,
46
- * and is adapted to the computation of Multi-Field Persistent Homology (MF)
47
- * \cite boissonnat:hal-00922572 .
48
- *
49
- * \implements PersistentHomology
50
- *
51
- */
52
- // TODO(CM): Memory allocation policy: classic, use a mempool, etc.
53
- template<class FilteredComplex, class CoefficientField>
54
- class Persistent_cohomology {
55
- public:
56
- // Data attached to each simplex to interface with a Property Map.
57
-
58
- /** \brief Data stored for each simplex. */
59
- typedef typename FilteredComplex::Simplex_key Simplex_key;
60
- /** \brief Handle to specify a simplex. */
61
- typedef typename FilteredComplex::Simplex_handle Simplex_handle;
62
- /** \brief Type for the value of the filtration function. */
63
- typedef typename FilteredComplex::Filtration_value Filtration_value;
64
- /** \brief Type of element of the field. */
65
- typedef typename CoefficientField::Element Arith_element;
66
- /** \brief Type for birth and death FilteredComplex::Simplex_handle.
67
- * The Arith_element field is used for the multi-field framework. */
68
- typedef std::tuple<Simplex_handle, Simplex_handle, Arith_element> Persistent_interval;
69
-
70
- private:
71
- // Compressed Annotation Matrix types:
72
- // Column type
73
- typedef Persistent_cohomology_column<Simplex_key, Arith_element> Column; // contains 1 set_hook
74
- // Cell type
75
- typedef typename Column::Cell Cell; // contains 2 list_hooks
76
- // Remark: constant_time_size must be false because base_hook_cam_h has auto_unlink link_mode
77
- typedef boost::intrusive::list<Cell,
78
- boost::intrusive::constant_time_size<false>,
79
- boost::intrusive::base_hook<base_hook_cam_h> > Hcell;
80
-
81
- typedef boost::intrusive::set<Column,
82
- boost::intrusive::constant_time_size<false> > Cam;
83
- // Sparse column type for the annotation of the boundary of an element.
84
- typedef std::vector<std::pair<Simplex_key, Arith_element> > A_ds_type;
85
-
86
- public:
87
- /** \brief Initializes the Persistent_cohomology class.
88
- *
89
- * @param[in] cpx Complex for which the persistent homology is computed.
90
- * cpx is a model of FilteredComplex
91
- *
92
- * @param[in] persistence_dim_max if true, the persistent homology for the maximal dimension in the
93
- * complex is computed. If false, it is ignored. Default is false.
94
- *
95
- * @exception std::out_of_range In case the number of simplices is more than Simplex_key type numeric limit.
96
- */
97
- explicit Persistent_cohomology(FilteredComplex& cpx, bool persistence_dim_max = false)
98
- : cpx_(&cpx),
99
- dim_max_(cpx.dimension()), // upper bound on the dimension of the simplices
100
- coeff_field_(), // initialize the field coefficient structure.
101
- num_simplices_(cpx_->num_simplices()), // num_simplices save to avoid to call thrice the function
102
- ds_rank_(num_simplices_), // union-find
103
- ds_parent_(num_simplices_), // union-find
104
- ds_repr_(num_simplices_, NULL), // union-find -> annotation vectors
105
- dsets_(ds_rank_.data(), ds_parent_.data()), // union-find
106
- cam_(), // collection of annotation vectors
107
- zero_cocycles_(), // union-find -> Simplex_key of creator for 0-homology
108
- transverse_idx_(), // key -> row
109
- persistent_pairs_(),
110
- interval_length_policy(&cpx, 0),
111
- column_pool_(), // memory pools for the CAM
112
- cell_pool_() {
113
- if (num_simplices_ > std::numeric_limits<Simplex_key>::max()) {
114
- // num_simplices must be strictly lower than the limit, because a value is reserved for null_key.
115
- throw std::out_of_range("The number of simplices is more than Simplex_key type numeric limit.");
116
- }
117
- if (persistence_dim_max) {
118
- ++dim_max_;
119
- }
120
- }
121
-
122
- ~Persistent_cohomology() {
123
- // Clean the transversal lists
124
- for (auto & transverse_ref : transverse_idx_) {
125
- // Destruct all the cells
126
- transverse_ref.second.row_->clear_and_dispose([&](Cell*p){p->~Cell();});
127
- delete transverse_ref.second.row_;
128
- }
129
- }
130
-
131
- private:
132
- struct length_interval {
133
- length_interval(FilteredComplex * cpx, Filtration_value min_length)
134
- : cpx_(cpx),
135
- min_length_(min_length) {
136
- }
137
-
138
- bool operator()(Simplex_handle sh1, Simplex_handle sh2) {
139
- return cpx_->filtration(sh2) - cpx_->filtration(sh1) > min_length_;
140
- }
141
-
142
- void set_length(Filtration_value new_length) {
143
- min_length_ = new_length;
144
- }
145
-
146
- FilteredComplex * cpx_;
147
- Filtration_value min_length_;
148
- };
149
-
150
- public:
151
- /** \brief Initializes the coefficient field.*/
152
- void init_coefficients(int charac) {
153
- coeff_field_.init(charac);
154
- }
155
- /** \brief Initializes the coefficient field for multi-field persistent homology.*/
156
- void init_coefficients(int charac_min, int charac_max) {
157
- coeff_field_.init(charac_min, charac_max);
158
- }
159
-
160
- /** \brief Compute the persistent homology of the filtered simplicial
161
- * complex.
162
- *
163
- * @param[in] min_interval_length the computation discards all intervals of length
164
- * less or equal than min_interval_length
165
- *
166
- * Assumes that the filtration provided by the simplicial complex is
167
- * valid. Undefined behavior otherwise. */
168
- void compute_persistent_cohomology(Filtration_value min_interval_length = 0,
169
- const bool is_simplicial_and_starting_from_dim_0 = true) {
170
- interval_length_policy.set_length(min_interval_length);
171
- Simplex_key idx_fil = -1;
172
- std::vector<Simplex_key> vertices; // so we can check the connected components at the end
173
- // Compute all finite intervals
174
- for (auto sh : cpx_->filtration_simplex_range()) {
175
- cpx_->assign_key(sh, ++idx_fil);
176
- dsets_.make_set(cpx_->key(sh));
177
- int dim_simplex = cpx_->dimension(sh);
178
- if (is_simplicial_and_starting_from_dim_0){
179
- switch (dim_simplex) {
180
- case 0:
181
- vertices.push_back(idx_fil);
182
- break;
183
- case 1:
184
- update_cohomology_groups_edge(sh);
185
- break;
186
- default:
187
- update_cohomology_groups(sh, dim_simplex);
188
- break;
189
- }
190
- } else {
191
- update_cohomology_groups(sh, dim_simplex);
192
- }
193
- }
194
- // Compute infinite intervals of dimension 0
195
- for (Simplex_key key : vertices) { // for all 0-dimensional simplices
196
- if (ds_parent_[key] == key // root of its tree
197
- && zero_cocycles_.find(key) == zero_cocycles_.end()) {
198
- persistent_pairs_.emplace_back(
199
- cpx_->simplex(key), cpx_->null_simplex(), coeff_field_.characteristic());
200
- }
201
- }
202
- for (auto zero_idx : zero_cocycles_) {
203
- persistent_pairs_.emplace_back(
204
- cpx_->simplex(zero_idx.second), cpx_->null_simplex(), coeff_field_.characteristic());
205
- }
206
- // Compute infinite interval of dimension > 0
207
- for (auto cocycle : transverse_idx_) {
208
- persistent_pairs_.emplace_back(
209
- cpx_->simplex(cocycle.first), cpx_->null_simplex(), cocycle.second.characteristics_);
210
- }
211
- }
212
-
213
- private:
214
- /** \brief Update the cohomology groups under the insertion of an edge.
215
- *
216
- * The 0-homology is maintained with a simple Union-Find data structure, which
217
- * explains the existence of a specific function of edge insertions. */
218
- void update_cohomology_groups_edge(Simplex_handle sigma) {
219
- Simplex_handle u, v;
220
- boost::tie(u, v) = cpx_->endpoints(sigma);
221
-
222
- Simplex_key ku = dsets_.find_set(cpx_->key(u));
223
- Simplex_key kv = dsets_.find_set(cpx_->key(v));
224
-
225
- if (ku != kv) { // Destroy a connected component
226
- dsets_.link(ku, kv);
227
- // Keys of the simplices which created the connected components containing
228
- // respectively u and v.
229
- Simplex_key idx_coc_u, idx_coc_v;
230
- auto map_it_u = zero_cocycles_.find(ku);
231
- // If the index of the cocycle representing the class is already ku.
232
- if (map_it_u == zero_cocycles_.end()) {
233
- idx_coc_u = ku;
234
- } else {
235
- idx_coc_u = map_it_u->second;
236
- }
237
-
238
- auto map_it_v = zero_cocycles_.find(kv);
239
- // If the index of the cocycle representing the class is already kv.
240
- if (map_it_v == zero_cocycles_.end()) {
241
- idx_coc_v = kv;
242
- } else {
243
- idx_coc_v = map_it_v->second;
244
- }
245
-
246
- if (cpx_->filtration(cpx_->simplex(idx_coc_u))
247
- < cpx_->filtration(cpx_->simplex(idx_coc_v))) { // Kill cocycle [idx_coc_v], which is younger.
248
- if (interval_length_policy(cpx_->simplex(idx_coc_v), sigma)) {
249
- persistent_pairs_.emplace_back(
250
- cpx_->simplex(idx_coc_v), sigma, coeff_field_.characteristic());
251
- }
252
- // Maintain the index of the 0-cocycle alive.
253
- if (kv != idx_coc_v) {
254
- zero_cocycles_.erase(map_it_v);
255
- }
256
- if (kv == dsets_.find_set(kv)) {
257
- if (ku != idx_coc_u) {
258
- zero_cocycles_.erase(map_it_u);
259
- }
260
- zero_cocycles_[kv] = idx_coc_u;
261
- }
262
- } else { // Kill cocycle [idx_coc_u], which is younger.
263
- if (interval_length_policy(cpx_->simplex(idx_coc_u), sigma)) {
264
- persistent_pairs_.emplace_back(
265
- cpx_->simplex(idx_coc_u), sigma, coeff_field_.characteristic());
266
- }
267
- // Maintain the index of the 0-cocycle alive.
268
- if (ku != idx_coc_u) {
269
- zero_cocycles_.erase(map_it_u);
270
- }
271
- if (ku == dsets_.find_set(ku)) {
272
- if (kv != idx_coc_v) {
273
- zero_cocycles_.erase(map_it_v);
274
- }
275
- zero_cocycles_[ku] = idx_coc_v;
276
- }
277
- }
278
- cpx_->assign_key(sigma, cpx_->null_key());
279
- } else if (dim_max_ > 1) { // If ku == kv, same connected component: create a 1-cocycle class.
280
- create_cocycle(sigma, coeff_field_.multiplicative_identity(), coeff_field_.characteristic());
281
- }
282
- }
283
-
284
- /*
285
- * Compute the annotation of the boundary of a simplex.
286
- */
287
- void annotation_of_the_boundary(
288
- std::map<Simplex_key, Arith_element> & map_a_ds, Simplex_handle sigma,
289
- int dim_sigma) {
290
- // traverses the boundary of sigma, keeps track of the annotation vectors,
291
- // with multiplicity. We used to sum the coefficients directly in
292
- // annotations_in_boundary by using a map, we now do it later.
293
- typedef std::pair<Column *, int> annotation_t;
294
- thread_local std::vector<annotation_t> annotations_in_boundary;
295
- annotations_in_boundary.clear();
296
- int sign = 1 - 2 * (dim_sigma % 2); // \in {-1,1} provides the sign in the
297
- // alternate sum in the boundary.
298
- Simplex_key key;
299
- Column * curr_col;
300
-
301
- for (auto sh : cpx_->boundary_simplex_range(sigma)) {
302
- key = cpx_->key(sh);
303
- if (key != cpx_->null_key()) { // A simplex with null_key is a killer, and have null annotation
304
- // Find its annotation vector
305
- curr_col = ds_repr_[dsets_.find_set(key)];
306
- if (curr_col != NULL) { // and insert it in annotations_in_boundary with multyiplicative factor "sign".
307
- annotations_in_boundary.emplace_back(curr_col, sign);
308
- }
309
- }
310
- sign = -sign;
311
- }
312
- // Place identical annotations consecutively so we can easily sum their multiplicities.
313
- std::sort(annotations_in_boundary.begin(), annotations_in_boundary.end(),
314
- [](annotation_t const& a, annotation_t const& b) { return a.first < b.first; });
315
-
316
- // Sum the annotations with multiplicity, using a map<key,coeff>
317
- // to represent a sparse vector.
318
- std::pair<typename std::map<Simplex_key, Arith_element>::iterator, bool> result_insert_a_ds;
319
-
320
- for (auto ann_it = annotations_in_boundary.begin(); ann_it != annotations_in_boundary.end(); /**/) {
321
- Column* col = ann_it->first;
322
- int mult = ann_it->second;
323
- while (++ann_it != annotations_in_boundary.end() && ann_it->first == col) {
324
- mult += ann_it->second;
325
- }
326
- // The following test is just a heuristic, it is not required, and it is fine that is misses p == 0.
327
- if (mult != coeff_field_.additive_identity()) { // For all columns in the boundary,
328
- for (auto cell_ref : col->col_) { // insert every cell in map_a_ds with multiplicity
329
- Arith_element w_y = coeff_field_.times(cell_ref.coefficient_, mult); // coefficient * multiplicity
330
-
331
- if (w_y != coeff_field_.additive_identity()) { // if != 0
332
- result_insert_a_ds = map_a_ds.insert(std::pair<Simplex_key, Arith_element>(cell_ref.key_, w_y));
333
- if (!(result_insert_a_ds.second)) { // if cell_ref.key_ already a Key in map_a_ds
334
- result_insert_a_ds.first->second = coeff_field_.plus_equal(result_insert_a_ds.first->second, w_y);
335
- if (result_insert_a_ds.first->second == coeff_field_.additive_identity()) {
336
- map_a_ds.erase(result_insert_a_ds.first);
337
- }
338
- }
339
- }
340
- }
341
- }
342
- }
343
- }
344
-
345
- /*
346
- * Update the cohomology groups under the insertion of a simplex.
347
- */
348
- void update_cohomology_groups(Simplex_handle sigma, int dim_sigma) {
349
- // Compute the annotation of the boundary of sigma:
350
- std::map<Simplex_key, Arith_element> map_a_ds;
351
- annotation_of_the_boundary(map_a_ds, sigma, dim_sigma);
352
- // Update the cohomology groups:
353
- if (map_a_ds.empty()) { // sigma is a creator in all fields represented in coeff_field_
354
- if (dim_sigma < dim_max_) {
355
- create_cocycle(sigma, coeff_field_.multiplicative_identity(),
356
- coeff_field_.characteristic());
357
- }
358
- } else { // sigma is a destructor in at least a field in coeff_field_
359
- // Convert map_a_ds to a vector
360
- A_ds_type a_ds; // admits reverse iterators
361
- for (auto map_a_ds_ref : map_a_ds) {
362
- a_ds.push_back(
363
- std::pair<Simplex_key, Arith_element>(map_a_ds_ref.first,
364
- map_a_ds_ref.second));
365
- }
366
-
367
- Arith_element inv_x, charac;
368
- Arith_element prod = coeff_field_.characteristic(); // Product of characteristic of the fields
369
- for (auto a_ds_rit = a_ds.rbegin();
370
- (a_ds_rit != a_ds.rend())
371
- && (prod != coeff_field_.multiplicative_identity()); ++a_ds_rit) {
372
- std::tie(inv_x, charac) = coeff_field_.inverse(a_ds_rit->second, prod);
373
-
374
- if (inv_x != coeff_field_.additive_identity()) {
375
- destroy_cocycle(sigma, a_ds, a_ds_rit->first, inv_x, charac);
376
- prod /= charac;
377
- }
378
- }
379
- if (prod != coeff_field_.multiplicative_identity()
380
- && dim_sigma < dim_max_) {
381
- create_cocycle(sigma, coeff_field_.multiplicative_identity(prod), prod);
382
- }
383
- }
384
- }
385
-
386
- /* \brief Create a new cocycle class.
387
- *
388
- * The class is created by the insertion of the simplex sigma.
389
- * The methods adds a cocycle, representing the new cocycle class,
390
- * to the matrix representing the cohomology groups.
391
- * The new cocycle has value 0 on every simplex except on sigma
392
- * where it worths 1.*/
393
- void create_cocycle(Simplex_handle sigma, Arith_element x,
394
- Arith_element charac) {
395
- Simplex_key key = cpx_->key(sigma);
396
- // Create a column containing only one cell,
397
- Column * new_col = column_pool_.construct(key);
398
- Cell * new_cell = cell_pool_.construct(key, x, new_col);
399
- new_col->col_.push_back(*new_cell);
400
- // and insert it in the matrix, in constant time thanks to the hint cam_.end().
401
- // Indeed *new_col has the biggest lexicographic value because key is the
402
- // biggest key used so far.
403
- cam_.insert(cam_.end(), *new_col);
404
- // Update the disjoint sets data structure.
405
- Hcell * new_hcell = new Hcell;
406
- new_hcell->push_back(*new_cell);
407
- transverse_idx_[key] = cocycle(charac, new_hcell); // insert the new row
408
- ds_repr_[key] = new_col;
409
- }
410
-
411
- /* \brief Destroy a cocycle class.
412
- *
413
- * The cocycle class is destroyed by the insertion of sigma.
414
- * The methods proceeds to a reduction of the matrix representing
415
- * the cohomology groups using Gauss pivoting. The reduction zeros-out
416
- * the row containing the cell with highest key in
417
- * a_ds, the annotation of the boundary of simplex sigma. This key
418
- * is "death_key".*/
419
- void destroy_cocycle(Simplex_handle sigma, A_ds_type const& a_ds,
420
- Simplex_key death_key, Arith_element inv_x,
421
- Arith_element charac) {
422
- // Create a finite persistent interval for which the interval exists
423
- if (interval_length_policy(cpx_->simplex(death_key), sigma)) {
424
- persistent_pairs_.emplace_back(cpx_->simplex(death_key) // creator
425
- , sigma // destructor
426
- , charac); // fields
427
- }
428
-
429
- auto death_key_row = transverse_idx_.find(death_key); // Find the beginning of the row.
430
- std::pair<typename Cam::iterator, bool> result_insert_cam;
431
-
432
- auto row_cell_it = death_key_row->second.row_->begin();
433
-
434
- while (row_cell_it != death_key_row->second.row_->end()) { // Traverse all cells in
435
- // the row at index death_key.
436
- Arith_element w = coeff_field_.times_minus(inv_x, row_cell_it->coefficient_);
437
-
438
- if (w != coeff_field_.additive_identity()) {
439
- Column * curr_col = row_cell_it->self_col_;
440
- ++row_cell_it;
441
- // Disconnect the column from the rows in the CAM.
442
- for (auto& col_cell : curr_col->col_) {
443
- col_cell.base_hook_cam_h::unlink();
444
- }
445
-
446
- // Remove the column from the CAM before modifying its value
447
- cam_.erase(cam_.iterator_to(*curr_col));
448
- // Proceed to the reduction of the column
449
- plus_equal_column(*curr_col, a_ds, w);
450
-
451
- if (curr_col->col_.empty()) { // If the column is null
452
- ds_repr_[curr_col->class_key_] = NULL;
453
- column_pool_.destroy(curr_col); // delete curr_col;
454
- } else {
455
- // Find whether the column obtained is already in the CAM
456
- result_insert_cam = cam_.insert(*curr_col);
457
- if (result_insert_cam.second) { // If it was not in the CAM before: insertion has succeeded
458
- for (auto& col_cell : curr_col->col_) {
459
- // re-establish the row links
460
- transverse_idx_[col_cell.key_].row_->push_front(col_cell);
461
- }
462
- } else { // There is already an identical column in the CAM:
463
- // merge two disjoint sets.
464
- dsets_.link(curr_col->class_key_,
465
- result_insert_cam.first->class_key_);
466
-
467
- Simplex_key key_tmp = dsets_.find_set(curr_col->class_key_);
468
- ds_repr_[key_tmp] = &(*(result_insert_cam.first));
469
- result_insert_cam.first->class_key_ = key_tmp;
470
- // intrusive containers don't own their elements, we have to release them manually
471
- curr_col->col_.clear_and_dispose([&](Cell*p){cell_pool_.destroy(p);});
472
- column_pool_.destroy(curr_col); // delete curr_col;
473
- }
474
- }
475
- } else {
476
- ++row_cell_it;
477
- } // If w == 0, pass.
478
- }
479
-
480
- // Because it is a killer simplex, set the data of sigma to null_key().
481
- if (charac == coeff_field_.characteristic()) {
482
- cpx_->assign_key(sigma, cpx_->null_key());
483
- }
484
- if (death_key_row->second.characteristics_ == charac) {
485
- delete death_key_row->second.row_;
486
- transverse_idx_.erase(death_key_row);
487
- } else {
488
- death_key_row->second.characteristics_ /= charac;
489
- }
490
- }
491
-
492
- /*
493
- * Assign: target <- target + w * other.
494
- */
495
- void plus_equal_column(Column & target, A_ds_type const& other // value_type is pair<Simplex_key,Arith_element>
496
- , Arith_element w) {
497
- auto target_it = target.col_.begin();
498
- auto other_it = other.begin();
499
- while (target_it != target.col_.end() && other_it != other.end()) {
500
- if (target_it->key_ < other_it->first) {
501
- ++target_it;
502
- } else {
503
- if (target_it->key_ > other_it->first) {
504
- Cell * cell_tmp = cell_pool_.construct(Cell(other_it->first // key
505
- , coeff_field_.additive_identity(), &target));
506
-
507
- cell_tmp->coefficient_ = coeff_field_.plus_times_equal(cell_tmp->coefficient_, other_it->second, w);
508
-
509
- target.col_.insert(target_it, *cell_tmp);
510
-
511
- ++other_it;
512
- } else { // it1->key == it2->key
513
- // target_it->coefficient_ <- target_it->coefficient_ + other_it->second * w
514
- target_it->coefficient_ = coeff_field_.plus_times_equal(target_it->coefficient_, other_it->second, w);
515
- if (target_it->coefficient_ == coeff_field_.additive_identity()) {
516
- auto tmp_it = target_it;
517
- ++target_it;
518
- ++other_it; // iterators remain valid
519
- Cell * tmp_cell_ptr = &(*tmp_it);
520
- target.col_.erase(tmp_it); // removed from column
521
-
522
- cell_pool_.destroy(tmp_cell_ptr); // delete from memory
523
- } else {
524
- ++target_it;
525
- ++other_it;
526
- }
527
- }
528
- }
529
- }
530
- while (other_it != other.end()) {
531
- Cell * cell_tmp = cell_pool_.construct(Cell(other_it->first, coeff_field_.additive_identity(), &target));
532
- cell_tmp->coefficient_ = coeff_field_.plus_times_equal(cell_tmp->coefficient_, other_it->second, w);
533
- target.col_.insert(target.col_.end(), *cell_tmp);
534
-
535
- ++other_it;
536
- }
537
- }
538
-
539
- /*
540
- * Compare two intervals by length.
541
- */
542
- struct cmp_intervals_by_length {
543
- explicit cmp_intervals_by_length(FilteredComplex * sc)
544
- : sc_(sc) {
545
- }
546
- bool operator()(const Persistent_interval & p1, const Persistent_interval & p2) {
547
- return (sc_->filtration(get < 1 > (p1)) - sc_->filtration(get < 0 > (p1))
548
- > sc_->filtration(get < 1 > (p2)) - sc_->filtration(get < 0 > (p2)));
549
- }
550
- FilteredComplex * sc_;
551
- };
552
-
553
- public:
554
- /** \brief Output the persistence diagram in ostream.
555
- *
556
- * The file format is the following:
557
- * p1*...*pr dim b d
558
- *
559
- * where "dim" is the dimension of the homological feature,
560
- * b and d are respectively the birth and death of the feature and
561
- * p1*...*pr is the product of prime numbers pi such that the homology
562
- * feature exists in homology with Z/piZ coefficients.
563
- */
564
- void output_diagram(std::ostream& ostream = std::cout) {
565
- cmp_intervals_by_length cmp(cpx_);
566
- std::sort(std::begin(persistent_pairs_), std::end(persistent_pairs_), cmp);
567
- for (auto pair : persistent_pairs_) {
568
- ostream << get<2>(pair) << " " << cpx_->dimension(get<0>(pair)) << " "
569
- << cpx_->filtration(get<0>(pair)) << " "
570
- << cpx_->filtration(get<1>(pair)) << " " << std::endl;
571
- }
572
- }
573
-
574
- void write_output_diagram(std::string diagram_name) {
575
- std::ofstream diagram_out(diagram_name.c_str());
576
- diagram_out.exceptions(diagram_out.failbit);
577
- cmp_intervals_by_length cmp(cpx_);
578
- std::sort(std::begin(persistent_pairs_), std::end(persistent_pairs_), cmp);
579
- for (auto pair : persistent_pairs_) {
580
- diagram_out << cpx_->dimension(get<0>(pair)) << " "
581
- << cpx_->filtration(get<0>(pair)) << " "
582
- << cpx_->filtration(get<1>(pair)) << std::endl;
583
- }
584
- }
585
-
586
- /** @brief Returns Betti numbers.
587
- * @return A vector of Betti numbers.
588
- */
589
- std::vector<int> betti_numbers() const {
590
- // Init Betti numbers vector with zeros until Simplicial complex dimension and don't allocate a vector of negative
591
- // size for an empty complex
592
- std::vector<int> betti_numbers(std::max(dim_max_, 0));
593
-
594
- for (auto pair : persistent_pairs_) {
595
- // Count never ended persistence intervals
596
- if (cpx_->null_simplex() == get<1>(pair)) {
597
- // Increment corresponding betti number
598
- betti_numbers[cpx_->dimension(get<0>(pair))] += 1;
599
- }
600
- }
601
- return betti_numbers;
602
- }
603
-
604
- /** @brief Returns the Betti number of the dimension passed by parameter.
605
- * @param[in] dimension The Betti number dimension to get.
606
- * @return Betti number of the given dimension
607
- *
608
- */
609
- int betti_number(int dimension) const {
610
- int betti_number = 0;
611
-
612
- for (auto pair : persistent_pairs_) {
613
- // Count never ended persistence intervals
614
- if (cpx_->null_simplex() == get<1>(pair)) {
615
- if (cpx_->dimension(get<0>(pair)) == dimension) {
616
- // Increment betti number found
617
- ++betti_number;
618
- }
619
- }
620
- }
621
- return betti_number;
622
- }
623
-
624
- /** @brief Returns the persistent Betti numbers.
625
- * @param[in] from The persistence birth limit to be added in the number \f$(persistent birth \leq from)\f$.
626
- * @param[in] to The persistence death limit to be added in the number \f$(persistent death > to)\f$.
627
- * @return A vector of persistent Betti numbers.
628
- */
629
- std::vector<int> persistent_betti_numbers(Filtration_value from, Filtration_value to) const {
630
- // Init Betti numbers vector with zeros until Simplicial complex dimension and don't allocate a vector of negative
631
- // size for an empty complex
632
- std::vector<int> betti_numbers(std::max(dim_max_, 0));
633
- for (auto pair : persistent_pairs_) {
634
- // Count persistence intervals that covers the given interval
635
- // null_simplex test : if the function is called with to=+infinity, we still get something useful. And it will
636
- // still work if we change the complex filtration function to reject null simplices.
637
- if (cpx_->filtration(get<0>(pair)) <= from &&
638
- (get<1>(pair) == cpx_->null_simplex() || cpx_->filtration(get<1>(pair)) > to)) {
639
- // Increment corresponding betti number
640
- betti_numbers[cpx_->dimension(get<0>(pair))] += 1;
641
- }
642
- }
643
- return betti_numbers;
644
- }
645
-
646
- /** @brief Returns the persistent Betti number of the dimension passed by parameter.
647
- * @param[in] dimension The Betti number dimension to get.
648
- * @param[in] from The persistence birth limit to be added in the number \f$(persistent birth \leq from)\f$.
649
- * @param[in] to The persistence death limit to be added in the number \f$(persistent death > to)\f$.
650
- * @return Persistent Betti number of the given dimension
651
- */
652
- int persistent_betti_number(int dimension, Filtration_value from, Filtration_value to) const {
653
- int betti_number = 0;
654
-
655
- for (auto pair : persistent_pairs_) {
656
- // Count persistence intervals that covers the given interval
657
- // null_simplex test : if the function is called with to=+infinity, we still get something useful. And it will
658
- // still work if we change the complex filtration function to reject null simplices.
659
- if (cpx_->filtration(get<0>(pair)) <= from &&
660
- (get<1>(pair) == cpx_->null_simplex() || cpx_->filtration(get<1>(pair)) > to)) {
661
- if (cpx_->dimension(get<0>(pair)) == dimension) {
662
- // Increment betti number found
663
- ++betti_number;
664
- }
665
- }
666
- }
667
- return betti_number;
668
- }
669
-
670
- /** @brief Returns a list of persistence birth and death FilteredComplex::Simplex_handle pairs.
671
- * @return A list of Persistent_cohomology::Persistent_interval
672
- */
673
- const std::vector<Persistent_interval>& get_persistent_pairs() const {
674
- return persistent_pairs_;
675
- }
676
-
677
- /** @brief Returns persistence intervals for a given dimension.
678
- * @param[in] dimension Dimension to get the birth and death pairs from.
679
- * @return A vector of persistence intervals (birth and death) on a fixed dimension.
680
- */
681
- std::vector< std::pair< Filtration_value , Filtration_value > >
682
- intervals_in_dimension(int dimension) {
683
- std::vector< std::pair< Filtration_value , Filtration_value > > result;
684
- // auto && pair, to avoid unnecessary copying
685
- for (auto && pair : persistent_pairs_) {
686
- if (cpx_->dimension(get<0>(pair)) == dimension) {
687
- result.emplace_back(cpx_->filtration(get<0>(pair)), cpx_->filtration(get<1>(pair)));
688
- }
689
- }
690
- return result;
691
- }
692
-
693
- private:
694
- /*
695
- * Structure representing a cocycle.
696
- */
697
- struct cocycle {
698
- cocycle()
699
- : row_(nullptr),
700
- characteristics_() {
701
- }
702
- cocycle(Arith_element characteristics, Hcell * row)
703
- : row_(row),
704
- characteristics_(characteristics) {
705
- }
706
-
707
- Hcell * row_; // points to the corresponding row in the CAM
708
- Arith_element characteristics_; // product of field characteristics for which the cocycle exist
709
- };
710
-
711
- public:
712
- FilteredComplex * cpx_;
713
- int dim_max_;
714
- CoefficientField coeff_field_;
715
- size_t num_simplices_;
716
-
717
- /* Disjoint sets data structure to link the model of FilteredComplex
718
- * with the compressed annotation matrix.
719
- * ds_rank_ is a property map Simplex_key -> int, ds_parent_ is a property map
720
- * Simplex_key -> simplex_key_t */
721
- std::vector<int> ds_rank_;
722
- std::vector<Simplex_key> ds_parent_;
723
- std::vector<Column *> ds_repr_;
724
- boost::disjoint_sets<int *, Simplex_key *> dsets_;
725
- /* The compressed annotation matrix fields.*/
726
- Cam cam_;
727
- /* Dictionary establishing the correspondence between the Simplex_key of
728
- * the root vertex in the union-find ds and the Simplex_key of the vertex which
729
- * created the connected component as a 0-dimension homology feature.*/
730
- std::unordered_map<Simplex_key, Simplex_key> zero_cocycles_;
731
- /* Key -> row. */
732
- std::map<Simplex_key, cocycle> transverse_idx_;
733
- /* Persistent intervals. */
734
- std::vector<Persistent_interval> persistent_pairs_;
735
- length_interval interval_length_policy;
736
-
737
- Simple_object_pool<Column> column_pool_;
738
- Simple_object_pool<Cell> cell_pool_;
739
- };
740
-
741
- } // namespace persistent_cohomology
742
-
743
- } // namespace Gudhi
744
-
745
- #endif // PERSISTENT_COHOMOLOGY_H_
1
+ /* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
2
+ * See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
3
+ * Author(s): Clément Maria
4
+ *
5
+ * Copyright (C) 2014 Inria
6
+ *
7
+ * Modification(s):
8
+ * - YYYY/MM Author: Description of the modification
9
+ */
10
+
11
+ #ifndef PERSISTENT_COHOMOLOGY_H_
12
+ #define PERSISTENT_COHOMOLOGY_H_
13
+
14
+ #include <gudhi/Persistent_cohomology/Persistent_cohomology_column.h>
15
+ #include <gudhi/Persistent_cohomology/Field_Zp.h>
16
+ #include <gudhi/Simple_object_pool.h>
17
+
18
+ #include <boost/intrusive/set.hpp>
19
+ #include <boost/pending/disjoint_sets.hpp>
20
+ #include <boost/intrusive/list.hpp>
21
+
22
+ #include <iostream>
23
+ #include <map>
24
+ #include <unordered_map>
25
+ #include <utility>
26
+ #include <list>
27
+ #include <vector>
28
+ #include <set>
29
+ #include <fstream> // std::ofstream
30
+ #include <limits> // for numeric_limits<>
31
+ #include <tuple>
32
+ #include <algorithm>
33
+ #include <string>
34
+ #include <stdexcept> // for std::out_of_range
35
+
36
+ namespace Gudhi {
37
+
38
+ namespace persistent_cohomology {
39
+
40
+ /** \brief Computes the persistent cohomology of a filtered complex.
41
+ *
42
+ * \ingroup persistent_cohomology
43
+ *
44
+ * The computation is implemented with a Compressed Annotation Matrix
45
+ * (CAM)\cite DBLP:conf/esa/BoissonnatDM13,
46
+ * and is adapted to the computation of Multi-Field Persistent Homology (MF)
47
+ * \cite boissonnat:hal-00922572 .
48
+ *
49
+ * \implements PersistentHomology
50
+ *
51
+ */
52
+ // TODO(CM): Memory allocation policy: classic, use a mempool, etc.
53
+ template<class FilteredComplex, class CoefficientField>
54
+ class Persistent_cohomology {
55
+ public:
56
+ // Data attached to each simplex to interface with a Property Map.
57
+
58
+ /** \brief Data stored for each simplex. */
59
+ typedef typename FilteredComplex::Simplex_key Simplex_key;
60
+ /** \brief Handle to specify a simplex. */
61
+ typedef typename FilteredComplex::Simplex_handle Simplex_handle;
62
+ /** \brief Type for the value of the filtration function. */
63
+ typedef typename FilteredComplex::Filtration_value Filtration_value;
64
+ /** \brief Type of element of the field. */
65
+ typedef typename CoefficientField::Element Arith_element;
66
+ /** \brief Type for birth and death FilteredComplex::Simplex_handle.
67
+ * The Arith_element field is used for the multi-field framework. */
68
+ typedef std::tuple<Simplex_handle, Simplex_handle, Arith_element> Persistent_interval;
69
+
70
+ private:
71
+ // Compressed Annotation Matrix types:
72
+ // Column type
73
+ typedef Persistent_cohomology_column<Simplex_key, Arith_element> Column; // contains 1 set_hook
74
+ // Cell type
75
+ typedef typename Column::Cell Cell; // contains 2 list_hooks
76
+ // Remark: constant_time_size must be false because base_hook_cam_h has auto_unlink link_mode
77
+ typedef boost::intrusive::list<Cell,
78
+ boost::intrusive::constant_time_size<false>,
79
+ boost::intrusive::base_hook<base_hook_cam_h> > Hcell;
80
+
81
+ typedef boost::intrusive::set<Column,
82
+ boost::intrusive::constant_time_size<false> > Cam;
83
+ // Sparse column type for the annotation of the boundary of an element.
84
+ typedef std::vector<std::pair<Simplex_key, Arith_element> > A_ds_type;
85
+
86
+ public:
87
+ /** \brief Initializes the Persistent_cohomology class.
88
+ *
89
+ * @param[in] cpx Complex for which the persistent homology is computed.
90
+ * cpx is a model of FilteredComplex
91
+ *
92
+ * @param[in] persistence_dim_max if true, the persistent homology for the maximal dimension in the
93
+ * complex is computed. If false, it is ignored. Default is false.
94
+ *
95
+ * @exception std::out_of_range In case the number of simplices is more than Simplex_key type numeric limit.
96
+ */
97
+ explicit Persistent_cohomology(FilteredComplex& cpx, bool persistence_dim_max = false)
98
+ : cpx_(&cpx),
99
+ dim_max_(cpx.dimension()), // upper bound on the dimension of the simplices
100
+ coeff_field_(), // initialize the field coefficient structure.
101
+ num_simplices_(cpx_->num_simplices()), // num_simplices save to avoid to call thrice the function
102
+ ds_rank_(num_simplices_), // union-find
103
+ ds_parent_(num_simplices_), // union-find
104
+ ds_repr_(num_simplices_, NULL), // union-find -> annotation vectors
105
+ dsets_(ds_rank_.data(), ds_parent_.data()), // union-find
106
+ cam_(), // collection of annotation vectors
107
+ zero_cocycles_(), // union-find -> Simplex_key of creator for 0-homology
108
+ transverse_idx_(), // key -> row
109
+ persistent_pairs_(),
110
+ interval_length_policy(&cpx, 0),
111
+ column_pool_(), // memory pools for the CAM
112
+ cell_pool_() {
113
+ if (num_simplices_ > std::numeric_limits<Simplex_key>::max()) {
114
+ // num_simplices must be strictly lower than the limit, because a value is reserved for null_key.
115
+ throw std::out_of_range("The number of simplices is more than Simplex_key type numeric limit.");
116
+ }
117
+ if (persistence_dim_max) {
118
+ ++dim_max_;
119
+ }
120
+ }
121
+
122
+ ~Persistent_cohomology() {
123
+ // Clean the transversal lists
124
+ for (auto & transverse_ref : transverse_idx_) {
125
+ // Destruct all the cells
126
+ transverse_ref.second.row_->clear_and_dispose([&](Cell*p){p->~Cell();});
127
+ delete transverse_ref.second.row_;
128
+ }
129
+ }
130
+
131
+ private:
132
+ struct length_interval {
133
+ length_interval(FilteredComplex * cpx, Filtration_value min_length)
134
+ : cpx_(cpx),
135
+ min_length_(min_length) {
136
+ }
137
+
138
+ bool operator()(Simplex_handle sh1, Simplex_handle sh2) {
139
+ return cpx_->filtration(sh2) - cpx_->filtration(sh1) > min_length_;
140
+ }
141
+
142
+ void set_length(Filtration_value new_length) {
143
+ min_length_ = new_length;
144
+ }
145
+
146
+ FilteredComplex * cpx_;
147
+ Filtration_value min_length_;
148
+ };
149
+
150
+ public:
151
+ /** \brief Initializes the coefficient field.*/
152
+ void init_coefficients(int charac) {
153
+ coeff_field_.init(charac);
154
+ }
155
+ /** \brief Initializes the coefficient field for multi-field persistent homology.*/
156
+ void init_coefficients(int charac_min, int charac_max) {
157
+ coeff_field_.init(charac_min, charac_max);
158
+ }
159
+
160
+ /** \brief Compute the persistent homology of the filtered simplicial
161
+ * complex.
162
+ *
163
+ * @param[in] min_interval_length the computation discards all intervals of length
164
+ * less or equal than min_interval_length
165
+ *
166
+ * Assumes that the filtration provided by the simplicial complex is
167
+ * valid. Undefined behavior otherwise. */
168
+ void compute_persistent_cohomology(Filtration_value min_interval_length = 0,
169
+ const bool is_simplicial_and_starting_from_dim_0 = true) {
170
+ interval_length_policy.set_length(min_interval_length);
171
+ Simplex_key idx_fil = -1;
172
+ std::vector<Simplex_key> vertices; // so we can check the connected components at the end
173
+ // Compute all finite intervals
174
+ for (auto sh : cpx_->filtration_simplex_range()) {
175
+ cpx_->assign_key(sh, ++idx_fil);
176
+ dsets_.make_set(cpx_->key(sh));
177
+ int dim_simplex = cpx_->dimension(sh);
178
+ if (is_simplicial_and_starting_from_dim_0){
179
+ switch (dim_simplex) {
180
+ case 0:
181
+ vertices.push_back(idx_fil);
182
+ break;
183
+ case 1:
184
+ update_cohomology_groups_edge(sh);
185
+ break;
186
+ default:
187
+ update_cohomology_groups(sh, dim_simplex);
188
+ break;
189
+ }
190
+ } else {
191
+ update_cohomology_groups(sh, dim_simplex);
192
+ }
193
+ }
194
+ // Compute infinite intervals of dimension 0
195
+ for (Simplex_key key : vertices) { // for all 0-dimensional simplices
196
+ if (ds_parent_[key] == key // root of its tree
197
+ && zero_cocycles_.find(key) == zero_cocycles_.end()) {
198
+ persistent_pairs_.emplace_back(
199
+ cpx_->simplex(key), cpx_->null_simplex(), coeff_field_.characteristic());
200
+ }
201
+ }
202
+ for (auto zero_idx : zero_cocycles_) {
203
+ persistent_pairs_.emplace_back(
204
+ cpx_->simplex(zero_idx.second), cpx_->null_simplex(), coeff_field_.characteristic());
205
+ }
206
+ // Compute infinite interval of dimension > 0
207
+ for (auto cocycle : transverse_idx_) {
208
+ persistent_pairs_.emplace_back(
209
+ cpx_->simplex(cocycle.first), cpx_->null_simplex(), cocycle.second.characteristics_);
210
+ }
211
+ }
212
+
213
+ private:
214
+ /** \brief Update the cohomology groups under the insertion of an edge.
215
+ *
216
+ * The 0-homology is maintained with a simple Union-Find data structure, which
217
+ * explains the existence of a specific function of edge insertions. */
218
+ void update_cohomology_groups_edge(Simplex_handle sigma) {
219
+ Simplex_handle u, v;
220
+ boost::tie(u, v) = cpx_->endpoints(sigma);
221
+
222
+ Simplex_key ku = dsets_.find_set(cpx_->key(u));
223
+ Simplex_key kv = dsets_.find_set(cpx_->key(v));
224
+
225
+ if (ku != kv) { // Destroy a connected component
226
+ dsets_.link(ku, kv);
227
+ // Keys of the simplices which created the connected components containing
228
+ // respectively u and v.
229
+ Simplex_key idx_coc_u, idx_coc_v;
230
+ auto map_it_u = zero_cocycles_.find(ku);
231
+ // If the index of the cocycle representing the class is already ku.
232
+ if (map_it_u == zero_cocycles_.end()) {
233
+ idx_coc_u = ku;
234
+ } else {
235
+ idx_coc_u = map_it_u->second;
236
+ }
237
+
238
+ auto map_it_v = zero_cocycles_.find(kv);
239
+ // If the index of the cocycle representing the class is already kv.
240
+ if (map_it_v == zero_cocycles_.end()) {
241
+ idx_coc_v = kv;
242
+ } else {
243
+ idx_coc_v = map_it_v->second;
244
+ }
245
+
246
+ if (cpx_->filtration(cpx_->simplex(idx_coc_u))
247
+ < cpx_->filtration(cpx_->simplex(idx_coc_v))) { // Kill cocycle [idx_coc_v], which is younger.
248
+ if (interval_length_policy(cpx_->simplex(idx_coc_v), sigma)) {
249
+ persistent_pairs_.emplace_back(
250
+ cpx_->simplex(idx_coc_v), sigma, coeff_field_.characteristic());
251
+ }
252
+ // Maintain the index of the 0-cocycle alive.
253
+ if (kv != idx_coc_v) {
254
+ zero_cocycles_.erase(map_it_v);
255
+ }
256
+ if (kv == dsets_.find_set(kv)) {
257
+ if (ku != idx_coc_u) {
258
+ zero_cocycles_.erase(map_it_u);
259
+ }
260
+ zero_cocycles_[kv] = idx_coc_u;
261
+ }
262
+ } else { // Kill cocycle [idx_coc_u], which is younger.
263
+ if (interval_length_policy(cpx_->simplex(idx_coc_u), sigma)) {
264
+ persistent_pairs_.emplace_back(
265
+ cpx_->simplex(idx_coc_u), sigma, coeff_field_.characteristic());
266
+ }
267
+ // Maintain the index of the 0-cocycle alive.
268
+ if (ku != idx_coc_u) {
269
+ zero_cocycles_.erase(map_it_u);
270
+ }
271
+ if (ku == dsets_.find_set(ku)) {
272
+ if (kv != idx_coc_v) {
273
+ zero_cocycles_.erase(map_it_v);
274
+ }
275
+ zero_cocycles_[ku] = idx_coc_v;
276
+ }
277
+ }
278
+ cpx_->assign_key(sigma, cpx_->null_key());
279
+ } else if (dim_max_ > 1) { // If ku == kv, same connected component: create a 1-cocycle class.
280
+ create_cocycle(sigma, coeff_field_.multiplicative_identity(), coeff_field_.characteristic());
281
+ }
282
+ }
283
+
284
+ /*
285
+ * Compute the annotation of the boundary of a simplex.
286
+ */
287
+ void annotation_of_the_boundary(
288
+ std::map<Simplex_key, Arith_element> & map_a_ds, Simplex_handle sigma,
289
+ int dim_sigma) {
290
+ // traverses the boundary of sigma, keeps track of the annotation vectors,
291
+ // with multiplicity. We used to sum the coefficients directly in
292
+ // annotations_in_boundary by using a map, we now do it later.
293
+ typedef std::pair<Column *, int> annotation_t;
294
+ thread_local std::vector<annotation_t> annotations_in_boundary;
295
+ annotations_in_boundary.clear();
296
+ int sign = 1 - 2 * (dim_sigma % 2); // \in {-1,1} provides the sign in the
297
+ // alternate sum in the boundary.
298
+ Simplex_key key;
299
+ Column * curr_col;
300
+
301
+ for (auto sh : cpx_->boundary_simplex_range(sigma)) {
302
+ key = cpx_->key(sh);
303
+ if (key != cpx_->null_key()) { // A simplex with null_key is a killer, and have null annotation
304
+ // Find its annotation vector
305
+ curr_col = ds_repr_[dsets_.find_set(key)];
306
+ if (curr_col != NULL) { // and insert it in annotations_in_boundary with multyiplicative factor "sign".
307
+ annotations_in_boundary.emplace_back(curr_col, sign);
308
+ }
309
+ }
310
+ sign = -sign;
311
+ }
312
+ // Place identical annotations consecutively so we can easily sum their multiplicities.
313
+ std::sort(annotations_in_boundary.begin(), annotations_in_boundary.end(),
314
+ [](annotation_t const& a, annotation_t const& b) { return a.first < b.first; });
315
+
316
+ // Sum the annotations with multiplicity, using a map<key,coeff>
317
+ // to represent a sparse vector.
318
+ std::pair<typename std::map<Simplex_key, Arith_element>::iterator, bool> result_insert_a_ds;
319
+
320
+ for (auto ann_it = annotations_in_boundary.begin(); ann_it != annotations_in_boundary.end(); /**/) {
321
+ Column* col = ann_it->first;
322
+ int mult = ann_it->second;
323
+ while (++ann_it != annotations_in_boundary.end() && ann_it->first == col) {
324
+ mult += ann_it->second;
325
+ }
326
+ // The following test is just a heuristic, it is not required, and it is fine that is misses p == 0.
327
+ if (mult != coeff_field_.additive_identity()) { // For all columns in the boundary,
328
+ for (auto cell_ref : col->col_) { // insert every cell in map_a_ds with multiplicity
329
+ Arith_element w_y = coeff_field_.times(cell_ref.coefficient_, mult); // coefficient * multiplicity
330
+
331
+ if (w_y != coeff_field_.additive_identity()) { // if != 0
332
+ result_insert_a_ds = map_a_ds.insert(std::pair<Simplex_key, Arith_element>(cell_ref.key_, w_y));
333
+ if (!(result_insert_a_ds.second)) { // if cell_ref.key_ already a Key in map_a_ds
334
+ result_insert_a_ds.first->second = coeff_field_.plus_equal(result_insert_a_ds.first->second, w_y);
335
+ if (result_insert_a_ds.first->second == coeff_field_.additive_identity()) {
336
+ map_a_ds.erase(result_insert_a_ds.first);
337
+ }
338
+ }
339
+ }
340
+ }
341
+ }
342
+ }
343
+ }
344
+
345
+ /*
346
+ * Update the cohomology groups under the insertion of a simplex.
347
+ */
348
+ void update_cohomology_groups(Simplex_handle sigma, int dim_sigma) {
349
+ // Compute the annotation of the boundary of sigma:
350
+ std::map<Simplex_key, Arith_element> map_a_ds;
351
+ annotation_of_the_boundary(map_a_ds, sigma, dim_sigma);
352
+ // Update the cohomology groups:
353
+ if (map_a_ds.empty()) { // sigma is a creator in all fields represented in coeff_field_
354
+ if (dim_sigma < dim_max_) {
355
+ create_cocycle(sigma, coeff_field_.multiplicative_identity(),
356
+ coeff_field_.characteristic());
357
+ }
358
+ } else { // sigma is a destructor in at least a field in coeff_field_
359
+ // Convert map_a_ds to a vector
360
+ A_ds_type a_ds; // admits reverse iterators
361
+ for (auto map_a_ds_ref : map_a_ds) {
362
+ a_ds.push_back(
363
+ std::pair<Simplex_key, Arith_element>(map_a_ds_ref.first,
364
+ map_a_ds_ref.second));
365
+ }
366
+
367
+ Arith_element inv_x, charac;
368
+ Arith_element prod = coeff_field_.characteristic(); // Product of characteristic of the fields
369
+ for (auto a_ds_rit = a_ds.rbegin();
370
+ (a_ds_rit != a_ds.rend())
371
+ && (prod != coeff_field_.multiplicative_identity()); ++a_ds_rit) {
372
+ std::tie(inv_x, charac) = coeff_field_.inverse(a_ds_rit->second, prod);
373
+
374
+ if (inv_x != coeff_field_.additive_identity()) {
375
+ destroy_cocycle(sigma, a_ds, a_ds_rit->first, inv_x, charac);
376
+ prod /= charac;
377
+ }
378
+ }
379
+ if (prod != coeff_field_.multiplicative_identity()
380
+ && dim_sigma < dim_max_) {
381
+ create_cocycle(sigma, coeff_field_.multiplicative_identity(prod), prod);
382
+ }
383
+ }
384
+ }
385
+
386
+ /* \brief Create a new cocycle class.
387
+ *
388
+ * The class is created by the insertion of the simplex sigma.
389
+ * The methods adds a cocycle, representing the new cocycle class,
390
+ * to the matrix representing the cohomology groups.
391
+ * The new cocycle has value 0 on every simplex except on sigma
392
+ * where it worths 1.*/
393
+ void create_cocycle(Simplex_handle sigma, Arith_element x,
394
+ Arith_element charac) {
395
+ Simplex_key key = cpx_->key(sigma);
396
+ // Create a column containing only one cell,
397
+ Column * new_col = column_pool_.construct(key);
398
+ Cell * new_cell = cell_pool_.construct(key, x, new_col);
399
+ new_col->col_.push_back(*new_cell);
400
+ // and insert it in the matrix, in constant time thanks to the hint cam_.end().
401
+ // Indeed *new_col has the biggest lexicographic value because key is the
402
+ // biggest key used so far.
403
+ cam_.insert(cam_.end(), *new_col);
404
+ // Update the disjoint sets data structure.
405
+ Hcell * new_hcell = new Hcell;
406
+ new_hcell->push_back(*new_cell);
407
+ transverse_idx_[key] = cocycle(charac, new_hcell); // insert the new row
408
+ ds_repr_[key] = new_col;
409
+ }
410
+
411
+ /* \brief Destroy a cocycle class.
412
+ *
413
+ * The cocycle class is destroyed by the insertion of sigma.
414
+ * The methods proceeds to a reduction of the matrix representing
415
+ * the cohomology groups using Gauss pivoting. The reduction zeros-out
416
+ * the row containing the cell with highest key in
417
+ * a_ds, the annotation of the boundary of simplex sigma. This key
418
+ * is "death_key".*/
419
+ void destroy_cocycle(Simplex_handle sigma, A_ds_type const& a_ds,
420
+ Simplex_key death_key, Arith_element inv_x,
421
+ Arith_element charac) {
422
+ // Create a finite persistent interval for which the interval exists
423
+ if (interval_length_policy(cpx_->simplex(death_key), sigma)) {
424
+ persistent_pairs_.emplace_back(cpx_->simplex(death_key) // creator
425
+ , sigma // destructor
426
+ , charac); // fields
427
+ }
428
+
429
+ auto death_key_row = transverse_idx_.find(death_key); // Find the beginning of the row.
430
+ std::pair<typename Cam::iterator, bool> result_insert_cam;
431
+
432
+ auto row_cell_it = death_key_row->second.row_->begin();
433
+
434
+ while (row_cell_it != death_key_row->second.row_->end()) { // Traverse all cells in
435
+ // the row at index death_key.
436
+ Arith_element w = coeff_field_.times_minus(inv_x, row_cell_it->coefficient_);
437
+
438
+ if (w != coeff_field_.additive_identity()) {
439
+ Column * curr_col = row_cell_it->self_col_;
440
+ ++row_cell_it;
441
+ // Disconnect the column from the rows in the CAM.
442
+ for (auto& col_cell : curr_col->col_) {
443
+ col_cell.base_hook_cam_h::unlink();
444
+ }
445
+
446
+ // Remove the column from the CAM before modifying its value
447
+ cam_.erase(cam_.iterator_to(*curr_col));
448
+ // Proceed to the reduction of the column
449
+ plus_equal_column(*curr_col, a_ds, w);
450
+
451
+ if (curr_col->col_.empty()) { // If the column is null
452
+ ds_repr_[curr_col->class_key_] = NULL;
453
+ column_pool_.destroy(curr_col); // delete curr_col;
454
+ } else {
455
+ // Find whether the column obtained is already in the CAM
456
+ result_insert_cam = cam_.insert(*curr_col);
457
+ if (result_insert_cam.second) { // If it was not in the CAM before: insertion has succeeded
458
+ for (auto& col_cell : curr_col->col_) {
459
+ // re-establish the row links
460
+ transverse_idx_[col_cell.key_].row_->push_front(col_cell);
461
+ }
462
+ } else { // There is already an identical column in the CAM:
463
+ // merge two disjoint sets.
464
+ dsets_.link(curr_col->class_key_,
465
+ result_insert_cam.first->class_key_);
466
+
467
+ Simplex_key key_tmp = dsets_.find_set(curr_col->class_key_);
468
+ ds_repr_[key_tmp] = &(*(result_insert_cam.first));
469
+ result_insert_cam.first->class_key_ = key_tmp;
470
+ // intrusive containers don't own their elements, we have to release them manually
471
+ curr_col->col_.clear_and_dispose([&](Cell*p){cell_pool_.destroy(p);});
472
+ column_pool_.destroy(curr_col); // delete curr_col;
473
+ }
474
+ }
475
+ } else {
476
+ ++row_cell_it;
477
+ } // If w == 0, pass.
478
+ }
479
+
480
+ // Because it is a killer simplex, set the data of sigma to null_key().
481
+ if (charac == coeff_field_.characteristic()) {
482
+ cpx_->assign_key(sigma, cpx_->null_key());
483
+ }
484
+ if (death_key_row->second.characteristics_ == charac) {
485
+ delete death_key_row->second.row_;
486
+ transverse_idx_.erase(death_key_row);
487
+ } else {
488
+ death_key_row->second.characteristics_ /= charac;
489
+ }
490
+ }
491
+
492
+ /*
493
+ * Assign: target <- target + w * other.
494
+ */
495
+ void plus_equal_column(Column & target, A_ds_type const& other // value_type is pair<Simplex_key,Arith_element>
496
+ , Arith_element w) {
497
+ auto target_it = target.col_.begin();
498
+ auto other_it = other.begin();
499
+ while (target_it != target.col_.end() && other_it != other.end()) {
500
+ if (target_it->key_ < other_it->first) {
501
+ ++target_it;
502
+ } else {
503
+ if (target_it->key_ > other_it->first) {
504
+ Cell * cell_tmp = cell_pool_.construct(Cell(other_it->first // key
505
+ , coeff_field_.additive_identity(), &target));
506
+
507
+ cell_tmp->coefficient_ = coeff_field_.plus_times_equal(cell_tmp->coefficient_, other_it->second, w);
508
+
509
+ target.col_.insert(target_it, *cell_tmp);
510
+
511
+ ++other_it;
512
+ } else { // it1->key == it2->key
513
+ // target_it->coefficient_ <- target_it->coefficient_ + other_it->second * w
514
+ target_it->coefficient_ = coeff_field_.plus_times_equal(target_it->coefficient_, other_it->second, w);
515
+ if (target_it->coefficient_ == coeff_field_.additive_identity()) {
516
+ auto tmp_it = target_it;
517
+ ++target_it;
518
+ ++other_it; // iterators remain valid
519
+ Cell * tmp_cell_ptr = &(*tmp_it);
520
+ target.col_.erase(tmp_it); // removed from column
521
+
522
+ cell_pool_.destroy(tmp_cell_ptr); // delete from memory
523
+ } else {
524
+ ++target_it;
525
+ ++other_it;
526
+ }
527
+ }
528
+ }
529
+ }
530
+ while (other_it != other.end()) {
531
+ Cell * cell_tmp = cell_pool_.construct(Cell(other_it->first, coeff_field_.additive_identity(), &target));
532
+ cell_tmp->coefficient_ = coeff_field_.plus_times_equal(cell_tmp->coefficient_, other_it->second, w);
533
+ target.col_.insert(target.col_.end(), *cell_tmp);
534
+
535
+ ++other_it;
536
+ }
537
+ }
538
+
539
+ /*
540
+ * Compare two intervals by length.
541
+ */
542
+ struct cmp_intervals_by_length {
543
+ explicit cmp_intervals_by_length(FilteredComplex * sc)
544
+ : sc_(sc) {
545
+ }
546
+ bool operator()(const Persistent_interval & p1, const Persistent_interval & p2) {
547
+ return (sc_->filtration(get < 1 > (p1)) - sc_->filtration(get < 0 > (p1))
548
+ > sc_->filtration(get < 1 > (p2)) - sc_->filtration(get < 0 > (p2)));
549
+ }
550
+ FilteredComplex * sc_;
551
+ };
552
+
553
+ public:
554
+ /** \brief Output the persistence diagram in ostream.
555
+ *
556
+ * The file format is the following:
557
+ * p1*...*pr dim b d
558
+ *
559
+ * where "dim" is the dimension of the homological feature,
560
+ * b and d are respectively the birth and death of the feature and
561
+ * p1*...*pr is the product of prime numbers pi such that the homology
562
+ * feature exists in homology with Z/piZ coefficients.
563
+ */
564
+ void output_diagram(std::ostream& ostream = std::cout) {
565
+ cmp_intervals_by_length cmp(cpx_);
566
+ std::sort(std::begin(persistent_pairs_), std::end(persistent_pairs_), cmp);
567
+ for (auto pair : persistent_pairs_) {
568
+ ostream << get<2>(pair) << " " << cpx_->dimension(get<0>(pair)) << " "
569
+ << cpx_->filtration(get<0>(pair)) << " "
570
+ << cpx_->filtration(get<1>(pair)) << " " << std::endl;
571
+ }
572
+ }
573
+
574
+ void write_output_diagram(std::string diagram_name) {
575
+ std::ofstream diagram_out(diagram_name.c_str());
576
+ diagram_out.exceptions(diagram_out.failbit);
577
+ cmp_intervals_by_length cmp(cpx_);
578
+ std::sort(std::begin(persistent_pairs_), std::end(persistent_pairs_), cmp);
579
+ for (auto pair : persistent_pairs_) {
580
+ diagram_out << cpx_->dimension(get<0>(pair)) << " "
581
+ << cpx_->filtration(get<0>(pair)) << " "
582
+ << cpx_->filtration(get<1>(pair)) << std::endl;
583
+ }
584
+ }
585
+
586
+ /** @brief Returns Betti numbers.
587
+ * @return A vector of Betti numbers.
588
+ */
589
+ std::vector<int> betti_numbers() const {
590
+ // Init Betti numbers vector with zeros until Simplicial complex dimension and don't allocate a vector of negative
591
+ // size for an empty complex
592
+ std::vector<int> betti_numbers(std::max(dim_max_, 0));
593
+
594
+ for (auto pair : persistent_pairs_) {
595
+ // Count never ended persistence intervals
596
+ if (cpx_->null_simplex() == get<1>(pair)) {
597
+ // Increment corresponding betti number
598
+ betti_numbers[cpx_->dimension(get<0>(pair))] += 1;
599
+ }
600
+ }
601
+ return betti_numbers;
602
+ }
603
+
604
+ /** @brief Returns the Betti number of the dimension passed by parameter.
605
+ * @param[in] dimension The Betti number dimension to get.
606
+ * @return Betti number of the given dimension
607
+ *
608
+ */
609
+ int betti_number(int dimension) const {
610
+ int betti_number = 0;
611
+
612
+ for (auto pair : persistent_pairs_) {
613
+ // Count never ended persistence intervals
614
+ if (cpx_->null_simplex() == get<1>(pair)) {
615
+ if (cpx_->dimension(get<0>(pair)) == dimension) {
616
+ // Increment betti number found
617
+ ++betti_number;
618
+ }
619
+ }
620
+ }
621
+ return betti_number;
622
+ }
623
+
624
+ /** @brief Returns the persistent Betti numbers.
625
+ * @param[in] from The persistence birth limit to be added in the number \f$(persistent birth \leq from)\f$.
626
+ * @param[in] to The persistence death limit to be added in the number \f$(persistent death > to)\f$.
627
+ * @return A vector of persistent Betti numbers.
628
+ */
629
+ std::vector<int> persistent_betti_numbers(Filtration_value from, Filtration_value to) const {
630
+ // Init Betti numbers vector with zeros until Simplicial complex dimension and don't allocate a vector of negative
631
+ // size for an empty complex
632
+ std::vector<int> betti_numbers(std::max(dim_max_, 0));
633
+ for (auto pair : persistent_pairs_) {
634
+ // Count persistence intervals that covers the given interval
635
+ // null_simplex test : if the function is called with to=+infinity, we still get something useful. And it will
636
+ // still work if we change the complex filtration function to reject null simplices.
637
+ if (cpx_->filtration(get<0>(pair)) <= from &&
638
+ (get<1>(pair) == cpx_->null_simplex() || cpx_->filtration(get<1>(pair)) > to)) {
639
+ // Increment corresponding betti number
640
+ betti_numbers[cpx_->dimension(get<0>(pair))] += 1;
641
+ }
642
+ }
643
+ return betti_numbers;
644
+ }
645
+
646
+ /** @brief Returns the persistent Betti number of the dimension passed by parameter.
647
+ * @param[in] dimension The Betti number dimension to get.
648
+ * @param[in] from The persistence birth limit to be added in the number \f$(persistent birth \leq from)\f$.
649
+ * @param[in] to The persistence death limit to be added in the number \f$(persistent death > to)\f$.
650
+ * @return Persistent Betti number of the given dimension
651
+ */
652
+ int persistent_betti_number(int dimension, Filtration_value from, Filtration_value to) const {
653
+ int betti_number = 0;
654
+
655
+ for (auto pair : persistent_pairs_) {
656
+ // Count persistence intervals that covers the given interval
657
+ // null_simplex test : if the function is called with to=+infinity, we still get something useful. And it will
658
+ // still work if we change the complex filtration function to reject null simplices.
659
+ if (cpx_->filtration(get<0>(pair)) <= from &&
660
+ (get<1>(pair) == cpx_->null_simplex() || cpx_->filtration(get<1>(pair)) > to)) {
661
+ if (cpx_->dimension(get<0>(pair)) == dimension) {
662
+ // Increment betti number found
663
+ ++betti_number;
664
+ }
665
+ }
666
+ }
667
+ return betti_number;
668
+ }
669
+
670
+ /** @brief Returns a list of persistence birth and death FilteredComplex::Simplex_handle pairs.
671
+ * @return A list of Persistent_cohomology::Persistent_interval
672
+ */
673
+ const std::vector<Persistent_interval>& get_persistent_pairs() const {
674
+ return persistent_pairs_;
675
+ }
676
+
677
+ /** @brief Returns persistence intervals for a given dimension.
678
+ * @param[in] dimension Dimension to get the birth and death pairs from.
679
+ * @return A vector of persistence intervals (birth and death) on a fixed dimension.
680
+ */
681
+ std::vector< std::pair< Filtration_value , Filtration_value > >
682
+ intervals_in_dimension(int dimension) {
683
+ std::vector< std::pair< Filtration_value , Filtration_value > > result;
684
+ // auto && pair, to avoid unnecessary copying
685
+ for (auto && pair : persistent_pairs_) {
686
+ if (cpx_->dimension(get<0>(pair)) == dimension) {
687
+ result.emplace_back(cpx_->filtration(get<0>(pair)), cpx_->filtration(get<1>(pair)));
688
+ }
689
+ }
690
+ return result;
691
+ }
692
+
693
+ private:
694
+ /*
695
+ * Structure representing a cocycle.
696
+ */
697
+ struct cocycle {
698
+ cocycle()
699
+ : row_(nullptr),
700
+ characteristics_() {
701
+ }
702
+ cocycle(Arith_element characteristics, Hcell * row)
703
+ : row_(row),
704
+ characteristics_(characteristics) {
705
+ }
706
+
707
+ Hcell * row_; // points to the corresponding row in the CAM
708
+ Arith_element characteristics_; // product of field characteristics for which the cocycle exist
709
+ };
710
+
711
+ public:
712
+ FilteredComplex * cpx_;
713
+ int dim_max_;
714
+ CoefficientField coeff_field_;
715
+ size_t num_simplices_;
716
+
717
+ /* Disjoint sets data structure to link the model of FilteredComplex
718
+ * with the compressed annotation matrix.
719
+ * ds_rank_ is a property map Simplex_key -> int, ds_parent_ is a property map
720
+ * Simplex_key -> simplex_key_t */
721
+ std::vector<int> ds_rank_;
722
+ std::vector<Simplex_key> ds_parent_;
723
+ std::vector<Column *> ds_repr_;
724
+ boost::disjoint_sets<int *, Simplex_key *> dsets_;
725
+ /* The compressed annotation matrix fields.*/
726
+ Cam cam_;
727
+ /* Dictionary establishing the correspondence between the Simplex_key of
728
+ * the root vertex in the union-find ds and the Simplex_key of the vertex which
729
+ * created the connected component as a 0-dimension homology feature.*/
730
+ std::unordered_map<Simplex_key, Simplex_key> zero_cocycles_;
731
+ /* Key -> row. */
732
+ std::map<Simplex_key, cocycle> transverse_idx_;
733
+ /* Persistent intervals. */
734
+ std::vector<Persistent_interval> persistent_pairs_;
735
+ length_interval interval_length_policy;
736
+
737
+ Simple_object_pool<Column> column_pool_;
738
+ Simple_object_pool<Cell> cell_pool_;
739
+ };
740
+
741
+ } // namespace persistent_cohomology
742
+
743
+ } // namespace Gudhi
744
+
745
+ #endif // PERSISTENT_COHOMOLOGY_H_