test_triangular.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <cmath>
  4. #include <boost/numeric/ublas/vector.hpp>
  5. #include <boost/numeric/ublas/matrix.hpp>
  6. #include <boost/numeric/ublas/matrix_sparse.hpp>
  7. #include <boost/numeric/ublas/triangular.hpp>
  8. #include <boost/numeric/ublas/io.hpp>
  9. #include <boost/timer/timer.hpp>
  10. namespace ublas = boost::numeric::ublas;
  11. template<class mat, class vec>
  12. double diff(const mat& A, const vec& x, const vec& b) {
  13. vec temp(prod(A, x) - b);
  14. double result = 0;
  15. for (typename vec::size_type i=0; i<temp.size(); ++i) {
  16. result += temp(i)*temp(i);
  17. }
  18. return std::sqrt(result);
  19. }
  20. template<class mat, class vec>
  21. double diff(const vec& x, const mat& A, const vec& b) {
  22. return diff(trans(A), x, b);
  23. }
  24. namespace ublas = boost::numeric::ublas;
  25. int main() {
  26. const int n=7000;
  27. #if 1
  28. ublas::compressed_matrix<double, ublas::row_major> mat_row_upp(n, n);
  29. ublas::compressed_matrix<double, ublas::column_major> mat_col_upp(n, n);
  30. ublas::compressed_matrix<double, ublas::row_major> mat_row_low(n, n);
  31. ublas::compressed_matrix<double, ublas::column_major> mat_col_low(n, n);
  32. #else
  33. ublas::matrix<double, ublas::row_major> mat_row_upp(n, n, 0);
  34. ublas::matrix<double, ublas::column_major> mat_col_upp(n, n, 0);
  35. ublas::matrix<double, ublas::row_major> mat_row_low(n, n, 0);
  36. ublas::matrix<double, ublas::column_major> mat_col_low(n, n, 0);
  37. #endif
  38. ublas::vector<double> b(n, 1);
  39. std::cerr << "Constructing..." << std::endl;
  40. for (int i=0; i<n; ++i) {
  41. b(i) = std::rand() % 10;
  42. double main = -10 + std::rand() % 20 ;
  43. if (main == 0) main+=1;
  44. double side = -10 + std::rand() % 20 ;
  45. if (i-1>=0) {
  46. mat_row_low(i, i-1) = side;
  47. }
  48. mat_row_low(i, i) = main;
  49. mat_col_low(i, i) = main;
  50. if (i+1<n) {
  51. mat_col_low(i+1, i) = side;
  52. }
  53. mat_row_upp(i, i) = main;
  54. if (i+1<n) {
  55. mat_row_upp(i, i+1) = side;
  56. }
  57. if (i-1>=0) {
  58. mat_col_upp(i-1, i) = side;
  59. }
  60. mat_col_upp(i, i) = main;
  61. }
  62. std::cerr << "Starting..." << std::endl;
  63. {
  64. boost::timer::auto_cpu_timer t(std::cerr, "col_low x: %t sec CPU, %w sec real\n");
  65. ublas::vector<double> x(b);
  66. ublas::inplace_solve(mat_col_low, x, ublas::lower_tag());
  67. std::cerr << "delta: " << diff(mat_col_low, x, b) << "\n";
  68. }
  69. {
  70. boost::timer::auto_cpu_timer t(std::cerr, "row_low x: %t sec CPU, %w sec real\n");
  71. ublas::vector<double> x(b);
  72. ublas::inplace_solve(mat_row_low, x, ublas::lower_tag());
  73. std::cerr << "delta: " << diff(mat_row_low, x, b) << "\n";
  74. }
  75. {
  76. boost::timer::auto_cpu_timer t(std::cerr, "col_upp x: %t sec CPU, %w sec real\n");
  77. ublas::vector<double> x(b);
  78. ublas::inplace_solve(mat_col_upp, x, ublas::upper_tag());
  79. std::cerr << "delta: " << diff(mat_col_upp, x, b) << "\n";
  80. }
  81. {
  82. boost::timer::auto_cpu_timer t(std::cerr, "row_upp x: %t sec CPU, %w sec real\n");
  83. ublas::vector<double> x(b);
  84. ublas::inplace_solve(mat_row_upp, x, ublas::upper_tag());
  85. std::cerr << "delta: " << diff(mat_row_upp, x, b) << "\n";
  86. }
  87. {
  88. boost::timer::auto_cpu_timer t(std::cerr, "x col_low: %t sec CPU, %w sec real\n");
  89. ublas::vector<double> x(b);
  90. ublas::inplace_solve(x, mat_col_low, ublas::lower_tag());
  91. std::cerr << "delta: " << diff(x, mat_col_low, b) << "\n";
  92. }
  93. {
  94. boost::timer::auto_cpu_timer t(std::cerr, "x row_low: %t sec CPU, %w sec real\n");
  95. ublas::vector<double> x(b);
  96. ublas::inplace_solve(x, mat_row_low, ublas::lower_tag());
  97. std::cerr << "delta: " << diff(x, mat_row_low, b) << "\n";
  98. }
  99. {
  100. boost::timer::auto_cpu_timer t(std::cerr, "x col_upp: %t sec CPU, %w sec real\n");
  101. ublas::vector<double> x(b);
  102. ublas::inplace_solve(x, mat_col_upp, ublas::upper_tag());
  103. std::cerr << "delta: " << diff(x, mat_col_upp, b) << "\n";
  104. }
  105. {
  106. boost::timer::auto_cpu_timer t(std::cerr, "x row_upp: %t sec CPU, %w sec real\n");
  107. ublas::vector<double> x(b);
  108. ublas::inplace_solve(x, mat_row_upp, ublas::upper_tag());
  109. std::cerr << "delta: " << diff(x, mat_row_upp, b) << "\n";
  110. }
  111. }