5#ifndef DUNE_ISTL_MATRIX_HH
6#define DUNE_ISTL_MATRIX_HH
15#include <dune/common/ftraits.hh>
16#include <dune/common/typetraits.hh>
17#include <dune/common/scalarvectorview.hh>
18#include <dune/common/scalarmatrixview.hh>
39 template<
class B,
class A=std::allocator<B> >
40 class DenseMatrixBase :
public Imp::block_vector_unmanaged<B,typename A::size_type>
50 using field_type =
typename Imp::BlockTraits<B>::field_type;
98 this->n = rows*columns;
102 this->p = allocator_.allocate(this->n);
103 new (this->p)B[this->n];
120 columns_ = a.columns_;
124 this->p = allocator_.allocate(this->n);
125 new (this->p)B[this->n];
148 allocator_.deallocate(this->p,this->n);
160 allocator_.deallocate(this->p,this->n);
164 this->n = rows*columns;
167 this->p = allocator_.allocate(this->n);
168 new (this->p)B[this->n];
186 columns_ = a.columns_;
189 if (this->n!=a.n || rows_!=a.rows_)
196 allocator_.deallocate(this->p,this->n);
204 this->p = allocator_.allocate(this->n);
205 new (this->p)B[this->n];
231 (
static_cast<Imp::block_vector_unmanaged<B,size_type>&
>(*this)) = k;
243#ifdef DUNE_ISTL_WITH_CHECKING
244 if (i>=rows_) DUNE_THROW(
ISTLError,
"index out of range");
246 return window_type(this->p + i*columns_, columns_);
252#ifdef DUNE_ISTL_WITH_CHECKING
253 if (i<0 || i>=rows_) DUNE_THROW(
ISTLError,
"index out of range");
255 return window_type(this->p + i*columns_, columns_);
278 window_(data + _i*columns, columns)
286 window_.set(other.window_.getsize(),other.window_.getptr());
295 window_.set(other.window_.getsize(),other.window_.getptr());
303 window_.setptr(window_.getptr()+window_.getsize());
311 window_.setptr(window_.getptr()-window_.getsize());
318 return window_.getptr() == it.window_.getptr();
324 return window_.getptr() != it.window_.getptr();
330 return window_.getptr() == it.window_.getptr();
336 return window_.getptr() != it.window_.getptr();
367 return Iterator(this->p, columns_, 0);
373 return Iterator(this->p, columns_, rows_);
380 return Iterator(this->p, columns_, rows_-1);
387 return Iterator(this->p, columns_, -1);
393 return Iterator(this->p, columns_, std::min(i,rows_));
416 window_(const_cast<B*>(data + _i * columns), columns)
421 : i(it.i), window_(it.window_.getptr(),it.window_.getsize())
428 window_.set(other.window_.getsize(),other.window_.getptr());
436 window_.set(other.window_.getsize(),other.window_.getptr());
444 window_.setptr(window_.getptr()+window_.getsize());
452 window_.setptr(window_.getptr()-window_.getsize());
459 return window_.getptr() == it.window_.getptr();
465 return window_.getptr() != it.window_.getptr();
471 return window_.getptr() == it.window_.getptr();
477 return window_.getptr() != it.window_.getptr();
559 template<
class T,
class A=std::allocator<T> >
605 data_.resize(rows,cols);
612 return data_.begin();
625 return data_.beforeEnd();
632 return data_.beforeBegin();
638 return data_.begin();
651 return data_.beforeEnd();
658 return data_.beforeBegin();
670#ifdef DUNE_ISTL_WITH_CHECKING
672 DUNE_THROW(
ISTLError,
"Can't access negative rows!");
674 DUNE_THROW(
ISTLError,
"Row index out of range!");
681#ifdef DUNE_ISTL_WITH_CHECKING
683 DUNE_THROW(
ISTLError,
"Can't access negative rows!");
685 DUNE_THROW(
ISTLError,
"Row index out of range!");
718#ifdef DUNE_ISTL_WITH_CHECKING
719 if(
N()!=b.
N() ||
M() != b.
M())
720 DUNE_THROW(RangeError,
"Matrix sizes do not match!");
732#ifdef DUNE_ISTL_WITH_CHECKING
733 if(
N()!=b.
N() ||
M() != b.
M())
734 DUNE_THROW(RangeError,
"Matrix sizes do not match!");
745 out[j][i] = (*
this)[i][j];
758 out[i][j] += m1[i][k]*m2[k][j];
765 template <
class X,
class Y>
767#ifdef DUNE_ISTL_WITH_CHECKING
768 if (m.
M()!=vec.size())
769 DUNE_THROW(
ISTLError,
"Vector size doesn't match the number of matrix columns!");
774 for (
size_type i=0; i<out.size(); i++ ) {
776 out[i] += m[i][j]*vec[j];
783 template <
class X,
class Y>
784 void mv(
const X& x, Y& y)
const
786#ifdef DUNE_ISTL_WITH_CHECKING
787 if (x.N()!=
M()) DUNE_THROW(
ISTLError,
"vector/matrix size mismatch!");
788 if (y.N()!=
N()) DUNE_THROW(
ISTLError,
"vector/matrix size mismatch!");
794 auto&& xj = Impl::asVector(x[j]);
795 auto&& yi = Impl::asVector(y[i]);
796 Impl::asMatrix((*
this)[i][j]).umv(xj, yi);
802 template<
class X,
class Y>
803 void mtv (
const X& x, Y& y)
const
805#ifdef DUNE_ISTL_WITH_CHECKING
806 if (x.N()!=
N()) DUNE_THROW(
ISTLError,
"index out of range");
807 if (y.N()!=
M()) DUNE_THROW(
ISTLError,
"index out of range");
815 template <
class X,
class Y>
816 void umv(
const X& x, Y& y)
const
818#ifdef DUNE_ISTL_WITH_CHECKING
819 if (x.N()!=
M()) DUNE_THROW(
ISTLError,
"vector/matrix size mismatch!");
820 if (y.N()!=
N()) DUNE_THROW(
ISTLError,
"vector/matrix size mismatch!");
825 auto&& xj = Impl::asVector(x[j]);
826 auto&& yi = Impl::asVector(y[i]);
827 Impl::asMatrix((*
this)[i][j]).umv(xj, yi);
832 template<
class X,
class Y>
833 void mmv (
const X& x, Y& y)
const
835#ifdef DUNE_ISTL_WITH_CHECKING
836 if (x.N()!=
M()) DUNE_THROW(
ISTLError,
"vector/matrix size mismatch!");
837 if (y.N()!=
N()) DUNE_THROW(
ISTLError,
"vector/matrix size mismatch!");
842 auto&& xj = Impl::asVector(x[j]);
843 auto&& yi = Impl::asVector(y[i]);
844 Impl::asMatrix((*
this)[i][j]).mmv(xj, yi);
849 template <
class X,
class Y>
852#ifdef DUNE_ISTL_WITH_CHECKING
853 if (x.N()!=
M()) DUNE_THROW(
ISTLError,
"vector/matrix size mismatch!");
854 if (y.N()!=
N()) DUNE_THROW(
ISTLError,
"vector/matrix size mismatch!");
859 auto&& xj = Impl::asVector(x[j]);
860 auto&& yi = Impl::asVector(y[i]);
861 Impl::asMatrix((*
this)[i][j]).usmv(alpha, xj, yi);
866 template<
class X,
class Y>
867 void umtv (
const X& x, Y& y)
const
869#ifdef DUNE_ISTL_WITH_CHECKING
870 if (x.N()!=
N()) DUNE_THROW(
ISTLError,
"vector/matrix size mismatch!");
871 if (y.N()!=
M()) DUNE_THROW(
ISTLError,
"vector/matrix size mismatch!");
876 auto&& xi = Impl::asVector(x[i]);
877 auto&& yj = Impl::asVector(y[j]);
878 Impl::asMatrix((*
this)[i][j]).umtv(xi, yj);
883 template<
class X,
class Y>
884 void mmtv (
const X& x, Y& y)
const
886#ifdef DUNE_ISTL_WITH_CHECKING
887 if (x.N()!=
N()) DUNE_THROW(
ISTLError,
"vector/matrix size mismatch!");
888 if (y.N()!=
M()) DUNE_THROW(
ISTLError,
"vector/matrix size mismatch!");
893 auto&& xi = Impl::asVector(x[i]);
894 auto&& yj = Impl::asVector(y[j]);
895 Impl::asMatrix((*
this)[i][j]).mmtv(xi, yj);
900 template<
class X,
class Y>
903#ifdef DUNE_ISTL_WITH_CHECKING
904 if (x.N()!=
N()) DUNE_THROW(
ISTLError,
"vector/matrix size mismatch!");
905 if (y.N()!=
M()) DUNE_THROW(
ISTLError,
"vector/matrix size mismatch!");
910 auto&& xi = Impl::asVector(x[i]);
911 auto&& yj = Impl::asVector(y[j]);
912 Impl::asMatrix((*
this)[i][j]).usmtv(alpha, xi, yj);
917 template<
class X,
class Y>
918 void umhv (
const X& x, Y& y)
const
920#ifdef DUNE_ISTL_WITH_CHECKING
921 if (x.N()!=
N()) DUNE_THROW(
ISTLError,
"vector/matrix size mismatch!");
922 if (y.N()!=
M()) DUNE_THROW(
ISTLError,
"vector/matrix size mismatch!");
927 auto&& xi = Impl::asVector(x[i]);
928 auto&& yj = Impl::asVector(y[j]);
929 Impl::asMatrix((*
this)[i][j]).umhv(xi,yj);
934 template<
class X,
class Y>
935 void mmhv (
const X& x, Y& y)
const
937#ifdef DUNE_ISTL_WITH_CHECKING
938 if (x.N()!=
N()) DUNE_THROW(
ISTLError,
"vector/matrix size mismatch!");
939 if (y.N()!=
M()) DUNE_THROW(
ISTLError,
"vector/matrix size mismatch!");
944 auto&& xi = Impl::asVector(x[i]);
945 auto&& yj = Impl::asVector(y[j]);
946 Impl::asMatrix((*
this)[i][j]).mmhv(xi,yj);
951 template<
class X,
class Y>
954#ifdef DUNE_ISTL_WITH_CHECKING
955 if (x.N()!=
N()) DUNE_THROW(
ISTLError,
"vector/matrix size mismatch!");
956 if (y.N()!=
M()) DUNE_THROW(
ISTLError,
"vector/matrix size mismatch!");
961 auto&& xi = Impl::asVector(x[i]);
962 auto&& yj = Impl::asVector(y[j]);
963 Impl::asMatrix((*
this)[i][j]).usmhv(alpha,xi,yj);
978 typename FieldTraits<field_type>::real_type sum=0;
981 sum += Impl::asMatrix(
data_[i][j]).frobenius_norm2();
987 typename std::enable_if<!HasNaN<ft>::value,
int>::type = 0>
989 using real_type =
typename FieldTraits<ft>::real_type;
993 for (
auto const &x : *
this) {
995 for (
auto const &y : x)
996 sum += Impl::asMatrix(y).infinity_norm();
997 norm = max(sum, norm);
1006 typename std::enable_if<!HasNaN<ft>::value,
int>::type = 0>
1008 using real_type =
typename FieldTraits<ft>::real_type;
1012 for (
auto const &x : *
this) {
1014 for (
auto const &y : x)
1015 sum += Impl::asMatrix(y).infinity_norm_real();
1016 norm = max(sum, norm);
1023 typename std::enable_if<HasNaN<ft>::value,
int>::type = 0>
1025 using real_type =
typename FieldTraits<ft>::real_type;
1029 real_type isNaN = 1;
1030 for (
auto const &x : *
this) {
1032 for (
auto const &y : x)
1033 sum += Impl::asMatrix(y).infinity_norm();
1034 norm = max(sum, norm);
1038 return norm * (isNaN / isNaN);
1043 typename std::enable_if<HasNaN<ft>::value,
int>::type = 0>
1045 using real_type =
typename FieldTraits<ft>::real_type;
1049 real_type isNaN = 1;
1050 for (
auto const &x : *
this) {
1052 for (
auto const &y : x)
1053 sum += Impl::asMatrix(y).infinity_norm_real();
1054 norm = max(sum, norm);
1058 return norm * (isNaN / isNaN);
1066#ifdef DUNE_ISTL_WITH_CHECKING
1067 if (i<0 || i>=
N()) DUNE_THROW(
ISTLError,
"row index out of range");
1068 if (j<0 || i>=
M()) DUNE_THROW(
ISTLError,
"column index out of range");
1087 template<
class T,
class A>
1091 using real_type =
typename FieldTraits<field_type>::real_type;
This file implements a vector space as a tensor product of a given vector space. The number of compon...
Helper functions for determining the vector/matrix block level.
Definition allocator.hh:11
A vector of blocks with memory management.
Definition bvector.hh:392
derive error class from the base class in common
Definition istlexception.hh:19
A Vector of blocks with different blocksizes.
Definition matrix.hh:44
Imp::BlockVectorWindow< B, A > window_type
Definition matrix.hh:70
BlockVector< B, A > block_type
Same as value_type, here for historical reasons.
Definition matrix.hh:67
DenseMatrixBase & operator=(const DenseMatrixBase &a)
assignment
Definition matrix.hh:182
typename Imp::BlockTraits< B >::field_type field_type
export the type representing the field
Definition matrix.hh:50
Iterator beforeBegin() const
Definition matrix.hh:385
void resize(size_type rows, size_type columns)
same effect as constructor with same argument
Definition matrix.hh:153
const window_type const_reference
Definition matrix.hh:74
DenseMatrixBase(size_type rows, size_type columns)
Definition matrix.hh:95
Iterator end()
end Iterator
Definition matrix.hh:371
reference operator[](size_type i)
random access to blocks
Definition matrix.hh:241
BlockVector< B, A > value_type
Type of the elements of the outer vector, i.e., dynamic vectors of B.
Definition matrix.hh:63
Iterator find(size_type i)
random access returning iterator (end if not contained)
Definition matrix.hh:391
size_type N() const
number of blocks in the vector (are of variable size here)
Definition matrix.hh:539
ConstIterator beforeEnd() const
Definition matrix.hh:525
ConstIterator end() const
end ConstIterator
Definition matrix.hh:518
window_type reference
Definition matrix.hh:72
ConstIterator rend() const
end ConstIterator
Definition matrix.hh:531
Iterator beforeEnd()
Definition matrix.hh:378
ConstIterator begin() const
begin ConstIterator
Definition matrix.hh:512
A allocator_type
export the allocator type
Definition matrix.hh:53
DenseMatrixBase()
Definition matrix.hh:82
ConstIterator find(size_type i) const
random access returning iterator (end if not contained)
Definition matrix.hh:397
A::size_type size_type
The size type for the index access.
Definition matrix.hh:56
~DenseMatrixBase()
free dynamic memory
Definition matrix.hh:142
Iterator begin()
begin Iterator
Definition matrix.hh:365
DenseMatrixBase(const DenseMatrixBase &a)
copy constructor, has copy semantics
Definition matrix.hh:116
Iterator class for sequential access.
Definition matrix.hh:263
Iterator & operator--()
prefix decrement
Definition matrix.hh:308
size_type index() const
Definition matrix.hh:352
Iterator(Iterator &other)=default
Iterator(Iterator &&other)=default
bool operator!=(const Iterator &it) const
inequality
Definition matrix.hh:322
Iterator & operator=(Iterator &&other)
Move assignment.
Definition matrix.hh:282
Iterator & operator++()
prefix increment
Definition matrix.hh:300
Iterator()
constructor, no arguments
Definition matrix.hh:266
window_type & operator*() const
dereferencing
Definition matrix.hh:340
bool operator==(const Iterator &it) const
equality
Definition matrix.hh:316
Iterator & operator=(Iterator &other)
Copy assignment.
Definition matrix.hh:291
Iterator(B *data, size_type columns, size_type _i)
constructor
Definition matrix.hh:276
window_type * operator->() const
arrow
Definition matrix.hh:346
ConstIterator class for sequential access.
Definition matrix.hh:404
const window_type * operator->() const
arrow
Definition matrix.hh:487
const window_type & operator*() const
dereferencing
Definition matrix.hh:481
ConstIterator & operator++()
prefix increment
Definition matrix.hh:441
ConstIterator(const B *data, size_type columns, size_type _i)
constructor from pointer
Definition matrix.hh:414
ConstIterator & operator--()
prefix decrement
Definition matrix.hh:449
ConstIterator(const Iterator &it)
constructor from non_const iterator
Definition matrix.hh:420
bool operator!=(const ConstIterator &it) const
inequality
Definition matrix.hh:463
ConstIterator()
constructor
Definition matrix.hh:407
bool operator==(const ConstIterator &it) const
equality
Definition matrix.hh:457
size_type index() const
Definition matrix.hh:493
ConstIterator & operator=(Iterator &&other)
Definition matrix.hh:424
ConstIterator & operator=(Iterator &other)
Definition matrix.hh:432
A generic dynamic dense matrix.
Definition matrix.hh:561
size_type cols_
Number of columns of the matrix.
Definition matrix.hh:1084
FieldTraits< ft >::real_type infinity_norm() const
infinity norm (row sum norm, how to generalize for blocks?)
Definition matrix.hh:988
A allocator_type
Export the allocator.
Definition matrix.hh:571
FieldTraits< ft >::real_type infinity_norm_real() const
simplified infinity norm (uses Manhattan norm for complex values)
Definition matrix.hh:1007
void usmhv(const field_type &alpha, const X &x, Y &y) const
y += alpha A^H x
Definition matrix.hh:952
void usmv(const field_type &alpha, const X &x, Y &y) const
Definition matrix.hh:850
A::size_type size_type
Type for indices and sizes.
Definition matrix.hh:577
MatrixImp::DenseMatrixBase< T, A > data_
Abuse DenseMatrixBase as an engine for a 2d array ISTL-style.
Definition matrix.hh:1077
Matrix transpose() const
Return the transpose of the matrix.
Definition matrix.hh:741
void mtv(const X &x, Y &y) const
y = A^T x
Definition matrix.hh:803
void umv(const X &x, Y &y) const
y += A x
Definition matrix.hh:816
void mv(const X &x, Y &y) const
y = A x
Definition matrix.hh:784
MatrixImp::DenseMatrixBase< T, A >::ConstIterator ConstRowIterator
Const iterator over the matrix rows.
Definition matrix.hh:586
void setSize(size_type rows, size_type cols)
Change the matrix size.
Definition matrix.hh:604
RowIterator beforeBegin()
Definition matrix.hh:630
RowIterator beforeEnd()
Definition matrix.hh:623
Matrix()
Create empty matrix.
Definition matrix.hh:592
Matrix & operator-=(const Matrix &b)
Subtract the entries of another matrix from this one.
Definition matrix.hh:731
FieldTraits< field_type >::real_type frobenius_norm2() const
square of frobenius norm, need for block recursion
Definition matrix.hh:976
row_type::iterator ColIterator
Iterator for the entries of each row.
Definition matrix.hh:583
ConstRowIterator beforeEnd() const
Definition matrix.hh:649
Matrix & operator=(const field_type &t)
Assignment from scalar.
Definition matrix.hh:662
RowIterator end()
Get iterator to one beyond last row.
Definition matrix.hh:616
const row_type operator[](size_type row) const
The const index operator.
Definition matrix.hh:680
ConstRowIterator end() const
Get const iterator to one beyond last row.
Definition matrix.hh:642
friend Y operator*(const Matrix< T > &m, const X &vec)
Generic matrix-vector multiplication.
Definition matrix.hh:766
Matrix< T > & operator*=(const field_type &scalar)
Multiplication with a scalar.
Definition matrix.hh:701
row_type operator[](size_type row)
The index operator.
Definition matrix.hh:669
void mmv(const X &x, Y &y) const
y -= A x
Definition matrix.hh:833
Matrix & operator+=(const Matrix &b)
Add the entries of another matrix to this one.
Definition matrix.hh:717
ConstRowIterator begin() const
Get const iterator to first row.
Definition matrix.hh:636
void mmhv(const X &x, Y &y) const
y -= A^H x
Definition matrix.hh:935
RowIterator begin()
Get iterator to first row.
Definition matrix.hh:610
typename Imp::BlockTraits< T >::field_type field_type
Export the type representing the underlying field.
Definition matrix.hh:565
row_type::const_iterator ConstColIterator
Const iterator for the entries of each row.
Definition matrix.hh:589
size_type M() const
Return the number of columns.
Definition matrix.hh:696
T block_type
Export the type representing the components.
Definition matrix.hh:568
bool exists(size_type i, size_type j) const
return true if (i,j) is in pattern
Definition matrix.hh:1064
Matrix< T > & operator/=(const field_type &scalar)
Division by a scalar.
Definition matrix.hh:707
friend Matrix< T > operator*(const Matrix< T > &m1, const Matrix< T > &m2)
Generic matrix multiplication.
Definition matrix.hh:751
void umtv(const X &x, Y &y) const
y += A^T x
Definition matrix.hh:867
void mmtv(const X &x, Y &y) const
y -= A^T x
Definition matrix.hh:884
MatrixImp::DenseMatrixBase< T, A >::window_type row_type
The type implementing a matrix row.
Definition matrix.hh:574
FieldTraits< field_type >::real_type frobenius_norm() const
frobenius norm: sqrt(sum over squared values of entries)
Definition matrix.hh:970
void usmtv(const field_type &alpha, const X &x, Y &y) const
y += alpha A^T x
Definition matrix.hh:901
void umhv(const X &x, Y &y) const
y += A^H x
Definition matrix.hh:918
size_type N() const
Return the number of rows.
Definition matrix.hh:691
ConstRowIterator beforeBegin() const
Definition matrix.hh:656
Matrix(size_type rows, size_type cols)
Create uninitialized matrix of size rows x cols.
Definition matrix.hh:597
MatrixImp::DenseMatrixBase< T, A >::Iterator RowIterator
Iterator over the matrix rows.
Definition matrix.hh:580
typename Matrix< T, A >::field_type field_type
Definition matrix.hh:1090
typename FieldTraits< field_type >::real_type real_type
Definition matrix.hh:1091