3 #ifndef DUNE_FMATRIX_HH 4 #define DUNE_FMATRIX_HH 10 #include <initializer_list> 32 template<
class K,
int ROWS,
int COLS >
class FieldMatrix;
34 template<
class K,
int ROWS,
int COLS >
47 typedef typename container_type::size_type
size_type;
50 template<
class K,
int ROWS,
int COLS >
65 template<
class K,
int ROWS,
int COLS>
68 Dune::array< FieldVector<K,COLS>, ROWS > _data;
93 template<
class Other >
101 FieldMatrix (std::initializer_list<std::initializer_list<K> >
const &ll)
103 assert(ll.size() == rows);
104 std::copy_n(ll.begin(), std::min(static_cast<std::size_t>(ROWS),
112 assert(l.size() == rows);
113 std::copy_n(l.begin(), std::min(static_cast<std::size_t>(ROWS),
119 using Base::operator=;
127 for (size_type i=0; i<l; i++) {
128 for (size_type j=0; j<cols; j++) {
130 for (size_type k=0; k<rows; k++)
131 C[i][j] += M[i][k]*(*
this)[k][j];
137 using Base::rightmultiply;
140 template <
int r,
int c>
143 static_assert(r ==
c,
"Cannot rightmultiply with non-square matrix");
144 static_assert(r == cols,
"Size mismatch");
147 for (size_type i=0; i<rows; i++)
148 for (size_type j=0; j<cols; j++) {
150 for (size_type k=0; k<cols; k++)
151 (*
this)[i][j] += C[i][k]*M[k][j];
162 for (size_type i=0; i<rows; i++) {
163 for (size_type j=0; j<l; j++) {
165 for (size_type k=0; k<cols; k++)
166 C[i][j] += (*
this)[i][k]*M[k][j];
189 #ifndef DOXYGEN // hide specialization 239 template<
class Other >
252 for (size_type j=0; j<l; j++)
253 C[j][0] = M[j][0]*(*
this)[0][0];
270 for (size_type j=0; j<l; j++)
271 C[0][j] = M[0][j]*_data[0];
279 row_reference mat_access ( size_type i )
286 const_row_reference mat_access ( size_type i )
const 323 operator const K& ()
const {
return _data[0]; }
329 std::ostream& operator<< (std::ostream& s, const FieldMatrix<K,1,1>& a)
337 namespace FMatrixHelp {
340 template <
typename K>
343 inverse[0][0] = 1.0/matrix[0][0];
348 template <
typename K>
356 template <
typename K>
360 K det = (matrix[0][0]*matrix[1][1] - matrix[0][1]*matrix[1][0]);
362 inverse[0][0] = matrix[1][1] * det_1;
363 inverse[0][1] = - matrix[0][1] * det_1;
364 inverse[1][0] = - matrix[1][0] * det_1;
365 inverse[1][1] = matrix[0][0] * det_1;
371 template <
typename K>
375 K det = (matrix[0][0]*matrix[1][1] - matrix[0][1]*matrix[1][0]);
377 inverse[0][0] = matrix[1][1] * det_1;
378 inverse[1][0] = - matrix[0][1] * det_1;
379 inverse[0][1] = - matrix[1][0] * det_1;
380 inverse[1][1] = matrix[0][0] * det_1;
385 template <
typename K>
389 K t4 = matrix[0][0] * matrix[1][1];
390 K t6 = matrix[0][0] * matrix[1][2];
391 K t8 = matrix[0][1] * matrix[1][0];
392 K t10 = matrix[0][2] * matrix[1][0];
393 K t12 = matrix[0][1] * matrix[2][0];
394 K t14 = matrix[0][2] * matrix[2][0];
396 K det = (t4*matrix[2][2]-t6*matrix[2][1]-t8*matrix[2][2]+
397 t10*matrix[2][1]+t12*matrix[1][2]-t14*matrix[1][1]);
400 inverse[0][0] = (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1])*t17;
401 inverse[0][1] = -(matrix[0][1] * matrix[2][2] - matrix[0][2] * matrix[2][1])*t17;
402 inverse[0][2] = (matrix[0][1] * matrix[1][2] - matrix[0][2] * matrix[1][1])*t17;
403 inverse[1][0] = -(matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0])*t17;
404 inverse[1][1] = (matrix[0][0] * matrix[2][2] - t14) * t17;
405 inverse[1][2] = -(t6-t10) * t17;
406 inverse[2][0] = (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]) * t17;
407 inverse[2][1] = -(matrix[0][0] * matrix[2][1] - t12) * t17;
408 inverse[2][2] = (t4-t8) * t17;
414 template <
typename K>
418 K t4 = matrix[0][0] * matrix[1][1];
419 K t6 = matrix[0][0] * matrix[1][2];
420 K t8 = matrix[0][1] * matrix[1][0];
421 K t10 = matrix[0][2] * matrix[1][0];
422 K t12 = matrix[0][1] * matrix[2][0];
423 K t14 = matrix[0][2] * matrix[2][0];
425 K det = (t4*matrix[2][2]-t6*matrix[2][1]-t8*matrix[2][2]+
426 t10*matrix[2][1]+t12*matrix[1][2]-t14*matrix[1][1]);
429 inverse[0][0] = (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1])*t17;
430 inverse[1][0] = -(matrix[0][1] * matrix[2][2] - matrix[0][2] * matrix[2][1])*t17;
431 inverse[2][0] = (matrix[0][1] * matrix[1][2] - matrix[0][2] * matrix[1][1])*t17;
432 inverse[0][1] = -(matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0])*t17;
433 inverse[1][1] = (matrix[0][0] * matrix[2][2] - t14) * t17;
434 inverse[2][1] = -(t6-t10) * t17;
435 inverse[0][2] = (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]) * t17;
436 inverse[1][2] = -(matrix[0][0] * matrix[2][1] - t12) * t17;
437 inverse[2][2] = (t4-t8) * t17;
443 template<
class K,
int m,
int n,
int p >
450 for( size_type i = 0; i < m; ++i )
452 for( size_type j = 0; j < p; ++j )
454 ret[ i ][ j ] = K( 0 );
455 for( size_type k = 0; k < n; ++k )
456 ret[ i ][ j ] += A[ i ][ k ] * B[ k ][ j ];
462 template <
typename K,
int rows,
int cols>
467 for(size_type i=0; i<cols; i++)
468 for(size_type j=0; j<cols; j++)
471 for(size_type k=0; k<rows; k++)
472 ret[i][j]+=matrix[k][i]*matrix[k][j];
479 template <
typename K,
int rows,
int cols>
484 for(size_type i=0; i<cols; ++i)
487 for(size_type j=0; j<rows; ++j)
488 ret[i] += matrix[j][i]*x[j];
493 template <
typename K,
int rows,
int cols>
502 template <
typename K,
int rows,
int cols>
FieldMatrix(const Other &other)
Constructor initializing the whole matrix with a scalar.
Definition: fmatrix.hh:94
Base::size_type size_type
Definition: fmatrix.hh:80
Definition of the DUNE_CONSTEXPR macro.
static void multAssignTransposed(const FieldMatrix< K, rows, cols > &matrix, const FieldVector< K, rows > &x, FieldVector< K, cols > &ret)
calculates ret = matrix^T * x
Definition: fmatrix.hh:480
row_type & row_reference
Definition: fmatrix.hh:42
T real_type
export the type representing the real type of the field
Definition: ftraits.hh:27
static void multTransposedMatrix(const FieldMatrix< K, rows, cols > &matrix, FieldMatrix< K, cols, cols > &ret)
calculates ret= A_t*A
Definition: fmatrix.hh:463
Various precision settings for calculations with FieldMatrix and FieldVector.
Dune::array< row_type, ROWS > container_type
Definition: fmatrix.hh:45
static void multMatrix(const FieldMatrix< K, m, n > &A, const FieldMatrix< K, n, p > &B, FieldMatrix< K, m, p > &ret)
calculates ret = A * B
Definition: fmatrix.hh:444
#define DUNE_UNUSED_PARAMETER(parm)
A macro to mark intentional unused function parameters with.
Definition: unused.hh:18
Base::const_row_reference const_row_reference
Definition: fmatrix.hh:84
A dense n x m matrix.
Definition: densematrix.hh:22
container_type::size_type size_type
Definition: fmatrix.hh:47
FieldMatrix(std::initializer_list< Dune::FieldVector< K, cols > > const &l)
Constructor initializing the matrix from a list of vector.
Definition: fmatrix.hh:111
Base::row_reference row_reference
Definition: fmatrix.hh:83
static K invertMatrix(const FieldMatrix< K, 3, 3 > &matrix, FieldMatrix< K, 3, 3 > &inverse)
invert 3x3 Matrix without changing the original matrix
Definition: fmatrix.hh:386
const row_type & const_row_reference
Definition: fmatrix.hh:43
Definition: ftraits.hh:22
Traits::row_type row_type
The type used to represent a row (must fulfill the Dune::DenseVector interface)
Definition: densematrix.hh:211
static void multAssign(const DenseMatrix< MAT > &matrix, const DenseVector< V1 > &x, DenseVector< V2 > &ret)
calculates ret = matrix * x
Definition: densematrix.hh:1181
Traits::row_reference row_reference
The type used to represent a reference to a row (usually row_type &)
Definition: densematrix.hh:214
FieldTraits< K >::real_type real_type
Definition: fmatrix.hh:54
DUNE_CONSTEXPR size_type mat_cols() const
Definition: fmatrix.hh:174
Eigenvalue computations for the FieldMatrix class.
static FieldVector< K, rows > mult(const FieldMatrix< K, rows, cols > &matrix, const FieldVector< K, cols > &x)
calculates ret = matrix * x
Definition: fmatrix.hh:494
Traits::const_row_reference const_row_reference
The type used to represent a reference to a constant row (usually const row_type &) ...
Definition: densematrix.hh:217
#define DUNE_CONSTEXPR
Set method or expression constexpr if supported by the compiler.
Definition: constexpr.hh:21
FieldTraits< K >::field_type field_type
Definition: fmatrix.hh:53
FieldMatrix< K, rows, l > rightmultiplyany(const FieldMatrix< K, cols, l > &M) const
Multiplies M from the right to this matrix, this matrix is not modified.
Definition: fmatrix.hh:158
char c
Definition: alignment.hh:33
FieldMatrix()
Default constructor.
Definition: fmatrix.hh:89
A few common exception classes.
Implements a matrix constructed from a given type representing a field and a compile-time given numbe...
FieldMatrix< K, ROWS, COLS > derived_type
Definition: fmatrix.hh:37
DUNE_CONSTEXPR size_type mat_rows() const
Definition: fmatrix.hh:173
Definition: matvectraits.hh:29
FieldVector< K, COLS > row_type
Definition: fmatrix.hh:40
static K invertMatrix_retTransposed(const FieldMatrix< K, 3, 3 > &matrix, FieldMatrix< K, 3, 3 > &inverse)
invert 3x3 Matrix without changing the original matrix
Definition: fmatrix.hh:415
Base::row_type row_type
Definition: fmatrix.hh:81
FieldMatrix & rightmultiply(const FieldMatrix< K, r, c > &M)
Multiplies M from the right to this matrix.
Definition: fmatrix.hh:141
const_row_reference mat_access(size_type i) const
Definition: fmatrix.hh:182
static FieldVector< K, cols > multTransposed(const FieldMatrix< K, rows, cols > &matrix, const FieldVector< K, rows > &x)
calculates ret = matrix^T * x
Definition: fmatrix.hh:503
T field_type
export the type representing the field
Definition: ftraits.hh:25
FieldMatrix(std::initializer_list< std::initializer_list< K > > const &ll)
Constructor initializing the matrix from a list of lists of scalars.
Definition: fmatrix.hh:101
A dense n x m matrix.
Definition: densematrix.hh:35
Dune namespace.
Definition: alignment.hh:9
K value_type
Definition: fmatrix.hh:46
FieldMatrix< K, l, cols > leftmultiplyany(const FieldMatrix< K, l, rows > &M) const
Multiplies M from the left to this matrix, this matrix is not modified.
Definition: fmatrix.hh:123
row_reference mat_access(size_type i)
Definition: fmatrix.hh:176
Traits::size_type size_type
The type used for the index access and size operation.
Definition: densematrix.hh:208
you have to specialize this structure for any type that should be assignable to a DenseMatrix ...
Definition: densematrix.hh:71
Implements a vector constructed from a given type representing a field and a compile-time given size...