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.

122 lines
4.0 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 Benoit Jacob <jacob.benoit.1@gmail.com>
  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/LU>
  11. template<typename Derived>
  12. void doSomeRankPreservingOperations(Eigen::MatrixBase<Derived>& m)
  13. {
  14. typedef typename Derived::RealScalar RealScalar;
  15. for(int a = 0; a < 3*(m.rows()+m.cols()); a++)
  16. {
  17. RealScalar d = Eigen::ei_random<RealScalar>(-1,1);
  18. int i = Eigen::ei_random<int>(0,m.rows()-1); // i is a random row number
  19. int j;
  20. do {
  21. j = Eigen::ei_random<int>(0,m.rows()-1);
  22. } while (i==j); // j is another one (must be different)
  23. m.row(i) += d * m.row(j);
  24. i = Eigen::ei_random<int>(0,m.cols()-1); // i is a random column number
  25. do {
  26. j = Eigen::ei_random<int>(0,m.cols()-1);
  27. } while (i==j); // j is another one (must be different)
  28. m.col(i) += d * m.col(j);
  29. }
  30. }
  31. template<typename MatrixType> void lu_non_invertible()
  32. {
  33. /* this test covers the following files:
  34. LU.h
  35. */
  36. // NOTE there seems to be a problem with too small sizes -- could easily lie in the doSomeRankPreservingOperations function
  37. int rows = ei_random<int>(20,200), cols = ei_random<int>(20,200), cols2 = ei_random<int>(20,200);
  38. int rank = ei_random<int>(1, std::min(rows, cols)-1);
  39. MatrixType m1(rows, cols), m2(cols, cols2), m3(rows, cols2), k(1,1);
  40. m1 = MatrixType::Random(rows,cols);
  41. if(rows <= cols)
  42. for(int i = rank; i < rows; i++) m1.row(i).setZero();
  43. else
  44. for(int i = rank; i < cols; i++) m1.col(i).setZero();
  45. doSomeRankPreservingOperations(m1);
  46. LU<MatrixType> lu(m1);
  47. typename LU<MatrixType>::KernelResultType m1kernel = lu.kernel();
  48. typename LU<MatrixType>::ImageResultType m1image = lu.image();
  49. VERIFY(rank == lu.rank());
  50. VERIFY(cols - lu.rank() == lu.dimensionOfKernel());
  51. VERIFY(!lu.isInjective());
  52. VERIFY(!lu.isInvertible());
  53. VERIFY(lu.isSurjective() == (lu.rank() == rows));
  54. VERIFY((m1 * m1kernel).isMuchSmallerThan(m1));
  55. VERIFY(m1image.lu().rank() == rank);
  56. MatrixType sidebyside(m1.rows(), m1.cols() + m1image.cols());
  57. sidebyside << m1, m1image;
  58. VERIFY(sidebyside.lu().rank() == rank);
  59. m2 = MatrixType::Random(cols,cols2);
  60. m3 = m1*m2;
  61. m2 = MatrixType::Random(cols,cols2);
  62. lu.solve(m3, &m2);
  63. VERIFY_IS_APPROX(m3, m1*m2);
  64. /* solve now always returns true
  65. m3 = MatrixType::Random(rows,cols2);
  66. VERIFY(!lu.solve(m3, &m2));
  67. */
  68. }
  69. template<typename MatrixType> void lu_invertible()
  70. {
  71. /* this test covers the following files:
  72. LU.h
  73. */
  74. typedef typename NumTraits<typename MatrixType::Scalar>::Real RealScalar;
  75. int size = ei_random<int>(10,200);
  76. MatrixType m1(size, size), m2(size, size), m3(size, size);
  77. m1 = MatrixType::Random(size,size);
  78. if (ei_is_same_type<RealScalar,float>::ret)
  79. {
  80. // let's build a matrix more stable to inverse
  81. MatrixType a = MatrixType::Random(size,size*2);
  82. m1 += a * a.adjoint();
  83. }
  84. LU<MatrixType> lu(m1);
  85. VERIFY(0 == lu.dimensionOfKernel());
  86. VERIFY(size == lu.rank());
  87. VERIFY(lu.isInjective());
  88. VERIFY(lu.isSurjective());
  89. VERIFY(lu.isInvertible());
  90. VERIFY(lu.image().lu().isInvertible());
  91. m3 = MatrixType::Random(size,size);
  92. lu.solve(m3, &m2);
  93. VERIFY_IS_APPROX(m3, m1*m2);
  94. VERIFY_IS_APPROX(m2, lu.inverse()*m3);
  95. m3 = MatrixType::Random(size,size);
  96. VERIFY(lu.solve(m3, &m2));
  97. }
  98. void test_eigen2_lu()
  99. {
  100. for(int i = 0; i < g_repeat; i++) {
  101. CALL_SUBTEST_1( lu_non_invertible<MatrixXf>() );
  102. CALL_SUBTEST_2( lu_non_invertible<MatrixXd>() );
  103. CALL_SUBTEST_3( lu_non_invertible<MatrixXcf>() );
  104. CALL_SUBTEST_4( lu_non_invertible<MatrixXcd>() );
  105. CALL_SUBTEST_1( lu_invertible<MatrixXf>() );
  106. CALL_SUBTEST_2( lu_invertible<MatrixXd>() );
  107. CALL_SUBTEST_3( lu_invertible<MatrixXcf>() );
  108. CALL_SUBTEST_4( lu_invertible<MatrixXcd>() );
  109. }
  110. }