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.

81 lines
2.7 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. // Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
  6. //
  7. // This Source Code Form is subject to the terms of the Mozilla
  8. // Public License v. 2.0. If a copy of the MPL was not distributed
  9. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  10. #include "main.h"
  11. #include <Eigen/SVD>
  12. template<typename MatrixType, typename JacobiScalar>
  13. void jacobi(const MatrixType& m = MatrixType())
  14. {
  15. typedef typename MatrixType::Index Index;
  16. Index rows = m.rows();
  17. Index cols = m.cols();
  18. enum {
  19. RowsAtCompileTime = MatrixType::RowsAtCompileTime,
  20. ColsAtCompileTime = MatrixType::ColsAtCompileTime
  21. };
  22. typedef Matrix<JacobiScalar, 2, 1> JacobiVector;
  23. const MatrixType a(MatrixType::Random(rows, cols));
  24. JacobiVector v = JacobiVector::Random().normalized();
  25. JacobiScalar c = v.x(), s = v.y();
  26. JacobiRotation<JacobiScalar> rot(c, s);
  27. {
  28. Index p = internal::random<Index>(0, rows-1);
  29. Index q;
  30. do {
  31. q = internal::random<Index>(0, rows-1);
  32. } while (q == p);
  33. MatrixType b = a;
  34. b.applyOnTheLeft(p, q, rot);
  35. VERIFY_IS_APPROX(b.row(p), c * a.row(p) + numext::conj(s) * a.row(q));
  36. VERIFY_IS_APPROX(b.row(q), -s * a.row(p) + numext::conj(c) * a.row(q));
  37. }
  38. {
  39. Index p = internal::random<Index>(0, cols-1);
  40. Index q;
  41. do {
  42. q = internal::random<Index>(0, cols-1);
  43. } while (q == p);
  44. MatrixType b = a;
  45. b.applyOnTheRight(p, q, rot);
  46. VERIFY_IS_APPROX(b.col(p), c * a.col(p) - s * a.col(q));
  47. VERIFY_IS_APPROX(b.col(q), numext::conj(s) * a.col(p) + numext::conj(c) * a.col(q));
  48. }
  49. }
  50. void test_jacobi()
  51. {
  52. for(int i = 0; i < g_repeat; i++) {
  53. CALL_SUBTEST_1(( jacobi<Matrix3f, float>() ));
  54. CALL_SUBTEST_2(( jacobi<Matrix4d, double>() ));
  55. CALL_SUBTEST_3(( jacobi<Matrix4cf, float>() ));
  56. CALL_SUBTEST_3(( jacobi<Matrix4cf, std::complex<float> >() ));
  57. int r = internal::random<int>(2, internal::random<int>(1,EIGEN_TEST_MAX_SIZE)/2),
  58. c = internal::random<int>(2, internal::random<int>(1,EIGEN_TEST_MAX_SIZE)/2);
  59. CALL_SUBTEST_4(( jacobi<MatrixXf, float>(MatrixXf(r,c)) ));
  60. CALL_SUBTEST_5(( jacobi<MatrixXcd, double>(MatrixXcd(r,c)) ));
  61. CALL_SUBTEST_5(( jacobi<MatrixXcd, std::complex<double> >(MatrixXcd(r,c)) ));
  62. // complex<float> is really important to test as it is the only way to cover conjugation issues in certain unaligned paths
  63. CALL_SUBTEST_6(( jacobi<MatrixXcf, float>(MatrixXcf(r,c)) ));
  64. CALL_SUBTEST_6(( jacobi<MatrixXcf, std::complex<float> >(MatrixXcf(r,c)) ));
  65. TEST_SET_BUT_UNUSED_VARIABLE(r);
  66. TEST_SET_BUT_UNUSED_VARIABLE(c);
  67. }
  68. }