5#ifndef DUNE_DIAGONAL_MATRIX_HH
6#define DUNE_DIAGONAL_MATRIX_HH
17#include <initializer_list>
54 template<
class K,
int n>
85 constexpr static int rows = n;
87 constexpr static int cols = n;
121 std::copy_n(l.begin(), std::min<std::size_t>(
rows, l.size()),
126 template <
class OtherK>
139 template <
class OtherK>
149 return (
this==&other);
171 return Iterator(WrapperType(
this),0);
177 return Iterator(WrapperType(
this),n);
184 return Iterator(WrapperType(
this),n-1);
191 return Iterator(WrapperType(
this),-1);
294 template<
class X,
class Y>
295 void mv (
const X& x, Y& y)
const
297#ifdef DUNE_FMatrix_WITH_CHECKING
302 y[i] = diag_[i] * x[i];
306 template<
class X,
class Y>
307 void mtv (
const X& x, Y& y)
const
313 template<
class X,
class Y>
314 void umv (
const X& x, Y& y)
const
316#ifdef DUNE_FMatrix_WITH_CHECKING
321 y[i] += diag_[i] * x[i];
325 template<
class X,
class Y>
326 void umtv (
const X& x, Y& y)
const
328#ifdef DUNE_FMatrix_WITH_CHECKING
333 y[i] += diag_[i] * x[i];
337 template<
class X,
class Y>
338 void umhv (
const X& x, Y& y)
const
340#ifdef DUNE_FMatrix_WITH_CHECKING
349 template<
class X,
class Y>
350 void mmv (
const X& x, Y& y)
const
352#ifdef DUNE_FMatrix_WITH_CHECKING
357 y[i] -= diag_[i] * x[i];
361 template<
class X,
class Y>
362 void mmtv (
const X& x, Y& y)
const
364#ifdef DUNE_FMatrix_WITH_CHECKING
369 y[i] -= diag_[i] * x[i];
373 template<
class X,
class Y>
374 void mmhv (
const X& x, Y& y)
const
376#ifdef DUNE_FMatrix_WITH_CHECKING
385 template<
class X,
class Y>
387 const X& x, Y& y)
const
389#ifdef DUNE_FMatrix_WITH_CHECKING
394 y[i] += alpha * diag_[i] * x[i];
398 template<
class X,
class Y>
400 const X& x, Y& y)
const
402#ifdef DUNE_FMatrix_WITH_CHECKING
407 y[i] += alpha * diag_[i] * x[i];
411 template<
class X,
class Y>
413 const X& x, Y& y)
const
415#ifdef DUNE_FMatrix_WITH_CHECKING
428 return diag_.two_norm();
434 return diag_.two_norm2();
440 return diag_.infinity_norm();
446 return diag_.infinity_norm_real();
457 for (
int i=0; i<n; i++)
458 x[i] = b[i]/diag_[i];
465 for (
int i=0; i<n; i++)
473 for (
int i=1; i<n; i++)
484 template <
class OtherScalar>
489 for(
int i=0; i<n; ++i)
503 template <
class OtherMatrix,
504 std::enable_if_t<(Impl::IsDenseMatrix<OtherMatrix>::value),
int> = 0,
505 std::enable_if_t<(not Impl::IsFieldMatrix<OtherMatrix>::value),
int> = 0>
507 const OtherMatrix& matrixB)
513 if constexpr (Impl::IsStaticSizeMatrix_v<OtherMatrix>) {
514 static_assert(n == OtherMatrix::rows);
517 assert(n == matrixB.N());
522 for (
int i = 0; i < result.N(); ++i)
523 for (
int j = 0; j < result.M(); ++j)
524 result[i][j] = matrixA.
diagonal(i) * matrixB[i][j];
561 s << ((i==j) ? a.diag_[i] : 0) <<
" ";
570 return reference(
const_cast<K*
>(&diag_[i]), i);
609 template<
class K,
int n >
626 typedef typename Base::size_type
size_type;
632 typedef typename Base::row_type
row_type;
639 constexpr static int rows = 1;
642 constexpr static int cols = 1;
651 (*this)[0][0] = scalar;
655 template <
class OtherK>
657 : Base(FieldMatrix<OtherK,1,1>(other))
663 return (*
this)[0][0];
669 return (*
this)[0][0];
673 const FieldVector<K,1>&
diagonal()
const
692 template <
class OtherScalar>
699 template <
class OtherMatrix,
700 std::enable_if_t<(Impl::IsDenseMatrix<OtherMatrix>::value),
int> = 0,
701 std::enable_if_t<(not Impl::IsFieldMatrix<OtherMatrix>::value),
int> = 0>
703 const OtherMatrix& matrixB)
709 if constexpr (Impl::IsStaticSizeMatrix_v<OtherMatrix>) {
710 static_assert(1 == OtherMatrix::rows);
711 return FieldMatrix<F, 1, OtherMatrix::cols>{};
713 assert(1 == matrixB.N());
714 return DynamicMatrix<F>{1,matrixB.M()};
718 for (
int i = 0; i < result.N(); ++i)
719 for (
int j = 0; j < result.M(); ++j)
720 result[i][j] = matrixA.diagonal(i) * matrixB[i][j];
728 template<
class DiagonalMatrixType>
731 typedef typename DiagonalMatrixType::reference reference;
732 typedef typename DiagonalMatrixType::const_reference const_reference;
733 typedef typename DiagonalMatrixType::field_type K;
735 typedef std::size_t size_type;
748 mat_(const_cast<DiagonalMatrixType*>(mat))
758 row_ = row_type(&(mat_->diagonal(i)), i);
764 return mat_==other.mat_;
769 mutable DiagonalMatrixType* mat_;
770 mutable row_type row_;
776 template<
class K,
int n >
779 template<
class DiagonalMatrixType>
906 return const_cast<K*
>(
p_);
919 template<
class K,
int n >
922 template<
class DiagonalMatrixType>
1028 template<
class K,
int n>
1034 template<
class K,
int n>
1040 template<
class K,
int n>
1046 template<
class K,
int n>
1076 template<
class CW,
class T,
class R>
1079 typedef typename std::remove_const<CW>::type NonConstCW;
1084 typedef
ContainerWrapperIterator<CW, typename mutable_reference<T>::type, typename mutable_reference<R>::type> MyType;
1085 typedef
ContainerWrapperIterator<CW, typename const_reference<T>::type, typename const_reference<R>::type> MyConstType;
1091 containerWrapper_(),
1096 containerWrapper_(containerWrapper),
1100 template<
class OtherContainerWrapperIteratorType>
1102 containerWrapper_(other.containerWrapper_),
1103 position_(other.position_)
1107 containerWrapper_(other.containerWrapper_),
1108 position_(other.position_)
1112 containerWrapper_(other.containerWrapper_),
1113 position_(other.position_)
1116 template<
class OtherContainerWrapperIteratorType>
1119 containerWrapper_ = other.containerWrapper_;
1120 position_ = other.position_;
1128 return containerWrapper_.pointer(position_);
1134 return position_ == other.position_ && containerWrapper_.identical(other.containerWrapper_);
1139 return position_ == other.position_ && containerWrapper_.identical(other.containerWrapper_);
1144 return *containerWrapper_.pointer(position_);
1161 return *containerWrapper_.pointer(position_+i);
1166 position_=position_+n;
1169 template<
class OtherContainerWrapperIteratorType>
1170 std::ptrdiff_t
distanceTo(OtherContainerWrapperIteratorType& other)
const
1172 assert(containerWrapper_.identical(other));
1173 return other.position_ - position_;
1178 return containerWrapper_.realIndex(position_);
1182 NonConstCW containerWrapper_;
1186 template <
class DenseMatrix,
class field,
int N>
1190#ifdef DUNE_CHECK_BOUNDS
1191 if (denseMatrix.
M() != N || denseMatrix.
N() != N)
1195 denseMatrix = field(0);
1196 for (
int i = 0; i < N; ++i)
1197 denseMatrix[i][i] = rhs.
diagonal()[i];
Traits for type conversions and type information.
Implements a generic iterator class for writing stl conformant iterators.
Implements a vector constructed from a given type representing a field and a compile-time given size.
Type traits to determine the type of reals (when working with complex numbers).
Implements a matrix constructed from a given type representing a field and compile-time given number ...
A few common exception classes.
This file implements a dense matrix with dynamic numbers of rows and columns.
Implements a matrix constructed from a given type representing a field and a compile-time given numbe...
Macro for wrapping boundary checks.
#define DUNE_ASSERT_BOUNDS(cond)
If DUNE_CHECK_BOUNDS is defined: check if condition cond holds; otherwise, do nothing.
Definition boundschecking.hh:30
friend class DiagonalMatrix
Definition diagonalmatrix.hh:57
ConstIterator beforeBegin() const
Definition diagonalmatrix.hh:225
DiagonalMatrix< K, n > transposed() const
Return transposed of the matrix as DiagonalMatrix.
Definition diagonalmatrix.hh:153
void mmhv(const X &x, Y &y) const
y -= A^H x
Definition diagonalmatrix.hh:374
DiagonalMatrix & operator*=(const K &k)
vector space multiplication with scalar
Definition diagonalmatrix.hh:263
FieldTraits< K >::field_type field_type
Definition diagonalmatrix.hh:612
std::size_t size_type
The type used for the index access and size operations.
Definition diagonalmatrix.hh:71
size_type dim() const
dimension of the vector space
Definition diagonalmatrix.hh:880
ConstIterator ConstRowIterator
rename the iterators for easier access
Definition diagonalmatrix.hh:200
row_type row_reference
Definition diagonalmatrix.hh:79
K & operator[](size_type i)
random access
Definition diagonalmatrix.hh:959
ContainerWrapperIterator & operator=(OtherContainerWrapperIteratorType &other)
Definition diagonalmatrix.hh:1117
static constexpr int rows
The number of rows.
Definition diagonalmatrix.hh:85
static constexpr size_type M()
number of blocks in column direction
Definition diagonalmatrix.hh:537
FieldVector< K, n > & diagonal()
Get reference to diagonal vector.
Definition diagonalmatrix.hh:598
void usmhv(const typename FieldTraits< Y >::field_type &alpha, const X &x, Y &y) const
y += alpha A^H x
Definition diagonalmatrix.hh:412
Iterator iterator
Definition diagonalmatrix.hh:968
void increment()
Definition diagonalmatrix.hh:1147
static constexpr int blocklevel
The number of block levels we contain. This is the leaf, that is, 1.
Definition diagonalmatrix.hh:74
DiagonalRowVectorConst< K, n > type
Definition diagonalmatrix.hh:1037
T * operator->() const
Definition diagonalmatrix.hh:1126
static constexpr int size
The size of this vector.
Definition diagonalmatrix.hh:804
K * pointer(size_type i) const
Definition diagonalmatrix.hh:904
K field_type
export the type representing the field
Definition diagonalmatrix.hh:792
DiagonalRowVector< K, n > type
Definition diagonalmatrix.hh:1043
ConstIterator beforeEnd() const
Definition diagonalmatrix.hh:853
const_row_type::ConstIterator ConstColIterator
rename the iterators for easier access
Definition diagonalmatrix.hh:202
bool exists(size_type i, size_type j) const
return true when (i,j) is in pattern
Definition diagonalmatrix.hh:547
ContainerWrapperIterator< const WrapperType, const_reference, const_reference > ConstIterator
Iterator class for sequential access.
Definition diagonalmatrix.hh:196
const_row_type const_row_reference
Definition diagonalmatrix.hh:82
static constexpr size_type size()
Definition diagonalmatrix.hh:91
size_type row_
Definition diagonalmatrix.hh:916
ContainerWrapperIterator(CW containerWrapper, int position)
Definition diagonalmatrix.hh:1095
DiagonalRowVector(K *p, int col)
Constructor making vector with identical coordinates.
Definition diagonalmatrix.hh:945
K & diagonal(size_type i)
Get reference to diagonal entry.
Definition diagonalmatrix.hh:586
void solve(V &x, const V &b) const
Solve system A x = b.
Definition diagonalmatrix.hh:455
static constexpr size_type N()
number of blocks in row direction
Definition diagonalmatrix.hh:531
Iterator beforeBegin()
Definition diagonalmatrix.hh:991
ContainerWrapperIterator(OtherContainerWrapperIteratorType &other)
Definition diagonalmatrix.hh:1101
const_reference operator[](size_type i) const
Return const_reference object as row replacement.
Definition diagonalmatrix.hh:574
Iterator iterator
typedef for stl compliant access
Definition diagonalmatrix.hh:162
ConstIterator begin() const
begin ConstIterator
Definition diagonalmatrix.hh:840
ConstIterator const_iterator
Definition diagonalmatrix.hh:999
bool identical(const DiagonalRowVectorConst< K, n > &other) const
Definition diagonalmatrix.hh:829
DiagonalMatrix & operator-=(const DiagonalMatrix &y)
vector space subtraction
Definition diagonalmatrix.hh:242
DiagonalRowVectorConst(K *p, int col)
Constructor making vector with identical coordinates.
Definition diagonalmatrix.hh:813
void mmtv(const X &x, Y &y) const
y -= A^T x
Definition diagonalmatrix.hh:362
static constexpr int blocklevel
The number of block levels we contain.
Definition diagonalmatrix.hh:801
row_type * pointer(int i) const
Definition diagonalmatrix.hh:756
DiagonalMatrix(const K &k)
Constructor initializing the whole matrix with a scalar.
Definition diagonalmatrix.hh:102
ContainerWrapperIterator< DiagonalRowVector< K, n >, K, K & > Iterator
Iterator class for sequential access.
Definition diagonalmatrix.hh:966
R elementAt(int i) const
Definition diagonalmatrix.hh:1159
std::ptrdiff_t distanceTo(OtherContainerWrapperIteratorType &other) const
Definition diagonalmatrix.hh:1170
Iterator beforeEnd()
Definition diagonalmatrix.hh:984
void umtv(const X &x, Y &y) const
y += A^T x
Definition diagonalmatrix.hh:326
ConstIterator const_iterator
typedef for stl compliant access
Definition diagonalmatrix.hh:837
double infinity_norm_real() const
simplified infinity norm (uses Manhattan norm for complex values)
Definition diagonalmatrix.hh:444
DiagonalMatrixWrapper(const DiagonalMatrixType *mat)
Definition diagonalmatrix.hh:747
void umv(const X &x, Y &y) const
y += A x
Definition diagonalmatrix.hh:314
ContainerWrapperIterator< const WrapperType, reference, reference > Iterator
Iterator class for sequential access.
Definition diagonalmatrix.hh:160
DiagonalRowVector< K, n > type
Definition diagonalmatrix.hh:1049
void mv(const X &x, Y &y) const
y = A x
Definition diagonalmatrix.hh:295
double frobenius_norm() const
frobenius norm: sqrt(sum over squared values of entries)
Definition diagonalmatrix.hh:426
ConstIterator end() const
end iterator
Definition diagonalmatrix.hh:211
static constexpr int cols
The number of columns.
Definition diagonalmatrix.hh:87
static constexpr int dimension
Definition diagonalmatrix.hh:785
K * p_
Definition diagonalmatrix.hh:915
size_type rowIndex() const
index of this row in surrounding matrix
Definition diagonalmatrix.hh:886
R dereference() const
Definition diagonalmatrix.hh:1142
DiagonalRowVector & operator=(const K &k)
Assignment operator for scalar.
Definition diagonalmatrix.hh:950
bool operator!=(const DiagonalMatrix &other) const
incomparison operator
Definition diagonalmatrix.hh:285
ContainerWrapperIterator()
Definition diagonalmatrix.hh:1090
value_type field_type
Definition diagonalmatrix.hh:65
void advance(int n)
Definition diagonalmatrix.hh:1164
ConstIterator begin() const
begin iterator
Definition diagonalmatrix.hh:205
Iterator begin()
begin iterator
Definition diagonalmatrix.hh:971
void mmv(const X &x, Y &y) const
y -= A x
Definition diagonalmatrix.hh:350
DiagonalMatrix & operator/=(const K &k)
vector space division by scalar
Definition diagonalmatrix.hh:270
ConstIterator const_iterator
typedef for stl compliant access
Definition diagonalmatrix.hh:198
const_row_type const_reference
Definition diagonalmatrix.hh:81
bool identical(const DiagonalMatrix< K, n > &other) const
Check if matrix is the same object as the other matrix.
Definition diagonalmatrix.hh:147
size_type realIndex(int i) const
Definition diagonalmatrix.hh:751
Iterator end()
end iterator
Definition diagonalmatrix.hh:175
const K & operator[](size_type i) const
same for read only access
Definition diagonalmatrix.hh:821
DiagonalRowVector()
Constructor making uninitialized vector.
Definition diagonalmatrix.hh:941
row_type reference
Definition diagonalmatrix.hh:78
DiagonalRowVectorConst * operator&()
Definition diagonalmatrix.hh:909
bool operator==(const DiagonalRowVectorConst &y) const
Binary vector comparison.
Definition diagonalmatrix.hh:866
double frobenius_norm2() const
square of frobenius norm, need for block recursion
Definition diagonalmatrix.hh:432
constexpr DiagonalMatrix()=default
Default constructor.
void mtv(const X &x, Y &y) const
y = A^T x
Definition diagonalmatrix.hh:307
ContainerWrapperIterator< DiagonalRowVectorConst< K, n >, const K, const K & > ConstIterator
Definition diagonalmatrix.hh:997
bool identical(const DiagonalMatrixWrapper &other) const
Definition diagonalmatrix.hh:762
void usmtv(const typename FieldTraits< Y >::field_type &alpha, const X &x, Y &y) const
y += alpha A^T x
Definition diagonalmatrix.hh:399
void decrement()
Definition diagonalmatrix.hh:1153
std::ptrdiff_t index() const
Definition diagonalmatrix.hh:1176
const FieldVector< K, n > & diagonal() const
Get const reference to diagonal vector.
Definition diagonalmatrix.hh:592
void invert()
Compute inverse.
Definition diagonalmatrix.hh:462
DiagonalRowVector * operator&()
Definition diagonalmatrix.hh:1015
const K & diagonal(size_type i) const
Get const reference to diagonal entry.
Definition diagonalmatrix.hh:580
size_type N() const
number of blocks in the vector (are of size 1 here)
Definition diagonalmatrix.hh:874
DiagonalMatrix(std::initializer_list< K > const &l)
Construct diagonal matrix from an initializer list.
Definition diagonalmatrix.hh:119
row_type::Iterator ColIterator
rename the iterators for easier access
Definition diagonalmatrix.hh:166
K field_type
Definition diagonalmatrix.hh:932
reference operator[](size_type i)
Return reference object as row replacement.
Definition diagonalmatrix.hh:568
DiagonalRowVectorConst< K, n > type
Definition diagonalmatrix.hh:1031
Iterator end()
end iterator
Definition diagonalmatrix.hh:977
DiagonalMatrix(const FieldVector< K, n > &diag)
Constructor initializing the diagonal with a vector.
Definition diagonalmatrix.hh:107
std::size_t size_type
Definition diagonalmatrix.hh:938
friend class DiagonalMatrixWrapper
Definition diagonalmatrix.hh:780
void usmv(const typename FieldTraits< Y >::field_type &alpha, const X &x, Y &y) const
y += alpha A x
Definition diagonalmatrix.hh:386
double infinity_norm() const
infinity norm (row sum norm, how to generalize for blocks?)
Definition diagonalmatrix.hh:438
DiagonalMatrix & operator+=(const DiagonalMatrix &y)
vector space addition
Definition diagonalmatrix.hh:235
K block_type
export the type representing the components
Definition diagonalmatrix.hh:68
void umhv(const X &x, Y &y) const
y += A^H x
Definition diagonalmatrix.hh:338
Iterator beforeBegin()
Definition diagonalmatrix.hh:189
static void apply(DenseMatrix &denseMatrix, DiagonalMatrix< field, N > const &rhs)
Definition diagonalmatrix.hh:1188
ContainerWrapperIterator(const MyType &other)
Definition diagonalmatrix.hh:1106
ConstIterator end() const
end ConstIterator
Definition diagonalmatrix.hh:846
std::size_t size_type
The type used for the index access and size operation.
Definition diagonalmatrix.hh:798
DiagonalMatrix & operator=(const K &k)
Assignment from a scalar.
Definition diagonalmatrix.hh:132
DiagonalRowVectorConst()
Constructor making uninitialized vector.
Definition diagonalmatrix.hh:807
Iterator beforeEnd()
Definition diagonalmatrix.hh:182
size_type realIndex(int i) const
Definition diagonalmatrix.hh:899
friend std::ostream & operator<<(std::ostream &s, const DiagonalMatrix< K, n > &a)
Sends the matrix to an output stream.
Definition diagonalmatrix.hh:557
K value_type
export the type representing the field
Definition diagonalmatrix.hh:64
K block_type
export the type representing the components
Definition diagonalmatrix.hh:795
K determinant() const
calculates the determinant of this matrix
Definition diagonalmatrix.hh:470
FieldTraits< K >::real_type real_type
Definition diagonalmatrix.hh:613
ConstIterator beforeBegin() const
Definition diagonalmatrix.hh:860
DiagonalMatrixWrapper()
Definition diagonalmatrix.hh:743
ContainerWrapperIterator< DiagonalRowVectorConst< K, n >, const K, const K & > ConstIterator
ConstIterator class for sequential access.
Definition diagonalmatrix.hh:835
Iterator begin()
begin iterator
Definition diagonalmatrix.hh:169
bool operator==(const DiagonalMatrix &other) const
comparison operator
Definition diagonalmatrix.hh:279
DiagonalRowVectorConst< K, n > const_row_type
Definition diagonalmatrix.hh:80
DiagonalRowVector< K, n > row_type
Each row is implemented by a field vector.
Definition diagonalmatrix.hh:77
bool equals(const MyType &other) const
Definition diagonalmatrix.hh:1132
Iterator RowIterator
rename the iterators for easier access
Definition diagonalmatrix.hh:164
K block_type
Definition diagonalmatrix.hh:935
ConstIterator beforeEnd() const
Definition diagonalmatrix.hh:218
DiagonalMatrix(const DiagonalMatrix< OtherK, n > &other)
Converting constructor.
Definition diagonalmatrix.hh:127
const K & diagonal() const
the diagonal value
Definition diagonalmatrix.hh:892
#define DUNE_THROW(E,...)
Definition exceptions.hh:314
constexpr decltype(auto) operator*() const
Dereferencing operator.
Definition iteratorfacades.hh:1119
Dune namespace
Definition alignedallocator.hh:13
constexpr K conjugateComplex(const K &x)
compute conjugate complex of x
Definition math.hh:146
A dense n x m matrix.
Definition densematrix.hh:145
constexpr size_type M() const
number of columns
Definition densematrix.hh:708
constexpr size_type N() const
number of rows
Definition densematrix.hh:702
A dense n x m matrix.
Definition fmatrix.hh:117
vector space out of a tensor product of fields.
Definition fvector.hh:97
you have to specialize this structure for any type that should be assignable to a DenseMatrix
Definition densematrix.hh:61
Error thrown if operations of a FieldMatrix fail.
Definition densematrix.hh:131
Definition diagonalmatrix.hh:778
Definition diagonalmatrix.hh:921
Definition diagonalmatrix.hh:730
Iterator class for sparse vector-like containers.
Definition diagonalmatrix.hh:1078
ContainerWrapperIterator(const MyConstType &other)
Definition diagonalmatrix.hh:1111
bool equals(const MyConstType &other) const
Definition diagonalmatrix.hh:1137
A diagonal matrix of static size.
Definition diagonalmatrix.hh:56
Construct a matrix with a dynamic size.
Definition dynmatrix.hh:61
Default exception class for range errors.
Definition exceptions.hh:348
T field_type
export the type representing the field
Definition ftraits.hh:28
T real_type
export the type representing the real type of the field
Definition ftraits.hh:30
Get the 'const' version of a reference to a mutable object.
Definition genericiterator.hh:87
get the 'mutable' version of a reference to a const object
Definition genericiterator.hh:116
Facade class for stl conformant bidirectional iterators.
Definition iteratorfacades.hh:275
decltype(std::declval< T1 >()+std::declval< T2 >()) PromotedType
Definition promotiontraits.hh:28