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.

143 lines
5.3 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2003-2015 Yves Renard, Julien Pommier
  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_condition_number.h
  27. @author Yves Renard <Yves.Renard@insa-lyon.fr>, Julien Pommier <Julien.Pommier@insa-toulouse.fr>
  28. @date August 27, 2003.
  29. @brief computation of the condition number of dense matrices.
  30. */
  31. #ifndef GMM_CONDITION_NUMBER_H__
  32. #define GMM_CONDITION_NUMBER_H__
  33. #include "gmm_dense_qr.h"
  34. namespace gmm {
  35. /** computation of the condition number of dense matrices using SVD.
  36. Uses symmetric_qr_algorithm => dense matrices only.
  37. @param M a matrix.
  38. @param emin smallest (in magnitude) eigenvalue
  39. @param emax largest eigenvalue.
  40. */
  41. template <typename MAT>
  42. typename number_traits<typename
  43. linalg_traits<MAT>::value_type>::magnitude_type
  44. condition_number(const MAT& M,
  45. typename number_traits<typename
  46. linalg_traits<MAT>::value_type>::magnitude_type& emin,
  47. typename number_traits<typename
  48. linalg_traits<MAT>::value_type>::magnitude_type& emax) {
  49. typedef typename linalg_traits<MAT>::value_type T;
  50. typedef typename number_traits<T>::magnitude_type R;
  51. size_type m = mat_nrows(M), n = mat_ncols(M);
  52. emax = emin = R(0);
  53. std::vector<R> eig(m+n);
  54. if (m+n == 0) return R(0);
  55. if (is_hermitian(M)) {
  56. eig.resize(m);
  57. gmm::symmetric_qr_algorithm(M, eig);
  58. }
  59. else {
  60. dense_matrix<T> B(m+n, m+n); // not very efficient ??
  61. gmm::copy(conjugated(M), sub_matrix(B, sub_interval(m, n), sub_interval(0, m)));
  62. gmm::copy(M, sub_matrix(B, sub_interval(0, m),
  63. sub_interval(m, n)));
  64. gmm::symmetric_qr_algorithm(B, eig);
  65. }
  66. emin = emax = gmm::abs(eig[0]);
  67. for (size_type i = 1; i < eig.size(); ++i) {
  68. R e = gmm::abs(eig[i]);
  69. emin = std::min(emin, e);
  70. emax = std::max(emax, e);
  71. }
  72. // cout << "emin = " << emin << " emax = " << emax << endl;
  73. if (emin == R(0)) return gmm::default_max(R());
  74. return emax / emin;
  75. }
  76. template <typename MAT>
  77. typename number_traits<typename
  78. linalg_traits<MAT>::value_type>::magnitude_type
  79. condition_number(const MAT& M) {
  80. typename number_traits<typename
  81. linalg_traits<MAT>::value_type>::magnitude_type emax, emin;
  82. return condition_number(M, emin, emax);
  83. }
  84. template <typename MAT>
  85. typename number_traits<typename
  86. linalg_traits<MAT>::value_type>::magnitude_type
  87. Frobenius_condition_number_sqr(const MAT& M) {
  88. typedef typename linalg_traits<MAT>::value_type T;
  89. typedef typename number_traits<T>::magnitude_type R;
  90. size_type m = mat_nrows(M), n = mat_ncols(M);
  91. dense_matrix<T> B(std::min(m,n), std::min(m,n));
  92. if (m < n) mult(M,gmm::conjugated(M),B);
  93. else mult(gmm::conjugated(M),M,B);
  94. R trB = abs(mat_trace(B));
  95. lu_inverse(B);
  96. return trB*abs(mat_trace(B));
  97. }
  98. template <typename MAT>
  99. typename number_traits<typename
  100. linalg_traits<MAT>::value_type>::magnitude_type
  101. Frobenius_condition_number(const MAT& M)
  102. { return sqrt(Frobenius_condition_number_sqr(M)); }
  103. /** estimation of the condition number (TO BE DONE...)
  104. */
  105. template <typename MAT>
  106. typename number_traits<typename
  107. linalg_traits<MAT>::value_type>::magnitude_type
  108. condest(const MAT& M,
  109. typename number_traits<typename
  110. linalg_traits<MAT>::value_type>::magnitude_type& emin,
  111. typename number_traits<typename
  112. linalg_traits<MAT>::value_type>::magnitude_type& emax) {
  113. return condition_number(M, emin, emax);
  114. }
  115. template <typename MAT>
  116. typename number_traits<typename
  117. linalg_traits<MAT>::value_type>::magnitude_type
  118. condest(const MAT& M) {
  119. typename number_traits<typename
  120. linalg_traits<MAT>::value_type>::magnitude_type emax, emin;
  121. return condest(M, emin, emax);
  122. }
  123. }
  124. #endif