Newer
Older
// This file is part of the dune-xt-la project:
// https://github.com/dune-community/dune-xt-la
// Copyright 2009-2018 dune-xt-la developers and contributors. All rights reserved.
// License: Dual licensed as BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)
// or GPL-2.0+ (http://opensource.org/licenses/gpl-license)
// with "runtime exception" (http://www.dune-project.org/license.html)
// Authors:
// Barbara Verfürth (2015)
// Felix Schindler (2014 - 2018)
// Rene Milk (2015 - 2016, 2018)
// Tobias Leibner (2014, 2017 - 2018)
#ifndef DUNE_XT_LA_CONTAINER_MATRIX_VIEW_HH
#define DUNE_XT_LA_CONTAINER_MATRIX_VIEW_HH
#include <dune/xt/common/exceptions.hh>
#include <dune/xt/common/parallel/threadstorage.hh>
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "matrix-interface.hh"
namespace Dune {
namespace XT {
namespace LA {
// forwards
template <class MatrixImp>
class ConstMatrixView;
template <class MatrixImp>
class MatrixView;
namespace internal {
template <class MatrixImp>
class ConstMatrixViewTraits
: public MatrixTraitsBase<typename MatrixImp::ScalarType,
ConstMatrixView<MatrixImp>,
typename MatrixImp::Traits::BackendType,
MatrixImp::Traits::backend_type,
MatrixImp::Traits::vector_type,
MatrixImp::Traits::sparse>
{};
template <class MatrixImp>
class MatrixViewTraits
: public MatrixTraitsBase<typename MatrixImp::ScalarType,
MatrixView<MatrixImp>,
typename MatrixImp::Traits::BackendType,
MatrixImp::Traits::backend_type,
MatrixImp::Traits::vector_type,
MatrixImp::Traits::sparse>
{};
template <class MatrixImp>
MatrixImp& empty_matrix_ref()
{
static MatrixImp matrix_;
return matrix_;
}
} // namespace internal
template <class MatrixImp>
class ConstMatrixView
: public MatrixInterface<internal::ConstMatrixViewTraits<MatrixImp>, typename MatrixImp::ScalarType>
{
using BaseType = MatrixInterface<internal::ConstMatrixViewTraits<MatrixImp>, typename MatrixImp::ScalarType>;
using ThisType = ConstMatrixView;
public:
using ScalarType = typename BaseType::ScalarType;
using RealType = typename BaseType::RealType;
using Matrix = MatrixImp;
// This constructor is only here for the interface to compile
explicit ConstMatrixView(const size_t /*rr*/ = 0,
const size_t /*cc*/ = 0,
const ScalarType /*value*/ = ScalarType(0),
const size_t /*num_mutexes*/ = 1)
: matrix_(internal::empty_matrix_ref<MatrixImp>())
, first_row_(0)
, past_last_row_(0)
, first_col_(0)
, past_last_col_(0)
{
DUNE_THROW(XT::Common::Exceptions::you_are_using_this_wrong, "This constructor does not make sense for MatrixView");
}
// This constructor is only here for the interface to compile
ConstMatrixView(const size_t /*rr*/,
const size_t /*cc*/,
const SparsityPatternDefault& /*pattern*/,
const size_t /*num_mutexes*/ = 1)
: matrix_(internal::empty_matrix_ref<MatrixImp>())
, first_row_(0)
, past_last_row_(0)
, first_col_(0)
, past_last_col_(0)
{
DUNE_THROW(XT::Common::Exceptions::you_are_using_this_wrong, "This constructor does not make sense for MatrixView");
}
// This is the actual constructor
ConstMatrixView(const Matrix& matrix,
const size_t first_row,
const size_t past_last_row,
const size_t first_col,
const size_t past_last_col)
: matrix_(matrix)
, first_row_(first_row)
, past_last_row_(past_last_row)
, first_col_(first_col)
, past_last_col_(past_last_col)
{}
size_t row_index(const size_t ii) const
{
assert(ii < rows());
return first_row_ + ii;
}
size_t col_index(const size_t jj) const
{
assert(jj < cols());
return first_col_ + jj;
}
inline size_t rows() const
{
return past_last_row_ - first_row_;
}
inline size_t cols() const
{
return past_last_col_ - first_col_;
}
inline void scal(const ScalarType& /*alpha*/)
{
DUNE_THROW(XT::Common::Exceptions::you_are_using_this_wrong, "You cannot use non-const methods on ConstMatrixView");
}
inline void axpy(const ScalarType& /*alpha*/, const ThisType& /*xx*/)
{
DUNE_THROW(XT::Common::Exceptions::you_are_using_this_wrong, "You cannot use non-const methods on ConstMatrixView");
}
template <class XX, class YY>
inline void mv(const XX& xx, YY& yy) const
{
assert(xx.size() == cols() && yy.size() == rows());
const auto& patt = get_pattern();
for (size_t ii = 0; ii < rows(); ++ii) {
yy[ii] = 0.;
for (auto&& jj : patt.inner(ii))
yy[ii] += get_entry(ii, jj) * xx[jj];
}
}
template <class XX, class YY>
inline void mtv(const XX& xx, YY& yy) const
{
assert(xx.size() == rows() && yy.size() == cols());
const auto& patt = get_pattern();
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
std::fill(yy.begin(), yy.end(), 0.);
for (size_t ii = 0; ii < rows(); ++ii) {
for (auto&& jj : patt.inner(ii))
yy[jj] += get_entry(ii, jj) * xx[ii];
}
}
inline void add_to_entry(const size_t /*ii*/, const size_t /*jj*/, const ScalarType& /*value*/)
{
DUNE_THROW(XT::Common::Exceptions::you_are_using_this_wrong, "You cannot use non-const methods on ConstMatrixView");
}
inline void set_entry(const size_t /*ii*/, const size_t /*jj*/, const ScalarType& /*value*/)
{
DUNE_THROW(XT::Common::Exceptions::you_are_using_this_wrong, "You cannot use non-const methods on ConstMatrixView");
}
inline ScalarType get_entry(const size_t ii, const size_t jj) const
{
assert(ii < rows() && jj < cols());
return matrix_.get_entry(row_index(ii), col_index(jj));
}
inline void clear_row(const size_t /*ii*/)
{
DUNE_THROW(XT::Common::Exceptions::you_are_using_this_wrong, "You cannot use non-const methods on ConstMatrixView");
}
inline void clear_col(const size_t /*jj*/)
{
DUNE_THROW(XT::Common::Exceptions::you_are_using_this_wrong, "You cannot use non-const methods on ConstMatrixView");
}
inline void unit_row(const size_t /*ii*/)
{
DUNE_THROW(XT::Common::Exceptions::you_are_using_this_wrong, "You cannot use non-const methods on ConstMatrixView");
}
inline void unit_col(const size_t /*jj*/)
{
DUNE_THROW(XT::Common::Exceptions::you_are_using_this_wrong, "You cannot use non-const methods on ConstMatrixView");
}
inline bool valid() const
{
for (size_t ii = 0; ii < rows(); ++ii) {
for (size_t jj = 0; jj < cols(); ++jj) {
const auto entry = get_entry(ii, jj);
if (Common::isnan(entry) || Common::isinf(entry))
return false;
}
}
return true;
}
virtual RealType sup_norm() const override final
{
RealType ret = 0;
for (size_t ii = 0; ii < rows(); ++ii)
for (size_t jj = 0; jj < cols(); ++jj)
ret = std::max(ret, std::abs(get_entry(ii, jj)));
return ret;
} // ... sup_norm(...)
virtual SparsityPatternDefault pattern(const bool prune = false,
const typename Common::FloatCmp::DefaultEpsilon<ScalarType>::Type eps =
Common::FloatCmp::DefaultEpsilon<ScalarType>::value()) const override final
{
SparsityPatternDefault ret(rows());
auto matrix_patt = matrix_.pattern(prune, eps);
for (size_t ii = 0; ii < rows(); ++ii)
for (auto&& jj : matrix_patt.inner(row_index(ii)))
if (jj >= first_col_ && jj < past_last_col_)
ret.insert(ii, jj - first_col_);
return ret;
} // ... pattern(...)
const SparsityPatternDefault& get_pattern() const
{
initialize_pattern();
return **pattern_;
}
void initialize_pattern() const
{
if (!*pattern_)
*pattern_ = std::make_unique<SparsityPatternDefault>(pattern());
}
const Matrix& matrix_;
const size_t first_row_;
const size_t past_last_row_;
const size_t first_col_;
const size_t past_last_col_;
mutable XT::Common::PerThreadValue<std::unique_ptr<SparsityPatternDefault>> pattern_;
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
}; // class ConstMatrixView
template <class MatrixImp>
class MatrixView : public MatrixInterface<internal::MatrixViewTraits<MatrixImp>, typename MatrixImp::ScalarType>
{
using BaseType = MatrixInterface<internal::MatrixViewTraits<MatrixImp>, typename MatrixImp::ScalarType>;
using ConstMatrixViewType = ConstMatrixView<MatrixImp>;
using ThisType = MatrixView;
public:
using ScalarType = typename BaseType::ScalarType;
using RealType = typename BaseType::RealType;
using Matrix = MatrixImp;
// This constructor is only here for the interface to compile
explicit MatrixView(const size_t /*rr*/ = 0,
const size_t /*cc*/ = 0,
const ScalarType /*value*/ = ScalarType(0),
const size_t /*num_mutexes*/ = 1)
: const_matrix_view_()
, matrix_(internal::empty_matrix_ref<MatrixImp>())
{
DUNE_THROW(XT::Common::Exceptions::you_are_using_this_wrong, "This constructor does not make sense for MatrixView");
}
// This constructor is only here for the interface to compile
MatrixView(const size_t /*rr*/,
const size_t /*cc*/,
const SparsityPatternDefault& /*pattern*/,
const size_t /*num_mutexes*/ = 1)
: const_matrix_view_()
, matrix_(internal::empty_matrix_ref<MatrixImp>())
{
DUNE_THROW(XT::Common::Exceptions::you_are_using_this_wrong, "This constructor does not make sense for MatrixView");
}
// This is the actual constructor
MatrixView(Matrix& matrix,
const size_t first_row,
const size_t past_last_row,
const size_t first_col,
const size_t past_last_col)
: const_matrix_view_(matrix, first_row, past_last_row, first_col, past_last_col)
, matrix_(matrix)
{}
size_t row_index(const size_t ii) const
{
return const_matrix_view_.row_index(ii);
}
size_t col_index(const size_t jj) const
{
return const_matrix_view_.col_index(jj);
}
inline size_t rows() const
{
return const_matrix_view_.rows();
}
inline size_t cols() const
{
return const_matrix_view_.cols();
}
inline void scal(const ScalarType& alpha)
{
const auto& patt = const_matrix_view_.get_pattern();
for (size_t ii = 0; ii < rows(); ++ii)
for (auto&& jj : patt.inner(ii))
set_entry(ii, jj, get_entry(ii, jj) * alpha);
}
inline void axpy(const ScalarType& alpha, const ThisType& xx)
{
const auto other_patt = xx.pattern();
#ifndef NDEBUG
const auto& patt = const_matrix_view_.get_pattern();
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
if (xx.rows() != rows() || xx.cols() != cols())
DUNE_THROW(Common::Exceptions::shapes_do_not_match, "Shapes do not match!");
for (size_t ii = 0; ii < rows(); ++ii)
for (auto&& jj : other_patt.inner(ii))
if (!patt.contains(ii, jj))
DUNE_THROW(Dune::MathError, "Pattern of xx has to be a subset of this pattern!");
#endif
for (size_t ii = 0; ii < rows(); ++ii)
for (auto&& jj : other_patt.inner(ii))
add_to_entry(ii, jj, xx.get_entry(ii, jj) * alpha);
}
template <class XX, class YY>
inline void mv(const XX& xx, YY& yy) const
{
return const_matrix_view_.mv(xx, yy);
}
template <class XX, class YY>
inline void mtv(const XX& xx, YY& yy) const
{
return const_matrix_view_.mtv(xx, yy);
}
inline bool valid() const
{
return const_matrix_view_.valid();
}
virtual RealType sup_norm() const override final
{
return const_matrix_view_.sup_norm();
} // ... sup_norm(...)
virtual SparsityPatternDefault pattern(const bool prune = false,
const typename Common::FloatCmp::DefaultEpsilon<ScalarType>::Type eps =
Common::FloatCmp::DefaultEpsilon<ScalarType>::value()) const override final
{
return const_matrix_view_.pattern(prune, eps);
} // ... pattern(...)
inline void add_to_entry(const size_t ii, const size_t jj, const ScalarType& value)
{
assert(ii < rows() && jj < cols());
matrix_.add_to_entry(row_index(ii), col_index(jj), value);
}
inline void set_entry(const size_t ii, const size_t jj, const ScalarType& value)
{
assert(ii < rows() && jj < cols());
matrix_.set_entry(row_index(ii), col_index(jj), value);
}
inline ScalarType get_entry(const size_t ii, const size_t jj) const
{
return const_matrix_view_.get_entry(ii, jj);
}
inline void clear_row(const size_t ii)
{
const auto& patt = const_matrix_view_.get_pattern();
for (auto&& jj : patt.inner(ii))
set_entry(ii, jj, 0.);
}
inline void clear_col(const size_t jj)
{
const auto& patt = const_matrix_view_.get_pattern();
for (size_t ii = 0; ii < rows(); ++ii)
if (std::find(patt.inner(ii).begin(), patt.inner(ii).end(), jj) != patt.inner(ii).end())
set_entry(ii, jj, 0.);
}
inline void unit_row(const size_t ii)
{
clear_row(ii);
set_entry(ii, ii, 1.);
}
inline void unit_col(const size_t jj)
{
clear_col(jj);
set_entry(jj, jj, 1.);
}
private:
ConstMatrixViewType const_matrix_view_;
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
Matrix& matrix_;
}; // class MatrixView
} // namespace LA
namespace Common {
template <class MatrixImp>
struct MatrixAbstraction<LA::ConstMatrixView<MatrixImp>>
: public LA::internal::MatrixAbstractionBase<LA::ConstMatrixView<MatrixImp>>
{
using BaseType = LA::internal::MatrixAbstractionBase<MatrixImp>;
static const constexpr Common::StorageLayout storage_layout = MatrixAbstraction<MatrixImp>::storage_layout;
template <size_t rows = BaseType::static_rows,
size_t cols = BaseType::static_cols,
class FieldType = typename MatrixImp::ScalarType>
using MatrixTypeTemplate = LA::ConstMatrixView<MatrixImp>;
};
template <class MatrixImp>
struct MatrixAbstraction<LA::MatrixView<MatrixImp>>
: public LA::internal::MatrixAbstractionBase<LA::MatrixView<MatrixImp>>
{
using BaseType = LA::internal::MatrixAbstractionBase<MatrixImp>;
static const constexpr Common::StorageLayout storage_layout = MatrixAbstraction<MatrixImp>::storage_layout;
template <size_t rows = BaseType::static_rows,
size_t cols = BaseType::static_cols,
class FieldType = typename MatrixImp::ScalarType>
using MatrixTypeTemplate = LA::MatrixView<MatrixImp>;
};
} // namespace Common
} // namespace XT
} // namespace Dune
#endif // DUNE_XT_LA_CONTAINER_MATRIX_VIEW_HH