1

Suppose you have a long boost::numeric::ublas::vector and you want to perform an update operation on a subset of the elements. How many of the elements should be updated is somewhere between "all" or "none". Which elements to update is given by a sparse compressed_vector containing a "1" for each element that should be updated.

I could think of two ways to solve this problem:

  1. Just multiplying the right hand side with a mask:

    using namespace boost::numeric::ublas;
    vector<double> x,some,other,stuff;
    compressed_vector<int> update_mask;
    [...]
    noalias(x) += element_prod(update_mask, some+element_div(other,stuff))
    

    Problem with this is that it looks quite inefficient: wouldn't ublas calculate the whole vector and then just throw away all unused values in this case (ie. where update_mask==0)?

    I'd expect it to be even slower than just

    noalias(x) += some+element_div(other,stuff)
    

    which would be horribly inefficient if only a few elements have to be updated.

  2. Loop over all values to update

    [....]
    for(compressed_vector<int>::iterator it = update_mask.begin(); it!=update_mask.end(); ++it)
        x[it.index()] += some[it.index()]+other[it.index()]/stuff[it.index()]);
    

    The problem with this is that a) it looks awful, b) kinda defeats the purpose of using vectors in the first place and c.) should be horribly inefficient if a lot of indices are to be updated and/or the operation becomes more complex.

Any ideas on how to do this efficiently? I'm pretty sure this is a fairly common problem, but I couldn't find anything useful about it (and the ublas documentation is ... not fun).

4

0 回答 0