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.

111 lines
4.1 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2013 Gauthier Brun <brun.gauthier@gmail.com>
  5. // Copyright (C) 2013 Nicolas Carre <nicolas.carre@ensimag.fr>
  6. // Copyright (C) 2013 Jean Ceccato <jean.ceccato@ensimag.fr>
  7. // Copyright (C) 2013 Pierre Zoppitelli <pierre.zoppitelli@ensimag.fr>
  8. //
  9. // This Source Code Form is subject to the terms of the Mozilla
  10. // Public License v. 2.0. If a copy of the MPL was not distributed
  11. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/
  12. // discard stack allocation as that too bypasses malloc
  13. #define EIGEN_STACK_ALLOCATION_LIMIT 0
  14. #define EIGEN_RUNTIME_NO_MALLOC
  15. #include "main.h"
  16. #include <Eigen/SVD>
  17. #include <iostream>
  18. #include <Eigen/LU>
  19. #define SVD_DEFAULT(M) BDCSVD<M>
  20. #define SVD_FOR_MIN_NORM(M) BDCSVD<M>
  21. #include "svd_common.h"
  22. // Check all variants of JacobiSVD
  23. template<typename MatrixType>
  24. void bdcsvd(const MatrixType& a = MatrixType(), bool pickrandom = true)
  25. {
  26. MatrixType m = a;
  27. if(pickrandom)
  28. svd_fill_random(m);
  29. CALL_SUBTEST(( svd_test_all_computation_options<BDCSVD<MatrixType> >(m, false) ));
  30. }
  31. template<typename MatrixType>
  32. void bdcsvd_method()
  33. {
  34. enum { Size = MatrixType::RowsAtCompileTime };
  35. typedef typename MatrixType::RealScalar RealScalar;
  36. typedef Matrix<RealScalar, Size, 1> RealVecType;
  37. MatrixType m = MatrixType::Identity();
  38. VERIFY_IS_APPROX(m.bdcSvd().singularValues(), RealVecType::Ones());
  39. VERIFY_RAISES_ASSERT(m.bdcSvd().matrixU());
  40. VERIFY_RAISES_ASSERT(m.bdcSvd().matrixV());
  41. VERIFY_IS_APPROX(m.bdcSvd(ComputeFullU|ComputeFullV).solve(m), m);
  42. }
  43. // compare the Singular values returned with Jacobi and Bdc
  44. template<typename MatrixType>
  45. void compare_bdc_jacobi(const MatrixType& a = MatrixType(), unsigned int computationOptions = 0)
  46. {
  47. MatrixType m = MatrixType::Random(a.rows(), a.cols());
  48. BDCSVD<MatrixType> bdc_svd(m);
  49. JacobiSVD<MatrixType> jacobi_svd(m);
  50. VERIFY_IS_APPROX(bdc_svd.singularValues(), jacobi_svd.singularValues());
  51. if(computationOptions & ComputeFullU) VERIFY_IS_APPROX(bdc_svd.matrixU(), jacobi_svd.matrixU());
  52. if(computationOptions & ComputeThinU) VERIFY_IS_APPROX(bdc_svd.matrixU(), jacobi_svd.matrixU());
  53. if(computationOptions & ComputeFullV) VERIFY_IS_APPROX(bdc_svd.matrixV(), jacobi_svd.matrixV());
  54. if(computationOptions & ComputeThinV) VERIFY_IS_APPROX(bdc_svd.matrixV(), jacobi_svd.matrixV());
  55. }
  56. void test_bdcsvd()
  57. {
  58. CALL_SUBTEST_3(( svd_verify_assert<BDCSVD<Matrix3f> >(Matrix3f()) ));
  59. CALL_SUBTEST_4(( svd_verify_assert<BDCSVD<Matrix4d> >(Matrix4d()) ));
  60. CALL_SUBTEST_7(( svd_verify_assert<BDCSVD<MatrixXf> >(MatrixXf(10,12)) ));
  61. CALL_SUBTEST_8(( svd_verify_assert<BDCSVD<MatrixXcd> >(MatrixXcd(7,5)) ));
  62. CALL_SUBTEST_101(( svd_all_trivial_2x2(bdcsvd<Matrix2cd>) ));
  63. CALL_SUBTEST_102(( svd_all_trivial_2x2(bdcsvd<Matrix2d>) ));
  64. for(int i = 0; i < g_repeat; i++) {
  65. CALL_SUBTEST_3(( bdcsvd<Matrix3f>() ));
  66. CALL_SUBTEST_4(( bdcsvd<Matrix4d>() ));
  67. CALL_SUBTEST_5(( bdcsvd<Matrix<float,3,5> >() ));
  68. int r = internal::random<int>(1, EIGEN_TEST_MAX_SIZE/2),
  69. c = internal::random<int>(1, EIGEN_TEST_MAX_SIZE/2);
  70. TEST_SET_BUT_UNUSED_VARIABLE(r)
  71. TEST_SET_BUT_UNUSED_VARIABLE(c)
  72. CALL_SUBTEST_6(( bdcsvd(Matrix<double,Dynamic,2>(r,2)) ));
  73. CALL_SUBTEST_7(( bdcsvd(MatrixXf(r,c)) ));
  74. CALL_SUBTEST_7(( compare_bdc_jacobi(MatrixXf(r,c)) ));
  75. CALL_SUBTEST_10(( bdcsvd(MatrixXd(r,c)) ));
  76. CALL_SUBTEST_10(( compare_bdc_jacobi(MatrixXd(r,c)) ));
  77. CALL_SUBTEST_8(( bdcsvd(MatrixXcd(r,c)) ));
  78. CALL_SUBTEST_8(( compare_bdc_jacobi(MatrixXcd(r,c)) ));
  79. // Test on inf/nan matrix
  80. CALL_SUBTEST_7( (svd_inf_nan<BDCSVD<MatrixXf>, MatrixXf>()) );
  81. CALL_SUBTEST_10( (svd_inf_nan<BDCSVD<MatrixXd>, MatrixXd>()) );
  82. }
  83. // test matrixbase method
  84. CALL_SUBTEST_1(( bdcsvd_method<Matrix2cd>() ));
  85. CALL_SUBTEST_3(( bdcsvd_method<Matrix3f>() ));
  86. // Test problem size constructors
  87. CALL_SUBTEST_7( BDCSVD<MatrixXf>(10,10) );
  88. // Check that preallocation avoids subsequent mallocs
  89. CALL_SUBTEST_9( svd_preallocate<void>() );
  90. CALL_SUBTEST_2( svd_underoverflow<void>() );
  91. }