CubicRootsQR.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // David Eberly, Geometric Tools, Redmond WA 98052
  2. // Copyright (c) 1998-2020
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  6. // Version: 4.0.2019.08.13
  7. #pragma once
  8. #include <array>
  9. #include <cmath>
  10. #include <cstdint>
  11. // An implementation of the QR algorithm described in "Matrix Computations,
  12. // 2nd edition" by G. H. Golub and C. F. Van Loan, The Johns Hopkins
  13. // University Press, Baltimore MD, Fourth Printing 1993. In particular,
  14. // the implementation is based on Chapter 7 (The Unsymmetric Eigenvalue
  15. // Problem), Section 7.5 (The Practical QR Algorithm). The algorithm is
  16. // specialized for the companion matrix associated with a cubic polynomial.
  17. namespace WwiseGTE
  18. {
  19. template <typename Real>
  20. class CubicRootsQR
  21. {
  22. public:
  23. typedef std::array<std::array<Real, 3>, 3> Matrix;
  24. // Solve p(x) = c0 + c1 * x + c2 * x^2 + x^3 = 0.
  25. uint32_t operator() (uint32_t maxIterations, Real c0, Real c1, Real c2,
  26. uint32_t& numRoots, std::array<Real, 3>& roots) const
  27. {
  28. // Create the companion matrix for the polynomial. The matrix is
  29. // in upper Hessenberg form.
  30. Matrix A;
  31. A[0][0] = (Real)0;
  32. A[0][1] = (Real)0;
  33. A[0][2] = -c0;
  34. A[1][0] = (Real)1;
  35. A[1][1] = (Real)0;
  36. A[1][2] = -c1;
  37. A[2][0] = (Real)0;
  38. A[2][1] = (Real)1;
  39. A[2][2] = -c2;
  40. // Avoid the QR-cycle when c1 = c2 = 0 and avoid the slow
  41. // convergence when c1 and c2 are nearly zero.
  42. std::array<Real, 3> V{
  43. (Real)1,
  44. (Real)0.36602540378443865,
  45. (Real)0.36602540378443865 };
  46. DoIteration(V, A);
  47. return operator()(maxIterations, A, numRoots, roots);
  48. }
  49. // Compute the real eigenvalues of the upper Hessenberg matrix A. The
  50. // matrix is modified by in-place operations, so if you need to remember
  51. // A, you must make your own copy before calling this function.
  52. uint32_t operator() (uint32_t maxIterations, Matrix& A,
  53. uint32_t& numRoots, std::array<Real, 3>& roots) const
  54. {
  55. numRoots = 0;
  56. std::fill(roots.begin(), roots.end(), (Real)0);
  57. for (uint32_t numIterations = 0; numIterations < maxIterations; ++numIterations)
  58. {
  59. // Apply a Francis QR iteration.
  60. Real tr = A[1][1] + A[2][2];
  61. Real det = A[1][1] * A[2][2] - A[1][2] * A[2][1];
  62. std::array<Real, 3> X{
  63. A[0][0] * A[0][0] + A[0][1] * A[1][0] - tr * A[0][0] + det,
  64. A[1][0] * (A[0][0] + A[1][1] - tr),
  65. A[1][0] * A[2][1] };
  66. std::array<Real, 3> V = House<3>(X);
  67. DoIteration(V, A);
  68. // Test for uncoupling of A.
  69. Real tr01 = A[0][0] + A[1][1];
  70. if (tr01 + A[1][0] == tr01)
  71. {
  72. numRoots = 1;
  73. roots[0] = A[0][0];
  74. GetQuadraticRoots(1, 2, A, numRoots, roots);
  75. return numIterations;
  76. }
  77. Real tr12 = A[1][1] + A[2][2];
  78. if (tr12 + A[2][1] == tr12)
  79. {
  80. numRoots = 1;
  81. roots[0] = A[2][2];
  82. GetQuadraticRoots(0, 1, A, numRoots, roots);
  83. return numIterations;
  84. }
  85. }
  86. return maxIterations;
  87. }
  88. private:
  89. void DoIteration(std::array<Real, 3> const& V, Matrix& A) const
  90. {
  91. Real multV = (Real)-2 / (V[0] * V[0] + V[1] * V[1] + V[2] * V[2]);
  92. std::array<Real, 3> MV{ multV * V[0], multV * V[1], multV * V[2] };
  93. RowHouse<3>(0, 2, 0, 2, V, MV, A);
  94. ColHouse<3>(0, 2, 0, 2, V, MV, A);
  95. std::array<Real, 2> Y{ A[1][0], A[2][0] };
  96. std::array<Real, 2> W = House<2>(Y);
  97. Real multW = (Real)-2 / (W[0] * W[0] + W[1] * W[1]);
  98. std::array<Real, 2> MW{ multW * W[0], multW * W[1] };
  99. RowHouse<2>(1, 2, 0, 2, W, MW, A);
  100. ColHouse<2>(0, 2, 1, 2, W, MW, A);
  101. }
  102. template <int N>
  103. std::array<Real, N> House(std::array<Real, N> const& X) const
  104. {
  105. std::array<Real, N> V;
  106. Real length = (Real)0;
  107. for (int i = 0; i < N; ++i)
  108. {
  109. length += X[i] * X[i];
  110. }
  111. length = std::sqrt(length);
  112. if (length != (Real)0)
  113. {
  114. Real sign = (X[0] >= (Real)0 ? (Real)1 : (Real)-1);
  115. Real denom = X[0] + sign * length;
  116. for (int i = 1; i < N; ++i)
  117. {
  118. V[i] = X[i] / denom;
  119. }
  120. }
  121. else
  122. {
  123. V.fill((Real)0);
  124. }
  125. V[0] = (Real)1;
  126. return V;
  127. }
  128. template <int N>
  129. void RowHouse(int rmin, int rmax, int cmin, int cmax,
  130. std::array<Real, N> const& V, std::array<Real, N> const& MV, Matrix& A) const
  131. {
  132. // Only the elements cmin through cmax are used.
  133. std::array<Real, 3> W;
  134. for (int c = cmin; c <= cmax; ++c)
  135. {
  136. W[c] = (Real)0;
  137. for (int r = rmin, k = 0; r <= rmax; ++r, ++k)
  138. {
  139. W[c] += V[k] * A[r][c];
  140. }
  141. }
  142. for (int r = rmin, k = 0; r <= rmax; ++r, ++k)
  143. {
  144. for (int c = cmin; c <= cmax; ++c)
  145. {
  146. A[r][c] += MV[k] * W[c];
  147. }
  148. }
  149. }
  150. template <int N>
  151. void ColHouse(int rmin, int rmax, int cmin, int cmax,
  152. std::array<Real, N> const& V, std::array<Real, N> const& MV, Matrix& A) const
  153. {
  154. // Only elements rmin through rmax are used.
  155. std::array<Real, 3> W;
  156. for (int r = rmin; r <= rmax; ++r)
  157. {
  158. W[r] = (Real)0;
  159. for (int c = cmin, k = 0; c <= cmax; ++c, ++k)
  160. {
  161. W[r] += V[k] * A[r][c];
  162. }
  163. }
  164. for (int r = rmin; r <= rmax; ++r)
  165. {
  166. for (int c = cmin, k = 0; c <= cmax; ++c, ++k)
  167. {
  168. A[r][c] += W[r] * MV[k];
  169. }
  170. }
  171. }
  172. void GetQuadraticRoots(int i0, int i1, Matrix const& A,
  173. uint32_t& numRoots, std::array<Real, 3>& roots) const
  174. {
  175. // Solve x^2 - t * x + d = 0, where t is the trace and d is the
  176. // determinant of the 2x2 matrix defined by indices i0 and i1.
  177. // The discriminant is D = (t/2)^2 - d. When D >= 0, the roots
  178. // are real values t/2 - sqrt(D) and t/2 + sqrt(D). To avoid
  179. // potential numerical issues with subtractive cancellation, the
  180. // roots are computed as
  181. // r0 = t/2 + sign(t/2)*sqrt(D), r1 = trace - r0.
  182. Real trace = A[i0][i0] + A[i1][i1];
  183. Real halfTrace = trace * (Real)0.5;
  184. Real determinant = A[i0][i0] * A[i1][i1] - A[i0][i1] * A[i1][i0];
  185. Real discriminant = halfTrace * halfTrace - determinant;
  186. if (discriminant >= (Real)0)
  187. {
  188. Real sign = (trace >= (Real)0 ? (Real)1 : (Real)-1);
  189. Real root = halfTrace + sign * std::sqrt(discriminant);
  190. roots[numRoots++] = root;
  191. roots[numRoots++] = trace - root;
  192. }
  193. }
  194. };
  195. }