17#ifndef PM_BOUNDARY_MATRIX_H
18#define PM_BOUNDARY_MATRIX_H
40template <
class Master_matrix>
42 public Master_matrix::template Base_swap_option<Boundary_matrix<Master_matrix> >,
43 public Master_matrix::Base_pairing_option,
44 protected Master_matrix::Matrix_row_access_option
47 using Dim_opt =
typename Master_matrix::Matrix_dimension_option;
48 using Swap_opt =
typename Master_matrix::template Base_swap_option<Boundary_matrix<Master_matrix> >;
49 using Pair_opt =
typename Master_matrix::Base_pairing_option;
50 using RA_opt =
typename Master_matrix::Matrix_row_access_option;
52 static constexpr bool activeDimOption_ =
53 Master_matrix::Option_list::has_matrix_maximal_dimension_access || Master_matrix::maxDimensionIsNeeded;
54 static constexpr bool activeSwapOption_ =
55 Master_matrix::Option_list::has_column_and_row_swaps || Master_matrix::Option_list::has_vine_update;
56 static constexpr bool activePairingOption_ = Master_matrix::Option_list::has_column_pairings &&
57 !Master_matrix::Option_list::has_vine_update &&
58 !Master_matrix::Option_list::can_retrieve_representative_cycles;
61 using Index =
typename Master_matrix::Index;
62 using ID_index =
typename Master_matrix::ID_index;
63 using Dimension =
typename Master_matrix::Dimension;
69 using Column =
typename Master_matrix::Column;
70 using Boundary =
typename Master_matrix::Boundary;
71 using Row =
typename Master_matrix::Row;
106 template <
class Boundary_range = Boundary>
158 template <
class Boundary_range = Boundary>
160 Dimension dim = Master_matrix::template get_null_value<Dimension>());
179 template <
class Boundary_range = Boundary>
181 const Boundary_range& boundary,
182 Dimension dim = Master_matrix::template get_null_value<Dimension>());
338 if constexpr (activeDimOption_) Dim_opt::_reset();
339 if constexpr (activeSwapOption_) Swap_opt::_reset();
340 if constexpr (activePairingOption_) Pair_opt::_reset();
342 nextInsertIndex_ = 0;
343 colSettings_ = colSettings;
360 swap(
static_cast<Dim_opt&
>(matrix1),
static_cast<Dim_opt&
>(matrix2));
361 swap(
static_cast<Swap_opt&
>(matrix1),
static_cast<Swap_opt&
>(matrix2));
362 swap(
static_cast<Pair_opt&
>(matrix1),
static_cast<Pair_opt&
>(matrix2));
363 matrix1.matrix_.swap(matrix2.matrix_);
364 std::swap(matrix1.nextInsertIndex_, matrix2.nextInsertIndex_);
365 std::swap(matrix1.colSettings_, matrix2.colSettings_);
367 if constexpr (Master_matrix::Option_list::has_row_access) {
368 swap(
static_cast<RA_opt&
>(matrix1),
static_cast<RA_opt&
>(matrix2));
375 using Column_container =
typename Master_matrix::Column_container;
380 Column_container matrix_;
381 Index nextInsertIndex_;
384 void _orderRowsIfNecessary();
385 const Column& _get_column(Index columnIndex)
const;
386 Column& _get_column(Index columnIndex);
387 Index _get_real_row_index(Index rowIndex)
const;
388 template <
class Container>
389 void _container_insert(
const Container& column, Index pos, Dimension dim);
390 void _container_insert(
const Column& column, [[maybe_unused]] Index pos = 0);
393template <
class Master_matrix>
395 : Dim_opt(Master_matrix::template get_null_value<
Dimension>()),
400 colSettings_(colSettings)
403template <
class Master_matrix>
404template <
class Boundary_range>
407 : Dim_opt(Master_matrix::template get_null_value<
Dimension>()),
408 Swap_opt(orderedBoundaries.size()),
410 RA_opt(orderedBoundaries.size()),
411 nextInsertIndex_(orderedBoundaries.size()),
412 colSettings_(colSettings)
414 matrix_.reserve(orderedBoundaries.size());
416 for (
Index i = 0; i < orderedBoundaries.size(); i++) {
417 _container_insert(orderedBoundaries[i], i, orderedBoundaries[i].size() == 0 ? 0 : orderedBoundaries[i].size() - 1);
421template <
class Master_matrix>
423 : Dim_opt(Master_matrix::template get_null_value<
Dimension>()),
424 Swap_opt(numberOfColumns),
426 RA_opt(numberOfColumns),
427 matrix_(!Master_matrix::Option_list::has_map_column_container && Master_matrix::Option_list::has_row_access
431 colSettings_(colSettings)
433 if constexpr (!Master_matrix::Option_list::has_map_column_container && Master_matrix::Option_list::has_row_access)
434 matrix_.reserve(numberOfColumns);
437template <
class Master_matrix>
440 : Dim_opt(static_cast<const Dim_opt&>(matrixToCopy)),
441 Swap_opt(static_cast<const Swap_opt&>(matrixToCopy)),
442 Pair_opt(static_cast<const Pair_opt&>(matrixToCopy)),
443 RA_opt(static_cast<const RA_opt&>(matrixToCopy)),
444 nextInsertIndex_(matrixToCopy.nextInsertIndex_),
445 colSettings_(colSettings == nullptr ? matrixToCopy.colSettings_ : colSettings)
447 matrix_.reserve(matrixToCopy.matrix_.size());
448 for (
const auto& cont : matrixToCopy.matrix_) {
449 if constexpr (Master_matrix::Option_list::has_map_column_container) {
450 _container_insert(cont.second, cont.first);
452 _container_insert(cont);
457template <
class Master_matrix>
459 : Dim_opt(std::move(
static_cast<Dim_opt&
>(other))),
460 Swap_opt(std::move(
static_cast<Swap_opt&
>(other))),
461 Pair_opt(std::move(
static_cast<Pair_opt&
>(other))),
462 RA_opt(std::move(
static_cast<RA_opt&
>(other))),
463 matrix_(std::move(other.matrix_)),
464 nextInsertIndex_(std::exchange(other.nextInsertIndex_, 0)),
465 colSettings_(std::exchange(other.colSettings_,
nullptr))
469template <
class Master_matrix>
470template <
class Boundary_range>
472 const Boundary_range& boundary,
478template <
class Master_matrix>
479template <
class Boundary_range>
483 if (dim == Master_matrix::template get_null_value<Dimension>()) dim = boundary.size() == 0 ? 0 : boundary.size() - 1;
485 _orderRowsIfNecessary();
488 if constexpr (Master_matrix::Option_list::has_row_access && !Master_matrix::Option_list::has_removable_rows) {
489 if (boundary.size() != 0) {
491 RA_opt::_resize(Master_matrix::get_row_index(*std::prev(boundary.end())));
496 if constexpr (activeSwapOption_) {
497 Swap_opt::_initialize_row_index(cellIndex);
501 if constexpr (activePairingOption_) {
502 if (cellIndex != nextInsertIndex_) {
503 Pair_opt::_insert_id_position(cellIndex, nextInsertIndex_);
504 if constexpr (Master_matrix::Option_list::has_removable_columns) {
505 Pair_opt::PIDM::map_.emplace(nextInsertIndex_, cellIndex);
510 _container_insert(boundary, nextInsertIndex_, dim);
512 return nextInsertIndex_++;
515template <
class Master_matrix>
518 _orderRowsIfNecessary();
520 return _get_column(columnIndex);
523template <
class Master_matrix>
526 static_assert(Master_matrix::Option_list::has_row_access,
"'get_row' is not implemented for the chosen options.");
528 _orderRowsIfNecessary();
530 return RA_opt::get_row(rowIndex);
533template <
class Master_matrix>
536 static_assert(Master_matrix::Option_list::has_removable_columns,
537 "'remove_last' is not implemented for the chosen options.");
539 if (nextInsertIndex_ == 0)
return Master_matrix::template get_null_value<Index>();
543 if constexpr (activeDimOption_) {
544 Dim_opt::_update_down(matrix_.at(nextInsertIndex_).get_dimension());
549 if constexpr (Master_matrix::Option_list::has_map_column_container) {
550 auto it = matrix_.find(nextInsertIndex_);
551 pivot = it->second.get_pivot();
552 if constexpr (activeSwapOption_) {
554 if (Swap_opt::_row_were_swapped() && pivot != Master_matrix::template get_null_value<ID_index>()) {
555 Swap_opt::_orderRows();
556 pivot = it->second.get_pivot();
561 pivot = matrix_[nextInsertIndex_].get_pivot();
562 if constexpr (activeSwapOption_) {
564 if (Swap_opt::_row_were_swapped() && pivot != Master_matrix::template get_null_value<ID_index>()) {
565 Swap_opt::_orderRows();
566 pivot = matrix_[nextInsertIndex_].get_pivot();
569 if constexpr (Master_matrix::Option_list::has_row_access) {
570 GUDHI_CHECK(nextInsertIndex_ == matrix_.size() - 1,
571 std::logic_error(
"Boundary_matrix::remove_last - Indexation problem."));
574 matrix_[nextInsertIndex_].clear();
581 if constexpr (activePairingOption_) {
582 Pair_opt::_remove_last(nextInsertIndex_);
588template <
class Master_matrix>
593 if constexpr (activeSwapOption_) {
594 rowID = Swap_opt::_erase_row(rowIndex);
597 if constexpr (Master_matrix::Option_list::has_row_access && Master_matrix::Option_list::has_removable_rows) {
598 RA_opt::erase_empty_row(rowID);
602template <
class Master_matrix>
605 if constexpr (Master_matrix::Option_list::has_map_column_container) {
606 return matrix_.size();
608 return nextInsertIndex_;
612template <
class Master_matrix>
614 Index columnIndex)
const
616 return _get_column(columnIndex).get_dimension();
619template <
class Master_matrix>
622 _get_column(targetColumnIndex) += _get_column(sourceColumnIndex);
625template <
class Master_matrix>
628 Index targetColumnIndex)
630 _get_column(targetColumnIndex).multiply_target_and_add(coefficient, _get_column(sourceColumnIndex));
633template <
class Master_matrix>
635 Index sourceColumnIndex,
636 Index targetColumnIndex)
638 _get_column(targetColumnIndex).multiply_source_and_add(_get_column(sourceColumnIndex), coefficient);
641template <
class Master_matrix>
644 _get_column(columnIndex).clear(_get_real_row_index(rowIndex));
647template <
class Master_matrix>
650 _get_column(columnIndex).clear();
653template <
class Master_matrix>
656 return !(_get_column(columnIndex).is_non_zero(_get_real_row_index(rowIndex)));
659template <
class Master_matrix>
662 return _get_column(columnIndex).is_empty();
665template <
class Master_matrix>
668 _orderRowsIfNecessary();
670 return _get_column(columnIndex).get_pivot();
673template <
class Master_matrix>
676 if (
this == &other)
return *
this;
678 Dim_opt::operator=(other);
679 Swap_opt::operator=(other);
680 Pair_opt::operator=(other);
681 RA_opt::operator=(other);
684 nextInsertIndex_ = other.nextInsertIndex_;
685 colSettings_ = other.colSettings_;
687 matrix_.reserve(other.matrix_.size());
688 for (
const auto& cont : other.matrix_) {
689 if constexpr (Master_matrix::Option_list::has_map_column_container) {
690 _container_insert(cont.second, cont.first);
692 _container_insert(cont);
699template <
class Master_matrix>
702 if (
this == &other)
return *
this;
704 Dim_opt::operator=(std::move(other));
705 Swap_opt::operator=(std::move(other));
706 Pair_opt::operator=(std::move(other));
707 RA_opt::operator=(std::move(other));
709 matrix_ = std::move(other.matrix_);
710 nextInsertIndex_ = std::exchange(other.nextInsertIndex_, 0);
711 colSettings_ = std::exchange(other.colSettings_,
nullptr);
716template <
class Master_matrix>
717inline void Boundary_matrix<Master_matrix>::print()
719 if constexpr (activeSwapOption_) {
720 if (Swap_opt::_row_were_swapped()) Swap_opt::_orderRows();
722 std::cout <<
"Boundary_matrix:\n";
723 for (Index i = 0; i < nextInsertIndex_; ++i) {
724 Column& col = matrix_[i];
725 for (
auto e : col.get_content(nextInsertIndex_)) {
729 std::cout << e <<
" ";
734 if constexpr (Master_matrix::Option_list::has_row_access) {
735 std::cout <<
"Row Matrix:\n";
736 for (ID_index i = 0; i < nextInsertIndex_; ++i) {
738 const auto& row = RA_opt::get_row(i);
739 for (
const typename Column::Entry& entry : row) {
740 std::cout << entry.get_column_index() <<
" ";
742 std::cout <<
"(" << i <<
")\n";
743 }
catch (
const std::out_of_range&) {
751template <
class Master_matrix>
752inline void Boundary_matrix<Master_matrix>::_orderRowsIfNecessary()
754 if constexpr (activeSwapOption_) {
755 if (Swap_opt::_row_were_swapped()) Swap_opt::_orderRows();
759template <
class Master_matrix>
760inline const typename Boundary_matrix<Master_matrix>::Column& Boundary_matrix<Master_matrix>::_get_column(
761 Index columnIndex)
const
763 if constexpr (Master_matrix::Option_list::has_map_column_container) {
764 return matrix_.at(columnIndex);
766 return matrix_[columnIndex];
770template <
class Master_matrix>
771inline typename Boundary_matrix<Master_matrix>::Column& Boundary_matrix<Master_matrix>::_get_column(Index columnIndex)
773 if constexpr (Master_matrix::Option_list::has_map_column_container) {
774 return matrix_.at(columnIndex);
776 return matrix_[columnIndex];
780template <
class Master_matrix>
781inline typename Boundary_matrix<Master_matrix>::Index Boundary_matrix<Master_matrix>::_get_real_row_index(
782 Index rowIndex)
const
784 if constexpr (Master_matrix::Option_list::has_column_and_row_swaps || Master_matrix::Option_list::has_vine_update) {
785 return Swap_opt::_get_row_index(rowIndex);
791template <
class Master_matrix>
792template <
class Container>
793inline void Boundary_matrix<Master_matrix>::_container_insert(
const Container& column, Index pos, Dimension dim)
795 if constexpr (Master_matrix::Option_list::has_map_column_container) {
796 if constexpr (Master_matrix::Option_list::has_row_access) {
797 matrix_.try_emplace(pos, Column(pos, column, dim, RA_opt::_get_rows_ptr(), colSettings_));
799 matrix_.try_emplace(pos, Column(column, dim, colSettings_));
802 if constexpr (Master_matrix::Option_list::has_row_access) {
803 matrix_.emplace_back(pos, column, dim, RA_opt::_get_rows_ptr(), colSettings_);
805 if (matrix_.size() <= pos) {
806 matrix_.emplace_back(column, dim, colSettings_);
808 matrix_[pos] = Column(column, dim, colSettings_);
812 if constexpr (activeDimOption_) {
813 Dim_opt::_update_up(dim);
817template <
class Master_matrix>
818inline void Boundary_matrix<Master_matrix>::_container_insert(
const Column& column, [[maybe_unused]] Index pos)
820 if constexpr (Master_matrix::Option_list::has_map_column_container) {
821 if constexpr (Master_matrix::Option_list::has_row_access) {
822 matrix_.try_emplace(pos, Column(column, column.get_column_index(), RA_opt::_get_rows_ptr(), colSettings_));
824 matrix_.try_emplace(pos, Column(column, colSettings_));
827 if constexpr (Master_matrix::Option_list::has_row_access) {
828 matrix_.emplace_back(column, column.get_column_index(), RA_opt::_get_rows_ptr(), colSettings_);
830 matrix_.emplace_back(column, colSettings_);
typename Matrix< PersistenceMatrixOptions >::Dimension Dimension
Definition Boundary_matrix.h:63
Boundary_matrix(unsigned int numberOfColumns, Column_settings *colSettings)
Constructs a new empty matrix and reserves space for the given number of columns.
Definition Boundary_matrix.h:422
typename Matrix< PersistenceMatrixOptions >::Entry_constructor Entry_constructor
Definition Boundary_matrix.h:73
typename Matrix< PersistenceMatrixOptions >::Element Field_element
Definition Boundary_matrix.h:68
Boundary_matrix(const std::vector< Boundary_range > &orderedBoundaries, Column_settings *colSettings)
Constructs a new matrix from the given ranges of Matrix::Entry_representative. Each range corresponds...
Definition Boundary_matrix.h:405
void erase_empty_row(Index rowIndex)
If PersistenceMatrixOptions::has_row_access and PersistenceMatrixOptions::has_removable_rows are true...
Definition Boundary_matrix.h:589
Boundary_matrix & operator=(Boundary_matrix &&other) noexcept
Move assign operator.
Definition Boundary_matrix.h:700
typename Matrix< PersistenceMatrixOptions >::Column_settings Column_settings
Definition Boundary_matrix.h:74
typename Matrix< PersistenceMatrixOptions >::Index Index
Definition Boundary_matrix.h:61
Dimension get_column_dimension(Index columnIndex) const
Returns the dimension of the given column.
Definition Boundary_matrix.h:613
void zero_entry(Index columnIndex, Index rowIndex)
Zeroes the entry at the given coordinates.
Definition Boundary_matrix.h:642
Boundary_matrix(Boundary_matrix &&other) noexcept
Move constructor.
Definition Boundary_matrix.h:458
Index remove_last()
Only available if PersistenceMatrixOptions::has_removable_columns is true. Removes the last cell in t...
Definition Boundary_matrix.h:534
bool is_zero_column(Index columnIndex)
Indicates if the column at given index has value zero.
Definition Boundary_matrix.h:660
Index insert_boundary(ID_index cellIndex, const Boundary_range &boundary, Dimension dim=Master_matrix::template get_null_value< Dimension >())
It does the same as the other version, but allows the boundary cells to be identified without restric...
Definition Boundary_matrix.h:481
Boundary_matrix(const Boundary_matrix &matrixToCopy, Column_settings *colSettings=nullptr)
Copy constructor. If colSettings is not a null pointer, its value is kept instead of the one in the c...
Definition Boundary_matrix.h:438
void zero_column(Index columnIndex)
Zeroes the column at the given index.
Definition Boundary_matrix.h:648
friend void swap(Boundary_matrix &matrix1, Boundary_matrix &matrix2) noexcept
Swap operator.
Definition Boundary_matrix.h:358
Index get_number_of_columns() const
Returns the current number of columns in the matrix.
Definition Boundary_matrix.h:603
bool is_zero_entry(Index columnIndex, Index rowIndex) const
Indicates if the entry at given coordinates has value zero.
Definition Boundary_matrix.h:654
typename Matrix< PersistenceMatrixOptions >::Boundary Boundary
Definition Boundary_matrix.h:70
void reset(Column_settings *colSettings)
Resets the matrix to an empty matrix.
Definition Boundary_matrix.h:336
typename Matrix< PersistenceMatrixOptions >::Row Row
Definition Boundary_matrix.h:71
Index get_pivot(Index columnIndex)
Returns the pivot of the given column.
Definition Boundary_matrix.h:666
Column & get_column(Index columnIndex)
Returns the column at the given MatIdx index. The type of the column depends on the chosen options,...
Definition Boundary_matrix.h:516
Index insert_boundary(const Boundary_range &boundary, Dimension dim=Master_matrix::template get_null_value< Dimension >())
Inserts at the end of the matrix a new ordered column corresponding to the given boundary....
Definition Boundary_matrix.h:471
typename Matrix< PersistenceMatrixOptions >::Column Column
Definition Boundary_matrix.h:69
void add_to(Index sourceColumnIndex, Index targetColumnIndex)
Adds column at sourceColumnIndex onto the column at targetColumnIndex in the matrix.
Definition Boundary_matrix.h:620
typename Matrix< PersistenceMatrixOptions >::ID_index ID_index
Definition Boundary_matrix.h:62
Boundary_matrix(Column_settings *colSettings)
Constructs an empty matrix.
Definition Boundary_matrix.h:394
void multiply_source_and_add_to(const Field_element &coefficient, Index sourceColumnIndex, Index targetColumnIndex)
Multiplies the source column with the coefficient before adding it to the target column....
Definition Boundary_matrix.h:634
void multiply_target_and_add_to(Index sourceColumnIndex, const Field_element &coefficient, Index targetColumnIndex)
Multiplies the target column with the coefficient and then adds the source column to it....
Definition Boundary_matrix.h:626
Row & get_row(Index rowIndex)
Only available if PersistenceMatrixOptions::has_row_access is true. Returns the row at the given row ...
Definition Boundary_matrix.h:524
typename Matrix< PersistenceMatrixOptions >::Field_operators Field_operators
Definition Boundary_matrix.h:67
Boundary_matrix & operator=(const Boundary_matrix &other)
Assign operator.
Definition Boundary_matrix.h:674
Persistence matrix namespace.
Definition FieldOperators.h:18
Gudhi namespace.
Definition SimplicialComplexForAlpha.h:14