CMR  1.3.0
matrix_reorder.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "permutations.hpp"
4 #include "matroid_transposed.hpp"
5 #include "matrix.hpp"
6 
7 namespace tu
8 {
9 
18  template <typename MatrixType>
19  inline void matrix_apply_row_permutation(MatrixType& matrix, const permutation& perm)
20  {
21  permutation p = perm;
22  for (size_t row = 0; row < matrix.size1(); ++row)
23  {
24  size_t target_row = p(row);
25  matrix_permute1(matrix, row, target_row);
26  p.rswap(row, target_row);
27  }
28  }
29 
38  template <typename MatrixType>
39  inline void matrix_apply_column_permutation(MatrixType& matrix, const permutation& perm)
40  {
41  matroid_transposed <MatrixType> transposed(matrix);
42  matrix_apply_row_permutation(transposed, perm);
43  }
44 
49  template <typename MatrixType, typename Less>
51  {
61  matrix_reorder_row_less(const MatrixType& matrix, size_t column_first, size_t column_beyond, Less less) :
62  matrix_(matrix), column_first_(column_first), column_beyond_(column_beyond), less_(less)
63  {
64 
65  }
66 
75  bool operator()(size_t a, size_t b)
76  {
77  for (size_t column = column_first_; column < column_beyond_; ++column)
78  {
79  typename MatrixType::value_type va = matrix_(a, column);
80  typename MatrixType::value_type vb = matrix_(b, column);
81 
82  if (less_(va, vb))
83  return true;
84  else if (less_(vb, va))
85  return false;
86  }
87 
88  return false;
89  }
90 
91  const MatrixType& matrix_;
92  size_t column_first_;
94  Less less_;
95  };
96 
109  template <typename MatrixType, typename ElementLess>
110  inline void matrix_reorder_rows(const MatrixType& matrix, size_t row_first, size_t row_beyond, size_t column_first, size_t column_beyond,
111  ElementLess element_less, permutation& result_permutation)
112  {
113  result_permutation.reset(matrix.size1());
114  matrix_reorder_row_less <MatrixType, ElementLess> less(matrix, column_first, column_beyond, element_less);
115  sort(result_permutation, row_first, row_beyond, less);
116  }
117 
130  template <typename MatrixType, typename ElementLess>
131  inline void matrix_reorder_columns(const MatrixType& matrix, size_t row_first, size_t row_beyond, size_t column_first, size_t column_beyond,
132  ElementLess element_less, permutation& result_permutation)
133  {
134  const matrix_transposed <MatrixType> transposed(matrix);
135  matrix_reorder_rows(transposed, column_first, column_beyond, row_first, row_beyond, element_less, result_permutation);
136  }
137 
149  template <typename MatrixType, typename ElementLess>
150  inline void matrix_reorder_rows(MatrixType& matrix, size_t row_first, size_t row_beyond, size_t column_first, size_t column_beyond,
151  ElementLess element_less)
152  {
153  permutation perm;
154  matrix_reorder_rows(matrix, row_first, row_beyond, column_first, column_beyond, element_less, perm);
155  matrix_apply_row_permutation(matrix, perm);
156  }
157 
169  template <typename MatrixType, typename ElementLess>
170  inline void matrix_reorder_columns(MatrixType& matrix, size_t row_first, size_t row_beyond, size_t column_first, size_t column_beyond,
171  ElementLess element_less)
172  {
173  matrix_transposed <MatrixType> transposed(matrix);
174  matrix_reorder_rows(matrix, column_first, column_beyond, row_first, row_beyond, element_less);
175  }
176 
177 } /* namespace tu */
Definition: matrix_transposed.hpp:47
Definition: matroid_transposed.hpp:14
Definition: permutations.hpp:44
void reset(size_t new_size)
Definition: permutations.hpp:111
void rswap(value_type a, value_type b)
Definition: permutations.hpp:199
Definition: algorithm.hpp:14
void sort(permutation &permutation, size_t first, size_t beyond, Less &less)
Definition: permutations.hpp:583
void matrix_permute1(MatrixType &matrix, size_t index1, size_t index2)
Definition: matrix.hpp:1723
void matrix_reorder_columns(const MatrixType &matrix, size_t row_first, size_t row_beyond, size_t column_first, size_t column_beyond, ElementLess element_less, permutation &result_permutation)
Definition: matrix_reorder.hpp:131
void matrix_reorder_rows(const MatrixType &matrix, size_t row_first, size_t row_beyond, size_t column_first, size_t column_beyond, ElementLess element_less, permutation &result_permutation)
Definition: matrix_reorder.hpp:110
void matrix_apply_column_permutation(MatrixType &matrix, const permutation &perm)
Definition: matrix_reorder.hpp:39
void matrix_apply_row_permutation(MatrixType &matrix, const permutation &perm)
Definition: matrix_reorder.hpp:19
Definition: matrix_reorder.hpp:51
bool operator()(size_t a, size_t b)
Definition: matrix_reorder.hpp:75
const MatrixType & matrix_
Definition: matrix_reorder.hpp:91
size_t column_first_
Definition: matrix_reorder.hpp:92
Less less_
Definition: matrix_reorder.hpp:94
size_t column_beyond_
Definition: matrix_reorder.hpp:93
matrix_reorder_row_less(const MatrixType &matrix, size_t column_first, size_t column_beyond, Less less)
Definition: matrix_reorder.hpp:61