Welcome to the NetCologne GmbH open source mirroring service!

This machine mirrors various open-source projects. 20 Gbit/s uplink.

If there are any issues or you want another project mirrored, please contact mirror-service -=AT=- netcologne DOT de !

GetFEM: src/gmm/gmm_opt.h Source File
GetFEM  5.4.2
gmm_opt.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2003-2020 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, write to the Free Software Foundation,
19  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 
21  As a special exception, you may use this file as it is a part of a free
22  software library without restriction. Specifically, if other files
23  instantiate templates or use macros or inline functions from this file,
24  or you compile this file and link it with other files to produce an
25  executable, this file does not by itself cause the resulting executable
26  to be covered by the GNU Lesser General Public License. This exception
27  does not however invalidate any other reasons why the executable file
28  might be covered by the GNU Lesser General Public License.
29 
30 ===========================================================================*/
31 
32 /**@file gmm_opt.h
33  @author Yves Renard <Yves.Renard@insa-lyon.fr>
34  @date July 9, 2003.
35  @brief Optimization for some small cases (inversion of 2x2 matrices etc.)
36 */
37 #ifndef GMM_OPT_H__
38 #define GMM_OPT_H__
39 
40 #include <gmm/gmm_dense_lu.h>
41 
42 namespace gmm {
43 
44  /* ********************************************************************* */
45  /* Optimized determinant and inverse for small matrices (2x2 and 3x3) */
46  /* with dense_matrix<T>. */
47  /* ********************************************************************* */
48 
49  template <typename T> T lu_det(const dense_matrix<T> &A) {
50  size_type n(mat_nrows(A));
51  if (n) {
52  const T *p = &(A(0,0));
53  switch (n) {
54  case 1 : return (*p);
55  case 2 : return (*p) * (*(p+3)) - (*(p+1)) * (*(p+2));
56 // Not stable for nearly singular matrices
57 // case 3 : return (*p) * ((*(p+4)) * (*(p+8)) - (*(p+5)) * (*(p+7)))
58 // - (*(p+1)) * ((*(p+3)) * (*(p+8)) - (*(p+5)) * (*(p+6)))
59 // + (*(p+2)) * ((*(p+3)) * (*(p+7)) - (*(p+4)) * (*(p+6)));
60  default :
61  {
62  dense_matrix<T> B(mat_nrows(A), mat_ncols(A));
63  lapack_ipvt ipvt(mat_nrows(A));
64  gmm::copy(A, B);
65  lu_factor(B, ipvt);
66  return lu_det(B, ipvt);
67  }
68  }
69  }
70  return T(1);
71  }
72 
73 
74  template <typename T> T lu_inverse(const dense_matrix<T> &A_,
75  bool doassert = true) {
76  dense_matrix<T>& A = const_cast<dense_matrix<T> &>(A_);
77  size_type N = mat_nrows(A);
78  T det(1);
79  if (N) {
80  T *p = &(A(0,0));
81  switch (N) {
82  case 1 : {
83  det = *p;
84  if (doassert) GMM_ASSERT1(det!=T(0), "non invertible matrix");
85  if (det == T(0)) break;
86  *p = T(1) / det;
87  } break;
88  case 2 : {
89  det = (*p) * (*(p+3)) - (*(p+1)) * (*(p+2));
90  if (doassert) GMM_ASSERT1(det!=T(0), "non invertible matrix");
91  if (det == T(0)) break;
92  std::swap(*p, *(p+3));
93  *p++ /= det; *p++ /= -det; *p++ /= -det; *p++ /= det;
94  } break;
95  default : {
96  if (N == 3) { // not stable for nearly singular matrices
97  T a, b, c, d, e, f, g, h, i;
98  a = (*(p+4)) * (*(p+8)) - (*(p+5)) * (*(p+7));
99  b = - (*(p+1)) * (*(p+8)) + (*(p+2)) * (*(p+7));
100  c = (*(p+1)) * (*(p+5)) - (*(p+2)) * (*(p+4));
101  d = - (*(p+3)) * (*(p+8)) + (*(p+5)) * (*(p+6));
102  e = (*(p+0)) * (*(p+8)) - (*(p+2)) * (*(p+6));
103  f = - (*(p+0)) * (*(p+5)) + (*(p+2)) * (*(p+3));
104  g = (*(p+3)) * (*(p+7)) - (*(p+4)) * (*(p+6));
105  h = - (*(p+0)) * (*(p+7)) + (*(p+1)) * (*(p+6));
106  i = (*(p+0)) * (*(p+4)) - (*(p+1)) * (*(p+3));
107  det = (*p) * a + (*(p+1)) * d + (*(p+2)) * g;
108  if (std::abs(det) > 1e-5) {
109  *p++ = a / det; *p++ = b / det; *p++ = c / det;
110  *p++ = d / det; *p++ = e / det; *p++ = f / det;
111  *p++ = g / det; *p++ = h / det; *p++ = i / det;
112  break;
113  }
114  }
115  dense_matrix<T> B(mat_nrows(A), mat_ncols(A));
116  lapack_ipvt ipvt(mat_nrows(A));
117  gmm::copy(A, B);
118  size_type info = lu_factor(B, ipvt);
119  GMM_ASSERT1(!info, "non invertible matrix");
120  lu_inverse(B, ipvt, A);
121  det = lu_det(B, ipvt);
122  break;
123  }
124  }
125  }
126  return det;
127  }
128 
129 
130 }
131 
132 #endif // GMM_OPT_H__
bgeot::size_type
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:49
gmm_dense_lu.h
LU factorizations and determinant computation for dense matrices.