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.

99 lines
3.2 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2014-2015 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. template<typename MatrixType>
  10. void svd_fill_random(MatrixType &m, int Option = 0)
  11. {
  12. typedef typename MatrixType::Scalar Scalar;
  13. typedef typename MatrixType::RealScalar RealScalar;
  14. typedef typename MatrixType::Index Index;
  15. Index diagSize = (std::min)(m.rows(), m.cols());
  16. RealScalar s = std::numeric_limits<RealScalar>::max_exponent10/4;
  17. s = internal::random<RealScalar>(1,s);
  18. Matrix<RealScalar,Dynamic,1> d = Matrix<RealScalar,Dynamic,1>::Random(diagSize);
  19. for(Index k=0; k<diagSize; ++k)
  20. d(k) = d(k)*std::pow(RealScalar(10),internal::random<RealScalar>(-s,s));
  21. bool dup = internal::random<int>(0,10) < 3;
  22. bool unit_uv = internal::random<int>(0,10) < (dup?7:3); // if we duplicate some diagonal entries, then increase the chance to preserve them using unitary U and V factors
  23. // duplicate some singular values
  24. if(dup)
  25. {
  26. Index n = internal::random<Index>(0,d.size()-1);
  27. for(Index i=0; i<n; ++i)
  28. d(internal::random<Index>(0,d.size()-1)) = d(internal::random<Index>(0,d.size()-1));
  29. }
  30. Matrix<Scalar,Dynamic,Dynamic> U(m.rows(),diagSize);
  31. Matrix<Scalar,Dynamic,Dynamic> VT(diagSize,m.cols());
  32. if(unit_uv)
  33. {
  34. // in very rare cases let's try with a pure diagonal matrix
  35. if(internal::random<int>(0,10) < 1)
  36. {
  37. U.setIdentity();
  38. VT.setIdentity();
  39. }
  40. else
  41. {
  42. createRandomPIMatrixOfRank(diagSize,U.rows(), U.cols(), U);
  43. createRandomPIMatrixOfRank(diagSize,VT.rows(), VT.cols(), VT);
  44. }
  45. }
  46. else
  47. {
  48. U.setRandom();
  49. VT.setRandom();
  50. }
  51. Matrix<Scalar,Dynamic,1> samples(7);
  52. samples << 0, 5.60844e-313, -5.60844e-313, 4.94e-324, -4.94e-324, -1./NumTraits<RealScalar>::highest(), 1./NumTraits<RealScalar>::highest();
  53. if(Option==Symmetric)
  54. {
  55. m = U * d.asDiagonal() * U.transpose();
  56. // randomly nullify some rows/columns
  57. {
  58. Index count = internal::random<Index>(-diagSize,diagSize);
  59. for(Index k=0; k<count; ++k)
  60. {
  61. Index i = internal::random<Index>(0,diagSize-1);
  62. m.row(i).setZero();
  63. m.col(i).setZero();
  64. }
  65. if(count<0)
  66. // (partly) cancel some coeffs
  67. if(!(dup && unit_uv))
  68. {
  69. Index n = internal::random<Index>(0,m.size()-1);
  70. for(Index k=0; k<n; ++k)
  71. {
  72. Index i = internal::random<Index>(0,m.rows()-1);
  73. Index j = internal::random<Index>(0,m.cols()-1);
  74. m(j,i) = m(i,j) = samples(internal::random<Index>(0,samples.size()-1));
  75. }
  76. }
  77. }
  78. }
  79. else
  80. {
  81. m = U * d.asDiagonal() * VT;
  82. // (partly) cancel some coeffs
  83. if(!(dup && unit_uv))
  84. {
  85. Index n = internal::random<Index>(0,m.size()-1);
  86. for(Index i=0; i<n; ++i)
  87. m(internal::random<Index>(0,m.rows()-1), internal::random<Index>(0,m.cols()-1)) = samples(internal::random<Index>(0,samples.size()-1));
  88. }
  89. }
  90. }