Skip to content

Commit

Permalink
fix for removal and indexation
Browse files Browse the repository at this point in the history
  • Loading branch information
hschreiber committed Feb 19, 2024
1 parent 865e396 commit a6aace5
Show file tree
Hide file tree
Showing 34 changed files with 2,844 additions and 2,135 deletions.
224 changes: 116 additions & 108 deletions src/Persistence_matrix/include/gudhi/Persistence_matrix/base_matrix.h

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class Base_matrix_with_column_compression : protected Master_matrix::Matrix_row_
const Row_type& get_row(index rowIndex) const;
void erase_row(index rowIndex); //assumes the row is empty, just thought as index a cleanup

unsigned int get_number_of_columns() const;
index get_number_of_columns() const;

template<class Cell_range_or_column_index>
void add_to(const Cell_range_or_column_index& sourceColumn, index targetColumnIndex);
Expand Down Expand Up @@ -165,7 +165,7 @@ class Base_matrix_with_column_compression : protected Master_matrix::Matrix_row_
boost::disjoint_sets_with_storage<> columnClasses_;
// Union_find columnClasses_;
std::vector<Column_type*> repToColumn_;
unsigned int nextColumnIndex_;
index nextColumnIndex_;
inline static Simple_object_pool<Column_type> columnPool_;
inline static const Column_type empty_column_;

Expand Down Expand Up @@ -257,7 +257,7 @@ inline void Base_matrix_with_column_compression<Master_matrix>::insert_boundary(

if constexpr (Master_matrix::Option_list::has_row_access && !Master_matrix::Option_list::has_removable_rows){
if (boundary.begin() != boundary.end()){
unsigned int pivot;
index pivot;
if constexpr (Master_matrix::Option_list::is_z2){
pivot = *std::prev(boundary.end());
} else {
Expand Down Expand Up @@ -299,11 +299,11 @@ Base_matrix_with_column_compression<Master_matrix>::get_column(index columnIndex

template<class Master_matrix>
inline const typename Base_matrix_with_column_compression<Master_matrix>::Row_type&
Base_matrix_with_column_compression<Master_matrix>::get_row(index columnIndex) const
Base_matrix_with_column_compression<Master_matrix>::get_row(index rowIndex) const
{
static_assert(Master_matrix::Option_list::has_row_access, "Row access has to be enabled for this method.");

return ra_opt::get_row(columnIndex);
return ra_opt::get_row(rowIndex);
}

template<class Master_matrix>
Expand All @@ -315,7 +315,7 @@ inline void Base_matrix_with_column_compression<Master_matrix>::erase_row(index
}

template<class Master_matrix>
inline unsigned int Base_matrix_with_column_compression<Master_matrix>::get_number_of_columns() const
inline typename Base_matrix_with_column_compression<Master_matrix>::index Base_matrix_with_column_compression<Master_matrix>::get_number_of_columns() const
{
return nextColumnIndex_;
}
Expand Down Expand Up @@ -427,7 +427,7 @@ inline void Base_matrix_with_column_compression<Master_matrix>::print()
else std::cout << e << " ";
}
std::cout << "(";
for (unsigned int i = 0; i < nextColumnIndex_; ++i){
for (index i = 0; i < nextColumnIndex_; ++i){
// if (columnClasses_.find(i) == col.get_rep())
if (columnClasses_.find_set(i) == col.get_rep())
std::cout << i << " ";
Expand All @@ -436,7 +436,7 @@ inline void Base_matrix_with_column_compression<Master_matrix>::print()
}
std::cout << "\n";
std::cout << "Row Matrix:\n";
for (unsigned int i = 0; i < ra_opt::rows_.size(); ++i){
for (index i = 0; i < ra_opt::rows_.size(); ++i){
const Row_type& row = ra_opt::rows_[i];
for (const auto &cell : row){
std::cout << cell.get_column_index() << " ";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <utility> //std::swap & std::move
#include <unordered_map>
#include <algorithm>

namespace Gudhi {
namespace persistence_matrix {
Expand All @@ -30,11 +31,11 @@ template<class Master_matrix>
class Base_pairing
{
public:
using Bar = typename Master_matrix::Bar;
using barcode_type = typename Master_matrix::barcode_type;
using matrix_type = typename Master_matrix::column_container_type;
using index = typename Master_matrix::index;
using dimension_type = typename Master_matrix::dimension_type;
using Bar = typename Master_matrix::Bar;

Base_pairing();
Base_pairing(const Base_pairing& matrixToCopy);
Expand All @@ -45,21 +46,21 @@ class Base_pairing
Base_pairing& operator=(Base_pairing other);
friend void swap(Base_pairing& pairing1, Base_pairing& pairing2){
pairing1.barcode_.swap(pairing2.barcode_);
pairing1.indexToBar_.swap(pairing2.indexToBar_);
pairing1.deathToBar_.swap(pairing2.deathToBar_);
std::swap(pairing1.isReduced_, pairing2.isReduced_);
}

protected:
using column_type = typename Master_matrix::Column_type;
using pos_index = typename Master_matrix::pos_index;
using dictionnary_type = typename Master_matrix::bar_dictionnary_type;
using base_matrix = typename Master_matrix::Boundary_matrix_type;

barcode_type barcode_;
dictionnary_type indexToBar_;
dictionnary_type deathToBar_; //records deaths only
bool isReduced_;

void _reduce();
void _remove_maximal(index columnIndex);
void _remove_last(pos_index columnIndex);

constexpr base_matrix* _matrix() { return static_cast<base_matrix*>(this); }
constexpr const base_matrix* _matrix() const { return static_cast<const base_matrix*>(this); }
Expand All @@ -73,14 +74,14 @@ inline Base_pairing<Master_matrix>::Base_pairing()
template<class Master_matrix>
inline Base_pairing<Master_matrix>::Base_pairing(const Base_pairing &matrixToCopy)
: barcode_(matrixToCopy.barcode_),
indexToBar_(matrixToCopy.indexToBar_),
deathToBar_(matrixToCopy.deathToBar_),
isReduced_(matrixToCopy.isReduced_)
{}

template<class Master_matrix>
inline Base_pairing<Master_matrix>::Base_pairing(Base_pairing<Master_matrix> &&other) noexcept
: barcode_(std::move(other.barcode_)),
indexToBar_(std::move(other.indexToBar_)),
deathToBar_(std::move(other.deathToBar_)),
isReduced_(std::move(other.isReduced_))
{}

Expand All @@ -95,20 +96,21 @@ Base_pairing<Master_matrix>::get_current_barcode()
template<class Master_matrix>
inline void Base_pairing<Master_matrix>::_reduce()
{
std::unordered_map<index, index> pivotsToColumn;
using id_index = typename Master_matrix::index;
std::unordered_map<id_index, index> pivotsToColumn;

for (int d = _matrix()->get_max_dimension(); d > 0; d--){
for (unsigned int i = 0; i < _matrix()->get_number_of_columns(); i++){
auto& curr = _matrix()->get_column(i);
if (!(curr.is_empty()) && curr.get_dimension() == d)
{
int pivot = curr.get_pivot();
id_index pivot = curr.get_pivot();

while (pivot != -1 && pivotsToColumn.find(pivot) != pivotsToColumn.end()){
if constexpr (Master_matrix::Option_list::is_z2){
curr += _matrix()->get_column(pivotsToColumn.at(pivot));
} else {
column_type &toadd = _matrix()->get_column(pivotsToColumn.at(pivot));
auto &toadd = _matrix()->get_column(pivotsToColumn.at(pivot));
typename Master_matrix::Field_type coef = curr.get_pivot_value();
coef = coef.get_inverse();
coef *= (Master_matrix::Field_type::get_characteristic() - static_cast<unsigned int>(toadd.get_pivot_value()));
Expand All @@ -121,50 +123,51 @@ inline void Base_pairing<Master_matrix>::_reduce()
if (pivot != -1){
pivotsToColumn.emplace(pivot, i);
_matrix()->get_column(pivot).clear();
barcode_.push_back(Bar(d - 1, pivot, i));
if constexpr (Master_matrix::Option_list::has_removable_columns){
indexToBar_.emplace(pivot,std::prev(barcode_.end()));
indexToBar_.emplace(i,std::prev(barcode_.end()));
}
barcode_.emplace_back(d - 1, pivot, i);
} else {
curr.clear();
barcode_.push_back(Bar(d, i, -1));
if constexpr (Master_matrix::Option_list::has_removable_columns){
indexToBar_.emplace(i,std::prev(barcode_.end()));
}
barcode_.emplace_back(d, i, -1);
}
}
}
}
for (unsigned int i = 0; i < _matrix()->get_number_of_columns(); i++){
if (_matrix()->get_column(i).get_dimension() == 0 && pivotsToColumn.find(i) == pivotsToColumn.end()){
barcode_.push_back(Bar(0, i, -1));
if constexpr (Master_matrix::Option_list::has_removable_columns){
indexToBar_.emplace(i,std::prev(barcode_.end()));
}
barcode_.emplace_back(0, i, -1);
}
}
//sort barcode by birth such that a removal is trivial
std::sort(barcode_.begin(), barcode_.end(), [](const Bar& b1, const Bar& b2){ return b1.birth < b2.birth; });
//map can only be constructed once barcode is sorted
for (index i = 0; i < barcode_.size(); ++i){
auto d = barcode_[i].death;
if (d != -1){
deathToBar_.emplace(d, i);
}
}

isReduced_ = true;
}

template<class Master_matrix>
inline void Base_pairing<Master_matrix>::_remove_maximal(index columnIndex){
inline void Base_pairing<Master_matrix>::_remove_last(pos_index columnIndex){
if (isReduced_){
auto bar = indexToBar_.at(columnIndex);

if (bar->death == -1) barcode_.erase(bar);
else bar->death = -1;

indexToBar_.erase(columnIndex);
auto it = deathToBar_.find(columnIndex);

if (it == deathToBar_.end()) { //birth
barcode_.pop_back(); //sorted by birth and columnIndex has to be the heighest one
} else { //death
barcode_[it->second].death = -1;
deathToBar_.erase(it);
};
}
}

template<class Master_matrix>
inline Base_pairing<Master_matrix> &Base_pairing<Master_matrix>::operator=(Base_pairing<Master_matrix> other)
{
barcode_.swap(other.barcode_);
indexToBar_.swap(other.indexToBar_);
deathToBar_.swap(other.deathToBar_);
std::swap(isReduced_, other.isReduced_);
return *this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ class Base_swap
public:
using matrix_type = typename Master_matrix::column_container_type;
using index = typename Master_matrix::index;
using id_index = typename Master_matrix::id_index;

Base_swap();
Base_swap(unsigned int numberOfColumns);
Base_swap(const Base_swap& matrixToCopy);
Base_swap(Base_swap&& other) noexcept;

void swap_columns(index columnIndex1, index columnIndex2);
void swap_rows(index rowIndex1, index rowIndex2);
void swap_rows(id_index rowIndex1, id_index rowIndex2);
void swap_at_indices(index index1, index index2);

Base_swap& operator=(Base_swap other);
Expand All @@ -50,8 +51,8 @@ class Base_swap
}

protected:
using index_dictionnary_type = typename Master_matrix::template dictionnary_type<unsigned int>;
using row_dictionnary_type = typename Master_matrix::template dictionnary_type<index>;
using index_dictionnary_type = typename Master_matrix::template dictionnary_type<index>;
using row_dictionnary_type = typename Master_matrix::template dictionnary_type<id_index>;

index_dictionnary_type indexToRow_;
row_dictionnary_type rowToIndex_;
Expand All @@ -74,7 +75,7 @@ inline Base_swap<Master_matrix,Base_matrix>::Base_swap(unsigned int numberOfColu
rowToIndex_(numberOfColumns),
rowSwapped_(false)
{
for (unsigned int i = 0; i < numberOfColumns; i++){
for (index i = 0; i < numberOfColumns; i++){
indexToRow_[i] = i;
rowToIndex_[i] = i;
}
Expand All @@ -101,7 +102,7 @@ inline void Base_swap<Master_matrix,Base_matrix>::swap_columns(index columnIndex
}

template<class Master_matrix, class Base_matrix>
inline void Base_swap<Master_matrix,Base_matrix>::swap_rows(index rowIndex1, index rowIndex2)
inline void Base_swap<Master_matrix,Base_matrix>::swap_rows(id_index rowIndex1, id_index rowIndex2)
{
rowSwapped_ = true;
std::swap(rowToIndex_[indexToRow_[rowIndex1]], rowToIndex_[indexToRow_[rowIndex2]]);
Expand Down Expand Up @@ -130,7 +131,7 @@ inline void Base_swap<Master_matrix,Base_matrix>::_orderRows()
for (unsigned int i = 0; i < _matrix()->get_number_of_columns(); i++){
_matrix()->matrix_.at(i).reorder(rowToIndex_);
}
for (unsigned int i = 0; i < _matrix()->get_number_of_columns(); i++){
for (index i = 0; i < _matrix()->get_number_of_columns(); i++){
indexToRow_[i] = i;
rowToIndex_[i] = i;
}
Expand Down
Loading

0 comments on commit a6aace5

Please sign in to comment.