3 #ifndef DUNE_ISTL_MATRIXMATRIX_HH 4 #define DUNE_ISTL_MATRIXMATRIX_HH 9 #include <dune/common/fmatrix.hh> 10 #include <dune/common/timer.hh> 36 struct NonzeroPatternTraverser
41 struct NonzeroPatternTraverser<0>
43 template<
class T,
class A1,
class A2,
class F,
int n,
int m,
int k>
49 DUNE_THROW(ISTLError,
"The sizes of the matrices do not match: "<<A.M()<<
"!="<<B.N());
52 typedef typename Dune::BCRSMatrix<Dune::FieldMatrix<T,n,k>,A1>::ConstColIterator Col;
54 for(Row row= A.begin(); row != A.end(); ++row) {
56 for(Col
col = row->begin();
col != row->end(); ++
col) {
59 for(BCol bcol = B[
col.index()].begin(); bcol != B[
col.index()].end(); ++bcol) {
60 func(*
col, *bcol, row.index(), bcol.index());
69 struct NonzeroPatternTraverser<1>
71 template<
class T,
class A1,
class A2,
class F,
int n,
int m,
int k>
78 DUNE_THROW(ISTLError,
"The sizes of the matrices do not match: "<<A.N()<<
"!="<<B.N());
81 typedef typename Dune::BCRSMatrix<Dune::FieldMatrix<T,k,n>,A1>::ConstColIterator Col;
82 typedef typename Dune::BCRSMatrix<Dune::FieldMatrix<T,k,m>,A2>::ConstColIterator BCol;
84 for(Row row=A.begin(); row!=A.end(); ++row) {
85 for(Col
col=row->begin();
col!=row->end(); ++
col) {
86 for(BCol bcol = B[row.index()].begin(); bcol != B[row.index()].end(); ++bcol) {
87 func(*
col, *bcol,
col.index(), bcol.index());
95 struct NonzeroPatternTraverser<2>
97 template<
class T,
class A1,
class A2,
class F,
int n,
int m,
int k>
98 static void traverse(
const BCRSMatrix<FieldMatrix<T,n,m>,A1>&
mat,
99 const BCRSMatrix<FieldMatrix<T,k,m>,A2>& matt,
102 if(
mat.M()!=matt.M())
103 DUNE_THROW(ISTLError,
"The sizes of the matrices do not match: "<<
mat.M()<<
"!="<<matt.M());
105 typedef typename BCRSMatrix<FieldMatrix<T,n,m>,A1>::ConstRowIterator row_iterator;
106 typedef typename BCRSMatrix<FieldMatrix<T,n,m>,A1>::ConstColIterator col_iterator;
107 typedef typename BCRSMatrix<FieldMatrix<T,k,m>,A2>::ConstRowIterator row_iterator_t;
108 typedef typename BCRSMatrix<FieldMatrix<T,k,m>,A2>::ConstColIterator col_iterator_t;
110 for(row_iterator mrow=
mat.begin(); mrow !=
mat.end(); ++mrow) {
117 for(row_iterator_t mtcol=matt.begin(); mtcol != matt.end(); ++mtcol) {
120 col_iterator_t mtrow=mtcol->
begin();
121 bool funcCalled =
false;
122 for(col_iterator mcol=mrow->begin(); mcol != mrow->end(); ++mcol) {
125 for( ; mtrow != mtcol->end(); ++mtrow)
126 if(mtrow.index()>=mcol.index())
128 if(mtrow != mtcol->end() && mtrow.index()==mcol.index()) {
129 func(*mcol, *mtrow, mtcol.index());
149 template<
class T,
class A,
int n,
int m>
150 class SparsityPatternInitializer
153 enum {do_break=
true};
157 SparsityPatternInitializer(CreateIterator iter)
161 template<
class T1,
class T2>
162 void operator()(
const T1&,
const T2&, size_type j)
175 CreateIterator rowiter;
179 template<
int transpose,
class T,
class TA,
int n,
int m>
180 class MatrixInitializer
183 enum {do_break=
true};
188 MatrixInitializer(Matrix& A_, size_type)
191 template<
class T1,
class T2>
192 void operator()(
const T1&,
const T2&,
int)
203 std::size_t nonzeros()
208 template<
class A1,
class A2,
int n2,
int m2,
int n3,
int m3>
212 SparsityPatternInitializer<T, TA, n, m> sparsity(A.createbegin());
213 NonzeroPatternTraverser<transpose>::traverse(mat1,mat2,sparsity);
221 template<
class T,
class TA,
int n,
int m>
222 class MatrixInitializer<1,T,TA,n,m>
225 enum {do_break=
false};
230 MatrixInitializer(Matrix& A_, size_type rows)
231 : A(A_), entries(rows)
234 template<
class T1,
class T2>
235 void operator()(
const T1&,
const T2&, size_type i, size_type j)
237 entries[i].insert(j);
246 typedef typename std::vector<std::set<size_t> >::const_iterator Iter;
247 for(Iter iter = entries.begin(); iter != entries.end(); ++iter)
251 template<
class A1,
class A2,
int n2,
int m2,
int n3,
int m3>
255 typedef typename std::vector<std::set<size_t> >::const_iterator Iter;
256 CreateIterator citer = A.createbegin();
257 for(Iter iter = entries.begin(); iter != entries.end(); ++iter, ++citer) {
258 typedef std::set<size_t>::const_iterator SetIter;
259 for(SetIter index=iter->begin(); index != iter->end(); ++index)
266 std::vector<std::set<size_t> > entries;
269 template<
class T,
class TA,
int n,
int m>
270 struct MatrixInitializer<0,T,TA,n,m>
271 :
public MatrixInitializer<1,T,TA,n,m>
275 : MatrixInitializer<1,T,TA,n,m>(A_,rows)
280 template<
class T,
class T1,
class T2,
int n,
int m,
int k>
286 for(size_type row=0; row<n; ++row)
288 for(size_type i=0; i < m; ++i)
289 res[row][
col]+=mat[row][i]*matt[
col][i];
293 template<
class T,
class T1,
class T2,
int n,
int m,
int k>
298 for(size_type i=0; i<m; ++i)
299 for(size_type row=0; row<n; ++row) {
301 res[row][
col]+=mat[i][row]*matt[i][
col];
305 template<
class T,
class T1,
class T2,
int n,
int m,
int k>
310 for(size_type row=0; row<n; ++row)
312 for(size_type i=0; i < k; ++i)
313 res[row][
col]+=mat[row][i]*matt[i][
col];
318 template<
class T,
class A,
int n,
int m>
319 class EntryAccumulatorFather
322 enum {do_break=
false};
323 typedef BCRSMatrix<FieldMatrix<T,n,m>,A>
Matrix;
327 EntryAccumulatorFather(Matrix& mat_)
328 : mat(mat_), row(mat.begin())
352 template<
class T,
class A,
int n,
int m,
int transpose>
353 class EntryAccumulator
354 :
public EntryAccumulatorFather<T,A,n,m>
357 typedef BCRSMatrix<FieldMatrix<T,n,m>,A>
Matrix;
360 EntryAccumulator(Matrix& mat_)
361 : EntryAccumulatorFather<T,A,n,m>(mat_)
364 template<
class T1,
class T2>
365 void operator()(
const T1& t1,
const T2& t2, size_type i)
367 assert(this->
col.index()==i);
368 addMatMultMat(*(this->
col),t1,t2);
372 template<
class T,
class A,
int n,
int m>
373 class EntryAccumulator<T,A,n,m,0>
374 :
public EntryAccumulatorFather<T,A,n,m>
377 typedef BCRSMatrix<FieldMatrix<T,n,m>,A>
Matrix;
380 EntryAccumulator(Matrix& mat_)
381 : EntryAccumulatorFather<T,A,n,m>(mat_)
384 template<
class T1,
class T2>
385 void operator()(
const T1& t1,
const T2& t2, size_type i, size_type j)
387 addMatMultMat(this->mat[i][j], t1, t2);
391 template<
class T,
class A,
int n,
int m>
392 class EntryAccumulator<T,A,n,m,1>
393 :
public EntryAccumulatorFather<T,A,n,m>
396 typedef BCRSMatrix<FieldMatrix<T,n,m>,A>
Matrix;
399 EntryAccumulator(Matrix& mat_)
400 : EntryAccumulatorFather<T,A,n,m>(mat_)
403 template<
class T1,
class T2>
404 void operator()(
const T1& t1,
const T2& t2, size_type i, size_type j)
406 addTransposeMatMultMat(this->mat[i][j], t1, t2);
410 template<
class T,
class A,
int n,
int m>
411 class EntryAccumulator<T,A,n,m,2>
412 :
public EntryAccumulatorFather<T,A,n,m>
415 typedef BCRSMatrix<FieldMatrix<T,n,m>,A>
Matrix;
418 EntryAccumulator(Matrix& mat_)
419 : EntryAccumulatorFather<T,A,n,m>(mat_)
422 template<
class T1,
class T2>
423 void operator()(
const T1& t1,
const T2& t2, size_type i)
425 DUNE_UNUSED_PARAMETER(i);
426 assert(this->
col.index()==i);
427 addMatMultTransposeMat(*this->
col,t1,t2);
432 template<
int transpose>
437 struct SizeSelector<0>
439 template<
class M1,
class M2>
440 static std::tuple<typename M1::size_type, typename M2::size_type>
441 size(
const M1& m1,
const M2& m2)
443 return std::make_tuple(m1.N(), m2.M());
448 struct SizeSelector<1>
450 template<
class M1,
class M2>
451 static std::tuple<typename M1::size_type, typename M2::size_type>
452 size(
const M1& m1,
const M2& m2)
454 return std::make_tuple(m1.M(), m2.M());
460 struct SizeSelector<2>
462 template<
class M1,
class M2>
463 static std::tuple<typename M1::size_type, typename M2::size_type>
464 size(
const M1& m1,
const M2& m2)
466 return std::make_tuple(m1.N(), m2.N());
470 template<
int transpose,
class T,
class A,
class A1,
class A2,
int n1,
int m1,
int n2,
int m2,
int n3,
int m3>
476 std::tie(rows,cols)=SizeSelector<transpose>::size(mat1, mat2);
477 MatrixInitializer<transpose,T,A,n1,m1> patternInit(res, rows);
479 NonzeroPatternTraverser<transpose>::traverse(mat1,mat2,patternInit);
480 res.setSize(rows, cols, patternInit.nonzeros());
487 patternInit.initPattern(mat1, mat2);
493 EntryAccumulator<T,A,n1,m1, transpose> entriesAccu(res);
494 NonzeroPatternTraverser<transpose>::traverse(mat1,mat2,entriesAccu);
507 template<
typename M1,
typename M2>
511 template<
typename T,
int n,
int k,
int m>
517 template<
typename T,
typename A,
typename A1,
int n,
int k,
int m>
532 template<
typename M1,
typename M2>
536 template<
typename T,
int n,
int k,
int m>
542 template<
typename T,
typename A,
typename A1,
int n,
int k,
int m>
558 template<
class T,
class A,
class A1,
class A2,
int n,
int m,
int k>
562 DUNE_UNUSED_PARAMETER(tryHard);
563 matMultMat<2>(res,
mat, matt);
574 template<
class T,
class A,
class A1,
class A2,
int n,
int m,
int k>
578 matMultMat<0>(res,
mat, matt);
589 template<
class T,
class A,
class A1,
class A2,
int n,
int m,
int k>
593 DUNE_UNUSED_PARAMETER(tryHard);
594 matMultMat<1>(res,
mat, matt);
void insert(size_type j)
put column index in row
Definition: bcrsmatrix.hh:1033
Helper TMP to get the result type of a sparse matrix matrix multiplication ( )
Definition: matrixmatrix.hh:533
FieldMatrix< T, n, m > type
Definition: matrixmatrix.hh:539
Helper TMP to get the result type of a sparse matrix matrix multiplication ( )
Definition: matrixmatrix.hh:508
Col col
Definition: matrixmatrix.hh:349
Matrix::RowIterator Row
Definition: matrixmatrix.hh:324
BCRSMatrix< FieldMatrix< T, n, m >, A > Matrix
Definition: matrixmatrix.hh:377
BCRSMatrix< FieldMatrix< T, n, m >, A > Matrix
Definition: matrixmatrix.hh:415
A::size_type size_type
The type for the index access and the size.
Definition: bcrsmatrix.hh:457
BCRSMatrix< typename MatMultMatResult< FieldMatrix< T, n, k >, FieldMatrix< T, k, m > >::type, std::allocator< typename MatMultMatResult< FieldMatrix< T, n, k >, FieldMatrix< T, k, m > >::type > > type
Definition: matrixmatrix.hh:521
Definition: example.cc:35
Iterator begin()
Get iterator to first row.
Definition: bcrsmatrix.hh:634
Matrix::size_type size_type
Definition: matrixmatrix.hh:416
Matrix::size_type size_type
Definition: matrixmatrix.hh:378
Definition: matrixutils.hh:25
void matMultTransposeMat(BCRSMatrix< FieldMatrix< T, n, k >, A > &res, const BCRSMatrix< FieldMatrix< T, n, m >, A1 > &mat, const BCRSMatrix< FieldMatrix< T, k, m >, A2 > &matt, bool tryHard=false)
Calculate product of a sparse matrix with a transposed sparse matrices ( ).
Definition: matrixmatrix.hh:559
A sparse block matrix with compressed row storage.
Definition: bcrsmatrix.hh:422
FieldMatrix< T, n, m > type
Definition: matrixmatrix.hh:514
Matrix::size_type size_type
Definition: matrixmatrix.hh:397
Matrix & mat
Definition: matrixmatrix.hh:345
Matrix::CreateIterator CreateIterator
Definition: matrixmatrix.hh:227
Dune::BCRSMatrix< FieldMatrix< T, n, m >, TA > Matrix
Definition: matrixmatrix.hh:184
Definition: basearray.hh:19
Iterator class for sequential creation of blocks
Definition: bcrsmatrix.hh:928
void matMultMat(BCRSMatrix< FieldMatrix< T, n, m >, A > &res, const BCRSMatrix< FieldMatrix< T, n, k >, A1 > &mat, const BCRSMatrix< FieldMatrix< T, k, m >, A2 > &matt, bool tryHard=false)
Calculate product of two sparse matrices ( ).
Definition: matrixmatrix.hh:575
Implementation of the BCRSMatrix class.
BCRSMatrix< typename MatMultMatResult< FieldMatrix< T, n, k >, FieldMatrix< T, k, m > >::type, std::allocator< typename MatMultMatResult< FieldMatrix< T, n, k >, FieldMatrix< T, k, m > >::type > > type
Definition: matrixmatrix.hh:546
Matrix::size_type size_type
Definition: matrixmatrix.hh:358
Matrix::ColIterator Col
Definition: matrixmatrix.hh:325
BCRSMatrix< FieldMatrix< T, n, m >, A >::size_type size_type
Definition: matrixmatrix.hh:155
Matrix::size_type size_type
Definition: matrixmatrix.hh:228
iterator class for sequential access
Definition: basearray.hh:563
Iterator access to matrix rows
Definition: bcrsmatrix.hh:536
BCRSMatrix< FieldMatrix< T, n, m >, A > Matrix
Definition: matrixmatrix.hh:396
Matrix::CreateIterator CreateIterator
Definition: matrixmatrix.hh:185
Dune::BCRSMatrix< Dune::FieldMatrix< T, n, m >, TA > Matrix
Definition: matrixmatrix.hh:226
Matrix::size_type size_type
Definition: matrixmatrix.hh:186
void transposeMatMultMat(BCRSMatrix< FieldMatrix< T, n, m >, A > &res, const BCRSMatrix< FieldMatrix< T, k, n >, A1 > &mat, const BCRSMatrix< FieldMatrix< T, k, m >, A2 > &matt, bool tryHard=false)
Calculate product of a transposed sparse matrix with another sparse matrices ( ). ...
Definition: matrixmatrix.hh:590