Tools.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief Helper functions for linear algebra component.
5  *
6  *
7  *
8  * \author O.Krause M.Thuma
9  * \date 20102011
10  *
11  *
12  * \par Copyright 1995-2015 Shark Development Team
13  *
14  * <BR><HR>
15  * This file is part of Shark.
16  * <http://image.diku.dk/shark/>
17  *
18  * Shark is free software: you can redistribute it and/or modify
19  * it under the terms of the GNU Lesser General Public License as published
20  * by the Free Software Foundation, either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * Shark is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU Lesser General Public License for more details.
27  *
28  * You should have received a copy of the GNU Lesser General Public License
29  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
30  *
31  */
32 #ifndef SHARK_LINALG_TOOLS_H
33 #define SHARK_LINALG_TOOLS_H
34 
35 namespace shark {
36 namespace blas{
37 
38 /**
39 * \ingroup shark_globals
40 *
41 * @{
42 */
43 
44 ///\brief partitions the matrix in 4 blocks defined by one splitting point (i,j).
45 ///
46 /// the blocks are defined using the intervals [0,...,i), [i,...,mat.size1()]
47 /// and [0,...,j), [j,...,mat.size2()]
48 template<class Matrix>
49 class Blocking{
50 public:
51 
52  Blocking(Matrix& matrix,std::size_t i, std::size_t j)
53  : m_upperLeft(subrange(matrix,0,i,0,j))
54  , m_upperRight(subrange(matrix,0,i,j,matrix.size2()))
55  , m_lowerLeft(subrange(matrix,i,matrix.size1(),0,j))
56  , m_lowerRight(subrange(matrix,i,matrix.size1(),j,matrix.size2()))
57  {}
58 
59  /// \brief Returns the lower left block of the matrix.
61  return m_upperLeft;
62  }
63  /// \brief Returns the upper right block of the matrix.
65  return m_upperRight;
66  }
67  /// \brief Returns the lower left block of the matrix.
69  return m_lowerLeft;
70  }
71 
72  /// \brief Returns the lower right block of the matrix.
74  return m_lowerRight;
75  }
76 
77  /// \brief Returns the lower left block of the matrix.
79  return m_upperLeft;
80  }
81  /// \brief Returns the upper right block of the matrix.
83  return m_upperRight;
84  }
85  /// \brief Returns the lower left block of the matrix.
87  return m_lowerLeft;
88  }
89 
90  /// \brief Returns the lower right block of the matrix.
92  return m_lowerRight;
93  }
94 
95 
96 private:
97  matrix_range<Matrix> m_upperLeft;
98  matrix_range<Matrix> m_upperRight;
99  matrix_range<Matrix> m_lowerLeft;
100  matrix_range<Matrix> m_lowerRight;
101 };
102 
103 //////////////////////////////////////MATRIX BLOCKS////////////////////////////////////////
104 
105 /// \brief Returns the ith row of an upper triangular matrix excluding the elements right of the diagonal
106 template<class Matrix>
108  matrix_expression<Matrix> const& mat,
109  std::size_t i
110 ){
111  matrix_row<Matrix const> matRow = row(mat(),i);
112  return subrange(matRow,0,i);
113 }
114 /// \brief Returns the ith row of an upper triangular matrix excluding the elements right of the diagonal
115 template<class Matrix>
119  std::size_t i
120 ){
121  matrix_row<Matrix> matRow = row(mat(),i);
122  return subrange(matRow,0,i);
123 }
124 /// \brief Returns the elements in the ith row of a lower triangular matrix left of the diagonal
125 template<class Matrix>
127  matrix_expression<Matrix> const& mat,
128  std::size_t i
129 ){
130  matrix_row<Matrix const> matRow = row(mat(),i);
131  return subrange(matRow,0,i+1);
132 }
133 /// \brief Returns the elements in the ith row of a lower triangular matrix left of the diagonal
134 template<class Matrix>
138  std::size_t i
139 ){
140  matrix_row<Matrix> matRow = row(mat(),i);
141  return subrange(matRow,0,i+1);
142 }
143 
144 
145 
146 /// \brief Returns the elements in the i-th column of the matrix below the diagonal
147 template<class Matrix>
149  matrix_expression<Matrix> const& mat,
150  std::size_t i
151 ){
152  matrix_column<Matrix const> col = column(mat(),i);
153  return subrange(col,i+1,mat().size2());
154 }
155 /// \brief Returns the elements in the i-th column of the matrix below the diagonal
156 template<class Matrix>
160  std::size_t i
161 ){
162  matrix_column<Matrix> col = column(mat(),i);
163  return subrange(col,i+1,mat().size2());
164 }
165 
166 /// \brief Returns the elements in the i-th column of the matrix excluding the zero elements
167 template<class Matrix>
169  matrix_expression<Matrix> const& mat,
170  std::size_t i
171 ){
172  matrix_column<Matrix const> col = column(mat(),i);
173  return subrange(col,i,mat().size2());
174 }
175 /// \brief Returns the elements in the i-th column of the matrix excluding the zero elements
176 template<class Matrix>
179  std::size_t i
180 ){
181  matrix_column<Matrix> col = column(mat(),i);
182  return subrange(col,i,mat().size2());
183 }
184 /** @}*/
185 }}
186 #endif