3
0

Solver3x3.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <algorithm>
  9. #include <cmath>
  10. #include <AzCore/std/algorithm.h>
  11. #include <LinearAlgebra.h>
  12. #include <Eigenanalysis/Solver3x3.h>
  13. #include <Eigenanalysis/Utilities.h>
  14. namespace NumericalMethods::Eigenanalysis
  15. {
  16. SolverResult<Real, 3> NonIterativeSymmetricEigensolver3x3(
  17. double a00, double a01, double a02, double a11, double a12, double a22
  18. )
  19. {
  20. // Using the notation from Eberly:
  21. // A - the symmetric input matrix
  22. // a<ij> - the upper elements of the matrix (0 <= i <= j <= 2).
  23. // B - a matrix derived from A, such that B = (A - q*I)/p where
  24. // p = sqrt( tr( (A-q*I)^2 ) / 6 )
  25. // q = tr(A) / 3
  26. // beta<i> - the eigenvalues of B (0 <= i <= 2)
  27. // alpha<i> - the eigenvalues of A (not explicit, stored in the result) (0 <= i <= 2)
  28. double alpha0 = 0.0;
  29. double alpha1 = 0.0;
  30. double alpha2 = 0.0;
  31. VectorVariable vec0 = VectorVariable::CreateFromVector({ 1.0, 0.0, 0.0 });
  32. VectorVariable vec1 = VectorVariable::CreateFromVector({ 0.0, 1.0, 0.0 });
  33. VectorVariable vec2 = VectorVariable::CreateFromVector({ 0.0, 0.0, 1.0 });
  34. // Precondition the matrix by factoring out the element of biggest magnitude. This is to guard against
  35. // floating-point overflow/underflow.
  36. double maxAbsElem = std::max({fabs(a00), fabs(a01), fabs(a02), fabs(a11), fabs(a12), fabs(a22)});
  37. if (maxAbsElem != 0.0)
  38. {
  39. // A is not the zero matrix.
  40. double invMaxAbsElem = 1.0 / maxAbsElem;
  41. a00 *= invMaxAbsElem;
  42. a01 *= invMaxAbsElem;
  43. a02 *= invMaxAbsElem;
  44. a11 *= invMaxAbsElem;
  45. a12 *= invMaxAbsElem;
  46. a22 *= invMaxAbsElem;
  47. double norm = a01 * a01 + a02 * a02 + a12 * a12;
  48. if (norm > 0.0)
  49. {
  50. // Compute the eigenvalues of A. For a detailed explanation of how the algorithm works, see Eberly.
  51. double q = (a00 + a11 + a22) / 3.0;
  52. double b00 = a00 - q;
  53. double b11 = a11 - q;
  54. double b22 = a22 - q;
  55. double p = sqrt((b00 * b00 + b11 * b11 + b22 * b22 + norm * 2.0) / 6.0);
  56. double c00 = b11 * b22 - a12 * a12;
  57. double c01 = a01 * b22 - a12 * a02;
  58. double c02 = a01 * a12 - b11 * a02;
  59. double det = (b00 * c00 - a01 * c01 + a02 * c02) / (p * p * p);
  60. double halfDet = AZStd::clamp(det * 0.5, -1.0, 1.0);
  61. double angle = acos(halfDet) / 3.0;
  62. static const double twoThirdsPi = 2.09439510239319549;
  63. // The eigenvalues of B are ordered such that beta0 <= beta1 <= beta2.
  64. double beta2 = cos(angle) * 2.0;
  65. double beta0 = cos(angle + twoThirdsPi) * 2.0;
  66. double beta1 = -(beta0 + beta2);
  67. // The eigenvalues of A are ordered such that alpha0 <= alpha1 <= alpha2.
  68. alpha0 = q + p * beta0;
  69. alpha1 = q + p * beta1;
  70. alpha2 = q + p * beta2;
  71. // Compute the eigenvectors. We either have
  72. // beta0 <= beta1 < 0 < beta2 (if halfDet >= 0); or
  73. // beta0 < 0 < beta1 <= beta2 (if halfDef < 0).
  74. // For numerical stability, we use different approaches to compute the eigenvector corresponding to the
  75. // eigenvalue that is definitely not repeated and the other two.
  76. if (halfDet >= 0.0)
  77. {
  78. vec2 = ComputeEigenvector0(a00, a01, a02, a11, a12, a22, alpha2);
  79. vec1 = ComputeEigenvector1(a00, a01, a02, a11, a12, a22, alpha1, vec2);
  80. vec0 = ComputeEigenvector2(vec1, vec2);
  81. }
  82. else
  83. {
  84. vec0 = ComputeEigenvector0(a00, a01, a02, a11, a12, a22, alpha0);
  85. vec1 = ComputeEigenvector1(a00, a01, a02, a11, a12, a22, alpha1, vec0);
  86. vec2 = ComputeEigenvector2(vec0, vec1);
  87. }
  88. }
  89. else
  90. {
  91. // A is a diagonal matrix. The eigenvalues in this case are the elements along the main diagonal, and
  92. // the eigenvectors are the standard Cartesian basis vectors.
  93. alpha0 = a00;
  94. alpha1 = a11;
  95. alpha2 = a22;
  96. }
  97. // The scaling applied to A in the precondition scales the eigenvalues by the same amount and must be
  98. // reverted.
  99. alpha0 *= maxAbsElem;
  100. alpha1 *= maxAbsElem;
  101. alpha2 *= maxAbsElem;
  102. }
  103. return SolverResult<Real, 3>{
  104. SolverOutcome::Success,
  105. {
  106. Eigenpair<Real, 3>{alpha0, {{vec0[0], vec0[1], vec0[2]}}},
  107. Eigenpair<Real, 3>{alpha1, {{vec1[0], vec1[1], vec1[2]}}},
  108. Eigenpair<Real, 3>{alpha2, {{vec2[0], vec2[1], vec2[2]}}}
  109. }
  110. };
  111. }
  112. } // namespace NumericalMethods::Eigenanalysis