TestDiagApprox.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * @file TestDiagApprox.cpp
  3. * @brief TestDiagApprox
  4. * @author Michael Koch
  5. * @date Di Aug 4 2009
  6. */
  7. #include "TestDiagApprox.h"
  8. #include <string>
  9. #include "core/basics/cppunitex.h"
  10. #include "core/basics/numerictools.h"
  11. #include "core/vector/Distance.h"
  12. #include "core/algebra/DiagonalMatrixApprox.h"
  13. using namespace std;
  14. using namespace NICE;
  15. CPPUNIT_TEST_SUITE_REGISTRATION(TestDiagApprox);
  16. void TestDiagApprox::setUp()
  17. {
  18. }
  19. void TestDiagApprox::tearDown()
  20. {
  21. }
  22. void TestDiagApprox::TestDiagApproxComputation()
  23. {
  24. // size of the matrix
  25. uint rows = 2;
  26. uint cols = rows;
  27. // number of eigenvalues used
  28. uint maxiterations = 20;
  29. // use a fixed seed, its a test case
  30. #ifdef WIN32
  31. srand(0);
  32. #else
  33. srand48(0);
  34. #endif
  35. DiagonalMatrixApprox diagApprox ( true /*verbose*/ );
  36. Vector D ( rows, 0.0 );
  37. Matrix Tdiag ( rows, rows, 0.0 );
  38. for (uint j = 0 ; j < rows ; j++)
  39. {
  40. #ifdef WIN32
  41. Tdiag(j, j) = double( rand() ) / RAND_MAX;
  42. #else
  43. Tdiag(j, j) = drand48();
  44. #endif
  45. }
  46. diagApprox.approx ( Tdiag, D );
  47. cerr << Tdiag << endl;
  48. cerr << D << endl;
  49. double error = 0.0;
  50. for ( uint i = 0 ; i < rows; i++ )
  51. error += fabs(Tdiag(i,i) - D[i]);
  52. CPPUNIT_ASSERT_DOUBLES_EQUAL_NOT_NAN( 0.0, error, 0.1 );
  53. D.set(0.0);
  54. NICE::Matrix T(rows, cols, 0.0);
  55. // generate random symmetric matrix
  56. for (uint i = 0 ; i < rows ; i++)
  57. for (uint j = i ; j < cols ; j++)
  58. {
  59. #ifdef WIN32
  60. T(i, j) = double( rand() ) / RAND_MAX;
  61. #else
  62. T(i, j) = drand48();
  63. #endif
  64. T(j, i) = T(i, j);
  65. }
  66. // create a positive definite matrix
  67. T = T*T;
  68. diagApprox.approx ( T, D );
  69. cerr << T << endl;
  70. cerr << D << endl;
  71. }