Boost logo

Glas :

Re: [glas] dense and sparse vectors

From: Toon Knapen (toon.knapen_at_[hidden])
Date: 2005-08-03 15:20:23


While going into more detail on how to iterate over all elements in a
sparse vector or only over all non-zero's I had a look again at
Trilinos, MTL2, OSKI, uBLAS and the MTL3-prototype and I did not find a
  documented way of how to iterate only over the non-zero's in any of
these (but please correct me if I'm wrong). OSKI however does allow to
modify explicitly non-zero's for instance (using the SetMatEntry function).
But adding two sparse vectors for instance can benefit (in terms of
performance) of this because we know the result will only have
non-zero's where either of the two initial matrices have non-zero's. And
in case we can detect that the initial matrices have the same structure,
the operation is as effient as a daxpy.

Theodore Papadopoulo wrote:
> On Tue, 2005-08-02 at 14:38 +0200, Toon Knapen wrote:
>
>>Theodore Papadopoulo wrote:
>
>
>>So IIUC 'begin()' returns a structure and this structure on this
>>structure is passed on to the constructor of an iterator? So 'begin()'
>>and 'end()' do not return an iterator, right?
>
>
> right.
>

Thinking about it again, your approach gave me following idea which
probably is more generic: The begin and end functions just take a 'tag'
as argument that specifies if the iterator iterates over all elements or
only all structural non-zero's. So soth like:

<code>
struct all{} ;
struct nz{} ;

class some_sparse_vector {
public:

   iterator<all> begin(all) ;
   iterator<nz> begin(nz) ;
   iterator<all> end(all) ;
   iterator<nz> end(nz) ;
} ;

// so implementing a function that prints the elements
// in a vector might be implemented to print all elements
// or only the non-zero's
template < class Tag >
void print(const some_sparse_vector& v)
{
   std::copy( v.begin(Tag()), v.end(Tag()),
              std::ostream_iterator(std::cout, ',')) ;
}
</code>

>
> I'll try that, but:
>
> cvs -z3 -d:pserver:anonymous_at_[hidden]:/cvsroot/glas co glas
> -r tk1
>
> gives me an error. I suceeded in doing:
>
> cvs -z3 -d:pserver:anonymous_at_[hidden]:/cvsroot/glas co glas
>
> I suppose modifying those sources should be OK.
>
> Theo.

What you can do is just checkout the trunk and than 'cd glas ; cvs
update -r tk1'. I just tried it and it works for me.