Go to the documentation of this file.
3 #ifndef DUNE_DIAGONAL_MATRIX_HH
4 #define DUNE_DIAGONAL_MATRIX_HH
15 #include <initializer_list>
50 template<
class K,
int n>
122 std::copy_n(l.begin(), std::min(
static_cast<std::size_t
>(
rows),
137 return (
this==&other);
276 template<
class X,
class Y>
277 void mv (
const X& x, Y& y)
const
279 #ifdef DUNE_FMatrix_WITH_CHECKING
284 y[i] = diag_[i] * x[i];
288 template<
class X,
class Y>
289 void mtv (
const X& x, Y& y)
const
295 template<
class X,
class Y>
296 void umv (
const X& x, Y& y)
const
298 #ifdef DUNE_FMatrix_WITH_CHECKING
303 y[i] += diag_[i] * x[i];
307 template<
class X,
class Y>
308 void umtv (
const X& x, Y& y)
const
310 #ifdef DUNE_FMatrix_WITH_CHECKING
315 y[i] += diag_[i] * x[i];
319 template<
class X,
class Y>
320 void umhv (
const X& x, Y& y)
const
322 #ifdef DUNE_FMatrix_WITH_CHECKING
331 template<
class X,
class Y>
332 void mmv (
const X& x, Y& y)
const
334 #ifdef DUNE_FMatrix_WITH_CHECKING
339 y[i] -= diag_[i] * x[i];
343 template<
class X,
class Y>
344 void mmtv (
const X& x, Y& y)
const
346 #ifdef DUNE_FMatrix_WITH_CHECKING
351 y[i] -= diag_[i] * x[i];
355 template<
class X,
class Y>
356 void mmhv (
const X& x, Y& y)
const
358 #ifdef DUNE_FMatrix_WITH_CHECKING
367 template<
class X,
class Y>
369 const X& x, Y& y)
const
371 #ifdef DUNE_FMatrix_WITH_CHECKING
376 y[i] += alpha * diag_[i] * x[i];
380 template<
class X,
class Y>
382 const X& x, Y& y)
const
384 #ifdef DUNE_FMatrix_WITH_CHECKING
389 y[i] += alpha * diag_[i] * x[i];
393 template<
class X,
class Y>
395 const X& x, Y& y)
const
397 #ifdef DUNE_FMatrix_WITH_CHECKING
410 return diag_.two_norm();
416 return diag_.two_norm2();
422 return diag_.infinity_norm();
428 return diag_.infinity_norm_real();
439 for (
int i=0; i<n; i++)
440 x[i] = b[i]/diag_[i];
446 for (
int i=0; i<n; i++)
447 diag_[i] = 1/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 >
550 #ifndef DOXYGEN // hide specialization
559 typedef typename Base::size_type
size_type;
568 typedef typename Base::row_type
row_type;
591 (*this)[0][0] = scalar;
597 return (*
this)[0][0];
603 return (*
this)[0][0];
607 const FieldVector<K,1>&
diagonal()
const
622 template<
class DiagonalMatrixType>
623 class DiagonalMatrixWrapper
625 typedef typename DiagonalMatrixType::reference reference;
626 typedef typename DiagonalMatrixType::const_reference const_reference;
627 typedef typename DiagonalMatrixType::field_type K;
628 typedef DiagonalRowVector<K, DiagonalMatrixType::rows> row_type;
629 typedef std::size_t size_type;
630 typedef DiagonalMatrixWrapper< DiagonalMatrixType> MyType;
642 mat_(const_cast<DiagonalMatrixType*>(mat))
652 row_ =
row_type(&(mat_->diagonal(i)), i);
658 return mat_==other.mat_;
663 mutable DiagonalMatrixType* mat_;
664 mutable row_type row_;
670 template<
class K,
int n >
671 class DiagonalRowVectorConst
673 template<
class DiagonalMatrixType>
732 return ((
p_ == other.p_)and (
row_ == other.row_));
769 return ((
p_==y.p_)and (
row_==y.row_));
809 return const_cast<K*
>(
p_);
822 template<
class K,
int n >
825 template<
class DiagonalMatrixType>
932 template<
class K,
int n>
938 template<
class K,
int n>
944 template<
class K,
int n>
950 template<
class K,
int n>
980 template<
class CW,
class T,
class R>
983 typedef typename std::remove_const<CW>::type NonConstCW;
1000 containerWrapper_(containerWrapper),
1004 template<
class OtherContainerWrapperIteratorType>
1006 containerWrapper_(other.containerWrapper_),
1007 position_(other.position_)
1011 containerWrapper_(other.containerWrapper_),
1012 position_(other.position_)
1016 containerWrapper_(other.containerWrapper_),
1017 position_(other.position_)
1020 template<
class OtherContainerWrapperIteratorType>
1023 containerWrapper_ = other.containerWrapper_;
1024 position_ = other.position_;
1032 return containerWrapper_.pointer(position_);
1038 return position_ == other.position_ && containerWrapper_.identical(other.containerWrapper_);
1043 return position_ == other.position_ && containerWrapper_.identical(other.containerWrapper_);
1048 return *containerWrapper_.pointer(position_);
1065 return *containerWrapper_.pointer(position_+i);
1070 position_=position_+n;
1073 template<
class OtherContainerWrapperIteratorType>
1074 std::ptrdiff_t
distanceTo(OtherContainerWrapperIteratorType& other)
const
1076 assert(containerWrapper_.identical(other));
1077 return other.position_ - position_;
1082 return containerWrapper_.realIndex(position_);
1086 NonConstCW containerWrapper_;
1090 template <
class DenseMatrix,
class field,
int N>
1096 denseMatrix = field(0);
1097 for (
int i = 0; i < N; ++i)
1098 denseMatrix[i][i] = rhs.
diagonal()[i];
DiagonalRowVectorConst< K, n > type
Definition: diagonalmatrix.hh:941
DiagonalMatrix & operator-=(const DiagonalMatrix &y)
vector space subtraction
Definition: diagonalmatrix.hh:224
Iterator iterator
typedef for stl compliant access
Definition: diagonalmatrix.hh:872
bool equals(const MyConstType &other) const
Definition: diagonalmatrix.hh:1041
size_type N() const
number of blocks in row direction
Definition: diagonalmatrix.hh:464
Iterator beforeEnd()
Definition: diagonalmatrix.hh:164
A dense n x m matrix.
Definition: densematrix.hh:39
std::size_t size_type
The type used for the index access and size operation.
Definition: diagonalmatrix.hh:841
Iterator class for sparse vector-like containers.
Definition: diagonalmatrix.hh:34
std::size_t size_type
The type used for the index access and size operations.
Definition: diagonalmatrix.hh:66
ConstIterator beforeEnd() const
Definition: diagonalmatrix.hh:754
ConstIterator begin() const
begin iterator
Definition: diagonalmatrix.hh:187
void usmhv(const typename FieldTraits< Y >::field_type &alpha, const X &x, Y &y) const
y += alpha A^H x
Definition: diagonalmatrix.hh:394
#define DUNE_UNUSED_PARAMETER(parm)
A macro to mark intentionally unused function parameters with.
Definition: unused.hh:25
ConstIterator const_iterator
typedef for stl compliant access
Definition: diagonalmatrix.hh:738
size_type realIndex(int i) const
Definition: diagonalmatrix.hh:800
size_type row_
Definition: diagonalmatrix.hh:819
DiagonalRowVector & operator=(const K &k)
Assignment operator for scalar.
Definition: diagonalmatrix.hh:853
ConstIterator const_iterator
typedef for stl compliant access
Definition: diagonalmatrix.hh:903
ContainerWrapperIterator(CW containerWrapper, int position)
Definition: diagonalmatrix.hh:999
ContainerWrapperIterator(const MyType &other)
Definition: diagonalmatrix.hh:1010
DiagonalRowVector< K, n > type
Definition: diagonalmatrix.hh:947
K field_type
export the type representing the field
Definition: diagonalmatrix.hh:835
size_type dim() const
dimension of the vector space
Definition: diagonalmatrix.hh:781
const_row_type const_row_reference
Definition: diagonalmatrix.hh:80
A few common exception classes.
ContainerWrapperIterator< DiagonalRowVectorConst< K, n >, const K, const K & > ConstIterator
ConstIterator class for sequential access.
Definition: diagonalmatrix.hh:901
Dune namespace.
Definition: alignedallocator.hh:9
Iterator begin()
begin iterator
Definition: diagonalmatrix.hh:875
std::ptrdiff_t index() const
Definition: diagonalmatrix.hh:1080
Iterator iterator
typedef for stl compliant access
Definition: diagonalmatrix.hh:144
std::ptrdiff_t distanceTo(OtherContainerWrapperIteratorType &other) const
Definition: diagonalmatrix.hh:1074
Implements a matrix constructed from a given type representing a field and a compile-time given numbe...
const_row_type const_reference
Definition: diagonalmatrix.hh:79
Definition of the DUNE_UNUSED macro for the case that config.h is not available.
K block_type
export the type representing the components
Definition: diagonalmatrix.hh:689
const_reference operator[](size_type i) const
Return const_reference object as row replacement.
Definition: diagonalmatrix.hh:507
const K & diagonal() const
the diagonal value
Definition: diagonalmatrix.hh:793
Facade class for stl conformant bidirectional iterators.
Definition: iteratorfacades.hh:269
K block_type
export the type representing the components
Definition: diagonalmatrix.hh:63
Iterator beforeBegin()
Definition: diagonalmatrix.hh:895
Iterator end()
end iterator
Definition: diagonalmatrix.hh:157
Implements a generic iterator class for writing stl conformant iterators.
ContainerWrapperIterator< DiagonalRowVector< K, n >, K, K & > Iterator
Iterator class for sequential access.
Definition: diagonalmatrix.hh:870
bool equals(const MyType &other) const
Definition: diagonalmatrix.hh:1036
ConstIterator end() const
end iterator
Definition: diagonalmatrix.hh:193
@ dimension
Definition: diagonalmatrix.hh:679
bool identical(const DiagonalMatrixWrapper &other) const
Definition: diagonalmatrix.hh:656
size_type size() const
Definition: diagonalmatrix.hh:92
ConstIterator const_iterator
typedef for stl compliant access
Definition: diagonalmatrix.hh:180
K * p_
Definition: diagonalmatrix.hh:818
DiagonalRowVectorConst(K *p, int col)
Constructor making vector with identical coordinates.
Definition: diagonalmatrix.hh:713
size_type N() const
number of rows
Definition: densematrix.hh:686
K * pointer(size_type i) const
Definition: diagonalmatrix.hh:806
FieldTraits< K >::field_type field_type
Definition: diagonalmatrix.hh:545
reference operator[](size_type i)
Return reference object as row replacement.
Definition: diagonalmatrix.hh:501
Implements a vector constructed from a given type representing a field and a compile-time given size.
Definition: diagonalmatrix.hh:33
void usmtv(const typename FieldTraits< Y >::field_type &alpha, const X &x, Y &y) const
y += alpha A^T x
Definition: diagonalmatrix.hh:381
DiagonalRowVector * operator&()
Definition: diagonalmatrix.hh:919
K block_type
export the type representing the components
Definition: diagonalmatrix.hh:838
void mmtv(const X &x, Y &y) const
y -= A^T x
Definition: diagonalmatrix.hh:344
row_type::Iterator ColIterator
rename the iterators for easier access
Definition: diagonalmatrix.hh:148
DiagonalRowVectorConst()
Constructor making uninitialized vector.
Definition: diagonalmatrix.hh:707
ContainerWrapperIterator< const WrapperType, reference, reference > Iterator
Iterator class for sequential access.
Definition: diagonalmatrix.hh:142
size_type rowIndex() const
index of this row in surrounding matrix
Definition: diagonalmatrix.hh:787
Definition: diagonalmatrix.hh:31
void decrement()
Definition: diagonalmatrix.hh:1057
size_type realIndex(int i) const
Definition: diagonalmatrix.hh:645
DiagonalRowVectorConst * operator&()
Definition: diagonalmatrix.hh:812
@ blocklevel
The number of block levels we contain. This is 1.
Definition: diagonalmatrix.hh:71
DiagonalMatrix & operator+=(const DiagonalMatrix &y)
vector space addition
Definition: diagonalmatrix.hh:217
friend std::ostream & operator<<(std::ostream &s, const DiagonalMatrix< K, n > &a)
Sends the matrix to an output stream.
Definition: diagonalmatrix.hh:490
double frobenius_norm() const
frobenius norm: sqrt(sum over squared values of entries)
Definition: diagonalmatrix.hh:408
ContainerWrapperIterator(const MyConstType &other)
Definition: diagonalmatrix.hh:1015
A diagonal matrix of static size.
Definition: diagonalmatrix.hh:51
FieldTraits< K >::real_type real_type
Definition: diagonalmatrix.hh:546
ConstIterator ConstRowIterator
rename the iterators for easier access
Definition: diagonalmatrix.hh:182
@ blocklevel
The number of block levels we contain.
Definition: diagonalmatrix.hh:697
DiagonalMatrix & operator/=(const K &k)
vector space division by scalar
Definition: diagonalmatrix.hh:252
ConstIterator end() const
end ConstIterator
Definition: diagonalmatrix.hh:747
@ size
The size of this vector.
Definition: diagonalmatrix.hh:703
Definition: diagonalmatrix.hh:32
double frobenius_norm2() const
square of frobenius norm, need for block recursion
Definition: diagonalmatrix.hh:414
T * operator->() const
Definition: diagonalmatrix.hh:1030
Iterator beforeBegin()
Definition: diagonalmatrix.hh:171
DiagonalMatrix()
Default constructor.
Definition: diagonalmatrix.hh:100
double infinity_norm() const
infinity norm (row sum norm, how to generalize for blocks?)
Definition: diagonalmatrix.hh:420
T real_type
export the type representing the real type of the field
Definition: ftraits.hh:28
DiagonalRowVector< K, n > row_type
Each row is implemented by a field vector.
Definition: diagonalmatrix.hh:75
you have to specialize this structure for any type that should be assignable to a DenseMatrix
Definition: densematrix.hh:79
ConstIterator begin() const
begin ConstIterator
Definition: diagonalmatrix.hh:741
DiagonalMatrix(std::initializer_list< K > const &l)
Construct diagonal matrix from an initializer list.
Definition: diagonalmatrix.hh:120
row_type * pointer(int i) const
Definition: diagonalmatrix.hh:650
DiagonalMatrix & operator*=(const K &k)
vector space multiplication with scalar
Definition: diagonalmatrix.hh:245
DiagonalRowVector(K *p, int col)
Constructor making vector with identical coordinates.
Definition: diagonalmatrix.hh:848
void usmv(const typename FieldTraits< Y >::field_type &alpha, const X &x, Y &y) const
y += alpha A x
Definition: diagonalmatrix.hh:368
@ rows
The number of rows.
Definition: diagonalmatrix.hh:85
bool operator==(const DiagonalRowVectorConst &y) const
Binary vector comparison.
Definition: diagonalmatrix.hh:767
void mv(const X &x, Y &y) const
y = A x
Definition: diagonalmatrix.hh:277
ContainerWrapperIterator< const WrapperType, const_reference, const_reference > ConstIterator
Iterator class for sequential access.
Definition: diagonalmatrix.hh:178
const K & diagonal(size_type i) const
Get const reference to diagonal entry.
Definition: diagonalmatrix.hh:513
row_type row_reference
Definition: diagonalmatrix.hh:77
ContainerWrapperIterator(OtherContainerWrapperIteratorType &other)
Definition: diagonalmatrix.hh:1005
const K & operator[](size_type i) const
same for read only access
Definition: diagonalmatrix.hh:721
K field_type
export the type representing the field
Definition: diagonalmatrix.hh:686
R elementAt(int i) const
Definition: diagonalmatrix.hh:1063
ConstIterator beforeEnd() const
Definition: diagonalmatrix.hh:200
Iterator end()
end iterator
Definition: diagonalmatrix.hh:881
DiagonalRowVectorConst< K, n > type
Definition: diagonalmatrix.hh:935
K determinant() const
calculates the determinant of this matrix
Definition: diagonalmatrix.hh:451
void mmv(const X &x, Y &y) const
y -= A x
Definition: diagonalmatrix.hh:332
DiagonalMatrix & operator=(const K &k)
Assignment from a scalar.
Definition: diagonalmatrix.hh:128
ContainerWrapperIterator< DiagonalRowVectorConst< K, n >, const K, const K & > ConstIterator
ConstIterator class for sequential access.
Definition: diagonalmatrix.hh:736
Macro for wrapping boundary checks.
bool operator==(const DiagonalMatrix &other) const
comparison operator
Definition: diagonalmatrix.hh:261
void advance(int n)
Definition: diagonalmatrix.hh:1068
DiagonalRowVector< K, n > type
Definition: diagonalmatrix.hh:953
size_type N() const
number of blocks in the vector (are of size 1 here)
Definition: diagonalmatrix.hh:775
K conjugateComplex(const K &x)
compute conjugate complex of x
Definition: math.hh:105
FieldVector< K, n > & diagonal()
Get reference to diagonal vector.
Definition: diagonalmatrix.hh:531
Definition: ftraits.hh:23
bool exists(size_type i, size_type j) const
return true when (i,j) is in pattern
Definition: diagonalmatrix.hh:480
static void apply(DenseMatrix &denseMatrix, DiagonalMatrix< field, N > const &rhs)
Definition: diagonalmatrix.hh:1092
bool identical(const DiagonalRowVectorConst< K, n > &other) const
Definition: diagonalmatrix.hh:730
bool operator!=(const DiagonalMatrix &other) const
incomparison operator
Definition: diagonalmatrix.hh:267
void solve(V &x, const V &b) const
Solve system A x = b.
Definition: diagonalmatrix.hh:437
DiagonalMatrixWrapper()
Definition: diagonalmatrix.hh:637
@ cols
The number of columns.
Definition: diagonalmatrix.hh:87
size_type M() const
number of blocks in column direction
Definition: diagonalmatrix.hh:470
DiagonalRowVector()
Constructor making uninitialized vector.
Definition: diagonalmatrix.hh:844
row_type reference
Definition: diagonalmatrix.hh:76
T field_type
export the type representing the field
Definition: ftraits.hh:26
void umv(const X &x, Y &y) const
y += A x
Definition: diagonalmatrix.hh:296
DiagonalMatrix(const K &k)
Constructor initializing the whole matrix with a scalar.
Definition: diagonalmatrix.hh:103
void mmhv(const X &x, Y &y) const
y -= A^H x
Definition: diagonalmatrix.hh:356
Iterator begin()
begin iterator
Definition: diagonalmatrix.hh:151
void mtv(const X &x, Y &y) const
y = A^T x
Definition: diagonalmatrix.hh:289
#define DUNE_ASSERT_BOUNDS(cond)
If DUNE_CHECK_BOUNDS is defined: check if condition cond holds; otherwise, do nothing.
Definition: boundschecking.hh:28
DiagonalMatrix(const FieldVector< K, n > &diag)
Constructor initializing the diagonal with a vector.
Definition: diagonalmatrix.hh:108
Implements a matrix constructed from a given type representing a field and compile-time given number ...
size_type M() const
number of columns
Definition: densematrix.hh:692
ConstIterator beforeBegin() const
Definition: diagonalmatrix.hh:761
const FieldVector< K, n > & diagonal() const
Get const reference to diagonal vector.
Definition: diagonalmatrix.hh:525
ConstIterator beforeBegin() const
Definition: diagonalmatrix.hh:207
DiagonalRowVectorConst< K, n > const_row_type
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:135
K & operator[](size_type i)
random access
Definition: diagonalmatrix.hh:862
void umhv(const X &x, Y &y) const
y += A^H x
Definition: diagonalmatrix.hh:320
K & diagonal(size_type i)
Get reference to diagonal entry.
Definition: diagonalmatrix.hh:519
const_row_type::ConstIterator ConstColIterator
rename the iterators for easier access
Definition: diagonalmatrix.hh:184
R dereference() const
Definition: diagonalmatrix.hh:1046
std::size_t size_type
The type used for the index access and size operation.
Definition: diagonalmatrix.hh:692
void increment()
Definition: diagonalmatrix.hh:1051
A dense n x m matrix.
Definition: densematrix.hh:26
Get the 'const' version of a reference to a mutable object.
Definition: genericiterator.hh:84
Traits for type conversions and type information.
void umtv(const X &x, Y &y) const
y += A^T x
Definition: diagonalmatrix.hh:308
K value_type
export the type representing the field
Definition: diagonalmatrix.hh:59
Error thrown if operations of a FieldMatrix fail.
Definition: densematrix.hh:146
ContainerWrapperIterator & operator=(OtherContainerWrapperIteratorType &other)
Definition: diagonalmatrix.hh:1021
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
value_type field_type
Definition: diagonalmatrix.hh:60
double infinity_norm_real() const
simplified infinity norm (uses Manhattan norm for complex values)
Definition: diagonalmatrix.hh:426
Iterator beforeEnd()
Definition: diagonalmatrix.hh:888
get the 'mutable' version of a reference to a const object
Definition: genericiterator.hh:113
Iterator RowIterator
rename the iterators for easier access
Definition: diagonalmatrix.hh:146
DiagonalMatrixWrapper(const DiagonalMatrixType *mat)
Definition: diagonalmatrix.hh:641
void invert()
Compute inverse.
Definition: diagonalmatrix.hh:444