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