Skip to content
Snippets Groups Projects
Commit e7924a91 authored by Oliver Sander's avatar Oliver Sander
Browse files

Implement operator[] for random-access to matrix rows

We use the same trick with std::integral_constant as in the MultiTypeBlockVector code.
parent c48ce863
No related branches found
No related tags found
No related merge requests found
......@@ -232,6 +232,42 @@ namespace Dune {
return FirstRow::size();
}
/** \brief Random-access operator
*
* This method mimicks the behavior of normal vector access with square brackets like, e.g., m[5] = ....
* The problem is that the return type is different for each value of the argument in the brackets.
* Therefore we implement a trick using std::integral_constant. To access the first row of
* a MultiTypeBlockMatrix named m write
* \code
* std::integral_constant<int,0> _0;
* m[_0] = ...
* \endcode
* The name '_0' used here as a static replacement of the integer number zero is arbitrary.
* Any other variable name can be used. If you don't like the separate variable, you can writee
* \code
* m[std::integral_constant<int,0>()] = ...
* \endcode
*/
template< int index >
typename mpl::at_c<type,index>::type&
operator[] ( const std::integral_constant< int, index > indexVariable )
{
DUNE_UNUSED_PARAMETER(indexVariable);
return fusion::at_c<index>(*this);
}
/** \brief Const random-access operator
*
* This is the const version of the random-access operator. See the non-const version for a full
* explanation of how to use it.
*/
template< int index >
const typename mpl::at_c<type,index>::type&
operator[] ( const std::integral_constant< int, index > indexVariable ) const
{
DUNE_UNUSED_PARAMETER(indexVariable);
return fusion::at_c<index>(*this);
}
/**
* assignment operator
*/
......
......@@ -46,21 +46,22 @@ int main(int argc, char** argv) try
MultiTypeBlockMatrix<RowType0,RowType1> multiMatrix;
fusion::at_c<0>(multiMatrix)[_0].setSize(3,3);
fusion::at_c<0>(multiMatrix)[_1].setSize(3,2);
fusion::at_c<1>(multiMatrix)[_0].setSize(2,3);
fusion::at_c<1>(multiMatrix)[_1].setSize(2,2);
//fusion::at_c<0>(multiMatrix)[_0].setSize(3,3);
multiMatrix[_0][_0].setSize(3,3);
multiMatrix[_0][_1].setSize(3,2);
multiMatrix[_1][_0].setSize(2,3);
multiMatrix[_1][_1].setSize(2,2);
// lazy solution: initialize the entire matrix with zeros
fusion::at_c<0>(multiMatrix)[_0] = 0;
fusion::at_c<0>(multiMatrix)[_1] = 0;
fusion::at_c<1>(multiMatrix)[_0] = 0;
fusion::at_c<1>(multiMatrix)[_1] = 0;
multiMatrix[_0][_0] = 0;
multiMatrix[_0][_1] = 0;
multiMatrix[_1][_0] = 0;
multiMatrix[_1][_1] = 0;
printmatrix(std::cout, fusion::at_c<0>(multiMatrix)[_0], "(0,0)", "--");
printmatrix(std::cout, fusion::at_c<0>(multiMatrix)[_1], "(0,1)", "--");
printmatrix(std::cout, fusion::at_c<1>(multiMatrix)[_0], "(1,0)", "--");
printmatrix(std::cout, fusion::at_c<1>(multiMatrix)[_1], "(1,1)", "--");
printmatrix(std::cout, multiMatrix[_0][_0], "(0,0)", "--");
printmatrix(std::cout, multiMatrix[_0][_1], "(0,1)", "--");
printmatrix(std::cout, multiMatrix[_1][_0], "(1,0)", "--");
printmatrix(std::cout, multiMatrix[_1][_1], "(1,1)", "--");
// set up a test vector
MultiTypeBlockVector<BlockVector<FieldVector<double,3> >, BlockVector<FieldVector<double,1> > > multiVector;
......@@ -174,10 +175,10 @@ int main(int argc, char** argv) try
typedef Dune::MultiTypeBlockVector<BCRSMat,BCRSMat> BCRS_Row;
typedef Dune::MultiTypeBlockMatrix<BCRS_Row,BCRS_Row> CM_BCRS;
CM_BCRS A;
fusion::at_c<0>(A)[_0] = A11;
fusion::at_c<0>(A)[_1] = A12;
fusion::at_c<1>(A)[_0] = A21;
fusion::at_c<1>(A)[_1] = A22;
A[_0][_0] = A11;
A[_0][_1] = A12;
A[_1][_0] = A21;
A[_1][_1] = A22;
x = 1;
b = 1;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment