Boost logo

Glas :

Re: [glas] using (boost)range or STL style interface [was: dense and sparse vectors]

From: Peter Gottschling (pgottsch_at_[hidden])
Date: 2005-10-04 21:34:35

On 05.08.2005, at 11:08, Ian McCulloch wrote:

> For another example, I have a lot of uses for a sparse matrix for
> complex
> numbers, implemented as the sum of a pure real matrix and a pure
> imaginary
> matrix. I have some applications where I want to allow complex
> arithmetic
> without penalizing too much the real case, and even in the complex case
> many of the matrix elements will be purely real. This begs for
> something
> like
> template <typename T>
> class ComplexSparseMatrix
> {
> // ....
> SparseMatrix<T> RealPart;
> SparseMatrix<T> ImaginaryPart;
> };
> It is trivial to implement the basic arithmetic operations, but I would
> very much like to see an efficient iterator design for this type!
Dear Ian,

This indeed an interesting case. Dave Abrahams and I introduced in MTL
the conception of cursors and property maps in order to separate the
notion of traversal and access. The cursors represents in an
appropriate way the position in the matrix whereby different traversals
are (or will be) available for different matrices. The property maps
allow to ask for the row or the column of some cursor. Of course, the
value on this position can be read and in case the property map has
write access also written.

Your example showed me that this separation has another advantage. If
your matrices for the RealPart and for the ImaginaryPart have the same
sparsity pattern the property map for the value can pick easily the
real and the imaginary part and return the complex number. For writing
a complex number can be split into its part and written into both
matrices. In addition, you can introduce new property maps for
accessing only the real or only the imaginary part.

So, thank you very much for this example because it showed that the
idea of property maps and cursors has also a wider applicability than

Best Regards,


> FWIW, the iterator model I ended up with uses a single iterator (no
> begin/end pair) with a conversion to bool that becomes false once the
> iterator hits the end, and an index() member (for matrices, a two level
> segmented iterator with index1()/index2() on the inner iterator).
> Iterator categories distinguish between unordered(sparse),
> ordered(sparse), and dense indices. This is all done with free
> functions,
> so it works for std::vector too. Since examples seem to be the order
> of
> the day,
> template <typename T>
> void print_vector(T const& v)
> {
> typename iterator<T>::type I = iterate(v);
> while (I)
> {
> cout << "v[" << I.index() << "] = " << (*I) << '\n';
> ++I;
> }
> }
> Cheers,
> Ian
> _______________________________________________
> glas mailing list
> glas_at_[hidden]
Peter Gottschling
Research Associate
Open Systems Laboratory
Indiana University
301i Lindley Hall
Bloomington, IN 47405
Tel.: +1 812 855-8898 Fax: +1 812 856 0853