CppNoddy  0.92
Loading...
Searching...
No Matches
SparseMatrix.h
Go to the documentation of this file.
1/// \file SparseMatrix.h
2/// A matrix class that constructs a SPARSE matrix as
3/// an STL Vector of SparseVectors, inheriting from Matrix_base.
4
5#ifndef SPARSEMATRIX_H
6#define SPARSEMATRIX_H
7
8#include <vector>
9#include <iostream>
10#include <iomanip>
11#include <fstream>
12#include <complex>
13
14#include <SparseVector.h>
15#include <DenseVector.h>
16#include <Exceptions.h>
18
19#if defined(PETSC_D) || defined(PETSC_Z)
20 #include "petsc.h"
21#endif
22
23namespace CppNoddy {
24
25 template <typename _SystemType>
26 class SparseLinearSystem;
27
28 /// A matrix class that constructs a SPARSE matrix as a
29 /// row major std::vector of SparseVectors.
30 template <typename _Type>
31 class SparseMatrix : public Sequential_Matrix_base<_Type> {
32 typedef typename std::map< std::size_t, _Type >::const_iterator citer;
33 typedef typename std::map< std::size_t, _Type >::iterator iter;
34
35 public:
36
37 /// Construct with a set number of rows
38 /// \param rows The number of rows in the matrix
39 /// \param cols The number of columns in the matrix
40 SparseMatrix(const std::size_t& rows, const std::size_t& cols);
41
42 /// Construct from a row permutation of another sparse matrix
43 /// \param source_rows Defines the permutation, row i of this matrix
44 /// is row source_rows[i] of the source
45 SparseMatrix(const SparseMatrix<_Type>& source, const std::vector<std::size_t>& source_rows);
46
47 /// Copy constructor.
48 /// \param source The source object to be copied
49 SparseMatrix(const SparseMatrix& source);
50
51 /// Assignment operator.
52 /// \param source The source object for the assignment
53 /// \return The newly assigned object
54 SparseMatrix& operator=(const SparseMatrix& source);
55
56 /// Default d-tor
58
59 /// Blank the contents of this matrix
60 void blank() {
61 m_matrix.clear();
62 m_matrix.reserve(m_nr);
63 SparseVector<_Type> sparse_row(m_nc);
64 for(std::size_t i = 0; i < m_nr; ++i) {
65 m_matrix.push_back(sparse_row);
66 }
67 }
68
69 /// Access operator
70 const _Type& operator()(const std::size_t& row, const std::size_t& col) const;
71
72 /// Access operator
73 _Type& operator()(const std::size_t& row, const std::size_t& col);
74 /// Access operator
75 const _Type& get(const std::size_t& row, const std::size_t& col) const;
76 /// Access operator
77 _Type& set(const std::size_t& row, const std::size_t& col);
78
79
80 /// Get a row of the matrix
81 /// \param row The index of the row to be get
82 /// \return A sparse vector of values in the row
83 SparseVector<_Type> get_row(const std::size_t& row) const {
84 return m_matrix[row];
85 }
86
87 /// Set a row of the matrix
88 /// \param row The index of the row to be set
89 /// \param A sparse vector of values to go into the row
90 void set_row(const std::size_t& row, const SparseVector<_Type>& row_vector) {
91 m_matrix[row] = row_vector;
92 }
93
94 /// Get the number of rows in the matrix
95 /// \return The number of rows
96 std::size_t nrows() const;
97
98 /// Get the number of columns in the matrix
99 /// \return The number of columns
100 std::size_t ncols() const;
101
102 /// Get the number of (non-zero) elements in the matrix
103 /// \return The number of (non-zero) elements
104 std::size_t nelts() const;
105
106 /// Scale the matrix by a scalar
107 /// \param mult The scalar multiplier
108 void scale(const _Type& mult);
109
110 /// Transpose the matrix in place
111 // void transpose();
112
113 /// \return The maximum one_norm of all rows
114 double one_norm() const;
115
116 /// \return The maximum two_norm of all rows
117 double two_norm() const;
118
119 /// \return The maximum inf_norm of all rows
120 double inf_norm() const;
121
122 /// \return The sum of the two_norm of all rows
123 double frob_norm() const;
124
125 /// Right-multiply by a DENSE vector
126 /// \param X The DENSE vector to be multiplied by
127 /// \return A DENSE vector of the result of the multiplication
129
130
131 /// Output the contents of the matrix to std::cout
132 void dump() const;
133
134 /// A simple method for dumping the matrix to a file
135 /// \param filename The filename to write the data to (will overwrite)
136 /// \param precision Precision of the output strings
137 void dump(std::string filename, int precision = 10) const {
138 std::ofstream dump;
139 dump.open(filename.c_str());
140 dump.precision(precision);
141 dump.setf(std::ios::showpoint);
142 dump.setf(std::ios::showpos);
143 dump.setf(std::ios::scientific);
144 for(std::size_t row = 0; row < m_nr; ++row) {
145 for(citer pos = m_matrix[row].begin(); pos != m_matrix[row].end(); ++pos) {
146 dump << row << " " << pos -> first << " " << pos -> second << "\n";
147 }
148 }
149 dump << "\n";
150 dump.close();
151 }
152
153 //
154 // NON-INHERITED MEMBER FUNCTIONS
155 //
156
157 /// Operator overloading for ROW access
158 /// \param row The row to access
159 /// \return The DENSE vector of the row data
160 SparseVector<_Type>& operator[](const std::size_t& row);
161
162 /// Operator overloading for ROW access
163 /// \param row The row to access
164 /// \return The DENSE vector of the row data
165 const SparseVector<_Type>& operator[](const std::size_t& row) const;
166
167
168#if defined (PETSC_D) || defined (PETSC_Z)
169 // PETSc is compiled separately for double or complex ... but only linked against
170 // once in the build process, so it's either/or.
171
172 /// Takes the contents of the SparseMatrix and converts it to a
173 /// standard compressed format for a specified row.
174 /// \param row_number The row index to extract the data for
175 /// \param storage A contiguous vector of the non-zero elts
176 /// (has to be allocated and big enough)
177 /// \param cols The column indices of each entry in the storage vector
178 /// (has to be allocated and big enough)
179 void get_row_petsc(PetscInt row_number, PetscScalar* storage, PetscInt* cols);
180
181 /// Extracts the number of non-zero elements in each row and returns them as a
182 /// PetscInt array of length m_nr. The array should be allocated on entry.
183 /// \param The array to fill with the number of non-zero elts in each row
184 /// (has to be allocated and big enough)
185 void nelts_all_rows(PetscInt* row_nnz) {
186 for(std::size_t i = 0; i < m_nr; ++i) {
187 row_nnz[i] = m_matrix[i].nelts();
188 }
189 }
190#endif
191
192 /// The number of non-zero elements in a specified row
193 /// \param row The row index to return the number of non-zero elts for
194 std::size_t nelts_in_row(int row) {
195 return m_matrix[row].nelts();
196 }
197
198 /// Find the maximum entry in a column -- used in the native solver.
199 /// \param col The column to search through
200 /// \param row_min The start row for the search
201 /// \param row_max The end row for the search (NOT INCLUSIVE)
202 std::size_t max_in_col(const std::size_t& col, const std::size_t& row_min,
203 const std::size_t& row_max) const;
204
205 /// Swap two rows in the matrix -- used in the native solver.
206 /// \param row1 The first row to be exchanged
207 /// \param row2 The second row to be exchanged
208 void row_swap(const std::size_t& row1, const std::size_t& row2);
209
210 private:
211
212 /// An STL vector of SparseVectors.
213 std::vector< SparseVector<_Type> > m_matrix;
214 /// The max number of rows in the matrix.
215 std::size_t m_nr;
216 /// The max number of columns in the matrix.
217 std::size_t m_nc;
218
219 template <typename _SystemType>
220 friend class SparseLinearSystem;
221
222 }
223 ; // END CLASS
224
225
226 // INLINED METHODS FOLLOW
227
228 template <typename _Type>
229 inline const _Type& SparseMatrix<_Type>::operator()(const std::size_t& row, const std::size_t& col) const {
230 return m_matrix[ row ].get(col);
231 }
232
233 template <typename _Type>
234 inline _Type& SparseMatrix<_Type>::operator()(const std::size_t& row, const std::size_t& col) {
235 return m_matrix[ row ][ col ];
236 }
237
238 template <typename _Type>
239 inline const _Type& SparseMatrix<_Type>::get(const std::size_t& row, const std::size_t& col) const {
240 return this -> operator()(row, col);
241 }
242
243 template <typename _Type>
244 inline _Type& SparseMatrix<_Type>::set(const std::size_t& row, const std::size_t& col) {
245 return this -> operator()(row, col);
246 }
247
248 template <typename _Type>
249 inline SparseVector<_Type>& SparseMatrix<_Type>::operator[](const std::size_t& row) {
250#ifdef PARANOID
251 if((row > m_nr) || (row < 0)) {
252 std::string problem("The SparseMatrix.get_row has a range error.\n");
253 throw ExceptionRange(problem, m_nr, row);
254 }
255#endif
256 return m_matrix[ row ];
257 }
258
259
260 template <typename _Type>
261 inline const SparseVector<_Type>& SparseMatrix<_Type>::operator[](const std::size_t& row) const {
262#ifdef PARANOID
263 if((row > m_nr) || (row < 0)) {
264 std::string problem("The SparseMatrix.get_row has a range error.\n");
265 throw ExceptionRange(problem, m_nr, row);
266 }
267#endif
268 return m_matrix[ row ];
269 }
270
271 template <typename _Type>
272 inline std::size_t SparseMatrix<_Type>::nrows() const {
273 return m_nr;
274 }
275
276 template <typename _Type>
277 inline std::size_t SparseMatrix<_Type>::ncols() const {
278 return m_nc;
279 }
280
281 template <typename _Type>
282 inline void SparseMatrix<_Type>::row_swap(const std::size_t& row1, const std::size_t& row2) {
283 // actually do the swap, rather than keep a row permutation vector.
284 // std::swap<SparseVector<_Type> > ( matrix[ row1 ], matrix[ row2 ] );
285 m_matrix[ row1 ].swap(m_matrix[ row2 ]);
286 }
287
288
289} // end namespace
290
291
292#endif
Specification for a templated DenseVector class – a dense, dynamic, vector object.
The collection of CppNoddy exceptions.
A base matrix class to ensure a consistent interface between the inheriting dense/banded matrix class...
A templated SparseVector class – a sparse, variable size, vector object.
An DenseVector class – a dense vector object.
Definition: DenseVector.h:34
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 linear system class for vector right-hand sides.
A matrix class that constructs a SPARSE matrix as a row major std::vector of SparseVectors.
Definition: SparseMatrix.h:31
void row_swap(const std::size_t &row1, const std::size_t &row2)
Swap two rows in the matrix – used in the native solver.
Definition: SparseMatrix.h:282
std::size_t nelts_in_row(int row)
The number of non-zero elements in a specified row.
Definition: SparseMatrix.h:194
void blank()
Blank the contents of this matrix.
Definition: SparseMatrix.h:60
void dump() const
Output the contents of the matrix to std::cout.
~SparseMatrix()
Default d-tor.
Definition: SparseMatrix.h:57
void dump(std::string filename, int precision=10) const
A simple method for dumping the matrix to a file.
Definition: SparseMatrix.h:137
_Type & set(const std::size_t &row, const std::size_t &col)
Access operator.
Definition: SparseMatrix.h:244
void set_row(const std::size_t &row, const SparseVector< _Type > &row_vector)
Set a row of the matrix.
Definition: SparseMatrix.h:90
DenseVector< _Type > multiply(const DenseVector< _Type > &X) const
Right-multiply by a DENSE vector.
SparseVector< _Type > get_row(const std::size_t &row) const
Get a row of the matrix.
Definition: SparseMatrix.h:83
const _Type & get(const std::size_t &row, const std::size_t &col) const
Access operator.
Definition: SparseMatrix.h:239
const _Type & operator()(const std::size_t &row, const std::size_t &col) const
Access operator.
Definition: SparseMatrix.h:229
SparseMatrix & operator=(const SparseMatrix &source)
Assignment operator.
void scale(const _Type &mult)
Scale the matrix by a scalar.
double inf_norm() const
double one_norm() const
Transpose the matrix in place.
double two_norm() const
std::size_t nelts() const
Get the number of (non-zero) elements in the matrix.
SparseVector< _Type > & operator[](const std::size_t &row)
Operator overloading for ROW access.
Definition: SparseMatrix.h:249
std::size_t ncols() const
Get the number of columns in the matrix.
Definition: SparseMatrix.h:277
double frob_norm() const
std::size_t max_in_col(const std::size_t &col, const std::size_t &row_min, const std::size_t &row_max) const
Find the maximum entry in a column – used in the native solver.
std::size_t nrows() const
Get the number of rows in the matrix.
Definition: SparseMatrix.h:272
An SparseVector class – a sparse vector object.
Definition: SparseVector.h:20
A collection of OO numerical routines aimed at simple (typical) applied problems in continuum mechani...

© 2012

R.E. Hewitt