TestLinearSolve.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * @file TestLinearSolve.cpp
  3. * @brief TestLinearSolve
  4. * @author Erik Rodner
  5. * @date 21.12.2011
  6. */
  7. #include "TestLinearSolve.h"
  8. #include <string>
  9. #include <vector>
  10. #include "core/basics/cppunitex.h"
  11. #include "core/basics/numerictools.h"
  12. #include "core/vector/Distance.h"
  13. #include "core/vector/Algorithms.h"
  14. #include "core/algebra/ILSPlainGradient.h"
  15. #include "core/algebra/ILSConjugateGradients.h"
  16. #include "core/algebra/ILSConjugateGradientsLanczos.h"
  17. #include "core/algebra/ILSSymmLqLanczos.h"
  18. #include "core/algebra/ILSMinResLanczos.h"
  19. #include "core/algebra/GMStandard.h"
  20. #include "core/algebra/GBCDSolver.h"
  21. using namespace std;
  22. using namespace NICE;
  23. CPPUNIT_TEST_SUITE_REGISTRATION(TestLinearSolve);
  24. void TestLinearSolve::setUp()
  25. {
  26. }
  27. void TestLinearSolve::tearDown()
  28. {
  29. }
  30. void TestLinearSolve::TestLinearSolveComputation()
  31. {
  32. // verbose flag for additional output for each iteration
  33. bool verbose = false;
  34. // size of the matrix
  35. uint rows = 15;
  36. uint cols = rows;
  37. // probability of zero entries
  38. double sparse_prob = 0.0;
  39. NICE::Matrix T(rows, cols, 0.0);
  40. // use a fixed seed, its a test case
  41. srand48(0);
  42. // generate random symmetric matrix
  43. for (uint i = 0 ; i < rows ; i++)
  44. for (uint j = i ; j < cols ; j++)
  45. {
  46. if (sparse_prob != 0.0)
  47. if (drand48() < sparse_prob)
  48. continue;
  49. T(i, j) = drand48();
  50. T(j, i) = T(i, j);
  51. }
  52. // use positive definite matrices
  53. T = T*T;
  54. T.addIdentity(1.0);
  55. NICE::Vector b = Vector::UniformRandom( rows, 0.0, 1.0, 0 );
  56. GMStandard Tg(T);
  57. GMSparse Ts(T, 0.0);
  58. CPPUNIT_ASSERT_EQUAL((int)T.rows(),(int)Ts.rows());
  59. CPPUNIT_ASSERT_EQUAL((int)T.cols(),(int)Ts.cols());
  60. // first of all test the generic matrix interface
  61. for ( uint i = 0 ; i < rows ; i++ )
  62. {
  63. NICE::Vector x ( rows );
  64. for ( uint j = 0 ; j < x.size(); j++ )
  65. x[j] = drand48();
  66. Vector yg;
  67. Vector ys;
  68. Tg.multiply ( yg, x );
  69. Ts.multiply ( ys, x );
  70. CPPUNIT_ASSERT_DOUBLES_EQUAL_NOT_NAN(0.0,(yg-ys).normL2(),1e-10);
  71. }
  72. vector< IterativeLinearSolver * > methods;
  73. int max_iterations = 100;
  74. methods.push_back ( new ILSPlainGradient(verbose, max_iterations*10) );
  75. methods.push_back ( new ILSConjugateGradients(verbose, max_iterations) );
  76. methods.push_back ( new ILSConjugateGradientsLanczos(verbose, max_iterations) );
  77. methods.push_back ( new ILSSymmLqLanczos(verbose, max_iterations) );
  78. methods.push_back ( new ILSMinResLanczos(verbose, max_iterations) );
  79. // the following method is pretty instable!! and needs to much time
  80. //methods.push_back ( new ILSPlainGradient(verbose, max_iterations, false /* minResidual */) );
  81. //methods.push_back ( new ILSPlainGradient(verbose, max_iterations, true /* minResidual */) );
  82. //Vector solstd;
  83. //solveLinearEquationQR( T, b, solstd );
  84. for ( vector< IterativeLinearSolver * >::const_iterator i = methods.begin();
  85. i != methods.end(); i++ )
  86. {
  87. IterativeLinearSolver *method = *i;
  88. Vector solg (Tg.cols(), 0.0);
  89. Vector sols (Ts.cols(), 0.0);
  90. if ( verbose )
  91. cerr << "solving the sparse system ..." << endl;
  92. method->solveLin ( Ts, b, sols );
  93. if ( verbose )
  94. cerr << "solving the dense system ..." << endl;
  95. method->solveLin ( Tg, b, solg );
  96. Vector bg;
  97. Tg.multiply ( bg, solg );
  98. Vector bs;
  99. Ts.multiply ( bs, sols );
  100. // compute residuals
  101. if ( verbose )
  102. {
  103. cerr << "solg = " << solg << endl;
  104. cerr << "sols = " << sols << endl;
  105. cerr << "bg = " << bg << endl;
  106. cerr << "bs = " << bs << endl;
  107. cerr << "b = " << b << endl;
  108. }
  109. double err_dense = ( b - bg ).normL2();
  110. double err_sparse = ( b - bs ).normL2();
  111. CPPUNIT_ASSERT_DOUBLES_EQUAL_NOT_NAN(0.0,err_dense,1e-1);
  112. CPPUNIT_ASSERT_DOUBLES_EQUAL_NOT_NAN(0.0,err_sparse,1e-1);
  113. }
  114. // ---------- check the greedy block coordinate descent method
  115. Vector solg (0.0);
  116. GBCDSolver gbcd ( 5, 10, verbose, 100 /*maximum iterations*/ );
  117. gbcd.solveLin ( Tg, b, solg );
  118. Vector bg;
  119. Tg.multiply ( bg, solg );
  120. double err_dense = ( b - bg ).normL2();
  121. CPPUNIT_ASSERT_DOUBLES_EQUAL_NOT_NAN(0.0,err_dense,1e-4);
  122. }