Dune Core Modules (2.9.1)

scaledidmatrix.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (C) DUNE Project contributors, see file LICENSE.md in module root
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4// vi: set et ts=4 sw=2 sts=2:
5#ifndef DUNE_ISTL_SCALEDIDMATRIX_HH
6#define DUNE_ISTL_SCALEDIDMATRIX_HH
7
14#include <cmath>
15#include <cstddef>
16#include <complex>
17#include <iostream>
22
23namespace Dune {
24
28 template<class K, int n>
30 {
31 typedef DiagonalMatrixWrapper< ScaledIdentityMatrix<K,n> > WrapperType;
32
33 public:
34 //===== type definitions and constants
35
37 typedef K field_type;
38
40 typedef K block_type;
41
43 typedef std::size_t size_type;
44
46 [[deprecated("Use free function blockLevel(). Will be removed after 2.8.")]]
47 static constexpr std::size_t blocklevel = 1;
48
50 typedef DiagonalRowVector<K,n> row_type;
51 typedef row_type reference;
52 typedef DiagonalRowVectorConst<K,n> const_row_type;
53 typedef const_row_type const_reference;
54
56 enum {
58 rows = n,
60 cols = n
61 };
62
63 //===== constructors
67
71 : p_(k)
72 {}
73
74 //===== assignment from scalar
75 ScaledIdentityMatrix& operator= (const K& k)
76 {
77 p_ = k;
78 return *this;
79 }
80
81 // check if matrix is identical to other matrix (not only identical values)
82 bool identical(const ScaledIdentityMatrix<K,n>& other) const
83 {
84 return (this==&other);
85 }
86
87 //===== iterator interface to rows of the matrix
96
99 {
100 return Iterator(WrapperType(this),0);
101 }
102
105 {
106 return Iterator(WrapperType(this),n);
107 }
108
112 {
113 return Iterator(WrapperType(this),n-1);
114 }
115
119 {
120 return Iterator(WrapperType(this),-1);
121 }
122
123
132
135 {
136 return ConstIterator(WrapperType(this),0);
137 }
138
141 {
142 return ConstIterator(WrapperType(this),n);
143 }
144
148 {
149 return ConstIterator(WrapperType(this),n-1);
150 }
151
155 {
156 return ConstIterator(WrapperType(this),-1);
157 }
158
159 //===== vector space arithmetic
160
163 {
164 p_ += y.p_;
165 return *this;
166 }
167
170 {
171 p_ -= y.p_;
172 return *this;
173 }
174
177 {
178 p_ += k;
179 return *this;
180 }
181
184 {
185 p_ -= k;
186 return *this;
187 }
190 {
191 p_ *= k;
192 return *this;
193 }
194
197 {
198 p_ /= k;
199 return *this;
200 }
201
202 //===== binary operators
203
205 template <class Scalar,
206 std::enable_if_t<IsNumber<Scalar>::value, int> = 0>
207 friend auto operator* ( const ScaledIdentityMatrix& matrix, Scalar scalar)
208 {
210 }
211
213 template <class Scalar,
214 std::enable_if_t<IsNumber<Scalar>::value, int> = 0>
215 friend auto operator* (Scalar scalar, const ScaledIdentityMatrix& matrix)
216 {
218 }
219
220 //===== comparison ops
221
223 bool operator==(const ScaledIdentityMatrix& other) const
224 {
225 return p_==other.scalar();
226 }
227
229 bool operator!=(const ScaledIdentityMatrix& other) const
230 {
231 return p_!=other.scalar();
232 }
233
234 //===== linear maps
235
237 template<class X, class Y>
238 void mv (const X& x, Y& y) const
239 {
240#ifdef DUNE_FMatrix_WITH_CHECKING
241 if (x.N()!=M()) DUNE_THROW(FMatrixError,"index out of range");
242 if (y.N()!=N()) DUNE_THROW(FMatrixError,"index out of range");
243#endif
244 for (size_type i=0; i<n; ++i)
245 y[i] = p_ * x[i];
246 }
247
249 template<class X, class Y>
250 void mtv (const X& x, Y& y) const
251 {
252 mv(x, y);
253 }
254
256 template<class X, class Y>
257 void umv (const X& x, Y& y) const
258 {
259#ifdef DUNE_FMatrix_WITH_CHECKING
260 if (x.N()!=M()) DUNE_THROW(FMatrixError,"index out of range");
261 if (y.N()!=N()) DUNE_THROW(FMatrixError,"index out of range");
262#endif
263 for (size_type i=0; i<n; ++i)
264 y[i] += p_ * x[i];
265 }
266
268 template<class X, class Y>
269 void umtv (const X& x, Y& y) const
270 {
271#ifdef DUNE_FMatrix_WITH_CHECKING
272 if (x.N()!=N()) DUNE_THROW(FMatrixError,"index out of range");
273 if (y.N()!=M()) DUNE_THROW(FMatrixError,"index out of range");
274#endif
275 for (size_type i=0; i<n; ++i)
276 y[i] += p_ * x[i];
277 }
278
280 template<class X, class Y>
281 void umhv (const X& x, Y& y) const
282 {
283#ifdef DUNE_FMatrix_WITH_CHECKING
284 if (x.N()!=N()) DUNE_THROW(FMatrixError,"index out of range");
285 if (y.N()!=M()) DUNE_THROW(FMatrixError,"index out of range");
286#endif
287 for (size_type i=0; i<n; i++)
288 y[i] += conjugateComplex(p_)*x[i];
289 }
290
292 template<class X, class Y>
293 void mmv (const X& x, Y& y) const
294 {
295#ifdef DUNE_FMatrix_WITH_CHECKING
296 if (x.N()!=M()) DUNE_THROW(FMatrixError,"index out of range");
297 if (y.N()!=N()) DUNE_THROW(FMatrixError,"index out of range");
298#endif
299 for (size_type i=0; i<n; ++i)
300 y[i] -= p_ * x[i];
301 }
302
304 template<class X, class Y>
305 void mmtv (const X& x, Y& y) const
306 {
307#ifdef DUNE_FMatrix_WITH_CHECKING
308 if (x.N()!=N()) DUNE_THROW(FMatrixError,"index out of range");
309 if (y.N()!=M()) DUNE_THROW(FMatrixError,"index out of range");
310#endif
311 for (size_type i=0; i<n; ++i)
312 y[i] -= p_ * x[i];
313 }
314
316 template<class X, class Y>
317 void mmhv (const X& x, Y& y) const
318 {
319#ifdef DUNE_FMatrix_WITH_CHECKING
320 if (x.N()!=N()) DUNE_THROW(FMatrixError,"index out of range");
321 if (y.N()!=M()) DUNE_THROW(FMatrixError,"index out of range");
322#endif
323 for (size_type i=0; i<n; i++)
324 y[i] -= conjugateComplex(p_)*x[i];
325 }
326
328 template<class X, class Y>
329 void usmv (const K& alpha, const X& x, Y& y) const
330 {
331#ifdef DUNE_FMatrix_WITH_CHECKING
332 if (x.N()!=M()) DUNE_THROW(FMatrixError,"index out of range");
333 if (y.N()!=N()) DUNE_THROW(FMatrixError,"index out of range");
334#endif
335 for (size_type i=0; i<n; i++)
336 y[i] += alpha * p_ * x[i];
337 }
338
340 template<class X, class Y>
341 void usmtv (const K& alpha, const X& x, Y& y) const
342 {
343#ifdef DUNE_FMatrix_WITH_CHECKING
344 if (x.N()!=N()) DUNE_THROW(FMatrixError,"index out of range");
345 if (y.N()!=M()) DUNE_THROW(FMatrixError,"index out of range");
346#endif
347 for (size_type i=0; i<n; i++)
348 y[i] += alpha * p_ * x[i];
349 }
350
352 template<class X, class Y>
353 void usmhv (const K& alpha, const X& x, Y& y) const
354 {
355#ifdef DUNE_FMatrix_WITH_CHECKING
356 if (x.N()!=N()) DUNE_THROW(FMatrixError,"index out of range");
357 if (y.N()!=M()) DUNE_THROW(FMatrixError,"index out of range");
358#endif
359 for (size_type i=0; i<n; i++)
360 y[i] += alpha * conjugateComplex(p_) * x[i];
361 }
362
363 //===== norms
364
366 typename FieldTraits<field_type>::real_type frobenius_norm () const
367 {
368 return fvmeta::sqrt(n*p_*p_);
369 }
370
372 typename FieldTraits<field_type>::real_type frobenius_norm2 () const
373 {
374 return n*p_*p_;
375 }
376
378 typename FieldTraits<field_type>::real_type infinity_norm () const
379 {
380 return std::abs(p_);
381 }
382
384 typename FieldTraits<field_type>::real_type infinity_norm_real () const
385 {
386 return fvmeta::absreal(p_);
387 }
388
389 //===== solve
390
393 template<class V>
394 void solve (V& x, const V& b) const
395 {
396 for (int i=0; i<n; i++)
397 x[i] = b[i]/p_;
398 }
399
402 void invert()
403 {
404 p_ = 1/p_;
405 }
406
408 K determinant () const {
409 return std::pow(p_,n);
410 }
411
412 //===== sizes
413
415 size_type N () const
416 {
417 return n;
418 }
419
421 size_type M () const
422 {
423 return n;
424 }
425
426 //===== query
427
429 bool exists (size_type i, size_type j) const
430 {
431#ifdef DUNE_FMatrix_WITH_CHECKING
432 if (i<0 || i>=n) DUNE_THROW(FMatrixError,"row index out of range");
433 if (j<0 || j>=n) DUNE_THROW(FMatrixError,"column index out of range");
434#endif
435 return i==j;
436 }
437
438 //===== conversion operator
439
441 friend std::ostream& operator<< (std::ostream& s, const ScaledIdentityMatrix<K,n>& a)
442 {
443 for (size_type i=0; i<n; i++) {
444 for (size_type j=0; j<n; j++)
445 s << ((i==j) ? a.p_ : 0) << " ";
446 s << std::endl;
447 }
448 return s;
449 }
450
453 {
454 return reference(const_cast<K*>(&p_), i);
455 }
456
458 const_reference operator[](size_type i) const
459 {
460 return const_reference(const_cast<K*>(&p_), i);
461 }
462
464 const K& diagonal(size_type /*i*/) const
465 {
466 return p_;
467 }
468
471 {
472 return p_;
473 }
474
477 const K& scalar() const
478 {
479 return p_;
480 }
481
485 {
486 return p_;
487 }
488
489 private:
490 // the data, very simply a single number
491 K p_;
492
493 };
494
495 template <class DenseMatrix, class field, int N>
496 struct DenseMatrixAssigner<DenseMatrix, ScaledIdentityMatrix<field, N>> {
497 static void apply(DenseMatrix& denseMatrix,
498 ScaledIdentityMatrix<field, N> const& rhs) {
499 assert(denseMatrix.M() == N);
500 assert(denseMatrix.N() == N);
501 denseMatrix = field(0);
502 for (int i = 0; i < N; ++i)
503 denseMatrix[i][i] = rhs.scalar();
504 }
505 };
506
507 template<class K, int n>
508 struct FieldTraits< ScaledIdentityMatrix<K, n> >
509 {
510 using field_type = typename ScaledIdentityMatrix<K, n>::field_type;
511 using real_type = typename FieldTraits<field_type>::real_type;
512 };
513
514} // end namespace
515
516#endif
Iterator class for sparse vector-like containers.
Definition: diagonalmatrix.hh:998
Error thrown if operations of a FieldMatrix fail.
Definition: densematrix.hh:126
A multiple of the identity matrix of static size.
Definition: scaledidmatrix.hh:30
void usmhv(const K &alpha, const X &x, Y &y) const
y += alpha A^H x
Definition: scaledidmatrix.hh:353
void mmtv(const X &x, Y &y) const
y -= A^T x
Definition: scaledidmatrix.hh:305
ScaledIdentityMatrix & operator-=(const ScaledIdentityMatrix &y)
vector space subtraction
Definition: scaledidmatrix.hh:169
const_row_type::ConstIterator ConstColIterator
rename the iterators for easier access
Definition: scaledidmatrix.hh:131
ConstIterator end() const
end iterator
Definition: scaledidmatrix.hh:140
Iterator beforeBegin()
Definition: scaledidmatrix.hh:118
bool operator!=(const ScaledIdentityMatrix &other) const
incomparison operator
Definition: scaledidmatrix.hh:229
void mmv(const X &x, Y &y) const
y -= A x
Definition: scaledidmatrix.hh:293
std::size_t size_type
The type used for the index access and size operations.
Definition: scaledidmatrix.hh:43
void usmv(const K &alpha, const X &x, Y &y) const
y += alpha A x
Definition: scaledidmatrix.hh:329
row_type::Iterator ColIterator
rename the iterators for easier access
Definition: scaledidmatrix.hh:95
void mv(const X &x, Y &y) const
y = A x
Definition: scaledidmatrix.hh:238
void umtv(const X &x, Y &y) const
y += A^T x
Definition: scaledidmatrix.hh:269
void umhv(const X &x, Y &y) const
y += A^H x
Definition: scaledidmatrix.hh:281
DiagonalRowVector< K, n > row_type
Each row is implemented by a field vector.
Definition: scaledidmatrix.hh:50
ContainerWrapperIterator< const WrapperType, reference, reference > Iterator
Iterator class for sequential access.
Definition: scaledidmatrix.hh:89
Iterator beforeEnd()
Definition: scaledidmatrix.hh:111
K determinant() const
calculates the determinant of this matrix
Definition: scaledidmatrix.hh:408
K field_type
export the type representing the field
Definition: scaledidmatrix.hh:37
void usmtv(const K &alpha, const X &x, Y &y) const
y += alpha A^T x
Definition: scaledidmatrix.hh:341
@ rows
The number of rows.
Definition: scaledidmatrix.hh:58
@ cols
The number of columns.
Definition: scaledidmatrix.hh:60
Iterator end()
end iterator
Definition: scaledidmatrix.hh:104
Iterator iterator
typedef for stl compliant access
Definition: scaledidmatrix.hh:91
const K & scalar() const
Get const reference to the scalar diagonal value.
Definition: scaledidmatrix.hh:477
void umv(const X &x, Y &y) const
y += A x
Definition: scaledidmatrix.hh:257
static constexpr std::size_t blocklevel
We are at the leaf of the block recursion.
Definition: scaledidmatrix.hh:47
const K & diagonal(size_type) const
Get const reference to diagonal entry.
Definition: scaledidmatrix.hh:464
ContainerWrapperIterator< const WrapperType, const_reference, const_reference > ConstIterator
Iterator class for sequential access.
Definition: scaledidmatrix.hh:125
K & diagonal(size_type)
Get reference to diagonal entry.
Definition: scaledidmatrix.hh:470
void solve(V &x, const V &b) const
Solve system A x = b.
Definition: scaledidmatrix.hh:394
bool exists(size_type i, size_type j) const
return true when (i,j) is in pattern
Definition: scaledidmatrix.hh:429
Iterator RowIterator
rename the iterators for easier access
Definition: scaledidmatrix.hh:93
ConstIterator const_iterator
typedef for stl compliant access
Definition: scaledidmatrix.hh:127
ScaledIdentityMatrix()
Default constructor.
Definition: scaledidmatrix.hh:66
bool operator==(const ScaledIdentityMatrix &other) const
comparison operator
Definition: scaledidmatrix.hh:223
ConstIterator beforeBegin() const
Definition: scaledidmatrix.hh:154
ScaledIdentityMatrix & operator/=(const K &k)
vector space division by scalar
Definition: scaledidmatrix.hh:196
friend std::ostream & operator<<(std::ostream &s, const ScaledIdentityMatrix< K, n > &a)
Sends the matrix to an output stream.
Definition: scaledidmatrix.hh:441
FieldTraits< field_type >::real_type frobenius_norm2() const
square of frobenius norm, need for block recursion
Definition: scaledidmatrix.hh:372
FieldTraits< field_type >::real_type frobenius_norm() const
frobenius norm: sqrt(sum over squared values of entries)
Definition: scaledidmatrix.hh:366
FieldTraits< field_type >::real_type infinity_norm() const
infinity norm (row sum norm, how to generalize for blocks?)
Definition: scaledidmatrix.hh:378
ConstIterator ConstRowIterator
rename the iterators for easier access
Definition: scaledidmatrix.hh:129
ConstIterator beforeEnd() const
Definition: scaledidmatrix.hh:147
size_type M() const
number of blocks in column direction
Definition: scaledidmatrix.hh:421
const_reference operator[](size_type i) const
Return const_reference object as row replacement.
Definition: scaledidmatrix.hh:458
ScaledIdentityMatrix(const K &k)
Constructor initializing the whole matrix with a scalar.
Definition: scaledidmatrix.hh:70
friend auto operator*(const ScaledIdentityMatrix &matrix, Scalar scalar)
vector space multiplication with scalar
Definition: scaledidmatrix.hh:207
FieldTraits< field_type >::real_type infinity_norm_real() const
simplified infinity norm (uses Manhattan norm for complex values)
Definition: scaledidmatrix.hh:384
ScaledIdentityMatrix & operator*=(const K &k)
vector space multiplication with scalar
Definition: scaledidmatrix.hh:189
void invert()
Compute inverse.
Definition: scaledidmatrix.hh:402
Iterator begin()
begin iterator
Definition: scaledidmatrix.hh:98
K & scalar()
Get reference to the scalar diagonal value.
Definition: scaledidmatrix.hh:484
K block_type
export the type representing the components
Definition: scaledidmatrix.hh:40
void mmhv(const X &x, Y &y) const
y -= A^H x
Definition: scaledidmatrix.hh:317
ScaledIdentityMatrix & operator+=(const ScaledIdentityMatrix &y)
vector space addition
Definition: scaledidmatrix.hh:162
void mtv(const X &x, Y &y) const
y = A^T x
Definition: scaledidmatrix.hh:250
reference operator[](size_type i)
Return reference object as row replacement.
Definition: scaledidmatrix.hh:452
ConstIterator begin() const
begin iterator
Definition: scaledidmatrix.hh:134
size_type N() const
number of blocks in row direction
Definition: scaledidmatrix.hh:415
This file implements a quadratic diagonal matrix of fixed size.
A few common exception classes.
Implements a matrix constructed from a given type representing a field and compile-time given number ...
Type traits to determine the type of reals (when working with complex numbers)
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
typename Overloads::ScalarType< std::decay_t< V > >::type Scalar
Element type of some SIMD type.
Definition: interface.hh:235
Dune namespace.
Definition: alignedallocator.hh:13
K conjugateComplex(const K &x)
compute conjugate complex of x
Definition: math.hh:164
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 21, 23:30, 2024)