3#ifndef DUNE_MATRIXMATRIX_HH
4#define DUNE_MATRIXMATRIX_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());
52 for(Row row= A.begin(); row != A.end(); ++row) {
54 for(Col col = row->begin(); col != row->end(); ++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());
82 for(Row row=A.begin(); row!=A.end(); ++row) {
83 for(Col col=row->begin(); col!=row->end(); ++col) {
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};
152 typedef typename BCRSMatrix<FieldMatrix<T,n,m>,A>::CreateIterator CreateIterator;
153 typedef typename BCRSMatrix<FieldMatrix<T,n,m>,A>::size_type size_type;
155 SparsityPatternInitializer(CreateIterator iter)
159 template<
class T1,
class T2>
160 void operator()(
const T1& t1,
const T2& t2, size_type j)
173 CreateIterator rowiter;
177 template<
int transpose,
class T,
class TA,
int n,
int m>
178 class MatrixInitializer
181 enum {do_break=
true};
186 MatrixInitializer(Matrix& A_, size_type rows)
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};
228 MatrixInitializer(Matrix& A_, size_type rows)
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;
257 for(SetIter index=iter->begin(); index != iter->end(); ++index)
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;
284 for(size_type row=0; row<n; ++row)
285 for(size_type col=0; col<k; ++col) {
286 for(size_type i=0; i < m; ++i)
287 res[row][col]+=mat[row][i]*matt[col][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)
297 for(size_type row=0; row<n; ++row) {
298 for(size_type col=0; col < k; ++col)
299 res[row][col]+=mat[i][row]*matt[i][col];
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;
308 for(size_type row=0; row<n; ++row)
309 for(size_type col=0; col<m; ++col) {
310 for(size_type i=0; i < k; ++i)
311 res[row][col]+=mat[row][i]*matt[i][col];
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>
514 template<
typename T,
typename A,
typename A1,
int n,
int k,
int m>
515 struct MatMultMatResult<BCRSMatrix<FieldMatrix<T,n,k>,A >,BCRSMatrix<FieldMatrix<T,k,m>,A1 > >
517 typedef BCRSMatrix<typename MatMultMatResult<FieldMatrix<T,n,k>,FieldMatrix<T,k,m> >::type,
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>
539 template<
typename T,
typename A,
typename A1,
int n,
int k,
int m>
540 struct TransposedMatMultMatResult<BCRSMatrix<FieldMatrix<T,k,n>,A >,BCRSMatrix<FieldMatrix<T,k,m>,A1 > >
542 typedef BCRSMatrix<typename MatMultMatResult<FieldMatrix<T,n,k>,FieldMatrix<T,k,m> >::type,
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>
559 matMultMat<2>(res,mat, matt);
570 template<
class T,
class A,
class A1,
class A2,
int n,
int m,
int k>
574 matMultMat<0>(res,mat, matt);
585 template<
class T,
class A,
class A1,
class A2,
int n,
int m,
int k>
589 matMultMat<1>(res,mat, matt);
Implementation of the BCRSMatrix class.
void insert(size_type j)
put column index in row
Definition: bcrsmatrix.hh:1027
A sparse block matrix with compressed row storage.
Definition: bcrsmatrix.hh:414
friend class CreateIterator
allow CreateIterator to access internal data
Definition: bcrsmatrix.hh:1060
Iterator RowIterator
rename the iterators for easier access
Definition: bcrsmatrix.hh:652
Iterator end()
Get iterator to one beyond last row.
Definition: bcrsmatrix.hh:632
row_type::Iterator ColIterator
Iterator for the entries of each row.
Definition: bcrsmatrix.hh:655
A::size_type size_type
The type for the index access and the size.
Definition: bcrsmatrix.hh:448
CreateIterator createbegin()
get initial create iterator
Definition: bcrsmatrix.hh:1063
A dense n x m matrix.
Definition: fmatrix.hh:67
Implements a matrix constructed from a given type representing a field and compile-time given number ...
#define DUNE_THROW(E, m)
Definition: exceptions.hh:244
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:586
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:571
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:556
Dune namespace.
Definition: alignment.hh:14
Helper TMP to get the result type of a sparse matrix matrix multiplication ( )
Definition: matrixmatrix.hh:506
Helper TMP to get the result type of a sparse matrix matrix multiplication ( )
Definition: matrixmatrix.hh:531
Fallback implementation of the std::tuple class.