3 #ifndef DUNE_DIAGONAL_MATRIX_HH
4 #define DUNE_DIAGONAL_MATRIX_HH
15 #include <initializer_list>
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>
367 void usmv (
const K& alpha,
const X& x, Y& y)
const
369 #ifdef DUNE_FMatrix_WITH_CHECKING
374 y[i] += alpha * diag_[i] * x[i];
378 template<
class X,
class Y>
379 void usmtv (
const K& alpha,
const X& x, Y& y)
const
381 #ifdef DUNE_FMatrix_WITH_CHECKING
386 y[i] += alpha * diag_[i] * x[i];
390 template<
class X,
class Y>
391 void usmhv (
const K& alpha,
const X& x, Y& y)
const
393 #ifdef DUNE_FMatrix_WITH_CHECKING
435 for (
int i=0; i<n; i++)
436 x[i] = b[i]/diag_[i];
442 for (
int i=0; i<n; i++)
443 diag_[i] = 1/diag_[i];
450 for (
int i=1; i<n; i++)
478 #ifdef DUNE_FMatrix_WITH_CHECKING
488 friend std::ostream& operator<< (std::ostream& s, const DiagonalMatrix<K,n>& a)
492 s << ((i==j) ? a.diag_[i] : 0) <<
" ";
501 return reference(const_cast<K*>(&diag_[i]), i);
540 #ifndef DOXYGEN // hide specialization
544 class DiagonalMatrix<K, 1> :
public FieldMatrix<K, 1, 1>
546 typedef FieldMatrix<K,1,1> Base;
549 typedef typename Base::size_type
size_type;
558 typedef typename Base::row_type
row_type;
581 (*this)[0][0] = scalar;
587 return (*
this)[0][0];
593 return (*
this)[0][0];
597 const FieldVector<K,1>&
diagonal()
const
612 template<
class DiagonalMatrixType>
613 class DiagonalMatrixWrapper
615 typedef typename DiagonalMatrixType::reference reference;
616 typedef typename DiagonalMatrixType::const_reference const_reference;
617 typedef typename DiagonalMatrixType::field_type K;
618 typedef DiagonalRowVector<K, DiagonalMatrixType::rows> row_type;
619 typedef std::size_t size_type;
620 typedef DiagonalMatrixWrapper< DiagonalMatrixType> MyType;
632 mat_(const_cast<DiagonalMatrixType*>(mat))
642 row_ =
row_type(&(mat_->diagonal(i)), i);
648 return mat_==other.mat_;
653 mutable DiagonalMatrixType* mat_;
654 mutable row_type row_;
660 template<
class K,
int n >
661 class DiagonalRowVectorConst
663 template<
class DiagonalMatrixType>
713 #ifdef DUNE_FMatrix_WITH_CHECKING
726 return ((
p_ == other.p_)and (
row_ == other.row_));
763 return ((
p_==y.p_)and (
row_==y.row_));
801 return const_cast<K*
>(
p_);
814 template<
class K,
int n >
817 template<
class DiagonalMatrixType>
857 #ifdef DUNE_FMatrix_WITH_CHECKING
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 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_;
1026 return containerWrapper_.pointer(position_);
1032 return position_ == other.position_ && containerWrapper_.identical(other.containerWrapper_);
1037 return position_ == other.position_ && containerWrapper_.identical(other.containerWrapper_);
1042 return *containerWrapper_.pointer(position_);
1059 return *containerWrapper_.pointer(position_+i);
1064 position_=position_+n;
1067 template<
class OtherContainerWrapperIteratorType>
1068 std::ptrdiff_t
distanceTo(OtherContainerWrapperIteratorType& other)
const
1070 assert(containerWrapper_.identical(other));
1071 return other.position_ - position_;
1076 return containerWrapper_.realIndex(position_);
1080 NonConstCW containerWrapper_;
1086 template<
class M,
class K,
int n>
1089 assert( fm.
rows() == n );
1090 assert( fm.
cols() == n );
1092 for(
int i=0; i<n; ++i)
1093 fm[i][i] = s.diagonal()[i];
DiagonalRowVectorConst * operator&()
Definition: diagonalmatrix.hh:804
DiagonalRowVectorConst< K, n > type
Definition: diagonalmatrix.hh:930
Iterator beforeEnd()
Definition: diagonalmatrix.hh:163
Iterator end()
end iterator
Definition: diagonalmatrix.hh:156
Iterator beforeEnd()
Definition: diagonalmatrix.hh:883
bool operator==(const DiagonalMatrix &other) const
comparison operator
Definition: diagonalmatrix.hh:260
double frobenius_norm2() const
square of frobenius norm, need for block recursion
Definition: diagonalmatrix.hh:410
void increment()
Definition: diagonalmatrix.hh:1045
Facade class for stl conformant bidirectional iterators.
Definition: iteratorfacades.hh:266
K value_type
export the type representing the field
Definition: diagonalmatrix.hh:58
size_type cols() const
number of columns
Definition: densematrix.hh:701
row_type::Iterator ColIterator
rename the iterators for easier access
Definition: diagonalmatrix.hh:147
A dense n x m matrix.
Definition: densematrix.hh:22
DiagonalMatrix & operator-=(const DiagonalMatrix &y)
vector space subtraction
Definition: diagonalmatrix.hh:223
ContainerWrapperIterator & operator=(OtherContainerWrapperIteratorType &other)
Definition: diagonalmatrix.hh:1016
DiagonalMatrixWrapper(const DiagonalMatrixType *mat)
Definition: diagonalmatrix.hh:631
ConstIterator beforeEnd() const
Definition: diagonalmatrix.hh:199
DiagonalMatrix(const FieldVector< K, n > &diag)
Constructor initializing the diagonal with a vector.
Definition: diagonalmatrix.hh:107
DiagonalRowVector< K, n > type
Definition: diagonalmatrix.hh:942
Iterator beforeBegin()
Definition: diagonalmatrix.hh:170
bool equals(const MyConstType &other) const
Definition: diagonalmatrix.hh:1035
ContainerWrapperIterator(OtherContainerWrapperIteratorType &other)
Definition: diagonalmatrix.hh:1000
The number of columns.
Definition: diagonalmatrix.hh:86
const K & diagonal(size_type i) const
Get const reference to diagonal entry.
Definition: diagonalmatrix.hh:511
K conjugateComplex(const K &x)
compute conjugate complex of x
Definition: math.hh:103
void istl_assign_to_fmatrix(DenseMatrix &denseMatrix, const K(&values)[M][N])
Definition: densematrix.hh:76
R dereference() const
Definition: diagonalmatrix.hh:1040
A diagonal matrix of static size.
Definition: diagonalmatrix.hh:50
bool identical(const DiagonalRowVectorConst< K, n > &other) const
Definition: diagonalmatrix.hh:724
std::ptrdiff_t index() const
Definition: diagonalmatrix.hh:1074
ContainerWrapperIterator< const WrapperType, reference, reference > Iterator
Iterator class for sequential access.
Definition: diagonalmatrix.hh:141
const_row_type const_row_reference
Definition: diagonalmatrix.hh:79
std::ptrdiff_t distanceTo(OtherContainerWrapperIteratorType &other) const
Definition: diagonalmatrix.hh:1068
const FieldVector< K, n > & diagonal() const
Get const reference to diagonal vector.
Definition: diagonalmatrix.hh:523
FieldTraits< value_type >::real_type infinity_norm_real() const
simplified infinity norm (uses Manhattan norm for complex values)
Definition: densevector.hh:625
A few common exception classes.
size_type M() const
number of blocks in column direction
Definition: diagonalmatrix.hh:466
double frobenius_norm() const
frobenius norm: sqrt(sum over squared values of entries)
Definition: diagonalmatrix.hh:404
double infinity_norm_real() const
simplified infinity norm (uses Manhattan norm for complex values)
Definition: diagonalmatrix.hh:422
ConstIterator begin() const
begin ConstIterator
Definition: diagonalmatrix.hh:735
void usmtv(const K &alpha, const X &x, Y &y) const
y += alpha A^T x
Definition: diagonalmatrix.hh:379
T * operator->() const
Definition: diagonalmatrix.hh:1024
Definition: diagonalmatrix.hh:30
DiagonalRowVector * operator&()
Definition: diagonalmatrix.hh:914
DiagonalMatrix()
Default constructor.
Definition: diagonalmatrix.hh:99
void mmhv(const X &x, Y &y) const
y -= A^H x
Definition: diagonalmatrix.hh:355
reference operator[](size_type i)
Return reference object as row replacement.
Definition: diagonalmatrix.hh:499
ConstIterator beforeEnd() const
Definition: diagonalmatrix.hh:748
size_type realIndex(int i) const
Definition: diagonalmatrix.hh:794
size_type N() const
number of blocks in the vector (are of size 1 here)
Definition: diagonalmatrix.hh:769
DiagonalRowVector(K *p, int col)
Constructor making vector with identical coordinates.
Definition: diagonalmatrix.hh:840
row_type row_reference
Definition: diagonalmatrix.hh:76
ConstIterator begin() const
begin iterator
Definition: diagonalmatrix.hh:186
Definition: diagonalmatrix.hh:669
ConstIterator end() const
end ConstIterator
Definition: diagonalmatrix.hh:741
ConstIterator const_iterator
typedef for stl compliant access
Definition: diagonalmatrix.hh:179
const_row_type::ConstIterator ConstColIterator
rename the iterators for easier access
Definition: diagonalmatrix.hh:183
K block_type
export the type representing the components
Definition: diagonalmatrix.hh:679
FieldTraits< value_type >::real_type two_norm() const
two norm sqrt(sum over squared values of entries)
Definition: densevector.hh:589
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:598
K * pointer(size_type i) const
Definition: diagonalmatrix.hh:799
void decrement()
Definition: diagonalmatrix.hh:1051
Iterator begin()
begin iterator
Definition: diagonalmatrix.hh:870
const_row_type const_reference
Definition: diagonalmatrix.hh:78
DiagonalMatrix & operator/=(const K &k)
vector space division by scalar
Definition: diagonalmatrix.hh:251
DiagonalRowVectorConst()
Constructor making uninitialized vector.
Definition: diagonalmatrix.hh:697
void solve(V &x, const V &b) const
Solve system A x = b.
Definition: diagonalmatrix.hh:433
std::size_t size_type
The type used for the index access and size operations.
Definition: diagonalmatrix.hh:65
The number of block levels we contain.
Definition: diagonalmatrix.hh:687
std::size_t size_type
The type used for the index access and size operation.
Definition: diagonalmatrix.hh:682
get the 'mutable' version of a reference to a const object
Definition: genericiterator.hh:113
ContainerWrapperIterator(CW containerWrapper, int position)
Definition: diagonalmatrix.hh:994
row_type * pointer(int i) const
Definition: diagonalmatrix.hh:640
std::size_t size_type
The type used for the index access and size operation.
Definition: diagonalmatrix.hh:833
Definition of the DUNE_UNUSED macro for the case that config.h is not available.
void umtv(const X &x, Y &y) const
y += A^T x
Definition: diagonalmatrix.hh:307
size_type size() const
Definition: diagonalmatrix.hh:91
void umv(const X &x, Y &y) const
y += A x
Definition: diagonalmatrix.hh:295
void umhv(const X &x, Y &y) const
y += A^H x
Definition: diagonalmatrix.hh:319
The number of rows.
Definition: diagonalmatrix.hh:84
DiagonalRowVector & operator=(const K &k)
Assignment operator for scalar.
Definition: diagonalmatrix.hh:845
K field_type
export the type representing the field
Definition: diagonalmatrix.hh:676
Implements a vector constructed from a given type representing a field and a compile-time given size...
bool identical(const DiagonalMatrix< K, n > &other) const
Check if matrix is the same object as the other matrix.
Definition: diagonalmatrix.hh:134
ContainerWrapperIterator(const MyType &other)
Definition: diagonalmatrix.hh:1005
ContainerWrapperIterator< const WrapperType, const_reference, const_reference > ConstIterator
Iterator class for sequential access.
Definition: diagonalmatrix.hh:177
ConstIterator const_iterator
typedef for stl compliant access
Definition: diagonalmatrix.hh:732
Definition: diagonalmatrix.hh:31
DiagonalMatrix(std::initializer_list< K > const &l)
Construct diagonal matrix from an initializer list.
Definition: diagonalmatrix.hh:119
void usmhv(const K &alpha, const X &x, Y &y) const
y += alpha A^H x
Definition: diagonalmatrix.hh:391
Get the 'const' version of a reference to a mutable object.
Definition: genericiterator.hh:84
ConstIterator beforeBegin() const
Definition: diagonalmatrix.hh:206
FieldVector< K, n > & diagonal()
Get reference to diagonal vector.
Definition: diagonalmatrix.hh:529
Iterator iterator
typedef for stl compliant access
Definition: diagonalmatrix.hh:143
K & operator[](size_type i)
random access
Definition: diagonalmatrix.hh:854
Iterator begin()
begin iterator
Definition: diagonalmatrix.hh:150
ConstIterator end() const
end iterator
Definition: diagonalmatrix.hh:192
DiagonalRowVectorConst< K, n > const_row_type
Definition: diagonalmatrix.hh:77
ContainerWrapperIterator(const MyConstType &other)
Definition: diagonalmatrix.hh:1010
R elementAt(int i) const
Definition: diagonalmatrix.hh:1057
DiagonalRowVectorConst< K, n > type
Definition: diagonalmatrix.hh:936
ConstIterator ConstRowIterator
rename the iterators for easier access
Definition: diagonalmatrix.hh:181
DiagonalMatrix & operator=(const K &k)
Assignment from a scalar.
Definition: diagonalmatrix.hh:127
DiagonalMatrix(const K &k)
Constructor initializing the whole matrix with a scalar.
Definition: diagonalmatrix.hh:102
ConstIterator const_iterator
typedef for stl compliant access
Definition: diagonalmatrix.hh:898
#define DUNE_UNUSED_PARAMETER(parm)
A macro to mark intentional unused function parameters with.
Definition: unused.hh:18
Iterator RowIterator
rename the iterators for easier access
Definition: diagonalmatrix.hh:145
Implements a matrix constructed from a given type representing a field and a compile-time given numbe...
DiagonalRowVectorConst(K *p, int col)
Constructor making vector with identical coordinates.
Definition: diagonalmatrix.hh:703
bool identical(const DiagonalMatrixWrapper &other) const
Definition: diagonalmatrix.hh:646
Iterator beforeBegin()
Definition: diagonalmatrix.hh:890
K field_type
export the type representing the field
Definition: diagonalmatrix.hh:827
bool exists(size_type i, size_type j) const
return true when (i,j) is in pattern
Definition: diagonalmatrix.hh:476
K determinant() const
calculates the determinant of this matrix
Definition: diagonalmatrix.hh:447
const_reference operator[](size_type i) const
Return const_reference object as row replacement.
Definition: diagonalmatrix.hh:505
K block_type
export the type representing the components
Definition: diagonalmatrix.hh:62
std::size_t position_
The current position in the buffer.
Definition: variablesizecommunicator.hh:136
DiagonalMatrix & operator*=(const K &k)
vector space multiplication with scalar
Definition: diagonalmatrix.hh:244
DiagonalRowVector()
Constructor making uninitialized vector.
Definition: diagonalmatrix.hh:836
size_type row_
Definition: diagonalmatrix.hh:811
void mmv(const X &x, Y &y) const
y -= A x
Definition: diagonalmatrix.hh:331
double infinity_norm() const
infinity norm (row sum norm, how to generalize for blocks?)
Definition: diagonalmatrix.hh:416
Iterator begin()
begin iterator
Definition: densevector.hh:307
const K & diagonal() const
the diagonal value
Definition: diagonalmatrix.hh:787
size_type N() const
number of blocks in row direction
Definition: diagonalmatrix.hh:460
Iterator end()
end iterator
Definition: diagonalmatrix.hh:876
Iterator iterator
typedef for stl compliant access
Definition: diagonalmatrix.hh:867
size_type rowIndex() const
index of this row in surrounding matrix
Definition: diagonalmatrix.hh:781
Definition: diagonalmatrix.hh:32
void mtv(const X &x, Y &y) const
y = A^T x
Definition: diagonalmatrix.hh:288
const K & operator[](size_type i) const
same for read only access
Definition: diagonalmatrix.hh:711
ConstIterator beforeBegin() const
Definition: diagonalmatrix.hh:755
FieldTraits< value_type >::real_type infinity_norm() const
infinity norm (maximum of absolute values of entries)
Definition: densevector.hh:607
Traits for type conversions and type information.
DiagonalMatrixWrapper()
Definition: diagonalmatrix.hh:627
row_type reference
Definition: diagonalmatrix.hh:75
size_type rows() const
number of rows
Definition: densematrix.hh:695
Implements a matrix constructed from a given type representing a field and compile-time given number ...
K block_type
export the type representing the components
Definition: diagonalmatrix.hh:830
void mv(const X &x, Y &y) const
y = A x
Definition: diagonalmatrix.hh:276
K & diagonal(size_type i)
Get reference to diagonal entry.
Definition: diagonalmatrix.hh:517
Iterator class for sparse vector-like containers.
Definition: diagonalmatrix.hh:33
ContainerWrapperIterator< DiagonalRowVector< K, n >, K, K & > Iterator
Iterator class for sequential access.
Definition: diagonalmatrix.hh:865
The number of block levels we contain. This is 1.
Definition: diagonalmatrix.hh:70
bool operator==(const DiagonalRowVectorConst &y) const
Binary vector comparison.
Definition: diagonalmatrix.hh:761
size_type dim() const
dimension of the vector space
Definition: diagonalmatrix.hh:775
DiagonalRowVector< K, n > type
Definition: diagonalmatrix.hh:948
void mmtv(const X &x, Y &y) const
y -= A^T x
Definition: diagonalmatrix.hh:343
void advance(int n)
Definition: diagonalmatrix.hh:1062
size_type realIndex(int i) const
Definition: diagonalmatrix.hh:635
DiagonalMatrix & operator+=(const DiagonalMatrix &y)
vector space addition
Definition: diagonalmatrix.hh:216
DiagonalRowVector< K, n > row_type
Each row is implemented by a field vector.
Definition: diagonalmatrix.hh:74
bool operator!=(const DiagonalMatrix &other) const
incomparison operator
Definition: diagonalmatrix.hh:266
value_type field_type
Definition: diagonalmatrix.hh:59
#define DUNE_THROW(E, m)
Definition: exceptions.hh:243
ContainerWrapperIterator< DiagonalRowVectorConst< K, n >, const K, const K & > ConstIterator
ConstIterator class for sequential access.
Definition: diagonalmatrix.hh:730
bool equals(const MyType &other) const
Definition: diagonalmatrix.hh:1030
ContainerWrapperIterator< DiagonalRowVectorConst< K, n >, const K, const K & > ConstIterator
ConstIterator class for sequential access.
Definition: diagonalmatrix.hh:896
void invert()
Compute inverse.
Definition: diagonalmatrix.hh:440
Error thrown if operations of a FieldMatrix fail.
Definition: densematrix.hh:171
The size of this vector.
Definition: diagonalmatrix.hh:693
void usmv(const K &alpha, const X &x, Y &y) const
y += alpha A x
Definition: diagonalmatrix.hh:367
Implements a generic iterator class for writing stl conformant iterators.
K * p_
Definition: diagonalmatrix.hh:810