3#ifndef DUNE_DIAGONAL_MATRIX_HH
4#define DUNE_DIAGONAL_MATRIX_HH
15#include <initializer_list>
31 template<
class K,
int n >
class DiagonalRowVectorConst;
32 template<
class K,
int n >
class DiagonalRowVector;
33 template<
class DiagonalMatrixType >
class DiagonalMatrixWrapper;
34 template<
class C,
class T,
class R>
class ContainerWrapperIterator;
50 template<
class K,
int n>
122 std::copy_n(l.begin(), std::min(
static_cast<std::size_t
>(
rows),
137 return (
this==&other);
153 return Iterator(WrapperType(
this),0);
159 return Iterator(WrapperType(
this),n);
166 return Iterator(WrapperType(
this),n-1);
173 return Iterator(WrapperType(
this),-1);
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++)
494 s << ((i==j) ? a.diag_[i] : 0) <<
" ";
503 return reference(
const_cast<K*
>(&diag_[i]), i);
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>
617 typedef typename DiagonalMatrixType::reference reference;
618 typedef typename DiagonalMatrixType::const_reference const_reference;
619 typedef typename DiagonalMatrixType::field_type K;
621 typedef std::size_t size_type;
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 >
665 template<
class DiagonalMatrixType>
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];
Implements a generic iterator class for writing stl conformant iterators.
#define DUNE_ASSERT_BOUNDS(cond)
Definition boundschecking.hh:20
Implements a vector constructed from a given type representing a field and a compile-time given size.
Traits for type conversions and type information.
Implements a matrix constructed from a given type representing a field and a compile-time given numbe...
Implements a matrix constructed from a given type representing a field and compile-time given number ...
A few common exception classes.
Definition of the DUNE_UNUSED macro for the case that config.h is not available.
#define DUNE_UNUSED_PARAMETER(parm)
A macro to mark intentionally unused function parameters with.
Definition unused.hh:18
ConstIterator beforeBegin() const
Definition diagonalmatrix.hh:207
void mmhv(const X &x, Y &y) const
y -= A^H x
Definition diagonalmatrix.hh:356
DiagonalMatrix & operator*=(const K &k)
vector space multiplication with scalar
Definition diagonalmatrix.hh:245
std::size_t size_type
The type used for the index access and size operations.
Definition diagonalmatrix.hh:66
size_type dim() const
dimension of the vector space
Definition diagonalmatrix.hh:773
ConstIterator ConstRowIterator
rename the iterators for easier access
Definition diagonalmatrix.hh:182
size_type size() const
Definition diagonalmatrix.hh:92
row_type row_reference
Definition diagonalmatrix.hh:77
K & operator[](size_type i)
random access
Definition diagonalmatrix.hh:852
ContainerWrapperIterator & operator=(OtherContainerWrapperIteratorType &other)
Definition diagonalmatrix.hh:1011
FieldVector< K, n > & diagonal()
Get reference to diagonal vector.
Definition diagonalmatrix.hh:531
DiagonalMatrix()
Default constructor.
Definition diagonalmatrix.hh:100
void usmhv(const typename FieldTraits< Y >::field_type &alpha, const X &x, Y &y) const
y += alpha A^H x
Definition diagonalmatrix.hh:394
Iterator iterator
typedef for stl compliant access
Definition diagonalmatrix.hh:862
void increment()
Definition diagonalmatrix.hh:1041
DiagonalRowVectorConst< K, n > type
Definition diagonalmatrix.hh:931
T * operator->() const
Definition diagonalmatrix.hh:1020
K * pointer(size_type i) const
Definition diagonalmatrix.hh:797
K field_type
export the type representing the field
Definition diagonalmatrix.hh:678
DiagonalRowVector< K, n > type
Definition diagonalmatrix.hh:937
ConstIterator beforeEnd() const
Definition diagonalmatrix.hh:746
const_row_type::ConstIterator ConstColIterator
rename the iterators for easier access
Definition diagonalmatrix.hh:184
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:178
const_row_type const_row_reference
Definition diagonalmatrix.hh:80
size_type row_
Definition diagonalmatrix.hh:809
ContainerWrapperIterator(CW containerWrapper, int position)
Definition diagonalmatrix.hh:989
DiagonalRowVector(K *p, int col)
Constructor making vector with identical coordinates.
Definition diagonalmatrix.hh:838
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
Iterator beforeBegin()
Definition diagonalmatrix.hh:885
size_type M() const
number of blocks in column direction
Definition diagonalmatrix.hh:470
ContainerWrapperIterator(OtherContainerWrapperIteratorType &other)
Definition diagonalmatrix.hh:995
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:144
ConstIterator begin() const
begin ConstIterator
Definition diagonalmatrix.hh:733
ConstIterator const_iterator
typedef for stl compliant access
Definition diagonalmatrix.hh:893
bool identical(const DiagonalRowVectorConst< K, n > &other) const
Definition diagonalmatrix.hh:722
DiagonalMatrix & operator-=(const DiagonalMatrix &y)
vector space subtraction
Definition diagonalmatrix.hh:224
DiagonalRowVectorConst(K *p, int col)
Constructor making vector with identical coordinates.
Definition diagonalmatrix.hh:705
void mmtv(const X &x, Y &y) const
y -= A^T x
Definition diagonalmatrix.hh:344
row_type * pointer(int i) const
Definition diagonalmatrix.hh:642
DiagonalMatrix(const K &k)
Constructor initializing the whole matrix with a scalar.
Definition diagonalmatrix.hh:103
ContainerWrapperIterator< DiagonalRowVector< K, n >, K, K & > Iterator
Iterator class for sequential access.
Definition diagonalmatrix.hh:860
R elementAt(int i) const
Definition diagonalmatrix.hh:1053
std::ptrdiff_t distanceTo(OtherContainerWrapperIteratorType &other) const
Definition diagonalmatrix.hh:1064
Iterator beforeEnd()
Definition diagonalmatrix.hh:878
void umtv(const X &x, Y &y) const
y += A^T x
Definition diagonalmatrix.hh:308
ConstIterator const_iterator
typedef for stl compliant access
Definition diagonalmatrix.hh:730
double infinity_norm_real() const
simplified infinity norm (uses Manhattan norm for complex values)
Definition diagonalmatrix.hh:426
DiagonalMatrixWrapper(const DiagonalMatrixType *mat)
Definition diagonalmatrix.hh:633
void umv(const X &x, Y &y) const
y += A x
Definition diagonalmatrix.hh:296
ContainerWrapperIterator< const WrapperType, reference, reference > Iterator
Iterator class for sequential access.
Definition diagonalmatrix.hh:142
DiagonalRowVector< K, n > type
Definition diagonalmatrix.hh:943
void mv(const X &x, Y &y) const
y = A x
Definition diagonalmatrix.hh:277
double frobenius_norm() const
frobenius norm: sqrt(sum over squared values of entries)
Definition diagonalmatrix.hh:408
ConstIterator end() const
end iterator
Definition diagonalmatrix.hh:193
K * p_
Definition diagonalmatrix.hh:808
size_type rowIndex() const
index of this row in surrounding matrix
Definition diagonalmatrix.hh:779
R dereference() const
Definition diagonalmatrix.hh:1036
DiagonalRowVector & operator=(const K &k)
Assignment operator for scalar.
Definition diagonalmatrix.hh:843
bool operator!=(const DiagonalMatrix &other) const
incomparison operator
Definition diagonalmatrix.hh:267
value_type field_type
Definition diagonalmatrix.hh:60
void advance(int n)
Definition diagonalmatrix.hh:1058
ConstIterator begin() const
begin iterator
Definition diagonalmatrix.hh:187
Iterator begin()
begin iterator
Definition diagonalmatrix.hh:865
void mmv(const X &x, Y &y) const
y -= A x
Definition diagonalmatrix.hh:332
DiagonalMatrix & operator/=(const K &k)
vector space division by scalar
Definition diagonalmatrix.hh:252
ConstIterator const_iterator
typedef for stl compliant access
Definition diagonalmatrix.hh:180
const_row_type const_reference
Definition diagonalmatrix.hh:79
bool identical(const DiagonalMatrix< K, n > &other) const
Check if matrix is the same object as the other matrix.
Definition diagonalmatrix.hh:135
size_type realIndex(int i) const
Definition diagonalmatrix.hh:637
Iterator end()
end iterator
Definition diagonalmatrix.hh:157
const K & operator[](size_type i) const
same for read only access
Definition diagonalmatrix.hh:713
DiagonalRowVector()
Constructor making uninitialized vector.
Definition diagonalmatrix.hh:834
row_type reference
Definition diagonalmatrix.hh:76
DiagonalRowVectorConst * operator&()
Definition diagonalmatrix.hh:802
bool operator==(const DiagonalRowVectorConst &y) const
Binary vector comparison.
Definition diagonalmatrix.hh:759
double frobenius_norm2() const
square of frobenius norm, need for block recursion
Definition diagonalmatrix.hh:414
void mtv(const X &x, Y &y) const
y = A^T x
Definition diagonalmatrix.hh:289
ContainerWrapperIterator< DiagonalRowVectorConst< K, n >, const K, const K & > ConstIterator
ConstIterator class for sequential access.
Definition diagonalmatrix.hh:891
bool identical(const DiagonalMatrixWrapper &other) const
Definition diagonalmatrix.hh:648
void usmtv(const typename FieldTraits< Y >::field_type &alpha, const X &x, Y &y) const
y += alpha A^T x
Definition diagonalmatrix.hh:381
void decrement()
Definition diagonalmatrix.hh:1047
std::ptrdiff_t index() const
Definition diagonalmatrix.hh:1070
const FieldVector< K, n > & diagonal() const
Get const reference to diagonal vector.
Definition diagonalmatrix.hh:525
void invert()
Compute inverse.
Definition diagonalmatrix.hh:444
DiagonalRowVector * operator&()
Definition diagonalmatrix.hh:909
const K & diagonal(size_type i) const
Get const reference to diagonal entry.
Definition diagonalmatrix.hh:513
size_type N() const
number of blocks in the vector (are of size 1 here)
Definition diagonalmatrix.hh:767
DiagonalMatrix(std::initializer_list< K > const &l)
Construct diagonal matrix from an initializer list.
Definition diagonalmatrix.hh:120
row_type::Iterator ColIterator
rename the iterators for easier access
Definition diagonalmatrix.hh:148
K field_type
export the type representing the field
Definition diagonalmatrix.hh:825
reference operator[](size_type i)
Return reference object as row replacement.
Definition diagonalmatrix.hh:501
DiagonalRowVectorConst< K, n > type
Definition diagonalmatrix.hh:925
size_type N() const
number of blocks in row direction
Definition diagonalmatrix.hh:464
Iterator end()
end iterator
Definition diagonalmatrix.hh:871
DiagonalMatrix(const FieldVector< K, n > &diag)
Constructor initializing the diagonal with a vector.
Definition diagonalmatrix.hh:108
std::size_t size_type
The type used for the index access and size operation.
Definition diagonalmatrix.hh:831
void usmv(const typename FieldTraits< Y >::field_type &alpha, const X &x, Y &y) const
y += alpha A x
Definition diagonalmatrix.hh:368
double infinity_norm() const
infinity norm (row sum norm, how to generalize for blocks?)
Definition diagonalmatrix.hh:420
DiagonalMatrix & operator+=(const DiagonalMatrix &y)
vector space addition
Definition diagonalmatrix.hh:217
K block_type
export the type representing the components
Definition diagonalmatrix.hh:63
void umhv(const X &x, Y &y) const
y += A^H x
Definition diagonalmatrix.hh:320
Iterator beforeBegin()
Definition diagonalmatrix.hh:171
static void apply(DenseMatrix &denseMatrix, DiagonalMatrix< field, N > const &rhs)
Definition diagonalmatrix.hh:1082
ContainerWrapperIterator(const MyType &other)
Definition diagonalmatrix.hh:1000
ConstIterator end() const
end ConstIterator
Definition diagonalmatrix.hh:739
std::size_t size_type
The type used for the index access and size operation.
Definition diagonalmatrix.hh:684
DiagonalMatrix & operator=(const K &k)
Assignment from a scalar.
Definition diagonalmatrix.hh:128
DiagonalRowVectorConst()
Constructor making uninitialized vector.
Definition diagonalmatrix.hh:699
Iterator beforeEnd()
Definition diagonalmatrix.hh:164
size_type realIndex(int i) const
Definition diagonalmatrix.hh:792
friend std::ostream & operator<<(std::ostream &s, const DiagonalMatrix< K, n > &a)
Sends the matrix to an output stream.
Definition diagonalmatrix.hh:490
K value_type
export the type representing the field
Definition diagonalmatrix.hh:59
K block_type
export the type representing the components
Definition diagonalmatrix.hh:681
K determinant() const
calculates the determinant of this matrix
Definition diagonalmatrix.hh:451
ConstIterator beforeBegin() const
Definition diagonalmatrix.hh:753
DiagonalMatrixWrapper()
Definition diagonalmatrix.hh:629
ContainerWrapperIterator< DiagonalRowVectorConst< K, n >, const K, const K & > ConstIterator
ConstIterator class for sequential access.
Definition diagonalmatrix.hh:728
Iterator begin()
begin iterator
Definition diagonalmatrix.hh:151
bool operator==(const DiagonalMatrix &other) const
comparison operator
Definition diagonalmatrix.hh:261
DiagonalRowVectorConst< K, n > const_row_type
Definition diagonalmatrix.hh:78
DiagonalRowVector< K, n > row_type
Each row is implemented by a field vector.
Definition diagonalmatrix.hh:75
bool equals(const MyType &other) const
Definition diagonalmatrix.hh:1026
Iterator RowIterator
rename the iterators for easier access
Definition diagonalmatrix.hh:146
K block_type
export the type representing the components
Definition diagonalmatrix.hh:828
ConstIterator beforeEnd() const
Definition diagonalmatrix.hh:200
const K & diagonal() const
the diagonal value
Definition diagonalmatrix.hh:785
@ cols
The number of columns.
Definition diagonalmatrix.hh:87
@ rows
The number of rows.
Definition diagonalmatrix.hh:85
@ dimension
Definition diagonalmatrix.hh:671
@ size
The size of this vector.
Definition diagonalmatrix.hh:695
@ blocklevel
The number of block levels we contain. This is 1.
Definition diagonalmatrix.hh:71
@ blocklevel
The number of block levels we contain.
Definition diagonalmatrix.hh:689
#define DUNE_THROW(E, m)
Definition exceptions.hh:216
Dune namespace.
Definition alignment.hh:11
K conjugateComplex(const K &x)
compute conjugate complex of x
Definition math.hh:103
A dense n x m matrix.
Definition densematrix.hh:135
size_type M() const
number of columns
Definition densematrix.hh:658
size_type N() const
number of rows
Definition densematrix.hh:652
vector space out of a tensor product of fields.
Definition fvector.hh:93
you have to specialize this structure for any type that should be assignable to a DenseMatrix
Definition densematrix.hh:73
Error thrown if operations of a FieldMatrix fail.
Definition densematrix.hh:121
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
FieldTraits< value_type >::real_type two_norm() const
two norm sqrt(sum over squared values of entries)
Definition densevector.hh:590
Iterator begin()
begin iterator
Definition densevector.hh:308
FieldTraits< vt >::real_type infinity_norm() const
infinity norm (maximum of absolute values of entries)
Definition densevector.hh:610
FieldTraits< vt >::real_type infinity_norm_real() const
simplified infinity norm (uses Manhattan norm for complex values)
Definition densevector.hh:626
Definition diagonalmatrix.hh:664
Definition diagonalmatrix.hh:814
Definition diagonalmatrix.hh:616
Iterator class for sparse vector-like containers.
Definition diagonalmatrix.hh:972
ContainerWrapperIterator(const MyConstType &other)
Definition diagonalmatrix.hh:1005
bool equals(const MyConstType &other) const
Definition diagonalmatrix.hh:1031
A diagonal matrix of static size.
Definition diagonalmatrix.hh:52
T field_type
export the type representing the field
Definition ftraits.hh:26
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:275