test_lu.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2008 Gunter Winkler <guwi17@gmx.de>
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // switch automatic singular check off
  6. #define BOOST_UBLAS_TYPE_CHECK 0
  7. #include <boost/numeric/ublas/io.hpp>
  8. #include <boost/numeric/ublas/lu.hpp>
  9. #include <boost/cstdlib.hpp>
  10. #include "common/testhelper.hpp"
  11. #include <iostream>
  12. #include <sstream>
  13. using namespace boost::numeric::ublas;
  14. using std::string;
  15. static const string matrix_IN = "[3,3]((1,2,2),(2,3,3),(3,4,6))\0";
  16. static const string matrix_LU = "[3,3]((3,4,6),(3.33333343e-01,6.66666627e-01,0),(6.66666687e-01,4.99999911e-01,-1))\0";
  17. static const string matrix_INV= "[3,3]((-3,2,-7.94728621e-08),(1.50000012,0,-5.00000060e-01),(4.99999911e-01,-1,5.00000060e-01))\0";
  18. static const string matrix_PM = "[3](2,2,2)";
  19. int main () {
  20. typedef float TYPE;
  21. typedef matrix<TYPE> MATRIX;
  22. MATRIX A;
  23. MATRIX LU;
  24. MATRIX INV;
  25. {
  26. std::istringstream is(matrix_IN);
  27. is >> A;
  28. }
  29. {
  30. std::istringstream is(matrix_LU);
  31. is >> LU;
  32. }
  33. {
  34. std::istringstream is(matrix_INV);
  35. is >> INV;
  36. }
  37. permutation_matrix<>::vector_type temp;
  38. {
  39. std::istringstream is(matrix_PM);
  40. is >> temp;
  41. }
  42. permutation_matrix<> PM(temp);
  43. permutation_matrix<> pm(3);
  44. std::size_t result = lu_factorize<MATRIX, permutation_matrix<> >(A, pm);
  45. assertTrue("factorization completed: ", 0 == result);
  46. assertTrue("LU factors are correct: ", compare(A, LU));
  47. assertTrue("permutation is correct: ", compare(pm, PM));
  48. MATRIX B = identity_matrix<TYPE>(A.size2());
  49. lu_substitute(A, pm, B);
  50. assertTrue("inverse is correct: ", compare(B, INV));
  51. return (getResults().second > 0) ? boost::exit_failure : boost::exit_success;
  52. }