condition_numbers.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // (C) Copyright Nick Thompson 2019.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_MATH_TOOLS_CONDITION_NUMBERS_HPP
  6. #define BOOST_MATH_TOOLS_CONDITION_NUMBERS_HPP
  7. #include <cmath>
  8. #include <limits>
  9. #include <boost/math/differentiation/finite_difference.hpp>
  10. #include <boost/math/tools/config.hpp>
  11. namespace boost { namespace math { namespace tools {
  12. template<class Real, bool kahan=true>
  13. class summation_condition_number {
  14. public:
  15. summation_condition_number(Real const x = 0)
  16. {
  17. using std::abs;
  18. m_l1 = abs(x);
  19. m_sum = x;
  20. m_c = 0;
  21. }
  22. void operator+=(Real const & x)
  23. {
  24. using std::abs;
  25. // No need to Kahan the l1 calc; it's well conditioned:
  26. m_l1 += abs(x);
  27. BOOST_MATH_IF_CONSTEXPR (kahan)
  28. {
  29. Real y = x - m_c;
  30. Real t = m_sum + y;
  31. m_c = (t-m_sum) -y;
  32. m_sum = t;
  33. }
  34. else
  35. {
  36. m_sum += x;
  37. }
  38. }
  39. inline void operator-=(Real const & x)
  40. {
  41. this->operator+=(-x);
  42. }
  43. // Is operator*= relevant? Presumably everything gets rescaled,
  44. // (m_sum -> k*m_sum, m_l1->k*m_l1, m_c->k*m_c),
  45. // but is this sensible? More important is it useful?
  46. // In addition, it might change the condition number.
  47. Real operator()() const
  48. {
  49. using std::abs;
  50. if (m_sum == Real(0) && m_l1 != Real(0))
  51. {
  52. return std::numeric_limits<Real>::infinity();
  53. }
  54. return m_l1/abs(m_sum);
  55. }
  56. Real sum() const
  57. {
  58. // Higham, 1993, "The Accuracy of Floating Point Summation":
  59. // "In [17] and [18], Kahan describes a variation of compensated summation in which the final sum is also corrected
  60. // thus s=s+e is appended to the algorithm above)."
  61. return m_sum + m_c;
  62. }
  63. Real l1_norm() const
  64. {
  65. return m_l1;
  66. }
  67. private:
  68. Real m_l1;
  69. Real m_sum;
  70. Real m_c;
  71. };
  72. template<class F, class Real>
  73. Real evaluation_condition_number(F const & f, Real const & x)
  74. {
  75. using std::abs;
  76. using std::isnan;
  77. using std::sqrt;
  78. using boost::math::differentiation::finite_difference_derivative;
  79. Real fx = f(x);
  80. if (isnan(fx))
  81. {
  82. return std::numeric_limits<Real>::quiet_NaN();
  83. }
  84. bool caught_exception = false;
  85. Real fp;
  86. #ifndef BOOST_MATH_NO_EXCEPTIONS
  87. try
  88. {
  89. #endif
  90. fp = finite_difference_derivative(f, x);
  91. #ifndef BOOST_MATH_NO_EXCEPTIONS
  92. }
  93. catch(...)
  94. {
  95. caught_exception = true;
  96. }
  97. #endif
  98. if (isnan(fp) || caught_exception)
  99. {
  100. // Check if the right derivative exists:
  101. fp = finite_difference_derivative<decltype(f), Real, 1>(f, x);
  102. if (isnan(fp))
  103. {
  104. // Check if a left derivative exists:
  105. const Real eps = (std::numeric_limits<Real>::epsilon)();
  106. Real h = - 2 * sqrt(eps);
  107. h = boost::math::differentiation::detail::make_xph_representable(x, h);
  108. Real yh = f(x + h);
  109. Real y0 = f(x);
  110. Real diff = yh - y0;
  111. fp = diff / h;
  112. if (isnan(fp))
  113. {
  114. return std::numeric_limits<Real>::quiet_NaN();
  115. }
  116. }
  117. }
  118. if (fx == 0)
  119. {
  120. if (x==0 || fp==0)
  121. {
  122. return std::numeric_limits<Real>::quiet_NaN();
  123. }
  124. return std::numeric_limits<Real>::infinity();
  125. }
  126. return abs(x*fp/fx);
  127. }
  128. }}} // Namespaces
  129. #endif