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.

250 lines
11 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2003-2017 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. // This file is a modified version of lu.h from MTL.
  27. // See http://osl.iu.edu/research/mtl/
  28. // Following the corresponding Copyright notice.
  29. //===========================================================================
  30. //
  31. // Copyright (c) 1998-2001, University of Notre Dame. All rights reserved.
  32. // Redistribution and use in source and binary forms, with or without
  33. // modification, are permitted provided that the following conditions are met:
  34. //
  35. // * Redistributions of source code must retain the above copyright
  36. // notice, this list of conditions and the following disclaimer.
  37. // * Redistributions in binary form must reproduce the above copyright
  38. // notice, this list of conditions and the following disclaimer in the
  39. // documentation and/or other materials provided with the distribution.
  40. // * Neither the name of the University of Notre Dame nor the
  41. // names of its contributors may be used to endorse or promote products
  42. // derived from this software without specific prior written permission.
  43. //
  44. // THIS SOFTWARE IS PROVIDED BY THE TRUSTEES OF INDIANA UNIVERSITY AND
  45. // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  46. // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  47. // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TRUSTEES
  48. // OF INDIANA UNIVERSITY AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  49. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  50. // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  51. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  52. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  53. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  54. // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  55. //
  56. //===========================================================================
  57. /**@file gmm_dense_lu.h
  58. @author Andrew Lumsdaine, Jeremy G. Siek, Lie-Quan Lee, Y. Renard
  59. @date June 5, 2003.
  60. @brief LU factorizations and determinant computation for dense matrices.
  61. */
  62. #ifndef GMM_DENSE_LU_H
  63. #define GMM_DENSE_LU_H
  64. #include "gmm_dense_Householder.h"
  65. #include "gmm_opt.h"
  66. namespace gmm {
  67. /** LU Factorization of a general (dense) matrix (real or complex).
  68. This is the outer product (a level-2 operation) form of the LU
  69. Factorization with pivoting algorithm . This is equivalent to
  70. LAPACK's dgetf2. Also see "Matrix Computations" 3rd Ed. by Golub
  71. and Van Loan section 3.2.5 and especially page 115.
  72. The pivot indices in ipvt are indexed starting from 1
  73. so that this is compatible with LAPACK (Fortran).
  74. */
  75. template <typename DenseMatrix, typename Pvector>
  76. size_type lu_factor(DenseMatrix& A, Pvector& ipvt) {
  77. typedef typename linalg_traits<DenseMatrix>::value_type T;
  78. typedef typename linalg_traits<Pvector>::value_type int_T;
  79. typedef typename number_traits<T>::magnitude_type R;
  80. size_type info(0), i, j, jp, M(mat_nrows(A)), N(mat_ncols(A));
  81. size_type NN = std::min(M, N);
  82. std::vector<T> c(M), r(N);
  83. GMM_ASSERT2(ipvt.size()+1 >= NN, "IPVT too small");
  84. for (i = 0; i+1 < NN; ++i) ipvt[i] = int_T(i);
  85. if (M || N) {
  86. for (j = 0; j+1 < NN; ++j) {
  87. R max = gmm::abs(A(j,j)); jp = j;
  88. for (i = j+1; i < M; ++i) /* find pivot. */
  89. if (gmm::abs(A(i,j)) > max) { jp = i; max = gmm::abs(A(i,j)); }
  90. ipvt[j] = int_T(jp + 1);
  91. if (max == R(0)) { info = j + 1; break; }
  92. if (jp != j) for (i = 0; i < N; ++i) std::swap(A(jp, i), A(j, i));
  93. for (i = j+1; i < M; ++i) { A(i, j) /= A(j,j); c[i-j-1] = -A(i, j); }
  94. for (i = j+1; i < N; ++i) r[i-j-1] = A(j, i); // avoid the copy ?
  95. rank_one_update(sub_matrix(A, sub_interval(j+1, M-j-1),
  96. sub_interval(j+1, N-j-1)), c, conjugated(r));
  97. }
  98. ipvt[NN-1] = int_T(NN);
  99. }
  100. return info;
  101. }
  102. /** LU Solve : Solve equation Ax=b, given an LU factored matrix.*/
  103. // Thanks to Valient Gough for this routine!
  104. template <typename DenseMatrix, typename VectorB, typename VectorX,
  105. typename Pvector>
  106. void lu_solve(const DenseMatrix &LU, const Pvector& pvector,
  107. VectorX &x, const VectorB &b) {
  108. typedef typename linalg_traits<DenseMatrix>::value_type T;
  109. copy(b, x);
  110. for(size_type i = 0; i < pvector.size(); ++i) {
  111. size_type perm = pvector[i]-1; // permutations stored in 1's offset
  112. if(i != perm) { T aux = x[i]; x[i] = x[perm]; x[perm] = aux; }
  113. }
  114. /* solve Ax = b -> LUx = b -> Ux = L^-1 b. */
  115. lower_tri_solve(LU, x, true);
  116. upper_tri_solve(LU, x, false);
  117. }
  118. template <typename DenseMatrix, typename VectorB, typename VectorX>
  119. void lu_solve(const DenseMatrix &A, VectorX &x, const VectorB &b) {
  120. typedef typename linalg_traits<DenseMatrix>::value_type T;
  121. dense_matrix<T> B(mat_nrows(A), mat_ncols(A));
  122. std::vector<int> ipvt(mat_nrows(A));
  123. gmm::copy(A, B);
  124. size_type info = lu_factor(B, ipvt);
  125. GMM_ASSERT1(!info, "Singular system, pivot = " << info);
  126. lu_solve(B, ipvt, x, b);
  127. }
  128. template <typename DenseMatrix, typename VectorB, typename VectorX,
  129. typename Pvector>
  130. void lu_solve_transposed(const DenseMatrix &LU, const Pvector& pvector,
  131. VectorX &x, const VectorB &b) {
  132. typedef typename linalg_traits<DenseMatrix>::value_type T;
  133. copy(b, x);
  134. lower_tri_solve(transposed(LU), x, false);
  135. upper_tri_solve(transposed(LU), x, true);
  136. for(size_type i = pvector.size(); i > 0; --i) {
  137. size_type perm = pvector[i-1]-1; // permutations stored in 1's offset
  138. if(i-1 != perm) { T aux = x[i-1]; x[i-1] = x[perm]; x[perm] = aux; }
  139. }
  140. }
  141. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  142. template <typename DenseMatrixLU, typename DenseMatrix, typename Pvector>
  143. void lu_inverse(const DenseMatrixLU& LU, const Pvector& pvector,
  144. DenseMatrix& AInv, col_major) {
  145. typedef typename linalg_traits<DenseMatrixLU>::value_type T;
  146. std::vector<T> tmp(pvector.size(), T(0));
  147. std::vector<T> result(pvector.size());
  148. for(size_type i = 0; i < pvector.size(); ++i) {
  149. tmp[i] = T(1);
  150. lu_solve(LU, pvector, result, tmp);
  151. copy(result, mat_col(AInv, i));
  152. tmp[i] = T(0);
  153. }
  154. }
  155. template <typename DenseMatrixLU, typename DenseMatrix, typename Pvector>
  156. void lu_inverse(const DenseMatrixLU& LU, const Pvector& pvector,
  157. DenseMatrix& AInv, row_major) {
  158. typedef typename linalg_traits<DenseMatrixLU>::value_type T;
  159. std::vector<T> tmp(pvector.size(), T(0));
  160. std::vector<T> result(pvector.size());
  161. for(size_type i = 0; i < pvector.size(); ++i) {
  162. tmp[i] = T(1); // to be optimized !!
  163. // on peut sur le premier tri solve reduire le systeme
  164. // et peut etre faire un solve sur une serie de vecteurs au lieu
  165. // de vecteur a vecteur (accumulation directe de l'inverse dans la
  166. // matrice au fur et a mesure du calcul ... -> evite la copie finale
  167. lu_solve_transposed(LU, pvector, result, tmp);
  168. copy(result, mat_row(AInv, i));
  169. tmp[i] = T(0);
  170. }
  171. }
  172. ///@endcond
  173. /** Given an LU factored matrix, build the inverse of the matrix. */
  174. template <typename DenseMatrixLU, typename DenseMatrix, typename Pvector>
  175. void lu_inverse(const DenseMatrixLU& LU, const Pvector& pvector,
  176. const DenseMatrix& AInv_) {
  177. DenseMatrix& AInv = const_cast<DenseMatrix&>(AInv_);
  178. lu_inverse(LU, pvector, AInv, typename principal_orientation_type<typename
  179. linalg_traits<DenseMatrix>::sub_orientation>::potype());
  180. }
  181. /** Given a dense matrix, build the inverse of the matrix, and
  182. return the determinant */
  183. template <typename DenseMatrix>
  184. typename linalg_traits<DenseMatrix>::value_type
  185. lu_inverse(const DenseMatrix& A_, bool doassert = true) {
  186. typedef typename linalg_traits<DenseMatrix>::value_type T;
  187. DenseMatrix& A = const_cast<DenseMatrix&>(A_);
  188. dense_matrix<T> B(mat_nrows(A), mat_ncols(A));
  189. std::vector<int> ipvt(mat_nrows(A));
  190. gmm::copy(A, B);
  191. size_type info = lu_factor(B, ipvt);
  192. if (doassert) GMM_ASSERT1(!info, "Non invertible matrix, pivot = "<<info);
  193. if (!info) lu_inverse(B, ipvt, A);
  194. return lu_det(B, ipvt);
  195. }
  196. /** Compute the matrix determinant (via a LU factorization) */
  197. template <typename DenseMatrixLU, typename Pvector>
  198. typename linalg_traits<DenseMatrixLU>::value_type
  199. lu_det(const DenseMatrixLU& LU, const Pvector &pvector) {
  200. typedef typename linalg_traits<DenseMatrixLU>::value_type T;
  201. T det(1);
  202. for (size_type j = 0; j < std::min(mat_nrows(LU), mat_ncols(LU)); ++j)
  203. det *= LU(j,j);
  204. for(size_type i = 0; i < pvector.size(); ++i)
  205. if (i != size_type(pvector[i]-1)) { det = -det; }
  206. return det;
  207. }
  208. template <typename DenseMatrix>
  209. typename linalg_traits<DenseMatrix>::value_type
  210. lu_det(const DenseMatrix& A) {
  211. typedef typename linalg_traits<DenseMatrix>::value_type T;
  212. dense_matrix<T> B(mat_nrows(A), mat_ncols(A));
  213. std::vector<int> ipvt(mat_nrows(A));
  214. gmm::copy(A, B);
  215. lu_factor(B, ipvt);
  216. return lu_det(B, ipvt);
  217. }
  218. }
  219. #endif