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
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++)
490 friend std::ostream& operator<< (std::ostream& s, const DiagonalMatrix<K,n>& a)
494 s << ((i==j) ? a.diag_[i] : 0) <<
" ";
503 return reference(const_cast<K*>(&diag_[i]), i);
542 #ifndef DOXYGEN // hide specialization
546 class DiagonalMatrix<K, 1> :
public FieldMatrix<K, 1, 1>
548 typedef FieldMatrix<K,1,1> Base;
551 typedef typename Base::size_type
size_type;
560 typedef typename Base::row_type
row_type;
583 (*this)[0][0] = scalar;
589 return (*
this)[0][0];
595 return (*
this)[0][0];
599 const FieldVector<K,1>&
diagonal()
const
614 template<
class DiagonalMatrixType>
615 class DiagonalMatrixWrapper
617 typedef typename DiagonalMatrixType::reference reference;
618 typedef typename DiagonalMatrixType::const_reference const_reference;
619 typedef typename DiagonalMatrixType::field_type K;
620 typedef DiagonalRowVector<K, DiagonalMatrixType::rows> row_type;
621 typedef std::size_t size_type;
622 typedef DiagonalMatrixWrapper< DiagonalMatrixType> MyType;
634 mat_(const_cast<DiagonalMatrixType*>(mat))
644 row_ =
row_type(&(mat_->diagonal(i)), i);
650 return mat_==other.mat_;
655 mutable DiagonalMatrixType* mat_;
656 mutable row_type row_;
662 template<
class K,
int n >
663 class DiagonalRowVectorConst
665 template<
class DiagonalMatrixType>
724 return ((
p_ == other.p_)and (
row_ == other.row_));
761 return ((
p_==y.p_)and (
row_==y.row_));
799 return const_cast<K*
>(
p_);
812 template<
class K,
int n >
815 template<
class DiagonalMatrixType>
922 template<
class K,
int n>
928 template<
class K,
int n>
934 template<
class K,
int n>
940 template<
class K,
int n>
970 template<
class CW,
class T,
class R>
973 typedef typename std::remove_const<CW>::type NonConstCW;
990 containerWrapper_(containerWrapper),
994 template<
class OtherContainerWrapperIteratorType>
996 containerWrapper_(other.containerWrapper_),
997 position_(other.position_)
1001 containerWrapper_(other.containerWrapper_),
1002 position_(other.position_)
1006 containerWrapper_(other.containerWrapper_),
1007 position_(other.position_)
1010 template<
class OtherContainerWrapperIteratorType>
1013 containerWrapper_ = other.containerWrapper_;
1014 position_ = other.position_;
1022 return containerWrapper_.pointer(position_);
1028 return position_ == other.position_ && containerWrapper_.identical(other.containerWrapper_);
1033 return position_ == other.position_ && containerWrapper_.identical(other.containerWrapper_);
1038 return *containerWrapper_.pointer(position_);
1055 return *containerWrapper_.pointer(position_+i);
1060 position_=position_+n;
1063 template<
class OtherContainerWrapperIteratorType>
1064 std::ptrdiff_t
distanceTo(OtherContainerWrapperIteratorType& other)
const
1066 assert(containerWrapper_.identical(other));
1067 return other.position_ - position_;
1072 return containerWrapper_.realIndex(position_);
1076 NonConstCW containerWrapper_;
1080 template <
class DenseMatrix,
class field,
int N>
1086 denseMatrix = field(0);
1087 for (
int i = 0; i < N; ++i)
1088 denseMatrix[i][i] = rhs.diagonal()[i];
ConstIterator beforeEnd() const
Definition: diagonalmatrix.hh:746
DiagonalRowVector(K *p, int col)
Constructor making vector with identical coordinates.
Definition: diagonalmatrix.hh:838
DiagonalMatrix(const FieldVector< K, n > &diag)
Constructor initializing the diagonal with a vector.
Definition: diagonalmatrix.hh:108
bool operator!=(const DiagonalMatrix &other) const
incomparison operator
Definition: diagonalmatrix.hh:267
T field_type
export the type representing the field
Definition: ftraits.hh:26
size_type row_
Definition: diagonalmatrix.hh:809
size_type size() const
Definition: diagonalmatrix.hh:92
A diagonal matrix of static size.
Definition: diagonalmatrix.hh:51
void decrement()
Definition: diagonalmatrix.hh:1047
K * pointer(size_type i) const
Definition: diagonalmatrix.hh:797
ContainerWrapperIterator(CW containerWrapper, int position)
Definition: diagonalmatrix.hh:989
Iterator begin()
begin iterator
Definition: diagonalmatrix.hh:865
Iterator beforeEnd()
Definition: diagonalmatrix.hh:164
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
ConstIterator beforeBegin() const
Definition: diagonalmatrix.hh:207
DiagonalMatrix(std::initializer_list< K > const &l)
Construct diagonal matrix from an initializer list.
Definition: diagonalmatrix.hh:120
FieldVector< K, n > & diagonal()
Get reference to diagonal vector.
Definition: diagonalmatrix.hh:531
Iterator end()
end iterator
Definition: diagonalmatrix.hh:871
DiagonalRowVectorConst()
Constructor making uninitialized vector.
Definition: diagonalmatrix.hh:699
size_type N() const
number of rows
Definition: densematrix.hh:671
Iterator iterator
typedef for stl compliant access
Definition: diagonalmatrix.hh:862
bool identical(const DiagonalMatrix< K, n > &other) const
Check if matrix is the same object as the other matrix.
Definition: diagonalmatrix.hh:135
const K & operator[](size_type i) const
same for read only access
Definition: diagonalmatrix.hh:713
Error thrown if operations of a FieldMatrix fail.
Definition: densematrix.hh:140
DiagonalMatrix(const K &k)
Constructor initializing the whole matrix with a scalar.
Definition: diagonalmatrix.hh:103
Definition of the DUNE_UNUSED macro for the case that config.h is not available.
const_row_type const_row_reference
Definition: diagonalmatrix.hh:80
ConstIterator const_iterator
typedef for stl compliant access
Definition: diagonalmatrix.hh:180
ConstIterator ConstRowIterator
rename the iterators for easier access
Definition: diagonalmatrix.hh:182
FieldTraits< value_type >::real_type two_norm() const
two norm sqrt(sum over squared values of entries)
Definition: densevector.hh:590
K block_type
export the type representing the components
Definition: diagonalmatrix.hh:828
size_type M() const
number of blocks in column direction
Definition: diagonalmatrix.hh:470
you have to specialize this structure for any type that should be assignable to a DenseMatrix ...
Definition: densematrix.hh:74
ContainerWrapperIterator< DiagonalRowVector< K, n >, K, K & > Iterator
Iterator class for sequential access.
Definition: diagonalmatrix.hh:860
Iterator beforeBegin()
Definition: diagonalmatrix.hh:171
const_reference operator[](size_type i) const
Return const_reference object as row replacement.
Definition: diagonalmatrix.hh:507
K block_type
export the type representing the components
Definition: diagonalmatrix.hh:63
Iterator iterator
typedef for stl compliant access
Definition: diagonalmatrix.hh:144
bool operator==(const DiagonalRowVectorConst &y) const
Binary vector comparison.
Definition: diagonalmatrix.hh:759
ConstIterator end() const
end iterator
Definition: diagonalmatrix.hh:193
std::size_t size_type
The type used for the index access and size operation.
Definition: diagonalmatrix.hh:831
void usmhv(const typename FieldTraits< Y >::field_type &alpha, const X &x, Y &y) const
y += alpha A^H x
Definition: diagonalmatrix.hh:394
K conjugateComplex(const K &x)
compute conjugate complex of x
Definition: math.hh:103
std::size_t size_type
The type used for the index access and size operations.
Definition: diagonalmatrix.hh:66
A few common exception classes.
Iterator begin()
begin iterator
Definition: densevector.hh:308
DiagonalRowVectorConst< K, n > const_row_type
Definition: diagonalmatrix.hh:78
void usmtv(const typename FieldTraits< Y >::field_type &alpha, const X &x, Y &y) const
y += alpha A^T x
Definition: diagonalmatrix.hh:381
Iterator RowIterator
rename the iterators for easier access
Definition: diagonalmatrix.hh:146
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:599
Implements a matrix constructed from a given type representing a field and a compile-time given numbe...
void advance(int n)
Definition: diagonalmatrix.hh:1058
DiagonalMatrix & operator=(const K &k)
Assignment from a scalar.
Definition: diagonalmatrix.hh:128
double infinity_norm() const
infinity norm (row sum norm, how to generalize for blocks?)
Definition: diagonalmatrix.hh:420
DiagonalRowVector & operator=(const K &k)
Assignment operator for scalar.
Definition: diagonalmatrix.hh:843
K * p_
Definition: diagonalmatrix.hh:808
#define DUNE_ASSERT_BOUNDS(cond)
If DUNE_CHECK_BOUNDS is defined: check if condition cond holds; otherwise, do nothing.
Definition: boundschecking.hh:28
const_row_type const_reference
Definition: diagonalmatrix.hh:79
const FieldVector< K, n > & diagonal() const
Get const reference to diagonal vector.
Definition: diagonalmatrix.hh:525
Iterator beforeEnd()
Definition: diagonalmatrix.hh:878
K determinant() const
calculates the determinant of this matrix
Definition: diagonalmatrix.hh:451
double infinity_norm_real() const
simplified infinity norm (uses Manhattan norm for complex values)
Definition: diagonalmatrix.hh:426
ConstIterator const_iterator
typedef for stl compliant access
Definition: diagonalmatrix.hh:730
row_type reference
Definition: diagonalmatrix.hh:76
void mmv(const X &x, Y &y) const
y -= A x
Definition: diagonalmatrix.hh:332
Facade class for stl conformant bidirectional iterators.
Definition: iteratorfacades.hh:269
DiagonalMatrix & operator*=(const K &k)
vector space multiplication with scalar
Definition: diagonalmatrix.hh:245
Definition: diagonalmatrix.hh:32
Macro for wrapping boundary checks.
Definition: diagonalmatrix.hh:671
bool equals(const MyConstType &other) const
Definition: diagonalmatrix.hh:1031
row_type * pointer(int i) const
Definition: diagonalmatrix.hh:642
The number of columns.
Definition: diagonalmatrix.hh:87
DiagonalMatrixWrapper()
Definition: diagonalmatrix.hh:629
ConstIterator const_iterator
typedef for stl compliant access
Definition: diagonalmatrix.hh:893
The number of block levels we contain. This is 1.
Definition: diagonalmatrix.hh:71
DiagonalRowVectorConst(K *p, int col)
Constructor making vector with identical coordinates.
Definition: diagonalmatrix.hh:705
void mmhv(const X &x, Y &y) const
y -= A^H x
Definition: diagonalmatrix.hh:356
DiagonalMatrix & operator/=(const K &k)
vector space division by scalar
Definition: diagonalmatrix.hh:252
K field_type
export the type representing the field
Definition: diagonalmatrix.hh:825
K & diagonal(size_type i)
Get reference to diagonal entry.
Definition: diagonalmatrix.hh:519
void solve(V &x, const V &b) const
Solve system A x = b.
Definition: diagonalmatrix.hh:437
bool identical(const DiagonalRowVectorConst< K, n > &other) const
Definition: diagonalmatrix.hh:722
std::ptrdiff_t index() const
Definition: diagonalmatrix.hh:1070
void mmtv(const X &x, Y &y) const
y -= A^T x
Definition: diagonalmatrix.hh:344
Iterator end()
end iterator
Definition: diagonalmatrix.hh:157
Iterator begin()
begin iterator
Definition: diagonalmatrix.hh:151
DiagonalRowVector()
Constructor making uninitialized vector.
Definition: diagonalmatrix.hh:834
DiagonalMatrix & operator+=(const DiagonalMatrix &y)
vector space addition
Definition: diagonalmatrix.hh:217
DiagonalRowVector< K, n > row_type
Each row is implemented by a field vector.
Definition: diagonalmatrix.hh:75
R dereference() const
Definition: diagonalmatrix.hh:1036
void invert()
Compute inverse.
Definition: diagonalmatrix.hh:444
A dense n x m matrix.
Definition: densematrix.hh:25
ConstIterator begin() const
begin ConstIterator
Definition: diagonalmatrix.hh:733
const K & diagonal() const
the diagonal value
Definition: diagonalmatrix.hh:785
DiagonalRowVector * operator&()
Definition: diagonalmatrix.hh:909
size_type rowIndex() const
index of this row in surrounding matrix
Definition: diagonalmatrix.hh:779
size_type realIndex(int i) const
Definition: diagonalmatrix.hh:792
bool operator==(const DiagonalMatrix &other) const
comparison operator
Definition: diagonalmatrix.hh:261
Get the 'const' version of a reference to a mutable object.
Definition: genericiterator.hh:84
size_type N() const
number of blocks in the vector (are of size 1 here)
Definition: diagonalmatrix.hh:767
double frobenius_norm2() const
square of frobenius norm, need for block recursion
Definition: diagonalmatrix.hh:414
ConstIterator beforeBegin() const
Definition: diagonalmatrix.hh:753
get the 'mutable' version of a reference to a const object
Definition: genericiterator.hh:113
ContainerWrapperIterator(OtherContainerWrapperIteratorType &other)
Definition: diagonalmatrix.hh:995
const K & diagonal(size_type i) const
Get const reference to diagonal entry.
Definition: diagonalmatrix.hh:513
ConstIterator end() const
end ConstIterator
Definition: diagonalmatrix.hh:739
K block_type
export the type representing the components
Definition: diagonalmatrix.hh:681
void umhv(const X &x, Y &y) const
y += A^H x
Definition: diagonalmatrix.hh:320
The number of rows.
Definition: diagonalmatrix.hh:85
ConstIterator beforeEnd() const
Definition: diagonalmatrix.hh:200
DiagonalRowVectorConst< K, n > type
Definition: diagonalmatrix.hh:925
DiagonalMatrix & operator-=(const DiagonalMatrix &y)
vector space subtraction
Definition: diagonalmatrix.hh:224
Implements a matrix constructed from a given type representing a field and compile-time given number ...
void usmv(const typename FieldTraits< Y >::field_type &alpha, const X &x, Y &y) const
y += alpha A x
Definition: diagonalmatrix.hh:368
bool identical(const DiagonalMatrixWrapper &other) const
Definition: diagonalmatrix.hh:648
size_type dim() const
dimension of the vector space
Definition: diagonalmatrix.hh:773
DiagonalMatrixWrapper(const DiagonalMatrixType *mat)
Definition: diagonalmatrix.hh:633
std::ptrdiff_t distanceTo(OtherContainerWrapperIteratorType &other) const
Definition: diagonalmatrix.hh:1064
DiagonalRowVector< K, n > type
Definition: diagonalmatrix.hh:937
ContainerWrapperIterator< DiagonalRowVectorConst< K, n >, const K, const K & > ConstIterator
ConstIterator class for sequential access.
Definition: diagonalmatrix.hh:728
ContainerWrapperIterator< const WrapperType, reference, reference > Iterator
Iterator class for sequential access.
Definition: diagonalmatrix.hh:142
std::size_t position_
The current position in the buffer.
Definition: variablesizecommunicator.hh:136
ContainerWrapperIterator< DiagonalRowVectorConst< K, n >, const K, const K & > ConstIterator
ConstIterator class for sequential access.
Definition: diagonalmatrix.hh:891
bool equals(const MyType &other) const
Definition: diagonalmatrix.hh:1026
FieldTraits< vt >::real_type infinity_norm() const
infinity norm (maximum of absolute values of entries)
Definition: densevector.hh:610
Traits for type conversions and type information.
void increment()
Definition: diagonalmatrix.hh:1041
DiagonalRowVector< K, n > type
Definition: diagonalmatrix.hh:943
std::size_t size_type
The type used for the index access and size operation.
Definition: diagonalmatrix.hh:684
double frobenius_norm() const
frobenius norm: sqrt(sum over squared values of entries)
Definition: diagonalmatrix.hh:408
reference operator[](size_type i)
Return reference object as row replacement.
Definition: diagonalmatrix.hh:501
Definition: diagonalmatrix.hh:33
ContainerWrapperIterator(const MyConstType &other)
Definition: diagonalmatrix.hh:1005
DiagonalRowVectorConst< K, n > type
Definition: diagonalmatrix.hh:931
DiagonalRowVectorConst * operator&()
Definition: diagonalmatrix.hh:802
size_type N() const
number of blocks in row direction
Definition: diagonalmatrix.hh:464
Iterator class for sparse vector-like containers.
Definition: diagonalmatrix.hh:34
ContainerWrapperIterator< const WrapperType, const_reference, const_reference > ConstIterator
Iterator class for sequential access.
Definition: diagonalmatrix.hh:178
Implements a generic iterator class for writing stl conformant iterators.
const_row_type::ConstIterator ConstColIterator
rename the iterators for easier access
Definition: diagonalmatrix.hh:184
size_type realIndex(int i) const
Definition: diagonalmatrix.hh:637
void mtv(const X &x, Y &y) const
y = A^T x
Definition: diagonalmatrix.hh:289
row_type row_reference
Definition: diagonalmatrix.hh:77
ConstIterator begin() const
begin iterator
Definition: diagonalmatrix.hh:187
#define DUNE_UNUSED_PARAMETER(parm)
A macro to mark intentionally unused function parameters with.
Definition: unused.hh:18
The number of block levels we contain.
Definition: diagonalmatrix.hh:689
Implements a vector constructed from a given type representing a field and a compile-time given size...
ContainerWrapperIterator & operator=(OtherContainerWrapperIteratorType &other)
Definition: diagonalmatrix.hh:1011
DiagonalMatrix()
Default constructor.
Definition: diagonalmatrix.hh:100
R elementAt(int i) const
Definition: diagonalmatrix.hh:1053
void mv(const X &x, Y &y) const
y = A x
Definition: diagonalmatrix.hh:277
K field_type
export the type representing the field
Definition: diagonalmatrix.hh:678
row_type::Iterator ColIterator
rename the iterators for easier access
Definition: diagonalmatrix.hh:148
value_type field_type
Definition: diagonalmatrix.hh:60
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:1082
K & operator[](size_type i)
random access
Definition: diagonalmatrix.hh:852
void umtv(const X &x, Y &y) const
y += A^T x
Definition: diagonalmatrix.hh:308
FieldTraits< vt >::real_type infinity_norm_real() const
simplified infinity norm (uses Manhattan norm for complex values)
Definition: densevector.hh:626
K value_type
export the type representing the field
Definition: diagonalmatrix.hh:59
The size of this vector.
Definition: diagonalmatrix.hh:695
ContainerWrapperIterator(const MyType &other)
Definition: diagonalmatrix.hh:1000
size_type M() const
number of columns
Definition: densematrix.hh:677
Definition: diagonalmatrix.hh:31
void umv(const X &x, Y &y) const
y += A x
Definition: diagonalmatrix.hh:296
T * operator->() const
Definition: diagonalmatrix.hh:1020
Iterator beforeBegin()
Definition: diagonalmatrix.hh:885