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.

87 lines
2.9 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra. Eigen itself is part of the KDE project.
  3. //
  4. // Copyright (C) 2008 Gael Guennebaud <g.gael@free.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. #include "main.h"
  10. #include <Eigen/SVD>
  11. template<typename MatrixType> void svd(const MatrixType& m)
  12. {
  13. /* this test covers the following files:
  14. SVD.h
  15. */
  16. int rows = m.rows();
  17. int cols = m.cols();
  18. typedef typename MatrixType::Scalar Scalar;
  19. typedef typename NumTraits<Scalar>::Real RealScalar;
  20. MatrixType a = MatrixType::Random(rows,cols);
  21. Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> b =
  22. Matrix<Scalar, MatrixType::RowsAtCompileTime, 1>::Random(rows,1);
  23. Matrix<Scalar, MatrixType::ColsAtCompileTime, 1> x(cols,1), x2(cols,1);
  24. RealScalar largerEps = test_precision<RealScalar>();
  25. if (ei_is_same_type<RealScalar,float>::ret)
  26. largerEps = 1e-3f;
  27. {
  28. SVD<MatrixType> svd(a);
  29. MatrixType sigma = MatrixType::Zero(rows,cols);
  30. MatrixType matU = MatrixType::Zero(rows,rows);
  31. sigma.block(0,0,cols,cols) = svd.singularValues().asDiagonal();
  32. matU.block(0,0,rows,cols) = svd.matrixU();
  33. VERIFY_IS_APPROX(a, matU * sigma * svd.matrixV().transpose());
  34. }
  35. if (rows==cols)
  36. {
  37. if (ei_is_same_type<RealScalar,float>::ret)
  38. {
  39. MatrixType a1 = MatrixType::Random(rows,cols);
  40. a += a * a.adjoint() + a1 * a1.adjoint();
  41. }
  42. SVD<MatrixType> svd(a);
  43. svd.solve(b, &x);
  44. VERIFY_IS_APPROX(a * x,b);
  45. }
  46. if(rows==cols)
  47. {
  48. SVD<MatrixType> svd(a);
  49. MatrixType unitary, positive;
  50. svd.computeUnitaryPositive(&unitary, &positive);
  51. VERIFY_IS_APPROX(unitary * unitary.adjoint(), MatrixType::Identity(unitary.rows(),unitary.rows()));
  52. VERIFY_IS_APPROX(positive, positive.adjoint());
  53. for(int i = 0; i < rows; i++) VERIFY(positive.diagonal()[i] >= 0); // cheap necessary (not sufficient) condition for positivity
  54. VERIFY_IS_APPROX(unitary*positive, a);
  55. svd.computePositiveUnitary(&positive, &unitary);
  56. VERIFY_IS_APPROX(unitary * unitary.adjoint(), MatrixType::Identity(unitary.rows(),unitary.rows()));
  57. VERIFY_IS_APPROX(positive, positive.adjoint());
  58. for(int i = 0; i < rows; i++) VERIFY(positive.diagonal()[i] >= 0); // cheap necessary (not sufficient) condition for positivity
  59. VERIFY_IS_APPROX(positive*unitary, a);
  60. }
  61. }
  62. void test_eigen2_svd()
  63. {
  64. for(int i = 0; i < g_repeat; i++) {
  65. CALL_SUBTEST_1( svd(Matrix3f()) );
  66. CALL_SUBTEST_2( svd(Matrix4d()) );
  67. CALL_SUBTEST_3( svd(MatrixXf(7,7)) );
  68. CALL_SUBTEST_4( svd(MatrixXd(14,7)) );
  69. // complex are not implemented yet
  70. // CALL_SUBTEST( svd(MatrixXcd(6,6)) );
  71. // CALL_SUBTEST( svd(MatrixXcf(3,3)) );
  72. SVD<MatrixXf> s;
  73. MatrixXf m = MatrixXf::Random(10,1);
  74. s.compute(m);
  75. }
  76. }