CppNoddy  0.92
Loading...
Searching...
No Matches
DenseMatrix.h
Go to the documentation of this file.
1/// \file DenseMatrix.h
2/// A matrix class that constructs a DENSE matrix as
3/// an STL Vector of DenseVectors.
4
5#ifndef DENSEMATRIX_H
6#define DENSEMATRIX_H
7
8#include <vector>
10#include <DenseVector.h>
11
12namespace CppNoddy {
13
14 /// A matrix class that constructs a DENSE matrix as a
15 /// row major std::vector of DenseVectors. This is generally a nice
16 /// way to implement the matrix, but the data elements [i][j] are
17 /// not necessarily in contiguous row_major format in memory
18 /// because there is a system dependent padding at the end
19 /// of each row vector. Thus, in general, &[i][j+1] - &[i][j] !=
20 /// &[i][0] - &[i-1][Nc] .. ie. the step may be larger from the
21 /// end of row i-1 to the start of row i. Typically, the data must
22 /// be copied into contiguous memory for use in external libraries
23 /// that take base pointers & assume uniform steps between elements.
24 template <typename _Type>
25 class DenseMatrix : public Sequential_Matrix_base<_Type> {
26 public:
27
28 /// Typedef iterator types
29 typedef typename std::vector<DenseVector<_Type> >::iterator row_iter;
30 typedef typename std::vector<DenseVector<_Type> >::const_iterator row_citer;
31 typedef typename std::vector<DenseVector<_Type> >::reverse_iterator row_riter;
32 typedef typename std::vector<DenseVector<_Type> >::const_reverse_iterator row_criter;
37
38 /// Allow empty construction
40
41 /// Noddy Matrix constructor with a specified fill data.
42 /// \param rows The number of rows in the matrix.
43 /// \param cols The number of columns in the matrix.
44 /// \param fill The entry to be placed in all elements.
45 DenseMatrix(const std::size_t & rows,
46 const std::size_t & cols,
47 const _Type & fill);
48
49 /// Construct a Noddy Matrix from a contiguous set of data.
50 /// This will be nasty if you pass the wrong pointer, but is
51 /// useful in interfacing with external libraries.
52 /// This assumes the contiguous data is in row_major format.
53 /// \param rows The number of rows in the matrix.
54 /// \param cols The number of columns in the matrix.
55 /// \param p A pointer to the start of the data.
56 DenseMatrix(const std::size_t& rows,
57 const std::size_t& cols,
58 const _Type* p);
59
60 /// Construct a dense matrix from its banded counterpart.
61 /// \param source The banded matrix to be used in the construction.
63 // get the number of off diagonal elts
64 int l = source.noffdiag();
65 // banded matrix class is always square
66 int n = source.nrows();
67 for(int row = 0; row < n; ++row) {
68 DenseVector<_Type> vecrow(n, 0.0);
69 for(int col = std::max(row - l, 0);
70 col < std::min(n, row + l + 1);
71 ++col) {
72 vecrow[ col ] = source(row, col);
73 }
74 m_matrix.push_back(vecrow);
75 }
76 m_nr = m_nc = n;
77 }
78
79
80 /// Copy constructor.
81 /// \param source The source object to be copied
82 DenseMatrix(const DenseMatrix& source);
83
84 /// Assignment operator.
85 /// \param source The source object for the assignment
86 /// \return The newly assigned object
87 DenseMatrix& operator=(const DenseMatrix& source);
88
90
91 /// Access operator
92 const _Type& operator()(const std::size_t& row, const std::size_t& col) const;
93 /// Access operator
94 _Type& operator()(const std::size_t& row, const std::size_t& col);
95 /// Access operator
96 const _Type& get(const std::size_t& row, const std::size_t& col) const;
97 /// Access operator
98 _Type& set(const std::size_t& row, const std::size_t& col);
99
100 /// SIMD operator sugar
101 DenseMatrix<_Type> operator*(const double& m) const {
102 DenseMatrix<_Type> temp(*this);
103 temp.scale(m);
104 return temp;
105 }
107 DenseMatrix<_Type> temp(*this);
108 for(std::size_t row = 0; row < m_nr; ++row) {
109 for(std::size_t col = 0; col < m_nc; ++col) {
110 temp(row, col) += A(row, col);
111 }
112 }
113 return temp;
114 }
116 DenseMatrix<_Type> temp(*this);
117 for(std::size_t row = 0; row < m_nr; ++row) {
118 for(std::size_t col = 0; col < m_nc; ++col) {
119 temp(row, col) -= A(row, col);
120 }
121 }
122 return temp;
123 }
124
125 /// \return The number of rows
126 std::size_t nrows() const;
127 /// \return The number of columns
128 std::size_t ncols() const;
129 /// \return The number of elements
130 std::size_t nelts() const;
131
132 /// Scale all matrix elements by a scalar
133 /// \param mult The scalar multiplier
134 void scale(const _Type& mult);
135
136 /// Transpose the matrix
137 void transpose();
138
139 /// Return the maximum one_norm of all rows
140 double one_norm() const;
141
142 /// Rreturn the maximum two_norm of all rows
143 double two_norm() const;
144
145 /// Return the maximum inf_norm of all rows
146 double inf_norm() const;
147
148 /// Return the sum of the two_norm of all rows
149 double frob_norm() const;
150
151
152 /// Right multiply the matrix by a DENSE vector
153 /// \param x The DENSE vector to be multiplied
154 /// \return The DENSE vector of the multiplication
156
157 /// Output the matrix to std::cout
158 void dump() const;
159
160 //
161 // NON-INHERITED MEMBER FUNCTIONS
162 //
163
164 /// Assign a value to the matrix but keep the same
165 /// geometry.
166 /// \param elt The value to be assigned to all entries
167 void assign(_Type elt) {
168 m_matrix.assign(m_matrix.size(), DenseVector<_Type>(m_nc, elt));
169 }
170
171 /// Pass through of iterator calls.
172 /// \return Iterator to the begin row
174 return m_matrix.begin();
175 }
176
177 /// Pass through of iterator calls.
178 /// \return Iterator to the end row
180 return m_matrix.end();
181 }
182
183 /// Pass through of iterator calls.
184 /// \return Reverse iterator to the begin row
186 return m_matrix.rbegin();
187 }
188
189 /// Pass through of iterator calls.
190 /// \return Reverse iterator to the end row
192 return m_matrix.rend();
193 }
194
195 /// Pass through of iterator calls.
196 /// \return Const iterator to the begin row
197 row_citer begin() const {
198 return m_matrix.begin();
199 }
200
201 /// Pass through of iterator calls.
202 /// \return Const iterator to the end row
203 row_citer end() const {
204 return m_matrix.end();
205 }
206
207 /// Pass through of iterator calls.
208 /// \return Const reverse iterator to the begin row
210 return m_matrix.rbegin();
211 }
212
213 /// Pass through of iterator calls.
214 /// \return Const reverse iterator to the end row
215 row_criter rend() const {
216 return m_matrix.rend();
217 }
218
219 /// Operator overloading for ROW access
220 /// \param row The row to access
221 /// \return The DENSE vector of the row data
222 DenseVector<_Type>& operator[](const std::size_t& row);
223
224 /// Operator overloading for ROW access
225 /// \param row The row to access
226 /// \return The DENSE vector of the row data
227 const DenseVector<_Type>& operator[](const std::size_t& row) const;
228
229 /// Add a DENSE matrix to this object
230 /// \param b The DENSE matrix to add to 'this'
231 void add(const DenseMatrix<_Type>& b);
232
233 /// Subtract a DENSE matrix from this object
234 /// \param b The DENSE matrix to subtract from 'this'
235 void sub(const DenseMatrix<_Type>& b);
236
237 /// Right-multiply by a DENSE matrix and return the result
238 /// \param b The matrix to right-multiply by
239 /// \return The matrix result of the multiplication
241
242 /// Set a column of the matrix
243 /// \param col The column to be set
244 /// \param x The DENSE vector of column information
245 void set_col(const std::size_t& col, const DenseVector<_Type>& x);
246
247 /// Get a column of the matrix
248 /// \param col The column to get
249 /// \return A DENSE vector of the column of data
250 DenseVector<_Type> get_col(const std::size_t& col) const;
251
252 /// Conversion to contiguous data in row major format
253 /// Inefficient ... the void method is preferred
254 /// \param padding An integer padding hack
255 /// \return A DENSE vector containing the matrix
256 DenseVector<double> matrix_to_vector(const std::size_t &padding = 0) const;
257
258 /// Conversion to contiguous data in row major format
259 /// \param padding An integer padding hack
260 /// \param p A DENSE vector containing the matrix
261 void matrix_to_vector(DenseVector<double> &p, const std::size_t &padding = 0) const;
262
263 /// Find the maximum abs value in a column
264 /// \param col The column offset to search through
265 /// \param row_min Iterator to the begin row
266 /// \param row_max Iterator to final row (NOT INCLUSIVE)
267 /// \return An iterator to the row that contains the maximum value
268 row_iter max_in_col(const std::size_t& col, row_iter row_min, row_iter row_max) const;
269
270 private:
271
272 /// An NVector of NVectors.
273 std::vector< DenseVector<_Type> > m_matrix;
274 /// The number of rows in the matrix.
275 std::size_t m_nr;
276 /// The number of columns in the matrix.
277 std::size_t m_nc;
278
279 }
280 ; // END CLASS
281
282
283 // INLINE METHODS BELOW
284
285
286 template <typename _Type>
287 inline const _Type& DenseMatrix<_Type>::operator()(const std::size_t& row,
288 const std::size_t& col) const {
289#ifdef PARANOID
290 if((row > m_nr) || (row < 0)) {
291 std::string problem("The DenseMatrix.() has a range error.\n");
292 throw ExceptionRange(problem, m_nr, row, m_nc, col);
293 }
294 if((col > m_nc) || (col < 0)) {
295 std::string problem("The DenseMatrix.() has a range error.\n");
296 throw ExceptionRange(problem, m_nr, row, m_nc, col);
297 }
298#endif
299 return m_matrix[ row ][ col ];
300 }
301
302 template <typename _Type>
303 inline _Type& DenseMatrix<_Type>::operator()(const std::size_t& row,
304 const std::size_t& col) {
305#ifdef PARANOID
306 if((row > m_nr) || (row < 0)) {
307 std::string problem("The DenseMatrix.() has a range error.\n");
308 throw ExceptionRange(problem, m_nr, row, m_nc, col);
309 }
310 if((col > m_nc) || (col < 0)) {
311 std::string problem("The DenseMatrix.() has a range error.\n");
312 throw ExceptionRange(problem, m_nr, row, m_nc, col);
313 }
314#endif
315 return m_matrix[ row ][ col ];
316 }
317
318 template <typename _Type>
319 inline const _Type& DenseMatrix<_Type>::get
320 (const std::size_t& row, const std::size_t& col) const {
321#ifdef PARANOID
322 if((row > m_nr) || (row < 0)) {
323 std::string problem("The DenseMatrix.get has a range error.\n");
324 throw ExceptionRange(problem, m_nr, row, m_nc, col);
325 }
326
327 if((col > m_nc) || (col < 0)) {
328 std::string problem("The DenseMatrix.get has a range error.\n");
329 throw ExceptionRange(problem, m_nr, row, m_nc, col);
330 }
331#endif
332 return m_matrix[ row ][ col ];
333 }
334
335 template <typename _Type>
337 (const std::size_t& row,
338 const std::size_t& col) {
339#ifdef PARANOID
340 if((row > m_nr) || (row < 0)) {
341 std::string problem("The DenseMatrix.set has a range error.\n");
342 throw ExceptionRange(problem, m_nr, row, m_nc, col);
343 }
344 if((col > m_nc) || (col < 0)) {
345 std::string problem("The DenseMatrix.set has a range error.\n");
346 throw ExceptionRange(problem, m_nr, row, m_nc, col);
347 }
348#endif
349 return m_matrix[ row ][ col ];
350 }
351
352 template <typename _Type>
353 inline DenseVector<_Type>& DenseMatrix<_Type>::operator [](const std::size_t& row) {
354#ifdef PARANOID
355 if((row > m_nr) || (row < 0)) {
356 std::string problem("The DenseMatrix.get_row has a range error.\n");
357 throw ExceptionRange(problem, m_nr, row);
358 }
359#endif
360 return m_matrix[ row ];
361 }
362
363 template <typename _Type>
364 inline const DenseVector<_Type>& DenseMatrix<_Type>::operator [](const std::size_t& row) const {
365#ifdef PARANOID
366 if((row > m_nr) || (row < 0)) {
367 std::string problem("The DenseMatrix.get_row has a range error.\n");
368 throw ExceptionRange(problem, m_nr, row);
369 }
370#endif
371 return m_matrix[ row ];
372 }
373
374 template <typename _Type>
375 inline std::size_t DenseMatrix<_Type>::nrows() const {
376 return m_nr;
377 }
378
379 template <typename _Type>
380 inline std::size_t DenseMatrix<_Type>::ncols() const {
381 return m_nc;
382 }
383
384
385} // end namespace
386
387
388#endif
Specification for a templated DenseVector class – a dense, dynamic, vector object.
A base matrix class to ensure a consistent interface between the inheriting dense/banded matrix class...
A matrix class that constructs a BANDED matrix.
Definition: BandedMatrix.h:16
A matrix class that constructs a DENSE matrix as a row major std::vector of DenseVectors.
Definition: DenseMatrix.h:25
std::vector< DenseVector< _Type > >::reverse_iterator row_riter
Definition: DenseMatrix.h:31
void assign(_Type elt)
Assign a value to the matrix but keep the same geometry.
Definition: DenseMatrix.h:167
std::size_t nelts() const
Definition: DenseMatrix.cpp:71
void dump() const
Output the matrix to std::cout.
std::vector< DenseVector< _Type > >::const_reverse_iterator row_criter
Definition: DenseMatrix.h:32
row_citer begin() const
Pass through of iterator calls.
Definition: DenseMatrix.h:197
DenseVector< _Type >::elt_iter elt_iter
Definition: DenseMatrix.h:33
double one_norm() const
Return the maximum one_norm of all rows.
DenseMatrix & operator=(const DenseMatrix &source)
Assignment operator.
Definition: DenseMatrix.cpp:61
row_iter begin()
Pass through of iterator calls.
Definition: DenseMatrix.h:173
void matrix_to_vector(DenseVector< double > &p, const std::size_t &padding=0) const
Conversion to contiguous data in row major format.
std::size_t nrows() const
Definition: DenseMatrix.h:375
DenseMatrix< _Type > operator*(const double &m) const
SIMD operator sugar.
Definition: DenseMatrix.h:101
row_citer end() const
Pass through of iterator calls.
Definition: DenseMatrix.h:203
row_iter max_in_col(const std::size_t &col, row_iter row_min, row_iter row_max) const
Find the maximum abs value in a column.
void set_col(const std::size_t &col, const DenseVector< _Type > &x)
Set a column of the matrix.
row_iter end()
Pass through of iterator calls.
Definition: DenseMatrix.h:179
row_riter rbegin()
Pass through of iterator calls.
Definition: DenseMatrix.h:185
DenseVector< double > matrix_to_vector(const std::size_t &padding=0) const
Conversion to contiguous data in row major format Inefficient ... the void method is preferred.
DenseVector< _Type >::elt_criter elt_criter
Definition: DenseMatrix.h:36
std::vector< DenseVector< _Type > >::iterator row_iter
Typedef iterator types.
Definition: DenseMatrix.h:29
_Type & operator()(const std::size_t &row, const std::size_t &col)
Access operator.
Definition: DenseMatrix.h:303
_Type & set(const std::size_t &row, const std::size_t &col)
Access operator.
Definition: DenseMatrix.h:337
DenseVector< _Type > multiply(const DenseVector< _Type > &x) const
Right multiply the matrix by a DENSE vector.
DenseMatrix(const BandedMatrix< _Type > &source)
Construct a dense matrix from its banded counterpart.
Definition: DenseMatrix.h:62
row_criter rend() const
Pass through of iterator calls.
Definition: DenseMatrix.h:215
void transpose()
Transpose the matrix.
row_riter rend()
Pass through of iterator calls.
Definition: DenseMatrix.h:191
double frob_norm() const
Return the sum of the two_norm of all rows.
DenseVector< _Type > get_col(const std::size_t &col) const
Get a column of the matrix.
const DenseVector< _Type > & operator[](const std::size_t &row) const
Operator overloading for ROW access.
Definition: DenseMatrix.h:364
void add(const DenseMatrix< _Type > &b)
Add a DENSE matrix to this object.
Definition: DenseMatrix.cpp:76
const _Type & operator()(const std::size_t &row, const std::size_t &col) const
Access operator.
Definition: DenseMatrix.h:287
double inf_norm() const
Return the maximum inf_norm of all rows.
DenseMatrix< _Type > operator-(const DenseMatrix< _Type > &A) const
Definition: DenseMatrix.h:115
DenseVector< _Type > & operator[](const std::size_t &row)
Operator overloading for ROW access.
Definition: DenseMatrix.h:353
void scale(const _Type &mult)
Scale all matrix elements by a scalar.
void sub(const DenseMatrix< _Type > &b)
Subtract a DENSE matrix from this object.
Definition: DenseMatrix.cpp:88
row_criter rbegin() const
Pass through of iterator calls.
Definition: DenseMatrix.h:209
std::vector< DenseVector< _Type > >::const_iterator row_citer
Definition: DenseMatrix.h:30
DenseMatrix()
Allow empty construction.
Definition: DenseMatrix.cpp:17
DenseVector< _Type >::elt_riter elt_riter
Definition: DenseMatrix.h:35
const _Type & get(const std::size_t &row, const std::size_t &col) const
Access operator.
Definition: DenseMatrix.h:320
double two_norm() const
Rreturn the maximum two_norm of all rows.
std::size_t ncols() const
Definition: DenseMatrix.h:380
DenseVector< _Type >::elt_citer elt_citer
Definition: DenseMatrix.h:34
DenseMatrix< _Type > operator+(const DenseMatrix< _Type > &A) const
Definition: DenseMatrix.h:106
An DenseVector class – a dense vector object.
Definition: DenseVector.h:34
std::vector< _Type >::const_reverse_iterator elt_criter
Definition: DenseVector.h:40
std::vector< _Type >::reverse_iterator elt_riter
Definition: DenseVector.h:39
std::vector< _Type >::const_iterator elt_citer
Definition: DenseVector.h:38
std::vector< _Type >::iterator elt_iter
Definition: DenseVector.h:37
An exception to indicate that a CppNoddy container has been accessed with index/indices outside the m...
Definition: Exceptions.h:117
A base matrix class for sequential matrices.
A collection of OO numerical routines aimed at simple (typical) applied problems in continuum mechani...

© 2012

R.E. Hewitt