11#ifndef PERSISTENT_COHOMOLOGY_H_
12#define PERSISTENT_COHOMOLOGY_H_
14#include <gudhi/Persistent_cohomology/Persistent_cohomology_column.h>
15#include <gudhi/Persistent_cohomology/Field_Zp.h>
16#include <gudhi/Simple_object_pool.h>
18#include <boost/intrusive/set.hpp>
19#include <boost/pending/disjoint_sets.hpp>
20#include <boost/intrusive/list.hpp>
24#include <unordered_map>
38namespace persistent_cohomology {
53template<
class FilteredComplex,
class CoefficientField>
73 typedef Persistent_cohomology_column<Simplex_key, Arith_element> Column;
75 typedef typename Column::Cell Cell;
77 typedef boost::intrusive::list<Cell,
78 boost::intrusive::constant_time_size<false>,
79 boost::intrusive::base_hook<base_hook_cam_h> > Hcell;
81 typedef boost::intrusive::set<Column,
82 boost::intrusive::constant_time_size<false> > Cam;
84 typedef std::vector<std::pair<Simplex_key, Arith_element> > A_ds_type;
99 dim_max_(cpx.dimension()),
101 num_simplices_(cpx_->num_simplices()),
102 ds_rank_(num_simplices_),
103 ds_parent_(num_simplices_),
104 ds_repr_(num_simplices_, NULL),
105 dsets_(ds_rank_.data(), ds_parent_.data()),
110 interval_length_policy(&cpx, 0),
113 if (num_simplices_ > std::numeric_limits<Simplex_key>::max()) {
115 throw std::out_of_range(
"The number of simplices is more than Simplex_key type numeric limit.");
117 if (persistence_dim_max) {
124 for (
auto & transverse_ref : transverse_idx_) {
126 transverse_ref.second.row_->clear_and_dispose([&](Cell*p){p->~Cell();});
127 delete transverse_ref.second.row_;
132 struct length_interval {
135 min_length_(min_length) {
138 bool operator()(Simplex_handle sh1, Simplex_handle sh2) {
139 return cpx_->filtration(sh2) - cpx_->filtration(sh1) > min_length_;
143 min_length_ = new_length;
153 coeff_field_.init(charac);
157 coeff_field_.init(charac_min, charac_max);
172 interval_length_policy.set_length(min_interval_length);
174 std::vector<Simplex_key> vertices;
178 dsets_.make_set(cpx_->
key(sh));
180 switch (dim_simplex) {
182 vertices.push_back(idx_fil);
185 update_cohomology_groups_edge(sh);
188 update_cohomology_groups(sh, dim_simplex);
194 if (ds_parent_[key] == key
195 && zero_cocycles_.find(key) == zero_cocycles_.end()) {
196 persistent_pairs_.emplace_back(
200 for (
auto zero_idx : zero_cocycles_) {
201 persistent_pairs_.emplace_back(
205 for (
auto cocycle : transverse_idx_) {
206 persistent_pairs_.emplace_back(
216 void update_cohomology_groups_edge(Simplex_handle sigma) {
218 boost::tie(u, v) = cpx_->endpoints(sigma);
228 auto map_it_u = zero_cocycles_.find(ku);
230 if (map_it_u == zero_cocycles_.end()) {
233 idx_coc_u = map_it_u->second;
236 auto map_it_v = zero_cocycles_.find(kv);
238 if (map_it_v == zero_cocycles_.end()) {
241 idx_coc_v = map_it_v->second;
246 if (interval_length_policy(cpx_->
simplex(idx_coc_v), sigma)) {
247 persistent_pairs_.emplace_back(
251 if (kv != idx_coc_v) {
252 zero_cocycles_.erase(map_it_v);
254 if (kv == dsets_.find_set(kv)) {
255 if (ku != idx_coc_u) {
256 zero_cocycles_.erase(map_it_u);
258 zero_cocycles_[kv] = idx_coc_u;
261 if (interval_length_policy(cpx_->
simplex(idx_coc_u), sigma)) {
262 persistent_pairs_.emplace_back(
266 if (ku != idx_coc_u) {
267 zero_cocycles_.erase(map_it_u);
269 if (ku == dsets_.find_set(ku)) {
270 if (kv != idx_coc_v) {
271 zero_cocycles_.erase(map_it_v);
273 zero_cocycles_[ku] = idx_coc_v;
277 }
else if (dim_max_ > 1) {
285 void annotation_of_the_boundary(
286 std::map<Simplex_key, Arith_element> & map_a_ds, Simplex_handle sigma,
291 typedef std::pair<Column *, int> annotation_t;
292 thread_local std::vector<annotation_t> annotations_in_boundary;
293 annotations_in_boundary.clear();
294 int sign = 1 - 2 * (dim_sigma % 2);
303 curr_col = ds_repr_[dsets_.find_set(key)];
304 if (curr_col != NULL) {
305 annotations_in_boundary.emplace_back(curr_col, sign);
311 std::sort(annotations_in_boundary.begin(), annotations_in_boundary.end(),
312 [](annotation_t
const& a, annotation_t
const& b) { return a.first < b.first; });
316 std::pair<typename std::map<Simplex_key, Arith_element>::iterator,
bool> result_insert_a_ds;
318 for (
auto ann_it = annotations_in_boundary.begin(); ann_it != annotations_in_boundary.end(); ) {
319 Column* col = ann_it->first;
320 int mult = ann_it->second;
321 while (++ann_it != annotations_in_boundary.end() && ann_it->first == col) {
322 mult += ann_it->second;
326 for (
auto cell_ref : col->col_) {
327 Arith_element w_y = coeff_field_.times(cell_ref.coefficient_, mult);
330 result_insert_a_ds = map_a_ds.insert(std::pair<Simplex_key, Arith_element>(cell_ref.key_, w_y));
331 if (!(result_insert_a_ds.second)) {
332 result_insert_a_ds.first->second = coeff_field_.
plus_equal(result_insert_a_ds.first->second, w_y);
334 map_a_ds.erase(result_insert_a_ds.first);
346 void update_cohomology_groups(Simplex_handle sigma,
int dim_sigma) {
348 std::map<Simplex_key, Arith_element> map_a_ds;
349 annotation_of_the_boundary(map_a_ds, sigma, dim_sigma);
351 if (map_a_ds.empty()) {
352 if (dim_sigma < dim_max_) {
359 for (
auto map_a_ds_ref : map_a_ds) {
361 std::pair<Simplex_key, Arith_element>(map_a_ds_ref.first,
362 map_a_ds_ref.second));
367 for (
auto a_ds_rit = a_ds.rbegin();
368 (a_ds_rit != a_ds.rend())
370 std::tie(inv_x, charac) = coeff_field_.inverse(a_ds_rit->second, prod);
373 destroy_cocycle(sigma, a_ds, a_ds_rit->first, inv_x, charac);
378 && dim_sigma < dim_max_) {
395 Column * new_col = column_pool_.construct(key);
396 Cell * new_cell = cell_pool_.construct(key, x, new_col);
397 new_col->col_.push_back(*new_cell);
401 cam_.insert(cam_.end(), *new_col);
403 Hcell * new_hcell =
new Hcell;
404 new_hcell->push_back(*new_cell);
405 transverse_idx_[key] = cocycle(charac, new_hcell);
406 ds_repr_[key] = new_col;
417 void destroy_cocycle(Simplex_handle sigma, A_ds_type
const& a_ds,
421 if (interval_length_policy(cpx_->
simplex(death_key), sigma)) {
422 persistent_pairs_.emplace_back(cpx_->
simplex(death_key)
427 auto death_key_row = transverse_idx_.find(death_key);
428 std::pair<typename Cam::iterator, bool> result_insert_cam;
430 auto row_cell_it = death_key_row->second.row_->begin();
432 while (row_cell_it != death_key_row->second.row_->end()) {
434 Arith_element w = coeff_field_.times_minus(inv_x, row_cell_it->coefficient_);
437 Column * curr_col = row_cell_it->self_col_;
440 for (
auto& col_cell : curr_col->col_) {
441 col_cell.base_hook_cam_h::unlink();
445 cam_.erase(cam_.iterator_to(*curr_col));
447 plus_equal_column(*curr_col, a_ds, w);
449 if (curr_col->col_.empty()) {
450 ds_repr_[curr_col->class_key_] = NULL;
451 column_pool_.destroy(curr_col);
454 result_insert_cam = cam_.insert(*curr_col);
455 if (result_insert_cam.second) {
456 for (
auto& col_cell : curr_col->col_) {
458 transverse_idx_[col_cell.key_].row_->push_front(col_cell);
462 dsets_.link(curr_col->class_key_,
463 result_insert_cam.first->class_key_);
465 Simplex_key key_tmp = dsets_.find_set(curr_col->class_key_);
466 ds_repr_[key_tmp] = &(*(result_insert_cam.first));
467 result_insert_cam.first->class_key_ = key_tmp;
469 curr_col->col_.clear_and_dispose([&](Cell*p){cell_pool_.destroy(p);});
470 column_pool_.destroy(curr_col);
482 if (death_key_row->second.characteristics_ == charac) {
483 delete death_key_row->second.row_;
484 transverse_idx_.erase(death_key_row);
486 death_key_row->second.characteristics_ /= charac;
493 void plus_equal_column(Column & target, A_ds_type
const& other
495 auto target_it = target.col_.begin();
496 auto other_it = other.begin();
497 while (target_it != target.col_.end() && other_it != other.end()) {
498 if (target_it->key_ < other_it->first) {
501 if (target_it->key_ > other_it->first) {
502 Cell * cell_tmp = cell_pool_.construct(Cell(other_it->first
505 cell_tmp->coefficient_ = coeff_field_.plus_times_equal(cell_tmp->coefficient_, other_it->second, w);
507 target.col_.insert(target_it, *cell_tmp);
512 target_it->coefficient_ = coeff_field_.plus_times_equal(target_it->coefficient_, other_it->second, w);
514 auto tmp_it = target_it;
517 Cell * tmp_cell_ptr = &(*tmp_it);
518 target.col_.erase(tmp_it);
520 cell_pool_.destroy(tmp_cell_ptr);
528 while (other_it != other.end()) {
529 Cell * cell_tmp = cell_pool_.construct(Cell(other_it->first, coeff_field_.
additive_identity(), &target));
530 cell_tmp->coefficient_ = coeff_field_.plus_times_equal(cell_tmp->coefficient_, other_it->second, w);
531 target.col_.insert(target.col_.end(), *cell_tmp);
540 struct cmp_intervals_by_length {
545 return (sc_->filtration(get < 1 > (p1)) - sc_->filtration(get < 0 > (p1))
546 > sc_->filtration(get < 1 > (p2)) - sc_->filtration(get < 0 > (p2)));
563 cmp_intervals_by_length cmp(cpx_);
564 std::sort(std::begin(persistent_pairs_), std::end(persistent_pairs_), cmp);
565 for (
auto pair : persistent_pairs_) {
566 ostream << get<2>(pair) <<
" " << cpx_->
dimension(get<0>(pair)) <<
" "
568 << cpx_->
filtration(get<1>(pair)) <<
" " << std::endl;
572 void write_output_diagram(std::string diagram_name) {
573 std::ofstream diagram_out(diagram_name.c_str());
574 diagram_out.exceptions(diagram_out.failbit);
575 cmp_intervals_by_length cmp(cpx_);
576 std::sort(std::begin(persistent_pairs_), std::end(persistent_pairs_), cmp);
577 for (
auto pair : persistent_pairs_) {
578 diagram_out << cpx_->
dimension(get<0>(pair)) <<
" "
580 << cpx_->
filtration(get<1>(pair)) << std::endl;
592 for (
auto pair : persistent_pairs_) {
610 for (
auto pair : persistent_pairs_) {
613 if (cpx_->
dimension(get<0>(pair)) == dimension) {
631 for (
auto pair : persistent_pairs_) {
653 for (
auto pair : persistent_pairs_) {
659 if (cpx_->
dimension(get<0>(pair)) == dimension) {
672 return persistent_pairs_;
679 std::vector< std::pair< Filtration_value , Filtration_value > >
681 std::vector< std::pair< Filtration_value , Filtration_value > > result;
683 for (
auto && pair : persistent_pairs_) {
684 if (cpx_->
dimension(get<0>(pair)) == dimension) {
702 characteristics_(characteristics) {
713 size_t num_simplices_;
719 std::vector<int> ds_rank_;
720 std::vector<Simplex_key> ds_parent_;
721 std::vector<Column *> ds_repr_;
722 boost::disjoint_sets<int *, Simplex_key *> dsets_;
728 std::unordered_map<Simplex_key, Simplex_key> zero_cocycles_;
730 std::map<Simplex_key, cocycle> transverse_idx_;
732 std::vector<Persistent_interval> persistent_pairs_;
733 length_interval interval_length_policy;
735 Simple_object_pool<Column> column_pool_;
736 Simple_object_pool<Cell> cell_pool_;
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:627
std::vector< std::pair< Filtration_value, Filtration_value > > intervals_in_dimension(int dimension)
Returns persistence intervals for a given dimension.
Definition: Persistent_cohomology.h:680
std::vector< int > betti_numbers() const
Returns Betti numbers.
Definition: Persistent_cohomology.h:587
void output_diagram(std::ostream &ostream=std::cout)
Output the persistence diagram in ostream.
Definition: Persistent_cohomology.h:562
std::tuple< Simplex_handle, Simplex_handle, Arith_element > Persistent_interval
Type for birth and death FilteredComplex::Simplex_handle. The Arith_element field is used for the mul...
Definition: Persistent_cohomology.h:68
FilteredComplex::Simplex_handle Simplex_handle
Handle to specify a simplex.
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:650
int betti_number(int dimension) const
Returns the Betti number of the dimension passed by parameter.
Definition: Persistent_cohomology.h:607
FilteredComplex::Simplex_key Simplex_key
Data stored for each simplex.
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
CoefficientField::Element Arith_element
Type of element of the field.
Definition: Persistent_cohomology.h:65
FilteredComplex::Filtration_value Filtration_value
Type for the value of the filtration function.
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:671
Gudhi namespace.
Definition: SimplicialComplexForAlpha.h:14
Concept describing the requirements for a class to represent a field of coefficients to compute persi...
Definition: CoefficientField.h:14
Element additive_identity()
Element multiplicative_identity()
unspecified Element
Type of element of the field.
Definition: CoefficientField.h:19
void plus_equal(Element x, Element y)
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 null_key()
Returns a constant dummy number that is either negative, or at least as large as num_simplices()....
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.
Boundary_simplex_range boundary_simplex_range(Simplex_handle sh)
Returns a range giving access to all simplices of the boundary of a simplex, i.e. the set of codimens...
Value type for a filtration function on a cell complex.
Definition: FiltrationValue.h:20