17#ifndef PM_BOUNDARY_MATRIX_H
18#define PM_BOUNDARY_MATRIX_H
39template <
class Master_matrix>
41 public Master_matrix::template Base_swap_option<Boundary_matrix<Master_matrix> >,
42 public Master_matrix::Base_pairing_option,
43 protected Master_matrix::Matrix_row_access_option
46 using Dim_opt =
typename Master_matrix::Matrix_dimension_option;
47 using Swap_opt =
typename Master_matrix::template Base_swap_option<Boundary_matrix<Master_matrix> >;
48 using Pair_opt =
typename Master_matrix::Base_pairing_option;
49 using RA_opt =
typename Master_matrix::Matrix_row_access_option;
51 static constexpr bool activeDimOption_ =
52 Master_matrix::Option_list::has_matrix_maximal_dimension_access || Master_matrix::maxDimensionIsNeeded;
53 static constexpr bool activeSwapOption_ =
54 Master_matrix::Option_list::has_column_and_row_swaps || Master_matrix::Option_list::has_vine_update;
55 static constexpr bool activePairingOption_ = Master_matrix::Option_list::has_column_pairings &&
56 !Master_matrix::Option_list::has_vine_update &&
57 !Master_matrix::Option_list::can_retrieve_representative_cycles;
60 using Index =
typename Master_matrix::Index;
61 using ID_index =
typename Master_matrix::ID_index;
62 using Dimension =
typename Master_matrix::Dimension;
68 using Column =
typename Master_matrix::Column;
69 using Boundary =
typename Master_matrix::Boundary;
70 using Row =
typename Master_matrix::Row;
105 template <
class Boundary_range = Boundary>
157 template <
class Boundary_range = Boundary>
159 Dimension dim = Master_matrix::template get_null_value<Dimension>());
178 template <
class Boundary_range = Boundary>
180 const Boundary_range& boundary,
181 Dimension dim = Master_matrix::template get_null_value<Dimension>());
337 if constexpr (activeDimOption_) Dim_opt::_reset();
338 if constexpr (activeSwapOption_) Swap_opt::_reset();
339 if constexpr (activePairingOption_) Pair_opt::_reset();
341 nextInsertIndex_ = 0;
342 colSettings_ = colSettings;
359 swap(
static_cast<Dim_opt&
>(matrix1),
static_cast<Dim_opt&
>(matrix2));
360 swap(
static_cast<Swap_opt&
>(matrix1),
static_cast<Swap_opt&
>(matrix2));
361 swap(
static_cast<Pair_opt&
>(matrix1),
static_cast<Pair_opt&
>(matrix2));
362 matrix1.matrix_.swap(matrix2.matrix_);
363 std::swap(matrix1.nextInsertIndex_, matrix2.nextInsertIndex_);
364 std::swap(matrix1.colSettings_, matrix2.colSettings_);
366 if constexpr (Master_matrix::Option_list::has_row_access) {
367 swap(
static_cast<RA_opt&
>(matrix1),
static_cast<RA_opt&
>(matrix2));
374 using Column_container =
typename Master_matrix::Column_container;
379 Column_container matrix_;
380 Index nextInsertIndex_;
383 void _orderRowsIfNecessary();
384 const Column& _get_column(Index columnIndex)
const;
385 Column& _get_column(Index columnIndex);
386 Index _get_real_row_index(Index rowIndex)
const;
387 template <
class Container>
388 void _container_insert(
const Container& column, Index pos, Dimension dim);
389 void _container_insert(
const Column& column, [[maybe_unused]] Index pos = 0);
392template <
class Master_matrix>
394 : Dim_opt(Master_matrix::template get_null_value<
Dimension>()),
399 colSettings_(colSettings)
402template <
class Master_matrix>
403template <
class Boundary_range>
406 : Dim_opt(Master_matrix::template get_null_value<
Dimension>()),
407 Swap_opt(orderedBoundaries.size()),
409 RA_opt(orderedBoundaries.size()),
410 nextInsertIndex_(orderedBoundaries.size()),
411 colSettings_(colSettings)
413 matrix_.reserve(orderedBoundaries.size());
415 for (
Index i = 0; i < orderedBoundaries.size(); i++) {
416 _container_insert(orderedBoundaries[i], i, orderedBoundaries[i].size() == 0 ? 0 : orderedBoundaries[i].size() - 1);
420template <
class Master_matrix>
422 : Dim_opt(Master_matrix::template get_null_value<
Dimension>()),
423 Swap_opt(numberOfColumns),
425 RA_opt(numberOfColumns),
426 matrix_(!Master_matrix::Option_list::has_map_column_container && Master_matrix::Option_list::has_row_access
430 colSettings_(colSettings)
432 if constexpr (!Master_matrix::Option_list::has_map_column_container && Master_matrix::Option_list::has_row_access)
433 matrix_.reserve(numberOfColumns);
436template <
class Master_matrix>
439 : Dim_opt(static_cast<const Dim_opt&>(matrixToCopy)),
440 Swap_opt(static_cast<const Swap_opt&>(matrixToCopy)),
441 Pair_opt(static_cast<const Pair_opt&>(matrixToCopy)),
442 RA_opt(static_cast<const RA_opt&>(matrixToCopy)),
443 nextInsertIndex_(matrixToCopy.nextInsertIndex_),
444 colSettings_(colSettings == nullptr ? matrixToCopy.colSettings_ : colSettings)
446 matrix_.reserve(matrixToCopy.matrix_.size());
447 for (
const auto& cont : matrixToCopy.matrix_) {
448 if constexpr (Master_matrix::Option_list::has_map_column_container) {
449 _container_insert(cont.second, cont.first);
451 _container_insert(cont);
456template <
class Master_matrix>
458 : Dim_opt(std::move(
static_cast<Dim_opt&
>(other))),
459 Swap_opt(std::move(
static_cast<Swap_opt&
>(other))),
460 Pair_opt(std::move(
static_cast<Pair_opt&
>(other))),
461 RA_opt(std::move(
static_cast<RA_opt&
>(other))),
462 matrix_(std::move(other.matrix_)),
463 nextInsertIndex_(std::exchange(other.nextInsertIndex_, 0)),
464 colSettings_(std::exchange(other.colSettings_,
nullptr))
468template <
class Master_matrix>
469template <
class Boundary_range>
471 const Boundary_range& boundary,
477template <
class Master_matrix>
478template <
class Boundary_range>
482 if (dim == Master_matrix::template get_null_value<Dimension>()) dim = boundary.size() == 0 ? 0 : boundary.size() - 1;
484 _orderRowsIfNecessary();
487 if constexpr (Master_matrix::Option_list::has_row_access && !Master_matrix::Option_list::has_removable_rows) {
488 if (boundary.size() != 0) {
490 RA_opt::_resize(Master_matrix::get_row_index(*std::prev(boundary.end())));
495 if constexpr (activeSwapOption_) {
496 Swap_opt::_initialize_row_index(cellIndex);
500 if constexpr (activePairingOption_) {
501 if (cellIndex != nextInsertIndex_) {
502 Pair_opt::_insert_id_position(cellIndex, nextInsertIndex_);
503 if constexpr (Master_matrix::Option_list::has_removable_columns) {
504 Pair_opt::PIDM::map_.emplace(nextInsertIndex_, cellIndex);
509 _container_insert(boundary, nextInsertIndex_, dim);
511 return nextInsertIndex_++;
514template <
class Master_matrix>
517 _orderRowsIfNecessary();
519 return _get_column(columnIndex);
522template <
class Master_matrix>
525 static_assert(Master_matrix::Option_list::has_row_access,
"'get_row' is not implemented for the chosen options.");
527 _orderRowsIfNecessary();
529 return RA_opt::get_row(rowIndex);
532template <
class Master_matrix>
535 static_assert(Master_matrix::Option_list::has_removable_columns,
536 "'remove_last' is not implemented for the chosen options.");
538 if (nextInsertIndex_ == 0)
return Master_matrix::template get_null_value<Index>();
542 if constexpr (activeDimOption_) {
543 Dim_opt::_update_down(matrix_.at(nextInsertIndex_).get_dimension());
548 if constexpr (Master_matrix::Option_list::has_map_column_container) {
549 auto it = matrix_.find(nextInsertIndex_);
550 pivot = it->second.get_pivot();
551 if constexpr (activeSwapOption_) {
553 if (Swap_opt::_row_were_swapped() && pivot != Master_matrix::template get_null_value<ID_index>()) {
554 Swap_opt::_orderRows();
555 pivot = it->second.get_pivot();
560 pivot = matrix_[nextInsertIndex_].get_pivot();
561 if constexpr (activeSwapOption_) {
563 if (Swap_opt::_row_were_swapped() && pivot != Master_matrix::template get_null_value<ID_index>()) {
564 Swap_opt::_orderRows();
565 pivot = matrix_[nextInsertIndex_].get_pivot();
568 if constexpr (Master_matrix::Option_list::has_row_access) {
569 GUDHI_CHECK(nextInsertIndex_ == matrix_.size() - 1,
570 std::logic_error(
"Boundary_matrix::remove_last - Indexation problem."));
573 matrix_[nextInsertIndex_].clear();
580 if constexpr (activePairingOption_) {
581 Pair_opt::_remove_last(nextInsertIndex_);
587template <
class Master_matrix>
592 if constexpr (activeSwapOption_) {
593 rowID = Swap_opt::_erase_row(rowIndex);
596 if constexpr (Master_matrix::Option_list::has_row_access && Master_matrix::Option_list::has_removable_rows) {
597 RA_opt::erase_empty_row(rowID);
601template <
class Master_matrix>
604 if constexpr (Master_matrix::Option_list::has_map_column_container) {
605 return matrix_.size();
607 return nextInsertIndex_;
611template <
class Master_matrix>
613 Index columnIndex)
const
615 return _get_column(columnIndex).get_dimension();
618template <
class Master_matrix>
621 _get_column(targetColumnIndex) += _get_column(sourceColumnIndex);
624template <
class Master_matrix>
627 Index targetColumnIndex)
629 _get_column(targetColumnIndex).multiply_target_and_add(coefficient, _get_column(sourceColumnIndex));
632template <
class Master_matrix>
634 Index sourceColumnIndex,
635 Index targetColumnIndex)
637 _get_column(targetColumnIndex).multiply_source_and_add(_get_column(sourceColumnIndex), coefficient);
640template <
class Master_matrix>
643 _get_column(columnIndex).clear(_get_real_row_index(rowIndex));
646template <
class Master_matrix>
649 _get_column(columnIndex).clear();
652template <
class Master_matrix>
655 return !(_get_column(columnIndex).is_non_zero(_get_real_row_index(rowIndex)));
658template <
class Master_matrix>
661 return _get_column(columnIndex).is_empty();
664template <
class Master_matrix>
667 _orderRowsIfNecessary();
669 return _get_column(columnIndex).get_pivot();
672template <
class Master_matrix>
675 if (
this == &other)
return *
this;
677 Dim_opt::operator=(other);
678 Swap_opt::operator=(other);
679 Pair_opt::operator=(other);
680 RA_opt::operator=(other);
683 nextInsertIndex_ = other.nextInsertIndex_;
684 colSettings_ = other.colSettings_;
686 matrix_.reserve(other.matrix_.size());
687 for (
const auto& cont : other.matrix_) {
688 if constexpr (Master_matrix::Option_list::has_map_column_container) {
689 _container_insert(cont.second, cont.first);
691 _container_insert(cont);
698template <
class Master_matrix>
701 if (
this == &other)
return *
this;
703 Dim_opt::operator=(std::move(other));
704 Swap_opt::operator=(std::move(other));
705 Pair_opt::operator=(std::move(other));
706 RA_opt::operator=(std::move(other));
708 matrix_ = std::move(other.matrix_);
709 nextInsertIndex_ = std::exchange(other.nextInsertIndex_, 0);
710 colSettings_ = std::exchange(other.colSettings_,
nullptr);
715template <
class Master_matrix>
716inline void Boundary_matrix<Master_matrix>::print()
718 if constexpr (activeSwapOption_) {
719 if (Swap_opt::_row_were_swapped()) Swap_opt::_orderRows();
721 std::cout <<
"Boundary_matrix:\n";
722 for (Index i = 0; i < nextInsertIndex_; ++i) {
723 Column& col = matrix_[i];
724 for (
auto e : col.get_content(nextInsertIndex_)) {
728 std::cout << e <<
" ";
733 if constexpr (Master_matrix::Option_list::has_row_access) {
734 std::cout <<
"Row Matrix:\n";
735 for (ID_index i = 0; i < nextInsertIndex_; ++i) {
736 const auto& row = RA_opt::get_row(i);
737 for (
const typename Column::Entry& entry : row) {
738 std::cout << entry.get_column_index() <<
" ";
740 std::cout <<
"(" << i <<
")\n";
746template <
class Master_matrix>
747inline void Boundary_matrix<Master_matrix>::_orderRowsIfNecessary()
749 if constexpr (activeSwapOption_) {
750 if (Swap_opt::_row_were_swapped()) Swap_opt::_orderRows();
754template <
class Master_matrix>
755inline const typename Boundary_matrix<Master_matrix>::Column& Boundary_matrix<Master_matrix>::_get_column(
756 Index columnIndex)
const
758 if constexpr (Master_matrix::Option_list::has_map_column_container) {
759 return matrix_.at(columnIndex);
761 return matrix_[columnIndex];
765template <
class Master_matrix>
766inline typename Boundary_matrix<Master_matrix>::Column& Boundary_matrix<Master_matrix>::_get_column(Index columnIndex)
768 if constexpr (Master_matrix::Option_list::has_map_column_container) {
769 return matrix_.at(columnIndex);
771 return matrix_[columnIndex];
775template <
class Master_matrix>
776inline typename Boundary_matrix<Master_matrix>::Index Boundary_matrix<Master_matrix>::_get_real_row_index(
777 Index rowIndex)
const
779 if constexpr (Master_matrix::Option_list::has_column_and_row_swaps || Master_matrix::Option_list::has_vine_update) {
780 return Swap_opt::_get_row_index(rowIndex);
786template <
class Master_matrix>
787template <
class Container>
788inline void Boundary_matrix<Master_matrix>::_container_insert(
const Container& column, Index pos, Dimension dim)
790 if constexpr (Master_matrix::Option_list::has_map_column_container) {
791 if constexpr (Master_matrix::Option_list::has_row_access) {
792 matrix_.try_emplace(pos, Column(pos, column, dim, RA_opt::_get_rows_ptr(), colSettings_));
794 matrix_.try_emplace(pos, Column(column, dim, colSettings_));
797 if constexpr (Master_matrix::Option_list::has_row_access) {
798 matrix_.emplace_back(pos, column, dim, RA_opt::_get_rows_ptr(), colSettings_);
800 if (matrix_.size() <= pos) {
801 matrix_.emplace_back(column, dim, colSettings_);
803 matrix_[pos] = Column(column, dim, colSettings_);
807 if constexpr (activeDimOption_) {
808 Dim_opt::_update_up(dim);
812template <
class Master_matrix>
813inline void Boundary_matrix<Master_matrix>::_container_insert(
const Column& column, [[maybe_unused]] Index pos)
815 if constexpr (Master_matrix::Option_list::has_map_column_container) {
816 if constexpr (Master_matrix::Option_list::has_row_access) {
817 matrix_.try_emplace(pos, Column(column, column.get_column_index(), RA_opt::_get_rows_ptr(), colSettings_));
819 matrix_.try_emplace(pos, Column(column, colSettings_));
822 if constexpr (Master_matrix::Option_list::has_row_access) {
823 matrix_.emplace_back(column, column.get_column_index(), RA_opt::_get_rows_ptr(), colSettings_);
825 matrix_.emplace_back(column, colSettings_);
typename Matrix< PersistenceMatrixOptions >::Dimension Dimension
Definition Boundary_matrix.h:62
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:421
typename Matrix< PersistenceMatrixOptions >::Entry_constructor Entry_constructor
Definition Boundary_matrix.h:72
typename Matrix< PersistenceMatrixOptions >::Element Field_element
Definition Boundary_matrix.h:67
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:404
void erase_empty_row(Index rowIndex)
If PersistenceMatrixOptions::has_row_access and PersistenceMatrixOptions::has_removable_rows are true...
Definition Boundary_matrix.h:588
Boundary_matrix & operator=(Boundary_matrix &&other) noexcept
Move assign operator.
Definition Boundary_matrix.h:699
typename Matrix< PersistenceMatrixOptions >::Column_settings Column_settings
Definition Boundary_matrix.h:73
typename Matrix< PersistenceMatrixOptions >::Index Index
Definition Boundary_matrix.h:60
Dimension get_column_dimension(Index columnIndex) const
Returns the dimension of the given column.
Definition Boundary_matrix.h:612
void zero_entry(Index columnIndex, Index rowIndex)
Zeroes the entry at the given coordinates.
Definition Boundary_matrix.h:641
Boundary_matrix(Boundary_matrix &&other) noexcept
Move constructor.
Definition Boundary_matrix.h:457
Index remove_last()
Only available if PersistenceMatrixOptions::has_removable_columns is true. Removes the last cell in t...
Definition Boundary_matrix.h:533
bool is_zero_column(Index columnIndex)
Indicates if the column at given index has value zero.
Definition Boundary_matrix.h:659
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:480
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:437
void zero_column(Index columnIndex)
Zeroes the column at the given index.
Definition Boundary_matrix.h:647
friend void swap(Boundary_matrix &matrix1, Boundary_matrix &matrix2) noexcept
Swap operator.
Definition Boundary_matrix.h:357
Index get_number_of_columns() const
Returns the current number of columns in the matrix.
Definition Boundary_matrix.h:602
bool is_zero_entry(Index columnIndex, Index rowIndex) const
Indicates if the entry at given coordinates has value zero.
Definition Boundary_matrix.h:653
typename Matrix< PersistenceMatrixOptions >::Boundary Boundary
Definition Boundary_matrix.h:69
void reset(Column_settings *colSettings)
Resets the matrix to an empty matrix.
Definition Boundary_matrix.h:335
typename Matrix< PersistenceMatrixOptions >::Row Row
Definition Boundary_matrix.h:70
Index get_pivot(Index columnIndex)
Returns the pivot of the given column.
Definition Boundary_matrix.h:665
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:515
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:470
typename Matrix< PersistenceMatrixOptions >::Column Column
Definition Boundary_matrix.h:68
void add_to(Index sourceColumnIndex, Index targetColumnIndex)
Adds column at sourceColumnIndex onto the column at targetColumnIndex in the matrix.
Definition Boundary_matrix.h:619
typename Matrix< PersistenceMatrixOptions >::ID_index ID_index
Definition Boundary_matrix.h:61
Boundary_matrix(Column_settings *colSettings)
Constructs an empty matrix.
Definition Boundary_matrix.h:393
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:633
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:625
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:523
typename Matrix< PersistenceMatrixOptions >::Field_operators Field_operators
Definition Boundary_matrix.h:66
Boundary_matrix & operator=(const Boundary_matrix &other)
Assign operator.
Definition Boundary_matrix.h:673
Persistence matrix namespace.
Definition FieldOperators.h:18
Gudhi namespace.
Definition SimplicialComplexForAlpha.h:14