123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- #include <iostream>
- #include <boost/numeric/ublas/banded.hpp>
- #include <boost/numeric/ublas/io.hpp>
- #include <boost/numeric/ublas/operation.hpp>
- #include <iomanip>
- #include "utils.hpp"
- using namespace boost::numeric::ublas;
- int expected_index( size_t index, column_major ) {
- // this is the data shown on http://www.netlib.org/lapack/lug/node124.html
- // read column-by-column, aka column_major
- int mapping[] = { 0, 11, 21, 31, 12, 22, 32, 42, 23, 33, 43, 53, 34, 44, 54, 0, 45, 55, 0, 0 };
- return mapping[ index ];
- }
- int expected_index( size_t index, row_major ) {
- // this is the data shown on http://www.netlib.org/lapack/lug/node124.html
- // read row-by-row, aka row_major
- int mapping[] = { 0, 0, 11, 12, 0, 21, 22, 23, 31, 32, 33, 34, 42, 43, 44, 45, 53, 54, 55, 0 };
- return mapping[ index ];
- }
- int expected_index_6_by_5( size_t index, column_major ) {
- // read column-by-column, aka column_major
- int mapping[] = { 0, 11, 21, 31, 12, 22, 32, 42, 23, 33, 43, 53, 34, 44, 54, 64, 45, 55, 65, 0 };
- return mapping[ index ];
- }
- int expected_index_6_by_5( size_t index, row_major ) {
- // read row-by-row, aka row_major
- 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 };
- return mapping[ index ];
- }
- int expected_index_5_by_6( size_t index, column_major ) {
- // read column-by-column, aka column_major
- 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 };
- return mapping[ index ];
- }
- int expected_index_5_by_6( size_t index, row_major ) {
- // read row-by-row, aka row_major
- int mapping[] = { 0, 0, 11, 12, 0, 21, 22, 23, 31, 32, 33, 34, 42, 43, 44, 45, 53, 54, 55, 56};
- return mapping[ index ];
- }
- template< typename Orientation >
- bool test_band_storage() {
-
- int m = 5;
- int n = 5;
- int kl = 2;
- int ku = 1;
-
- banded_matrix< int, Orientation > test_matrix( m, n, kl, ku );
- test_matrix.clear();
- size_t band_storage_size = test_matrix.data().size();
-
- test_matrix( 0, 0 ) = 11;
- test_matrix( 0, 1 ) = 12;
- test_matrix( 1, 0 ) = 21;
- test_matrix( 1, 1 ) = 22;
- test_matrix( 1, 2 ) = 23;
- test_matrix( 2, 0 ) = 31;
- test_matrix( 2, 1 ) = 32;
- test_matrix( 2, 2 ) = 33;
- test_matrix( 2, 3 ) = 34;
- test_matrix( 3, 1 ) = 42;
- test_matrix( 3, 2 ) = 43;
- test_matrix( 3, 3 ) = 44;
- test_matrix( 3, 4 ) = 45;
- test_matrix( 4, 2 ) = 53;
- test_matrix( 4, 3 ) = 54;
- test_matrix( 4, 4 ) = 55;
-
- BOOST_UBLAS_TEST_TRACE( "Full matrix" );
- BOOST_UBLAS_TEST_TRACE( std::setw( 3 ) << test_matrix );
-
- BOOST_UBLAS_TEST_TRACE( "data() of matrix" );
- for ( size_t i = 0; i < band_storage_size; ++i ) {
- std::cerr << test_matrix.data()[ i ] << " ";
- }
- std::cerr << std::endl;
-
- BOOST_UBLAS_TEST_TRACE( "Expected data() of matrix" );
- for ( size_t i = 0; i < band_storage_size; ++i ) {
- std::cerr << expected_index( i, Orientation() ) << " ";
- }
- std::cerr << std::endl;
-
- size_t mismatch = 0;
- for ( size_t i = 0; i < band_storage_size; ++i ) {
- if ( test_matrix.data()[ i ] != expected_index( i, Orientation() ) ) {
- ++mismatch;
- }
- }
- return 0 == mismatch;
- }
- template< typename Orientation >
- bool test_band_storage_6_by_5() {
- int m = 6;
- int n = 5;
- int kl = 2;
- int ku = 1;
- banded_matrix< int, Orientation > test_matrix( m, n, kl, ku );
- test_matrix.clear();
- size_t band_storage_size = test_matrix.data().size();
- test_matrix( 0, 0 ) = 11;
- test_matrix( 0, 1 ) = 12;
- test_matrix( 1, 0 ) = 21;
- test_matrix( 1, 1 ) = 22;
- test_matrix( 1, 2 ) = 23;
- test_matrix( 2, 0 ) = 31;
- test_matrix( 2, 1 ) = 32;
- test_matrix( 2, 2 ) = 33;
- test_matrix( 2, 3 ) = 34;
- test_matrix( 3, 1 ) = 42;
- test_matrix( 3, 2 ) = 43;
- test_matrix( 3, 3 ) = 44;
- test_matrix( 3, 4 ) = 45;
- test_matrix( 4, 2 ) = 53;
- test_matrix( 4, 3 ) = 54;
- test_matrix( 4, 4 ) = 55;
- test_matrix( 5, 3 ) = 64;
- test_matrix( 5, 4 ) = 65;
- BOOST_UBLAS_TEST_TRACE( "Full matrix" );
- BOOST_UBLAS_TEST_TRACE( std::setw( 3 ) << test_matrix );
- BOOST_UBLAS_TEST_TRACE( "data() of matrix" );
- for ( size_t i = 0; i < band_storage_size; ++i ) {
- std::cerr << test_matrix.data()[ i ] << " ";
- }
- std::cerr << std::endl;
- BOOST_UBLAS_TEST_TRACE( "Expected data() of matrix" );
- for ( size_t i = 0; i < band_storage_size; ++i ) {
- std::cerr << expected_index_6_by_5( i, Orientation() ) << " ";
- }
- std::cerr << std::endl;
- size_t mismatch = 0;
- for ( size_t i = 0; i < band_storage_size; ++i ) {
- if ( test_matrix.data()[ i ] != expected_index_6_by_5( i, Orientation() ) ) {
- ++mismatch;
- }
- }
- return 0 == mismatch;
- }
- template< typename Orientation >
- bool test_band_storage_5_by_6() {
- int m = 5;
- int n = 6;
- int kl = 2;
- int ku = 1;
- banded_matrix< int, Orientation > test_matrix( m, n, kl, ku );
- test_matrix.clear();
- size_t band_storage_size = test_matrix.data().size();
- test_matrix( 0, 0 ) = 11;
- test_matrix( 0, 1 ) = 12;
- test_matrix( 1, 0 ) = 21;
- test_matrix( 1, 1 ) = 22;
- test_matrix( 1, 2 ) = 23;
- test_matrix( 2, 0 ) = 31;
- test_matrix( 2, 1 ) = 32;
- test_matrix( 2, 2 ) = 33;
- test_matrix( 2, 3 ) = 34;
- test_matrix( 3, 1 ) = 42;
- test_matrix( 3, 2 ) = 43;
- test_matrix( 3, 3 ) = 44;
- test_matrix( 3, 4 ) = 45;
- test_matrix( 4, 2 ) = 53;
- test_matrix( 4, 3 ) = 54;
- test_matrix( 4, 4 ) = 55;
- test_matrix( 4, 5 ) = 56;
- BOOST_UBLAS_TEST_TRACE( "Full matrix" );
- BOOST_UBLAS_TEST_TRACE( std::setw( 3 ) << test_matrix );
- BOOST_UBLAS_TEST_TRACE( "data() of matrix" );
- for ( size_t i = 0; i < band_storage_size; ++i ) {
- std::cerr << test_matrix.data()[ i ] << " ";
- }
- std::cerr << std::endl;
- BOOST_UBLAS_TEST_TRACE( "Expected data() of matrix" );
- for ( size_t i = 0; i < band_storage_size; ++i ) {
- std::cerr << expected_index_5_by_6( i, Orientation() ) << " ";
- }
- std::cerr << std::endl;
- size_t mismatch = 0;
- for ( size_t i = 0; i < band_storage_size; ++i ) {
- if ( test_matrix.data()[ i ] != expected_index_5_by_6( i, Orientation() ) ) {
- ++mismatch;
- }
- }
- return 0 == mismatch;
- }
- BOOST_UBLAS_TEST_DEF( banded_matrix_column_major )
- {
- BOOST_UBLAS_TEST_TRACE( "Test case: storage layout banded_matrix < column_major >" );
- BOOST_UBLAS_TEST_CHECK( test_band_storage< column_major >() );
- }
- BOOST_UBLAS_TEST_DEF( banded_matrix_row_major )
- {
- BOOST_UBLAS_TEST_TRACE( "Test case: storage layout banded_matrix < row_major >" );
- BOOST_UBLAS_TEST_CHECK( test_band_storage< row_major >() );
- }
- BOOST_UBLAS_TEST_DEF( banded_matrix_column_major_6_by_5 )
- {
- BOOST_UBLAS_TEST_TRACE( "Test case: storage layout banded_matrix < column_major > 6x5" );
- BOOST_UBLAS_TEST_CHECK( test_band_storage_6_by_5< column_major >() );
- }
- BOOST_UBLAS_TEST_DEF( banded_matrix_row_major_6_by_5 )
- {
- BOOST_UBLAS_TEST_TRACE( "Test case: storage layout banded_matrix < row_major > 6x5" );
- BOOST_UBLAS_TEST_CHECK( test_band_storage_6_by_5< row_major >() );
- }
- BOOST_UBLAS_TEST_DEF( banded_matrix_column_major_5_by_6 )
- {
- BOOST_UBLAS_TEST_TRACE( "Test case: storage layout banded_matrix < column_major > 5x6" );
- BOOST_UBLAS_TEST_CHECK( test_band_storage_5_by_6< column_major >() );
- }
- BOOST_UBLAS_TEST_DEF( banded_matrix_row_major_5_by_6 )
- {
- BOOST_UBLAS_TEST_TRACE( "Test case: storage layout banded_matrix < row_major > 5x6" );
- BOOST_UBLAS_TEST_CHECK( test_band_storage_5_by_6< row_major >() );
- }
- int main()
- {
- BOOST_UBLAS_TEST_SUITE( "Test storage layout of banded matrix type" );
- BOOST_UBLAS_TEST_TRACE( "Example data taken from http://www.netlib.org/lapack/lug/node124.html" );
- BOOST_UBLAS_TEST_BEGIN();
- BOOST_UBLAS_TEST_DO( banded_matrix_column_major );
-
- BOOST_UBLAS_TEST_DO( banded_matrix_row_major );
- BOOST_UBLAS_TEST_DO( banded_matrix_column_major_6_by_5 );
- BOOST_UBLAS_TEST_DO( banded_matrix_row_major_6_by_5 );
- BOOST_UBLAS_TEST_DO( banded_matrix_column_major_5_by_6 );
- BOOST_UBLAS_TEST_DO( banded_matrix_row_major_5_by_6 );
- BOOST_UBLAS_TEST_END();
- }
|