test_banded_storage_layout.cpp 8.1 KB

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