Skip to content

Commit

Permalink
RU vine fix
Browse files Browse the repository at this point in the history
  • Loading branch information
hschreiber committed Dec 6, 2023
1 parent e3bef46 commit d02e1ce
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ inline void Base_matrix<Master_matrix>::insert_column(const Container_type &colu
if (columnIndex > static_cast<int>(nextInsertIndex_)) nextInsertIndex_ = columnIndex + 1;
else if (columnIndex < 0) nextInsertIndex_++;

if constexpr (Master_matrix::Option_list::has_column_and_row_swaps){
if constexpr (Master_matrix::Option_list::has_column_and_row_swaps || Master_matrix::Option_list::has_vine_update){
if (swap_opt::rowSwapped_) swap_opt::_orderRows();
swap_opt::indexToRow_[id] = id;
swap_opt::rowToIndex_[id] = id;
Expand Down Expand Up @@ -265,7 +265,7 @@ inline void Base_matrix<Master_matrix>::insert_boundary(const Boundary_type &bou
}

if constexpr (Master_matrix::Option_list::has_removable_columns){
if constexpr (Master_matrix::Option_list::has_column_and_row_swaps){
if constexpr (Master_matrix::Option_list::has_column_and_row_swaps || Master_matrix::Option_list::has_vine_update){
swap_opt::indexToRow_[nextInsertIndex_] = nextInsertIndex_;
swap_opt::rowToIndex_[nextInsertIndex_] = nextInsertIndex_;
}
Expand All @@ -282,7 +282,7 @@ inline void Base_matrix<Master_matrix>::insert_boundary(const Boundary_type &bou
} else {
unsigned int size = matrix_.size();
if (size <= nextInsertIndex_) {
if constexpr (Master_matrix::Option_list::has_column_and_row_swaps){
if constexpr (Master_matrix::Option_list::has_column_and_row_swaps || Master_matrix::Option_list::has_vine_update){
for (unsigned int i = size; i <= size * 2; i++){
swap_opt::indexToRow_.push_back(i);
swap_opt::rowToIndex_.push_back(i);
Expand Down Expand Up @@ -329,7 +329,7 @@ inline void Base_matrix<Master_matrix>::erase_column(index columnIndex)
template<class Master_matrix>
inline void Base_matrix<Master_matrix>::erase_row(index rowIndex)
{
if constexpr (Master_matrix::Option_list::has_column_and_row_swaps){
if constexpr (Master_matrix::Option_list::has_column_and_row_swaps || Master_matrix::Option_list::has_vine_update){
if constexpr (Master_matrix::Option_list::has_row_access && Master_matrix::Option_list::has_removable_rows){
ra_opt::erase_row(swap_opt::indexToRow_[rowIndex]);
}
Expand Down Expand Up @@ -417,13 +417,13 @@ template<class Master_matrix>
inline void Base_matrix<Master_matrix>::zero_cell(index columnIndex, index rowIndex)
{
if constexpr (Master_matrix::Option_list::has_removable_columns){
if constexpr (Master_matrix::Option_list::has_column_and_row_swaps){
if constexpr (Master_matrix::Option_list::has_column_and_row_swaps || Master_matrix::Option_list::has_vine_update){
matrix_.at(columnIndex).clear(swap_opt::indexToRow_.at(rowIndex)); //TODO: reorganize columns
} else {
matrix_.at(columnIndex).clear(rowIndex); //TODO: reorganize columns
}
} else {
if constexpr (Master_matrix::Option_list::has_column_and_row_swaps){
if constexpr (Master_matrix::Option_list::has_column_and_row_swaps || Master_matrix::Option_list::has_vine_update){
matrix_[columnIndex].clear(swap_opt::indexToRow_[rowIndex]); //TODO: reorganize columns
} else {
matrix_[columnIndex].clear(rowIndex); //TODO: reorganize columns
Expand All @@ -445,13 +445,13 @@ template<class Master_matrix>
inline bool Base_matrix<Master_matrix>::is_zero_cell(index columnIndex, index rowIndex) const
{
if constexpr (Master_matrix::Option_list::has_removable_columns){
if constexpr (Master_matrix::Option_list::has_column_and_row_swaps){
if constexpr (Master_matrix::Option_list::has_column_and_row_swaps || Master_matrix::Option_list::has_vine_update){
return !(matrix_.at(columnIndex).is_non_zero(swap_opt::indexToRow_.at(rowIndex)));
} else {
return !(matrix_.at(columnIndex).is_non_zero(rowIndex));
}
} else { //operator[] non const for maps
if constexpr (Master_matrix::Option_list::has_column_and_row_swaps){
if constexpr (Master_matrix::Option_list::has_column_and_row_swaps || Master_matrix::Option_list::has_vine_update){
return !(matrix_[columnIndex].is_non_zero(swap_opt::indexToRow_[rowIndex]));
} else {
return !(matrix_[columnIndex].is_non_zero(rowIndex));
Expand Down Expand Up @@ -498,6 +498,7 @@ inline Base_matrix<Master_matrix> &Base_matrix<Master_matrix>::operator=(const B
template<class Master_matrix>
inline void Base_matrix<Master_matrix>::print()
{
_orderRowsIfNecessary();
std::cout << "Base_matrix:\n";
for (unsigned int i = 0; i < nextInsertIndex_; ++i){
const Column_type& col = matrix_[i];
Expand All @@ -523,7 +524,7 @@ inline void Base_matrix<Master_matrix>::print()

template<class Master_matrix>
inline void Base_matrix<Master_matrix>::_orderRowsIfNecessary(){
if constexpr (Master_matrix::Option_list::has_column_and_row_swaps){
if constexpr (Master_matrix::Option_list::has_column_and_row_swaps || Master_matrix::Option_list::has_vine_update){
if (swap_opt::rowSwapped_) swap_opt::_orderRows();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,10 @@ inline Boundary_matrix<Master_matrix> &Boundary_matrix<Master_matrix>::operator=
template<class Master_matrix>
inline void Boundary_matrix<Master_matrix>::print()
{
std::cout << "Base_matrix:\n";
if constexpr (activeSwapOption){
if (swap_opt::rowSwapped_) swap_opt::_orderRows();
}
std::cout << "Boundary_matrix:\n";
for (unsigned int i = 0; i < nextInsertIndex_; ++i){
Column_type& col = matrix_[i];
for (auto e : col.get_content(nextInsertIndex_)){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class RU_matrix
//avoid calling with specialized options or make it such that it makes sense for persistence
//=================================================================
void add_to(index sourceColumnIndex, index targetColumnIndex);
void add_to(index sourceColumnIndex, const Field_element_type& coefficient, index targetColumnIndex);
void add_to(const Field_element_type& coefficient, index sourceColumnIndex, index targetColumnIndex);
void add_to(index sourceColumnIndex, const Field_element_type& coefficient, index targetColumnIndex); //do not call with vine updates
void add_to(const Field_element_type& coefficient, index sourceColumnIndex, index targetColumnIndex); //do not call with vine updates

void zero_cell(index columnIndex, index rowIndex, bool inR = true);
void zero_column(index columnIndex, bool inR = true);
Expand Down Expand Up @@ -270,7 +270,10 @@ template<class Master_matrix>
inline void RU_matrix<Master_matrix>::add_to(index sourceColumnIndex, index targetColumnIndex)
{
reducedMatrixR_.add_to(sourceColumnIndex, targetColumnIndex);
mirrorMatrixU_.add_to(sourceColumnIndex, targetColumnIndex);
if constexpr (Master_matrix::Option_list::has_vine_update)
mirrorMatrixU_.add_to(targetColumnIndex, sourceColumnIndex);
else
mirrorMatrixU_.add_to(sourceColumnIndex, targetColumnIndex);
}

template<class Master_matrix>
Expand Down Expand Up @@ -406,7 +409,10 @@ inline void RU_matrix<Master_matrix>::_reduce()
while (pivot != -1 && currIndex != -1){
if constexpr (Master_matrix::Option_list::is_z2){
curr += reducedMatrixR_.get_column(currIndex);
mirrorMatrixU_.get_column(i) += mirrorMatrixU_.get_column(currIndex);
if constexpr (Master_matrix::Option_list::has_vine_update)
mirrorMatrixU_.get_column(currIndex) += mirrorMatrixU_.get_column(i);
else
mirrorMatrixU_.get_column(i) += mirrorMatrixU_.get_column(currIndex);
} else {
Column_type &toadd = reducedMatrixR_.get_column(currIndex);
typename Master_matrix::Field_type coef = curr.get_pivot_value();
Expand Down Expand Up @@ -492,7 +498,10 @@ inline void RU_matrix<Master_matrix>::_reduce_last_column()
while (pivot != -1 && currIndex != -1){
if constexpr (Master_matrix::Option_list::is_z2){
curr += reducedMatrixR_.get_column(currIndex);
mirrorMatrixU_.get_column(nextInsertIndex_) += mirrorMatrixU_.get_column(currIndex);
if constexpr (Master_matrix::Option_list::has_vine_update)
mirrorMatrixU_.get_column(currIndex) += mirrorMatrixU_.get_column(nextInsertIndex_);
else
mirrorMatrixU_.get_column(nextInsertIndex_) += mirrorMatrixU_.get_column(currIndex);
} else {
Column_type &toadd = reducedMatrixR_.get_column(currIndex);
typename Master_matrix::Field_type coef = curr.get_pivot_value();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,40 @@ inline RU_representative_cycles<Master_matrix>::RU_representative_cycles(RU_repr
template<class Master_matrix>
inline void RU_representative_cycles<Master_matrix>::update_representative_cycles()
{
birthToCycle_.clear();
birthToCycle_.resize(_matrix()->reducedMatrixR_.get_number_of_columns(), -1);
for (unsigned int i = 0; i < _matrix()->reducedMatrixR_.get_number_of_columns(); i++){
if (_matrix()->reducedMatrixR_.is_zero_column(i)){
representativeCycles_.push_back(cycle_type());
if constexpr (Master_matrix::Option_list::has_vine_update){
birthToCycle_.clear();
birthToCycle_.resize(_matrix()->reducedMatrixR_.get_number_of_columns(), -1);
unsigned int c = 0;
for (unsigned int i = 0; i < _matrix()->reducedMatrixR_.get_number_of_columns(); i++){
if (_matrix()->reducedMatrixR_.is_zero_column(i)){
birthToCycle_[i] = c;
++c;
}
}
representativeCycles_.clear();
representativeCycles_.resize(c);
for (unsigned int i = 0; i < _matrix()->mirrorMatrixU_.get_number_of_columns(); i++){
for (const auto& cell : _matrix()->mirrorMatrixU_.get_column(i)){
representativeCycles_.back().push_back(cell.get_row_index());
auto idx = birthToCycle_[cell.get_row_index()];
if (idx != -1){
representativeCycles_[idx].push_back(i);
}
}
}
} else {
birthToCycle_.clear();
birthToCycle_.resize(_matrix()->reducedMatrixR_.get_number_of_columns(), -1);
for (unsigned int i = 0; i < _matrix()->reducedMatrixR_.get_number_of_columns(); i++){
if (_matrix()->reducedMatrixR_.is_zero_column(i)){
representativeCycles_.push_back(cycle_type());
for (const auto& cell : _matrix()->mirrorMatrixU_.get_column(i)){
representativeCycles_.back().push_back(cell.get_row_index());
}
if constexpr (std::is_same_v<typename Master_matrix::Column_type, typename Master_matrix::Heap_column_type>
|| std::is_same_v<typename Master_matrix::Column_type, typename Master_matrix::Unordered_set_column_type>)
std::sort(representativeCycles_.back().begin(), representativeCycles_.back().end());
birthToCycle_[i] = representativeCycles_.size() - 1;
}
if constexpr (std::is_same_v<typename Master_matrix::Column_type, typename Master_matrix::Heap_column_type>
|| std::is_same_v<typename Master_matrix::Column_type, typename Master_matrix::Unordered_set_column_type>)
std::sort(representativeCycles_.back().begin(), representativeCycles_.back().end());
birthToCycle_[i] = representativeCycles_.size() - 1;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ inline bool RU_vine_swap<Master_matrix>::vine_swap_with_z_eq_1_case(index index)
bool iiIsPositive = _matrix()->reducedMatrixR_.is_zero_column(index + 1);

if (iIsPositive && iiIsPositive) {
_matrix()->mirrorMatrixU_.zero_cell(index + 1, index);
_matrix()->mirrorMatrixU_.zero_cell(index, index + 1);
return _positive_vine_swap(index);
} else if (!iIsPositive && !iiIsPositive)
return _negative_vine_swap(index);
Expand All @@ -143,26 +143,26 @@ inline bool RU_vine_swap<Master_matrix>::vine_swap(index index)
_swap_at_index(index);
return true;
}
if (!_matrix()->mirrorMatrixU_.is_zero_cell(index + 1, index)){
_matrix()->mirrorMatrixU_.zero_cell(index + 1, index);
if (!_matrix()->mirrorMatrixU_.is_zero_cell(index, index + 1)){
_matrix()->mirrorMatrixU_.zero_cell(index, index + 1);
}
return _positive_vine_swap(index);
} else if (!iIsPositive && !iiIsPositive) {
if (_matrix()->reducedMatrixR_.get_column_dimension(index) != _matrix()->reducedMatrixR_.get_column_dimension(index + 1) || _matrix()->mirrorMatrixU_.is_zero_cell(index + 1, index)){
if (_matrix()->reducedMatrixR_.get_column_dimension(index) != _matrix()->reducedMatrixR_.get_column_dimension(index + 1) || _matrix()->mirrorMatrixU_.is_zero_cell(index, index + 1)){
_negative_transpose(index);
_swap_at_index(index);
return true;
}
return _negative_vine_swap(index);
} else if (iIsPositive && !iiIsPositive) {
if (_matrix()->reducedMatrixR_.get_column_dimension(index) != _matrix()->reducedMatrixR_.get_column_dimension(index + 1) || _matrix()->mirrorMatrixU_.is_zero_cell(index + 1, index)){
if (_matrix()->reducedMatrixR_.get_column_dimension(index) != _matrix()->reducedMatrixR_.get_column_dimension(index + 1) || _matrix()->mirrorMatrixU_.is_zero_cell(index, index + 1)){
_positive_negative_transpose(index);
_swap_at_index(index);
return true;
}
return _positive_negative_vine_swap(index);
} else {
if (_matrix()->reducedMatrixR_.get_column_dimension(index) != _matrix()->reducedMatrixR_.get_column_dimension(index + 1) || _matrix()->mirrorMatrixU_.is_zero_cell(index + 1, index)){
if (_matrix()->reducedMatrixR_.get_column_dimension(index) != _matrix()->reducedMatrixR_.get_column_dimension(index + 1) || _matrix()->mirrorMatrixU_.is_zero_cell(index, index + 1)){
_negative_positive_transpose(index);
_swap_at_index(index);
return true;
Expand Down Expand Up @@ -213,14 +213,14 @@ inline int RU_vine_swap<Master_matrix>::_get_pair(index index) {
template<class Master_matrix>
inline void RU_vine_swap<Master_matrix>::_swap_at_index(index index){
_matrix()->reducedMatrixR_.swap_at_indices(index, index + 1);
_matrix()->mirrorMatrixU_.swap_at_indices(index + 1, index);
_matrix()->mirrorMatrixU_.swap_at_indices(index, index + 1);
}

template<class Master_matrix>
inline void RU_vine_swap<Master_matrix>::_add_to(index sourceIndex, index targetIndex)
{
_matrix()->reducedMatrixR_.add_to(sourceIndex, targetIndex);
_matrix()->mirrorMatrixU_.add_to(sourceIndex, targetIndex);
_matrix()->mirrorMatrixU_.add_to(targetIndex, sourceIndex);
}

template<class Master_matrix>
Expand Down Expand Up @@ -353,7 +353,7 @@ inline bool RU_vine_swap<Master_matrix>::_negative_vine_swap(index index)
template<class Master_matrix>
inline bool RU_vine_swap<Master_matrix>::_positive_negative_vine_swap(index index)
{
_matrix()->mirrorMatrixU_.zero_cell(index + 1, index);
_matrix()->mirrorMatrixU_.zero_cell(index, index + 1);

_swap_at_index(index);
_positive_negative_transpose(index);
Expand Down
Loading

0 comments on commit d02e1ce

Please sign in to comment.