blob: ae019031148898ce4864b2be282f50e93fe0572f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
// Copyright (c) 2011 David Bellot
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_UBLAS_NO_ELEMENT_PROXIES
# define BOOST_UBLAS_NO_ELEMENT_PROXIES
#endif
#include <boost/numeric/ublas/assignment.hpp>
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/vector_sparse.hpp>
#include <boost/numeric/ublas/vector_expression.hpp>
#include <boost/numeric/ublas/io.hpp>
#include "libs/numeric/ublas/test/utils.hpp"
const double TOL = 1e-15;
template <class AE>
typename AE::value_type mean_square(const boost::numeric::ublas::vector_expression<AE> &me) {
typename AE::value_type s(0);
typename AE::size_type i;
for (i=0; i!= me().size(); i++) {
s += boost::numeric::ublas::scalar_traits<typename AE::value_type>::type_abs(me()(i));
}
return s/me().size();
}
template<typename T>
bool check_sortedness(const boost::numeric::ublas::coordinate_vector<T>& vector) {
bool result = true;
typedef boost::numeric::ublas::coordinate_vector<T> vector_type;
typename vector_type::index_array_type idx = vector.index_data();
typename vector_type::size_type size = vector.filled();
for (typename vector_type::size_type i = 0; i + 1 < size && result; ++ i) {
result &= (idx[i] < idx[i + 1]);
}
return result;
}
void print_entries(size_t size,
const std::vector<size_t>& entries)
{
std::cerr << "Error entries - Size:" << size << ". Entries: ";
for (size_t i = 0; i < entries.size(); ++ i) {
std::cerr << entries[i] << "; ";
}
std::cerr << "\n";
}
BOOST_UBLAS_TEST_DEF( test_coordinate_vector_inplace_merge_random )
{
const size_t max_repeats = 100;
const size_t max_size = 100;
const size_t dim_var = 10;
const size_t nr_entries = 10;
for (size_t repeats = 1; repeats < max_repeats; ++repeats ) {
for (size_t size = 1; size < max_size; size += 5) {
size_t size_vec = size + rand() % dim_var;
boost::numeric::ublas::coordinate_vector<double> vector_coord(size_vec);
boost::numeric::ublas::vector<double> vector_dense(size_vec, 0);
vector_coord.sort();
std::vector<size_t> entries;
for (size_t entry = 0; entry < nr_entries; ++ entry) {
int x = rand() % size_vec;
entries.push_back(x);
vector_coord.append_element(x, 1);
vector_dense(x) += 1;
}
vector_coord.sort();
{
bool sorted = check_sortedness(vector_coord);
bool identical = mean_square(vector_coord - vector_dense) < TOL;
if (!(sorted && identical)) {
print_entries(size_vec, entries);
}
BOOST_UBLAS_TEST_CHECK( check_sortedness(vector_coord) );
BOOST_UBLAS_TEST_CHECK( mean_square(vector_coord - vector_dense) < TOL);
}
for (size_t entry = 0; entry < nr_entries; ++ entry) {
int x = rand() % size_vec;
entries.push_back(x);
vector_coord(x) += 1;
vector_dense(x) += 1;
vector_coord.sort();
}
{
bool sorted = check_sortedness(vector_coord);
bool identical = mean_square(vector_coord - vector_dense) < TOL;
if (!(sorted && identical)) {
print_entries(size_vec, entries);
}
BOOST_UBLAS_TEST_CHECK( sorted );
BOOST_UBLAS_TEST_CHECK( identical );
}
}
}
}
int main()
{
BOOST_UBLAS_TEST_BEGIN();
BOOST_UBLAS_TEST_DO( test_coordinate_vector_inplace_merge_random );
BOOST_UBLAS_TEST_END();
return EXIT_SUCCESS;;
}
|