jacobisvd.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008-2014 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. // discard stack allocation as that too bypasses malloc
  11. #define EIGEN_STACK_ALLOCATION_LIMIT 0
  12. #define EIGEN_RUNTIME_NO_MALLOC
  13. #include "main.h"
  14. #include <Eigen/SVD>
  15. #define SVD_DEFAULT(M) JacobiSVD<M>
  16. #define SVD_FOR_MIN_NORM(M) JacobiSVD<M,ColPivHouseholderQRPreconditioner>
  17. #include "svd_common.h"
  18. // Check all variants of JacobiSVD
  19. template<typename MatrixType>
  20. void jacobisvd(const MatrixType& a = MatrixType(), bool pickrandom = true)
  21. {
  22. MatrixType m = a;
  23. if(pickrandom)
  24. svd_fill_random(m);
  25. CALL_SUBTEST(( svd_test_all_computation_options<JacobiSVD<MatrixType, FullPivHouseholderQRPreconditioner> >(m, true) )); // check full only
  26. CALL_SUBTEST(( svd_test_all_computation_options<JacobiSVD<MatrixType, ColPivHouseholderQRPreconditioner> >(m, false) ));
  27. CALL_SUBTEST(( svd_test_all_computation_options<JacobiSVD<MatrixType, HouseholderQRPreconditioner> >(m, false) ));
  28. if(m.rows()==m.cols())
  29. CALL_SUBTEST(( svd_test_all_computation_options<JacobiSVD<MatrixType, NoQRPreconditioner> >(m, false) ));
  30. }
  31. template<typename MatrixType> void jacobisvd_verify_assert(const MatrixType& m)
  32. {
  33. svd_verify_assert<JacobiSVD<MatrixType> >(m);
  34. svd_verify_assert<JacobiSVD<MatrixType, FullPivHouseholderQRPreconditioner> >(m, true);
  35. svd_verify_assert<JacobiSVD<MatrixType, ColPivHouseholderQRPreconditioner> >(m);
  36. svd_verify_assert<JacobiSVD<MatrixType, HouseholderQRPreconditioner> >(m);
  37. Index rows = m.rows();
  38. Index cols = m.cols();
  39. enum {
  40. ColsAtCompileTime = MatrixType::ColsAtCompileTime
  41. };
  42. MatrixType a = MatrixType::Zero(rows, cols);
  43. a.setZero();
  44. if (ColsAtCompileTime == Dynamic)
  45. {
  46. JacobiSVD<MatrixType, FullPivHouseholderQRPreconditioner> svd_fullqr;
  47. VERIFY_RAISES_ASSERT(svd_fullqr.compute(a, ComputeFullU|ComputeThinV))
  48. VERIFY_RAISES_ASSERT(svd_fullqr.compute(a, ComputeThinU|ComputeThinV))
  49. VERIFY_RAISES_ASSERT(svd_fullqr.compute(a, ComputeThinU|ComputeFullV))
  50. }
  51. }
  52. template<typename MatrixType>
  53. void jacobisvd_method()
  54. {
  55. enum { Size = MatrixType::RowsAtCompileTime };
  56. typedef typename MatrixType::RealScalar RealScalar;
  57. typedef Matrix<RealScalar, Size, 1> RealVecType;
  58. MatrixType m = MatrixType::Identity();
  59. VERIFY_IS_APPROX(m.jacobiSvd().singularValues(), RealVecType::Ones());
  60. VERIFY_RAISES_ASSERT(m.jacobiSvd().matrixU());
  61. VERIFY_RAISES_ASSERT(m.jacobiSvd().matrixV());
  62. VERIFY_IS_APPROX(m.jacobiSvd(ComputeFullU|ComputeFullV).solve(m), m);
  63. VERIFY_IS_APPROX(m.jacobiSvd(ComputeFullU|ComputeFullV).transpose().solve(m), m);
  64. VERIFY_IS_APPROX(m.jacobiSvd(ComputeFullU|ComputeFullV).adjoint().solve(m), m);
  65. }
  66. namespace Foo {
  67. // older compiler require a default constructor for Bar
  68. // cf: https://stackoverflow.com/questions/7411515/
  69. class Bar {public: Bar() {}};
  70. bool operator<(const Bar&, const Bar&) { return true; }
  71. }
  72. // regression test for a very strange MSVC issue for which simply
  73. // including SVDBase.h messes up with std::max and custom scalar type
  74. void msvc_workaround()
  75. {
  76. const Foo::Bar a;
  77. const Foo::Bar b;
  78. std::max EIGEN_NOT_A_MACRO (a,b);
  79. }
  80. EIGEN_DECLARE_TEST(jacobisvd)
  81. {
  82. CALL_SUBTEST_3(( jacobisvd_verify_assert(Matrix3f()) ));
  83. CALL_SUBTEST_4(( jacobisvd_verify_assert(Matrix4d()) ));
  84. CALL_SUBTEST_7(( jacobisvd_verify_assert(MatrixXf(10,12)) ));
  85. CALL_SUBTEST_8(( jacobisvd_verify_assert(MatrixXcd(7,5)) ));
  86. CALL_SUBTEST_11(svd_all_trivial_2x2(jacobisvd<Matrix2cd>));
  87. CALL_SUBTEST_12(svd_all_trivial_2x2(jacobisvd<Matrix2d>));
  88. for(int i = 0; i < g_repeat; i++) {
  89. CALL_SUBTEST_3(( jacobisvd<Matrix3f>() ));
  90. CALL_SUBTEST_4(( jacobisvd<Matrix4d>() ));
  91. CALL_SUBTEST_5(( jacobisvd<Matrix<float,3,5> >() ));
  92. CALL_SUBTEST_6(( jacobisvd<Matrix<double,Dynamic,2> >(Matrix<double,Dynamic,2>(10,2)) ));
  93. int r = internal::random<int>(1, 30),
  94. c = internal::random<int>(1, 30);
  95. TEST_SET_BUT_UNUSED_VARIABLE(r)
  96. TEST_SET_BUT_UNUSED_VARIABLE(c)
  97. CALL_SUBTEST_10(( jacobisvd<MatrixXd>(MatrixXd(r,c)) ));
  98. CALL_SUBTEST_7(( jacobisvd<MatrixXf>(MatrixXf(r,c)) ));
  99. CALL_SUBTEST_8(( jacobisvd<MatrixXcd>(MatrixXcd(r,c)) ));
  100. (void) r;
  101. (void) c;
  102. // Test on inf/nan matrix
  103. CALL_SUBTEST_7( (svd_inf_nan<JacobiSVD<MatrixXf>, MatrixXf>()) );
  104. CALL_SUBTEST_10( (svd_inf_nan<JacobiSVD<MatrixXd>, MatrixXd>()) );
  105. // bug1395 test compile-time vectors as input
  106. CALL_SUBTEST_13(( jacobisvd_verify_assert(Matrix<double,6,1>()) ));
  107. CALL_SUBTEST_13(( jacobisvd_verify_assert(Matrix<double,1,6>()) ));
  108. CALL_SUBTEST_13(( jacobisvd_verify_assert(Matrix<double,Dynamic,1>(r)) ));
  109. CALL_SUBTEST_13(( jacobisvd_verify_assert(Matrix<double,1,Dynamic>(c)) ));
  110. }
  111. CALL_SUBTEST_7(( jacobisvd<MatrixXf>(MatrixXf(internal::random<int>(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE/2), internal::random<int>(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE/2))) ));
  112. CALL_SUBTEST_8(( jacobisvd<MatrixXcd>(MatrixXcd(internal::random<int>(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE/3), internal::random<int>(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE/3))) ));
  113. // test matrixbase method
  114. CALL_SUBTEST_1(( jacobisvd_method<Matrix2cd>() ));
  115. CALL_SUBTEST_3(( jacobisvd_method<Matrix3f>() ));
  116. // Test problem size constructors
  117. CALL_SUBTEST_7( JacobiSVD<MatrixXf>(10,10) );
  118. // Check that preallocation avoids subsequent mallocs
  119. CALL_SUBTEST_9( svd_preallocate<void>() );
  120. CALL_SUBTEST_2( svd_underoverflow<void>() );
  121. msvc_workaround();
  122. }