Boost logo

Ublas :

From: Michael Stevens (m-stevens_at_[hidden])
Date: 2005-01-20 02:35:27


Vento,

> Thanks for your answer. I tried iterator1 and iterator2 on a sparse
> matrix...but they are good to get rows and column....(with 0). How to get
> only the non-zeros? If you have some examples, please attach them.

Iterators definitely only iterate over the sparse elements for the sparse
matrix types. That is they iterate over the structural non zeros. If you are
getting full rows and column that would mean that you have previously
assigned values to them. In many cases uBLAS tries to avoid create zero
valued elements but for efficiency it cannot enforce this constraint. You
should check previous steps in you algorithm to see where this has happened.

The follow code demonstrates the principles:
/*
 * Demonstrate matrix non-zeros
 */
#include <boost/numeric/ublas/matrix_sparse.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <iostream>

int main()
{
        typedef boost::numeric::ublas::compressed_matrix<double> CMatrix;
        
        CMatrix cm(3,3);
        
        cm(0,0) = 1;
        cm(0,1) = 2;
        cm(1,1) = 3;
        
        // Output - Complete matrix
        std::cout << cm << std::endl;
        
        // Output - Iterate over sparse structure
        for (CMatrix::const_iterator1 ri = cm.begin1(); ri != cm.end1(); ++ri) {
                for (CMatrix::const_iterator2 ci = ri.begin(); ci != ri.end(); ++ci) {
                        std::cout<< ci.index1() <<','<< ci.index2() <<'='<< *ci <<std::endl;
                }
        }
}

___________________________________
Michael Stevens Systems Engineering

Navigation Systems, Estimation and
                 Bayesian Filtering
    http://bayesclasses.sf.net
___________________________________