/usr/include/dune/common/test/checkmatrixinterface.hh is in libdune-common-dev 2.3.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 | // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_COMMON_CHECK_MATRIX_INTERFACE_HH
#define DUNE_COMMON_CHECK_MATRIX_INTERFACE_HH
#include <algorithm>
#include <limits>
#include <dune/common/dynvector.hh>
#include <dune/common/exceptions.hh>
#include <dune/common/ftraits.hh>
#include <dune/common/fvector.hh>
/*
* @file
* @brief This file provides an interface check for dense matrices.
* @author Christoph Gersbacher
*/
namespace Dune
{
// External forward declarations for namespace Dune
// ------------------------------------------------
template< class, int, int > class FieldMatrix;
template< class, int > class DiagonalMatrix;
} // namespace Dune
namespace CheckMatrixInterface
{
namespace Capabilities
{
// hasStaticSizes
// --------------
template< class Matrix >
struct hasStaticSizes
{
static const bool v = false;
static const int rows = ~0;
static const int cols = ~0;
};
template< class Matrix >
struct hasStaticSizes< const Matrix >
{
static const bool v = hasStaticSizes< Matrix >::v;
static const int rows = hasStaticSizes< Matrix >::rows;
static const int cols = hasStaticSizes< Matrix >::cols;
};
// isSquare
// --------
template< class Matrix >
struct isSquare
{
static const bool v = false;
};
template< class Matrix >
struct isSquare< const Matrix >
{
static const bool v = isSquare< Matrix >::v;
};
// Template specializations for Dune::FieldMatrix
// ----------------------------------------------
template< class K, int r, int c >
struct hasStaticSizes< Dune::FieldMatrix< K, r, c > >
{
static const bool v = true;
static const int rows = r;
static const int cols = c;
};
template< class K, int rows, int cols >
struct isSquare< Dune::FieldMatrix< K, rows, cols > >
{
static const bool v = ( rows == cols );
};
// Template specializations for Dune::DiagonalMatrix
// -------------------------------------------------
template< class K, int n >
struct hasStaticSizes< Dune::DiagonalMatrix<K,n> >
{
static const bool v = true;
static const int rows = n;
static const int cols = n;
};
template< class K, int n >
struct isSquare< Dune::DiagonalMatrix<K,n> >
{
static const bool v = true;
};
} // namespace Capabilities
// UseDynamicVector
// ----------------
template< class Matrix >
struct UseDynamicVector
{
typedef typename Matrix::value_type value_type;
typedef Dune::DynamicVector< value_type > domain_type;
typedef domain_type range_type;
static domain_type domain ( const Matrix &matrix, value_type v = value_type() )
{
return domain_type( matrix.M(), v );
}
static range_type range ( const Matrix &matrix, value_type v = value_type() )
{
return range_type( matrix.N(), v );
}
};
// UseFieldVector
// --------------
template< class K, int rows, int cols >
struct UseFieldVector
{
typedef K value_type;
typedef Dune::FieldVector< K, cols > domain_type;
typedef Dune::FieldVector< K, rows > range_type;
template< class Matrix >
static domain_type domain ( const Matrix &, value_type v = value_type() )
{
return domain_type( v );
}
template< class Matrix >
static range_type range ( const Matrix &, value_type v = value_type() )
{
return range_type( v );
}
};
// MatrixSizeHelper
// ----------------
template< class Matrix, bool hasStaticSizes = Capabilities::hasStaticSizes< Matrix >::v >
struct MatrixSizeHelper;
template< class Matrix >
struct MatrixSizeHelper< Matrix, false >
{
typedef typename Matrix::size_type size_type;
static const size_type rows ( const Matrix &matrix ) { return matrix.rows(); }
static const size_type cols ( const Matrix &matrix ) { return matrix.cols(); }
};
template< class Matrix >
struct MatrixSizeHelper< Matrix, true >
{
typedef typename Matrix::size_type size_type;
static const size_type rows ( const Matrix & ) { return Matrix::rows; }
static const size_type cols ( const Matrix & ) { return Matrix::cols; }
};
// CheckIfSquareMatrix
// -------------------
template< class Matrix, class Traits, bool isSquare = Capabilities::isSquare< Matrix >::v >
struct CheckIfSquareMatrix;
template< class Matrix, class Traits >
struct CheckIfSquareMatrix< Matrix, Traits, false >
{
static void apply ( const Matrix &) {}
static void apply ( Matrix &) {}
};
template< class Matrix, class Traits >
struct CheckIfSquareMatrix< Matrix, Traits, true >
{
typedef typename Matrix::value_type value_type;
static value_type tolerance ()
{
return value_type( 16 ) * std::numeric_limits< value_type >::epsilon();
}
static void apply ( const Matrix &matrix )
{
const value_type determinant = matrix.determinant();
if( determinant > tolerance() )
{
typename Traits::domain_type x = Traits::domain( matrix );
const typename Traits::range_type b = Traits::range( matrix );
matrix.solve( x, b );
}
}
static void apply ( Matrix &matrix )
{
apply( const_cast< const Matrix & >( matrix ) );
if( matrix.determinant() > tolerance() )
matrix.invert();
}
};
// CheckConstMatrix
// ----------------
template< class Matrix, class Traits >
struct CheckConstMatrix
{
// required type definitions
typedef typename Matrix::size_type size_type;
typedef typename Matrix::value_type value_type;
typedef typename Matrix::field_type field_type;
typedef typename Matrix::block_type block_type;
typedef typename Matrix::const_row_reference const_row_reference;
typedef typename Matrix::ConstIterator ConstIterator;
static void apply ( const Matrix &matrix )
{
checkDataAccess ( matrix );
checkIterators ( matrix );
checkLinearAlgebra ( matrix );
checkNorms ( matrix );
checkSizes ( matrix );
CheckIfSquareMatrix< Matrix, Traits >::apply( matrix );
// TODO: check comparison
// bool operator == ( const Matrix &other );
// bool operator != ( const Matrix &other );
}
// check size methods
static void checkSizes ( const Matrix &matrix )
{
DUNE_UNUSED const size_type size = matrix.size();
const size_type rows = MatrixSizeHelper< Matrix >::rows( matrix );
const size_type cols = MatrixSizeHelper< Matrix >::cols( matrix );
const size_type N = matrix.N();
const size_type M = matrix.M();
if( N != rows || M != cols )
DUNE_THROW( Dune::RangeError, "Returned inconsistent sizes." );
}
// check read-only access to data
static void checkDataAccess ( const Matrix &matrix )
{
const size_type size = matrix.size();
for( size_type i = size_type( 0 ); i < size; ++i )
DUNE_UNUSED const_row_reference row = matrix[ i ];
const size_type rows = MatrixSizeHelper< Matrix >::rows( matrix );
const size_type cols = MatrixSizeHelper< Matrix >::cols( matrix );
for( size_type i = size_type( 0 ); i < rows; ++i )
{
for( size_type j = size_type( 0 ); j < cols; ++j )
{
DUNE_UNUSED bool exists = matrix.exists( i, j );
DUNE_UNUSED const value_type &value = matrix[ i ][ j ];
}
}
}
// check norms
static void checkNorms ( const Matrix &matrix )
{
typedef typename Dune::FieldTraits< value_type >::real_type real_type;
real_type frobenius_norm = matrix.frobenius_norm();
real_type frobenius_norm2 = matrix.frobenius_norm2();
real_type infinity_norm = matrix.infinity_norm() ;
real_type infinity_norm_real = matrix.infinity_norm_real();
if( std::min( std::min( frobenius_norm, frobenius_norm2 ),
std::min( infinity_norm, infinity_norm_real ) ) < real_type( 0 ) )
DUNE_THROW( Dune::InvalidStateException, "Norms must return non-negative value." );
}
// check basic linear algebra methods
static void checkLinearAlgebra ( const Matrix &matrix )
{
typename Traits::domain_type domain = Traits::domain( matrix );
typename Traits::range_type range = Traits::range( matrix );
typename Traits::value_type alpha( 1 );
matrix.mv( domain, range );
matrix.mtv( range, domain );
matrix.umv( domain, range );
matrix.umtv( range, domain );
matrix.umhv( range, domain );
matrix.mmv( domain, range );
matrix.mmtv( range, domain );
matrix.mmhv( range, domain );
matrix.usmv( alpha, domain, range );
matrix.usmtv( alpha, range, domain );
matrix.usmhv( alpha, range, domain );
}
// check iterator methods
static void checkIterators ( const Matrix &matrix )
{
const ConstIterator end = matrix.end();
for( ConstIterator it = matrix.begin(); it != end; ++it )
DUNE_UNUSED const_row_reference row = *it;
}
};
// CheckNonConstMatrix
// -------------------
template< class Matrix, class Traits >
struct CheckNonConstMatrix
{
// required type definitions
typedef typename Matrix::size_type size_type;
typedef typename Matrix::value_type value_type;
typedef typename Matrix::row_reference row_reference;
typedef typename Matrix::row_type row_type;
typedef typename Matrix::Iterator Iterator;
static void apply ( Matrix &matrix )
{
checkIterators( matrix );
checkAssignment( matrix );
CheckIfSquareMatrix< Matrix, Traits >::apply( matrix );
// TODO: check scalar/matrix and matrix/matrix operations
// Matrix &operator+= ( const Matrix &other );
// Matrix &operator-= ( const Matrix &other );
// Matrix &operator*= ( const value_type &v );
// Matrix &operator/= ( const value_type &v );
// Matrix &axpy += ( const value_type &v, const Matrix &other );
// Matrix &axpy += ( const value_type &v, const Matrix &other );
// Matrix &leftmultiply ( const Matrix &other );
// Matrix &rightmultiply ( const Matrix &other );
}
// check assignment
static void checkAssignment ( Matrix &matrix )
{
matrix = value_type( 1 );
const size_type size = matrix.size();
for( size_type i = size_type( 0 ); i < size; ++i )
{
row_reference row = matrix[ i ];
row = row_type( value_type( 0 ) );
}
const size_type rows = MatrixSizeHelper< Matrix >::rows( matrix );
const size_type cols = MatrixSizeHelper< Matrix >::cols( matrix );
for( size_type i = size_type( 0 ); i < rows; ++i )
{
for( size_type j = size_type( 0 ); j < cols; ++j )
matrix[ i ][ j ] = ( i == j ? value_type( 1 ) : value_type( 0 ) );
}
}
// check iterator methods
static void checkIterators ( Matrix &matrix )
{
const Iterator end = matrix.end();
for( Iterator it = matrix.begin(); it != end; ++it )
{
row_reference row = *it;
row = row_type( value_type( 0 ) );
}
}
};
} // namespace CheckMatrixInterface
namespace Dune
{
// checkMatrixInterface
// --------------------
template< class Matrix, class Traits = CheckMatrixInterface::UseDynamicVector< Matrix > >
void checkMatrixInterface ( const Matrix &matrix )
{
CheckMatrixInterface::CheckConstMatrix< Matrix, Traits >::apply( matrix );
}
template< class Matrix, class Traits = CheckMatrixInterface::UseDynamicVector< Matrix > >
void checkMatrixInterface ( Matrix &matrix )
{
checkMatrixInterface( const_cast< const Matrix & >( matrix ) );
CheckMatrixInterface::CheckNonConstMatrix< Matrix, Traits >::apply( matrix );
}
} // namespace Dune
#endif // #ifndef DUNE_COMMON_CHECK_MATRIX_INTERFACE_HH
|