5#ifndef DUNE_COMMON_CHECK_MATRIX_INTERFACE_HH
6#define DUNE_COMMON_CHECK_MATRIX_INTERFACE_HH
30 template<
class,
int,
int >
class FieldMatrix;
31 template<
class,
int >
class DiagonalMatrix;
37namespace CheckMatrixInterface
40 namespace Capabilities
46 template<
class Matrix >
49 static const bool v =
false;
50 static const int rows = ~0;
51 static const int cols = ~0;
54 template<
class Matrix >
55 struct hasStaticSizes< const Matrix >
57 static const bool v = hasStaticSizes< Matrix >::v;
58 static const int rows = hasStaticSizes< Matrix >::rows;
59 static const int cols = hasStaticSizes< Matrix >::cols;
67 template<
class Matrix >
70 static const bool v =
false;
73 template<
class Matrix >
74 struct isSquare< const Matrix >
76 static const bool v = isSquare< Matrix >::v;
84 template<
class K,
int r,
int c >
85 struct hasStaticSizes<
Dune::FieldMatrix< K, r, c > >
87 static const bool v =
true;
88 static const int rows = r;
89 static const int cols = c;
92 template<
class K,
int rows,
int cols >
93 struct isSquare<
Dune::FieldMatrix< K, rows, cols > >
95 static const bool v = ( rows == cols );
103 template<
class K,
int n >
104 struct hasStaticSizes<
Dune::DiagonalMatrix<K,n> >
106 static const bool v =
true;
107 static const int rows = n;
108 static const int cols = n;
111 template<
class K,
int n >
112 struct isSquare<
Dune::DiagonalMatrix<K,n> >
114 static const bool v =
true;
124 template<
class Matrix >
125 struct UseDynamicVector
127 typedef typename Matrix::value_type value_type;
130 typedef domain_type range_type;
132 static domain_type domain (
const Matrix &matrix, value_type v = value_type() )
134 return domain_type( matrix.M(), v );
137 static range_type range (
const Matrix &matrix, value_type v = value_type() )
139 return range_type( matrix.N(), v );
148 template<
class K,
int rows,
int cols >
149 struct UseFieldVector
151 typedef K value_type;
156 template<
class Matrix >
157 static domain_type domain (
const Matrix &, value_type v = value_type() )
159 return domain_type( v );
162 template<
class Matrix >
163 static range_type range (
const Matrix &, value_type v = value_type() )
165 return range_type( v );
174 template< class Matrix, bool hasStaticSizes = Capabilities::hasStaticSizes< Matrix >::v >
175 struct MatrixSizeHelper;
177 template<
class Matrix >
178 struct MatrixSizeHelper< Matrix, false >
180 typedef typename Matrix::size_type size_type;
181 static const size_type rows (
const Matrix &matrix ) {
return matrix.rows(); }
182 static const size_type cols (
const Matrix &matrix ) {
return matrix.cols(); }
185 template<
class Matrix >
186 struct MatrixSizeHelper< Matrix, true >
188 typedef typename Matrix::size_type size_type;
189 static const size_type rows (
const Matrix & ) {
return Matrix::rows; }
190 static const size_type cols (
const Matrix & ) {
return Matrix::cols; }
198 template< class Matrix, class Traits, bool isSquare = Capabilities::isSquare< Matrix >::v >
199 struct CheckIfSquareMatrix;
201 template<
class Matrix,
class Traits >
202 struct CheckIfSquareMatrix< Matrix, Traits, false >
204 static void apply (
const Matrix &) {}
206 static void apply ( Matrix &) {}
209 template<
class Matrix,
class Traits >
210 struct CheckIfSquareMatrix< Matrix, Traits, true >
212 typedef typename Matrix::value_type value_type;
214 static value_type tolerance ()
216 return value_type( 16 ) * std::numeric_limits< value_type >::epsilon();
219 static void apply (
const Matrix &matrix )
221 const value_type determinant = matrix.determinant();
222 if( determinant > tolerance() )
224 typename Traits::domain_type x = Traits::domain( matrix );
225 const typename Traits::range_type b = Traits::range( matrix );
226 matrix.solve( x, b );
230 static void apply ( Matrix &matrix )
232 apply(
const_cast< const Matrix &
>( matrix ) );
233 if( matrix.determinant() > tolerance() )
243 template<
class Matrix,
class Traits >
244 struct CheckConstMatrix
247 typedef typename Matrix::size_type size_type;
249 typedef typename Matrix::value_type value_type;
250 typedef typename Matrix::field_type field_type;
251 typedef typename Matrix::block_type block_type;
253 typedef typename Matrix::const_row_reference const_row_reference;
255 typedef typename Matrix::ConstIterator ConstIterator;
257 static void apply (
const Matrix &matrix )
259 checkDataAccess ( matrix );
260 checkIterators ( matrix );
261 checkLinearAlgebra ( matrix );
262 checkNorms ( matrix );
263 checkSizes ( matrix );
264 CheckIfSquareMatrix< Matrix, Traits >::apply( matrix );
272 static void checkSizes (
const Matrix &matrix )
274 [[maybe_unused]]
const size_type size = matrix.size();
275 const size_type rows = MatrixSizeHelper< Matrix >::rows( matrix );
276 const size_type cols = MatrixSizeHelper< Matrix >::cols( matrix );
277 const size_type N = matrix.N();
278 const size_type M = matrix.M();
280 if( N != rows || M != cols )
285 static void checkDataAccess (
const Matrix &matrix )
287 const size_type size = matrix.size();
288 for( size_type i = size_type( 0 ); i < size; ++i )
289 [[maybe_unused]] const_row_reference row = matrix[ i ];
291 const size_type rows = MatrixSizeHelper< Matrix >::rows( matrix );
292 const size_type cols = MatrixSizeHelper< Matrix >::cols( matrix );
293 for( size_type i = size_type( 0 ); i < rows; ++i )
295 for( size_type j = size_type( 0 ); j < cols; ++j )
297 [[maybe_unused]]
bool exists = matrix.exists( i, j );
298 [[maybe_unused]]
const value_type &value = matrix[ i ][ j ];
304 static void checkNorms (
const Matrix &matrix )
306 typedef typename Dune::FieldTraits< value_type >::real_type real_type;
307 real_type frobenius_norm = matrix.frobenius_norm();
308 real_type frobenius_norm2 = matrix.frobenius_norm2();
309 real_type infinity_norm = matrix.infinity_norm() ;
310 real_type infinity_norm_real = matrix.infinity_norm_real();
313 std::min( infinity_norm, infinity_norm_real ) ) < real_type( 0 ) )
318 static void checkLinearAlgebra (
const Matrix &matrix )
320 typename Traits::domain_type domain = Traits::domain( matrix );
321 typename Traits::range_type range = Traits::range( matrix );
322 typename Traits::value_type alpha( 1 );
324 matrix.mv( domain, range );
325 matrix.mtv( range, domain );
326 matrix.umv( domain, range );
327 matrix.umtv( range, domain );
328 matrix.umhv( range, domain );
329 matrix.mmv( domain, range );
330 matrix.mmtv( range, domain );
331 matrix.mmhv( range, domain );
332 matrix.usmv( alpha, domain, range );
333 matrix.usmtv( alpha, range, domain );
334 matrix.usmhv( alpha, range, domain );
338 static void checkIterators (
const Matrix &matrix )
340 const ConstIterator end = matrix.end();
341 for( ConstIterator it = matrix.begin(); it != end; ++it )
342 [[maybe_unused]] const_row_reference row = *it;
351 template<
class Matrix,
class Traits >
352 struct CheckNonConstMatrix
355 typedef typename Matrix::size_type size_type;
356 typedef typename Matrix::value_type value_type;
357 typedef typename Matrix::row_reference row_reference;
358 typedef typename Matrix::row_type row_type;
359 typedef typename Matrix::Iterator Iterator;
361 static void apply ( Matrix &matrix )
363 checkIterators( matrix );
364 checkAssignment( matrix );
366 CheckIfSquareMatrix< Matrix, Traits >::apply( matrix );
380 static void checkAssignment ( Matrix &matrix )
382 matrix = value_type( 1 );
384 const size_type size = matrix.size();
385 for( size_type i = size_type( 0 ); i < size; ++i )
387 row_reference row = matrix[ i ];
388 row = row_type( value_type( 0 ) );
391 const size_type rows = MatrixSizeHelper< Matrix >::rows( matrix );
392 const size_type cols = MatrixSizeHelper< Matrix >::cols( matrix );
393 for( size_type i = size_type( 0 ); i < rows; ++i )
395 for( size_type j = size_type( 0 ); j < cols; ++j )
396 matrix[ i ][ j ] = ( i == j ? value_type( 1 ) : value_type( 0 ) );
401 static void checkIterators ( Matrix &matrix )
403 const Iterator end = matrix.end();
404 for( Iterator it = matrix.begin(); it != end; ++it )
406 row_reference row = *it;
407 row = row_type( value_type( 0 ) );
422 template<
class Matrix,
class Traits = CheckMatrixInterface::UseDynamicVector< Matrix > >
423 void checkMatrixInterface (
const Matrix &matrix )
425 CheckMatrixInterface::CheckConstMatrix< Matrix, Traits >::apply( matrix );
428 template<
class Matrix,
class Traits = CheckMatrixInterface::UseDynamicVector< Matrix > >
429 void checkMatrixInterface ( Matrix &matrix )
431 checkMatrixInterface(
const_cast< const Matrix &
>( matrix ) );
432 CheckMatrixInterface::CheckNonConstMatrix< Matrix, Traits >::apply( matrix );
Construct a vector with a dynamic size.
Definition: dynvector.hh:59
vector space out of a tensor product of fields.
Definition: fvector.hh:91
Default exception if a function was called while the object is not in a valid state for that function...
Definition: exceptions.hh:281
Default exception class for range errors.
Definition: exceptions.hh:254
A few common exception classes.
This file implements a dense vector with a dynamic size.
Type traits to determine the type of reals (when working with complex numbers)
Implements a vector constructed from a given type representing a field and a compile-time given size.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
constexpr auto min
Function object that returns the smaller of the given values.
Definition: hybridutilities.hh:506
Dune namespace.
Definition: alignedallocator.hh:13