TestSVD.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * NICE-Core - efficient algebra and computer vision methods
  3. * - libbasicvector - An core/vector/template for new NICE libraries
  4. * See file License for license information.
  5. */
  6. #ifdef NICE_USELIB_CPPUNIT
  7. #include "TestSVD.h"
  8. #include "core/basics/cppunitex.h"
  9. #include "core/vector/MatrixT.h"
  10. #include "core/vector/SVD.h"
  11. #include <string>
  12. #include <exception>
  13. using namespace std;
  14. CPPUNIT_TEST_SUITE_REGISTRATION( TestSVD );
  15. void TestSVD::setUp() {
  16. }
  17. void TestSVD::tearDown() {
  18. }
  19. #define sr(tr, tm, t0, t1, t2, t3) tm(tr,0)=t0; tm(tr,1)=t1; tm(tr,2)=t2; tm(tr,3)=t3;
  20. #define ASSERT_MATRIX_EQUAL(m1, m2) for(int i=0; i<m1.rows(); i++) { \
  21. for(int j=0; j<m1.cols(); j++) { \
  22. CPPUNIT_ASSERT_DOUBLES_EQUAL(m1(i,j),m2(i,j),0.0002); \
  23. } \
  24. }
  25. void TestSVD::testSVD() {
  26. NICE::MatrixT<double> matrix(4,4);
  27. sr(0, matrix, 1, 2, 3, 4);
  28. sr(1, matrix, 4, 5, 6, 7);
  29. sr(2, matrix, 2, 3, 4, 5);
  30. sr(3, matrix, 2, 9, 8, 1);
  31. NICE::MatrixT<double> exu(4,4,0);
  32. sr(0, exu, -0.2852, -0.2948, -0.7390, -0.5345);
  33. sr(1, exu, -0.6079, -0.4409, 0.6038, -0.2673);
  34. sr(2, exu, -0.3927, -0.3435, -0.2914, 0.8018);
  35. sr(3, exu, -0.6284, 0.7750, -0.0667, -0.0000);
  36. NICE::MatrixT<double> exs(4,4,0);
  37. exs(0,0) = 17.8539;
  38. exs(1,1) = 6.3372;
  39. exs(2,2) = 1.0374;
  40. NICE::MatrixT<double> exv(4,4,0);
  41. sr(0, exv, -0.2666, -0.1866, 0.9256, -0.1913);
  42. sr(1, exv, -0.5849, 0.4971, 0.0646, 0.6376);
  43. sr(2, exv, -0.6218, 0.2046, -0.2823, -0.7013);
  44. sr(3, exv, -0.4474, -0.8218, -0.2436, 0.2550);
  45. NICE::MatrixT<double> fail(4,4,999.999);
  46. #ifdef NICE_USELIB_LINAL
  47. NICE::SVD<double> test_svd(matrix);
  48. ASSERT_MATRIX_EQUAL(exu, test_svd.getU());
  49. ASSERT_MATRIX_EQUAL(exs, test_svd.getS());
  50. ASSERT_MATRIX_EQUAL(exv, test_svd.getV());
  51. #else
  52. ASSERT_MATRIX_EQUAL(exu, fail);
  53. #endif
  54. }
  55. #endif