3 #ifndef DUNE_MATRIXMATRIX_HH
4 #define DUNE_MATRIXMATRIX_HH
6 #include <dune/common/fmatrix.hh>
7 #include <dune/common/tuples.hh>
8 #include <dune/common/timer.hh>
34 struct NonzeroPatternTraverser
39 struct NonzeroPatternTraverser<0>
41 template<
class T,
class A1,
class A2,
class F,
int n,
int m,
int k>
47 DUNE_THROW(ISTLError,
"The sizes of the matrices do not match: "<<
A.M()<<
"!="<<B.N());
50 typedef typename Dune::BCRSMatrix<Dune::FieldMatrix<T,n,k>,A1>::ConstColIterator Col;
57 for(BCol bcol = B[
col.index()].begin(); bcol != B[
col.index()].end(); ++bcol) {
58 func(*
col, *bcol,
row.index(), bcol.index());
67 struct NonzeroPatternTraverser<1>
69 template<
class T,
class A1,
class A2,
class F,
int n,
int m,
int k>
76 DUNE_THROW(ISTLError,
"The sizes of the matrices do not match: "<<
A.N()<<
"!="<<B.N());
79 typedef typename Dune::BCRSMatrix<Dune::FieldMatrix<T,k,n>,A1>::ConstColIterator Col;
80 typedef typename Dune::BCRSMatrix<Dune::FieldMatrix<T,k,m>,A2>::ConstColIterator BCol;
84 for(BCol bcol = B[
row.index()].begin(); bcol != B[
row.index()].end(); ++bcol) {
85 func(*
col, *bcol,
col.index(), bcol.index());
93 struct NonzeroPatternTraverser<2>
95 template<
class T,
class A1,
class A2,
class F,
int n,
int m,
int k>
96 static void traverse(
const BCRSMatrix<FieldMatrix<T,n,m>,A1>&
mat,
97 const BCRSMatrix<FieldMatrix<T,k,m>,A2>& matt,
100 if(
mat.M()!=matt.M())
101 DUNE_THROW(ISTLError,
"The sizes of the matrices do not match: "<<
mat.M()<<
"!="<<matt.M());
103 typedef typename BCRSMatrix<FieldMatrix<T,n,m>,A1>::ConstRowIterator row_iterator;
104 typedef typename BCRSMatrix<FieldMatrix<T,n,m>,A1>::ConstColIterator col_iterator;
105 typedef typename BCRSMatrix<FieldMatrix<T,k,m>,A2>::ConstRowIterator row_iterator_t;
106 typedef typename BCRSMatrix<FieldMatrix<T,k,m>,A2>::ConstColIterator col_iterator_t;
108 for(row_iterator mrow=
mat.begin(); mrow !=
mat.end(); ++mrow) {
115 for(row_iterator_t mtcol=matt.begin(); mtcol != matt.end(); ++mtcol) {
118 col_iterator_t mtrow=mtcol->
begin();
119 bool funcCalled =
false;
120 for(col_iterator mcol=mrow->begin(); mcol != mrow->end(); ++mcol) {
123 for( ; mtrow != mtcol->end(); ++mtrow)
124 if(mtrow.index()>=mcol.index())
126 if(mtrow != mtcol->end() && mtrow.index()==mcol.index()) {
127 func(*mcol, *mtrow, mtcol.index());
147 template<
class T,
class A,
int n,
int m>
148 class SparsityPatternInitializer
151 enum {do_break=
true};
159 template<
class T1,
class T2>
160 void operator()(
const T1& t1,
const T2& t2, size_type j)
177 template<
int transpose,
class T,
class TA,
int n,
int m>
178 class MatrixInitializer
181 enum {do_break=
true};
189 template<
class T1,
class T2>
190 void operator()(
const T1& t1,
const T2& t2,
int j)
201 std::size_t nonzeros()
206 template<
class A1,
class A2,
int n2,
int m2,
int n3,
int m3>
207 void initPattern(
const BCRSMatrix<FieldMatrix<T,n2,m2>,A1>& mat1,
208 const BCRSMatrix<FieldMatrix<T,n3,m3>,A2>& mat2)
210 SparsityPatternInitializer<T, TA, n, m> sparsity(
A.
createbegin());
211 NonzeroPatternTraverser<transpose>::traverse(mat1,mat2,sparsity);
219 template<
class T,
class TA,
int n,
int m>
220 class MatrixInitializer<1,T,TA,n,m>
223 enum {do_break=
false};
229 :
A(A_), entries(rows)
232 template<
class T1,
class T2>
233 void operator()(
const T1& t1,
const T2& t2, size_type i, size_type j)
235 entries[i].insert(j);
244 typedef typename std::vector<std::set<size_t> >::const_iterator Iter;
245 for(Iter iter = entries.begin(); iter != entries.end(); ++iter)
249 template<
class A1,
class A2,
int n2,
int m2,
int n3,
int m3>
250 void initPattern(
const BCRSMatrix<FieldMatrix<T,n2,m2>,A1>& mat1,
251 const BCRSMatrix<FieldMatrix<T,n3,m3>,A2>& mat2)
253 typedef typename std::vector<std::set<size_t> >::const_iterator Iter;
255 for(Iter iter = entries.begin(); iter != entries.end(); ++iter, ++citer) {
256 typedef std::set<size_t>::const_iterator SetIter;
264 std::vector<std::set<size_t> > entries;
267 template<
class T,
class TA,
int n,
int m>
268 struct MatrixInitializer<0,T,TA,n,m>
269 :
public MatrixInitializer<1,T,TA,n,m>
273 : MatrixInitializer<1,T,TA,n,m>(A_,rows)
278 template<
class T,
class T1,
class T2,
int n,
int m,
int k>
279 void addMatMultTransposeMat(FieldMatrix<T,n,k>& res,
const FieldMatrix<T1,n,m>&
mat,
280 const FieldMatrix<T2,k,m>& matt)
282 typedef typename FieldMatrix<T,n,k>::size_type size_type;
286 for(size_type i=0; i < m; ++i)
291 template<
class T,
class T1,
class T2,
int n,
int m,
int k>
292 void addTransposeMatMultMat(FieldMatrix<T,n,k>& res,
const FieldMatrix<T1,m,n>& mat,
293 const FieldMatrix<T2,m,k>& matt)
295 typedef typename FieldMatrix<T,n,k>::size_type size_type;
296 for(size_type i=0; i<m; ++i)
303 template<
class T,
class T1,
class T2,
int n,
int m,
int k>
304 void addMatMultMat(FieldMatrix<T,n,m>& res,
const FieldMatrix<T1,n,k>& mat,
305 const FieldMatrix<T2,k,m>& matt)
307 typedef typename FieldMatrix<T,n,k>::size_type size_type;
310 for(size_type i=0; i < k; ++i)
316 template<
class T,
class A,
int n,
int m>
317 class EntryAccumulatorFather
320 enum {do_break=
false};
321 typedef BCRSMatrix<FieldMatrix<T,n,m>,
A>
Matrix;
325 EntryAccumulatorFather(
Matrix& mat_)
326 : mat(mat_),
row(mat.begin())
350 template<
class T,
class A,
int n,
int m,
int transpose>
351 class EntryAccumulator
352 :
public EntryAccumulatorFather<T,A,n,m>
355 typedef BCRSMatrix<FieldMatrix<T,n,m>,
A>
Matrix;
358 EntryAccumulator(
Matrix& mat_)
359 : EntryAccumulatorFather<T,
A,n,m>(mat_)
362 template<
class T1,
class T2>
363 void operator()(
const T1& t1,
const T2& t2, size_type i)
365 assert(this->
col.index()==i);
366 addMatMultMat(*(this->
col),t1,t2);
370 template<
class T,
class A,
int n,
int m>
371 class EntryAccumulator<T,
A,n,m,0>
372 :
public EntryAccumulatorFather<T,A,n,m>
375 typedef BCRSMatrix<FieldMatrix<T,n,m>,
A>
Matrix;
378 EntryAccumulator(
Matrix& mat_)
379 : EntryAccumulatorFather<T,
A,n,m>(mat_)
382 template<
class T1,
class T2>
383 void operator()(
const T1& t1,
const T2& t2, size_type i, size_type j)
385 addMatMultMat(this->mat[i][j], t1, t2);
389 template<
class T,
class A,
int n,
int m>
390 class EntryAccumulator<T,
A,n,m,1>
391 :
public EntryAccumulatorFather<T,A,n,m>
394 typedef BCRSMatrix<FieldMatrix<T,n,m>,
A>
Matrix;
397 EntryAccumulator(
Matrix& mat_)
398 : EntryAccumulatorFather<T,
A,n,m>(mat_)
401 template<
class T1,
class T2>
402 void operator()(
const T1& t1,
const T2& t2, size_type i, size_type j)
404 addTransposeMatMultMat(this->mat[i][j], t1, t2);
408 template<
class T,
class A,
int n,
int m>
409 class EntryAccumulator<T,
A,n,m,2>
410 :
public EntryAccumulatorFather<T,A,n,m>
413 typedef BCRSMatrix<FieldMatrix<T,n,m>,
A>
Matrix;
416 EntryAccumulator(
Matrix& mat_)
417 : EntryAccumulatorFather<T,
A,n,m>(mat_)
420 template<
class T1,
class T2>
421 void operator()(
const T1& t1,
const T2& t2, size_type i)
423 assert(this->
col.index()==i);
424 addMatMultTransposeMat(*this->
col,t1,t2);
429 template<
int transpose>
434 struct SizeSelector<0>
436 template<
class M1,
class M2>
437 static tuple<typename M1::size_type, typename M2::size_type>
438 size(
const M1& m1,
const M2& m2)
440 return make_tuple(m1.N(), m2.M());
445 struct SizeSelector<1>
447 template<
class M1,
class M2>
448 static tuple<typename M1::size_type, typename M2::size_type>
449 size(
const M1& m1,
const M2& m2)
451 return make_tuple(m1.M(), m2.M());
457 struct SizeSelector<2>
459 template<
class M1,
class M2>
460 static tuple<typename M1::size_type, typename M2::size_type>
461 size(
const M1& m1,
const M2& m2)
463 return make_tuple(m1.N(), m2.N());
467 template<
int transpose,
class T,
class A,
class A1,
class A2,
int n1,
int m1,
int n2,
int m2,
int n3,
int m3>
468 void matMultMat(BCRSMatrix<FieldMatrix<T,n1,m1>,
A>& res,
const BCRSMatrix<FieldMatrix<T,n2,m2>,A1>& mat1,
469 const BCRSMatrix<FieldMatrix<T,n3,m3>,A2>& mat2)
472 typename BCRSMatrix<FieldMatrix<T,n1,m1>,
A>::size_type rows, cols;
473 tie(rows,cols)=SizeSelector<transpose>::size(mat1, mat2);
474 MatrixInitializer<transpose,T,A,n1,m1> patternInit(res, rows);
476 NonzeroPatternTraverser<transpose>::traverse(mat1,mat2,patternInit);
477 res.setSize(rows, cols, patternInit.nonzeros());
478 res.setBuildMode(BCRSMatrix<FieldMatrix<T,n1,m1>,
A>::row_wise);
484 patternInit.initPattern(mat1, mat2);
490 EntryAccumulator<T,A,n1,m1, transpose> entriesAccu(res);
491 NonzeroPatternTraverser<transpose>::traverse(mat1,mat2,entriesAccu);
504 template<
typename M1,
typename M2>
508 template<
typename T,
int n,
int k,
int m>
511 typedef FieldMatrix<T,n,m>
type;
514 template<
typename T,
typename A,
typename A1,
int n,
int k,
int m>
518 std::allocator<typename MatMultMatResult<FieldMatrix<T,n,k>,FieldMatrix<T,k,m> >
::type> >
type;
529 template<
typename M1,
typename M2>
533 template<
typename T,
int n,
int k,
int m>
536 typedef FieldMatrix<T,n,m>
type;
539 template<
typename T,
typename A,
typename A1,
int n,
int k,
int m>
543 std::allocator<typename MatMultMatResult<FieldMatrix<T,n,k>,FieldMatrix<T,k,m> >
::type> >
type;
555 template<
class T,
class A,
class A1,
class A2,
int n,
int m,
int k>
557 const BCRSMatrix<FieldMatrix<T,k,m>,A2>& matt,
bool tryHard=
false)
559 matMultMat<2>(res,
mat, matt);
570 template<
class T,
class A,
class A1,
class A2,
int n,
int m,
int k>
572 const BCRSMatrix<FieldMatrix<T,k,m>,A2>& matt,
bool tryHard=
false)
574 matMultMat<0>(res,
mat, matt);
585 template<
class T,
class A,
class A1,
class A2,
int n,
int m,
int k>
587 const BCRSMatrix<FieldMatrix<T,k,m>,A2>& matt,
bool tryHard=
false)
589 matMultMat<1>(res,
mat, matt);