Skip to content

Commit

Permalink
add possibility to give dimension for insert_boundary in case of a ce…
Browse files Browse the repository at this point in the history
…llular complex
  • Loading branch information
hschreiber committed Nov 24, 2023
1 parent 8817950 commit 750da6e
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Base_matrix
{
public:
using index = typename Master_matrix::index;
using dimension_type = typename Master_matrix::dimension_type;
using Field_element_type = typename Master_matrix::Field_type;
using Column_type = typename Master_matrix::Column_type;
using container_type = typename Master_matrix::boundary_type;
Expand All @@ -43,7 +44,7 @@ class Base_matrix
template<class Container_type = container_type>
void insert_column(const Container_type& column, int columnIndex);
template<class Boundary_type>
void insert_boundary(const Boundary_type& boundary); //same as insert_column
void insert_boundary(const Boundary_type& boundary, dimension_type dim = -1); //same as insert_column
Column_type& get_column(index columnIndex);
//get_row(rowIndex) --> simplex ID (=/= columnIndex)
Row_type& get_row(index rowIndex);
Expand Down Expand Up @@ -205,47 +206,7 @@ template<class Master_matrix>
template<class Container_type>
inline void Base_matrix<Master_matrix>::insert_column(const Container_type &column)
{
_orderRowsIfNecessary();

if constexpr (Master_matrix::Option_list::has_row_access && !Master_matrix::Option_list::has_removable_rows){
unsigned int pivot;
if constexpr (Master_matrix::Option_list::is_z2){
pivot = *std::prev(column.end());
} else {
pivot = std::prev(column.end())->first;
}
if (ra_opt::rows_.size() <= pivot) ra_opt::rows_.resize(pivot + 1);
}

if constexpr (Master_matrix::Option_list::has_removable_columns){
if constexpr (Master_matrix::Option_list::has_column_and_row_swaps){
swap_opt::indexToRow_[nextInsertIndex_] = nextInsertIndex_;
swap_opt::rowToIndex_[nextInsertIndex_] = nextInsertIndex_;
}

if constexpr (Master_matrix::Option_list::has_row_access){
matrix_.try_emplace(nextInsertIndex_, Column_type(nextInsertIndex_, column, ra_opt::rows_));
++nextInsertIndex_;
} else {
matrix_.try_emplace(nextInsertIndex_++, column);
}
} else {
if constexpr (Master_matrix::Option_list::has_row_access){
matrix_.emplace_back(nextInsertIndex_++, column, ra_opt::rows_);
} else {
unsigned int size = matrix_.size();
if (size <= nextInsertIndex_) {
if constexpr (Master_matrix::Option_list::has_column_and_row_swaps){
for (unsigned int i = size; i <= size * 2; i++){
swap_opt::indexToRow_.push_back(i);
swap_opt::rowToIndex_.push_back(i);
}
}
matrix_.resize(size * 2);
}
matrix_[nextInsertIndex_++] = Column_type(column);
}
}
insert_boundary(column);
}

template<class Master_matrix>
Expand Down Expand Up @@ -288,9 +249,50 @@ inline void Base_matrix<Master_matrix>::insert_column(const Container_type &colu

template<class Master_matrix>
template<class Boundary_type>
inline void Base_matrix<Master_matrix>::insert_boundary(const Boundary_type &boundary)
inline void Base_matrix<Master_matrix>::insert_boundary(const Boundary_type &boundary, dimension_type dim)
{
insert_column(boundary);
if (dim == -1) dim = boundary.size() == 0 ? 0 : boundary.size() - 1;
_orderRowsIfNecessary();

if constexpr (Master_matrix::Option_list::has_row_access && !Master_matrix::Option_list::has_removable_rows){
unsigned int pivot;
if constexpr (Master_matrix::Option_list::is_z2){
pivot = *std::prev(boundary.end());
} else {
pivot = std::prev(boundary.end())->first;
}
if (ra_opt::rows_.size() <= pivot) ra_opt::rows_.resize(pivot + 1);
}

if constexpr (Master_matrix::Option_list::has_removable_columns){
if constexpr (Master_matrix::Option_list::has_column_and_row_swaps){
swap_opt::indexToRow_[nextInsertIndex_] = nextInsertIndex_;
swap_opt::rowToIndex_[nextInsertIndex_] = nextInsertIndex_;
}

if constexpr (Master_matrix::Option_list::has_row_access){
matrix_.try_emplace(nextInsertIndex_, Column_type(nextInsertIndex_, boundary, dim, ra_opt::rows_));
++nextInsertIndex_;
} else {
matrix_.try_emplace(nextInsertIndex_++, boundary, dim);
}
} else {
if constexpr (Master_matrix::Option_list::has_row_access){
matrix_.emplace_back(nextInsertIndex_++, boundary, dim, ra_opt::rows_);
} else {
unsigned int size = matrix_.size();
if (size <= nextInsertIndex_) {
if constexpr (Master_matrix::Option_list::has_column_and_row_swaps){
for (unsigned int i = size; i <= size * 2; i++){
swap_opt::indexToRow_.push_back(i);
swap_opt::rowToIndex_.push_back(i);
}
}
matrix_.resize(size * 2);
}
matrix_[nextInsertIndex_++] = Column_type(boundary, dim);
}
}
}

template<class Master_matrix>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class Base_matrix_with_column_compression : protected Master_matrix::Matrix_row_
template<class Container_type>
void insert_column(const Container_type& column);
template<class Boundary_type>
void insert_boundary(const Boundary_type& boundary); //same as insert_column
void insert_boundary(const Boundary_type& boundary, dimension_type dim = -1); //same as insert_column
const Column_type& get_column(index columnIndex); //non const because of path compression in union-find
//get_row(rowIndex) --> simplex ID (=/= columnIndex)
const Row_type& get_row(index rowIndex) const;
Expand Down Expand Up @@ -246,13 +246,22 @@ template<class Master_matrix>
template<class Container_type>
inline void Base_matrix_with_column_compression<Master_matrix>::insert_column(const Container_type &column)
{
insert_boundary(column);
}

template<class Master_matrix>
template<class Boundary_type>
inline void Base_matrix_with_column_compression<Master_matrix>::insert_boundary(const Boundary_type &boundary, dimension_type dim)
{
if (dim == -1) dim = boundary.size() == 0 ? 0 : boundary.size() - 1;

if constexpr (Master_matrix::Option_list::has_row_access && !Master_matrix::Option_list::has_removable_rows){
if (column.begin() != column.end()){
if (boundary.begin() != boundary.end()){
unsigned int pivot;
if constexpr (Master_matrix::Option_list::is_z2){
pivot = *std::prev(column.end());
pivot = *std::prev(boundary.end());
} else {
pivot = std::prev(column.end())->first;
pivot = std::prev(boundary.end())->first;
}
if (ra_opt::rows_.size() <= pivot) ra_opt::rows_.resize(pivot + 1);
}
Expand All @@ -262,29 +271,22 @@ inline void Base_matrix_with_column_compression<Master_matrix>::insert_column(co
if (repToColumn_.size() == nextColumnIndex_){
columnClasses_.link(nextColumnIndex_, nextColumnIndex_); //could perhaps be avoided, if find_set returns something special when it does not find
if constexpr (Master_matrix::Option_list::has_row_access){
repToColumn_.push_back(columnPool_.construct(nextColumnIndex_, column, ra_opt::rows_));
repToColumn_.push_back(columnPool_.construct(nextColumnIndex_, boundary, dim, ra_opt::rows_));
} else {
repToColumn_.push_back(columnPool_.construct(column));
repToColumn_.push_back(columnPool_.construct(boundary, dim));
}
} else {
if constexpr (Master_matrix::Option_list::has_row_access){
repToColumn_[nextColumnIndex_] = columnPool_.construct(nextColumnIndex_, column, ra_opt::rows_);
repToColumn_[nextColumnIndex_] = columnPool_.construct(nextColumnIndex_, boundary, dim, ra_opt::rows_);
} else {
repToColumn_[nextColumnIndex_] = columnPool_.construct(column);
repToColumn_[nextColumnIndex_] = columnPool_.construct(boundary, dim);
}
}
_insert_column(nextColumnIndex_);

nextColumnIndex_++;
}

template<class Master_matrix>
template<class Boundary_type>
inline void Base_matrix_with_column_compression<Master_matrix>::insert_boundary(const Boundary_type &boundary)
{
insert_column(boundary);
}

template<class Master_matrix>
inline const typename Base_matrix_with_column_compression<Master_matrix>::Column_type&
Base_matrix_with_column_compression<Master_matrix>::get_column(index columnIndex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Boundary_matrix //TODO: factorize/inheritate/compose with base matrix?
Boundary_matrix(Boundary_matrix&& other) noexcept;

template<class Boundary_type = boundary_type>
void insert_boundary(const Boundary_type& boundary); //does not update barcode as it needs reduction
void insert_boundary(const Boundary_type& boundary, dimension_type dim = -1); //does not update barcode as it needs reduction
Column_type& get_column(index columnIndex);
Row_type& get_row(index rowIndex);
void remove_maximal_simplex(index columnIndex); //update barcode if already computed
Expand Down Expand Up @@ -225,8 +225,10 @@ inline Boundary_matrix<Master_matrix>::Boundary_matrix(Boundary_matrix &&other)

template<class Master_matrix>
template<class Boundary_type>
inline void Boundary_matrix<Master_matrix>::insert_boundary(const Boundary_type &boundary)
inline void Boundary_matrix<Master_matrix>::insert_boundary(const Boundary_type &boundary, dimension_type dim)
{
if (dim == -1) dim = boundary.size() == 0 ? 0 : boundary.size() - 1;

if constexpr (activeSwapOption){
if (swap_opt::rowSwapped_) swap_opt::_orderRows();
}
Expand All @@ -248,14 +250,14 @@ inline void Boundary_matrix<Master_matrix>::insert_boundary(const Boundary_type
}

if constexpr (Master_matrix::Option_list::has_row_access){
matrix_.try_emplace(nextInsertIndex_, Column_type(nextInsertIndex_, boundary, ra_opt::rows_));
matrix_.try_emplace(nextInsertIndex_, Column_type(nextInsertIndex_, boundary, dim, ra_opt::rows_));
++nextInsertIndex_;
} else {
matrix_.try_emplace(nextInsertIndex_++, boundary);
matrix_.try_emplace(nextInsertIndex_++, boundary, dim);
}
} else {
if constexpr (Master_matrix::Option_list::has_row_access){
matrix_.emplace_back(nextInsertIndex_++, boundary, ra_opt::rows_);
matrix_.emplace_back(nextInsertIndex_++, boundary, dim, ra_opt::rows_);
} else {
unsigned int size = matrix_.size();
if (size <= nextInsertIndex_) {
Expand All @@ -267,7 +269,7 @@ inline void Boundary_matrix<Master_matrix>::insert_boundary(const Boundary_type
}
matrix_.resize(size * 2);
}
matrix_[nextInsertIndex_++] = Column_type(boundary);
matrix_[nextInsertIndex_++] = Column_type(boundary, dim);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ class Chain_matrix

//new simplex = new ID even if the same simplex was already inserted and then removed, ie., an ID cannot come back.
template<class Boundary_type = boundary_type>
std::vector<cell_rep_type> insert_boundary(const Boundary_type& boundary);
std::vector<cell_rep_type> insert_boundary(const Boundary_type& boundary, dimension_type dim = -1);
template<class Boundary_type = boundary_type>
std::vector<cell_rep_type> insert_boundary(index simplexIndex, const Boundary_type& boundary);
std::vector<cell_rep_type> insert_boundary(index simplexIndex, const Boundary_type& boundary, dimension_type dim = -1);
Column_type& get_column(index columnIndex);
const Column_type& get_column(index columnIndex) const;
void remove_maximal_simplex(index simplexIndex);
Expand Down Expand Up @@ -143,7 +143,7 @@ class Chain_matrix
index nextInsertIndex_;

template<class Boundary_type>
std::vector<cell_rep_type> _reduce_boundary(index simplexIndex, const Boundary_type& boundary);
std::vector<cell_rep_type> _reduce_boundary(index simplexIndex, const Boundary_type& boundary, dimension_type dim);
void _reduce_by_G(tmp_column_type& column,
std::vector<cell_rep_type>& chainsInH,
index currentPivot);
Expand Down Expand Up @@ -337,15 +337,15 @@ inline Chain_matrix<Master_matrix>::Chain_matrix(
template<class Master_matrix>
template<class Boundary_type>
inline std::vector<typename Master_matrix::cell_rep_type> Chain_matrix<Master_matrix>::insert_boundary(
const Boundary_type &boundary)
const Boundary_type &boundary, dimension_type dim)
{
return insert_boundary(nextInsertIndex_, boundary);
return insert_boundary(nextInsertIndex_, boundary, dim);
}

template<class Master_matrix>
template<class Boundary_type>
inline std::vector<typename Master_matrix::cell_rep_type> Chain_matrix<Master_matrix>::insert_boundary(
index simplexIndex, const Boundary_type &boundary)
index simplexIndex, const Boundary_type &boundary, dimension_type dim)
{
if constexpr (!Master_matrix::Option_list::has_removable_columns){
if (pivotToColumnIndex_.size() <= simplexIndex){
Expand All @@ -364,10 +364,10 @@ inline std::vector<typename Master_matrix::cell_rep_type> Chain_matrix<Master_ma
}

if constexpr (Master_matrix::Option_list::has_matrix_maximal_dimension_access){
dim_opt::update_up(boundary.size() == 0 ? 0 : boundary.size() - 1);
dim_opt::update_up(dim == -1 ? (boundary.size() == 0 ? 0 : boundary.size() - 1) : dim);
}

return _reduce_boundary(simplexIndex, boundary);
return _reduce_boundary(simplexIndex, boundary, dim);
}

template<class Master_matrix>
Expand Down Expand Up @@ -623,10 +623,10 @@ inline void Chain_matrix<Master_matrix>::print() const
template<class Master_matrix>
template<class Boundary_type>
inline std::vector<typename Master_matrix::cell_rep_type> Chain_matrix<Master_matrix>::_reduce_boundary(
index simplexIndex, const Boundary_type& boundary)
index simplexIndex, const Boundary_type& boundary, dimension_type dim)
{
tmp_column_type column(boundary.begin(), boundary.end());
int dim = boundary.begin() == boundary.end() ? 0 : boundary.size() - 1;
if (dim == -1) dim = boundary.begin() == boundary.end() ? 0 : boundary.size() - 1;
std::vector<cell_rep_type> chainsInH; //for corresponding indices in H (paired columns)
std::vector<cell_rep_type> chainsInF; //for corresponding indices in F (unpaired, essential columns)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ class Id_to_position_indexation_overlay
//boundary: does not update barcode as it needs reduction
//ru
template<class Boundary_type = boundary_type>
void insert_boundary(const Boundary_type& boundary);
void insert_boundary(const Boundary_type& boundary, dimension_type dim = -1);
//chain: new simplex = new ID even if the same simplex was already inserted and then removed, ie., an ID cannot come back.
template<class Boundary_type = boundary_type>
void insert_boundary(index simplexIndex, const Boundary_type& boundary);
void insert_boundary(index simplexIndex, const Boundary_type& boundary, dimension_type dim = -1);
//boundary
//ru: inR = true forced
//chain
Expand Down Expand Up @@ -206,9 +206,9 @@ inline Id_to_position_indexation_overlay<Matrix_type,Master_matrix_type>::Id_to_

template<class Matrix_type, class Master_matrix_type>
template<class Boundary_type>
inline void Id_to_position_indexation_overlay<Matrix_type,Master_matrix_type>::insert_boundary(const Boundary_type& boundary)
inline void Id_to_position_indexation_overlay<Matrix_type,Master_matrix_type>::insert_boundary(const Boundary_type& boundary, dimension_type dim)
{
matrix_.insert_boundary(boundary);
matrix_.insert_boundary(boundary, dim);
if constexpr (Master_matrix_type::Option_list::has_removable_columns){
columnIDToPosition_[nextIndex_] = columnIDToPosition_.size();
} else {
Expand All @@ -223,14 +223,14 @@ inline void Id_to_position_indexation_overlay<Matrix_type,Master_matrix_type>::i

template<class Matrix_type, class Master_matrix_type>
template<class Boundary_type>
inline void Id_to_position_indexation_overlay<Matrix_type,Master_matrix_type>::insert_boundary(index simplexIndex, const Boundary_type& boundary)
inline void Id_to_position_indexation_overlay<Matrix_type,Master_matrix_type>::insert_boundary(index simplexIndex, const Boundary_type& boundary, dimension_type dim)
{
if constexpr (Master_matrix_type::Option_list::has_removable_columns){
assert(columnIDToPosition_.find(simplexIndex) == columnIDToPosition_.end() && "Index for simplex already chosen!");
} else {
assert((columnIDToPosition_.size() <= simplexIndex || columnIDToPosition_[simplexIndex] == -1) && "Index for simplex already chosen!");
}
matrix_.insert_boundary(boundary);
matrix_.insert_boundary(boundary, dim);
if constexpr (Master_matrix_type::Option_list::has_removable_columns){
columnIDToPosition_[simplexIndex] = matrix_.get_number_of_columns();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Position_to_id_indexation_overlay

//chain: new simplex = new ID even if the same simplex was already inserted and then removed, ie., an ID cannot come back.
template<class Boundary_type = boundary_type>
std::vector<cell_rep_type> insert_boundary(const Boundary_type& boundary);
std::vector<cell_rep_type> insert_boundary(const Boundary_type& boundary, dimension_type dim = -1);
//boundary
//ru: inR = true forced
//chain
Expand Down Expand Up @@ -251,15 +251,15 @@ inline Position_to_id_indexation_overlay<Matrix_type,Master_matrix_type>::Positi
template<class Matrix_type, class Master_matrix_type>
template<class Boundary_type>
inline std::vector<typename Position_to_id_indexation_overlay<Matrix_type,Master_matrix_type>::cell_rep_type>
Position_to_id_indexation_overlay<Matrix_type,Master_matrix_type>::insert_boundary(const Boundary_type &boundary)
Position_to_id_indexation_overlay<Matrix_type,Master_matrix_type>::insert_boundary(const Boundary_type &boundary, dimension_type dim)
{
if (columnPositionToID_.size() <= nextIndex_) {
columnPositionToID_.resize(nextIndex_ * 2 + 1);
}

columnPositionToID_[nextIndex_++] = nextID_++;

return matrix_.insert_boundary(boundary);
return matrix_.insert_boundary(boundary, dim);
}

template<class Matrix_type, class Master_matrix_type>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class RU_matrix
RU_matrix(RU_matrix&& other) noexcept;

template<class Boundary_type = boundary_type>
void insert_boundary(const Boundary_type& boundary);
void insert_boundary(const Boundary_type& boundary, dimension_type dim = -1);
Column_type& get_column(index columnIndex, bool inR = true);
//get_row(rowIndex) --> simplex ID (=/= columnIndex)
Row_type& get_row(index rowIndex, bool inR = true);
Expand Down Expand Up @@ -177,9 +177,9 @@ inline RU_matrix<Master_matrix>::RU_matrix(RU_matrix &&other) noexcept

template<class Master_matrix>
template<class Boundary_type>
inline void RU_matrix<Master_matrix>::insert_boundary(const Boundary_type &boundary)
inline void RU_matrix<Master_matrix>::insert_boundary(const Boundary_type &boundary, dimension_type dim)
{
reducedMatrixR_.insert_boundary(boundary);
reducedMatrixR_.insert_boundary(boundary, dim);

if constexpr (Master_matrix::Option_list::is_z2) {
mirrorMatrixU_.insert_column({nextInsertIndex_});
Expand Down
Loading

0 comments on commit 750da6e

Please sign in to comment.