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
19using std::cout;
20using std::endl;
21
22const double TOL = 1e-15;
23
24template<typename T>
25bool 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
41void 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
52BOOST_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(x: std::make_pair(x&: x, y&: y));
74 matrix_coord.append_element(i: x, j: y, t: 1);
75 matrix_dense(x, y) += 1;
76 }
77 matrix_coord.sort();
78
79 {
80 bool sorted = check_sortedness(matrix: matrix_coord);
81 bool identical = compare_distance(m1: matrix_coord, m2: matrix_dense, tolerance: 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(x: std::make_pair(x&: x, y&: 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: matrix_coord);
100 bool identical = compare_distance(m1: matrix_coord, m2: matrix_dense, tolerance: 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
111int 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

source code of boost/libs/numeric/ublas/test/test_coordinate_matrix_inplace_merge.cpp