12#include <initializer_list>
21#include <dune/common/matrixconcepts.hh>
30 class ColumnVectorView
34 using value_type =
typename M::value_type;
35 using size_type =
typename M::size_type;
37 constexpr ColumnVectorView(M& matrix, size_type col) :
42 constexpr size_type N ()
const {
46 template<
class M_ = M,
47 std::enable_if_t<std::is_same_v<M_,M> and not std::is_const_v<M_>,
int> = 0>
48 constexpr value_type& operator[] (size_type row) {
49 return matrix_[row][col_];
52 constexpr const value_type& operator[] (size_type row)
const {
53 return matrix_[row][col_];
64 struct FieldTraits< Impl::ColumnVectorView<M> >
66 using field_type =
typename FieldTraits<M>::field_type;
67 using real_type =
typename FieldTraits<M>::real_type;
81 template<
class K,
int ROWS,
int COLS = ROWS >
class FieldMatrix;
84 template<
class K,
int ROWS,
int COLS >
85 struct DenseMatVecTraits< FieldMatrix<K,ROWS,COLS> >
87 typedef FieldMatrix<K,ROWS,COLS> derived_type;
90 typedef FieldVector<K,COLS> row_type;
92 typedef row_type &row_reference;
93 typedef const row_type &const_row_reference;
95 typedef std::array<row_type,ROWS> container_type;
97 typedef typename container_type::size_type size_type;
100 template<
class K,
int ROWS,
int COLS >
101 struct FieldTraits< FieldMatrix<K,ROWS,COLS> >
103 typedef typename FieldTraits<K>::field_type field_type;
104 typedef typename FieldTraits<K>::real_type real_type;
115 template<
class K,
int ROWS,
int COLS>
118 std::array< FieldVector<K,COLS>, ROWS > _data;
123 constexpr static int rows = ROWS;
125 constexpr static int cols = COLS;
141 assert(l.size() ==
rows);
142 for(std::size_t i=0; i<std::min<std::size_t>(ROWS, l.size()); ++i)
143 _data[i] = std::data(l)[i];
151 typename = std::enable_if_t<HasDenseMatrixAssigner<FieldMatrix, T>::value>>
158 using Base::operator=;
172 template <
typename T,
int rows,
int cols>
179 for(
int i = 0; i < ROWS; ++i )
180 for(
int j = 0; j < COLS; ++j )
181 AT[j][i] = (*
this)[i][j];
186 template <
class OtherScalar>
192 for (size_type i = 0; i < ROWS; ++i)
193 for (size_type j = 0; j < COLS; ++j)
194 result[i][j] = matrixA[i][j] + matrixB[i][j];
200 template <
class OtherScalar>
206 for (size_type i = 0; i < ROWS; ++i)
207 for (size_type j = 0; j < COLS; ++j)
208 result[i][j] = matrixA[i][j] - matrixB[i][j];
215 std::enable_if_t<IsNumber<Scalar>::value,
int> = 0>
220 for (size_type i = 0; i < ROWS; ++i)
221 for (size_type j = 0; j < COLS; ++j)
222 result[i][j] = matrix[i][j] * scalar;
229 std::enable_if_t<IsNumber<Scalar>::value,
int> = 0>
234 for (size_type i = 0; i < ROWS; ++i)
235 for (size_type j = 0; j < COLS; ++j)
236 result[i][j] = scalar * matrix[i][j];
243 std::enable_if_t<IsNumber<Scalar>::value,
int> = 0>
248 for (size_type i = 0; i < ROWS; ++i)
249 for (size_type j = 0; j < COLS; ++j)
250 result[i][j] = matrix[i][j] / scalar;
257 template <
class OtherScalar,
int otherCols>
263 for (size_type i = 0; i < matrixA.mat_rows(); ++i)
264 for (size_type j = 0; j < matrixB.mat_cols(); ++j)
267 for (size_type k = 0; k < matrixA.mat_cols(); ++k)
268 result[i][j] += matrixA[i][k] * matrixB[k][j];
280 template <
class OtherMatrix, std::enable_if_t<
281 Impl::IsStaticSizeMatrix_v<OtherMatrix>
282 and not Impl::IsFieldMatrix_v<OtherMatrix>
285 const OtherMatrix& matrixB)
287 using Field =
typename PromotionTraits<K, typename OtherMatrix::field_type>::PromotedType;
289 for (std::size_t j=0; j<
rows; ++j)
290 matrixB.
mtv(matrixA[j], result[j]);
300 template <
class OtherMatrix, std::enable_if_t<
301 Impl::IsStaticSizeMatrix_v<OtherMatrix>
302 and not Impl::IsFieldMatrix_v<OtherMatrix>
304 friend constexpr auto operator* (
const OtherMatrix& matrixA,
307 using Field =
typename PromotionTraits<K, typename OtherMatrix::field_type>::PromotedType;
309 for (std::size_t j=0; j<
cols; ++j)
311 auto B_j = Impl::ColumnVectorView(matrixB, j);
312 auto result_j = Impl::ColumnVectorView(result, j);
313 matrixA.mv(B_j, result_j);
324 for (size_type i=0; i<l; i++) {
325 for (size_type j=0; j<
cols; j++) {
327 for (size_type k=0; k<
rows; k++)
328 C[i][j] +=
M[i][k]*(*
this)[k][j];
337 template <
int r,
int c>
340 static_assert(r == c,
"Cannot rightmultiply with non-square matrix");
341 static_assert(r ==
cols,
"Size mismatch");
344 for (size_type i=0; i<
rows; i++)
345 for (size_type j=0; j<
cols; j++) {
347 for (size_type k=0; k<
cols; k++)
348 (*
this)[i][j] += C[i][k]*
M[k][j];
359 for (size_type i=0; i<
rows; i++) {
360 for (size_type j=0; j<l; j++) {
362 for (size_type k=0; k<
cols; k++)
363 C[i][j] += (*
this)[i][k]*
M[k][j];
370 static constexpr size_type mat_rows() {
return ROWS; }
371 static constexpr size_type mat_cols() {
return COLS; }
373 constexpr row_reference mat_access ( size_type i )
379 constexpr const_row_reference mat_access ( size_type i )
const
390 class FieldMatrix<K,1,1> :
public DenseMatrix< FieldMatrix<K,1,1> >
392 FieldVector<K,1> _data;
393 typedef DenseMatrix< FieldMatrix<K,1,1> > Base;
413 constexpr static int rows = 1;
416 constexpr static int cols = 1;
427 std::copy_n(l.begin(), std::min<std::size_t>(1, l.size()), &_data);
431 typename = std::enable_if_t<HasDenseMatrixAssigner<FieldMatrix, T>::value>>
437 using Base::operator=;
440 constexpr FieldMatrix<K, 1, 1>
transposed()
const
446 template <
class OtherScalar>
448 const FieldMatrix<OtherScalar,1,1>& matrixB)
450 return FieldMatrix<typename PromotionTraits<K,OtherScalar>::PromotedType,1,1>{matrixA[0][0] + matrixB[0][0]};
455 std::enable_if_t<IsNumber<Scalar>::value,
int> = 0>
459 return FieldMatrix<typename PromotionTraits<K,Scalar>::PromotedType,1,1>{matrix[0][0] + scalar};
464 std::enable_if_t<IsNumber<Scalar>::value,
int> = 0>
468 return FieldMatrix<typename PromotionTraits<Scalar,K>::PromotedType,1,1>{scalar + matrix[0][0]};
472 template <
class OtherScalar>
474 const FieldMatrix<OtherScalar,1,1>& matrixB)
476 return FieldMatrix<typename PromotionTraits<K,OtherScalar>::PromotedType,1,1>{matrixA[0][0] - matrixB[0][0]};
481 std::enable_if_t<IsNumber<Scalar>::value,
int> = 0>
485 return FieldMatrix<typename PromotionTraits<K,Scalar>::PromotedType,1,1>{matrix[0][0] - scalar};
490 std::enable_if_t<IsNumber<Scalar>::value,
int> = 0>
494 return FieldMatrix<typename PromotionTraits<Scalar,K>::PromotedType,1,1>{scalar - matrix[0][0]};
499 std::enable_if_t<IsNumber<Scalar>::value,
int> = 0>
502 return FieldMatrix<typename PromotionTraits<K,Scalar>::PromotedType,1,1> {matrix[0][0] * scalar};
507 std::enable_if_t<IsNumber<Scalar>::value,
int> = 0>
510 return FieldMatrix<typename PromotionTraits<K,Scalar>::PromotedType,1,1> {scalar * matrix[0][0]};
515 std::enable_if_t<IsNumber<Scalar>::value,
int> = 0>
518 return FieldMatrix<typename PromotionTraits<K,Scalar>::PromotedType,1,1> {matrix[0][0] / scalar};
525 template <
class OtherScalar,
int otherCols>
527 const FieldMatrix<OtherScalar, 1, otherCols>& matrixB)
529 FieldMatrix<typename PromotionTraits<K,OtherScalar>::PromotedType,1,otherCols> result;
531 for (size_type j = 0; j < matrixB.mat_cols(); ++j)
532 result[0][j] = matrixA[0][0] * matrixB[0][j];
543 template <
class OtherMatrix, std::enable_if_t<
544 Impl::IsStaticSizeMatrix_v<OtherMatrix>
545 and not Impl::IsFieldMatrix_v<OtherMatrix>
546 and (OtherMatrix::rows==1)
549 const OtherMatrix& matrixB)
551 using Field =
typename PromotionTraits<K, typename OtherMatrix::field_type>::PromotedType;
553 for (std::size_t j=0; j<
rows; ++j)
554 matrixB.
mtv(matrixA[j], result[j]);
564 template <
class OtherMatrix, std::enable_if_t<
565 Impl::IsStaticSizeMatrix_v<OtherMatrix>
566 and not Impl::IsFieldMatrix_v<OtherMatrix>
567 and (OtherMatrix::cols==1)
569 friend constexpr auto operator* (
const OtherMatrix& matrixA,
572 using Field =
typename PromotionTraits<K, typename OtherMatrix::field_type>::PromotedType;
574 for (std::size_t j=0; j<
cols; ++j)
576 auto B_j = Impl::ColumnVectorView(matrixB, j);
577 auto result_j = Impl::ColumnVectorView(result, j);
578 matrixA.mv(B_j, result_j);
585 constexpr FieldMatrix<K,l,1>
leftmultiplyany (
const FieldMatrix<K,l,1>&
M)
const
587 FieldMatrix<K,l,1> C;
588 for (size_type j=0; j<l; j++)
589 C[j][0] =
M[j][0]*(*
this)[0][0];
602 constexpr FieldMatrix<K,1,l>
rightmultiplyany (
const FieldMatrix<K,1,l>&
M)
const
604 FieldMatrix<K,1,l> C;
606 for (size_type j=0; j<l; j++)
607 C[0][j] =
M[0][j]*_data[0];
612 static constexpr size_type mat_rows() {
return 1; }
613 static constexpr size_type mat_cols() {
return 1; }
615 constexpr row_reference mat_access ([[maybe_unused]] size_type i)
621 constexpr const_row_reference mat_access ([[maybe_unused]] size_type i)
const
657 constexpr operator const K& ()
const {
return _data[0]; }
663 std::ostream& operator<< (std::ostream& s,
const FieldMatrix<K,1,1>& a)
671 namespace FMatrixHelp {
674 template <
typename K>
677 using real_type =
typename FieldTraits<K>::real_type;
678 inverse[0][0] = real_type(1.0)/matrix[0][0];
683 template <
typename K>
691 template <
typename K>
694 using real_type =
typename FieldTraits<K>::real_type;
696 K det = (matrix[0][0]*matrix[1][1] - matrix[0][1]*matrix[1][0]);
697 K det_1 = real_type(1.0)/det;
698 inverse[0][0] = matrix[1][1] * det_1;
699 inverse[0][1] = - matrix[0][1] * det_1;
700 inverse[1][0] = - matrix[1][0] * det_1;
701 inverse[1][1] = matrix[0][0] * det_1;
707 template <
typename K>
710 using real_type =
typename FieldTraits<K>::real_type;
712 K det = (matrix[0][0]*matrix[1][1] - matrix[0][1]*matrix[1][0]);
713 K det_1 = real_type(1.0)/det;
714 inverse[0][0] = matrix[1][1] * det_1;
715 inverse[1][0] = - matrix[0][1] * det_1;
716 inverse[0][1] = - matrix[1][0] * det_1;
717 inverse[1][1] = matrix[0][0] * det_1;
722 template <
typename K>
725 using real_type =
typename FieldTraits<K>::real_type;
727 K t4 = matrix[0][0] * matrix[1][1];
728 K t6 = matrix[0][0] * matrix[1][2];
729 K t8 = matrix[0][1] * matrix[1][0];
730 K t10 = matrix[0][2] * matrix[1][0];
731 K t12 = matrix[0][1] * matrix[2][0];
732 K t14 = matrix[0][2] * matrix[2][0];
734 K det = (t4*matrix[2][2]-t6*matrix[2][1]-t8*matrix[2][2]+
735 t10*matrix[2][1]+t12*matrix[1][2]-t14*matrix[1][1]);
736 K t17 = real_type(1.0)/det;
738 inverse[0][0] = (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1])*t17;
739 inverse[0][1] = -(matrix[0][1] * matrix[2][2] - matrix[0][2] * matrix[2][1])*t17;
740 inverse[0][2] = (matrix[0][1] * matrix[1][2] - matrix[0][2] * matrix[1][1])*t17;
741 inverse[1][0] = -(matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0])*t17;
742 inverse[1][1] = (matrix[0][0] * matrix[2][2] - t14) * t17;
743 inverse[1][2] = -(t6-t10) * t17;
744 inverse[2][0] = (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]) * t17;
745 inverse[2][1] = -(matrix[0][0] * matrix[2][1] - t12) * t17;
746 inverse[2][2] = (t4-t8) * t17;
752 template <
typename K>
755 using real_type =
typename FieldTraits<K>::real_type;
757 K t4 = matrix[0][0] * matrix[1][1];
758 K t6 = matrix[0][0] * matrix[1][2];
759 K t8 = matrix[0][1] * matrix[1][0];
760 K t10 = matrix[0][2] * matrix[1][0];
761 K t12 = matrix[0][1] * matrix[2][0];
762 K t14 = matrix[0][2] * matrix[2][0];
764 K det = (t4*matrix[2][2]-t6*matrix[2][1]-t8*matrix[2][2]+
765 t10*matrix[2][1]+t12*matrix[1][2]-t14*matrix[1][1]);
766 K t17 = real_type(1.0)/det;
768 inverse[0][0] = (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1])*t17;
769 inverse[1][0] = -(matrix[0][1] * matrix[2][2] - matrix[0][2] * matrix[2][1])*t17;
770 inverse[2][0] = (matrix[0][1] * matrix[1][2] - matrix[0][2] * matrix[1][1])*t17;
771 inverse[0][1] = -(matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0])*t17;
772 inverse[1][1] = (matrix[0][0] * matrix[2][2] - t14) * t17;
773 inverse[2][1] = -(t6-t10) * t17;
774 inverse[0][2] = (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]) * t17;
775 inverse[1][2] = -(matrix[0][0] * matrix[2][1] - t12) * t17;
776 inverse[2][2] = (t4-t8) * t17;
782 template<
class K,
int m,
int n,
int p >
789 for( size_type i = 0; i < m; ++i )
791 for( size_type j = 0; j < p; ++j )
793 ret[ i ][ j ] = K( 0 );
794 for( size_type k = 0; k < n; ++k )
795 ret[ i ][ j ] += A[ i ][ k ] * B[ k ][ j ];
801 template <
typename K,
int rows,
int cols>
804 typedef typename FieldMatrix<K,rows,cols>::size_type size_type;
806 for(size_type i=0; i<cols; i++)
807 for(size_type j=0; j<cols; j++)
810 for(size_type k=0; k<rows; k++)
811 ret[i][j]+=matrix[k][i]*matrix[k][j];
815 using Dune::DenseMatrixHelp::multAssign;
818 template <
typename K,
int rows,
int cols>
821 typedef typename FieldMatrix<K,rows,cols>::size_type size_type;
823 for(size_type i=0; i<cols; ++i)
826 for(size_type j=0; j<rows; ++j)
827 ret[i] += matrix[j][i]*x[j];
832 template <
typename K,
int rows,
int cols>
836 multAssign(matrix,x,ret);
841 template <
typename K,
int rows,
int cols>
Macro for wrapping boundary checks.
A dense n x m matrix.
Definition: densematrix.hh:145
constexpr derived_type & operator+=(const DenseMatrix< Other > &x)
vector space addition
Definition: densematrix.hh:294
constexpr derived_type & operator*=(const field_type &k)
vector space multiplication with scalar
Definition: densematrix.hh:326
constexpr derived_type & operator-=(const DenseMatrix< Other > &x)
vector space subtraction
Definition: densematrix.hh:317
constexpr size_type M() const
number of columns
Definition: densematrix.hh:708
FieldMatrix< K, ROWS, COLS > & rightmultiply(const DenseMatrix< M2 > &M)
Multiplies M from the right to this matrix.
Definition: densematrix.hh:650
constexpr derived_type & operator/=(const field_type &k)
vector space division by scalar
Definition: densematrix.hh:334
constexpr derived_type operator-() const
Matrix negation.
Definition: densematrix.hh:303
constexpr void mtv(const X &x, Y &y) const
y = A^T x
Definition: densematrix.hh:392
static constexpr int blocklevel
The number of block levels we contain. This is the leaf, that is, 1.
Definition: densematrix.hh:183
Traits::row_type row_type
The type used to represent a row (must fulfill the Dune::DenseVector interface)
Definition: densematrix.hh:174
Traits::size_type size_type
The type used for the index access and size operation.
Definition: densematrix.hh:171
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:180
Traits::row_reference row_reference
The type used to represent a reference to a row (usually row_type &)
Definition: densematrix.hh:177
A dense n x m matrix.
Definition: fmatrix.hh:117
constexpr FieldMatrix()=default
Default constructor.
constexpr 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:355
constexpr FieldMatrix & rightmultiply(const FieldMatrix< K, r, c > &M)
Multiplies M from the right to this matrix.
Definition: fmatrix.hh:338
friend constexpr auto operator*(const FieldMatrix &matrix, Scalar scalar)
vector space multiplication with scalar
Definition: fmatrix.hh:216
constexpr FieldMatrix & operator=(const FieldMatrix< T, ROWS, COLS > &x)
copy assignment from FieldMatrix over a different field
Definition: fmatrix.hh:165
constexpr FieldMatrix(T const &rhs)
copy constructor from assignable type T
Definition: fmatrix.hh:152
FieldMatrix & operator=(FieldMatrix< T, rows, cols > const &)=delete
no copy assignment from FieldMatrix of different size
constexpr FieldMatrix(std::initializer_list< Dune::FieldVector< K, cols > > const &l)
Constructor initializing the matrix from a list of vector.
Definition: fmatrix.hh:140
static constexpr int rows
The number of rows.
Definition: fmatrix.hh:123
constexpr FieldMatrix & operator=(const FieldMatrix &)=default
copy assignment operator
static constexpr int cols
The number of columns.
Definition: fmatrix.hh:125
constexpr FieldMatrix< K, COLS, ROWS > transposed() const
Return transposed of the matrix as FieldMatrix.
Definition: fmatrix.hh:176
friend constexpr auto operator/(const FieldMatrix &matrix, Scalar scalar)
vector space division by scalar
Definition: fmatrix.hh:244
friend constexpr auto operator+(const FieldMatrix &matrixA, const FieldMatrix< OtherScalar, ROWS, COLS > &matrixB)
vector space addition – two-argument version
Definition: fmatrix.hh:187
constexpr 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:320
FieldMatrix(const FieldMatrix &)=default
copy constructor
Implements a matrix constructed from a given type representing a field and a compile-time given numbe...
A few common exception classes.
Traits for type conversions and type information.
static constexpr 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:819
static constexpr FieldVector< K, cols > multTransposed(const FieldMatrix< K, rows, cols > &matrix, const FieldVector< K, rows > &x)
calculates ret = matrix^T * x
Definition: fmatrix.hh:842
static constexpr K invertMatrix_retTransposed(const FieldMatrix< K, 1, 1 > &matrix, FieldMatrix< K, 1, 1 > &inverse)
invert scalar without changing the original matrix
Definition: fmatrix.hh:684
static constexpr void multTransposedMatrix(const FieldMatrix< K, rows, cols > &matrix, FieldMatrix< K, cols, cols > &ret)
calculates ret= A_t*A
Definition: fmatrix.hh:802
static constexpr 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:783
static constexpr K invertMatrix(const FieldMatrix< K, 1, 1 > &matrix, FieldMatrix< K, 1, 1 > &inverse)
invert scalar without changing the original matrix
Definition: fmatrix.hh:675
static constexpr FieldVector< K, rows > mult(const FieldMatrix< K, rows, cols > &matrix, const FieldVector< K, cols > &x)
calculates ret = matrix * x
Definition: fmatrix.hh:833
Eigenvalue computations for the FieldMatrix class.
Implements a vector constructed from a given type representing a field and a compile-time given size.
#define DUNE_ASSERT_BOUNDS(cond)
If DUNE_CHECK_BOUNDS is defined: check if condition cond holds; otherwise, do nothing.
Definition: boundschecking.hh:30
typename Overloads::ScalarType< std::decay_t< V > >::type Scalar
Element type of some SIMD type.
Definition: interface.hh:235
Dune namespace.
Definition: alignedallocator.hh:13
Various precision settings for calculations with FieldMatrix and FieldVector.