TestDiagApprox.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. srand48(0);
  31. DiagonalMatrixApprox diagApprox ( true /*verbose*/ );
  32. Vector D ( rows, 0.0 );
  33. Matrix Tdiag ( rows, rows, 0.0 );
  34. for (uint j = 0 ; j < rows ; j++)
  35. {
  36. Tdiag(j, j) = drand48();
  37. }
  38. diagApprox.approx ( Tdiag, D );
  39. cerr << Tdiag << endl;
  40. cerr << D << endl;
  41. double error = 0.0;
  42. for ( uint i = 0 ; i < rows; i++ )
  43. error += fabs(Tdiag(i,i) - D[i]);
  44. CPPUNIT_ASSERT_DOUBLES_EQUAL_NOT_NAN( 0.0, error, 0.1 );
  45. D.set(0.0);
  46. NICE::Matrix T(rows, cols, 0.0);
  47. // generate random symmetric matrix
  48. for (uint i = 0 ; i < rows ; i++)
  49. for (uint j = i ; j < cols ; j++)
  50. {
  51. T(i, j) = drand48();
  52. T(j, i) = T(i, j);
  53. }
  54. // create a positive definite matrix
  55. T = T*T;
  56. diagApprox.approx ( T, D );
  57. cerr << T << endl;
  58. cerr << D << endl;
  59. }