5#ifndef DUNE_ISTL_SCALEDIDMATRIX_HH
6#define DUNE_ISTL_SCALEDIDMATRIX_HH
18#include <dune/common/exceptions.hh>
19#include <dune/common/fmatrix.hh>
20#include <dune/common/diagonalmatrix.hh>
21#include <dune/common/ftraits.hh>
40 template<
class K,
int n>
43 typedef DiagonalMatrixWrapper< ScaledIdentityMatrix<K,n> > WrapperType;
73 static constexpr std::integral_constant<size_type,n>
rows = {};
76 static constexpr std::integral_constant<size_type,n>
cols = {};
103 return (
this==&other);
108 typedef ContainerWrapperIterator<const WrapperType, reference, reference>
Iterator;
119 return Iterator(WrapperType(
this),0);
125 return Iterator(WrapperType(
this),n);
132 return Iterator(WrapperType(
this),n-1);
139 return Iterator(WrapperType(
this),-1);
144 typedef ContainerWrapperIterator<const WrapperType, const_reference, const_reference>
ConstIterator;
231 template <
class Scalar,
232 std::enable_if_t<IsNumber<Scalar>::value,
int> = 0>
239 template <
class Scalar,
240 std::enable_if_t<IsNumber<Scalar>::value,
int> = 0>
247 template <
class OtherScalar>
248 requires requires(K k, OtherScalar otherScalar) { k + otherScalar; }
252 for (
int i=0; i<n; i++)
253 fieldMatrix[i][i] += matrix.p_;
259 template <
class OtherScalar>
260 requires requires(K k, OtherScalar otherScalar) { k + otherScalar; }
265 Result result = fieldMatrix;
271 template <
class OtherScalar>
272 requires requires(K k, OtherScalar otherScalar) { k + otherScalar; }
276 return fieldMatrix + matrix;
280 template <
class OtherScalar>
281 requires requires(K k, OtherScalar otherScalar) { k + otherScalar; }
282 friend auto operator+= (DiagonalMatrix<OtherScalar,n>& diagonalMatrix,
285 for (std::size_t i=0; i<n; i++)
286 diagonalMatrix.
diagonal(i) += matrix.p_;
288 return diagonalMatrix;
292 template <
class OtherScalar>
293 requires requires(K k, OtherScalar otherScalar) { k + otherScalar; }
294 friend auto operator+ (
const DiagonalMatrix<OtherScalar,n>& diagonalMatrix,
297 using Result = DiagonalMatrix<typename PromotionTraits<K,OtherScalar>::PromotedType,n>;
298 Result result = diagonalMatrix;
304 template <
class OtherScalar>
305 requires requires(K k, OtherScalar otherScalar) { k + otherScalar; }
307 const DiagonalMatrix<OtherScalar,n>& diagonalMatrix)
309 return diagonalMatrix + matrix;
318 return p_==other.
scalar();
324 return p_!=other.
scalar();
332 template<
class X,
class Y>
333 void mv (
const X& x, Y& y)
const
335#ifdef DUNE_FMatrix_WITH_CHECKING
336 if (x.N()!=
M()) DUNE_THROW(FMatrixError,
"index out of range");
337 if (y.N()!=
N()) DUNE_THROW(FMatrixError,
"index out of range");
344 template<
class X,
class Y>
345 void mtv (
const X& x, Y& y)
const
351 template<
class X,
class Y>
352 void umv (
const X& x, Y& y)
const
354#ifdef DUNE_FMatrix_WITH_CHECKING
355 if (x.N()!=
M()) DUNE_THROW(FMatrixError,
"index out of range");
356 if (y.N()!=
N()) DUNE_THROW(FMatrixError,
"index out of range");
363 template<
class X,
class Y>
364 void umtv (
const X& x, Y& y)
const
366#ifdef DUNE_FMatrix_WITH_CHECKING
367 if (x.N()!=
N()) DUNE_THROW(FMatrixError,
"index out of range");
368 if (y.N()!=
M()) DUNE_THROW(FMatrixError,
"index out of range");
375 template<
class X,
class Y>
376 void umhv (
const X& x, Y& y)
const
378#ifdef DUNE_FMatrix_WITH_CHECKING
379 if (x.N()!=
N()) DUNE_THROW(FMatrixError,
"index out of range");
380 if (y.N()!=
M()) DUNE_THROW(FMatrixError,
"index out of range");
383 y[i] += conjugateComplex(p_)*x[i];
387 template<
class X,
class Y>
388 void mmv (
const X& x, Y& y)
const
390#ifdef DUNE_FMatrix_WITH_CHECKING
391 if (x.N()!=
M()) DUNE_THROW(FMatrixError,
"index out of range");
392 if (y.N()!=
N()) DUNE_THROW(FMatrixError,
"index out of range");
399 template<
class X,
class Y>
400 void mmtv (
const X& x, Y& y)
const
402#ifdef DUNE_FMatrix_WITH_CHECKING
403 if (x.N()!=
N()) DUNE_THROW(FMatrixError,
"index out of range");
404 if (y.N()!=
M()) DUNE_THROW(FMatrixError,
"index out of range");
411 template<
class X,
class Y>
412 void mmhv (
const X& x, Y& y)
const
414#ifdef DUNE_FMatrix_WITH_CHECKING
415 if (x.N()!=
N()) DUNE_THROW(FMatrixError,
"index out of range");
416 if (y.N()!=
M()) DUNE_THROW(FMatrixError,
"index out of range");
419 y[i] -= conjugateComplex(p_)*x[i];
423 template<
class X,
class Y>
424 void usmv (
const K& alpha,
const X& x, Y& y)
const
426#ifdef DUNE_FMatrix_WITH_CHECKING
427 if (x.N()!=
M()) DUNE_THROW(FMatrixError,
"index out of range");
428 if (y.N()!=
N()) DUNE_THROW(FMatrixError,
"index out of range");
431 y[i] += alpha * p_ * x[i];
435 template<
class X,
class Y>
436 void usmtv (
const K& alpha,
const X& x, Y& y)
const
438#ifdef DUNE_FMatrix_WITH_CHECKING
439 if (x.N()!=
N()) DUNE_THROW(FMatrixError,
"index out of range");
440 if (y.N()!=
M()) DUNE_THROW(FMatrixError,
"index out of range");
443 y[i] += alpha * p_ * x[i];
447 template<
class X,
class Y>
448 void usmhv (
const K& alpha,
const X& x, Y& y)
const
450#ifdef DUNE_FMatrix_WITH_CHECKING
451 if (x.N()!=
N()) DUNE_THROW(FMatrixError,
"index out of range");
452 if (y.N()!=
M()) DUNE_THROW(FMatrixError,
"index out of range");
455 y[i] += alpha * conjugateComplex(p_) * x[i];
468 return fvmeta::sqrt(n*p_*p_);
490 return fvmeta::absreal(p_);
502 for (
int i=0; i<n; i++)
515 return std::pow(p_,n);
537#ifdef DUNE_FMatrix_WITH_CHECKING
538 if (i<0 || i>=n) DUNE_THROW(FMatrixError,
"row index out of range");
539 if (j<0 || j>=n) DUNE_THROW(FMatrixError,
"column index out of range");
551 s << ((i==j) ? a.p_ : 0) <<
" ";
560 return reference(
const_cast<K*
>(&p_), i);
601 template <
class DenseMatrix,
class field,
int N>
603 static void apply(DenseMatrix& denseMatrix,
605 assert(denseMatrix.M() == N);
606 assert(denseMatrix.N() == N);
607 denseMatrix = field(0);
608 for (
int i = 0; i < N; ++i)
609 denseMatrix[i][i] = rhs.
scalar();
613 template<
class K,
int n>
617 using real_type =
typename FieldTraits<field_type>::real_type;
Definition allocator.hh:11
Definition matrixutils.hh:27
A multiple of the identity matrix of static size.
Definition scaledidmatrix.hh:42
void usmhv(const K &alpha, const X &x, Y &y) const
y += alpha A^H x
Definition scaledidmatrix.hh:448
void mmtv(const X &x, Y &y) const
y -= A^T x
Definition scaledidmatrix.hh:400
ScaledIdentityMatrix & operator-=(const ScaledIdentityMatrix &y)
Vector space subtraction.
Definition scaledidmatrix.hh:190
const_row_type::ConstIterator ConstColIterator
rename the iterators for easier access
Definition scaledidmatrix.hh:150
ConstIterator end() const
end iterator
Definition scaledidmatrix.hh:159
Iterator beforeBegin()
Definition scaledidmatrix.hh:137
static constexpr std::integral_constant< size_type, n > rows
The number of matrix rows.
Definition scaledidmatrix.hh:73
bool operator!=(const ScaledIdentityMatrix &other) const
Test for inequality.
Definition scaledidmatrix.hh:322
void mmv(const X &x, Y &y) const
y -= A x
Definition scaledidmatrix.hh:388
std::size_t size_type
The type used for the indices and sizes.
Definition scaledidmatrix.hh:55
void usmv(const K &alpha, const X &x, Y &y) const
y += alpha A x
Definition scaledidmatrix.hh:424
row_type::Iterator ColIterator
rename the iterators for easier access
Definition scaledidmatrix.hh:114
const_row_type const_reference
Definition scaledidmatrix.hh:70
void mv(const X &x, Y &y) const
y = A x
Definition scaledidmatrix.hh:333
void umtv(const X &x, Y &y) const
y += A^T x
Definition scaledidmatrix.hh:364
void umhv(const X &x, Y &y) const
y += A^H x
Definition scaledidmatrix.hh:376
DiagonalRowVector< K, n > row_type
Type of a single matrix row.
Definition scaledidmatrix.hh:65
ContainerWrapperIterator< const WrapperType, reference, reference > Iterator
Iterator class for sequential access.
Definition scaledidmatrix.hh:108
Iterator beforeEnd()
Definition scaledidmatrix.hh:130
K determinant() const
Calculates the determinant of this matrix.
Definition scaledidmatrix.hh:514
K field_type
The type representing numbers.
Definition scaledidmatrix.hh:49
void usmtv(const K &alpha, const X &x, Y &y) const
y += alpha A^T x
Definition scaledidmatrix.hh:436
Iterator end()
end iterator
Definition scaledidmatrix.hh:123
Iterator iterator
typedef for stl compliant access
Definition scaledidmatrix.hh:110
static constexpr std::integral_constant< size_type, n > cols
The number of matrix columns.
Definition scaledidmatrix.hh:76
const K & scalar() const
Get const reference to the scalar diagonal value.
Definition scaledidmatrix.hh:583
friend auto operator+(const FieldMatrix< OtherScalar, n, n > &fieldMatrix, const ScaledIdentityMatrix &matrix)
Addition of ScaledIdentityMatrix to FieldMatrix.
Definition scaledidmatrix.hh:261
void umv(const X &x, Y &y) const
y += A x
Definition scaledidmatrix.hh:352
const K & diagonal(size_type) const
Get const reference to diagonal entry.
Definition scaledidmatrix.hh:570
ScaledIdentityMatrix & operator=(const K &k)
Assignment from scalar.
Definition scaledidmatrix.hh:91
ContainerWrapperIterator< const WrapperType, const_reference, const_reference > ConstIterator
Iterator class for sequential access.
Definition scaledidmatrix.hh:144
K & diagonal(size_type)
Get reference to diagonal entry.
Definition scaledidmatrix.hh:576
void solve(V &x, const V &b) const
Solve linear system A x = b.
Definition scaledidmatrix.hh:500
bool exists(size_type i, size_type j) const
Return true if (i,j) is in the matrix pattern, i.e., if i==j.
Definition scaledidmatrix.hh:535
Iterator RowIterator
rename the iterators for easier access
Definition scaledidmatrix.hh:112
ConstIterator const_iterator
typedef for stl compliant access
Definition scaledidmatrix.hh:146
ScaledIdentityMatrix()
Default constructor.
Definition scaledidmatrix.hh:81
bool operator==(const ScaledIdentityMatrix &other) const
Test for equality.
Definition scaledidmatrix.hh:316
ConstIterator beforeBegin() const
Definition scaledidmatrix.hh:173
ScaledIdentityMatrix & operator/=(const K &k)
Vector space division by scalar.
Definition scaledidmatrix.hh:224
friend std::ostream & operator<<(std::ostream &s, const ScaledIdentityMatrix< K, n > &a)
Sends the matrix to an output stream.
Definition scaledidmatrix.hh:547
FieldTraits< field_type >::real_type frobenius_norm2() const
The square of the Frobenius norm.
Definition scaledidmatrix.hh:472
FieldTraits< field_type >::real_type frobenius_norm() const
The Frobenius norm.
Definition scaledidmatrix.hh:466
FieldTraits< field_type >::real_type infinity_norm() const
The row sum norm.
Definition scaledidmatrix.hh:482
ConstIterator ConstRowIterator
rename the iterators for easier access
Definition scaledidmatrix.hh:148
ConstIterator beforeEnd() const
Definition scaledidmatrix.hh:166
size_type M() const
number of blocks in column direction
Definition scaledidmatrix.hh:527
const_reference operator[](size_type i) const
Return const_reference object as row replacement.
Definition scaledidmatrix.hh:564
ScaledIdentityMatrix(const K &k)
Constructor initializing the whole matrix with a scalar.
Definition scaledidmatrix.hh:85
friend auto operator*(const ScaledIdentityMatrix &matrix, Scalar scalar)
Vector space multiplication with scalar.
Definition scaledidmatrix.hh:233
FieldTraits< field_type >::real_type infinity_norm_real() const
simplified infinity norm (uses Manhattan norm for complex values)
Definition scaledidmatrix.hh:488
ScaledIdentityMatrix & operator*=(const K &k)
Vector space multiplication with scalar.
Definition scaledidmatrix.hh:217
bool identical(const ScaledIdentityMatrix< K, n > &other) const
Check if matrix is identical to other matrix.
Definition scaledidmatrix.hh:101
void invert()
Compute inverse.
Definition scaledidmatrix.hh:508
Iterator begin()
begin iterator
Definition scaledidmatrix.hh:117
K & scalar()
Get reference to the scalar diagonal value.
Definition scaledidmatrix.hh:590
row_type reference
Definition scaledidmatrix.hh:66
K block_type
The type representing matrix entries (which may be matrices themselves).
Definition scaledidmatrix.hh:52
void mmhv(const X &x, Y &y) const
y -= A^H x
Definition scaledidmatrix.hh:412
ScaledIdentityMatrix & operator+=(const ScaledIdentityMatrix &y)
Vector space addition.
Definition scaledidmatrix.hh:183
DiagonalRowVectorConst< K, n > const_row_type
Const type of a single row.
Definition scaledidmatrix.hh:69
void mtv(const X &x, Y &y) const
y = A^T x
Definition scaledidmatrix.hh:345
reference operator[](size_type i)
Return reference object as row replacement.
Definition scaledidmatrix.hh:558
ConstIterator begin() const
begin iterator
Definition scaledidmatrix.hh:153
size_type N() const
number of blocks in row direction
Definition scaledidmatrix.hh:521
static void apply(DenseMatrix &denseMatrix, ScaledIdentityMatrix< field, N > const &rhs)
Definition scaledidmatrix.hh:603
typename ScaledIdentityMatrix< K, n >::field_type field_type
Definition scaledidmatrix.hh:616
typename FieldTraits< field_type >::real_type real_type
Definition scaledidmatrix.hh:617