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.h 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #pragma once
  27. #include "Common.h"
  28. #include "Types.h"
  29. #include <algorithm>
  30. namespace Dsp
  31. {
  32. //
  33. // Finds the complex roots of the given polynomial with
  34. // complex-valued coefficients using a numerical method.
  35. //
  36. class RootFinderBase
  37. {
  38. public:
  39. struct Array
  40. {
  41. Array(int /*max*/, complex_t* /*values*/) /* : m_max (max), m_values (values)*/ { }
  42. //complex_t& operator[] (int index) { };
  43. };
  44. //
  45. // Find roots of polynomial f(x)=a[0]+a[1]*x+a[2]*x^2...+a[degree]*x^degree
  46. // The input coefficients are set using coef()[].
  47. // The solutions are placed in roots.
  48. //
  49. void solve(int degree, bool polish = true, bool doSort = true);
  50. // Evaluates the polynomial at x
  51. complex_t eval(int degree, const complex_t& x);
  52. // Direct access to the input coefficient array of size degree+1.
  53. complex_t* coef() { return m_a; }
  54. // Direct access to the resulting roots array of size degree
  55. complex_t* root() { return m_root; }
  56. // sort the roots by descending imaginary part
  57. void sort(int degree);
  58. private:
  59. // Improves x as a root using Laguerre's method.
  60. // The input coefficient array has degree+1 elements.
  61. void laguerre(int degree, complex_t a[], complex_t& x, int& its);
  62. protected:
  63. int m_maxdegree = 0;
  64. complex_t* m_a = nullptr; // input coefficients (m_maxdegree+1 elements)
  65. complex_t* m_ad = nullptr; // copy of deflating coefficients
  66. complex_t* m_root = nullptr; // array of roots (maxdegree elements)
  67. };
  68. //------------------------------------------------------------------------------
  69. template <int maxdegree>
  70. struct RootFinder : RootFinderBase
  71. {
  72. RootFinder()
  73. {
  74. m_maxdegree = maxdegree;
  75. m_a = m_a0;
  76. m_ad = m_ad0;
  77. m_root = m_r;
  78. }
  79. private:
  80. complex_t m_a0 [maxdegree + 1];
  81. complex_t m_ad0[maxdegree + 1];
  82. complex_t m_r [maxdegree];
  83. };
  84. } // namespace Dsp