Boost logo

Ublas :

Subject: Re: [ublas] Exception when resizing matrix & vector: how to correctly allocate space dynamically?
From: oswin krause (oswin.krause_at_[hidden])
Date: 2013-10-05 14:09:37


Hi,

I would be a lot easier for all of us if you wouldn't include the
working code parts and only show the errors and problems you encounter.
>
> /*
> multiply matrix and vector attempting to resize existing objects
>
> What does this error message mean?
> Check failed in file
> ...boost_1_54_0/boost/numeric/ublas/matrix_sparse.hpp at line 2807:
> !preserve
> libc++abi.dylib: terminate called throwing an exception
> */
>
looking at the line 2807 in the file you get:

void resize (size_type size1, size_type size2, bool preserve = true) {
             // FIXME preserve unimplemented
             BOOST_UBLAS_CHECK (!preserve, internal_logic ());

so you need to write resize(size1,size2,false);
The default behavior of resize is to save the old values if possible. this is cumbersome to implement
for sparse matrices when they are shrunk, so this is not implemented yet. But yeah, we also had fun with that issue. Default
behaviour should never be to throw an exception :).

But the correct way would be not to use resize when you can create the matrices with the right sizes instead.

> // what happens to the memory of v2 here?
> noalias(*v2.at(i)) = prod(*theMatrix.at(i),*v1.at(i));
>
I am not sure i understand the question.
It is used to store the result of the computation. therefore the target
needs to have the same size as the result of prod. Also be aware that
you can not write

noalias(*v2.at(i)) = prod(*theMatrix.at(i),*v2.at(i));

because this would be aliasing.

for the rest: use resize without preserve or push_back of the std::vector.
Never use new/delete as long as no one forces you to.
Instead of storing n vectors of same size in a std::vector use a matrix.