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) {
139 return cpx_->filtration(sh2) - cpx_->filtration(sh1) > min_length_;
143 min_length_ = new_length;
146 FilteredComplex * cpx_;
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;
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) {
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(
197 cpx_->simplex(key), cpx_->null_simplex(), coeff_field_.characteristic());
200 for (
auto zero_idx : zero_cocycles_) {
201 persistent_pairs_.emplace_back(
202 cpx_->simplex(zero_idx.second), cpx_->null_simplex(), coeff_field_.characteristic());
205 for (
auto cocycle : transverse_idx_) {
206 persistent_pairs_.emplace_back(
207 cpx_->simplex(cocycle.first), cpx_->null_simplex(), cocycle.second.characteristics_);
216 void compute_persistent_cohomology_without_optimizations(Filtration_value min_interval_length = 0) {
220 interval_length_policy.set_length(min_interval_length);
225 dsets_.make_set(cpx_->
key(sh));
227 update_cohomology_groups(sh, dim_simplex);
230 for (
auto cocycle : transverse_idx_) {
231 persistent_pairs_.emplace_back(
243 boost::tie(u, v) = cpx_->endpoints(sigma);
253 auto map_it_u = zero_cocycles_.find(ku);
255 if (map_it_u == zero_cocycles_.end()) {
258 idx_coc_u = map_it_u->second;
261 auto map_it_v = zero_cocycles_.find(kv);
263 if (map_it_v == zero_cocycles_.end()) {
266 idx_coc_v = map_it_v->second;
269 if (cpx_->filtration(cpx_->simplex(idx_coc_u))
270 < cpx_->filtration(cpx_->simplex(idx_coc_v))) {
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());
276 if (kv != idx_coc_v) {
277 zero_cocycles_.erase(map_it_v);
279 if (kv == dsets_.find_set(kv)) {
280 if (ku != idx_coc_u) {
281 zero_cocycles_.erase(map_it_u);
283 zero_cocycles_[kv] = idx_coc_u;
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());
291 if (ku != idx_coc_u) {
292 zero_cocycles_.erase(map_it_u);
294 if (ku == dsets_.find_set(ku)) {
295 if (kv != idx_coc_v) {
296 zero_cocycles_.erase(map_it_v);
298 zero_cocycles_[ku] = idx_coc_v;
301 cpx_->assign_key(sigma, cpx_->null_key());
302 }
else if (dim_max_ > 1) {
303 create_cocycle(sigma, coeff_field_.multiplicative_identity(), coeff_field_.characteristic());
310 void annotation_of_the_boundary(
311 std::map<Simplex_key, Arith_element> & map_a_ds,
Simplex_handle sigma,
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);
324 for (
auto sh : cpx_->boundary_simplex_range(sigma)) {
326 if (key != cpx_->null_key()) {
328 curr_col = ds_repr_[dsets_.find_set(key)];
329 if (curr_col != NULL) {
330 annotations_in_boundary.emplace_back(curr_col, sign);
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; });
341 std::pair<typename std::map<Simplex_key, Arith_element>::iterator,
bool> result_insert_a_ds;
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;
350 if (mult != coeff_field_.additive_identity()) {
351 for (
auto cell_ref : col->col_) {
352 Arith_element w_y = coeff_field_.times(cell_ref.coefficient_, mult);
354 if (w_y != coeff_field_.additive_identity()) {
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)) {
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);
371 void update_cohomology_groups(
Simplex_handle sigma,
int dim_sigma) {
373 std::map<Simplex_key, Arith_element> map_a_ds;
374 annotation_of_the_boundary(map_a_ds, sigma, dim_sigma);
376 if (map_a_ds.empty()) {
377 if (dim_sigma < dim_max_) {
378 create_cocycle(sigma, coeff_field_.multiplicative_identity(),
379 coeff_field_.characteristic());
384 for (
auto map_a_ds_ref : map_a_ds) {
386 std::pair<Simplex_key, Arith_element>(map_a_ds_ref.first,
387 map_a_ds_ref.second));
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);
397 if (inv_x != coeff_field_.additive_identity()) {
398 destroy_cocycle(sigma, a_ds, a_ds_rit->first, inv_x, charac);
402 if (prod != coeff_field_.multiplicative_identity()
403 && dim_sigma < dim_max_) {
404 create_cocycle(sigma, coeff_field_.multiplicative_identity(prod), prod);
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);
426 cam_.insert(cam_.end(), *new_col);
428 Hcell * new_hcell =
new Hcell;
429 new_hcell->push_back(*new_cell);
430 transverse_idx_[key] = cocycle(charac, new_hcell);
431 ds_repr_[key] = new_col;
446 if (interval_length_policy(cpx_->simplex(death_key), sigma)) {
447 persistent_pairs_.emplace_back(cpx_->simplex(death_key)
452 auto death_key_row = transverse_idx_.find(death_key);
453 std::pair<typename Cam::iterator, bool> result_insert_cam;
455 auto row_cell_it = death_key_row->second.row_->begin();
457 while (row_cell_it != death_key_row->second.row_->end()) {
459 Arith_element w = coeff_field_.times_minus(inv_x, row_cell_it->coefficient_);
461 if (w != coeff_field_.additive_identity()) {
462 Column * curr_col = row_cell_it->self_col_;
465 for (
auto& col_cell : curr_col->col_) {
466 col_cell.base_hook_cam_h::unlink();
470 cam_.erase(cam_.iterator_to(*curr_col));
472 plus_equal_column(*curr_col, a_ds, w);
474 if (curr_col->col_.empty()) {
475 ds_repr_[curr_col->class_key_] = NULL;
476 column_pool_.destroy(curr_col);
479 result_insert_cam = cam_.insert(*curr_col);
480 if (result_insert_cam.second) {
481 for (
auto& col_cell : curr_col->col_) {
483 transverse_idx_[col_cell.key_].row_->push_front(col_cell);
487 dsets_.link(curr_col->class_key_,
488 result_insert_cam.first->class_key_);
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;
494 curr_col->col_.clear_and_dispose([&](Cell*p){cell_pool_.destroy(p);});
495 column_pool_.destroy(curr_col);
504 if (charac == coeff_field_.characteristic()) {
505 cpx_->assign_key(sigma, cpx_->null_key());
507 if (death_key_row->second.characteristics_ == charac) {
508 delete death_key_row->second.row_;
509 transverse_idx_.erase(death_key_row);
511 death_key_row->second.characteristics_ /= charac;
518 void plus_equal_column(Column & target, A_ds_type
const& other
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) {
526 if (target_it->key_ > other_it->first) {
527 Cell * cell_tmp = cell_pool_.construct(Cell(other_it->first
528 , coeff_field_.additive_identity(), &target));
530 cell_tmp->coefficient_ = coeff_field_.plus_times_equal(cell_tmp->coefficient_, other_it->second, w);
532 target.col_.insert(target_it, *cell_tmp);
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;
542 Cell * tmp_cell_ptr = &(*tmp_it);
543 target.col_.erase(tmp_it);
545 cell_pool_.destroy(tmp_cell_ptr);
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);
565 struct cmp_intervals_by_length {
566 explicit cmp_intervals_by_length(FilteredComplex * sc)
573 FilteredComplex * sc_;
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;
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)) <<
" "
605 << cpx_->
filtration(get<1>(pair)) << std::endl;
617 for (
auto pair : persistent_pairs_) {
619 if (cpx_->null_simplex() == get<1>(pair)) {
635 for (
auto pair : persistent_pairs_) {
637 if (cpx_->null_simplex() == get<1>(pair)) {
638 if (cpx_->dimension(get<0>(pair)) == dimension) {
656 for (
auto pair : persistent_pairs_) {
660 if (cpx_->filtration(get<0>(pair)) <= from &&
661 (get<1>(pair) == cpx_->null_simplex() || cpx_->filtration(get<1>(pair)) > to)) {
678 for (
auto pair : persistent_pairs_) {
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) {
697 return persistent_pairs_;
704 std::vector< std::pair< Filtration_value , Filtration_value > >
706 std::vector< std::pair< Filtration_value , Filtration_value > > result;
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)));
727 characteristics_(characteristics) {
735 FilteredComplex * cpx_;
737 CoefficientField coeff_field_;
738 size_t num_simplices_;
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_;
753 std::unordered_map<Simplex_key, Simplex_key> zero_cocycles_;
755 std::map<Simplex_key, cocycle> transverse_idx_;
757 std::vector<Persistent_interval> persistent_pairs_;
758 length_interval interval_length_policy;
760 Simple_object_pool<Column> column_pool_;
761 Simple_object_pool<Cell> cell_pool_;