Loading...
Searching...
No Matches
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
36namespace Gudhi {
37
38namespace persistent_cohomology {
39
52// TODO(CM): Memory allocation policy: classic, use a mempool, etc.
53template<class FilteredComplex, class CoefficientField>
55 public:
56 // Data attached to each simplex to interface with a Property Map.
57
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:
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
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:
152 void init_coefficients(int charac) {
153 coeff_field_.init(charac);
154 }
155
156 void init_coefficients(int charac_min, int charac_max) {
157 coeff_field_.init(charac_min, charac_max);
158 }
159
168 void compute_persistent_cohomology(Filtration_value min_interval_length = 0) {
169 if (dim_max_ <= 0)
170 return; // --------->>
171
172 interval_length_policy.set_length(min_interval_length);
173 Simplex_key idx_fil = -1;
174 std::vector<Simplex_key> vertices; // so we can check the connected components at the end
175 // Compute all finite intervals
176 for (auto sh : cpx_->filtration_simplex_range()) {
177 cpx_->assign_key(sh, ++idx_fil);
178 dsets_.make_set(cpx_->key(sh));
179 int dim_simplex = cpx_->dimension(sh);
180 switch (dim_simplex) {
181 case 0:
182 vertices.push_back(idx_fil);
183 break;
184 case 1:
185 update_cohomology_groups_edge(sh);
186 break;
187 default:
188 update_cohomology_groups(sh, dim_simplex);
189 break;
190 }
191 }
192 // Compute infinite intervals of dimension 0
193 for (Simplex_key key : vertices) { // for all 0-dimensional simplices
194 if (ds_parent_[key] == key // root of its tree
195 && zero_cocycles_.find(key) == zero_cocycles_.end()) {
196 persistent_pairs_.emplace_back(
197 cpx_->simplex(key), cpx_->null_simplex(), coeff_field_.characteristic());
198 }
199 }
200 for (auto zero_idx : zero_cocycles_) {
201 persistent_pairs_.emplace_back(
202 cpx_->simplex(zero_idx.second), cpx_->null_simplex(), coeff_field_.characteristic());
203 }
204 // Compute infinite interval of dimension > 0
205 for (auto cocycle : transverse_idx_) {
206 persistent_pairs_.emplace_back(
207 cpx_->simplex(cocycle.first), cpx_->null_simplex(), cocycle.second.characteristics_);
208 }
209 }
210
216 void compute_persistent_cohomology_without_optimizations(Filtration_value min_interval_length = 0) {
217 if (dim_max_ <= 0)
218 return; // --------->>
219
220 interval_length_policy.set_length(min_interval_length);
221 Simplex_key idx_fil = -1;
222 // Compute all finite intervals
223 for (auto sh : cpx_->filtration_simplex_range()) {
224 cpx_->assign_key(sh, ++idx_fil);
225 dsets_.make_set(cpx_->key(sh));
226 int dim_simplex = cpx_->dimension(sh);
227 update_cohomology_groups(sh, dim_simplex);
228 }
229
230 for (auto cocycle : transverse_idx_) {
231 persistent_pairs_.emplace_back(
232 cpx_->simplex(cocycle.first), cpx_->null_simplex(), cocycle.second.characteristics_);
233 }
234 }
235
236 private:
241 void update_cohomology_groups_edge(Simplex_handle sigma) {
242 Simplex_handle u, v;
243 boost::tie(u, v) = cpx_->endpoints(sigma);
244
245 Simplex_key ku = dsets_.find_set(cpx_->key(u));
246 Simplex_key kv = dsets_.find_set(cpx_->key(v));
247
248 if (ku != kv) { // Destroy a connected component
249 dsets_.link(ku, kv);
250 // Keys of the simplices which created the connected components containing
251 // respectively u and v.
252 Simplex_key idx_coc_u, idx_coc_v;
253 auto map_it_u = zero_cocycles_.find(ku);
254 // If the index of the cocycle representing the class is already ku.
255 if (map_it_u == zero_cocycles_.end()) {
256 idx_coc_u = ku;
257 } else {
258 idx_coc_u = map_it_u->second;
259 }
260
261 auto map_it_v = zero_cocycles_.find(kv);
262 // If the index of the cocycle representing the class is already kv.
263 if (map_it_v == zero_cocycles_.end()) {
264 idx_coc_v = kv;
265 } else {
266 idx_coc_v = map_it_v->second;
267 }
268
269 if (cpx_->filtration(cpx_->simplex(idx_coc_u))
270 < cpx_->filtration(cpx_->simplex(idx_coc_v))) { // Kill cocycle [idx_coc_v], which is younger.
271 if (interval_length_policy(cpx_->simplex(idx_coc_v), sigma)) {
272 persistent_pairs_.emplace_back(
273 cpx_->simplex(idx_coc_v), sigma, coeff_field_.characteristic());
274 }
275 // Maintain the index of the 0-cocycle alive.
276 if (kv != idx_coc_v) {
277 zero_cocycles_.erase(map_it_v);
278 }
279 if (kv == dsets_.find_set(kv)) {
280 if (ku != idx_coc_u) {
281 zero_cocycles_.erase(map_it_u);
282 }
283 zero_cocycles_[kv] = idx_coc_u;
284 }
285 } else { // Kill cocycle [idx_coc_u], which is younger.
286 if (interval_length_policy(cpx_->simplex(idx_coc_u), sigma)) {
287 persistent_pairs_.emplace_back(
288 cpx_->simplex(idx_coc_u), sigma, coeff_field_.characteristic());
289 }
290 // Maintain the index of the 0-cocycle alive.
291 if (ku != idx_coc_u) {
292 zero_cocycles_.erase(map_it_u);
293 }
294 if (ku == dsets_.find_set(ku)) {
295 if (kv != idx_coc_v) {
296 zero_cocycles_.erase(map_it_v);
297 }
298 zero_cocycles_[ku] = idx_coc_v;
299 }
300 }
301 cpx_->assign_key(sigma, cpx_->null_key());
302 } else if (dim_max_ > 1) { // If ku == kv, same connected component: create a 1-cocycle class.
303 create_cocycle(sigma, coeff_field_.multiplicative_identity(), coeff_field_.characteristic());
304 }
305 }
306
307 /*
308 * Compute the annotation of the boundary of a simplex.
309 */
310 void annotation_of_the_boundary(
311 std::map<Simplex_key, Arith_element> & map_a_ds, Simplex_handle sigma,
312 int dim_sigma) {
313 // traverses the boundary of sigma, keeps track of the annotation vectors,
314 // with multiplicity. We used to sum the coefficients directly in
315 // annotations_in_boundary by using a map, we now do it later.
316 typedef std::pair<Column *, int> annotation_t;
317 thread_local std::vector<annotation_t> annotations_in_boundary;
318 annotations_in_boundary.clear();
319 int sign = 1 - 2 * (dim_sigma % 2); // \in {-1,1} provides the sign in the
320 // alternate sum in the boundary.
321 Simplex_key key;
322 Column * curr_col;
323
324 for (auto sh : cpx_->boundary_simplex_range(sigma)) {
325 key = cpx_->key(sh);
326 if (key != cpx_->null_key()) { // A simplex with null_key is a killer, and have null annotation
327 // Find its annotation vector
328 curr_col = ds_repr_[dsets_.find_set(key)];
329 if (curr_col != NULL) { // and insert it in annotations_in_boundary with multyiplicative factor "sign".
330 annotations_in_boundary.emplace_back(curr_col, sign);
331 }
332 }
333 sign = -sign;
334 }
335 // Place identical annotations consecutively so we can easily sum their multiplicities.
336 std::sort(annotations_in_boundary.begin(), annotations_in_boundary.end(),
337 [](annotation_t const& a, annotation_t const& b) { return a.first < b.first; });
338
339 // Sum the annotations with multiplicity, using a map<key,coeff>
340 // to represent a sparse vector.
341 std::pair<typename std::map<Simplex_key, Arith_element>::iterator, bool> result_insert_a_ds;
342
343 for (auto ann_it = annotations_in_boundary.begin(); ann_it != annotations_in_boundary.end(); /**/) {
344 Column* col = ann_it->first;
345 int mult = ann_it->second;
346 while (++ann_it != annotations_in_boundary.end() && ann_it->first == col) {
347 mult += ann_it->second;
348 }
349 // The following test is just a heuristic, it is not required, and it is fine that is misses p == 0.
350 if (mult != coeff_field_.additive_identity()) { // For all columns in the boundary,
351 for (auto cell_ref : col->col_) { // insert every cell in map_a_ds with multiplicity
352 Arith_element w_y = coeff_field_.times(cell_ref.coefficient_, mult); // coefficient * multiplicity
353
354 if (w_y != coeff_field_.additive_identity()) { // if != 0
355 result_insert_a_ds = map_a_ds.insert(std::pair<Simplex_key, Arith_element>(cell_ref.key_, w_y));
356 if (!(result_insert_a_ds.second)) { // if cell_ref.key_ already a Key in map_a_ds
357 result_insert_a_ds.first->second = coeff_field_.plus_equal(result_insert_a_ds.first->second, w_y);
358 if (result_insert_a_ds.first->second == coeff_field_.additive_identity()) {
359 map_a_ds.erase(result_insert_a_ds.first);
360 }
361 }
362 }
363 }
364 }
365 }
366 }
367
368 /*
369 * Update the cohomology groups under the insertion of a simplex.
370 */
371 void update_cohomology_groups(Simplex_handle sigma, int dim_sigma) {
372// Compute the annotation of the boundary of sigma:
373 std::map<Simplex_key, Arith_element> map_a_ds;
374 annotation_of_the_boundary(map_a_ds, sigma, dim_sigma);
375// Update the cohomology groups:
376 if (map_a_ds.empty()) { // sigma is a creator in all fields represented in coeff_field_
377 if (dim_sigma < dim_max_) {
378 create_cocycle(sigma, coeff_field_.multiplicative_identity(),
379 coeff_field_.characteristic());
380 }
381 } else { // sigma is a destructor in at least a field in coeff_field_
382 // Convert map_a_ds to a vector
383 A_ds_type a_ds; // admits reverse iterators
384 for (auto map_a_ds_ref : map_a_ds) {
385 a_ds.push_back(
386 std::pair<Simplex_key, Arith_element>(map_a_ds_ref.first,
387 map_a_ds_ref.second));
388 }
389
390 Arith_element inv_x, charac;
391 Arith_element prod = coeff_field_.characteristic(); // Product of characteristic of the fields
392 for (auto a_ds_rit = a_ds.rbegin();
393 (a_ds_rit != a_ds.rend())
394 && (prod != coeff_field_.multiplicative_identity()); ++a_ds_rit) {
395 std::tie(inv_x, charac) = coeff_field_.inverse(a_ds_rit->second, prod);
396
397 if (inv_x != coeff_field_.additive_identity()) {
398 destroy_cocycle(sigma, a_ds, a_ds_rit->first, inv_x, charac);
399 prod /= charac;
400 }
401 }
402 if (prod != coeff_field_.multiplicative_identity()
403 && dim_sigma < dim_max_) {
404 create_cocycle(sigma, coeff_field_.multiplicative_identity(prod), prod);
405 }
406 }
407 }
408
409 /* \brief Create a new cocycle class.
410 *
411 * The class is created by the insertion of the simplex sigma.
412 * The methods adds a cocycle, representing the new cocycle class,
413 * to the matrix representing the cohomology groups.
414 * The new cocycle has value 0 on every simplex except on sigma
415 * where it worths 1.*/
416 void create_cocycle(Simplex_handle sigma, Arith_element x,
417 Arith_element charac) {
418 Simplex_key key = cpx_->key(sigma);
419 // Create a column containing only one cell,
420 Column * new_col = column_pool_.construct(key);
421 Cell * new_cell = cell_pool_.construct(key, x, new_col);
422 new_col->col_.push_back(*new_cell);
423 // and insert it in the matrix, in constant time thanks to the hint cam_.end().
424 // Indeed *new_col has the biggest lexicographic value because key is the
425 // biggest key used so far.
426 cam_.insert(cam_.end(), *new_col);
427 // Update the disjoint sets data structure.
428 Hcell * new_hcell = new Hcell;
429 new_hcell->push_back(*new_cell);
430 transverse_idx_[key] = cocycle(charac, new_hcell); // insert the new row
431 ds_repr_[key] = new_col;
432 }
433
434 /* \brief Destroy a cocycle class.
435 *
436 * The cocycle class is destroyed by the insertion of sigma.
437 * The methods proceeds to a reduction of the matrix representing
438 * the cohomology groups using Gauss pivoting. The reduction zeros-out
439 * the row containing the cell with highest key in
440 * a_ds, the annotation of the boundary of simplex sigma. This key
441 * is "death_key".*/
442 void destroy_cocycle(Simplex_handle sigma, A_ds_type const& a_ds,
443 Simplex_key death_key, Arith_element inv_x,
444 Arith_element charac) {
445 // Create a finite persistent interval for which the interval exists
446 if (interval_length_policy(cpx_->simplex(death_key), sigma)) {
447 persistent_pairs_.emplace_back(cpx_->simplex(death_key) // creator
448 , sigma // destructor
449 , charac); // fields
450 }
451
452 auto death_key_row = transverse_idx_.find(death_key); // Find the beginning of the row.
453 std::pair<typename Cam::iterator, bool> result_insert_cam;
454
455 auto row_cell_it = death_key_row->second.row_->begin();
456
457 while (row_cell_it != death_key_row->second.row_->end()) { // Traverse all cells in
458 // the row at index death_key.
459 Arith_element w = coeff_field_.times_minus(inv_x, row_cell_it->coefficient_);
460
461 if (w != coeff_field_.additive_identity()) {
462 Column * curr_col = row_cell_it->self_col_;
463 ++row_cell_it;
464 // Disconnect the column from the rows in the CAM.
465 for (auto& col_cell : curr_col->col_) {
466 col_cell.base_hook_cam_h::unlink();
467 }
468
469 // Remove the column from the CAM before modifying its value
470 cam_.erase(cam_.iterator_to(*curr_col));
471 // Proceed to the reduction of the column
472 plus_equal_column(*curr_col, a_ds, w);
473
474 if (curr_col->col_.empty()) { // If the column is null
475 ds_repr_[curr_col->class_key_] = NULL;
476 column_pool_.destroy(curr_col); // delete curr_col;
477 } else {
478 // Find whether the column obtained is already in the CAM
479 result_insert_cam = cam_.insert(*curr_col);
480 if (result_insert_cam.second) { // If it was not in the CAM before: insertion has succeeded
481 for (auto& col_cell : curr_col->col_) {
482 // re-establish the row links
483 transverse_idx_[col_cell.key_].row_->push_front(col_cell);
484 }
485 } else { // There is already an identical column in the CAM:
486 // merge two disjoint sets.
487 dsets_.link(curr_col->class_key_,
488 result_insert_cam.first->class_key_);
489
490 Simplex_key key_tmp = dsets_.find_set(curr_col->class_key_);
491 ds_repr_[key_tmp] = &(*(result_insert_cam.first));
492 result_insert_cam.first->class_key_ = key_tmp;
493 // intrusive containers don't own their elements, we have to release them manually
494 curr_col->col_.clear_and_dispose([&](Cell*p){cell_pool_.destroy(p);});
495 column_pool_.destroy(curr_col); // delete curr_col;
496 }
497 }
498 } else {
499 ++row_cell_it;
500 } // If w == 0, pass.
501 }
502
503 // Because it is a killer simplex, set the data of sigma to null_key().
504 if (charac == coeff_field_.characteristic()) {
505 cpx_->assign_key(sigma, cpx_->null_key());
506 }
507 if (death_key_row->second.characteristics_ == charac) {
508 delete death_key_row->second.row_;
509 transverse_idx_.erase(death_key_row);
510 } else {
511 death_key_row->second.characteristics_ /= charac;
512 }
513 }
514
515 /*
516 * Assign: target <- target + w * other.
517 */
518 void plus_equal_column(Column & target, A_ds_type const& other // value_type is pair<Simplex_key,Arith_element>
519 , Arith_element w) {
520 auto target_it = target.col_.begin();
521 auto other_it = other.begin();
522 while (target_it != target.col_.end() && other_it != other.end()) {
523 if (target_it->key_ < other_it->first) {
524 ++target_it;
525 } else {
526 if (target_it->key_ > other_it->first) {
527 Cell * cell_tmp = cell_pool_.construct(Cell(other_it->first // key
528 , coeff_field_.additive_identity(), &target));
529
530 cell_tmp->coefficient_ = coeff_field_.plus_times_equal(cell_tmp->coefficient_, other_it->second, w);
531
532 target.col_.insert(target_it, *cell_tmp);
533
534 ++other_it;
535 } else { // it1->key == it2->key
536 // target_it->coefficient_ <- target_it->coefficient_ + other_it->second * w
537 target_it->coefficient_ = coeff_field_.plus_times_equal(target_it->coefficient_, other_it->second, w);
538 if (target_it->coefficient_ == coeff_field_.additive_identity()) {
539 auto tmp_it = target_it;
540 ++target_it;
541 ++other_it; // iterators remain valid
542 Cell * tmp_cell_ptr = &(*tmp_it);
543 target.col_.erase(tmp_it); // removed from column
544
545 cell_pool_.destroy(tmp_cell_ptr); // delete from memory
546 } else {
547 ++target_it;
548 ++other_it;
549 }
550 }
551 }
552 }
553 while (other_it != other.end()) {
554 Cell * cell_tmp = cell_pool_.construct(Cell(other_it->first, coeff_field_.additive_identity(), &target));
555 cell_tmp->coefficient_ = coeff_field_.plus_times_equal(cell_tmp->coefficient_, other_it->second, w);
556 target.col_.insert(target.col_.end(), *cell_tmp);
557
558 ++other_it;
559 }
560 }
561
562 /*
563 * Compare two intervals by length.
564 */
565 struct cmp_intervals_by_length {
566 explicit cmp_intervals_by_length(FilteredComplex * sc)
567 : sc_(sc) {
568 }
569 bool operator()(const Persistent_interval & p1, const Persistent_interval & p2) {
570 return (sc_->filtration(get < 1 > (p1)) - sc_->filtration(get < 0 > (p1))
571 > sc_->filtration(get < 1 > (p2)) - sc_->filtration(get < 0 > (p2)));
572 }
573 FilteredComplex * sc_;
574 };
575
576 public:
587 void output_diagram(std::ostream& ostream = std::cout) {
588 cmp_intervals_by_length cmp(cpx_);
589 std::sort(std::begin(persistent_pairs_), std::end(persistent_pairs_), cmp);
590 for (auto pair : persistent_pairs_) {
591 ostream << get<2>(pair) << " " << cpx_->dimension(get<0>(pair)) << " "
592 << cpx_->filtration(get<0>(pair)) << " "
593 << cpx_->filtration(get<1>(pair)) << " " << std::endl;
594 }
595 }
596
597 void write_output_diagram(std::string diagram_name) {
598 std::ofstream diagram_out(diagram_name.c_str());
599 diagram_out.exceptions(diagram_out.failbit);
600 cmp_intervals_by_length cmp(cpx_);
601 std::sort(std::begin(persistent_pairs_), std::end(persistent_pairs_), cmp);
602 for (auto pair : persistent_pairs_) {
603 diagram_out << cpx_->dimension(get<0>(pair)) << " "
604 << cpx_->filtration(get<0>(pair)) << " "
605 << cpx_->filtration(get<1>(pair)) << std::endl;
606 }
607 }
608
612 std::vector<int> betti_numbers() const {
613 // Init Betti numbers vector with zeros until Simplicial complex dimension and don't allocate a vector of negative
614 // size for an empty complex
615 std::vector<int> betti_numbers(std::max(dim_max_, 0));
616
617 for (auto pair : persistent_pairs_) {
618 // Count never ended persistence intervals
619 if (cpx_->null_simplex() == get<1>(pair)) {
620 // Increment corresponding betti number
621 betti_numbers[cpx_->dimension(get<0>(pair))] += 1;
622 }
623 }
624 return betti_numbers;
625 }
626
632 int betti_number(int dimension) const {
633 int betti_number = 0;
634
635 for (auto pair : persistent_pairs_) {
636 // Count never ended persistence intervals
637 if (cpx_->null_simplex() == get<1>(pair)) {
638 if (cpx_->dimension(get<0>(pair)) == dimension) {
639 // Increment betti number found
640 ++betti_number;
641 }
642 }
643 }
644 return betti_number;
645 }
646
653 // Init Betti numbers vector with zeros until Simplicial complex dimension and don't allocate a vector of negative
654 // size for an empty complex
655 std::vector<int> betti_numbers(std::max(dim_max_, 0));
656 for (auto pair : persistent_pairs_) {
657 // Count persistence intervals that covers the given interval
658 // null_simplex test : if the function is called with to=+infinity, we still get something useful. And it will
659 // still work if we change the complex filtration function to reject null simplices.
660 if (cpx_->filtration(get<0>(pair)) <= from &&
661 (get<1>(pair) == cpx_->null_simplex() || cpx_->filtration(get<1>(pair)) > to)) {
662 // Increment corresponding betti number
663 betti_numbers[cpx_->dimension(get<0>(pair))] += 1;
664 }
665 }
666 return betti_numbers;
667 }
668
675 int persistent_betti_number(int dimension, Filtration_value from, Filtration_value to) const {
676 int betti_number = 0;
677
678 for (auto pair : persistent_pairs_) {
679 // Count persistence intervals that covers the given interval
680 // null_simplex test : if the function is called with to=+infinity, we still get something useful. And it will
681 // still work if we change the complex filtration function to reject null simplices.
682 if (cpx_->filtration(get<0>(pair)) <= from &&
683 (get<1>(pair) == cpx_->null_simplex() || cpx_->filtration(get<1>(pair)) > to)) {
684 if (cpx_->dimension(get<0>(pair)) == dimension) {
685 // Increment betti number found
686 ++betti_number;
687 }
688 }
689 }
690 return betti_number;
691 }
692
696 const std::vector<Persistent_interval>& get_persistent_pairs() const {
697 return persistent_pairs_;
698 }
699
704 std::vector< std::pair< Filtration_value , Filtration_value > >
705 intervals_in_dimension(int dimension) {
706 std::vector< std::pair< Filtration_value , Filtration_value > > result;
707 // auto && pair, to avoid unnecessary copying
708 for (auto && pair : persistent_pairs_) {
709 if (cpx_->dimension(get<0>(pair)) == dimension) {
710 result.emplace_back(cpx_->filtration(get<0>(pair)), cpx_->filtration(get<1>(pair)));
711 }
712 }
713 return result;
714 }
715
716 private:
717 /*
718 * Structure representing a cocycle.
719 */
720 struct cocycle {
721 cocycle()
722 : row_(nullptr),
723 characteristics_() {
724 }
725 cocycle(Arith_element characteristics, Hcell * row)
726 : row_(row),
727 characteristics_(characteristics) {
728 }
729
730 Hcell * row_; // points to the corresponding row in the CAM
731 Arith_element characteristics_; // product of field characteristics for which the cocycle exist
732 };
733
734 public:
735 FilteredComplex * cpx_;
736 int dim_max_;
737 CoefficientField coeff_field_;
738 size_t num_simplices_;
739
740 /* Disjoint sets data structure to link the model of FilteredComplex
741 * with the compressed annotation matrix.
742 * ds_rank_ is a property map Simplex_key -> int, ds_parent_ is a property map
743 * Simplex_key -> simplex_key_t */
744 std::vector<int> ds_rank_;
745 std::vector<Simplex_key> ds_parent_;
746 std::vector<Column *> ds_repr_;
747 boost::disjoint_sets<int *, Simplex_key *> dsets_;
748 /* The compressed annotation matrix fields.*/
749 Cam cam_;
750 /* Dictionary establishing the correspondence between the Simplex_key of
751 * the root vertex in the union-find ds and the Simplex_key of the vertex which
752 * created the connected component as a 0-dimension homology feature.*/
753 std::unordered_map<Simplex_key, Simplex_key> zero_cocycles_;
754 /* Key -> row. */
755 std::map<Simplex_key, cocycle> transverse_idx_;
756 /* Persistent intervals. */
757 std::vector<Persistent_interval> persistent_pairs_;
758 length_interval interval_length_policy;
759
760 Simple_object_pool<Column> column_pool_;
761 Simple_object_pool<Cell> cell_pool_;
762};
763
764} // namespace persistent_cohomology
765
766} // namespace Gudhi
767
768#endif // PERSISTENT_COHOMOLOGY_H_
Computes the persistent cohomology of a filtered complex.
Definition Persistent_cohomology.h:54
std::vector< int > persistent_betti_numbers(Filtration_value from, Filtration_value to) const
Returns the persistent Betti numbers.
Definition Persistent_cohomology.h:652
std::vector< std::pair< Filtration_value, Filtration_value > > intervals_in_dimension(int dimension)
Returns persistence intervals for a given dimension.
Definition Persistent_cohomology.h:705
std::vector< int > betti_numbers() const
Returns Betti numbers.
Definition Persistent_cohomology.h:612
void output_diagram(std::ostream &ostream=std::cout)
Output the persistence diagram in ostream.
Definition Persistent_cohomology.h:587
std::tuple< Simplex_handle, Simplex_handle, Arith_element > Persistent_interval
Definition Persistent_cohomology.h:68
Simplex_tree::Simplex_handle Simplex_handle
Definition Persistent_cohomology.h:61
Persistent_cohomology(FilteredComplex &cpx, bool persistence_dim_max=false)
Initializes the Persistent_cohomology class.
Definition Persistent_cohomology.h:97
int persistent_betti_number(int dimension, Filtration_value from, Filtration_value to) const
Returns the persistent Betti number of the dimension passed by parameter.
Definition Persistent_cohomology.h:675
int betti_number(int dimension) const
Returns the Betti number of the dimension passed by parameter.
Definition Persistent_cohomology.h:632
Simplex_tree::Simplex_key Simplex_key
Definition Persistent_cohomology.h:59
void compute_persistent_cohomology(Filtration_value min_interval_length=0)
Compute the persistent homology of the filtered simplicial complex.
Definition Persistent_cohomology.h:168
void init_coefficients(int charac)
Initializes the coefficient field.
Definition Persistent_cohomology.h:152
void init_coefficients(int charac_min, int charac_max)
Initializes the coefficient field for multi-field persistent homology.
Definition Persistent_cohomology.h:156
Field_Zp::Element Arith_element
Definition Persistent_cohomology.h:65
Simplex_tree::Filtration_value Filtration_value
Definition Persistent_cohomology.h:63
const std::vector< Persistent_interval > & get_persistent_pairs() const
Returns a list of persistence birth and death FilteredComplex::Simplex_handle pairs.
Definition Persistent_cohomology.h:696
Gudhi namespace.
Definition SimplicialComplexForAlpha.h:14
unspecified Element
Type of element of the field.
Definition CoefficientField.h:19
The concept FilteredComplex describes the requirements for a type to implement a filtered cell comple...
Definition FilteredComplex.h:17
unspecified Simplex_key
Data stored for each simplex.
Definition FilteredComplex.h:91
Filtration_value filtration(Simplex_handle sh)
Returns the filtration value of a simplex.
void assign_key(Simplex_handle sh, Simplex_key n)
Store a number for a simplex, which can later be retrieved with key(sh).
Simplex_handle null_simplex()
Returns a Simplex_handle that is different from all simplex handles of the simplices.
Filtration_simplex_range filtration_simplex_range()
Returns a range over the simplices of the complex in the order of the filtration.
unspecified Simplex_handle
Handle to specify a simplex.
Definition FilteredComplex.h:19
Simplex_key key(Simplex_handle sh)
Returns the number stored for a simplex by assign_key.
unspecified Filtration_value
Type for the value of the filtration function.
Definition FilteredComplex.h:23
int dimension(Simplex_handle sh)
Returns the dimension of a simplex.
Simplex_handle simplex(size_t idx)
Returns the simplex that has index idx in the filtration.