Boost logo

Ublas :

From: Michael Stevens (mail_at_[hidden])
Date: 2005-03-29 06:39:11

Hi Jon,

On Tuesday 29 March 2005 11:48, Jon Pearce wrote:

> Dear All,
> I would be most grateful if someone might be able to shed some light on
> why the first line of matrix::operator= is commented in the UBlas
> distributed with Boost 1.32
> [ublas/matrix.hpp]
> 195 template<class AE>
> 197 matrix &operator = (const matrix_expression<AE> &ae) {
> 198 // return assign (self_type (ae));
> 199 self_type temporary (ae);
> 200 return assign_temporary (temporary);
> 201 }

I think the comment line is only there to explain what we logically is about
to happen. I don't think it was ever part of the compiled code. It really is
a comment and not a commented out line! The form using assign_temporary is
used for efficiency.

> The current implementation appears to result in incorrect behaviour when
> used with shallow_array_adaptor<T> since the storage member is not
> available for a shallow copy in matrix::matrix(matrix_expression):

That is a interesting problem. shallow_array_adaptor are defined with

    // Array adaptor with shallow (reference) copy semantics of elements.
    // shared_array are used maintain reference counting.
    // This class breaks the normal copy semantics for a storage container and
is very dangerous!

When you use
        c = xx;
then uBLAS needs to create a temporary of the same type as 'c' before the
assignment can take place. This avoids any problems is the same elements
(aliases) appear on the left and right handside of an arbitrary expression.

Creating and copying such a temporary is not really possible for a container
using shallow_array as storage. It is however faked with the constructor

      shallow_array_adaptor (size_type size):
            size_ (size), own_ (true), data_ (new value_type [size]) {
which construct something which is of the type shallow_array_adaptor but not
really the same thing.

Personally I would remove this completely! That way you would get a compile
time error when you try and construct such a temporary. Also we could get rid
of the own_ flag which makes shallow_array_adaptor inefficient.

The correct solution would be to use the following for assignment instead
        c.assign ( xx );
or the equivilent
        noalias( c ) = xx;

Hopefully that works. Have you tried the same with 'array_adaptor'?


Michael Stevens Systems Engineering
34128 Kassel, Germany
Phone/Fax: +49 561 5218038
Navigation Systems, Estimation  and
                 Bayesian Filtering