EigenValueSymmetric.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Core/FPFlushDenormals.h>
  5. JPH_NAMESPACE_BEGIN
  6. /// Function to determine the eigen vectors and values of a N x N real symmetric matrix
  7. /// by Jacobi transformations. This method is most suitable for N < 10.
  8. ///
  9. /// Taken and adapted from Numerical Recipies paragraph 11.1
  10. ///
  11. /// An eigen vector is a vector v for which \f$A \: v = \lambda \: v\f$
  12. ///
  13. /// Where:
  14. /// A: A square matrix.
  15. /// \f$\lambda\f$: a non-zero constant value.
  16. ///
  17. /// @see https://en.wikipedia.org/wiki/Eigenvalues_and_eigenvectors
  18. ///
  19. /// Matrix is a matrix type, which has dimensions N x N.
  20. /// @param inMatrix is the matrix of which to return the eigenvalues and vectors
  21. /// @param outEigVec will contain a matrix whose columns contain the normalized eigenvectors (must be identity before call)
  22. /// @param outEigVal will contain the eigenvalues
  23. template <class Vector, class Matrix>
  24. bool EigenValueSymmetric(const Matrix &inMatrix, Matrix &outEigVec, Vector &outEigVal)
  25. {
  26. // This algorithm works with very small numbers and can trigger invalid float exceptions when not flushing denormals
  27. FPFlushDenormals flush_denormals;
  28. (void)flush_denormals;
  29. // Maximum number of sweeps to make
  30. const int cMaxSweeps = 50;
  31. // Get problem dimension
  32. const uint n = inMatrix.GetRows();
  33. // Make sure the dimensions are right
  34. JPH_ASSERT(inMatrix.GetRows() == n);
  35. JPH_ASSERT(inMatrix.GetCols() == n);
  36. JPH_ASSERT(outEigVec.GetRows() == n);
  37. JPH_ASSERT(outEigVec.GetCols() == n);
  38. JPH_ASSERT(outEigVal.GetRows() == n);
  39. JPH_ASSERT(outEigVec.IsIdentity());
  40. // Get the matrix in a so we can mess with it
  41. Matrix a = inMatrix;
  42. Vector b, z;
  43. for (uint ip = 0; ip < n; ++ip)
  44. {
  45. // Initialize b to diagonal of a
  46. b[ip] = a(ip, ip);
  47. // Initialize output to diagonal of a
  48. outEigVal[ip] = a(ip, ip);
  49. // Reset z
  50. z[ip] = 0.0f;
  51. }
  52. for (int sweep = 0; sweep < cMaxSweeps; ++sweep)
  53. {
  54. // Get the sum of the off-diagonal elements of a
  55. float sm = 0.0f;
  56. for (uint ip = 0; ip < n - 1; ++ip)
  57. for (uint iq = ip + 1; iq < n; ++iq)
  58. sm += abs(a(ip, iq));
  59. // Normal return, convergence to machine underflow
  60. if (sm == 0.0f)
  61. {
  62. // Sanity checks
  63. #ifdef JPH_ENABLE_ASSERTS
  64. for (uint c = 0; c < n; ++c)
  65. {
  66. // Check if the eigenvector is normalized
  67. JPH_ASSERT(outEigVec.GetColumn(c).IsNormalized());
  68. // Check if inMatrix * eigen_vector = eigen_value * eigen_vector
  69. Vector mat_eigvec = inMatrix * outEigVec.GetColumn(c);
  70. Vector eigval_eigvec = outEigVal[c] * outEigVec.GetColumn(c);
  71. JPH_ASSERT(mat_eigvec.IsClose(eigval_eigvec, max(mat_eigvec.LengthSq(), eigval_eigvec.LengthSq()) * 1.0e-6f));
  72. }
  73. #endif
  74. // Success
  75. return true;
  76. }
  77. // On the first three sweeps use a fraction of the sum of the off diagonal elements as treshold
  78. float tresh = sweep < 4? 0.2f * sm / Square(n) : 0.0f;
  79. for (uint ip = 0; ip < n - 1; ++ip)
  80. for (uint iq = ip + 1; iq < n; ++iq)
  81. {
  82. float g = 100.0f * abs(a(ip, iq));
  83. // After four sweeps, skip the rotation if the off-diagonal element is small
  84. if (sweep > 4
  85. && abs(outEigVal[ip]) + g == abs(outEigVal[ip])
  86. && abs(outEigVal[iq]) + g == abs(outEigVal[iq]))
  87. {
  88. a(ip, iq) = 0.0f;
  89. }
  90. else if (abs(a(ip, iq)) > tresh)
  91. {
  92. float h = outEigVal[iq] - outEigVal[ip];
  93. float t;
  94. if (abs(h) + g == abs(h))
  95. {
  96. t = a(ip, iq) / h;
  97. }
  98. else
  99. {
  100. float theta = 0.5f * h / a(ip, iq); // Warning: Can become inf if a(ip, iq) too small
  101. t = 1.0f / (abs(theta) + sqrt(1.0f + theta * theta)); // Warning: Squaring large value can make it inf
  102. if (theta < 0.0f) t = -t;
  103. }
  104. float c = 1.0f / sqrt(1.0f + t * t);
  105. float s = t * c;
  106. float tau = s / (1.0f + c);
  107. h = t * a(ip, iq);
  108. a(ip, iq) = 0.0f;
  109. // !Modification from Numerical Recipes!
  110. // h can become infinite due to numerical overflow, this only happens when a(ip, iq) is very small
  111. // so we can safely set a(ip, iq) to zero and skip the rotation, see lines marked with 'Warning' above.
  112. if (!isnan(h))
  113. {
  114. z[ip] -= h;
  115. z[iq] += h;
  116. outEigVal[ip] -= h;
  117. outEigVal[iq] += h;
  118. #define JPH_EVS_ROTATE(a, i, j, k, l) \
  119. g = a(i, j), \
  120. h = a(k, l), \
  121. a(i, j) = g - s * (h + g * tau), \
  122. a(k, l) = h + s * (g - h * tau)
  123. uint j;
  124. for (j = 0; j < ip; ++j) JPH_EVS_ROTATE(a, j, ip, j, iq);
  125. for (j = ip + 1; j < iq; ++j) JPH_EVS_ROTATE(a, ip, j, j, iq);
  126. for (j = iq + 1; j < n; ++j) JPH_EVS_ROTATE(a, ip, j, iq, j);
  127. for (j = 0; j < n; ++j) JPH_EVS_ROTATE(outEigVec, j, ip, j, iq);
  128. #undef JPH_EVS_ROTATE
  129. }
  130. }
  131. }
  132. // Update eigenvalues with the sum of ta_pq and reinitialize z
  133. for (uint ip = 0; ip < n; ++ip)
  134. {
  135. b[ip] += z[ip];
  136. outEigVal[ip] = b[ip];
  137. z[ip] = 0.0f;
  138. }
  139. }
  140. // Failure
  141. JPH_ASSERT(false, "Too many iterations");
  142. return false;
  143. }
  144. JPH_NAMESPACE_END