Boost logo

Ublas :

Subject: [ublas] how to use boost::numeric::ublas::matrix_vector_range with boost::numeric::ublas::basic_range<int> correctly
From: Guo, Gcwenken (gcwenken.guo_at_[hidden])
Date: 2014-08-19 19:51:50


Hi:
    I construct boost::numeric::ublas::compressed_matrix<std::complex<double>,boost::numeric::ublas::row_major,0,boost::numeric::ublas::unbounded_array<std::size_t>> and then extract the diagonal part via boost::numeric::ublas::matrix_vector_range. The source is:

#include<boost/numeric/ublas/matrix_sparse.hpp>
#include<boost/numeric/ublas/matrix_proxy.hpp>
#include<boost/numeric/ublas/io.hpp>

int main()
{
  boost::numeric::ublas::compressed_matrix<std::complex<double>,boost::numeric::ublas::row_major,0,boost::numeric::ublas::unbounded_array<std::size_t>>matrix(3,3);
  matrix.push_back(0,0,{1,1});
  matrix.push_back(0,1,{2,1});
  matrix.push_back(1,1,{3,1});
  matrix.push_back(2,2,{4,1});
  std::cout<<boost::numeric::ublas::matrix_vector_range<decltype(matrix)>(matrix,boost::numeric::ublas::basic_range<std::size_t>(0,matrix.size1()),boost::numeric::ublas::basic_range<std::size_t>(0,matrix.size2()))<<std::endl;
}

and it works fine.

And then, because I need to pass the compressed_matrix to some fortran function as well as the fortran function requires the row and column indexes must be int, I change std::size_t to int. The new source code is:
#include<boost/numeric/ublas/matrix_sparse.hpp>
#include<boost/numeric/ublas/matrix_proxy.hpp>
#include<boost/numeric/ublas/io.hpp>

int main()
{
  boost::numeric::ublas::compressed_matrix<std::complex<double>,boost::numeric::ublas::row_major,0,boost::numeric::ublas::unbounded_array<int>>matrix(3,3);
  matrix.push_back(0,0,{1,1});
  matrix.push_back(0,1,{2,1});
  matrix.push_back(1,1,{3,1});
  matrix.push_back(2,2,{4,1});
  std::cout<<boost::numeric::ublas::matrix_vector_range<decltype(matrix)>(matrix,boost::numeric::ublas::basic_range<int>(0,matrix.size1()),boost::numeric::ublas::basic_range<int>(0,matrix.size2()))<<std::endl;
}

After that I use g++ -std=c++11 -O2 -march=native -DNEBUG to compile the source code and run the executable file (gcc version 4.9.1). The following error appears:
Check failed in file /usr/include/boost/numeric/ublas/storage.hpp at line 892:
start_ <= stop
terminate called after throwing an instance of 'boost::numeric::ublas::bad_index'
  what(): bad index
Aborted

Then I go to /usr/include/boost/numeric/ublas/storage.hpp at line 892 and see:
BOOST_UBLAS_INLINE
        basic_range ():
            start_ (0), size_ (0) {}
        BOOST_UBLAS_INLINE
        basic_range (size_type start, size_type stop):
            start_ (start), size_ (stop - start) {
            BOOST_UBLAS_CHECK (start_ <= stop, bad_index ());
        }

But in my case start_ is less than stop. So I have no idea why such error will appear. Any idea?

Thank you very much