Boost logo

Ublas :

From: Gunter Winkler (guwi17_at_[hidden])
Date: 2006-06-19 05:46:06


On Monday 19 June 2006 05:26, Haroon Khan wrote:
> I've been trying experiment with the matrix_slice class with the
> following code, and it seems that the assignment operator defined for
> matrix_slice only assigns the data of the matrix_slice, but not the
> slice information.
>
> using namespace boost::numeric::ublas;
> matrix<double> m(6,6);
> matrix_slice<matrix<double> >ms(m, slice(0,1,3), slice(0,1,3));
> for (unsigned int i=0;i<ms.size1();++i){
> for (unsigned int j=0;j<ms.size2();++j){
> ms(i,j)=6*i+j +1 ;
> }
> }
>
> //then I define another matrix slice with a stride of 2 and size of
> 3 matrix_slice<matrix<double> > ms1(m, slice(0,2,3), slice(0,2,3);
>
> //and then I try to assign ms <- ms1
> ms=ms1 ; //This line does'nt work,
>
> Is this by design or could it be a bug ?? I'm using boost v1.33.

No. This is a typical "const"-trap. matrix_slice has to (quite different)
assignment operators.

the first is called when a the rhs is constant (rvalue - assignment)

        BOOST_UBLAS_INLINE
        matrix_slice &operator = (const matrix_slice &ms) {
            matrix_assign<scalar_assign> (*this, ms);
            return *this;
        }

the second is called when the rhs is mutable (lvalue assignment)

        BOOST_UBLAS_INLINE
        matrix_slice &assign_temporary (matrix_slice &ms) {
            return *this = ms;
        }

You have to use constant slices wherever possible.

try:
  matrix_slice<matrix<double> > ms(m, slice(0,1,3), slice(0,1,3));
  const matrix_slice<const matrix<double> > ms1(m, slice(0,2,3), slice(0,2,3);
  ms = ms1;

The same tricky behaviour occurs when you use assign elements of a sparse
matrix. This is a typical problem for proxies and we decided to allow
different behavior of const and non-const references.

mfg
Gunter