5#ifndef DUNE_GEOMETRY_UTILITY_DEFAULTMATRIXHELPER_HH 
    6#define DUNE_GEOMETRY_UTILITY_DEFAULTMATRIXHELPER_HH 
   20struct FieldMatrixHelper
 
   25  template< 
int m, 
int n >
 
   26  [[ deprecated(
"Use A.mv(x,y) instead.") ]]
 
   27  static void Ax ( 
const FieldMatrix< ctype, m, n > &A, 
const FieldVector< ctype, n > &x, FieldVector< ctype, m > &ret )
 
   33  template< 
int m, 
int n >
 
   34  [[ deprecated(
"Use A.mtv(x,y) instead.") ]]
 
   35  static void ATx ( 
const FieldMatrix< ctype, m, n > &A, 
const FieldVector< ctype, m > &x, FieldVector< ctype, n > &ret )
 
   41  template< 
int m, 
int n, 
int p >
 
   42  [[ deprecated(
"Use FMatrixHelp::multMatrix(A,B,ret)") ]]
 
   43  static void AB ( 
const FieldMatrix< ctype, m, n > &A, 
const FieldMatrix< ctype, n, p > &B, FieldMatrix< ctype, m, p > &ret )
 
   45    FMatrixHelp::multMatrix(A,B,ret);
 
   49  template< 
int m, 
int n, 
int p >
 
   50  static void ATBT ( 
const FieldMatrix< ctype, m, n > &A, 
const FieldMatrix< ctype, p, m > &B, FieldMatrix< ctype, n, p > &ret )
 
   52    for( 
int i = 0; i < n; ++i )
 
   54      for( 
int j = 0; j < p; ++j )
 
   56        ret[ i ][ j ] = ctype( 0 );
 
   57        for( 
int k = 0; k < m; ++k )
 
   58          ret[ i ][ j ] += A[ k ][ i ] * B[ j ][ k ];
 
   64  template< 
int m, 
int n >
 
   65  static void ATA_L ( 
const FieldMatrix< ctype, m, n > &A, FieldMatrix< ctype, n, n > &ret )
 
   67    for( 
int i = 0; i < n; ++i )
 
   69      for( 
int j = 0; j <= i; ++j )
 
   71        ret[ i ][ j ] = ctype( 0 );
 
   72        for( 
int k = 0; k < m; ++k )
 
   73          ret[ i ][ j ] += A[ k ][ i ] * A[ k ][ j ];
 
   79  template< 
int m, 
int n >
 
   80  [[ deprecated(
"Use FMatrixHelp::multTransposedMatrix(A,ret)") ]]
 
   81  static void ATA ( 
const FieldMatrix< ctype, m, n > &A, FieldMatrix< ctype, n, n > &ret )
 
   83    return FMatrixHelp::multTransposedMatrix(A,ret);
 
   87  template< 
int m, 
int n >
 
   88  static void AAT_L ( 
const FieldMatrix< ctype, m, n > &A, FieldMatrix< ctype, m, m > &ret )
 
   90    for( 
int i = 0; i < m; ++i )
 
   92      for( 
int j = 0; j <= i; ++j )
 
   94        ctype &retij = ret[ i ][ j ];
 
   95        retij = A[ i ][ 0 ] * A[ j ][ 0 ];
 
   96        for( 
int k = 1; k < n; ++k )
 
   97          retij += A[ i ][ k ] * A[ j ][ k ];
 
  103  template< 
int m, 
int n >
 
  104  static void AAT ( 
const FieldMatrix< ctype, m, n > &A, FieldMatrix< ctype, m, m > &ret )
 
  106    for( 
int i = 0; i < m; ++i )
 
  108      for( 
int j = 0; j < i; ++j )
 
  110        ret[ i ][ j ] = ctype( 0 );
 
  111        for( 
int k = 0; k < n; ++k )
 
  112          ret[ i ][ j ] += A[ i ][ k ] * A[ j ][ k ];
 
  113        ret[ j ][ i ] = ret[ i ][ j ];
 
  115      ret[ i ][ i ] = ctype( 0 );
 
  116      for( 
int k = 0; k < n; ++k )
 
  117        ret[ i ][ i ] += A[ i ][ k ] * A[ i ][ k ];
 
  124  static void Lx ( 
const FieldMatrix< ctype, n, n > &L, 
const FieldVector< ctype, n > &x, FieldVector< ctype, n > &ret )
 
  126    for( 
int i = 0; i < n; ++i )
 
  128      ret[ i ] = ctype( 0 );
 
  129      for( 
int j = 0; j <= i; ++j )
 
  130        ret[ i ] += L[ i ][ j ] * x[ j ];
 
  137  static void LTx ( 
const FieldMatrix< ctype, n, n > &L, 
const FieldVector< ctype, n > &x, FieldVector< ctype, n > &ret )
 
  139    for( 
int i = 0; i < n; ++i )
 
  141      ret[ i ] = ctype( 0 );
 
  142      for( 
int j = i; j < n; ++j )
 
  143        ret[ i ] += L[ j ][ i ] * x[ j ];
 
  150  static void LTL ( 
const FieldMatrix< ctype, n, n > &L, FieldMatrix< ctype, n, n > &ret )
 
  152    for( 
int i = 0; i < n; ++i )
 
  154      for( 
int j = 0; j < i; ++j )
 
  156        ret[ i ][ j ] = ctype( 0 );
 
  157        for( 
int k = i; k < n; ++k )
 
  158          ret[ i ][ j ] += L[ k ][ i ] * L[ k ][ j ];
 
  159        ret[ j ][ i ] = ret[ i ][ j ];
 
  161      ret[ i ][ i ] = ctype( 0 );
 
  162      for( 
int k = i; k < n; ++k )
 
  163        ret[ i ][ i ] += L[ k ][ i ] * L[ k ][ i ];
 
  170  static void LLT ( 
const FieldMatrix< ctype, n, n > &L, FieldMatrix< ctype, n, n > &ret )
 
  172    for( 
int i = 0; i < n; ++i )
 
  174      for( 
int j = 0; j < i; ++j )
 
  176        ret[ i ][ j ] = ctype( 0 );
 
  177        for( 
int k = 0; k <= j; ++k )
 
  178          ret[ i ][ j ] += L[ i ][ k ] * L[ j ][ k ];
 
  179        ret[ j ][ i ] = ret[ i ][ j ];
 
  181      ret[ i ][ i ] = ctype( 0 );
 
  182      for( 
int k = 0; k <= i; ++k )
 
  183        ret[ i ][ i ] += L[ i ][ k ] * L[ i ][ k ];
 
  191  static bool cholesky_L ( 
const FieldMatrix< ctype, n, n > &A, FieldMatrix< ctype, n, n > &ret, 
const bool checkSingular = 
false )
 
  194    for( 
int i = 0; i < n; ++i )
 
  196      ctype &rii = ret[ i ][ i ];
 
  198      ctype xDiag = A[ i ][ i ];
 
  199      for( 
int j = 0; j < i; ++j )
 
  200        xDiag -= ret[ i ][ j ] * ret[ i ][ j ];
 
  204      if( checkSingular && ! ( xDiag > ctype( 0 )) )
 
  208      assert( xDiag > ctype( 0 ) );
 
  211      ctype invrii = ctype( 1 ) / rii;
 
  212      for( 
int k = i+1; k < n; ++k )
 
  214        ctype x = A[ k ][ i ];
 
  215        for( 
int j = 0; j < i; ++j )
 
  216          x -= ret[ i ][ j ] * ret[ k ][ j ];
 
  217        ret[ k ][ i ] = invrii * x;
 
  228  static ctype detL ( 
const FieldMatrix< ctype, n, n > &L )
 
  231    for( 
int i = 0; i < n; ++i )
 
  239  static ctype invL ( FieldMatrix< ctype, n, n > &L )
 
  242    for( 
int i = 0; i < n; ++i )
 
  244      ctype &lii = L[ i ][ i ];
 
  246      lii = ctype( 1 ) / lii;
 
  247      for( 
int j = 0; j < i; ++j )
 
  249        ctype &lij = L[ i ][ j ];
 
  250        ctype x = lij * L[ j ][ j ];
 
  251        for( 
int k = j+1; k < i; ++k )
 
  252          x += L[ i ][ k ] * L[ k ][ j ];
 
  262  static void invLx ( FieldMatrix< ctype, n, n > &L, FieldVector< ctype, n > &x )
 
  264    for( 
int i = 0; i < n; ++i )
 
  266      for( 
int j = 0; j < i; ++j )
 
  267        x[ i ] -= L[ i ][ j ] * x[ j ];
 
  268      x[ i ] /= L[ i ][ i ];
 
  275  static void invLTx ( FieldMatrix< ctype, n, n > &L, FieldVector< ctype, n > &x )
 
  277    for( 
int i = n; i > 0; --i )
 
  279      for( 
int j = i; j < n; ++j )
 
  280        x[ i-1 ] -= L[ j ][ i-1 ] * x[ j ];
 
  281      x[ i-1 ] /= L[ i-1 ][ i-1 ];
 
  288  static ctype spdDetA ( 
const FieldMatrix< ctype, n, n > &A )
 
  290    FieldMatrix< ctype, n, n > L;
 
  298  static ctype spdInvA ( FieldMatrix< ctype, n, n > &A )
 
  300    FieldMatrix< ctype, n, n > L;
 
  302    const ctype det = invL( L );
 
  310  static bool spdInvAx ( FieldMatrix< ctype, n, n > &A, FieldVector< ctype, n > &x, 
const bool checkSingular = 
false )
 
  312    FieldMatrix< ctype, n, n > L;
 
  313    const bool invertible = cholesky_L( A, L, checkSingular );
 
  314    if( ! invertible ) 
return invertible ;
 
  321  template< 
int m, 
int n >
 
  322  static ctype detATA ( 
const FieldMatrix< ctype, m, n > &A )
 
  324    if constexpr( m >= n )
 
  326      FieldMatrix< ctype, n, n > ata;
 
  328      return spdDetA( ata );
 
  339  template< 
int m, 
int n >
 
  340  static ctype sqrtDetAAT ( 
const FieldMatrix< ctype, m, n > &A )
 
  347    if constexpr( (n == 2) && (m == 2) )
 
  350      return abs( A[ 0 ][ 0 ]*A[ 1 ][ 1 ] - A[ 1 ][ 0 ]*A[ 0 ][ 1 ] );
 
  352    else if constexpr( (n == 3) && (m == 3) )
 
  355      const ctype v0 = A[ 0 ][ 1 ] * A[ 1 ][ 2 ] - A[ 1 ][ 1 ] * A[ 0 ][ 2 ];
 
  356      const ctype v1 = A[ 0 ][ 2 ] * A[ 1 ][ 0 ] - A[ 1 ][ 2 ] * A[ 0 ][ 0 ];
 
  357      const ctype v2 = A[ 0 ][ 0 ] * A[ 1 ][ 1 ] - A[ 1 ][ 0 ] * A[ 0 ][ 1 ];
 
  358      return abs( v0 * A[ 2 ][ 0 ] + v1 * A[ 2 ][ 1 ] + v2 * A[ 2 ][ 2 ] );
 
  360    else if constexpr( (n == 3) && (m == 2) )
 
  363      const ctype v0 = A[ 0 ][ 0 ] * A[ 1 ][ 1 ] - A[ 0 ][ 1 ] * A[ 1 ][ 0 ];
 
  364      const ctype v1 = A[ 0 ][ 0 ] * A[ 1 ][ 2 ] - A[ 1 ][ 0 ] * A[ 0 ][ 2 ];
 
  365      const ctype v2 = A[ 0 ][ 1 ] * A[ 1 ][ 2 ] - A[ 0 ][ 2 ] * A[ 1 ][ 1 ];
 
  366      return sqrt( v0*v0 + v1*v1 + v2*v2);
 
  368    else if constexpr( n >= m )
 
  371      FieldMatrix< ctype, m, m > aat;
 
  373      return spdDetA( aat );
 
  381  template< 
int m, 
int n >
 
  382  static ctype leftInvA ( 
const FieldMatrix< ctype, m, n > &A, FieldMatrix< ctype, n, m > &ret )
 
  385    if constexpr( (n == 2) && (m == 2) )
 
  387      const ctype det = (A[ 0 ][ 0 ]*A[ 1 ][ 1 ] - A[ 1 ][ 0 ]*A[ 0 ][ 1 ]);
 
  388      const ctype detInv = ctype( 1 ) / det;
 
  389      ret[ 0 ][ 0 ] = A[ 1 ][ 1 ] * detInv;
 
  390      ret[ 1 ][ 1 ] = A[ 0 ][ 0 ] * detInv;
 
  391      ret[ 1 ][ 0 ] = -A[ 1 ][ 0 ] * detInv;
 
  392      ret[ 0 ][ 1 ] = -A[ 0 ][ 1 ] * detInv;
 
  397      FieldMatrix< ctype, n, n > ata;
 
  399      const ctype det = spdInvA( ata );
 
  406  template< 
int m, 
int n >
 
  407  static bool leftInvAx ( 
const FieldMatrix< ctype, m, n > &A, 
const FieldVector< ctype, m > &x, FieldVector< ctype, n > &y )
 
  409    static_assert((m >= n), 
"Matrix has no left inverse.");
 
  410    FieldMatrix< ctype, n, n > ata;
 
  413    return spdInvAx( ata, y, 
true );
 
  417  template< 
int m, 
int n >
 
  418  static ctype rightInvA ( 
const FieldMatrix< ctype, m, n > &A, FieldMatrix< ctype, n, m > &ret )
 
  420    static_assert((n >= m), 
"Matrix has no right inverse.");
 
  422    if constexpr( (n == 2) && (m == 2) )
 
  424      const ctype det = (A[ 0 ][ 0 ]*A[ 1 ][ 1 ] - A[ 1 ][ 0 ]*A[ 0 ][ 1 ]);
 
  425      const ctype detInv = ctype( 1 ) / det;
 
  426      ret[ 0 ][ 0 ] = A[ 1 ][ 1 ] * detInv;
 
  427      ret[ 1 ][ 1 ] = A[ 0 ][ 0 ] * detInv;
 
  428      ret[ 1 ][ 0 ] = -A[ 1 ][ 0 ] * detInv;
 
  429      ret[ 0 ][ 1 ] = -A[ 0 ][ 1 ] * detInv;
 
  434      FieldMatrix< ctype, m , m > aat;
 
  436      const ctype det = spdInvA( aat );
 
  437      ATBT( A , aat , ret );
 
  443  template< 
int m, 
int n >
 
  444  static bool xTRightInvA ( 
const FieldMatrix< ctype, m, n > &A, 
const FieldVector< ctype, n > &x, FieldVector< ctype, m > &y )
 
  446    static_assert((n >= m), 
"Matrix has no right inverse.");
 
  447    FieldMatrix< ctype, m, m > aat;
 
  451    return spdInvAx( aat, y, 
true );
 
Implements a matrix constructed from a given type representing a field and compile-time given number ...
 
Implements a vector constructed from a given type representing a field and a compile-time given size.
 
Dune namespace.
Definition: alignedallocator.hh:13