Boost logo

Ublas :

From: Ian McCulloch (ianmcc_at_[hidden])
Date: 2005-02-02 09:47:16


Austin Bingham wrote:

> NOTE: I'd already posted this to boost-users, when someone directed me
> to this list. So, if it seems like a repost, that's probably why :)
>
> Is there any way to set the "default" value of a sparse_matrix?
> sparse_matrix seems to guarantee that you always get a value when you
> access an index, even if that index hasn't been explicitly set. At
> least, that's the behavior I've seen. For sparse_matrix<float>, for
> instance, you get 0. Is there any way to
> specify this value? That is, if I wanted to get back, say, -1 for
> unset entries, is there a way to do this? There's no direct mention of
> this that I can find, although there were some discussions of
> zero-constructors or somesuch that may have been dealing with this
> question. Thanks.

You *could* do something like this, to get a float that is
default-initialized to -1:

struct my_weird_float
{
   my_weird_float() : f_(-1) {}

   operator float&() { return f_; }

   // ...

   float f_;
};

But storing this in a sparse matrix is a really bad idea, as all linear
algebra operations must assume that the 'missing' elements in a sparse
container are ZERO. Imagine, for a minute, how a matrix-vector
multiplication would work!

HTH,
Ian McCulloch