DUNE-FEM (unstable)

blockmatrix.hh
1#ifndef DUNE_FEM_BLOCKSPMATRIX_HH
2#define DUNE_FEM_BLOCKSPMATRIX_HH
3
4//- system includes
5#include <stack>
6
7//- local includes
8#include "spmatrix.hh"
9
10namespace Dune
11{
12
13 namespace Fem
14 {
15
16 //*****************************************************************
17 //
18 // --DenseMatrix
19 //
21 //*****************************************************************
22 template <class T>
24 {
25 public:
26 typedef T Ttype;
27
28 typedef std::vector < T > RowType;
29 private:
30 typedef std::vector < RowType > MatrixType;
31
32 MatrixType matrix_;
33
34 int rows_;
35 int cols_;
36
37 public:
38 // creating empty matrix
39 DenseMatrix() :
40 matrix_()
41 {
42 rows_ = 0;
43 cols_ = 0;
44 }
45
48 matrix_(org.matrix_) , rows_(org.rows_) , cols_(org.cols_)
49 {
50 }
51
55 DenseMatrix(int rows, int cols)
56 {
57 resize(rows,cols);
58 }
59
60 void resize(int rows, int cols)
61 {
62 if( (rows == rows_) && (cols == cols_) ) return ;
63
64 matrix_.resize(rows);
65 rows_ = rows;
66 cols_ = cols;
67
68 for(int row=0; row<rows_; ++row)
69 {
70 matrix_[row].resize(cols);
71 for(int col=0; col<cols_; ++col) matrix_[row][col] = 0;
72 }
73 }
74
75 /*******************************/
76 /* Access and info functions */
77 /*******************************/
78 int rows() const {return rows_; }
79 int cols() const {return cols_; }
80
81 //int base() const {return base_;}
82 T& operator() (int row, int col)
83 {
84 assert( row>= 0);
85 assert( col>= 0);
86
87 assert( row < rows_);
88 assert( col < cols_);
89 return matrix_[row][col];
90 }
91 const T& operator() (int row, int col) const
92 {
93 assert( row >= 0);
94 assert( col >= 0);
95
96 assert( row < rows_);
97 assert( col < cols_);
98 return matrix_[row][col];
99 }
100
101 RowType & operator [] (int row) { return matrix_[row]; }
102 const RowType & operator [] (int row) const { return matrix_[row]; }
103
104 // result = this * vec
105 void mult(const T * vec, RowType & result) const
106 {
107 assert( ((int) result.size() != rows()) ?
108 (std::cout << result.size() << " s|r " << rows() << "\n",0) : 1 );
109 const int nRow= rows();
110 const int nCol= cols();
111 for(int row=0; row<nRow; ++row)
112 {
113 result[row] = 0;
114 for(int col=0; col<nCol; ++col)
115 {
116 result[row] += matrix_[row][col]*vec[col];
117 }
118 }
119 }
120
121 // result = this * vec
122 void multOEM(const T * vec, T * result) const
123 {
124 const int nRow= rows();
125 const int nCol= cols();
126 for(int row=0; row<nRow; ++row)
127 {
128 result[row] = 0;
129 for(int col=0; col<nCol; ++col)
130 {
131 result[row] += matrix_[row][col]*vec[col];
132 }
133 }
134 }
135
136 // result = this * vec
137 void mult(const RowType & vec, RowType & result) const
138 {
139 this->mult(&vec[0],result);
140 }
141
142 // result = this^T * vec
143 void multTransposed(const RowType & vec, RowType & result) const
144 {
145 assert( (int) result.size() == cols() );
146 const int nCols = cols();
147 const int nRows = rows();
148 for(int col=0; col<nCols; ++col)
149 {
150 result[col] = 0;
151 for(int row=0; row<nRows; ++row)
152 {
153 result[col] += matrix_[row][col]*vec[row];
154 }
155 }
156 }
157
158 // this = A * B
159 void multiply(const DenseMatrix & A, const DenseMatrix & B)
160 {
161 assert( A.cols() == B.rows() );
162
163 resize( A.rows() , B.cols() );
164
165 const int nRows = rows();
166 const int nCols = cols();
167 const int Acols = A.cols();
168 for(int row=0; row<nRows; ++row)
169 {
170 for(int col=0; col<nCols; ++col)
171 {
172 T sum = 0;
173 for(int k=0; k<Acols; ++k)
174 {
175 sum += A[row][k] * B[k][col];
176 }
177 matrix_[row][col] = sum;
178 }
179 }
180 };
181
182 // this = A * B
183 void multiplyTransposed(const DenseMatrix & A, const DenseMatrix & B)
184 {
185 assert( A.cols() == B.cols() );
186
187 resize(A.rows() , B.rows());
188
189 for(int row=0; row<rows(); ++row)
190 {
191 for(int col=0; col<cols(); ++col)
192 {
193 T sum = 0;
194 for(int k=0; k<A.cols(); ++k)
195 {
196 sum += A[row][k] * B[col][k];
197 }
198 matrix_[row][col] = sum;
199 }
200 }
201 };
202
205 {
206 resize(A.cols() , A.cols());
207
208 for(int row=0; row<rows(); ++row)
209 {
210 for(int col=0; col<cols(); ++col)
211 {
212 T sum = 0;
213 const int aRows = A.rows();
214 for(int k=0; k<aRows; ++k)
215 {
216 sum += A[k][row] * A[k][col];
217 }
218 matrix_[row][col] = sum;
219 }
220 }
221 }
222
224 void scale ( const T& val )
225 {
226 for(int row=0; row<rows(); ++row)
227 {
228 for(int col=0; col<cols(); ++col)
229 {
230 matrix_[row][col] *= val;
231 }
232 }
233 }
234
237 {
238 for(int row=0; row<rows(); ++row)
239 {
240 for(int col=0; col<cols(); ++col)
241 {
242 matrix_[row][col] = val;
243 }
244 }
245 return *this;
246 }
247
250 {
251 rows_ = org.rows_;
252 cols_ = org.cols_;
253
254 matrix_ = org.matrix_;
255 return *this;
256 }
257
260 {
261 const int nRows = rows();
262 const int nCols = cols();
263 assert( nRows == org.rows() );
264 assert( nCols == org.cols() );
265 for(int row=0; row<nRows; ++row)
266 {
267 for(int col=0; col<nCols; ++col)
268 {
269 matrix_[row][col] += org.matrix_[row][col];
270 }
271 }
272 return *this;
273 }
274
277 {
278 assert( rows() == rows() );
279 assert( cols() == org.cols() );
280 for(int row=0; row<rows(); ++row)
281 {
282 for(int col=0; col<cols(); ++col)
283 matrix_[row][col] -= org.matrix_[row][col];
284 }
285 return *this;
286 }
287
289 void print(std::ostream & s=std::cout) const
290 {
291 for(int i=0; i<rows(); ++i)
292 {
293 for(int j=0; j<cols(); ++j)
294 s << matrix_[i][j] << " ";
295 s << std::endl;
296 }
297 }
298
299 // set all matrix entries to zero
300 void clear()
301 {
302 for(int row=0; row<rows(); ++row)
303 {
304 for(int col=0; col<cols(); ++col)
305 matrix_[row][col] = 0;
306 }
307 }
308 }; // end class DenseMatrix
309
311 template<typename K>
312 std::ostream& operator<< (std::ostream& s, const DenseMatrix<K> & matrix)
313 {
314 matrix.print(s);
315 return s;
316 }
317
318 } // namespace Fem
319
320} // namespace Dune
321
322#endif // #ifndef DUNE_FEM_BLOCKSPMATRIX_HH
DenseMatrix based on std::vector< std::vector< T > >
Definition: blockmatrix.hh:24
DenseMatrix< T > & operator=(const T &val)
set all values of the matrix to given value
Definition: blockmatrix.hh:236
DenseMatrix< T > & operator+=(const DenseMatrix &org)
add matrix
Definition: blockmatrix.hh:259
DenseMatrix(int rows, int cols)
Definition: blockmatrix.hh:55
void scale(const T &val)
scale matrix with scalar
Definition: blockmatrix.hh:224
void print(std::ostream &s=std::cout) const
print matrix
Definition: blockmatrix.hh:289
std::vector< T > RowType
remember the value type
Definition: blockmatrix.hh:28
DenseMatrix< T > & operator-=(const DenseMatrix &org)
substract matrix
Definition: blockmatrix.hh:276
DenseMatrix(const DenseMatrix< T > &org)
Copy Constructor.
Definition: blockmatrix.hh:47
void multiply_AT_A(const DenseMatrix &A)
this = A^T * A
Definition: blockmatrix.hh:204
Dune namespace.
Definition: alignedallocator.hh:13
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 27, 22:29, 2024)