summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/numeric/ublas/test/test_matrix_vector.cpp
blob: 5860287375ed3a06ef65a8efa6eca36bae535d11 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
//
//  Copyright (c) 2013 Joaquim Duran
//
//  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)
//

#include <boost/numeric/ublas/assignment.hpp>
#include <boost/numeric/ublas/matrix_sparse.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/matrix_vector.hpp>
#include "common/testhelper.hpp"
#include "utils.hpp"

using namespace boost::numeric::ublas;


template <class Vector, class StorageCategory>
void guardSparsePreserveResize( Vector &vec, typename Vector::size_type new_size, StorageCategory) // Because sparse matrices don't have preserve data implemented
{
    vec.resize( new_size );
}


template <class Vector>
void guardSparsePreserveResize( Vector &vec, typename Vector::size_type new_size, sparse_tag) // Because sparse matrices don't have preserve data implemented
{
    vec.resize( new_size, false );
}

template <class Matrix>
bool test_matrix_row_facade() {
    bool pass = true;

    typedef matrix_row_vector<Matrix> RowVector;

    { // Testing resize
    BOOST_UBLAS_DEBUG_TRACE( "test_matrix_row_facade: resize" );

    typename Matrix::size_type num_rows = 3;
    typename Matrix::size_type num_cols = 5;
		
    Matrix matrix(num_rows, num_cols);
    RowVector rows(matrix);
    pass &= (matrix.size1() == num_rows);
    pass &= (rows.size() == num_rows);
    pass &= (matrix.size2() == num_cols);
	
    typename Matrix::size_type new_num_rows = 6;
    guardSparsePreserveResize( rows, new_num_rows, typename Matrix::storage_category());
    //rows.resize(new_num_rows);

    pass &= (matrix.size1() == new_num_rows);
    pass &= (rows.size() == new_num_rows);
    pass &= (matrix.size2() == num_cols);
    }

    { // Testing operator()
    BOOST_UBLAS_DEBUG_TRACE( "test_matrix_row_facade: operator()" );

    Matrix A(3,3), RA(3,3);
    RowVector rows(A);

    RA <<=  1, 2, 3,
            4, 5, 6,
            7, 8, 9;

    for(typename Matrix::size_type i = 0; i < A.size1(); i++) {
        rows(i) = matrix_row<Matrix>(RA, i);
    }
    
    pass &= compare_distance(A, RA);
    }

    { // Testing operator[]
    BOOST_UBLAS_DEBUG_TRACE( "test_matrix_row_facade: operator[]" );

    Matrix A(3,3), RA(3,3);
    RowVector rows(A);

    RA <<=  1, 2, 3,
            4, 5, 6,
            7, 8, 9;
		
    for(typename Matrix::size_type i = 0; i < A.size1(); i++) {
        rows[i] = matrix_row<Matrix>(RA, i);
    }
    
    pass &= compare_distance(A, RA);
    }
    
    { // Testing operator[] const
    BOOST_UBLAS_DEBUG_TRACE( "test_matrix_row_facade: operator[] const" );

    Matrix RA(3,3);
    RowVector rows(RA);

    RA <<=  1, 2, 3,
            4, 5, 6,
            7, 8, 9;

    for(typename Matrix::size_type i = 0; i < RA.size1(); i++) {
      pass &= compare_distance(rows[i], matrix_row<Matrix>(RA, i));
    }
    }

    { // Testing const iterator
    BOOST_UBLAS_DEBUG_TRACE( "test_matrix_row_facade: const iterator" );

    Matrix RA(3,3);
    RowVector rows(RA);

    RA <<=  1, 2, 3,
            4, 5, 6,
            7, 8, 9;

    typename RowVector::size_type i = 0;
    for(typename RowVector::const_iterator iter = rows.begin();
	iter != rows.end();
	iter++) {
      pass &= compare_distance(*iter, matrix_row<Matrix>(RA, i++));
    }
    }

    { // Testing iterator
    BOOST_UBLAS_DEBUG_TRACE( "test_matrix_row_facade: iterator" );

    Matrix A(3,3), RA(3,3);
    RowVector rows(A);

    RA <<=  1, 2, 3,
            4, 5, 6,
            7, 8, 9;

    typename RowVector::size_type i = 0;
    for(typename RowVector::iterator iter = rows.begin();
	iter != rows.end();
	iter++) {
      *iter = matrix_row<Matrix>(RA, i++);
    }

    pass &= compare_distance(A, RA);
    }

    { // Testing reserse iterator
    BOOST_UBLAS_DEBUG_TRACE( "test_matrix_row_facade: reverse iterator" );

    Matrix A(3,3), RA(3,3);
    RowVector rows(A);

    RA <<=  1, 2, 3,
            4, 5, 6,
            7, 8, 9;

    typename RowVector::size_type i = rows.size();
    for(typename RowVector::reverse_iterator iter = rows.rbegin();
	iter != rows.rend();
	iter++) {
      *iter = matrix_row<Matrix>(RA, --i);
    }

    pass &= compare_distance(A, RA);
    }

    { // Testing const reverse iterator
    BOOST_UBLAS_DEBUG_TRACE( "test_matrix_row_facade: const reverse iterator" );

    Matrix RA(3,3);
    RowVector rows(RA);

    RA <<=  1, 2, 3,
            4, 5, 6,
            7, 8, 9;

    typename RowVector::size_type i = rows.size();
    for(typename RowVector::const_reverse_iterator iter = rows.rbegin();
	iter != rows.rend();
	iter++) {
      pass &= compare_distance(*iter, matrix_row<Matrix>(RA, --i));
    }
    }

    return pass;
}


template <class Matrix>
bool test_matrix_column_facade() {
    bool pass = true;

    typedef matrix_column_vector<Matrix> ColumnVector;

    { // Testing resize
    BOOST_UBLAS_DEBUG_TRACE( "test_matrix_column_facade: resize" );
    typename Matrix::size_type num_rows = 5;
    typename Matrix::size_type num_cols = 3;
    
    Matrix matrix(num_rows, num_cols);
    ColumnVector columns(matrix);
    pass &= (matrix.size2() == num_cols);
    pass &= (columns.size() == num_cols);
    pass &= (matrix.size1() == num_rows);
	
    typename Matrix::size_type new_num_cols = 6;
    guardSparsePreserveResize( columns, new_num_cols, typename Matrix::storage_category());
    //columns.resize(new_num_cols);
    pass &= (matrix.size2() == new_num_cols);
    pass &= (columns.size() == new_num_cols);
    pass &= (matrix.size1() == num_rows);
    }

    { // Testing operator ()
    BOOST_UBLAS_DEBUG_TRACE( "test_matrix_column_facade: operator()" );

    Matrix A(3,3), RA(3,3);
    ColumnVector columns(A);

    RA <<=  1, 2, 3,
            4, 5, 6,
            7, 8, 9;
		
    for(typename Matrix::size_type i = 0; i < A.size2(); i++) {
        columns(i) = matrix_column<Matrix>(RA, i);
    }
    
    pass &= compare_distance(A, RA);
    }

    { // Testing operator[]
    BOOST_UBLAS_DEBUG_TRACE( "test_matrix_column_facade: operator[]" );

    Matrix A(3,3), RA(3,3);
    ColumnVector columns(A);

    RA <<=  1, 2, 3,
            4, 5, 6,
            7, 8, 9;
		
    for(typename Matrix::size_type i = 0; i < A.size2(); i++) {
        columns[i] = matrix_column<Matrix>(RA, i);
    }
    
    pass &= compare_distance(A, RA);
    }
    
    { // Testing operator[] const
    BOOST_UBLAS_DEBUG_TRACE( "test_matrix_column_facade: operator[] const" );

    Matrix RA(3,3);
    ColumnVector columns(RA);

    RA <<=  1, 2, 3,
            4, 5, 6,
            7, 8, 9;

    for(typename Matrix::size_type i = 0; i < RA.size2(); i++) {
      pass &= compare_distance(columns[i], matrix_column<Matrix>(RA, i));
    }
    }

    { // Testing iterator
    BOOST_UBLAS_DEBUG_TRACE( "test_matrix_column_facade: iterator" );

     Matrix A(3,3), RA(3,3);
    ColumnVector columns(A);

    RA <<=  1, 2, 3,
            4, 5, 6,
            7, 8, 9;

    typename ColumnVector::size_type i = 0;
    for(typename ColumnVector::iterator iter = columns.begin();
	iter != columns.end();
	iter++) {
      *iter = matrix_column<Matrix>(RA, i++);
    }

    pass &= compare_distance(A, RA);
    }

    { // Testing const iterator
    BOOST_UBLAS_DEBUG_TRACE( "test_matrix_column_facade: const iterator" );

    Matrix RA(3,3);
    ColumnVector columns(RA);

    RA <<=  1, 2, 3,
            4, 5, 6,
            7, 8, 9;

    typename ColumnVector::size_type i = 0;
    for(typename ColumnVector::const_iterator iter = columns.begin();
	iter != columns.end();
	iter++) {
      pass &= compare_distance(*iter, matrix_column<Matrix>(RA, i++));
    }
    }

    { // Testing reserse iterator
    BOOST_UBLAS_DEBUG_TRACE( "test_matrix_column_facade: reverese iterator" );

    Matrix A(3,3), RA(3,3);
    ColumnVector columns(A);

    RA <<=  1, 2, 3,
            4, 5, 6,
            7, 8, 9;

    typename ColumnVector::size_type i = columns.size();
    for(typename ColumnVector::reverse_iterator iter = columns.rbegin();
	iter != columns.rend();
	iter++) {
      *iter = matrix_column<Matrix>(RA, --i);
    }

    pass &= compare_distance(A, RA);
    }

    { // Testing const reverse iterator
    BOOST_UBLAS_DEBUG_TRACE( "test_matrix_column_facade: const reverese iterator" );

    Matrix RA(3,3);
    ColumnVector columns(RA);

    RA <<=  1, 2, 3,
            4, 5, 6,
            7, 8, 9;

    typename ColumnVector::size_type i = columns.size();
    for(typename ColumnVector::const_reverse_iterator iter = columns.rbegin();
	iter != columns.rend();
	iter++) {
      pass &= compare_distance(*iter, matrix_column<Matrix>(RA, --i));
    }
    }

    return pass;
}


BOOST_UBLAS_TEST_DEF (test_matrix_row_facade) {

    BOOST_UBLAS_DEBUG_TRACE( "Starting matrix row vector facade" );

    BOOST_UBLAS_DEBUG_TRACE( "Testing matrix..." );
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<matrix<double> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<matrix<float> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<matrix<long> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<matrix<unsigned long> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<matrix<int> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<matrix<unsigned int> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<matrix<std::size_t> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<matrix<char> >());

    BOOST_UBLAS_DEBUG_TRACE( "Testing bounded_matrix..." );
    BOOST_UBLAS_TEST_CHECK((test_matrix_row_facade<bounded_matrix<double,7, 7> >()));
    BOOST_UBLAS_TEST_CHECK((test_matrix_row_facade<bounded_matrix<float,7, 7> >()));
    BOOST_UBLAS_TEST_CHECK((test_matrix_row_facade<bounded_matrix<long,7, 7> >()));
    BOOST_UBLAS_TEST_CHECK((test_matrix_row_facade<bounded_matrix<unsigned long,7, 7> >()));
    BOOST_UBLAS_TEST_CHECK((test_matrix_row_facade<bounded_matrix<int,7,7 > >()));
    BOOST_UBLAS_TEST_CHECK((test_matrix_row_facade<bounded_matrix<unsigned int,7, 7> >()));
    BOOST_UBLAS_TEST_CHECK((test_matrix_row_facade<bounded_matrix<char,7, 7> >()));
    BOOST_UBLAS_TEST_CHECK((test_matrix_row_facade<bounded_matrix<std::size_t,7, 7> >()));

    BOOST_UBLAS_DEBUG_TRACE( "Testing mapped_matrix..." );
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<mapped_matrix<double> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<mapped_matrix<float> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<mapped_matrix<long> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<mapped_matrix<unsigned long> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<mapped_matrix<int> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<mapped_matrix<unsigned int> >())
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<mapped_matrix<std::size_t> >())
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<mapped_matrix<char> >());

    BOOST_UBLAS_DEBUG_TRACE( "Testing compressed_matrix..." );
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<compressed_matrix<double> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<compressed_matrix<float> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<compressed_matrix<long> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<compressed_matrix<unsigned long> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<compressed_matrix<int> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<compressed_matrix<unsigned int> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<compressed_matrix<std::size_t> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<compressed_matrix<char> >());

    BOOST_UBLAS_DEBUG_TRACE( "Testing coordinate_matrix..." );
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<coordinate_matrix<double> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<coordinate_matrix<float> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<coordinate_matrix<long> >())
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<coordinate_matrix<unsigned long> >())
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<coordinate_matrix<int> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<coordinate_matrix<unsigned int> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<coordinate_matrix<std::size_t> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_row_facade<coordinate_matrix<char> >());
}


BOOST_UBLAS_TEST_DEF (test_matrix_column_facade) {

    BOOST_UBLAS_DEBUG_TRACE( "Starting matrix row column facade" );

    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<matrix<double> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<matrix<float> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<matrix<long> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<matrix<unsigned long> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<matrix<int> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<matrix<unsigned int> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<matrix<std::size_t> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<matrix<char> >());

    BOOST_UBLAS_TEST_CHECK((test_matrix_column_facade<bounded_matrix<double,7, 7> >()));
    BOOST_UBLAS_TEST_CHECK((test_matrix_column_facade<bounded_matrix<float,7, 7> >()));
    BOOST_UBLAS_TEST_CHECK((test_matrix_column_facade<bounded_matrix<long,7, 7> >()));
    BOOST_UBLAS_TEST_CHECK((test_matrix_column_facade<bounded_matrix<unsigned long,7, 7> >()));
    BOOST_UBLAS_TEST_CHECK((test_matrix_column_facade<bounded_matrix<int,7,7 > >()));
    BOOST_UBLAS_TEST_CHECK((test_matrix_column_facade<bounded_matrix<unsigned int,7, 7> >()));
    BOOST_UBLAS_TEST_CHECK((test_matrix_column_facade<bounded_matrix<char,7, 7> >()));
    BOOST_UBLAS_TEST_CHECK((test_matrix_column_facade<bounded_matrix<std::size_t,7, 7> >()));

    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<mapped_matrix<double> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<mapped_matrix<float> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<mapped_matrix<long> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<mapped_matrix<unsigned long> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<mapped_matrix<int> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<mapped_matrix<unsigned int> >())
    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<mapped_matrix<std::size_t> >())
    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<mapped_matrix<char> >());

    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<compressed_matrix<double> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<compressed_matrix<float> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<compressed_matrix<long> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<compressed_matrix<unsigned long> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<compressed_matrix<int> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<compressed_matrix<unsigned int> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<compressed_matrix<std::size_t> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<compressed_matrix<char> >());

    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<coordinate_matrix<double> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<coordinate_matrix<float> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<coordinate_matrix<long> >())
    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<coordinate_matrix<unsigned long> >())
    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<coordinate_matrix<int> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<coordinate_matrix<unsigned int> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<coordinate_matrix<std::size_t> >());
    BOOST_UBLAS_TEST_CHECK(test_matrix_column_facade<coordinate_matrix<char> >());
}


int main () {
    BOOST_UBLAS_TEST_BEGIN();

    BOOST_UBLAS_TEST_DO( test_matrix_row_facade );
    BOOST_UBLAS_TEST_DO( test_matrix_column_facade );

    BOOST_UBLAS_TEST_END();
}