1 
2 #include <iostream>
3 #include <boost/numeric/ublas/banded.hpp>
4 #include <boost/numeric/ublas/io.hpp>
5 #include <boost/numeric/ublas/operation.hpp>
6 #include <iomanip>
7 
8 #include "utils.hpp"
9 
10 using namespace boost::numeric::ublas;
11 
expected_index(int index,column_major)12 int expected_index( int index, column_major ) {
13    // this is the data shown on http://www.netlib.org/lapack/lug/node124.html
14    // read column-by-column, aka column_major
15    int mapping[] = { 0, 11, 21, 31, 12, 22, 32, 42, 23, 33, 43, 53, 34, 44, 54, 0, 45, 55, 0, 0 };
16    return mapping[ index ];
17 }
18 
19 
expected_index(int index,row_major)20 int expected_index( int index, row_major ) {
21    // this is the data shown on http://www.netlib.org/lapack/lug/node124.html
22    // read row-by-row, aka row_major
23    int mapping[] = { 0, 0, 11, 12, 0, 21, 22, 23, 31, 32, 33, 34, 42, 43, 44, 45, 53, 54, 55, 0 };
24    return mapping[ index ];
25 }
26 
expected_index_6_by_5(int index,column_major)27 int expected_index_6_by_5( int index, column_major ) {
28    // read column-by-column, aka column_major
29    int mapping[] = { 0, 11, 21, 31, 12, 22, 32, 42, 23, 33, 43, 53, 34, 44, 54, 64, 45, 55, 65, 0 };
30    return mapping[ index ];
31 }
32 
expected_index_6_by_5(int index,row_major)33 int expected_index_6_by_5( int index, row_major ) {
34    // read row-by-row, aka row_major
35    int mapping[] = { 0, 0, 11, 12, 0, 21, 22, 23, 31, 32, 33, 34, 42, 43, 44, 45, 53, 54, 55, 0, 64, 65, 0, 0 };
36    return mapping[ index ];
37 }
38 
expected_index_5_by_6(int index,column_major)39 int expected_index_5_by_6( int index, column_major ) {
40    // read column-by-column, aka column_major
41    int mapping[] = { 0, 11, 21, 31, 12, 22, 32, 42, 23, 33, 43, 53, 34, 44, 54, 0, 45, 55, 0, 0, 56, 0, 0, 0 };
42    return mapping[ index ];
43 }
44 
expected_index_5_by_6(int index,row_major)45 int expected_index_5_by_6( int index, row_major ) {
46    // read row-by-row, aka row_major
47    int mapping[] = { 0, 0, 11, 12, 0, 21, 22, 23, 31, 32, 33, 34, 42, 43, 44, 45, 53, 54, 55, 56};
48    return mapping[ index ];
49 }
50 
51 template< typename Orientation >
test_band_storage()52 bool test_band_storage() {
53 
54     int m = 5;
55     int n = 5;
56     int kl = 2;
57     int ku = 1;
58 
59     banded_matrix< int, Orientation > test_matrix( m, n, kl, ku );
60     test_matrix.clear();
61     int band_storage_size = test_matrix.data().size();
62 
63     test_matrix( 0, 0 ) = 11;
64     test_matrix( 0, 1 ) = 12;
65     test_matrix( 1, 0 ) = 21;
66     test_matrix( 1, 1 ) = 22;
67     test_matrix( 1, 2 ) = 23;
68     test_matrix( 2, 0 ) = 31;
69     test_matrix( 2, 1 ) = 32;
70     test_matrix( 2, 2 ) = 33;
71     test_matrix( 2, 3 ) = 34;
72     test_matrix( 3, 1 ) = 42;
73     test_matrix( 3, 2 ) = 43;
74     test_matrix( 3, 3 ) = 44;
75     test_matrix( 3, 4 ) = 45;
76     test_matrix( 4, 2 ) = 53;
77     test_matrix( 4, 3 ) = 54;
78     test_matrix( 4, 4 ) = 55;
79 
80     BOOST_UBLAS_TEST_TRACE( "Full matrix" );
81     BOOST_UBLAS_TEST_TRACE( std::setw( 3 ) << test_matrix );
82 
83     BOOST_UBLAS_TEST_TRACE( "data() of matrix" );
84     for ( int i = 0; i < band_storage_size; ++i ) {
85         std::cerr << test_matrix.data()[ i ] << " ";
86     }
87     std::cerr << std::endl;
88 
89     BOOST_UBLAS_TEST_TRACE( "Expected data() of matrix" );
90     for ( int i = 0; i < band_storage_size; ++i ) {
91         std::cerr << expected_index( i, Orientation() ) << " ";
92     }
93     std::cerr << std::endl;
94 
95     size_t mismatch = 0;
96 
97     for ( int i = 0; i < band_storage_size; ++i ) {
98       if ( test_matrix.data()[ i ] != expected_index( i, Orientation() ) ) {
99         ++mismatch;
100       }
101     }
102 
103     return 0 == mismatch;
104 }
105 
106 template< typename Orientation >
test_band_storage_6_by_5()107 bool test_band_storage_6_by_5() {
108 
109     int m = 6;
110     int n = 5;
111     int kl = 2;
112     int ku = 1;
113 
114 
115     banded_matrix< int, Orientation > test_matrix( m, n, kl, ku );
116     test_matrix.clear();
117     int band_storage_size = test_matrix.data().size();
118 
119     test_matrix( 0, 0 ) = 11;
120     test_matrix( 0, 1 ) = 12;
121     test_matrix( 1, 0 ) = 21;
122     test_matrix( 1, 1 ) = 22;
123     test_matrix( 1, 2 ) = 23;
124     test_matrix( 2, 0 ) = 31;
125     test_matrix( 2, 1 ) = 32;
126     test_matrix( 2, 2 ) = 33;
127     test_matrix( 2, 3 ) = 34;
128     test_matrix( 3, 1 ) = 42;
129     test_matrix( 3, 2 ) = 43;
130     test_matrix( 3, 3 ) = 44;
131     test_matrix( 3, 4 ) = 45;
132     test_matrix( 4, 2 ) = 53;
133     test_matrix( 4, 3 ) = 54;
134     test_matrix( 4, 4 ) = 55;
135     test_matrix( 5, 3 ) = 64;
136     test_matrix( 5, 4 ) = 65;
137 
138     BOOST_UBLAS_TEST_TRACE( "Full matrix" );
139     BOOST_UBLAS_TEST_TRACE( std::setw( 3 ) << test_matrix );
140 
141     BOOST_UBLAS_TEST_TRACE( "data() of matrix" );
142     for ( int i = 0; i < band_storage_size; ++i ) {
143         std::cerr << test_matrix.data()[ i ] << " ";
144     }
145     std::cerr << std::endl;
146 
147     BOOST_UBLAS_TEST_TRACE( "Expected data() of matrix" );
148     for ( int i = 0; i < band_storage_size; ++i ) {
149         std::cerr << expected_index_6_by_5( i, Orientation() ) << " ";
150     }
151     std::cerr << std::endl;
152 
153     size_t mismatch = 0;
154 
155     for ( int i = 0; i < band_storage_size; ++i ) {
156       if ( test_matrix.data()[ i ] != expected_index_6_by_5( i, Orientation() ) ) {
157         ++mismatch;
158       }
159     }
160 
161     return 0 == mismatch;
162 }
163 
164 template< typename Orientation >
test_band_storage_5_by_6()165 bool test_band_storage_5_by_6() {
166 
167     int m = 5;
168     int n = 6;
169     int kl = 2;
170     int ku = 1;
171 
172     banded_matrix< int, Orientation > test_matrix( m, n, kl, ku );
173     test_matrix.clear();
174     int band_storage_size = test_matrix.data().size();
175 
176     test_matrix( 0, 0 ) = 11;
177     test_matrix( 0, 1 ) = 12;
178     test_matrix( 1, 0 ) = 21;
179     test_matrix( 1, 1 ) = 22;
180     test_matrix( 1, 2 ) = 23;
181     test_matrix( 2, 0 ) = 31;
182     test_matrix( 2, 1 ) = 32;
183     test_matrix( 2, 2 ) = 33;
184     test_matrix( 2, 3 ) = 34;
185     test_matrix( 3, 1 ) = 42;
186     test_matrix( 3, 2 ) = 43;
187     test_matrix( 3, 3 ) = 44;
188     test_matrix( 3, 4 ) = 45;
189     test_matrix( 4, 2 ) = 53;
190     test_matrix( 4, 3 ) = 54;
191     test_matrix( 4, 4 ) = 55;
192     test_matrix( 4, 5 ) = 56;
193 
194     BOOST_UBLAS_TEST_TRACE( "Full matrix" );
195     BOOST_UBLAS_TEST_TRACE( std::setw( 3 ) << test_matrix );
196 
197     BOOST_UBLAS_TEST_TRACE( "data() of matrix" );
198     for ( int i = 0; i < band_storage_size; ++i ) {
199         std::cerr << test_matrix.data()[ i ] << " ";
200     }
201     std::cerr << std::endl;
202 
203     BOOST_UBLAS_TEST_TRACE( "Expected data() of matrix" );
204     for ( int i = 0; i < band_storage_size; ++i ) {
205         std::cerr << expected_index_5_by_6( i, Orientation() ) << " ";
206     }
207     std::cerr << std::endl;
208 
209     size_t mismatch = 0;
210 
211     for ( int i = 0; i < band_storage_size; ++i ) {
212       if ( test_matrix.data()[ i ] != expected_index_5_by_6( i, Orientation() ) ) {
213         ++mismatch;
214       }
215     }
216 
217     return 0 == mismatch;
218 }
219 
220 
221 
222 
BOOST_UBLAS_TEST_DEF(banded_matrix_column_major)223 BOOST_UBLAS_TEST_DEF( banded_matrix_column_major )
224 {
225 	BOOST_UBLAS_TEST_TRACE( "Test case: storage layout banded_matrix < column_major >" );
226 
227     BOOST_UBLAS_TEST_CHECK( test_band_storage< column_major >() );
228 }
229 
BOOST_UBLAS_TEST_DEF(banded_matrix_row_major)230 BOOST_UBLAS_TEST_DEF( banded_matrix_row_major )
231 {
232 	BOOST_UBLAS_TEST_TRACE( "Test case: storage layout banded_matrix < row_major >" );
233 
234     BOOST_UBLAS_TEST_CHECK( test_band_storage< row_major >() );
235 }
236 
BOOST_UBLAS_TEST_DEF(banded_matrix_column_major_6_by_5)237 BOOST_UBLAS_TEST_DEF( banded_matrix_column_major_6_by_5 )
238 {
239     BOOST_UBLAS_TEST_TRACE( "Test case: storage layout banded_matrix < column_major > 6x5" );
240 
241     BOOST_UBLAS_TEST_CHECK( test_band_storage_6_by_5< column_major >() );
242 }
243 
BOOST_UBLAS_TEST_DEF(banded_matrix_row_major_6_by_5)244 BOOST_UBLAS_TEST_DEF( banded_matrix_row_major_6_by_5 )
245 {
246     BOOST_UBLAS_TEST_TRACE( "Test case: storage layout banded_matrix < row_major > 6x5" );
247 
248     BOOST_UBLAS_TEST_CHECK( test_band_storage_6_by_5< row_major >() );
249 }
250 
BOOST_UBLAS_TEST_DEF(banded_matrix_column_major_5_by_6)251 BOOST_UBLAS_TEST_DEF( banded_matrix_column_major_5_by_6 )
252 {
253     BOOST_UBLAS_TEST_TRACE( "Test case: storage layout banded_matrix < column_major > 5x6" );
254 
255     BOOST_UBLAS_TEST_CHECK( test_band_storage_5_by_6< column_major >() );
256 }
257 
BOOST_UBLAS_TEST_DEF(banded_matrix_row_major_5_by_6)258 BOOST_UBLAS_TEST_DEF( banded_matrix_row_major_5_by_6 )
259 {
260     BOOST_UBLAS_TEST_TRACE( "Test case: storage layout banded_matrix < row_major > 5x6" );
261 
262     BOOST_UBLAS_TEST_CHECK( test_band_storage_5_by_6< row_major >() );
263 }
264 
main()265 int main()
266 {
267 
268 	BOOST_UBLAS_TEST_SUITE( "Test storage layout of banded matrix type" );
269 
270 	BOOST_UBLAS_TEST_TRACE( "Example data taken from http://www.netlib.org/lapack/lug/node124.html" );
271 
272 	BOOST_UBLAS_TEST_BEGIN();
273 
274     BOOST_UBLAS_TEST_DO( banded_matrix_column_major );
275 
276     BOOST_UBLAS_TEST_DO( banded_matrix_row_major );
277 
278     BOOST_UBLAS_TEST_DO( banded_matrix_column_major_6_by_5 );
279 
280     BOOST_UBLAS_TEST_DO( banded_matrix_row_major_6_by_5 );
281 
282     BOOST_UBLAS_TEST_DO( banded_matrix_column_major_5_by_6 );
283 
284     BOOST_UBLAS_TEST_DO( banded_matrix_row_major_5_by_6 );
285 
286 	BOOST_UBLAS_TEST_END();
287 
288     return EXIT_SUCCESS;
289 }
290 
291 
292