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.

147 lines
5.5 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2003-2017 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. // Added because of errors in complex with zero det
  52. if (sizeof(T) != sizeof(R) && gmm::abs(gmm::lu_det(M)) == R(0))
  53. return gmm::default_max(R());
  54. size_type m = mat_nrows(M), n = mat_ncols(M);
  55. emax = emin = R(0);
  56. std::vector<R> eig(m+n);
  57. if (m+n == 0) return R(0);
  58. if (is_hermitian(M)) {
  59. eig.resize(m);
  60. gmm::symmetric_qr_algorithm(M, eig);
  61. }
  62. else {
  63. dense_matrix<T> B(m+n, m+n); // not very efficient ??
  64. gmm::copy(conjugated(M), sub_matrix(B, sub_interval(m, n), sub_interval(0, m)));
  65. gmm::copy(M, sub_matrix(B, sub_interval(0, m),
  66. sub_interval(m, n)));
  67. gmm::symmetric_qr_algorithm(B, eig);
  68. }
  69. emin = emax = gmm::abs(eig[0]);
  70. for (size_type i = 1; i < eig.size(); ++i) {
  71. R e = gmm::abs(eig[i]);
  72. emin = std::min(emin, e);
  73. emax = std::max(emax, e);
  74. }
  75. // cout << "emin = " << emin << " emax = " << emax << endl;
  76. if (emin == R(0)) return gmm::default_max(R());
  77. return emax / emin;
  78. }
  79. template <typename MAT>
  80. typename number_traits<typename
  81. linalg_traits<MAT>::value_type>::magnitude_type
  82. condition_number(const MAT& M) {
  83. typename number_traits<typename
  84. linalg_traits<MAT>::value_type>::magnitude_type emax, emin;
  85. return condition_number(M, emin, emax);
  86. }
  87. template <typename MAT>
  88. typename number_traits<typename
  89. linalg_traits<MAT>::value_type>::magnitude_type
  90. Frobenius_condition_number_sqr(const MAT& M) {
  91. typedef typename linalg_traits<MAT>::value_type T;
  92. typedef typename number_traits<T>::magnitude_type R;
  93. size_type m = mat_nrows(M), n = mat_ncols(M);
  94. dense_matrix<T> B(std::min(m,n), std::min(m,n));
  95. if (m < n) mult(M,gmm::conjugated(M),B);
  96. else mult(gmm::conjugated(M),M,B);
  97. R trB = abs(mat_trace(B));
  98. lu_inverse(B);
  99. return trB*abs(mat_trace(B));
  100. }
  101. template <typename MAT>
  102. typename number_traits<typename
  103. linalg_traits<MAT>::value_type>::magnitude_type
  104. Frobenius_condition_number(const MAT& M)
  105. { return sqrt(Frobenius_condition_number_sqr(M)); }
  106. /** estimation of the condition number (TO BE DONE...)
  107. */
  108. template <typename MAT>
  109. typename number_traits<typename
  110. linalg_traits<MAT>::value_type>::magnitude_type
  111. condest(const MAT& M,
  112. typename number_traits<typename
  113. linalg_traits<MAT>::value_type>::magnitude_type& emin,
  114. typename number_traits<typename
  115. linalg_traits<MAT>::value_type>::magnitude_type& emax) {
  116. return condition_number(M, emin, emax);
  117. }
  118. template <typename MAT>
  119. typename number_traits<typename
  120. linalg_traits<MAT>::value_type>::magnitude_type
  121. condest(const MAT& M) {
  122. typename number_traits<typename
  123. linalg_traits<MAT>::value_type>::magnitude_type emax, emin;
  124. return condest(M, emin, emax);
  125. }
  126. }
  127. #endif