TestLUDecomposition.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * @file TestLUDecomposition.cpp
  3. * @brief TestLUDecomposition
  4. * @author Alexander Freytag
  5. * @date 22-10-2012
  6. */
  7. #include <string>
  8. #include <vector>
  9. #include <core/vector/MatrixT.h>
  10. #include "core/algebra/LUDecomposition.h"
  11. #include "TestLUDecomposition.h"
  12. using namespace std;
  13. using namespace NICE;
  14. CPPUNIT_TEST_SUITE_REGISTRATION(TestLUDecomposition);
  15. void TestLUDecomposition::setUp()
  16. {
  17. }
  18. void TestLUDecomposition::tearDown()
  19. {
  20. }
  21. void TestLUDecomposition::testLUDecomposition()
  22. {
  23. // verbose flag for additional output for each iteration
  24. bool verbose (false);
  25. NICE::Matrix m (4, 4, 0.0);
  26. for (int j = 0; j < 4; j++)
  27. for (int i = 0; i < 4; i++)
  28. m(i,j) = i*4+(j+1);
  29. //ensure the non-singularity :)
  30. m.addIdentity(1.0);
  31. LUDecomposition luDecomp;
  32. NICE::Matrix L;
  33. NICE::Matrix U;
  34. //perform the actual decomposition - with pivotization if needed
  35. luDecomp.decompose(m, L, U);
  36. NICE::Matrix mult;
  37. mult.multiply(L,U);
  38. double diff(0.0);
  39. for (int j = 0; j < 4; j++)
  40. for (int i = 0; i < 4; i++)
  41. diff += pow( mult(i,j) - m(i,j) , 2);
  42. diff = sqrt(diff);
  43. if (verbose)
  44. {
  45. std::cerr << "LUDecomposition test: " << std::endl;
  46. std::cerr << "m: " << m << std::endl;
  47. std::cerr << "L: " << L << std::endl;
  48. std::cerr << "U: " << U << std::endl;
  49. std::cerr << "LU: " << mult << std::endl;
  50. std::cerr << "LUDecomposition test done " << std::endl;
  51. }
  52. double tol(10e-6);
  53. if (verbose)
  54. std::cerr << "tol: " << tol << " diff: " << diff << std::endl;
  55. CPPUNIT_ASSERT( diff < tol);
  56. }