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.

196 lines
7.0 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2010 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. // The computeRoots function included in this is based on materials
  10. // covered by the following copyright and license:
  11. //
  12. // Geometric Tools, LLC
  13. // Copyright (c) 1998-2010
  14. // Distributed under the Boost Software License, Version 1.0.
  15. //
  16. // Permission is hereby granted, free of charge, to any person or organization
  17. // obtaining a copy of the software and accompanying documentation covered by
  18. // this license (the "Software") to use, reproduce, display, distribute,
  19. // execute, and transmit the Software, and to prepare derivative works of the
  20. // Software, and to permit third-parties to whom the Software is furnished to
  21. // do so, all subject to the following:
  22. //
  23. // The copyright notices in the Software and this entire statement, including
  24. // the above license grant, this restriction and the following disclaimer,
  25. // must be included in all copies of the Software, in whole or in part, and
  26. // all derivative works of the Software, unless such copies or derivative
  27. // works are solely in the form of machine-executable object code generated by
  28. // a source language processor.
  29. //
  30. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  31. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  32. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  33. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  34. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  35. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  36. // DEALINGS IN THE SOFTWARE.
  37. #include <iostream>
  38. #include <Eigen/Core>
  39. #include <Eigen/Eigenvalues>
  40. #include <Eigen/Geometry>
  41. #include <bench/BenchTimer.h>
  42. using namespace Eigen;
  43. using namespace std;
  44. template<typename Matrix, typename Roots>
  45. inline void computeRoots(const Matrix& m, Roots& roots)
  46. {
  47. typedef typename Matrix::Scalar Scalar;
  48. const Scalar s_inv3 = 1.0/3.0;
  49. const Scalar s_sqrt3 = internal::sqrt(Scalar(3.0));
  50. // The characteristic equation is x^3 - c2*x^2 + c1*x - c0 = 0. The
  51. // eigenvalues are the roots to this equation, all guaranteed to be
  52. // real-valued, because the matrix is symmetric.
  53. Scalar c0 = m(0,0)*m(1,1)*m(2,2) + Scalar(2)*m(0,1)*m(0,2)*m(1,2) - m(0,0)*m(1,2)*m(1,2) - m(1,1)*m(0,2)*m(0,2) - m(2,2)*m(0,1)*m(0,1);
  54. Scalar c1 = m(0,0)*m(1,1) - m(0,1)*m(0,1) + m(0,0)*m(2,2) - m(0,2)*m(0,2) + m(1,1)*m(2,2) - m(1,2)*m(1,2);
  55. Scalar c2 = m(0,0) + m(1,1) + m(2,2);
  56. // Construct the parameters used in classifying the roots of the equation
  57. // and in solving the equation for the roots in closed form.
  58. Scalar c2_over_3 = c2*s_inv3;
  59. Scalar a_over_3 = (c1 - c2*c2_over_3)*s_inv3;
  60. if (a_over_3 > Scalar(0))
  61. a_over_3 = Scalar(0);
  62. Scalar half_b = Scalar(0.5)*(c0 + c2_over_3*(Scalar(2)*c2_over_3*c2_over_3 - c1));
  63. Scalar q = half_b*half_b + a_over_3*a_over_3*a_over_3;
  64. if (q > Scalar(0))
  65. q = Scalar(0);
  66. // Compute the eigenvalues by solving for the roots of the polynomial.
  67. Scalar rho = internal::sqrt(-a_over_3);
  68. Scalar theta = std::atan2(internal::sqrt(-q),half_b)*s_inv3;
  69. Scalar cos_theta = internal::cos(theta);
  70. Scalar sin_theta = internal::sin(theta);
  71. roots(0) = c2_over_3 + Scalar(2)*rho*cos_theta;
  72. roots(1) = c2_over_3 - rho*(cos_theta + s_sqrt3*sin_theta);
  73. roots(2) = c2_over_3 - rho*(cos_theta - s_sqrt3*sin_theta);
  74. // Sort in increasing order.
  75. if (roots(0) >= roots(1))
  76. std::swap(roots(0),roots(1));
  77. if (roots(1) >= roots(2))
  78. {
  79. std::swap(roots(1),roots(2));
  80. if (roots(0) >= roots(1))
  81. std::swap(roots(0),roots(1));
  82. }
  83. }
  84. template<typename Matrix, typename Vector>
  85. void eigen33(const Matrix& mat, Matrix& evecs, Vector& evals)
  86. {
  87. typedef typename Matrix::Scalar Scalar;
  88. // Scale the matrix so its entries are in [-1,1]. The scaling is applied
  89. // only when at least one matrix entry has magnitude larger than 1.
  90. Scalar scale = mat.cwiseAbs()/*.template triangularView<Lower>()*/.maxCoeff();
  91. scale = std::max(scale,Scalar(1));
  92. Matrix scaledMat = mat / scale;
  93. // Compute the eigenvalues
  94. // scaledMat.setZero();
  95. computeRoots(scaledMat,evals);
  96. // compute the eigen vectors
  97. // **here we assume 3 differents eigenvalues**
  98. // "optimized version" which appears to be slower with gcc!
  99. // Vector base;
  100. // Scalar alpha, beta;
  101. // base << scaledMat(1,0) * scaledMat(2,1),
  102. // scaledMat(1,0) * scaledMat(2,0),
  103. // -scaledMat(1,0) * scaledMat(1,0);
  104. // for(int k=0; k<2; ++k)
  105. // {
  106. // alpha = scaledMat(0,0) - evals(k);
  107. // beta = scaledMat(1,1) - evals(k);
  108. // evecs.col(k) = (base + Vector(-beta*scaledMat(2,0), -alpha*scaledMat(2,1), alpha*beta)).normalized();
  109. // }
  110. // evecs.col(2) = evecs.col(0).cross(evecs.col(1)).normalized();
  111. // // naive version
  112. // Matrix tmp;
  113. // tmp = scaledMat;
  114. // tmp.diagonal().array() -= evals(0);
  115. // evecs.col(0) = tmp.row(0).cross(tmp.row(1)).normalized();
  116. //
  117. // tmp = scaledMat;
  118. // tmp.diagonal().array() -= evals(1);
  119. // evecs.col(1) = tmp.row(0).cross(tmp.row(1)).normalized();
  120. //
  121. // tmp = scaledMat;
  122. // tmp.diagonal().array() -= evals(2);
  123. // evecs.col(2) = tmp.row(0).cross(tmp.row(1)).normalized();
  124. // a more stable version:
  125. if((evals(2)-evals(0))<=Eigen::NumTraits<Scalar>::epsilon())
  126. {
  127. evecs.setIdentity();
  128. }
  129. else
  130. {
  131. Matrix tmp;
  132. tmp = scaledMat;
  133. tmp.diagonal ().array () -= evals (2);
  134. evecs.col (2) = tmp.row (0).cross (tmp.row (1)).normalized ();
  135. tmp = scaledMat;
  136. tmp.diagonal ().array () -= evals (1);
  137. evecs.col(1) = tmp.row (0).cross(tmp.row (1));
  138. Scalar n1 = evecs.col(1).norm();
  139. if(n1<=Eigen::NumTraits<Scalar>::epsilon())
  140. evecs.col(1) = evecs.col(2).unitOrthogonal();
  141. else
  142. evecs.col(1) /= n1;
  143. // make sure that evecs[1] is orthogonal to evecs[2]
  144. evecs.col(1) = evecs.col(2).cross(evecs.col(1).cross(evecs.col(2))).normalized();
  145. evecs.col(0) = evecs.col(2).cross(evecs.col(1));
  146. }
  147. // Rescale back to the original size.
  148. evals *= scale;
  149. }
  150. int main()
  151. {
  152. BenchTimer t;
  153. int tries = 10;
  154. int rep = 400000;
  155. typedef Matrix3f Mat;
  156. typedef Vector3f Vec;
  157. Mat A = Mat::Random(3,3);
  158. A = A.adjoint() * A;
  159. SelfAdjointEigenSolver<Mat> eig(A);
  160. BENCH(t, tries, rep, eig.compute(A));
  161. std::cout << "Eigen: " << t.best() << "s\n";
  162. Mat evecs;
  163. Vec evals;
  164. BENCH(t, tries, rep, eigen33(A,evecs,evals));
  165. std::cout << "Direct: " << t.best() << "s\n\n";
  166. std::cerr << "Eigenvalue/eigenvector diffs:\n";
  167. std::cerr << (evals - eig.eigenvalues()).transpose() << "\n";
  168. for(int k=0;k<3;++k)
  169. if(evecs.col(k).dot(eig.eigenvectors().col(k))<0)
  170. evecs.col(k) = -evecs.col(k);
  171. std::cerr << evecs - eig.eigenvectors() << "\n\n";
  172. }