CppNoddy  0.92
Loading...
Searching...
No Matches
BandedMatrix.h
Go to the documentation of this file.
1/// \file BandedMatrix.h
2/// A matrix class that constructs a BANDED matrix.
3/// Storage is contiguous for use with external banded solvers.
4
5#ifndef BANDEDMATRIX_H
6#define BANDEDMATRIX_H
7
8#include <DenseVector.h>
9#include <Exceptions.h>
11
12namespace CppNoddy {
13
14 /// A matrix class that constructs a BANDED matrix.
15 template <typename _Type>
16 class BandedMatrix : public Sequential_Matrix_base<_Type>{
17
18 public:
19
21
22 /// Empty constructor .. should we stop this?
24 {}
25
26 void blank() {
27 m_storage = DenseVector<_Type>(m_N * (3 * m_L + 1), 0.0);
28 }
29
30 /// Noddy Banded Matrix constructor.
31 /// \param rows The number of rows in the matrix.
32 /// \param offdiag The maximum number of bands above OR below the diagonal.
33 /// The total number stored will be 3 * offdiag + 1; i.e. assumes the
34 /// number of offdiagonal elts is the same both above and below, and
35 /// we must an extra 'offdiag' because of fill-in during pivotting.
36 /// \param fill The initial value to be placed in each element
37 /// of the banded matrix.
38 BandedMatrix(const std::size_t& rows, const std::size_t& offdiag, const _Type& fill);
39
40 /// Copy constructor.
41 /// \param source The source object to be copied
42 BandedMatrix(const BandedMatrix& source);
43
44 /// Assignment operator.
45 /// \param source The source object for the assignment
46 /// \return The newly assigned object
47 BandedMatrix& operator=(const BandedMatrix& source);
48
49 /// Access operator
50 const _Type& operator()(const std::size_t& row, const std::size_t& col) const;
51 /// Access operator
52 _Type& operator()(const std::size_t& row, const std::size_t& col);
53 /// Access operator
54 const _Type& get(const std::size_t& row, const std::size_t& col) const;
55 /// Access operator
56 _Type& set(const std::size_t& row, const std::size_t& col);
57
58 /// Get the number of rows
59 /// \return The number of rows in the matrix
60 std::size_t nrows() const;
61
62 /// Get the number of columns
63 /// \return The number of columns in the matrix
64 std::size_t ncols() const;
65
66 /// Get the number of elements
67 /// \return The number of elements
68 std::size_t nelts() const;
69
70 /// Scale all entries in the matrix by a scalar
71 /// \param mult The scalar multiplier
72 void scale(const _Type& mult);
73
74 /// Transpose the matrix
75 void transpose();
76
77 /// \return The maximum one_norm of all rows
78 double one_norm() const;
79
80 /// \return The maximum two_norm of all rows
81 double two_norm() const;
82
83 /// \return The maximum inf_norm of all rows
84 double inf_norm() const;
85
86 /// \return The sum of the two_norm of all rows
87 double frob_norm() const;
88
89 /// Right multiply the matrix by a DENSE vector
90 /// \param X The DENSE vector to multiply by
91 /// \return A DENSE vector of length ncols() produced from
92 /// the multiplication
94
95 /// Output the matrix contents to std::cout
96 void dump() const;
97
98 //
99 // NON-INHERITED MEMBER FUNCTIONS
100 //
101
102 /// Assign a value the matrix so that it has the same
103 /// geometry, but zero entries in all locations
104 /// including those reserved for pivotting.
105 /// \param elt The value to be assigned to all entries
106 void assign(_Type elt) {
107 m_storage.assign(m_storage.size(), elt);
108 }
109
110 /// Get the number of off-diagonal elements
111 /// where the total INPUT band width is 2*noffdiag+1
112 /// since the band structure is symmetric.
113 /// \return The number of upper or lower offdiagonal bands;
114 /// NOTE: the number of offdiagonals STORED will typically
115 /// be three times this to allow for pivotting
116 std::size_t noffdiag() const;
117
118 /// Exchange rows in the matrix
119 /// \param row1 First row to be swapped
120 /// \param row2 Second row to be swapped
121 void row_swap(const std::size_t& row1, const std::size_t& row2);
122
123 /// Allow direct access to the vector m_storage.
124 /// Dangerous, but used for passing to LAPACK.
125 double* base();
126
127 elt_iter get_elt_iter(std::size_t row, std::size_t col) {
128 return m_storage.begin() + m_L * (3 * col + 2) + row;
129 }
130
131 //private:
132
133 /// A contiguous vector
135 /// The number of rows/cols in the matrix.
136 std::size_t m_N;
137 /// Max number of (INPUT) bands above OR below the main diagonal
138 std::size_t m_L;
139
140 }
141 ; // end class
142
143
144 // INLINED METHODS ARE BELOW
145
146 template <typename _Type >
147 inline const _Type& BandedMatrix<_Type>::operator()(const std::size_t& row, const std::size_t& col) const {
148#ifdef PARANOID
149 // if outside the m_Nxm_N matrix
150 if((row >= m_N) || (row < 0) || (col >= m_N) || (col < 0)) {
151 std::string problem;
152 problem = " The const operator() of BandedMatrix has been called \n";
153 problem += " with a (row, col) index that is outside \n";
154 problem += " the square m_Nxm_N matrix.\n";
155 throw ExceptionGeom(problem, m_N, m_N, row, col);
156 }
157 // check if the subscripts are out of the band
158 if(!((col + m_L >= row) && (col <= 2*m_L + row))) {
159 std::string problem;
160 problem = " The const operator() of BandedMatrix has been called \n";
161 problem += " with a (row, col) index that is outside \n";
162 problem += " the band structure. Bandwidth and offset from\n";
163 problem += " the diagonal information follows.\n";
164 throw ExceptionGeom(problem, m_L, col - row);
165 }
166#endif
167 // MOSTLY WE PUSH BANDED MATRICES TO LAPACK - so we'll keep column major
168 // internal storage ... a la Fortran
169 // return m_storage[ col * ( 3 * m_L + 1 ) + ( row - col ) + 2 * m_L ];
170 // or equiv.
171 return m_storage[ m_L * (3 * col + 2) + row ];
172 }
173
174 template <typename _Type>
175 inline _Type& BandedMatrix<_Type>::operator()(const std::size_t& row, const std::size_t& col) {
176#ifdef PARANOID
177 // if outside the m_Nxm_N matrix
178 if((row >= m_N) || (row < 0) || (col >= m_N) || (col < 0)) {
179 std::string problem;
180 problem = " The operator() of BandedMatrix has been called \n";
181 problem += " with a (row, col) index that is outside \n";
182 problem += " the square m_Nxm_N matrix.\n";
183 throw ExceptionRange(problem, m_N, m_N, row, col);
184 }
185 // check if the subscripts are out of the band
186 if(!((col + m_L >= row) && (col <= 2*m_L + row))) {
187 std::string problem;
188 problem = " The operator() of BandedMatrix has been called \n";
189 problem += " with a (row, col) index that is outside \n";
190 problem += " the band structure.\n";
191 std::cout << " m_L = " << m_L << "\n";
192 std::cout << " row = " << row << "\n";
193 std::cout << " col = " << col << "\n";
194 throw ExceptionRange(problem, m_N, m_L, row, col);
195 }
196#endif
197 // MOSTLY WE PUSH BANDED MATRICES TO LAPACK - so we'll keep column major
198 // internal storage ... a la Fortran
199 // return m_storage[ col * ( 3 * m_L + 1 ) + ( row - col ) + 2 * m_L ];
200 // or equiv.
201 return m_storage[ m_L * (3 * col + 2) + row ];
202 }
203
204 template <typename _Type>
205 inline const _Type& BandedMatrix<_Type>::get
206 (const std::size_t& row, const std::size_t& col) const {
207 return operator()(row, col);
208 }
209
210 template <typename _Type>
212 (const std::size_t& row, const std::size_t& col) {
213 return operator()(row, col);
214 }
215
216 template <typename _Type>
217 inline std::size_t BandedMatrix<_Type>::nrows() const {
218 return m_N;
219 }
220
221 template <typename _Type>
222 inline std::size_t BandedMatrix<_Type>::ncols() const {
223 return m_N;
224 }
225
226 template <typename _Type>
227 inline std::size_t BandedMatrix<_Type>::noffdiag() const {
228 return m_L;
229 }
230
231
232
233} // end namespace
234
235#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 matrix class that constructs a BANDED matrix.
Definition: BandedMatrix.h:16
const _Type & operator()(const std::size_t &row, const std::size_t &col) const
Access operator.
Definition: BandedMatrix.h:147
double frob_norm() const
void transpose()
Transpose the matrix.
void scale(const _Type &mult)
Scale all entries in the matrix by a scalar.
std::size_t noffdiag() const
Get the number of off-diagonal elements where the total INPUT band width is 2*noffdiag+1 since the ba...
Definition: BandedMatrix.h:227
DenseVector< _Type > multiply(const DenseVector< _Type > &X) const
Right multiply the matrix by a DENSE vector.
std::size_t nrows() const
Get the number of rows.
Definition: BandedMatrix.h:217
std::size_t m_L
Max number of (INPUT) bands above OR below the main diagonal.
Definition: BandedMatrix.h:138
const _Type & get(const std::size_t &row, const std::size_t &col) const
Access operator.
Definition: BandedMatrix.h:206
double * base()
Allow direct access to the vector m_storage.
BandedMatrix & operator=(const BandedMatrix &source)
Assignment operator.
std::size_t m_N
The number of rows/cols in the matrix.
Definition: BandedMatrix.h:136
double one_norm() const
double two_norm() const
DenseVector< _Type > m_storage
A contiguous vector.
Definition: BandedMatrix.h:134
BandedMatrix()
Empty constructor .. should we stop this?
Definition: BandedMatrix.h:23
void assign(_Type elt)
Assign a value the matrix so that it has the same geometry, but zero entries in all locations includi...
Definition: BandedMatrix.h:106
std::size_t ncols() const
Get the number of columns.
Definition: BandedMatrix.h:222
void row_swap(const std::size_t &row1, const std::size_t &row2)
Exchange rows in the matrix.
std::size_t nelts() const
Get the number of elements.
double inf_norm() const
DenseVector< _Type >::elt_iter elt_iter
Definition: BandedMatrix.h:20
elt_iter get_elt_iter(std::size_t row, std::size_t col)
Definition: BandedMatrix.h:127
void dump() const
Output the matrix contents to std::cout.
_Type & set(const std::size_t &row, const std::size_t &col)
Access operator.
Definition: BandedMatrix.h:212
An DenseVector class – a dense vector object.
Definition: DenseVector.h:34
std::vector< _Type >::iterator elt_iter
Definition: DenseVector.h:37
An exception class to be thrown when a container of incorrect geometry used in any class/method.
Definition: Exceptions.h:47
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