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 #include <cmath>
8 #include <boost/numeric/ublas/traits/const_iterator_type.hpp>
9 #include <boost/numeric/ublas/traits/iterator_type.hpp>
10 #include <boost/numeric/ublas/traits/c_array.hpp>
11 #include <boost/numeric/ublas/fwd.hpp>
12 #include <boost/numeric/ublas/matrix.hpp>
13 #include <boost/numeric/ublas/matrix_expression.hpp>
14 #include <boost/numeric/ublas/operation/begin.hpp>
15 #include <boost/numeric/ublas/operation/end.hpp>
16 #include <boost/numeric/ublas/tags.hpp>
17 #include <boost/numeric/ublas/vector.hpp>
18 #include <boost/numeric/ublas/vector_expression.hpp>
19 #include <iostream>
20 #include "utils.hpp"
21 
22 
23 static const double TOL(1.0e-5); ///< Used for comparing two real numbers.
24 
25 #ifdef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
26 #error "sorry this feature is not supported by your compiler"
27 #endif
28 
BOOST_UBLAS_TEST_DEF(test_vector_iteration)29 BOOST_UBLAS_TEST_DEF( test_vector_iteration )
30 {
31     BOOST_UBLAS_DEBUG_TRACE( "TEST Vector Iteration" );
32 
33     typedef double value_type;
34     typedef boost::numeric::ublas::vector<value_type> vector_type;
35 
36     vector_type v(5);
37 
38     v(0) = 0.555950;
39     v(1) = 0.108929;
40     v(2) = 0.948014;
41     v(3) = 0.023787;
42     v(4) = 1.023787;
43 
44 
45     vector_type::size_type ix = 0;
46     for (
47             boost::numeric::ublas::iterator_type<vector_type>::type it = boost::numeric::ublas::begin<vector_type>(v);
48             it != boost::numeric::ublas::end<vector_type>(v);
49             ++it
50     ) {
51         BOOST_UBLAS_DEBUG_TRACE( "*it = " << *it << " ==> " << v(ix) );
52         BOOST_UBLAS_TEST_CHECK( std::abs(*it - v(ix)) <= TOL );
53         ++ix;
54     }
55 }
56 
57 
BOOST_UBLAS_TEST_DEF(test_vector_const_iteration)58 BOOST_UBLAS_TEST_DEF( test_vector_const_iteration )
59 {
60     BOOST_UBLAS_DEBUG_TRACE( "TEST Vector Const Iteration" );
61 
62     typedef double value_type;
63     typedef boost::numeric::ublas::vector<value_type> vector_type;
64 
65     vector_type v(5);
66 
67     v(0) = 0.555950;
68     v(1) = 0.108929;
69     v(2) = 0.948014;
70     v(3) = 0.023787;
71     v(4) = 1.023787;
72 
73 
74     vector_type::size_type ix = 0;
75     for (
76             boost::numeric::ublas::const_iterator_type<vector_type>::type it = boost::numeric::ublas::begin<vector_type>(v);
77             it != boost::numeric::ublas::end<vector_type>(v);
78             ++it
79     ) {
80         BOOST_UBLAS_DEBUG_TRACE( "*it = " << *it << " ==> " << v(ix) );
81         BOOST_UBLAS_TEST_CHECK( std::abs(*it - v(ix)) <= TOL );
82         ++ix;
83     }
84 }
85 
86 
BOOST_UBLAS_TEST_DEF(test_row_major_matrix_iteration)87 BOOST_UBLAS_TEST_DEF( test_row_major_matrix_iteration )
88 {
89     BOOST_UBLAS_DEBUG_TRACE( "TEST Row-major Matrix Iteration" );
90 
91     typedef double value_type;
92     typedef boost::numeric::ublas::matrix<value_type, boost::numeric::ublas::row_major> matrix_type;
93     typedef boost::numeric::ublas::iterator_type<matrix_type, boost::numeric::ublas::tag::major>::type outer_iterator_type;
94     typedef boost::numeric::ublas::iterator_type<matrix_type, boost::numeric::ublas::tag::minor>::type inner_iterator_type;
95 
96     matrix_type A(5,4);
97 
98     A(0,0) = 0.555950; A(0,1) = 0.274690; A(0,2) = 0.540605; A(0,3) = 0.798938;
99     A(1,0) = 0.108929; A(1,1) = 0.830123; A(1,2) = 0.891726; A(1,3) = 0.895283;
100     A(2,0) = 0.948014; A(2,1) = 0.973234; A(2,2) = 0.216504; A(2,3) = 0.883152;
101     A(3,0) = 0.023787; A(3,1) = 0.675382; A(3,2) = 0.231751; A(3,3) = 0.450332;
102     A(4,0) = 1.023787; A(4,1) = 1.675382; A(4,2) = 1.231751; A(4,3) = 1.450332;
103 
104 
105     matrix_type::size_type row(0);
106     for (
107             outer_iterator_type outer_it = boost::numeric::ublas::begin<boost::numeric::ublas::tag::major>(A);
108             outer_it != boost::numeric::ublas::end<boost::numeric::ublas::tag::major>(A);
109             ++outer_it
110     ) {
111         matrix_type::size_type col(0);
112 
113         for (
114                 inner_iterator_type inner_it = boost::numeric::ublas::begin(outer_it);
115                 inner_it != boost::numeric::ublas::end(outer_it);
116                 ++inner_it
117         ) {
118             BOOST_UBLAS_DEBUG_TRACE( "*it = " << *inner_it << " ==> " << A(row,col) );
119             BOOST_UBLAS_TEST_CHECK( std::abs(*inner_it - A(row,col)) <= TOL );
120 
121             ++col;
122         }
123 
124         ++row;
125     }
126 }
127 
128 
BOOST_UBLAS_TEST_DEF(test_col_major_matrix_iteration)129 BOOST_UBLAS_TEST_DEF( test_col_major_matrix_iteration )
130 {
131     BOOST_UBLAS_DEBUG_TRACE( "TEST Column-major Matrix Iteration" );
132 
133     typedef double value_type;
134     typedef boost::numeric::ublas::matrix<value_type, boost::numeric::ublas::column_major> matrix_type;
135     typedef boost::numeric::ublas::iterator_type<matrix_type, boost::numeric::ublas::tag::major>::type outer_iterator_type;
136     typedef boost::numeric::ublas::iterator_type<matrix_type, boost::numeric::ublas::tag::minor>::type inner_iterator_type;
137 
138     matrix_type A(5,4);
139 
140     A(0,0) = 0.555950; A(0,1) = 0.274690; A(0,2) = 0.540605; A(0,3) = 0.798938;
141     A(1,0) = 0.108929; A(1,1) = 0.830123; A(1,2) = 0.891726; A(1,3) = 0.895283;
142     A(2,0) = 0.948014; A(2,1) = 0.973234; A(2,2) = 0.216504; A(2,3) = 0.883152;
143     A(3,0) = 0.023787; A(3,1) = 0.675382; A(3,2) = 0.231751; A(3,3) = 0.450332;
144     A(4,0) = 1.023787; A(4,1) = 1.675382; A(4,2) = 1.231751; A(4,3) = 1.450332;
145 
146 
147     matrix_type::size_type col(0);
148     for (
149             outer_iterator_type outer_it = boost::numeric::ublas::begin<boost::numeric::ublas::tag::major>(A);
150             outer_it != boost::numeric::ublas::end<boost::numeric::ublas::tag::major>(A);
151             ++outer_it
152     ) {
153         matrix_type::size_type row(0);
154 
155         for (
156                 inner_iterator_type inner_it = boost::numeric::ublas::begin(outer_it);
157                 inner_it != boost::numeric::ublas::end(outer_it);
158                 ++inner_it
159         ) {
160             BOOST_UBLAS_DEBUG_TRACE( "*it = " << *inner_it << " ==> " << A(row,col) );
161             BOOST_UBLAS_TEST_CHECK( std::abs(*inner_it - A(row,col)) <= TOL );
162 
163             ++row;
164         }
165 
166         ++col;
167     }
168 }
169 
170 
main()171 int main()
172 {
173     BOOST_UBLAS_TEST_BEGIN();
174 
175     BOOST_UBLAS_TEST_DO( test_vector_iteration );
176     BOOST_UBLAS_TEST_DO( test_vector_const_iteration );
177     BOOST_UBLAS_TEST_DO( test_row_major_matrix_iteration );
178     BOOST_UBLAS_TEST_DO( test_col_major_matrix_iteration );
179 
180     BOOST_UBLAS_TEST_END();
181 }
182