You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
4.8 KiB

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