permutation.hpp
Go to the documentation of this file.
1 /*!
2  * \brief Permutations of vectors and matrices
3  *
4  * \author O. Krause
5  * \date 2013
6  *
7  *
8  * \par Copyright 1995-2015 Shark Development Team
9  *
10  * <BR><HR>
11  * This file is part of Shark.
12  * <http://image.diku.dk/shark/>
13  *
14  * Shark is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU Lesser General Public License as published
16  * by the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * Shark is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
26  *
27  */
28 #ifndef SHARK_LINALG_BLAS_PERMUTATION_HPP
29 #define SHARK_LINALG_BLAS_PERMUTATION_HPP
30 
31 #include "vector.hpp"
32 
33 namespace shark { namespace blas{
34 struct permutation_matrix:public vector<std::size_t> {
35  // Construction and destruction
36  explicit permutation_matrix(size_type size):vector<std::size_t> (size){
37  for (size_type i = 0; i < size; ++ i)
38  (*this)(i) = i;
39  }
40 
41  explicit permutation_matrix(vector<std::size_t> const& init): vector<std::size_t>(init){ }
42 
43  // Assignment
46  return *this;
47  }
48 };
49 
50 ///\brief implements row pivoting at matrix A using permutation P
51 ///
52 ///by convention it is not allowed that P(i) < i.
53 template<class M>
55  for (std::size_t i = 0; i != P.size(); ++ i)
56  swap_rows(A(),i,P(i));
57 }
58 
59 ///\brief implements column pivoting of vector A using permutation P
60 ///
61 ///by convention it is not allowed that P(i) < i.
62 template<class V>
64  for (std::size_t i = 0; i != P.size(); ++ i)
65  std::swap(v()(i),v()(P(i)));
66 }
67 
68 ///\brief implements the inverse row pivoting of vector v using permutation P
69 ///
70 ///This is the inverse operation to swap_rows.
71 template<class V, class Permutation>
72 void swap_rows_inverted(Permutation const& P, vector_expression<V>& v){
73  for(std::size_t i = P.size(); i != 0; --i){
74  std::size_t k = i-1;
75  if(k != P(k)){
76  using std::swap;
77  swap(v()(k),v()(P(k)));
78  }
79  }
80 }
81 
82 ///\brief implements column pivoting at matrix A using permutation P
83 ///
84 ///by convention it is not allowed that P(i) < i.
85 template<class M>
87  for(std::size_t i = 0; i != P.size(); ++i)
88  swap_columns(A(),i,P(i));
89 }
90 
91 ///\brief implements the inverse row pivoting at matrix A using permutation P
92 ///
93 ///This is the inverse operation to swapRows.
94 template<class M>
96  for(std::size_t i = P.size(); i != 0; --i){
97  swap_rows(A(),i-1,P(i-1));
98  }
99 }
100 
101 ///\brief implements the inverse column pivoting at matrix A using permutation P
102 ///
103 ///This is the inverse operation to swapColumns.
104 template<class M>
106  for(std::size_t i = P.size(); i != 0; --i){
107  swap_columns(A(),i-1,P(i-1));
108  }
109 }
110 
111 ///\brief Implements full pivoting at matrix A using permutation P
112 ///
113 ///full pivoting does swap rows and columns such that the diagonal element
114 ///A_ii is then at position A_P(i)P(i)
115 ///by convention it is not allowed that P(i) < i.
116 template<class M>
118  for(std::size_t i = 0; i != P.size(); ++i){
119  swap_rows(A(),i,P(i));
120  swap_columns(A(),i,P(i));
121  }
122 }
123 ///\brief implements the inverse full pivoting at matrix A using permutation P
124 ///
125 ///This is the inverse operation to swap_full.
126 template<class M>
128  for(std::size_t i = P.size(); i != 0; --i){
129  swap_rows(A(),i-1,P(i-1));
130  swap_columns(A(),i-1,P(i-1));
131  }
132 }
133 
134 }}
135 #endif