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

Implement matrix-matrix multiplication for FieldMatrix

parent bb1c92df
No related branches found
No related tags found
1 merge request!705Add various binary operators to DenseVector and DenseMatrix
Pipeline #21282 passed
......@@ -40,6 +40,7 @@
- Matrix = Scalar * Matrix
- Matrix = Matrix * Scalar
- Matrix = Matrix / Scalar
- Matrix = Matrix * Matrix
Note that the operators
- Vector = Vector + Vector
- Vector = Vector - Vector
......
......@@ -193,6 +193,25 @@ namespace Dune
return result;
}
/** \brief Matrix-matrix multiplication
*/
template <class OtherScalar, int otherCols>
friend auto operator* ( const FieldMatrix& matrixA,
const FieldMatrix<OtherScalar, COLS, otherCols>& matrixB)
{
FieldMatrix<typename PromotionTraits<K,OtherScalar>::PromotedType,ROWS,otherCols> result;
for (size_type i = 0; i < matrixA.mat_rows(); ++i)
for (size_type j = 0; j < matrixB.mat_cols(); ++j)
{
result[i][j] = 0;
for (size_type k = 0; k < matrixA.mat_cols(); ++k)
result[i][j] += matrixA[i][k] * matrixB[k][j];
}
return result;
}
//! Multiplies M from the left to this matrix, this matrix is not modified
template<int l>
FieldMatrix<K,l,cols> leftmultiplyany (const FieldMatrix<K,l,rows>& M) const
......@@ -398,6 +417,20 @@ namespace Dune
//===== solve
/** \brief Matrix-matrix multiplication
*/
template <class OtherScalar, int otherCols>
friend auto operator* ( const FieldMatrix& matrixA,
const FieldMatrix<OtherScalar, 1, otherCols>& matrixB)
{
FieldMatrix<typename PromotionTraits<K,OtherScalar>::PromotedType,1,otherCols> result;
for (size_type j = 0; j < matrixB.mat_cols(); ++j)
result[0][j] = matrixA[0][0] * matrixB[0][j];
return result;
}
//! Multiplies M from the left to this matrix, this matrix is not modified
template<int l>
FieldMatrix<K,l,1> leftmultiplyany (const FieldMatrix<K,l,1>& M) const
......
......@@ -469,6 +469,21 @@ void test_matrix()
DUNE_THROW(FMatrixError, "Return value of operator-(matrix) incorrect!");
}
// Matrix * Matrix
{
auto transposed = [](const FM& A)
{
FieldMatrix<typename FM::field_type,FM::cols,FM::rows> AT;
for (int i=0; i<AT.rows; i++)
for (int j=0; j<AT.cols; j++)
AT[i][j] = A[j][i];
return AT;
};
DUNE_UNUSED auto product = transposed(A) * A;
}
}
{
using std::abs;
......
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