5#ifndef DUNE_DIAGONAL_MATRIX_HH
6#define DUNE_DIAGONAL_MATRIX_HH
17#include <initializer_list>
32 template<
class K,
int n >
class DiagonalRowVectorConst;
33 template<
class K,
int n >
class DiagonalRowVector;
34 template<
class DiagonalMatrixType >
class DiagonalMatrixWrapper;
35 template<
class C,
class T,
class R>
class ContainerWrapperIterator;
51 template<
class K,
int n>
81 constexpr static int rows = n;
83 constexpr static int cols = n;
117 std::copy_n(l.begin(), std::min(
static_cast<std::size_t
>(
rows),
132 return (
this==&other);
277 template<
class X,
class Y>
278 void mv (
const X& x, Y& y)
const
280#ifdef DUNE_FMatrix_WITH_CHECKING
285 y[i] = diag_[i] * x[i];
289 template<
class X,
class Y>
290 void mtv (
const X& x, Y& y)
const
296 template<
class X,
class Y>
297 void umv (
const X& x, Y& y)
const
299#ifdef DUNE_FMatrix_WITH_CHECKING
304 y[i] += diag_[i] * x[i];
308 template<
class X,
class Y>
309 void umtv (
const X& x, Y& y)
const
311#ifdef DUNE_FMatrix_WITH_CHECKING
316 y[i] += diag_[i] * x[i];
320 template<
class X,
class Y>
321 void umhv (
const X& x, Y& y)
const
323#ifdef DUNE_FMatrix_WITH_CHECKING
332 template<
class X,
class Y>
333 void mmv (
const X& x, Y& y)
const
335#ifdef DUNE_FMatrix_WITH_CHECKING
340 y[i] -= diag_[i] * x[i];
344 template<
class X,
class Y>
345 void mmtv (
const X& x, Y& y)
const
347#ifdef DUNE_FMatrix_WITH_CHECKING
352 y[i] -= diag_[i] * x[i];
356 template<
class X,
class Y>
357 void mmhv (
const X& x, Y& y)
const
359#ifdef DUNE_FMatrix_WITH_CHECKING
368 template<
class X,
class Y>
370 const X& x, Y& y)
const
372#ifdef DUNE_FMatrix_WITH_CHECKING
377 y[i] += alpha * diag_[i] * x[i];
381 template<
class X,
class Y>
383 const X& x, Y& y)
const
385#ifdef DUNE_FMatrix_WITH_CHECKING
390 y[i] += alpha * diag_[i] * x[i];
394 template<
class X,
class Y>
396 const X& x, Y& y)
const
398#ifdef DUNE_FMatrix_WITH_CHECKING
440 for (
int i=0; i<n; i++)
441 x[i] = b[i]/diag_[i];
448 for (
int i=0; i<n; i++)
449 diag_[i] = real_type(1.0)/diag_[i];
456 for (
int i=1; i<n; i++)
467 template <
class OtherScalar>
472 for(
int i=0; i<n; ++i)
512 s << ((i==j) ? a.diag_[i] : 0) <<
" ";
521 return reference(
const_cast<K*
>(&diag_[i]), i);
560 template<
class K,
int n >
577 typedef typename Base::size_type
size_type;
583 typedef typename Base::row_type
row_type;
590 constexpr static int rows = 1;
593 constexpr static int cols = 1;
602 (*this)[0][0] = scalar;
608 return (*
this)[0][0];
614 return (*
this)[0][0];
618 const FieldVector<K,1>&
diagonal()
const
637 template <
class OtherScalar>
639 const DiagonalMatrix<OtherScalar, 1>& matrixB)
641 return DiagonalMatrix<typename PromotionTraits<K,OtherScalar>::PromotedType, 1>{matrixA.diagonal(0)*matrixB.diagonal(0)};
648 template<
class DiagonalMatrixType>
651 typedef typename DiagonalMatrixType::reference reference;
652 typedef typename DiagonalMatrixType::const_reference const_reference;
653 typedef typename DiagonalMatrixType::field_type K;
655 typedef std::size_t size_type;
668 mat_(const_cast<DiagonalMatrixType*>(mat))
678 row_ =
row_type(&(mat_->diagonal(i)), i);
684 return mat_==other.mat_;
689 mutable DiagonalMatrixType* mat_;
690 mutable row_type row_;
696 template<
class K,
int n >
699 template<
class DiagonalMatrixType>
826 return const_cast<K*
>(
p_);
839 template<
class K,
int n >
842 template<
class DiagonalMatrixType>
948 template<
class K,
int n>
954 template<
class K,
int n>
960 template<
class K,
int n>
966 template<
class K,
int n>
996 template<
class CW,
class T,
class R>
999 typedef typename std::remove_const<CW>::type NonConstCW;
1011 containerWrapper_(),
1016 containerWrapper_(containerWrapper),
1020 template<
class OtherContainerWrapperIteratorType>
1022 containerWrapper_(other.containerWrapper_),
1023 position_(other.position_)
1027 containerWrapper_(other.containerWrapper_),
1028 position_(other.position_)
1032 containerWrapper_(other.containerWrapper_),
1033 position_(other.position_)
1036 template<
class OtherContainerWrapperIteratorType>
1039 containerWrapper_ = other.containerWrapper_;
1040 position_ = other.position_;
1048 return containerWrapper_.pointer(position_);
1054 return position_ == other.position_ && containerWrapper_.identical(other.containerWrapper_);
1059 return position_ == other.position_ && containerWrapper_.identical(other.containerWrapper_);
1064 return *containerWrapper_.pointer(position_);
1081 return *containerWrapper_.pointer(position_+i);
1086 position_=position_+n;
1089 template<
class OtherContainerWrapperIteratorType>
1090 std::ptrdiff_t
distanceTo(OtherContainerWrapperIteratorType& other)
const
1092 assert(containerWrapper_.identical(other));
1093 return other.position_ - position_;
1098 return containerWrapper_.realIndex(position_);
1102 NonConstCW containerWrapper_;
1106 template <
class DenseMatrix,
class field,
int N>
1112 denseMatrix = field(0);
1113 for (
int i = 0; i < N; ++i)
1114 denseMatrix[i][i] = rhs.
diagonal()[i];
Implements a vector constructed from a given type representing a field and a compile-time given size.
Traits for type conversions and type information.
Implements a matrix constructed from a given type representing a field and a compile-time given numbe...
A few common exception classes.
Implements a matrix constructed from a given type representing a field and compile-time given number ...
Macro for wrapping boundary checks.
Implements a generic iterator class for writing stl conformant iterators.
#define DUNE_ASSERT_BOUNDS(cond)
If DUNE_CHECK_BOUNDS is defined: check if condition cond holds; otherwise, do nothing.
Definition boundschecking.hh:30
ConstIterator beforeBegin() const
Definition diagonalmatrix.hh:208
DiagonalMatrix< K, n > transposed() const
Return transposed of the matrix as DiagonalMatrix.
Definition diagonalmatrix.hh:136
void mmhv(const X &x, Y &y) const
y -= A^H x
Definition diagonalmatrix.hh:357
DiagonalMatrix & operator*=(const K &k)
vector space multiplication with scalar
Definition diagonalmatrix.hh:246
FieldTraits< K >::field_type field_type
Definition diagonalmatrix.hh:563
std::size_t size_type
The type used for the index access and size operations.
Definition diagonalmatrix.hh:67
size_type dim() const
dimension of the vector space
Definition diagonalmatrix.hh:800
ConstIterator ConstRowIterator
rename the iterators for easier access
Definition diagonalmatrix.hh:183
row_type row_reference
Definition diagonalmatrix.hh:75
K & operator[](size_type i)
random access
Definition diagonalmatrix.hh:879
ContainerWrapperIterator & operator=(OtherContainerWrapperIteratorType &other)
Definition diagonalmatrix.hh:1037
static constexpr int rows
The number of rows.
Definition diagonalmatrix.hh:81
static constexpr size_type M()
number of blocks in column direction
Definition diagonalmatrix.hh:488
FieldVector< K, n > & diagonal()
Get reference to diagonal vector.
Definition diagonalmatrix.hh:549
void usmhv(const typename FieldTraits< Y >::field_type &alpha, const X &x, Y &y) const
y += alpha A^H x
Definition diagonalmatrix.hh:395
Iterator iterator
typedef for stl compliant access
Definition diagonalmatrix.hh:888
void increment()
Definition diagonalmatrix.hh:1067
static constexpr int blocklevel
The number of block levels we contain. This is the leaf, that is, 1.
Definition diagonalmatrix.hh:70
DiagonalRowVectorConst< K, n > type
Definition diagonalmatrix.hh:957
T * operator->() const
Definition diagonalmatrix.hh:1046
static constexpr int size
The size of this vector.
Definition diagonalmatrix.hh:724
K * pointer(size_type i) const
Definition diagonalmatrix.hh:824
K field_type
export the type representing the field
Definition diagonalmatrix.hh:712
DiagonalRowVector< K, n > type
Definition diagonalmatrix.hh:963
ConstIterator beforeEnd() const
Definition diagonalmatrix.hh:773
const_row_type::ConstIterator ConstColIterator
rename the iterators for easier access
Definition diagonalmatrix.hh:185
bool exists(size_type i, size_type j) const
return true when (i,j) is in pattern
Definition diagonalmatrix.hh:498
ContainerWrapperIterator< const WrapperType, const_reference, const_reference > ConstIterator
Iterator class for sequential access.
Definition diagonalmatrix.hh:179
const_row_type const_row_reference
Definition diagonalmatrix.hh:78
static constexpr size_type size()
Definition diagonalmatrix.hh:87
size_type row_
Definition diagonalmatrix.hh:836
ContainerWrapperIterator(CW containerWrapper, int position)
Definition diagonalmatrix.hh:1015
DiagonalRowVector(K *p, int col)
Constructor making vector with identical coordinates.
Definition diagonalmatrix.hh:865
K & diagonal(size_type i)
Get reference to diagonal entry.
Definition diagonalmatrix.hh:537
void solve(V &x, const V &b) const
Solve system A x = b.
Definition diagonalmatrix.hh:438
static constexpr size_type N()
number of blocks in row direction
Definition diagonalmatrix.hh:482
Iterator beforeBegin()
Definition diagonalmatrix.hh:911
ContainerWrapperIterator(OtherContainerWrapperIteratorType &other)
Definition diagonalmatrix.hh:1021
const_reference operator[](size_type i) const
Return const_reference object as row replacement.
Definition diagonalmatrix.hh:525
Iterator iterator
typedef for stl compliant access
Definition diagonalmatrix.hh:145
ConstIterator begin() const
begin ConstIterator
Definition diagonalmatrix.hh:760
ConstIterator const_iterator
typedef for stl compliant access
Definition diagonalmatrix.hh:919
bool identical(const DiagonalRowVectorConst< K, n > &other) const
Definition diagonalmatrix.hh:749
DiagonalMatrix & operator-=(const DiagonalMatrix &y)
vector space subtraction
Definition diagonalmatrix.hh:225
DiagonalRowVectorConst(K *p, int col)
Constructor making vector with identical coordinates.
Definition diagonalmatrix.hh:733
void mmtv(const X &x, Y &y) const
y -= A^T x
Definition diagonalmatrix.hh:345
static constexpr int blocklevel
The number of block levels we contain.
Definition diagonalmatrix.hh:721
row_type * pointer(int i) const
Definition diagonalmatrix.hh:676
DiagonalMatrix(const K &k)
Constructor initializing the whole matrix with a scalar.
Definition diagonalmatrix.hh:98
ContainerWrapperIterator< DiagonalRowVector< K, n >, K, K & > Iterator
Iterator class for sequential access.
Definition diagonalmatrix.hh:886
R elementAt(int i) const
Definition diagonalmatrix.hh:1079
std::ptrdiff_t distanceTo(OtherContainerWrapperIteratorType &other) const
Definition diagonalmatrix.hh:1090
Iterator beforeEnd()
Definition diagonalmatrix.hh:904
void umtv(const X &x, Y &y) const
y += A^T x
Definition diagonalmatrix.hh:309
ConstIterator const_iterator
typedef for stl compliant access
Definition diagonalmatrix.hh:757
double infinity_norm_real() const
simplified infinity norm (uses Manhattan norm for complex values)
Definition diagonalmatrix.hh:427
DiagonalMatrixWrapper(const DiagonalMatrixType *mat)
Definition diagonalmatrix.hh:667
void umv(const X &x, Y &y) const
y += A x
Definition diagonalmatrix.hh:297
ContainerWrapperIterator< const WrapperType, reference, reference > Iterator
Iterator class for sequential access.
Definition diagonalmatrix.hh:143
DiagonalRowVector< K, n > type
Definition diagonalmatrix.hh:969
void mv(const X &x, Y &y) const
y = A x
Definition diagonalmatrix.hh:278
double frobenius_norm() const
frobenius norm: sqrt(sum over squared values of entries)
Definition diagonalmatrix.hh:409
ConstIterator end() const
end iterator
Definition diagonalmatrix.hh:194
static constexpr int cols
The number of columns.
Definition diagonalmatrix.hh:83
static constexpr int dimension
Definition diagonalmatrix.hh:705
K * p_
Definition diagonalmatrix.hh:835
size_type rowIndex() const
index of this row in surrounding matrix
Definition diagonalmatrix.hh:806
R dereference() const
Definition diagonalmatrix.hh:1062
DiagonalRowVector & operator=(const K &k)
Assignment operator for scalar.
Definition diagonalmatrix.hh:870
bool operator!=(const DiagonalMatrix &other) const
incomparison operator
Definition diagonalmatrix.hh:268
value_type field_type
Definition diagonalmatrix.hh:61
void advance(int n)
Definition diagonalmatrix.hh:1084
ConstIterator begin() const
begin iterator
Definition diagonalmatrix.hh:188
Iterator begin()
begin iterator
Definition diagonalmatrix.hh:891
void mmv(const X &x, Y &y) const
y -= A x
Definition diagonalmatrix.hh:333
DiagonalMatrix & operator/=(const K &k)
vector space division by scalar
Definition diagonalmatrix.hh:253
ConstIterator const_iterator
typedef for stl compliant access
Definition diagonalmatrix.hh:181
const_row_type const_reference
Definition diagonalmatrix.hh:77
bool identical(const DiagonalMatrix< K, n > &other) const
Check if matrix is the same object as the other matrix.
Definition diagonalmatrix.hh:130
friend auto operator*(const DiagonalMatrix &matrixA, const DiagonalMatrix< OtherScalar, n > &matrixB)
Matrix-matrix multiplication.
Definition diagonalmatrix.hh:468
size_type realIndex(int i) const
Definition diagonalmatrix.hh:671
Iterator end()
end iterator
Definition diagonalmatrix.hh:158
const K & operator[](size_type i) const
same for read only access
Definition diagonalmatrix.hh:741
DiagonalRowVector()
Constructor making uninitialized vector.
Definition diagonalmatrix.hh:861
row_type reference
Definition diagonalmatrix.hh:74
DiagonalRowVectorConst * operator&()
Definition diagonalmatrix.hh:829
bool operator==(const DiagonalRowVectorConst &y) const
Binary vector comparison.
Definition diagonalmatrix.hh:786
double frobenius_norm2() const
square of frobenius norm, need for block recursion
Definition diagonalmatrix.hh:415
constexpr DiagonalMatrix()=default
Default constructor.
void mtv(const X &x, Y &y) const
y = A^T x
Definition diagonalmatrix.hh:290
ContainerWrapperIterator< DiagonalRowVectorConst< K, n >, const K, const K & > ConstIterator
ConstIterator class for sequential access.
Definition diagonalmatrix.hh:917
bool identical(const DiagonalMatrixWrapper &other) const
Definition diagonalmatrix.hh:682
void usmtv(const typename FieldTraits< Y >::field_type &alpha, const X &x, Y &y) const
y += alpha A^T x
Definition diagonalmatrix.hh:382
void decrement()
Definition diagonalmatrix.hh:1073
std::ptrdiff_t index() const
Definition diagonalmatrix.hh:1096
const FieldVector< K, n > & diagonal() const
Get const reference to diagonal vector.
Definition diagonalmatrix.hh:543
void invert()
Compute inverse.
Definition diagonalmatrix.hh:445
DiagonalRowVector * operator&()
Definition diagonalmatrix.hh:935
const K & diagonal(size_type i) const
Get const reference to diagonal entry.
Definition diagonalmatrix.hh:531
size_type N() const
number of blocks in the vector (are of size 1 here)
Definition diagonalmatrix.hh:794
DiagonalMatrix(std::initializer_list< K > const &l)
Construct diagonal matrix from an initializer list.
Definition diagonalmatrix.hh:115
row_type::Iterator ColIterator
rename the iterators for easier access
Definition diagonalmatrix.hh:149
K field_type
export the type representing the field
Definition diagonalmatrix.hh:852
reference operator[](size_type i)
Return reference object as row replacement.
Definition diagonalmatrix.hh:519
DiagonalRowVectorConst< K, n > type
Definition diagonalmatrix.hh:951
Iterator end()
end iterator
Definition diagonalmatrix.hh:897
DiagonalMatrix(const FieldVector< K, n > &diag)
Constructor initializing the diagonal with a vector.
Definition diagonalmatrix.hh:103
std::size_t size_type
The type used for the index access and size operation.
Definition diagonalmatrix.hh:858
void usmv(const typename FieldTraits< Y >::field_type &alpha, const X &x, Y &y) const
y += alpha A x
Definition diagonalmatrix.hh:369
double infinity_norm() const
infinity norm (row sum norm, how to generalize for blocks?)
Definition diagonalmatrix.hh:421
DiagonalMatrix & operator+=(const DiagonalMatrix &y)
vector space addition
Definition diagonalmatrix.hh:218
K block_type
export the type representing the components
Definition diagonalmatrix.hh:64
void umhv(const X &x, Y &y) const
y += A^H x
Definition diagonalmatrix.hh:321
Iterator beforeBegin()
Definition diagonalmatrix.hh:172
static void apply(DenseMatrix &denseMatrix, DiagonalMatrix< field, N > const &rhs)
Definition diagonalmatrix.hh:1108
ContainerWrapperIterator(const MyType &other)
Definition diagonalmatrix.hh:1026
ConstIterator end() const
end ConstIterator
Definition diagonalmatrix.hh:766
std::size_t size_type
The type used for the index access and size operation.
Definition diagonalmatrix.hh:718
DiagonalMatrix & operator=(const K &k)
Assignment from a scalar.
Definition diagonalmatrix.hh:123
DiagonalRowVectorConst()
Constructor making uninitialized vector.
Definition diagonalmatrix.hh:727
Iterator beforeEnd()
Definition diagonalmatrix.hh:165
size_type realIndex(int i) const
Definition diagonalmatrix.hh:819
friend std::ostream & operator<<(std::ostream &s, const DiagonalMatrix< K, n > &a)
Sends the matrix to an output stream.
Definition diagonalmatrix.hh:508
K value_type
export the type representing the field
Definition diagonalmatrix.hh:60
K block_type
export the type representing the components
Definition diagonalmatrix.hh:715
K determinant() const
calculates the determinant of this matrix
Definition diagonalmatrix.hh:453
FieldTraits< K >::real_type real_type
Definition diagonalmatrix.hh:564
ConstIterator beforeBegin() const
Definition diagonalmatrix.hh:780
DiagonalMatrixWrapper()
Definition diagonalmatrix.hh:663
ContainerWrapperIterator< DiagonalRowVectorConst< K, n >, const K, const K & > ConstIterator
ConstIterator class for sequential access.
Definition diagonalmatrix.hh:755
Iterator begin()
begin iterator
Definition diagonalmatrix.hh:152
bool operator==(const DiagonalMatrix &other) const
comparison operator
Definition diagonalmatrix.hh:262
DiagonalRowVectorConst< K, n > const_row_type
Definition diagonalmatrix.hh:76
DiagonalRowVector< K, n > row_type
Each row is implemented by a field vector.
Definition diagonalmatrix.hh:73
bool equals(const MyType &other) const
Definition diagonalmatrix.hh:1052
Iterator RowIterator
rename the iterators for easier access
Definition diagonalmatrix.hh:147
K block_type
export the type representing the components
Definition diagonalmatrix.hh:855
ConstIterator beforeEnd() const
Definition diagonalmatrix.hh:201
const K & diagonal() const
the diagonal value
Definition diagonalmatrix.hh:812
#define DUNE_THROW(E, m)
Definition exceptions.hh:218
Dune namespace.
Definition alignedallocator.hh:13
K conjugateComplex(const K &x)
compute conjugate complex of x
Definition math.hh:164
A dense n x m matrix.
Definition densematrix.hh:140
constexpr size_type M() const
number of columns
Definition densematrix.hh:703
constexpr size_type N() const
number of rows
Definition densematrix.hh:697
A dense n x m matrix.
Definition fmatrix.hh:117
vector space out of a tensor product of fields.
Definition fvector.hh:95
you have to specialize this structure for any type that should be assignable to a DenseMatrix
Definition densematrix.hh:59
Error thrown if operations of a FieldMatrix fail.
Definition densematrix.hh:126
FieldTraits< value_type >::real_type two_norm2() const
square of two norm (sum over squared values of entries), need for block recursion
Definition densevector.hh:650
FieldTraits< value_type >::real_type two_norm() const
two norm sqrt(sum over squared values of entries)
Definition densevector.hh:641
Iterator begin()
begin iterator
Definition densevector.hh:347
FieldTraits< vt >::real_type infinity_norm() const
infinity norm (maximum of absolute values of entries)
Definition densevector.hh:661
FieldTraits< vt >::real_type infinity_norm_real() const
simplified infinity norm (uses Manhattan norm for complex values)
Definition densevector.hh:677
Definition diagonalmatrix.hh:698
Definition diagonalmatrix.hh:841
Definition diagonalmatrix.hh:650
Iterator class for sparse vector-like containers.
Definition diagonalmatrix.hh:998
ContainerWrapperIterator(const MyConstType &other)
Definition diagonalmatrix.hh:1031
bool equals(const MyConstType &other) const
Definition diagonalmatrix.hh:1057
A diagonal matrix of static size.
Definition diagonalmatrix.hh:53
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:274