GetFEM  5.6
gmm_condition_number.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2003-2026 Yves Renard, Julien Pommier
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_condition_number.h
32  @author Yves Renard <Yves.Renard@insa-lyon.fr>, Julien Pommier <Julien.Pommier@insa-toulouse.fr>
33  @date August 27, 2003.
34  @brief computation of the condition number of dense matrices.
35 */
36 #ifndef GMM_CONDITION_NUMBER_H__
37 #define GMM_CONDITION_NUMBER_H__
38 
39 #include "gmm_dense_qr.h"
40 
41 namespace gmm {
42 
43  /* Scalar type used internally for the eigenvalue computation of
44  condition_number. Single precision is promoted to double precision:
45  with the default tolerance of the single precision type, the
46  symmetric QR algorithm may fail to converge on moderately large
47  matrices ("QR algorithm failed."). */
48  template <typename T> struct condition_number_work_type
49  { typedef T type; };
50  template <> struct condition_number_work_type<float>
51  { typedef double type; };
52  template <> struct condition_number_work_type<std::complex<float> >
53  { typedef std::complex<double> type; };
54 
55  /** computation of the condition number of matrices (ratio of the
56  extreme singular values).
57 
58  Uses symmetric_qr_algorithm on a dense copy of the matrix. Single
59  precision matrices are handled in double precision.
60 
61  @param M a matrix.
62  @param emin smallest (in magnitude) eigenvalue
63  @param emax largest eigenvalue.
64  */
65  template <typename MAT>
66  typename number_traits<typename
67  linalg_traits<MAT>::value_type>::magnitude_type
68  condition_number(const MAT& M,
69  typename number_traits<typename
70  linalg_traits<MAT>::value_type>::magnitude_type& emin,
71  typename number_traits<typename
72  linalg_traits<MAT>::value_type>::magnitude_type& emax) {
73  typedef typename linalg_traits<MAT>::value_type T;
74  typedef typename number_traits<T>::magnitude_type R;
75  typedef typename condition_number_work_type<T>::type TW;
76  typedef typename number_traits<TW>::magnitude_type RW;
77 
78  // Added because of errors in complex with zero det
79  if (sizeof(T) != sizeof(R) && gmm::abs(gmm::lu_det(M)) == R(0))
80  return gmm::default_max(R());
81 
82  size_type m = mat_nrows(M), n = mat_ncols(M);
83  emax = emin = R(0);
84  std::vector<RW> eig(m+n);
85 
86  if (m+n == 0) return R(0);
87  if (is_hermitian(M)) {
88  dense_matrix<TW> A(m, m);
89  gmm::copy(M, A);
90  eig.resize(m);
91  gmm::symmetric_qr_algorithm(A, eig);
92  }
93  else {
94  dense_matrix<TW> B(m+n, m+n); // not very efficient ??
95  gmm::copy(conjugated(M), sub_matrix(B, sub_interval(m, n), sub_interval(0, m)));
96  gmm::copy(M, sub_matrix(B, sub_interval(0, m),
97  sub_interval(m, n)));
98  gmm::symmetric_qr_algorithm(B, eig);
99  }
100  RW eminw = gmm::abs(eig[0]), emaxw = eminw;
101  for (size_type i = 1; i < eig.size(); ++i) {
102  RW e = gmm::abs(eig[i]);
103  eminw = std::min(eminw, e);
104  emaxw = std::max(emaxw, e);
105  }
106  emin = R(eminw); emax = R(emaxw);
107  if (eminw == RW(0)) return gmm::default_max(R());
108  RW c = emaxw / eminw;
109  // avoid an overflow when converting back to single precision
110  if (c >= RW(gmm::default_max(R()))) return gmm::default_max(R());
111  return R(c);
112  }
113 
114  template <typename MAT>
115  typename number_traits<typename
116  linalg_traits<MAT>::value_type>::magnitude_type
117  condition_number(const MAT& M) {
118  typename number_traits<typename
119  linalg_traits<MAT>::value_type>::magnitude_type emax, emin;
120  return condition_number(M, emin, emax);
121  }
122 
123  template <typename MAT>
124  typename number_traits<typename
125  linalg_traits<MAT>::value_type>::magnitude_type
126  Frobenius_condition_number_sqr(const MAT& M) {
127  typedef typename linalg_traits<MAT>::value_type T;
128  typedef typename number_traits<T>::magnitude_type R;
129  size_type m = mat_nrows(M), n = mat_ncols(M);
130  dense_matrix<T> B(std::min(m,n), std::min(m,n));
131  if (m < n) mult(M,gmm::conjugated(M),B);
132  else mult(gmm::conjugated(M),M,B);
133  R trB = abs(mat_trace(B));
134  lu_inverse(B);
135  return trB*abs(mat_trace(B));
136  }
137 
138  template <typename MAT>
139  typename number_traits<typename
140  linalg_traits<MAT>::value_type>::magnitude_type
141  Frobenius_condition_number(const MAT& M)
142  { return sqrt(Frobenius_condition_number_sqr(M)); }
143 
144  /** estimation of the condition number (TO BE DONE...)
145  */
146  template <typename MAT>
147  typename number_traits<typename
148  linalg_traits<MAT>::value_type>::magnitude_type
149  condest(const MAT& M,
150  typename number_traits<typename
151  linalg_traits<MAT>::value_type>::magnitude_type& emin,
152  typename number_traits<typename
153  linalg_traits<MAT>::value_type>::magnitude_type& emax) {
154  return condition_number(M, emin, emax);
155  }
156 
157  template <typename MAT>
158  typename number_traits<typename
159  linalg_traits<MAT>::value_type>::magnitude_type
160  condest(const MAT& M) {
161  typename number_traits<typename
162  linalg_traits<MAT>::value_type>::magnitude_type emax, emin;
163  return condest(M, emin, emax);
164  }
165 }
166 
167 #endif
void copy(const L1 &l1, L2 &l2)
*‍/
Definition: gmm_blas.h:976
linalg_traits< M >::value_type mat_trace(const M &m)
Trace of a matrix.
Definition: gmm_blas.h:527
void mult(const L1 &l1, const L2 &l2, L3 &l3)
*‍/
Definition: gmm_blas.h:1663
bool is_hermitian(const MAT &A, magnitude_of_linalg(MAT) tol=magnitude_of_linalg(MAT)(-1))
*‍/
Definition: gmm_blas.h:2240
number_traits< typename linalg_traits< MAT >::value_type >::magnitude_type condition_number(const MAT &M, typename number_traits< typename linalg_traits< MAT >::value_type >::magnitude_type &emin, typename number_traits< typename linalg_traits< MAT >::value_type >::magnitude_type &emax)
computation of the condition number of matrices (ratio of the extreme singular values).
number_traits< typename linalg_traits< MAT >::value_type >::magnitude_type condest(const MAT &M, typename number_traits< typename linalg_traits< MAT >::value_type >::magnitude_type &emin, typename number_traits< typename linalg_traits< MAT >::value_type >::magnitude_type &emax)
estimation of the condition number (TO BE DONE...)
conjugated_return< L >::return_type conjugated(const L &v)
return a conjugated view of the input matrix or vector.
linalg_traits< DenseMatrixLU >::value_type lu_det(const DenseMatrixLU &LU, const Pvector &pvector)
Compute the matrix determinant (via a LU factorization)
Definition: gmm_dense_lu.h:240
Dense QR factorization.
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:48