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.

RootFinder.cpp 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*******************************************************************************
  2. "A Collection of Useful C++ Classes for Digital Signal Processing"
  3. By Vinnie Falco
  4. Official project location:
  5. https://github.com/vinniefalco/DSPFilters
  6. See Documentation.cpp for contact information, notes, and bibliography.
  7. --------------------------------------------------------------------------------
  8. License: MIT License (http://www.opensource.org/licenses/mit-license.php)
  9. Copyright (c) 2009 by Vinnie Falco
  10. Permission is hereby granted, free of charge, to any person obtaining a copy
  11. of this software and associated documentation files (the "Software"), to deal
  12. in the Software without restriction, including without limitation the rights
  13. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. copies of the Software, and to permit persons to whom the Software is
  15. furnished to do so, subject to the following conditions:
  16. The above copyright notice and this permission notice shall be included in
  17. all copies or substantial portions of the Software.
  18. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. THE SOFTWARE.
  25. *******************************************************************************/
  26. #include "Common.h"
  27. #include "RootFinder.h"
  28. #include <stdexcept>
  29. namespace Dsp
  30. {
  31. void RootFinderBase::solve(int degree,
  32. bool polish,
  33. bool doSort)
  34. {
  35. assert(degree <= m_maxdegree);
  36. const double EPS = 1.0e-30;
  37. int its;
  38. int m = degree;
  39. // copy coefficients
  40. for (int j = 0; j <= m; ++j) { m_ad[j] = m_a[j]; }
  41. // for each root
  42. for (int j = m - 1; j >= 0; --j)
  43. {
  44. // initial guess at 0
  45. complex_t x = 0.0;
  46. laguerre(j + 1, m_ad, x, its);
  47. if (fabs(std::imag(x)) <= 2.0 * EPS * fabs(std::real(x))) { x = complex_t(std::real(x), 0.0); }
  48. m_root[j] = x;
  49. // deflate
  50. complex_t b = m_ad[j + 1];
  51. for (int jj = j; jj >= 0; --jj)
  52. {
  53. complex_t c = m_ad[jj];
  54. m_ad[jj] = b;
  55. b = x * b + c;
  56. }
  57. }
  58. if (polish) { for (int j = 0; j < m; ++j) { laguerre(degree, m_a, m_root[j], its); } }
  59. if (doSort) { sort(degree); }
  60. }
  61. void RootFinderBase::sort(int degree)
  62. {
  63. for (int j = 1; j < degree; ++j)
  64. {
  65. complex_t x = m_root[j];
  66. int i;
  67. for (i = j - 1; i >= 0; --i)
  68. {
  69. if (m_root[i].imag() >= x.imag()) { break; }
  70. m_root[i + 1] = m_root[i];
  71. }
  72. m_root[i + 1] = x;
  73. }
  74. }
  75. //------------------------------------------------------------------------------
  76. void RootFinderBase::laguerre(int degree,
  77. complex_t a[],
  78. complex_t& x,
  79. int& its)
  80. {
  81. const int MR = 8, MT = 10, MAXIT = MT * MR;
  82. const double EPS = std::numeric_limits<double>::epsilon();
  83. static const double frac[MR + 1] =
  84. { 0.0, 0.5, 0.25, 0.75, 0.13, 0.38, 0.62, 0.88, 1.0 };
  85. complex_t f;
  86. int m = degree;
  87. for (int iter = 1; iter <= MAXIT; ++iter)
  88. {
  89. its = iter;
  90. complex_t b = a[m];
  91. double err = std::abs(b);
  92. complex_t d = f = 0.0;
  93. double abx = std::abs(x);
  94. for (int j = m - 1; j >= 0; --j)
  95. {
  96. f = x * f + d;
  97. d = x * d + b;
  98. b = x * b + a[j];
  99. err = std::abs(b) + abx * err;
  100. }
  101. err *= EPS;
  102. if (std::abs(b) <= err) { return; }
  103. complex_t g = d / b;
  104. complex_t g2 = g * g;
  105. complex_t h = g2 - 2.0 * f / b;
  106. complex_t sq = sqrt(double(m - 1) * (double(m) * h - g2));
  107. complex_t gp = g + sq;
  108. complex_t gm = g - sq;
  109. double abp = std::abs(gp);
  110. double abm = std::abs(gm);
  111. if (abp < abm) { gp = gm; }
  112. complex_t dx = std::max(abp, abm) > 0.0 ? double(m) / gp : std::polar(1 + abx, double(iter));
  113. complex_t x1 = x - dx;
  114. if (x == x1) { return; }
  115. if (iter % MT != 0) { x = x1; }
  116. else { x -= frac[iter / MT] * dx; }
  117. }
  118. throw std::logic_error("laguerre failed");
  119. }
  120. //------------------------------------------------------------------------------
  121. complex_t RootFinderBase::eval(int degree,
  122. const complex_t& x)
  123. {
  124. complex_t y;
  125. if (x != 0.) { for (int i = 0; i <= degree; ++i) { y += m_a[i] * pow(x, double(i)); } }
  126. else { y = m_a[0]; }
  127. return y;
  128. }
  129. } // namespace Dsp