Dune-Fufem 2.11-git
Loading...
Searching...
No Matches
mappedmatrix.hh
Go to the documentation of this file.
1// -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set ts=8 sw=4 et sts=4:
3
4// SPDX-FileCopyrightText: Copyright © DUNE-FUFEM Project contributors, see file AUTHORS.md
5// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
6
7#ifndef DUNE_FUFEM_MAPPEDMATRIX_HH
8#define DUNE_FUFEM_MAPPEDMATRIX_HH
9
11
13
14
27template<class Map>
30 MappedMatrixColIterator<Map>,
31 typename Map::block_type,
32 const typename Map::block_type>
33
34{
35 typedef typename Map::Matrix Matrix;
36 static const int rowFactor = Map::rowFactor;
37 static const int colFactor = Map::colFactor;
38
39public:
40 typedef typename Map::block_type Value;
41 typedef typename Map::block_type Reference;
42
51 MappedMatrixColIterator(const Map& map, typename Matrix::row_type::ConstIterator&& it, int row, int virtualRow, int virtualCol=0):
52 map_(&map),
53 it_(it),
54 row_(row),
55 virtualRow_(virtualRow),
56 virtualCol_(virtualCol)
57 {}
58
59 void increment()
60 {
61 if (virtualCol_<colFactor-1)
62 {
64 }
65 else
66 {
67 virtualCol_ = 0;
68 ++it_;
69 }
70 }
71
72 bool equals(const MappedMatrixColIterator& other) const
73 {
74 return (it_==other.it_) and (virtualRow_==other.virtualRow_) and (virtualCol_==other.virtualCol_);
75 }
76
77 const Reference dereference() const
78 {
79 return map_->apply(*it_, row_, it_.index(), virtualRow_, virtualCol_);
80 }
81
82 int index() const
83 {
84 return it_.index()*colFactor+virtualCol_;
85 }
86
87
88protected:
89 const Map* map_;
90 typename Matrix::row_type::ConstIterator it_;
91 const int row_;
94};
95
96
97
105template<class Map>
107{
108 typedef typename Map::Matrix Matrix;
109 using size_type = typename Matrix::size_type;
110 static const int rowFactor = Map::rowFactor;
111 static const int colFactor = Map::colFactor;
112public:
114
115 MappedMatrixRow(const Map& map, const Matrix& matrix, int row, int virtualRow):
116 map_(&map),
117 matrix_(&matrix),
118 row_(row),
119 virtualRow_(virtualRow)
120 {}
121
123 {
125 }
126
128 {
130 }
131
134 size_type size() const
135 {
136 return (*matrix_)[row_].size() * colFactor;
137 }
138
141 ConstIterator find(typename Matrix::size_type col) const
142 {
143 auto originalEntry = (*matrix_)[row_].find(col/colFactor);
144 return (originalEntry==(*matrix_)[row_].end())
145 ? end()
146 : ConstIterator(*map_, (*matrix_)[row_].find(col/colFactor), row_, virtualRow_, col % colFactor);
147 }
148
149protected:
150 const Map* map_;
151 const Matrix* matrix_;
152 int row_;
154};
155
156namespace Dune
157{
158 // Forward declaration, so MatrixDimension can be friend of MappedMatrix
159 template<typename M>
160 struct MatrixDimension;
161}
162
163
199template<class Map>
201{
202 template <typename M>
203 friend class MatrixDimension;
204
205 typedef typename Map::Matrix Matrix;
206 static const int rowFactor = Map::rowFactor;
207 static const int colFactor = Map::colFactor;
208
209public:
210 using size_type = typename Matrix::size_type;
212 typedef typename Map::block_type block_type;
214
216 template<class T>
218 : public Dune::RandomAccessIteratorFacade<RealRowIterator<T>, T, const T>
219 {
220 public:
223
225 RealRowIterator (const Map& map, const Matrix& matrix, typename Matrix::const_iterator originalRow)
226 : map_(&map), matrix_(&matrix), originalRow_(originalRow), virtualRow_(0)
227 {}
228
231 RealRowIterator () = default;
232
235 {
236 return originalRow_.index() * rowFactor + virtualRow_;
237 }
238
240 {
241 return originalRow_.distanceTo(other.originalRow_) * rowFactor + other.virtualRow_ - virtualRow_;
242 }
243
246 bool equals (const RealRowIterator<const ValueType>& other) const
247 {
248 return originalRow_==other.originalRow_ and virtualRow_==other.virtualRow_;
249 }
250
253 {
254 ++virtualRow_;
255 if (virtualRow_ == rowFactor)
256 {
257 ++ originalRow_;
258 virtualRow_ = 0;
259 }
260 }
261
264 {
265 if (virtualRow_ == 0)
266 {
267 virtualRow_ = rowFactor - 1;
268 --originalRow_;
269 }
270 else
271 --virtualRow_;
272 }
273
275 {
276 originalRow_ += diff/rowFactor;
277
278 if (diff > 0)
279 for (std::ptrdiff_t i=0; i<diff%rowFactor; ++i)
280 increment();
281 else
282 for (std::ptrdiff_t i=0; i<-diff%rowFactor; ++i)
283 decrement();
284 }
285
287 {
288 auto newOriginalRow = originalRow_.index() + diff/rowFactor;
289 auto newVirtualRow = (virtualRow_ + diff) % rowFactor;
290 return MappedMatrixRow<Map>(*map_, *matrix_, newOriginalRow, newVirtualRow);
291 }
292
295 {
297 }
298
299 const Map* map_;
300 const Matrix* matrix_;
301 typename Matrix::const_iterator originalRow_;
303 };
304
307
308 MappedMatrix(const Map& map, const Matrix& matrix):
309 map_(&map),
310 matrix_(&matrix)
311 {}
312
314 auto begin () const
315 {
317 }
318
320 auto end () const
321 {
322 return const_iterator(*map_, *matrix_, matrix_->end());
323 }
324
327 size_type N() const
328 {
329 return matrix_->N()*rowFactor;
330 }
331
334 size_type M() const
335 {
336 return matrix_->M()*colFactor;
337 }
338
339 const row_type operator[](int row) const
340 {
341 return MappedMatrixRow<Map>(*map_, *matrix_, row/rowFactor, row % rowFactor);
342 }
343
344 template<class X , class Y >
345 void mv(const X &x, Y &y) const
346 {
347 int rows = this->N();
348 y.resize(rows);
349 for(int i=0; i<rows; ++i)
350 {
351 y[i] = 0.0;
352 const row_type& Mi = (*this)[i];
353 auto it = Mi.begin();
354 auto end = Mi.end();
355 for(; it!=end; ++it)
356 (*it).umv(x[it.index()], y[i]);
357 }
358 }
359
360protected:
361 const Map* map_;
362 const Matrix* matrix_;
363};
364
365namespace Dune
366{
372 template<typename Map>
374 {
376 using size_type = typename Matrix::size_type;
377
378 static size_type rowdim (const Matrix& /*A*/, size_type /*i*/)
379 {
380 return Matrix::block_type::rows;
381 }
382
383 static size_type coldim (const Matrix& /*A*/, size_type /*c*/)
384 {
385 return Matrix::block_type::cols;
386 }
387
388 static size_type rowdim (const Matrix& A) {
389 return A.N()*Matrix::block_type::rows;
390 }
391
392 static size_type coldim (const Matrix& A) {
393 return A.M()*Matrix::block_type::cols;
394 }
395 };
396
397} // namespace Dune
398
399
400#endif // DUNE_FUFEM_MAPPEDMATRIX_HH
Col col
A::size_type size_type
Iterator over sparse row of MappedMatrix.
Definition mappedmatrix.hh:34
MappedMatrixColIterator(const Map &map, typename Matrix::row_type::ConstIterator &&it, int row, int virtualRow, int virtualCol=0)
Construct iterator that points at a specific column.
Definition mappedmatrix.hh:51
Map::block_type Reference
Definition mappedmatrix.hh:41
int index() const
Definition mappedmatrix.hh:82
int virtualRow_
Definition mappedmatrix.hh:92
const int row_
Definition mappedmatrix.hh:91
Matrix::row_type::ConstIterator it_
Definition mappedmatrix.hh:90
void increment()
Definition mappedmatrix.hh:59
const Reference dereference() const
Definition mappedmatrix.hh:77
int virtualCol_
Definition mappedmatrix.hh:93
const Map * map_
Definition mappedmatrix.hh:89
Map::block_type Value
Definition mappedmatrix.hh:40
bool equals(const MappedMatrixColIterator &other) const
Definition mappedmatrix.hh:72
Sparse row of MappedMatrix.
Definition mappedmatrix.hh:107
int row_
Definition mappedmatrix.hh:152
int virtualRow_
Definition mappedmatrix.hh:153
ConstIterator begin() const
Definition mappedmatrix.hh:122
MappedMatrixColIterator< Map > ConstIterator
Definition mappedmatrix.hh:113
const Map * map_
Definition mappedmatrix.hh:150
ConstIterator find(typename Matrix::size_type col) const
Get iterator to a particular column.
Definition mappedmatrix.hh:141
ConstIterator end() const
Definition mappedmatrix.hh:127
MappedMatrixRow(const Map &map, const Matrix &matrix, int row, int virtualRow)
Definition mappedmatrix.hh:115
const Matrix * matrix_
Definition mappedmatrix.hh:151
size_type size() const
The number of nonzero blocks in this row.
Definition mappedmatrix.hh:134
A sparse matrix built by entry-wise transformation.
Definition mappedmatrix.hh:201
auto end() const
Get const iterator to one beyond last row.
Definition mappedmatrix.hh:320
auto begin() const
Get const iterator to first row.
Definition mappedmatrix.hh:314
void mv(const X &x, Y &y) const
Definition mappedmatrix.hh:345
MappedMatrix(const Map &map, const Matrix &matrix)
Definition mappedmatrix.hh:308
RealRowIterator< const row_type > const_iterator
The const iterator over the matrix rows.
Definition mappedmatrix.hh:306
friend class MatrixDimension
Definition mappedmatrix.hh:203
MappedMatrixRow< Map > row_type
Definition mappedmatrix.hh:211
const Matrix * matrix_
Definition mappedmatrix.hh:362
size_type M() const
Number of matrix columns.
Definition mappedmatrix.hh:334
Map::block_type block_type
Definition mappedmatrix.hh:212
const Map * map_
Definition mappedmatrix.hh:361
typename Matrix::size_type size_type
Definition mappedmatrix.hh:210
size_type N() const
Number of matrix rows.
Definition mappedmatrix.hh:327
const row_type operator[](int row) const
Definition mappedmatrix.hh:339
MappedMatrixColIterator< Map > ConstColIterator
Definition mappedmatrix.hh:213
Iterator access to matrix rows
Definition mappedmatrix.hh:219
row_type elementAt(std::ptrdiff_t diff) const
Definition mappedmatrix.hh:286
row_type dereference() const
dereferencing
Definition mappedmatrix.hh:294
void decrement()
prefix decrement
Definition mappedmatrix.hh:263
size_type index() const
return index
Definition mappedmatrix.hh:234
void advance(std::ptrdiff_t diff)
Definition mappedmatrix.hh:274
std::remove_const< T >::type ValueType
The unqualified value type.
Definition mappedmatrix.hh:222
std::ptrdiff_t distanceTo(const RealRowIterator< const ValueType > &other) const
Definition mappedmatrix.hh:239
void increment()
prefix increment
Definition mappedmatrix.hh:252
Matrix::const_iterator originalRow_
Definition mappedmatrix.hh:301
const Matrix * matrix_
Definition mappedmatrix.hh:300
RealRowIterator(const Map &map, const Matrix &matrix, typename Matrix::const_iterator originalRow)
constructor
Definition mappedmatrix.hh:225
bool equals(const RealRowIterator< const ValueType > &other) const
Test two iterators for equality.
Definition mappedmatrix.hh:246
size_type virtualRow_
Definition mappedmatrix.hh:302
RealRowIterator()=default
Default constructor, does not initialize anything.
const Map * map_
Definition mappedmatrix.hh:299
static size_type coldim(const Matrix &, size_type)
Definition mappedmatrix.hh:383
typename Matrix::size_type size_type
Definition mappedmatrix.hh:376
static size_type rowdim(const Matrix &, size_type)
Definition mappedmatrix.hh:378
static size_type coldim(const Matrix &A)
Definition mappedmatrix.hh:392
static size_type rowdim(const Matrix &A)
Definition mappedmatrix.hh:388