1#undef BOOST_UBLAS_NO_EXCEPTIONS
2#include "common/testhelper.hpp"
3#include <boost/numeric/ublas/vector.hpp>
4#include <boost/numeric/ublas/matrix.hpp>
5#include <boost/numeric/ublas/assignment.hpp>
6#include <boost/numeric/ublas/io.hpp>
7#include <string>
8#include <complex>
9#include <iomanip>
10#include "utils.hpp"
11
12#ifdef BOOST_UBLAS_CPP_GE_2011
13
14using namespace boost::numeric::ublas;
15
16using std::cout;
17using std::endl;
18
19
20template <typename T>
21struct data_type {
22 typedef T value_type;
23};
24
25template <typename T>
26struct data_type< std::complex<T> > {
27 typedef typename std::complex<T>::value_type value_type;
28};
29
30
31template < class T >
32bool test_vector( std::string type_name)
33{
34 typedef typename data_type<T>::value_type component_type;
35
36 BOOST_UBLAS_DEBUG_TRACE( std::string("Testing for: ") + type_name );
37
38 bool pass = true;
39
40 {
41 typedef fixed_vector<T, 1> vec1;
42
43 vec1 v1( static_cast<component_type>(122.0) );
44
45 pass &= ( v1(0) == static_cast<component_type>(122.0) );
46
47 }
48
49 {
50 typedef fixed_vector<T, 3> vec3;
51
52 vec3 v1(static_cast<component_type>(0.0),
53 static_cast<component_type>(0.0),
54 static_cast<component_type>(0.0));
55
56 pass &=(sizeof( vec3 ) == v1.size() * sizeof( T ) ) ;
57
58 vector<T> v( 3, 0 ) ;
59
60 pass &= compare( v1, v );
61
62 v1 <<= static_cast<component_type>(10.0), 10, 33;
63 v <<= static_cast<component_type>(10.0), 10, 33;
64
65 pass &= compare( v1, v );
66
67
68 vec3 v2;
69
70 v2( 0 ) = static_cast<component_type>(10.0);
71 v2( 1 ) = 10;
72 v2( 2 ) = 33;
73 pass &= compare( v, v2 );
74
75 v2 += v;
76
77 pass &= compare( v2, 2*v );
78
79
80 v1 = 2*v1 + v - 6*v2;
81 pass &= compare( v1, (3-2*6)*v );
82
83
84 vec3 v3{ static_cast<component_type>(-90.0),
85 static_cast<component_type>(-90.0),
86 static_cast<component_type>(-297.0) };
87 pass &= compare( v3, v1 );
88
89 vec3 v4 = { static_cast<component_type>(-90.0),
90 static_cast<component_type>(-90.0),
91 static_cast<component_type>(-297.0) };
92 pass &= compare( v4, v1 );
93
94 vec3 v5( static_cast<component_type>(-90.0),
95 static_cast<component_type>(-90.0),
96 static_cast<component_type>(-297.0) );
97 pass &= compare( v5, v1 );
98
99 vec3 v6( static_cast<component_type>(5.0),
100 static_cast<component_type>(8.0),
101 static_cast<component_type>(9.0) );
102
103 matrix<T> M = outer_prod( v6, v6), L( 3, 3);
104
105 L <<= 25, 40, 45, 40, 64, 72, 45, 72, 81;
106
107 pass &= compare( M, L );
108
109 L <<= 1, 2, 3, 4, 5, 6, 7, 8, 9;
110 v6 <<= 4, 5, 6;
111 vec3 v7 ( static_cast<component_type>(32.0),
112 static_cast<component_type>(77.0),
113 static_cast<component_type>(122.0) );
114
115 pass &= compare( v7, prod(L, v6) );
116
117 vec3 v8(prod(L, v6));
118
119 pass &= compare( v7, v8 );
120 }
121
122
123 {
124 const std::size_t N = 33;
125 typedef fixed_vector<T, N> vec33;
126
127 vec33 v1;
128 vector<T> v( N );
129
130 for ( std::size_t i = 0; i != v1.size(); i++)
131 {
132 v1( i ) = static_cast<component_type>(3.14159*i);
133 v ( i ) = static_cast<component_type>(3.14159*i);
134 }
135
136 pass &= compare( v1, v );
137
138
139 auto ip = inner_prod( v, v);
140 auto ip1 = inner_prod( v1, v1);
141
142 pass &= ( ip == ip1 ) ;
143
144 T c = 0;
145 for (auto i = v1.begin(); i != v1.end(); i++)
146 {
147 *i = c;
148 c = c + 1;
149 }
150
151 c = 0;
152 for (auto i = v.begin(); i != v.end(); i++)
153 {
154 *i = c;
155 c = c + 1;
156 }
157
158 pass &= compare( v1, v );
159
160 // Check if bad index indeed works
161 try {
162 T a;
163 a=v1( 100 );
164 BOOST_UBLAS_NOT_USED( a );
165
166 } catch ( bad_index &e) {
167 std::cout << " Caught (GOOD): " << e.what() << endl;
168 pass &= true;
169 }
170
171
172 }
173 return pass;
174}
175
176template < class T >
177bool test_matrix( std::string type_name)
178{
179 typedef typename data_type<T>::value_type component_type;
180
181 BOOST_UBLAS_DEBUG_TRACE( std::string("Testing for: ") + type_name );
182
183 bool pass = true;
184
185 typedef fixed_matrix<T, 3, 4> mat34;
186 typedef fixed_matrix<T, 4, 3> mat43;
187 typedef fixed_matrix<T, 3, 3> mat33;
188
189
190 {
191 typedef fixed_matrix<T, 1, 1> mat1;
192
193 mat1 m1( static_cast<component_type>(122.0) );
194
195 pass &= ( m1(0, 0) == static_cast<component_type>(122.0) );
196 }
197
198
199 {
200 mat34 m1( T(static_cast<component_type>(3.0)) );
201
202 pass &=(sizeof( mat34 ) == m1.size1()*m1.size2()*sizeof( T ) ) ;
203
204 matrix<T> m( 3, 4, static_cast<component_type>(3.0) ) ;
205
206 pass &= compare( m1, m );
207
208 cout << m1 << endl;
209 cout << m << endl;
210
211
212 m1 <<= 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12;
213 m <<= 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12;
214
215 pass &= compare( m1, m );
216
217 cout << m1 << endl;
218 cout << m << endl;
219
220 mat34 m2( static_cast<component_type>(0.0) );
221
222 T count = 1 ;
223 for ( std::size_t i = 0; i != m2.size1(); i++)
224 {
225 for (std::size_t j = 0; j!= m2.size2(); j++)
226 {
227 m2( i, j ) = count;
228 count = count + 1;
229 }
230
231 }
232 pass &= compare( m2, m );
233 cout << m2 << endl;
234
235 }
236 {
237 mat34 m1((T)1, (T)2, (T)3, (T)3, (T)3, (T)2, (T)5, (T)4, (T)2, (T)6, (T)5, (T)2);
238 mat43 m2((T)4, (T)5, (T)6, (T)3, (T)2, (T)2, (T)1, (T)4, (T)2, (T)6, (T)5, (T)2);
239
240 mat33 m3(prod(m1, m2));
241
242 matrix<T> m(3, 3);
243 m <<= 31,36,22,47,59,40,43,52,38;
244
245 pass &= compare(m ,m3);
246
247 mat33 m4;
248 m4 <<= (T)1, (T)2, (T)1, (T)2, (T)1, (T)3, (T)1, (T)2, (T) 5;
249 m3 = prod(m4, trans(m4));
250
251 m<<=6,7,10,7,14,19,10,19,30;
252
253 cout << m3 << endl;
254 pass &= compare(m ,m3);
255
256 m3 = 2 * m4 - 1 * m3;
257
258 cout << m3 << endl;
259
260 m <<= -4,-3,-8,-3,-12,-13,-8,-15,-20;
261
262 pass &= compare(m, m3);
263
264 m = m3;
265
266 m3 = trans(m);
267
268 pass &= compare(m3, trans(m));
269
270 // Check if bad index indeed works
271 try {
272 T a;
273 a=m1( 100, 100 );
274 BOOST_UBLAS_NOT_USED( a );
275
276 } catch ( bad_index &e) {
277 std::cout << " Caught (GOOD): " << e.what() << endl;
278 pass &= true;
279 }
280
281 }
282
283 return pass;
284
285}
286
287BOOST_UBLAS_TEST_DEF (test_fixed) {
288
289 BOOST_UBLAS_DEBUG_TRACE( "Starting fixed container tests" );
290
291 BOOST_UBLAS_TEST_CHECK( test_vector< double >( "double") );
292 BOOST_UBLAS_TEST_CHECK( test_vector< float >( "float") );
293 BOOST_UBLAS_TEST_CHECK( test_vector< int >( "int") );
294
295 BOOST_UBLAS_TEST_CHECK( test_vector< std::complex<double> >( "std::complex<double>") );
296 BOOST_UBLAS_TEST_CHECK( test_vector< std::complex<float> >( "std::complex<float>") );
297 BOOST_UBLAS_TEST_CHECK( test_vector< std::complex<int> >( "std::complex<int>") );
298
299 BOOST_UBLAS_TEST_CHECK( test_matrix< double >( "double") );
300 BOOST_UBLAS_TEST_CHECK( test_matrix< float >( "float") );
301 BOOST_UBLAS_TEST_CHECK( test_matrix< int >( "int") );
302
303 BOOST_UBLAS_TEST_CHECK( test_matrix< std::complex<double> >( "std::complex<double>") );
304 BOOST_UBLAS_TEST_CHECK( test_matrix< std::complex<float> >( "std::complex<float>") );
305 BOOST_UBLAS_TEST_CHECK( test_matrix< std::complex<int> >( "std::complex<int>") );
306}
307
308
309int main () {
310
311 BOOST_UBLAS_TEST_BEGIN();
312
313 BOOST_UBLAS_TEST_DO( test_fixed );
314
315 BOOST_UBLAS_TEST_END();
316}
317
318#else
319
320int main () {
321
322 BOOST_UBLAS_TEST_BEGIN();
323 BOOST_UBLAS_TEST_END();
324}
325#endif // BOOST_UBLAS_CPP_GE_2011
326

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