GetFEM  5.5
gmm_opt.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2003-2026 Yves Renard
5 
6  This file is a part of GetFEM
7 
8  GetFEM is free software; you can redistribute it and/or modify it
9  under the terms of the GNU Lesser General Public License as published
10  by the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version along with the GCC Runtime Library
12  Exception either version 3.1 or (at your option) any later version.
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  License and GCC Runtime Library Exception for more details.
17  You should have received a copy of the GNU Lesser General Public License
18  along with this program. If not, see https://www.gnu.org/licenses/.
19 
20  As a special exception, you may use this file as it is a part of a free
21  software library without restriction. Specifically, if other files
22  instantiate templates or use macros or inline functions from this file,
23  or you compile this file and link it with other files to produce an
24  executable, this file does not by itself cause the resulting executable
25  to be covered by the GNU Lesser General Public License. This exception
26  does not however invalidate any other reasons why the executable file
27  might be covered by the GNU Lesser General Public License.
28 
29 ===========================================================================*/
30 
31 /**@file gmm_opt.h
32  @author Yves Renard <Yves.Renard@insa-lyon.fr>
33  @date July 9, 2003.
34  @brief Optimization for some small cases (inversion of 2x2 matrices etc.)
35 */
36 #ifndef GMM_OPT_H__
37 #define GMM_OPT_H__
38 
39 #include "gmm_dense_lu.h"
40 
41 namespace gmm {
42 
43  /* ********************************************************************* */
44  /* Optimized determinant and inverse for small matrices (2x2 and 3x3) */
45  /* with dense_matrix<T>. */
46  /* ********************************************************************* */
47 
48  template <typename T> T lu_det(const dense_matrix<T> &A) {
49  size_type n(mat_nrows(A));
50  if (n) {
51  const T *p = &(A(0,0));
52  switch (n) {
53  case 1 : return (*p);
54  case 2 : return (*p) * (*(p+3)) - (*(p+1)) * (*(p+2));
55 // Not stable for nearly singular matrices
56 // case 3 : return (*p) * ((*(p+4)) * (*(p+8)) - (*(p+5)) * (*(p+7)))
57 // - (*(p+1)) * ((*(p+3)) * (*(p+8)) - (*(p+5)) * (*(p+6)))
58 // + (*(p+2)) * ((*(p+3)) * (*(p+7)) - (*(p+4)) * (*(p+6)));
59  default :
60  {
61  dense_matrix<T> B(mat_nrows(A), mat_ncols(A));
62  lapack_ipvt ipvt(mat_nrows(A));
63  gmm::copy(A, B);
64  lu_factor(B, ipvt);
65  return lu_det(B, ipvt);
66  }
67  }
68  }
69  return T(1);
70  }
71 
72 
73  template <typename T> T lu_inverse(const dense_matrix<T> &A_,
74  bool doassert = true) {
75  dense_matrix<T>& A = const_cast<dense_matrix<T> &>(A_);
76  size_type N = mat_nrows(A);
77  T det(1);
78  if (N) {
79  T *p = &(A(0,0));
80  switch (N) {
81  case 1 : {
82  det = *p;
83  if (doassert) GMM_ASSERT1(det!=T(0), "non invertible matrix");
84  if (det == T(0)) break;
85  *p = T(1) / det;
86  } break;
87  case 2 : {
88  det = (*p) * (*(p+3)) - (*(p+1)) * (*(p+2));
89  if (doassert) GMM_ASSERT1(det!=T(0), "non invertible matrix");
90  if (det == T(0)) break;
91  std::swap(*p, *(p+3));
92  *p++ /= det; *p++ /= -det; *p++ /= -det; *p++ /= det;
93  } break;
94  default : {
95  if (N == 3) { // not stable for nearly singular matrices
96  T a, b, c, d, e, f, g, h, i;
97  a = (*(p+4)) * (*(p+8)) - (*(p+5)) * (*(p+7));
98  b = - (*(p+1)) * (*(p+8)) + (*(p+2)) * (*(p+7));
99  c = (*(p+1)) * (*(p+5)) - (*(p+2)) * (*(p+4));
100  d = - (*(p+3)) * (*(p+8)) + (*(p+5)) * (*(p+6));
101  e = (*(p+0)) * (*(p+8)) - (*(p+2)) * (*(p+6));
102  f = - (*(p+0)) * (*(p+5)) + (*(p+2)) * (*(p+3));
103  g = (*(p+3)) * (*(p+7)) - (*(p+4)) * (*(p+6));
104  h = - (*(p+0)) * (*(p+7)) + (*(p+1)) * (*(p+6));
105  i = (*(p+0)) * (*(p+4)) - (*(p+1)) * (*(p+3));
106  det = (*p) * a + (*(p+1)) * d + (*(p+2)) * g;
107  if (std::abs(det) > 1e-5) {
108  *p++ = a / det; *p++ = b / det; *p++ = c / det;
109  *p++ = d / det; *p++ = e / det; *p++ = f / det;
110  *p++ = g / det; *p++ = h / det; *p++ = i / det;
111  break;
112  }
113  }
114  dense_matrix<T> B(mat_nrows(A), mat_ncols(A));
115  lapack_ipvt ipvt(mat_nrows(A));
116  gmm::copy(A, B);
117  size_type info = lu_factor(B, ipvt);
118  GMM_ASSERT1(!info, "non invertible matrix");
119  lu_inverse(B, ipvt, A);
120  det = lu_det(B, ipvt);
121  break;
122  }
123  }
124  }
125  return det;
126  }
127 
128 
129 }
130 
131 #endif // GMM_OPT_H__
void copy(const L1 &l1, L2 &l2)
*‍/
Definition: gmm_blas.h:976
LU factorizations and determinant computation for dense matrices.
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:48