Boost logo

Ublas :

From: Vardan Akopian (vakopian_at_[hidden])
Date: 2007-09-20 05:44:04


Hello,

I just discovered something about ublas sparse matrix assignments, and
would like to check if this is by design, or a bug. Given 2 sparse
matrices a and b, it looks like doing
noalias(b) = a
does not transfer a's full structure to b, i.e. a's structural 0's are
lost. For example after that assignment a.nnz() may not be equal to
b.nnz(). Note that, "b = a" works as expected. So is this intended
behavior? In general, when is it guaranteed that two e.g. compressed
matrices will have the same structure? This cold be useful, for
example, for iterating over both matrices at the same time.

BTW, If this is really by design, it could provide a quick way to
"re-compress" sparse matrices (I remember some people asking for such
a functionality on this list).

Here is a quick test program

#include <iostream>
#include <boost/numeric/ublas/matrix_sparse.hpp>

int main(int argc, char ** argv)
{
        using namespace boost::numeric::ublas;

        size_t sz = 5;
        compressed_matrix<double> a(sz, sz), b(sz, sz);
        for (size_t i = 0; i < sz; ++i) {
                a(i,i) = 1;
                for (size_t j = i+1; j < sz; ++j) {
                        a(i,j) = 0; // structural 0
                }
        }

        std::cout << "a.nnz = " << a.nnz() << std::endl; // will print 15
        noalias(b) = a;
        std::cout << "b.nnz = " << b.nnz() << std::endl; // will print 5
        b = a;
        std::cout << "b.nnz = " << b.nnz() << std::endl; // will print 15
        return 0;
}

Thanks.
-Vardan