3 #ifndef DUNE_DIAGONAL_MATRIX_HH
4 #define DUNE_DIAGONAL_MATRIX_HH
15 #include <initializer_list>
30 template<
class K,
int n >
class DiagonalRowVectorConst;
31 template<
class K,
int n >
class DiagonalRowVector;
32 template<
class DiagonalMatrixType >
class DiagonalMatrixWrapper;
33 template<
class C,
class T,
class R>
class ContainerWrapperIterator;
49 template<
class K,
int n>
121 std::copy_n(l.begin(),
std::min(
static_cast<std::size_t
>(
rows),
136 return (
this==&other);
275 template<
class X,
class Y>
276 void mv (
const X& x, Y& y)
const
278 #ifdef DUNE_FMatrix_WITH_CHECKING
283 y[i] = diag_[i] * x[i];
287 template<
class X,
class Y>
288 void mtv (
const X& x, Y& y)
const
294 template<
class X,
class Y>
295 void umv (
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 umtv (
const X& x, Y& y)
const
309 #ifdef DUNE_FMatrix_WITH_CHECKING
314 y[i] += diag_[i] * x[i];
318 template<
class X,
class Y>
319 void umhv (
const X& x, Y& y)
const
321 #ifdef DUNE_FMatrix_WITH_CHECKING
330 template<
class X,
class Y>
331 void mmv (
const X& x, Y& y)
const
333 #ifdef DUNE_FMatrix_WITH_CHECKING
338 y[i] -= diag_[i] * x[i];
342 template<
class X,
class Y>
343 void mmtv (
const X& x, Y& y)
const
345 #ifdef DUNE_FMatrix_WITH_CHECKING
350 y[i] -= diag_[i] * x[i];
354 template<
class X,
class Y>
355 void mmhv (
const X& x, Y& y)
const
357 #ifdef DUNE_FMatrix_WITH_CHECKING
366 template<
class X,
class Y>
368 const X& x, Y& y)
const
370 #ifdef DUNE_FMatrix_WITH_CHECKING
375 y[i] += alpha * diag_[i] * x[i];
379 template<
class X,
class Y>
381 const X& x, Y& y)
const
383 #ifdef DUNE_FMatrix_WITH_CHECKING
388 y[i] += alpha * diag_[i] * x[i];
392 template<
class X,
class Y>
394 const X& x, Y& y)
const
396 #ifdef DUNE_FMatrix_WITH_CHECKING
438 for (
int i=0; i<n; i++)
439 x[i] = b[i]/diag_[i];
446 for (
int i=0; i<n; i++)
447 diag_[i] = real_type(1.0)/diag_[i];
454 for (
int i=1; i<n; i++)
494 s << ((i==j) ? a.diag_[i] : 0) <<
" ";
503 return reference(
const_cast<K*
>(&diag_[i]), i);
542 template<
class K,
int n >
559 typedef typename Base::size_type
size_type;
568 typedef typename Base::row_type
row_type;
590 (*this)[0][0] = scalar;
596 return (*
this)[0][0];
602 return (*
this)[0][0];
606 const FieldVector<K,1>&
diagonal()
const
621 template<
class DiagonalMatrixType>
624 typedef typename DiagonalMatrixType::reference reference;
625 typedef typename DiagonalMatrixType::const_reference const_reference;
626 typedef typename DiagonalMatrixType::field_type K;
628 typedef std::size_t size_type;
641 mat_(const_cast<DiagonalMatrixType*>(mat))
651 row_ =
row_type(&(mat_->diagonal(i)), i);
657 return mat_==other.mat_;
662 mutable DiagonalMatrixType* mat_;
663 mutable row_type row_;
669 template<
class K,
int n >
672 template<
class DiagonalMatrixType>
805 return const_cast<K*
>(
p_);
818 template<
class K,
int n >
821 template<
class DiagonalMatrixType>
927 template<
class K,
int n>
933 template<
class K,
int n>
939 template<
class K,
int n>
945 template<
class K,
int n>
975 template<
class CW,
class T,
class R>
978 typedef typename std::remove_const<CW>::type NonConstCW;
995 containerWrapper_(containerWrapper),
999 template<
class OtherContainerWrapperIteratorType>
1001 containerWrapper_(other.containerWrapper_),
1002 position_(other.position_)
1006 containerWrapper_(other.containerWrapper_),
1007 position_(other.position_)
1011 containerWrapper_(other.containerWrapper_),
1012 position_(other.position_)
1015 template<
class OtherContainerWrapperIteratorType>
1018 containerWrapper_ = other.containerWrapper_;
1019 position_ = other.position_;
1027 return containerWrapper_.pointer(position_);
1033 return position_ == other.position_ && containerWrapper_.identical(other.containerWrapper_);
1038 return position_ == other.position_ && containerWrapper_.identical(other.containerWrapper_);
1043 return *containerWrapper_.pointer(position_);
1060 return *containerWrapper_.pointer(position_+i);
1065 position_=position_+n;
1068 template<
class OtherContainerWrapperIteratorType>
1069 std::ptrdiff_t
distanceTo(OtherContainerWrapperIteratorType& other)
const
1071 assert(containerWrapper_.identical(other));
1072 return other.position_ - position_;
1077 return containerWrapper_.realIndex(position_);
1081 NonConstCW containerWrapper_;
1085 template <
class DenseMatrix,
class field,
int N>
1091 denseMatrix = field(0);
1092 for (
int i = 0; i < N; ++i)
1093 denseMatrix[i][i] = rhs.
diagonal()[i];
Macro for wrapping boundary checks.
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 ...
Implements a vector constructed from a given type representing a field and a compile-time given size.
Implements a generic iterator class for writing stl conformant iterators.
Traits for type conversions and type information.
#define DUNE_ASSERT_BOUNDS(cond)
If DUNE_CHECK_BOUNDS is defined: check if condition cond holds; otherwise, do nothing.
Definition: boundschecking.hh:28
ConstIterator beforeBegin() const
Definition: diagonalmatrix.hh:206
void mmhv(const X &x, Y &y) const
y -= A^H x
Definition: diagonalmatrix.hh:355
FieldTraits< K >::field_type field_type
Definition: diagonalmatrix.hh:545
std::size_t size_type
The type used for the index access and size operations.
Definition: diagonalmatrix.hh:65
size_type dim() const
dimension of the vector space
Definition: diagonalmatrix.hh:779
ConstIterator ConstRowIterator
rename the iterators for easier access
Definition: diagonalmatrix.hh:181
row_type row_reference
Definition: diagonalmatrix.hh:76
DiagonalMatrix & operator=(const K &k)
Assignment from a scalar.
Definition: diagonalmatrix.hh:127
static constexpr size_type M()
number of blocks in column direction
Definition: diagonalmatrix.hh:470
void usmhv(const typename FieldTraits< Y >::field_type &alpha, const X &x, Y &y) const
y += alpha A^H x
Definition: diagonalmatrix.hh:393
Iterator iterator
typedef for stl compliant access
Definition: diagonalmatrix.hh:867
void increment()
Definition: diagonalmatrix.hh:1046
DiagonalRowVectorConst< K, n > type
Definition: diagonalmatrix.hh:936
K field_type
export the type representing the field
Definition: diagonalmatrix.hh:685
DiagonalRowVector< K, n > type
Definition: diagonalmatrix.hh:942
ConstIterator beforeEnd() const
Definition: diagonalmatrix.hh:752
const_row_type::ConstIterator ConstColIterator
rename the iterators for easier access
Definition: diagonalmatrix.hh:183
bool exists(size_type i, size_type j) const
return true when (i,j) is in pattern
Definition: diagonalmatrix.hh:480
ContainerWrapperIterator< const WrapperType, const_reference, const_reference > ConstIterator
Iterator class for sequential access.
Definition: diagonalmatrix.hh:177
const_row_type const_row_reference
Definition: diagonalmatrix.hh:79
static constexpr size_type size()
Definition: diagonalmatrix.hh:91
size_type row_
Definition: diagonalmatrix.hh:815
ContainerWrapperIterator(CW containerWrapper, int position)
Definition: diagonalmatrix.hh:994
DiagonalRowVector(K *p, int col)
Constructor making vector with identical coordinates.
Definition: diagonalmatrix.hh:844
void solve(V &x, const V &b) const
Solve system A x = b.
Definition: diagonalmatrix.hh:436
static constexpr size_type N()
number of blocks in row direction
Definition: diagonalmatrix.hh:464
Iterator beforeBegin()
Definition: diagonalmatrix.hh:890
ContainerWrapperIterator(OtherContainerWrapperIteratorType &other)
Definition: diagonalmatrix.hh:1000
K & operator[]([[maybe_unused]] size_type i)
random access
Definition: diagonalmatrix.hh:858
const_reference operator[](size_type i) const
Return const_reference object as row replacement.
Definition: diagonalmatrix.hh:507
Iterator iterator
typedef for stl compliant access
Definition: diagonalmatrix.hh:143
ConstIterator begin() const
begin ConstIterator
Definition: diagonalmatrix.hh:739
K & diagonal(size_type i)
Get reference to diagonal entry.
Definition: diagonalmatrix.hh:519
ConstIterator const_iterator
typedef for stl compliant access
Definition: diagonalmatrix.hh:898
bool identical(const DiagonalRowVectorConst< K, n > &other) const
Definition: diagonalmatrix.hh:728
DiagonalRowVectorConst(K *p, int col)
Constructor making vector with identical coordinates.
Definition: diagonalmatrix.hh:712
void mmtv(const X &x, Y &y) const
y -= A^T x
Definition: diagonalmatrix.hh:343
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:865
R elementAt(int i) const
Definition: diagonalmatrix.hh:1058
std::ptrdiff_t distanceTo(OtherContainerWrapperIteratorType &other) const
Definition: diagonalmatrix.hh:1069
Iterator beforeEnd()
Definition: diagonalmatrix.hh:883
void umtv(const X &x, Y &y) const
y += A^T x
Definition: diagonalmatrix.hh:307
ConstIterator const_iterator
typedef for stl compliant access
Definition: diagonalmatrix.hh:736
double infinity_norm_real() const
simplified infinity norm (uses Manhattan norm for complex values)
Definition: diagonalmatrix.hh:425
DiagonalMatrixWrapper(const DiagonalMatrixType *mat)
Definition: diagonalmatrix.hh:640
void umv(const X &x, Y &y) const
y += A x
Definition: diagonalmatrix.hh:295
ContainerWrapperIterator< const WrapperType, reference, reference > Iterator
Iterator class for sequential access.
Definition: diagonalmatrix.hh:141
ContainerWrapperIterator & operator=(OtherContainerWrapperIteratorType &other)
Definition: diagonalmatrix.hh:1016
DiagonalRowVector< K, n > type
Definition: diagonalmatrix.hh:948
void mv(const X &x, Y &y) const
y = A x
Definition: diagonalmatrix.hh:276
double frobenius_norm() const
frobenius norm: sqrt(sum over squared values of entries)
Definition: diagonalmatrix.hh:407
ConstIterator end() const
end iterator
Definition: diagonalmatrix.hh:192
const K & operator[]([[maybe_unused]] size_type i) const
same for read only access
Definition: diagonalmatrix.hh:720
K * pointer([[maybe_unused]] size_type i) const
Definition: diagonalmatrix.hh:803
size_type realIndex([[maybe_unused]] int i) const
Definition: diagonalmatrix.hh:798
K * p_
Definition: diagonalmatrix.hh:814
size_type rowIndex() const
index of this row in surrounding matrix
Definition: diagonalmatrix.hh:785
T * operator->() const
Definition: diagonalmatrix.hh:1025
R dereference() const
Definition: diagonalmatrix.hh:1041
bool operator!=(const DiagonalMatrix &other) const
incomparison operator
Definition: diagonalmatrix.hh:266
value_type field_type
Definition: diagonalmatrix.hh:59
void advance(int n)
Definition: diagonalmatrix.hh:1063
ConstIterator begin() const
begin iterator
Definition: diagonalmatrix.hh:186
Iterator begin()
begin iterator
Definition: diagonalmatrix.hh:870
void mmv(const X &x, Y &y) const
y -= A x
Definition: diagonalmatrix.hh:331
ConstIterator const_iterator
typedef for stl compliant access
Definition: diagonalmatrix.hh:179
DiagonalRowVector & operator=(const K &k)
Assignment operator for scalar.
Definition: diagonalmatrix.hh:849
const_row_type const_reference
Definition: diagonalmatrix.hh:78
bool identical(const DiagonalMatrix< K, n > &other) const
Check if matrix is the same object as the other matrix.
Definition: diagonalmatrix.hh:134
size_type realIndex(int i) const
Definition: diagonalmatrix.hh:644
Iterator end()
end iterator
Definition: diagonalmatrix.hh:156
const K & diagonal(size_type i) const
Get const reference to diagonal entry.
Definition: diagonalmatrix.hh:513
DiagonalRowVector()
Constructor making uninitialized vector.
Definition: diagonalmatrix.hh:840
row_type reference
Definition: diagonalmatrix.hh:75
bool operator==(const DiagonalRowVectorConst &y) const
Binary vector comparison.
Definition: diagonalmatrix.hh:765
double frobenius_norm2() const
square of frobenius norm, need for block recursion
Definition: diagonalmatrix.hh:413
constexpr DiagonalMatrix()=default
Default constructor.
void mtv(const X &x, Y &y) const
y = A^T x
Definition: diagonalmatrix.hh:288
ContainerWrapperIterator< DiagonalRowVectorConst< K, n >, const K, const K & > ConstIterator
ConstIterator class for sequential access.
Definition: diagonalmatrix.hh:896
friend std::ostream & operator<<(std::ostream &s, const DiagonalMatrix< K, n > &a)
Sends the matrix to an output stream.
Definition: diagonalmatrix.hh:490
bool identical(const DiagonalMatrixWrapper &other) const
Definition: diagonalmatrix.hh:655
row_type * pointer(int i) const
Definition: diagonalmatrix.hh:649
void usmtv(const typename FieldTraits< Y >::field_type &alpha, const X &x, Y &y) const
y += alpha A^T x
Definition: diagonalmatrix.hh:380
void decrement()
Definition: diagonalmatrix.hh:1052
std::ptrdiff_t index() const
Definition: diagonalmatrix.hh:1075
void invert()
Compute inverse.
Definition: diagonalmatrix.hh:443
size_type N() const
number of blocks in the vector (are of size 1 here)
Definition: diagonalmatrix.hh:773
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:147
K field_type
export the type representing the field
Definition: diagonalmatrix.hh:831
const FieldVector< K, n > & diagonal() const
Get const reference to diagonal vector.
Definition: diagonalmatrix.hh:525
reference operator[](size_type i)
Return reference object as row replacement.
Definition: diagonalmatrix.hh:501
const K & diagonal() const
the diagonal value
Definition: diagonalmatrix.hh:791
DiagonalRowVectorConst< K, n > type
Definition: diagonalmatrix.hh:930
DiagonalMatrix & operator-=(const DiagonalMatrix &y)
vector space subtraction
Definition: diagonalmatrix.hh:223
DiagonalMatrix & operator/=(const K &k)
vector space division by scalar
Definition: diagonalmatrix.hh:251
Iterator end()
end iterator
Definition: diagonalmatrix.hh:876
DiagonalRowVectorConst * operator&()
Definition: diagonalmatrix.hh:808
DiagonalMatrix(const FieldVector< K, n > &diag)
Constructor initializing the diagonal with a vector.
Definition: diagonalmatrix.hh:107
std::size_t size_type
The type used for the index access and size operation.
Definition: diagonalmatrix.hh:837
void usmv(const typename FieldTraits< Y >::field_type &alpha, const X &x, Y &y) const
y += alpha A x
Definition: diagonalmatrix.hh:367
double infinity_norm() const
infinity norm (row sum norm, how to generalize for blocks?)
Definition: diagonalmatrix.hh:419
K block_type
export the type representing the components
Definition: diagonalmatrix.hh:62
void umhv(const X &x, Y &y) const
y += A^H x
Definition: diagonalmatrix.hh:319
Iterator beforeBegin()
Definition: diagonalmatrix.hh:170
static void apply(DenseMatrix &denseMatrix, DiagonalMatrix< field, N > const &rhs)
Definition: diagonalmatrix.hh:1087
ContainerWrapperIterator(const MyType &other)
Definition: diagonalmatrix.hh:1005
ConstIterator end() const
end ConstIterator
Definition: diagonalmatrix.hh:745
std::size_t size_type
The type used for the index access and size operation.
Definition: diagonalmatrix.hh:691
DiagonalRowVectorConst()
Constructor making uninitialized vector.
Definition: diagonalmatrix.hh:706
DiagonalRowVector * operator&()
Definition: diagonalmatrix.hh:914
Iterator beforeEnd()
Definition: diagonalmatrix.hh:163
DiagonalMatrix & operator+=(const DiagonalMatrix &y)
vector space addition
Definition: diagonalmatrix.hh:216
K value_type
export the type representing the field
Definition: diagonalmatrix.hh:58
K block_type
export the type representing the components
Definition: diagonalmatrix.hh:688
K determinant() const
calculates the determinant of this matrix
Definition: diagonalmatrix.hh:451
FieldVector< K, n > & diagonal()
Get reference to diagonal vector.
Definition: diagonalmatrix.hh:531
FieldTraits< K >::real_type real_type
Definition: diagonalmatrix.hh:546
ConstIterator beforeBegin() const
Definition: diagonalmatrix.hh:759
DiagonalMatrixWrapper()
Definition: diagonalmatrix.hh:636
ContainerWrapperIterator< DiagonalRowVectorConst< K, n >, const K, const K & > ConstIterator
ConstIterator class for sequential access.
Definition: diagonalmatrix.hh:734
Iterator begin()
begin iterator
Definition: diagonalmatrix.hh:150
bool operator==(const DiagonalMatrix &other) const
comparison operator
Definition: diagonalmatrix.hh:260
DiagonalRowVectorConst< K, n > const_row_type
Definition: diagonalmatrix.hh:77
DiagonalRowVector< K, n > row_type
Each row is implemented by a field vector.
Definition: diagonalmatrix.hh:74
bool equals(const MyType &other) const
Definition: diagonalmatrix.hh:1031
Iterator RowIterator
rename the iterators for easier access
Definition: diagonalmatrix.hh:145
K block_type
export the type representing the components
Definition: diagonalmatrix.hh:834
DiagonalMatrix & operator*=(const K &k)
vector space multiplication with scalar
Definition: diagonalmatrix.hh:244
ConstIterator beforeEnd() const
Definition: diagonalmatrix.hh:199
@ dimension
Definition: diagonalmatrix.hh:678
@ blocklevel
The number of block levels we contain.
Definition: diagonalmatrix.hh:696
@ blocklevel
The number of block levels we contain. This is 1.
Definition: diagonalmatrix.hh:70
@ cols
The number of columns.
Definition: diagonalmatrix.hh:86
@ rows
The number of rows.
Definition: diagonalmatrix.hh:84
@ size
The size of this vector.
Definition: diagonalmatrix.hh:702
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
Dune namespace.
Definition: alignedallocator.hh:11
K conjugateComplex(const K &x)
compute conjugate complex of x
Definition: math.hh:161
auto min(const AlignedNumber< T, align > &a, const AlignedNumber< T, align > &b)
Definition: debugalign.hh:434
A dense n x m matrix.
Definition: densematrix.hh:165
constexpr size_type M() const
number of columns
Definition: densematrix.hh:731
constexpr size_type N() const
number of rows
Definition: densematrix.hh:725
A dense n x m matrix.
Definition: fmatrix.hh:69
you have to specialize this structure for any type that should be assignable to a DenseMatrix
Definition: densematrix.hh:84
Error thrown if operations of a FieldMatrix fail.
Definition: densematrix.hh:151
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:651
Iterator begin()
begin iterator
Definition: densevector.hh:348
FieldTraits< value_type >::real_type two_norm() const
two norm sqrt(sum over squared values of entries)
Definition: densevector.hh:642
FieldTraits< vt >::real_type infinity_norm_real() const
simplified infinity norm (uses Manhattan norm for complex values)
Definition: densevector.hh:678
FieldTraits< vt >::real_type infinity_norm() const
infinity norm (maximum of absolute values of entries)
Definition: densevector.hh:662
Definition: diagonalmatrix.hh:671
Definition: diagonalmatrix.hh:820
Definition: diagonalmatrix.hh:623
Iterator class for sparse vector-like containers.
Definition: diagonalmatrix.hh:977
ContainerWrapperIterator(const MyConstType &other)
Definition: diagonalmatrix.hh:1010
bool equals(const MyConstType &other) const
Definition: diagonalmatrix.hh:1036
A diagonal matrix of static size.
Definition: diagonalmatrix.hh:51
Definition: ftraits.hh:24
T field_type
export the type representing the field
Definition: ftraits.hh:26
T real_type
export the type representing the real type of the field
Definition: ftraits.hh:28
Get the 'const' version of a reference to a mutable object.
Definition: genericiterator.hh:85
get the 'mutable' version of a reference to a const object
Definition: genericiterator.hh:114
Facade class for stl conformant bidirectional iterators.
Definition: iteratorfacades.hh:272