CppNoddy  0.92
Loading...
Searching...
No Matches
BandedMatrix.cpp
Go to the documentation of this file.
1/// \file BandedMatrix.cpp
2/// Implementation of the matrix class that constructs a BANDED matrix as
3/// a DenseVector.
4
5#include <string>
6#include <memory>
7#include <algorithm>
8
9#include <DenseVector.h>
10#include <BandedMatrix.h>
11#include <Exceptions.h>
12
13namespace CppNoddy {
14
15 template <typename _Type>
16 BandedMatrix<_Type>::BandedMatrix(const std::size_t& rows,
17 const std::size_t& upper_offdiag_bands,
18 const _Type& fill) :
20 m_N(rows),
21 m_L(upper_offdiag_bands) {
22 // we'll assume that the upper & lower offdiags are of equal size.
23 // logical m_storage is the main diagonal + upper offdiagonal + lower offdiagonal
24 // = 2 * m_L + 1.
25 // However, allowing for row interchanging during pivotting leads to additional
26 // padding of m_L, to make a total of 3*m_L+1.
27 m_storage = DenseVector<_Type>(m_N * (3 * m_L + 1), 0.0);
28 }
29
30 template <typename _Type>
32 Sequential_Matrix_base<_Type>() {
33 *this = source;
34 }
35
36 template <typename _Type>
38 if(this == &source)
39 return * this;
40 m_storage = source.m_storage;
41 m_N = source.m_N;
42 m_L = source.m_L;
43 return *this;
44 }
45
46 template <typename _Type>
47 std::size_t BandedMatrix<_Type>::nelts() const {
48 return m_storage.size();
49 }
50
51 template <typename _Type>
52 void BandedMatrix<_Type>::scale(const _Type& mult) {
53 m_storage *= mult;
54 }
55
56
57 template <typename _Type>
59 std::string problem;
60 problem = " The multiply method of the BandedMatrix class has \n";
61 problem += " not been implemented. \n";
62 throw ExceptionRuntime(problem);
63 return m_storage; // dummy return
64 }
65
66
67 template <typename _Type>
69 for(std::size_t row = 0; row < m_N; ++row) {
70 for(std::size_t col = std::max(0, int(row) - int(m_L)); col < row; ++col) {
71 // swap elements
72 std::swap(operator()(row, col), operator()(col, row));
73 }
74 }
75 }
76
77
78 template <typename _Type>
79 void BandedMatrix<_Type>::row_swap(const std::size_t& row1, const std::size_t& row2) {
80 // WE MUST HAVE row2 > row1 & row1 must have no elements left of the diagonal!
81 // only of any use to native Gaussian elimination solver
82 /// \todo MAKE ME PRIVATE OR ELSE!
83 for(std::size_t col = row1; col <= std::min(row1 + 2 * m_L, m_N - 1); ++col) {
84 std::swap(operator()(row1, col), operator()(row2, col));
85 }
86 }
87
88
89 template <typename _Type>
91 double max(0.0);
92 for(unsigned row = 0; row < m_N; ++row) {
93 // copy the row into a temp vector
94 DenseVector<_Type> temp(3 * m_L + 1, &m_storage[ row * (3 * m_L + 1) ]);
95 max = std::max(max, temp.one_norm());
96 }
97 return max;
98 }
99
100
101 template <typename _Type>
103 double max(0.0);
104 for(unsigned row = 0; row < m_N; ++row) {
105 // copy the row into a temp vector
106 DenseVector<_Type> temp(3 * m_L + 1, &m_storage[ row * (3 * m_L + 1) ]);
107 max = std::max(max, temp.two_norm());
108 }
109 return max;
110 }
111
112
113 template <typename _Type>
115 double max(0.0);
116 for(unsigned row = 0; row < m_N; ++row) {
117 // copy the row into a temp vector
118 DenseVector<_Type> temp(3 * m_L + 1, &m_storage[ row * (3 * m_L + 1) ]);
119 max = std::max(max, temp.inf_norm());
120 }
121 return max;
122 }
123
124
125 template <typename _Type>
127 double sum(0.0);
128 for(unsigned row = 0; row < m_N; ++row) {
129 // copy the row into a temp vector
130 DenseVector<_Type> temp(3 * m_L + 1, &m_storage[ row * (3 * m_L + 1) ]);
131 sum += temp.two_norm();
132 }
133 return sum;
134 }
135
136 template <typename _Type>
138 std::cout << "BANDED mtx size = " << nrows() << " x " << ncols()
139 << " with INPUT no. of bands = " << 2 * m_L + 1
140 << " and total bands STORED (for pivotting) = " << 3 * m_L + 1 << "\n";
141 std::cout.precision(4);
142 std::cout << "- start matrix \n";
143 for(std::size_t i = 0; i < nrows(); ++i) {
144 std::cout << " row " << i << " = ";
145 for(std::size_t j = 0; j < nrows(); ++j) {
146 if(i == j)
147 std::cout << "*";
148 if(((int) j < (int) i - (int) m_L) || ((int) j > (int) i + (int)(2*m_L))) {
149 std::cout << 0 << ", ";
150 } else {
151 std::cout << operator()(i, j) << ", ";
152 }
153 }
154 std::cout << "\n";
155 }
156 std::cout << "- end matrix \n";
157 }
158
159 template <>
161 return &(m_storage[0]);
162 }
163
164 template <>
166 //return &( m_storage[0].real() );
167 return &reinterpret_cast<double(&)[2]>(m_storage[0])[0];
168 }
169
170 // the templated versions we require are:
171 template class BandedMatrix<double>
172 ;
173 template class BandedMatrix<std::complex<double> >
174 ;
175
176} // end namespace
A matrix class that constructs a BANDED matrix.
Specification for a templated DenseVector class – a dense, dynamic, vector object.
The collection of CppNoddy exceptions.
A matrix class that constructs a BANDED matrix.
Definition: BandedMatrix.h:16
double frob_norm() const
void transpose()
Transpose the matrix.
void scale(const _Type &mult)
Scale all entries in the matrix by a scalar.
DenseVector< _Type > multiply(const DenseVector< _Type > &X) const
Right multiply the matrix by a DENSE vector.
std::size_t m_L
Max number of (INPUT) bands above OR below the main diagonal.
Definition: BandedMatrix.h:138
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 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
void dump() const
Output the matrix contents to std::cout.
An DenseVector class – a dense vector object.
Definition: DenseVector.h:34
double one_norm() const
l1-norm.
Definition: DenseVector.cpp:49
double inf_norm() const
Infinity norm.
Definition: DenseVector.cpp:59
double two_norm() const
l2-norm.
Definition: DenseVector.cpp:54
A generic runtime exception.
Definition: Exceptions.h:158
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