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
7 #ifndef BOOST_UBLAS_NO_ELEMENT_PROXIES
8 # define BOOST_UBLAS_NO_ELEMENT_PROXIES
9 #endif
10
11 #include <boost/numeric/ublas/assignment.hpp>
12 #include <boost/numeric/ublas/matrix.hpp>
13 #include <boost/numeric/ublas/matrix_sparse.hpp>
14 #include <boost/numeric/ublas/matrix_expression.hpp>
15 #include <boost/numeric/ublas/io.hpp>
16 #include "common/testhelper.hpp"
17 #include "utils.hpp"
18
19 using std::cout;
20 using std::endl;
21
22 const double TOL = 1e-15;
23
24 template<typename T>
check_sortedness(const boost::numeric::ublas::coordinate_matrix<T> & matrix)25 bool check_sortedness(const boost::numeric::ublas::coordinate_matrix<T>& matrix) {
26 bool result = true;
27 typedef boost::numeric::ublas::coordinate_matrix<T> matrix_type;
28 typename matrix_type::index_array_type i1 = matrix.index1_data();
29 typename matrix_type::index_array_type i2 = matrix.index2_data();
30 typename matrix_type::array_size_type size = matrix.filled();
31
32 for (typename matrix_type::array_size_type i = 0; i + 1 < size && result; ++ i) {
33 result &= ( (i1[i] < i1[i + 1]) ||
34 ((i1[i] == i1[i]) &&
35 (i2[i] < i2[i + 1])) );
36
37 }
38 return result;
39 }
40
print_entries(size_t size_x,size_t size_y,const std::vector<std::pair<size_t,size_t>> & entries)41 void print_entries(size_t size_x, size_t size_y,
42 const std::vector<std::pair<size_t, size_t> >& entries)
43 {
44 std::cerr << "Error - Size:" << size_x << " x " << size_y << ". Entries: ";
45 for (size_t i = 0; i < entries.size(); ++ i) {
46 std::cerr << entries[i].first << ", " << entries[i].second << "; ";
47 }
48 std::cerr << "\n";
49 }
50
51
BOOST_UBLAS_TEST_DEF(test_coordinate_matrix_inplace_merge_random)52 BOOST_UBLAS_TEST_DEF( test_coordinate_matrix_inplace_merge_random )
53 {
54 const size_t max_repeats = 100;
55 const size_t max_size = 100;
56 const size_t dim_var = 10;
57 const size_t nr_entries = 10;
58
59 for (size_t repeats = 1; repeats < max_repeats; ++repeats ) {
60 for (size_t size = 1; size < max_size; size += 5) {
61 size_t size_x = size + rand() % dim_var;
62 size_t size_y = size + rand() % dim_var;
63
64 boost::numeric::ublas::coordinate_matrix<double> matrix_coord(size_x, size_y);
65 boost::numeric::ublas::matrix<double> matrix_dense(size_x, size_y, 0);
66
67 matrix_coord.sort();
68
69 std::vector<std::pair<size_t, size_t> > entries;
70 for (size_t entry = 0; entry < nr_entries; ++ entry) {
71 int x = rand() % size_x;
72 int y = rand() % size_y;
73 entries.push_back(std::make_pair(x, y));
74 matrix_coord.append_element(x, y, 1);
75 matrix_dense(x, y) += 1;
76 }
77 matrix_coord.sort();
78
79 {
80 bool sorted = check_sortedness(matrix_coord);
81 bool identical = compare_distance(matrix_coord, matrix_dense, TOL);
82 if (!(sorted && identical)) {
83 print_entries(size_x, size_y, entries);
84 }
85 BOOST_UBLAS_TEST_CHECK( check_sortedness(matrix_coord) );
86 BOOST_UBLAS_TEST_CHECK( compare_distance(matrix_coord, matrix_dense, TOL) );
87 }
88
89 for (size_t entry = 0; entry < nr_entries; ++ entry) {
90 int x = rand() % size_x;
91 int y = rand() % size_y;
92 entries.push_back(std::make_pair(x, y));
93 matrix_coord(x, y) += 1;
94 matrix_dense(x, y) += 1;
95 matrix_coord.sort();
96 }
97
98 {
99 bool sorted = check_sortedness(matrix_coord);
100 bool identical = compare_distance(matrix_coord, matrix_dense, TOL);
101 if (!(sorted && identical)) {
102 print_entries(size_x, size_y, entries);
103 }
104 BOOST_UBLAS_TEST_CHECK( sorted );
105 BOOST_UBLAS_TEST_CHECK( identical );
106 }
107 }
108 }
109 }
110
main()111 int main()
112 {
113 BOOST_UBLAS_TEST_BEGIN();
114
115 BOOST_UBLAS_TEST_DO( test_coordinate_matrix_inplace_merge_random );
116
117 BOOST_UBLAS_TEST_END();
118 }
119