Boost logo

Ublas :

From: Gunter Winkler (guwi17_at_[hidden])
Date: 2007-03-06 03:49:55


Am Dienstag, 6. März 2007 08:58 schrieb Sourabh:
> If I use x.resize (), there is a chance that some of the extra
> entries remain in the vector, which I don't want.

You can use resize to shrink a vector, too. However, this results in an
extra copy operation.

> Is there any way by which I careate stl vector, fill that, and assign
> the contents of stl vector to uBLAS vector at the time of creation ?
> In uBLAS vector interface I could not find such thing.

I use:
ublas::vector<double> x( y.size() );
std::copy( y.begin(), y.end(), x.begin() );

> The iterator on the sparse vector will allow me to iterate over the
> nnz entries, I suppose. I can get their indices by index ()
> function on iterator.

But assure that you use const_iterator because iterator may create
(structural) non-zeros on access.

> My requirement is to create a dense vector out
> of the non-zero elements of the sparse vector. I think, I can give
> the size of the dense vector to be equal to A.nnz ().

Yes.

Note1: If you don't need the index values you can directly access the
non-zeros of a compressed/coordinate vector via
cv.value_data()[i]; // 0 <= i < nnz
cv.index_data()[i]; // gives the corresponding indices

Note2: If you use prod(A, v) with a sparse v you should define A as a
column major matrix.

mfg
Gunter