test_coordinate_matrix_inplace_merge.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright (c) 2011 David Bellot
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_UBLAS_NO_ELEMENT_PROXIES
  7. # define BOOST_UBLAS_NO_ELEMENT_PROXIES
  8. #endif
  9. #include <boost/numeric/ublas/assignment.hpp>
  10. #include <boost/numeric/ublas/matrix.hpp>
  11. #include <boost/numeric/ublas/matrix_sparse.hpp>
  12. #include <boost/numeric/ublas/matrix_expression.hpp>
  13. #include <boost/numeric/ublas/io.hpp>
  14. #include "common/testhelper.hpp"
  15. #include "utils.hpp"
  16. using std::cout;
  17. using std::endl;
  18. const double TOL = 1e-15;
  19. template<typename T>
  20. bool check_sortedness(const boost::numeric::ublas::coordinate_matrix<T>& matrix) {
  21. bool result = true;
  22. typedef boost::numeric::ublas::coordinate_matrix<T> matrix_type;
  23. typename matrix_type::index_array_type i1 = matrix.index1_data();
  24. typename matrix_type::index_array_type i2 = matrix.index2_data();
  25. typename matrix_type::array_size_type size = matrix.filled();
  26. for (typename matrix_type::array_size_type i = 0; i + 1 < size && result; ++ i) {
  27. result &= ( (i1[i] < i1[i + 1]) ||
  28. ((i1[i] == i1[i]) &&
  29. (i2[i] < i2[i + 1])) );
  30. }
  31. return result;
  32. }
  33. void print_entries(size_t size_x, size_t size_y,
  34. const std::vector<std::pair<size_t, size_t> >& entries)
  35. {
  36. std::cerr << "Error - Size:" << size_x << " x " << size_y << ". Entries: ";
  37. for (size_t i = 0; i < entries.size(); ++ i) {
  38. std::cerr << entries[i].first << ", " << entries[i].second << "; ";
  39. }
  40. std::cerr << "\n";
  41. }
  42. BOOST_UBLAS_TEST_DEF( test_coordinate_matrix_inplace_merge_random )
  43. {
  44. const size_t max_repeats = 100;
  45. const size_t max_size = 100;
  46. const size_t dim_var = 10;
  47. const size_t nr_entries = 10;
  48. for (size_t repeats = 1; repeats < max_repeats; ++repeats ) {
  49. for (size_t size = 1; size < max_size; size += 5) {
  50. size_t size_x = size + rand() % dim_var;
  51. size_t size_y = size + rand() % dim_var;
  52. boost::numeric::ublas::coordinate_matrix<double> matrix_coord(size_x, size_y);
  53. boost::numeric::ublas::matrix<double> matrix_dense(size_x, size_y, 0);
  54. matrix_coord.sort();
  55. std::vector<std::pair<size_t, size_t> > entries;
  56. for (size_t entry = 0; entry < nr_entries; ++ entry) {
  57. int x = rand() % size_x;
  58. int y = rand() % size_y;
  59. entries.push_back(std::make_pair(x, y));
  60. matrix_coord.append_element(x, y, 1);
  61. matrix_dense(x, y) += 1;
  62. }
  63. matrix_coord.sort();
  64. {
  65. bool sorted = check_sortedness(matrix_coord);
  66. bool identical = compare_distance(matrix_coord, matrix_dense, TOL);
  67. if (!(sorted && identical)) {
  68. print_entries(size_x, size_y, entries);
  69. }
  70. BOOST_UBLAS_TEST_CHECK( check_sortedness(matrix_coord) );
  71. BOOST_UBLAS_TEST_CHECK( compare_distance(matrix_coord, matrix_dense, TOL) );
  72. }
  73. for (size_t entry = 0; entry < nr_entries; ++ entry) {
  74. int x = rand() % size_x;
  75. int y = rand() % size_y;
  76. entries.push_back(std::make_pair(x, y));
  77. matrix_coord(x, y) += 1;
  78. matrix_dense(x, y) += 1;
  79. matrix_coord.sort();
  80. }
  81. {
  82. bool sorted = check_sortedness(matrix_coord);
  83. bool identical = compare_distance(matrix_coord, matrix_dense, TOL);
  84. if (!(sorted && identical)) {
  85. print_entries(size_x, size_y, entries);
  86. }
  87. BOOST_UBLAS_TEST_CHECK( sorted );
  88. BOOST_UBLAS_TEST_CHECK( identical );
  89. }
  90. }
  91. }
  92. }
  93. int main()
  94. {
  95. BOOST_UBLAS_TEST_BEGIN();
  96. BOOST_UBLAS_TEST_DO( test_coordinate_matrix_inplace_merge_random );
  97. BOOST_UBLAS_TEST_END();
  98. }