3 #ifndef DUNE_DIAGONAL_MATRIX_HH
4 #define DUNE_DIAGONAL_MATRIX_HH
47 template<
class K,
int n>
120 return (
this==&other);
136 return Iterator(WrapperType(
this),0);
142 return Iterator(WrapperType(
this),n);
149 return Iterator(WrapperType(
this),n-1);
156 return Iterator(WrapperType(
this),-1);
176 ConstIterator
end ()
const
259 template<
class X,
class Y>
260 void mv (
const X& x, Y& y)
const
262 #ifdef DUNE_FMatrix_WITH_CHECKING
266 for (size_type i=0; i<n; ++i)
267 y[i] = diag_[i] * x[i];
271 template<
class X,
class Y>
272 void mtv (
const X& x, Y& y)
const
278 template<
class X,
class Y>
279 void umv (
const X& x, Y& y)
const
281 #ifdef DUNE_FMatrix_WITH_CHECKING
285 for (size_type i=0; i<n; ++i)
286 y[i] += diag_[i] * x[i];
290 template<
class X,
class Y>
291 void umtv (
const X& x, Y& y)
const
293 #ifdef DUNE_FMatrix_WITH_CHECKING
297 for (size_type i=0; i<n; ++i)
298 y[i] += diag_[i] * x[i];
302 template<
class X,
class Y>
303 void umhv (
const X& x, Y& y)
const
305 #ifdef DUNE_FMatrix_WITH_CHECKING
309 for (size_type i=0; i<n; i++)
314 template<
class X,
class Y>
315 void mmv (
const X& x, Y& y)
const
317 #ifdef DUNE_FMatrix_WITH_CHECKING
321 for (size_type i=0; i<n; ++i)
322 y[i] -= diag_[i] * x[i];
326 template<
class X,
class Y>
327 void mmtv (
const X& x, Y& y)
const
329 #ifdef DUNE_FMatrix_WITH_CHECKING
333 for (size_type i=0; i<n; ++i)
334 y[i] -= diag_[i] * x[i];
338 template<
class X,
class Y>
339 void mmhv (
const X& x, Y& y)
const
341 #ifdef DUNE_FMatrix_WITH_CHECKING
345 for (size_type i=0; i<n; i++)
350 template<
class X,
class Y>
351 void usmv (
const K& alpha,
const X& x, Y& y)
const
353 #ifdef DUNE_FMatrix_WITH_CHECKING
357 for (size_type i=0; i<n; i++)
358 y[i] += alpha * diag_[i] * x[i];
362 template<
class X,
class Y>
363 void usmtv (
const K& alpha,
const X& x, Y& y)
const
365 #ifdef DUNE_FMatrix_WITH_CHECKING
369 for (size_type i=0; i<n; i++)
370 y[i] += alpha * diag_[i] * x[i];
374 template<
class X,
class Y>
375 void usmhv (
const K& alpha,
const X& x, Y& y)
const
377 #ifdef DUNE_FMatrix_WITH_CHECKING
381 for (size_type i=0; i<n; i++)
419 for (
int i=0; i<n; i++)
420 x[i] = b[i]/diag_[i];
426 for (
int i=0; i<n; i++)
427 diag_[i] = 1/diag_[i];
434 for (
int i=1; i<n; i++)
460 bool exists (size_type i, size_type j)
const
462 #ifdef DUNE_FMatrix_WITH_CHECKING
472 friend std::ostream& operator<< (std::ostream& s, const DiagonalMatrix<K,n>& a)
474 for (size_type i=0; i<n; i++) {
475 for (size_type j=0; j<n; j++)
476 s << ((i==j) ? a.diag_[i] : 0) <<
" ";
485 return reference(const_cast<K*>(&diag_[i]), i);
524 #ifndef DOXYGEN // hide specialization
528 class DiagonalMatrix<K, 1> :
public FieldMatrix<K, 1, 1>
530 typedef FieldMatrix<K,1,1> Base;
533 typedef typename Base::size_type
size_type;
542 typedef typename Base::row_type
row_type;
565 (*this)[0][0] = scalar;
571 return (*
this)[0][0];
577 return (*
this)[0][0];
581 const FieldVector<K,1>&
diagonal()
const
596 template<
class DiagonalMatrixType>
597 class DiagonalMatrixWrapper
599 typedef typename DiagonalMatrixType::reference reference;
600 typedef typename DiagonalMatrixType::const_reference const_reference;
601 typedef typename DiagonalMatrixType::field_type K;
602 typedef DiagonalRowVector<K, DiagonalMatrixType::rows> row_type;
603 typedef std::size_t size_type;
604 typedef DiagonalMatrixWrapper< DiagonalMatrixType> MyType;
616 mat_(const_cast<DiagonalMatrixType*>(mat))
626 row_ = row_type(&(mat_->diagonal(i)), i);
632 return mat_==other.mat_;
637 mutable DiagonalMatrixType* mat_;
638 mutable row_type row_;
644 template<
class K,
int n >
645 class DiagonalRowVectorConst
647 template<
class DiagonalMatrixType>
697 #ifdef DUNE_FMatrix_WITH_CHECKING
710 return ((
p_ == other.p_)and (
row_ == other.row_));
725 ConstIterator
end ()
const
747 return ((
p_==y.p_)and (
row_==y.row_));
785 return const_cast<K*
>(
p_);
798 template<
class K,
int n >
801 template<
class DiagonalMatrixType>
840 #ifdef DUNE_FMatrix_WITH_CHECKING
910 template<
class K,
int n>
916 template<
class K,
int n>
922 template<
class K,
int n>
928 template<
class K,
int n>
958 template<
class CW,
class T,
class R>
978 containerWrapper_(containerWrapper),
982 template<
class OtherContainerWrapperIteratorType>
984 containerWrapper_(other.containerWrapper_),
985 position_(other.position_)
989 containerWrapper_(other.containerWrapper_),
990 position_(other.position_)
994 containerWrapper_(other.containerWrapper_),
995 position_(other.position_)
998 template<
class OtherContainerWrapperIteratorType>
1001 containerWrapper_ = other.containerWrapper_;
1002 position_ = other.position_;
1009 return containerWrapper_.pointer(position_);
1015 return position_ == other.position_ && containerWrapper_.identical(other.containerWrapper_);
1020 return position_ == other.position_ && containerWrapper_.identical(other.containerWrapper_);
1025 return *containerWrapper_.pointer(position_);
1042 return *containerWrapper_.pointer(position_+i);
1047 position_=position_+n;
1050 template<
class OtherContainerWrapperIteratorType>
1051 std::ptrdiff_t
distanceTo(OtherContainerWrapperIteratorType& other)
const
1053 assert(containerWrapper_.identical(other));
1054 return other.position_ - position_;
1059 return containerWrapper_.realIndex(position_);
1063 NonConstCW containerWrapper_;
1069 template<
class M,
class K,
int n>
1072 assert( fm.
rows() == n );
1073 assert( fm.
cols() == n );
1075 for(
int i=0; i<n; ++i)
1076 fm[i][i] = s.diagonal()[i];
size_type M() const
number of blocks in column direction
Definition: diagonalmatrix.hh:450
ConstIterator const_iterator
typedef for stl compliant access
Definition: diagonalmatrix.hh:716
double frobenius_norm() const
frobenius norm: sqrt(sum over squared values of entries)
Definition: diagonalmatrix.hh:388
void mmv(const X &x, Y &y) const
y -= A x
Definition: diagonalmatrix.hh:315
Error thrown if operations of a FieldMatrix fail.
Definition: densematrix.hh:178
void invert()
Compute inverse.
Definition: diagonalmatrix.hh:424
ConstIterator begin() const
begin ConstIterator
Definition: diagonalmatrix.hh:719
DiagonalRowVectorConst< K, n > type
Definition: diagonalmatrix.hh:913
Traits for type conversions and type information.
void mtv(const X &x, Y &y) const
y = A^T x
Definition: diagonalmatrix.hh:272
size_type dim() const
dimension of the vector space
Definition: diagonalmatrix.hh:759
size_type realIndex(int i) const
Definition: diagonalmatrix.hh:619
K value_type
export the type representing the field
Definition: diagonalmatrix.hh:56
The number of block levels we contain.
Definition: diagonalmatrix.hh:671
const K & diagonal(size_type i) const
Get const reference to diagonal entry.
Definition: diagonalmatrix.hh:495
DiagonalRowVector< K, n > type
Definition: diagonalmatrix.hh:925
K block_type
export the type representing the components
Definition: diagonalmatrix.hh:814
void mv(const X &x, Y &y) const
y = A x
Definition: diagonalmatrix.hh:260
ConstIterator beforeEnd() const
Definition: diagonalmatrix.hh:732
Dune namespace.
Definition: alignment.hh:13
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:530
A few common exception classes.
The number of block levels we contain. This is 1.
Definition: diagonalmatrix.hh:68
DiagonalMatrix & operator/=(const K &k)
vector space division by scalar
Definition: diagonalmatrix.hh:235
const K & diagonal() const
the diagonal value
Definition: diagonalmatrix.hh:771
void solve(V &x, const V &b) const
Solve system A x = b.
Definition: diagonalmatrix.hh:417
Implements a matrix constructed from a given type representing a field and a compile-time given numbe...
ContainerWrapperIterator< const WrapperType, reference, reference > Iterator
Iterator class for sequential access.
Definition: diagonalmatrix.hh:125
DiagonalRowVectorConst()
Constructor making uninitialized vector.
Definition: diagonalmatrix.hh:681
const_row_type const_row_reference
Definition: diagonalmatrix.hh:77
ContainerWrapperIterator< DiagonalRowVectorConst< K, n >, const K, const K & > ConstIterator
ConstIterator class for sequential access.
Definition: diagonalmatrix.hh:879
void usmtv(const K &alpha, const X &x, Y &y) const
y += alpha A^T x
Definition: diagonalmatrix.hh:363
DiagonalRowVectorConst * operator&()
Definition: diagonalmatrix.hh:788
K conjugateComplex(const K &x)
compute conjugate complex of x
Definition: math.hh:103
void mmhv(const X &x, Y &y) const
y -= A^H x
Definition: diagonalmatrix.hh:339
Facade class for stl conformant bidirectional iterators.
Definition: iteratorfacades.hh:267
R dereference() const
Definition: diagonalmatrix.hh:1023
Iterator beforeEnd()
Definition: diagonalmatrix.hh:866
std::size_t size_type
The type used for the index access and size operation.
Definition: diagonalmatrix.hh:817
The number of rows.
Definition: diagonalmatrix.hh:82
bool identical(const DiagonalRowVectorConst< K, n > &other) const
Definition: diagonalmatrix.hh:708
void increment()
Definition: diagonalmatrix.hh:1028
row_type row_reference
Definition: diagonalmatrix.hh:74
Definition: diagonalmatrix.hh:29
ConstIterator begin() const
begin iterator
Definition: diagonalmatrix.hh:170
FieldTraits< value_type >::real_type infinity_norm() const
infinity norm (maximum of absolute values of entries)
Definition: densevector.hh:539
ConstIterator end() const
end ConstIterator
Definition: diagonalmatrix.hh:725
const FieldVector< K, n > & diagonal() const
Get const reference to diagonal vector.
Definition: diagonalmatrix.hh:507
std::size_t position_
The current position in the buffer.
Definition: variablesizecommunicator.hh:136
void umtv(const X &x, Y &y) const
y += A^T x
Definition: diagonalmatrix.hh:291
double infinity_norm_real() const
simplified infinity norm (uses Manhattan norm for complex values)
Definition: diagonalmatrix.hh:406
void advance(int n)
Definition: diagonalmatrix.hh:1045
void umhv(const X &x, Y &y) const
y += A^H x
Definition: diagonalmatrix.hh:303
DiagonalMatrix()
Default constructor.
Definition: diagonalmatrix.hh:97
reference operator[](size_type i)
Return reference object as row replacement.
Definition: diagonalmatrix.hh:483
Iterator beforeBegin()
Definition: diagonalmatrix.hh:154
R elementAt(int i) const
Definition: diagonalmatrix.hh:1040
size_type row_
Definition: diagonalmatrix.hh:795
ConstIterator const_iterator
typedef for stl compliant access
Definition: diagonalmatrix.hh:163
const_row_type::ConstIterator ConstColIterator
rename the iterators for easier access
Definition: diagonalmatrix.hh:167
void usmhv(const K &alpha, const X &x, Y &y) const
y += alpha A^H x
Definition: diagonalmatrix.hh:375
ContainerWrapperIterator(const MyConstType &other)
Definition: diagonalmatrix.hh:993
const_row_type const_reference
Definition: diagonalmatrix.hh:76
Get the 'const' version of a reference to a mutable object.
Definition: genericiterator.hh:85
ConstIterator beforeBegin() const
Definition: diagonalmatrix.hh:190
Iterator iterator
typedef for stl compliant access
Definition: diagonalmatrix.hh:127
row_type * pointer(int i) const
Definition: diagonalmatrix.hh:624
ContainerWrapperIterator(const MyType &other)
Definition: diagonalmatrix.hh:988
DiagonalRowVectorConst< K, n > const_row_type
Definition: diagonalmatrix.hh:75
get the 'mutable' version of a reference to a const object
Definition: genericiterator.hh:114
K field_type
export the type representing the field
Definition: diagonalmatrix.hh:660
DiagonalRowVectorConst(K *p, int col)
Constructor making vector with identical coordinates.
Definition: diagonalmatrix.hh:687
DiagonalMatrix(const K &k)
Constructor initializing the whole matrix with a scalar.
Definition: diagonalmatrix.hh:100
void umv(const X &x, Y &y) const
y += A x
Definition: diagonalmatrix.hh:279
DiagonalRowVector * operator&()
Definition: diagonalmatrix.hh:897
K field_type
export the type representing the field
Definition: diagonalmatrix.hh:811
DiagonalRowVector & operator=(const K &k)
Assignment operator for scalar.
Definition: diagonalmatrix.hh:829
const K & operator[](size_type i) const
same for read only access
Definition: diagonalmatrix.hh:695
bool exists(size_type i, size_type j) const
return true when (i,j) is in pattern
Definition: diagonalmatrix.hh:460
const_reference operator[](size_type i) const
Return const_reference object as row replacement.
Definition: diagonalmatrix.hh:489
DiagonalRowVector(K *p, int col)
Constructor making vector with identical coordinates.
Definition: diagonalmatrix.hh:824
bool equals(const MyType &other) const
Definition: diagonalmatrix.hh:1013
Implements a matrix constructed from a given type representing a field and compile-time given number ...
std::ptrdiff_t index() const
Definition: diagonalmatrix.hh:1057
Iterator begin()
begin iterator
Definition: diagonalmatrix.hh:853
#define DUNE_THROW(E, m)
Definition: exceptions.hh:244
Iterator iterator
typedef for stl compliant access
Definition: diagonalmatrix.hh:850
Definition: diagonalmatrix.hh:30
bool identical(const DiagonalMatrix< K, n > &other) const
Check if matrix is the same object as the other matrix.
Definition: diagonalmatrix.hh:118
std::size_t size_type
The type used for the index access and size operations.
Definition: diagonalmatrix.hh:63
Removes a const qualifier while preserving others.
Definition: typetraits.hh:175
ContainerWrapperIterator< const WrapperType, const_reference, const_reference > ConstIterator
Iterator class for sequential access.
Definition: diagonalmatrix.hh:161
size_type cols() const
number of columns
Definition: densematrix.hh:710
DiagonalMatrix & operator=(const K &k)
Assignment from a scalar.
Definition: diagonalmatrix.hh:111
Iterator RowIterator
rename the iterators for easier access
Definition: diagonalmatrix.hh:129
Implements a generic iterator class for writing stl conformant iterators.
K * p_
Definition: diagonalmatrix.hh:794
Iterator class for sparse vector-like containers.
Definition: diagonalmatrix.hh:31
Implements a vector constructed from a given type representing a field and a compile-time given size...
std::ptrdiff_t distanceTo(OtherContainerWrapperIteratorType &other) const
Definition: diagonalmatrix.hh:1051
ContainerWrapperIterator(OtherContainerWrapperIteratorType &other)
Definition: diagonalmatrix.hh:983
void mmtv(const X &x, Y &y) const
y -= A^T x
Definition: diagonalmatrix.hh:327
Definition of the DUNE_UNUSED macro for the case that config.h is not available.
Iterator begin()
begin iterator
Definition: diagonalmatrix.hh:134
Definition: diagonalmatrix.hh:653
A dense n x m matrix.
Definition: densematrix.hh:24
DiagonalMatrixWrapper()
Definition: diagonalmatrix.hh:611
value_type field_type
Definition: diagonalmatrix.hh:57
#define DUNE_UNUSED_PARAMETER(parm)
A macro to mark intentional unused function parameters with.
Definition: unused.hh:18
ConstIterator ConstRowIterator
rename the iterators for easier access
Definition: diagonalmatrix.hh:165
void decrement()
Definition: diagonalmatrix.hh:1034
ConstIterator const_iterator
typedef for stl compliant access
Definition: diagonalmatrix.hh:881
double infinity_norm() const
infinity norm (row sum norm, how to generalize for blocks?)
Definition: diagonalmatrix.hh:400
size_type size() const
Definition: diagonalmatrix.hh:89
bool equals(const MyConstType &other) const
Definition: diagonalmatrix.hh:1018
FieldTraits< value_type >::real_type two_norm() const
two norm sqrt(sum over squared values of entries)
Definition: densevector.hh:521
Iterator beforeBegin()
Definition: diagonalmatrix.hh:873
size_type N() const
number of blocks in row direction
Definition: diagonalmatrix.hh:444
K * pointer(size_type i) const
Definition: diagonalmatrix.hh:783
K determinant() const
calculates the determinant of this matrix
Definition: diagonalmatrix.hh:431
ContainerWrapperIterator & operator=(OtherContainerWrapperIteratorType &other)
Definition: diagonalmatrix.hh:999
bool operator==(const DiagonalMatrix &other) const
comparison operator
Definition: diagonalmatrix.hh:244
DiagonalMatrix & operator*=(const K &k)
vector space multiplication with scalar
Definition: diagonalmatrix.hh:228
DiagonalRowVector()
Constructor making uninitialized vector.
Definition: diagonalmatrix.hh:820
std::size_t size_type
The type used for the index access and size operation.
Definition: diagonalmatrix.hh:666
A diagonal matrix of static size.
Definition: diagonalmatrix.hh:48
ContainerWrapperIterator< DiagonalRowVector< K, n >, K, K & > Iterator
Iterator class for sequential access.
Definition: diagonalmatrix.hh:848
DiagonalMatrix & operator-=(const DiagonalMatrix &y)
vector space subtraction
Definition: diagonalmatrix.hh:207
bool identical(const DiagonalMatrixWrapper &other) const
Definition: diagonalmatrix.hh:630
ConstIterator beforeEnd() const
Definition: diagonalmatrix.hh:183
size_type realIndex(int i) const
Definition: diagonalmatrix.hh:778
size_type N() const
number of blocks in the vector (are of size 1 here)
Definition: diagonalmatrix.hh:753
Iterator end()
end iterator
Definition: diagonalmatrix.hh:859
DiagonalMatrix(const FieldVector< K, n > &diag)
Constructor initializing the diagonal with a vector.
Definition: diagonalmatrix.hh:105
FieldVector< K, n > & diagonal()
Get reference to diagonal vector.
Definition: diagonalmatrix.hh:513
K & operator[](size_type i)
random access
Definition: diagonalmatrix.hh:838
Definition: diagonalmatrix.hh:28
ConstIterator beforeBegin() const
Definition: diagonalmatrix.hh:739
ConstIterator end() const
end iterator
Definition: diagonalmatrix.hh:176
The number of columns.
Definition: diagonalmatrix.hh:84
The size of this vector.
Definition: diagonalmatrix.hh:677
row_type reference
Definition: diagonalmatrix.hh:73
size_type rows() const
number of rows
Definition: densematrix.hh:704
K block_type
export the type representing the components
Definition: diagonalmatrix.hh:663
DiagonalRowVector< K, n > type
Definition: diagonalmatrix.hh:931
K & diagonal(size_type i)
Get reference to diagonal entry.
Definition: diagonalmatrix.hh:501
size_type rowIndex() const
index of this row in surrounding matrix
Definition: diagonalmatrix.hh:765
void usmv(const K &alpha, const X &x, Y &y) const
y += alpha A x
Definition: diagonalmatrix.hh:351
DiagonalMatrixWrapper(const DiagonalMatrixType *mat)
Definition: diagonalmatrix.hh:615
DiagonalRowVectorConst< K, n > type
Definition: diagonalmatrix.hh:919
Iterator beforeEnd()
Definition: diagonalmatrix.hh:147
Iterator end()
end iterator
Definition: diagonalmatrix.hh:140
void istl_assign_to_fmatrix(DenseMatrix &denseMatrix, const K(&values)[M][N])
Definition: densematrix.hh:78
ContainerWrapperIterator< DiagonalRowVectorConst< K, n >, const K, const K & > ConstIterator
ConstIterator class for sequential access.
Definition: diagonalmatrix.hh:714
double frobenius_norm2() const
square of frobenius norm, need for block recursion
Definition: diagonalmatrix.hh:394
K block_type
export the type representing the components
Definition: diagonalmatrix.hh:60
DiagonalMatrix & operator+=(const DiagonalMatrix &y)
vector space addition
Definition: diagonalmatrix.hh:200
DiagonalRowVector< K, n > row_type
Each row is implemented by a field vector.
Definition: diagonalmatrix.hh:72
bool operator==(const DiagonalRowVectorConst &y) const
Binary vector comparison.
Definition: diagonalmatrix.hh:745
ContainerWrapperIterator(CW containerWrapper, int position)
Definition: diagonalmatrix.hh:977
row_type::Iterator ColIterator
rename the iterators for easier access
Definition: diagonalmatrix.hh:131
bool operator!=(const DiagonalMatrix &other) const
incomparison operator
Definition: diagonalmatrix.hh:250
T * operator->() const
Definition: diagonalmatrix.hh:1007
FieldTraits< value_type >::real_type infinity_norm_real() const
simplified infinity norm (uses Manhattan norm for complex values)
Definition: densevector.hh:553