Boost logo

Ublas :

From: Nikos Platis (nplatis_at_[hidden])
Date: 2005-03-14 12:05:25


Hi,

During my experiments with the library, I tried the sparse matrix
classes. Unfortunately, I think that reverse_iterators
of these classes do not work correctly. Below is my sample code, which
demonstrates some problems; the problems are the same with all three
sparse matrix classes. I tried it with gcc 3.4.1 on Linux.
Any comments would be appreciated.

Thanks in advance,

Nikos Platis

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

namespace ublas = boost::numeric::ublas;

//typedef ublas::sparse_matrix<double> spmd;
//typedef ublas::compressed_matrix<double> spmd;
typedef ublas::coordinate_matrix<double> spmd;

int main()
{
    // Construct a sample sparse matrix
    spmd M(5, 5);
    M(0,0) = 11; M(0,2) = 13;
    M(1,0) = 21; M(1,1) = 22;
    M(2,2) = 33; M(2,4) = 35;
    M(3,3) = 44;
    M(4,0) = 52; M(4,4) = 55;

/* OK */
    for (spmd::const_iterator1 it1 = M.begin1(); it1 != M.end1(); ++it1) {
        for (spmd::const_iterator2 it2 = it1.begin(); it2 != it1.end();
++it2) {
            std::cout << it2.index1() << " " << it2.index2() << ": " <<
*it2 << std::endl;
        }
    }
    std::cout << std::endl << std::endl;

/* The following does not compile, error about the const reverse iterators
    for (spmd::const_reverse_iterator1 it1 = M.rbegin1(); it1 !=
M.rend1(); ++it1) {
        for (spmd::const_reverse_iterator2 it2 = it1.rbegin(); it2 !=
it1.rend(); ++it2) {
            std::cout << it2.index1() << " " << it2.index2() << ": " <<
*it2 << std::endl;
        }
    }
    std::cout << std::endl << std::endl;
*/
/* The following compiles but throws bad_index exception */
    for (spmd::reverse_iterator1 it1 = M.rbegin1(); it1 != M.rend1();
++it1) {
        for (spmd::reverse_iterator2 it2 = it1.rbegin(); it2 !=
it1.rend(); ++it2) {
            std::cout << it2.index1() << " " << it2.index2() << ": " <<
*it2 << std::endl;
        }
    }
    std::cout << std::endl << std::endl;
}

----------------------------------------------------

-- 
-----------------------------------------------------------------------
Nikos Platis                                  e-mail: nplatis_at_[hidden]
Department of Informatics                        tel: +30 210-727-5106
University of Athens                             fax: +30 210-727-5114
"The empires of the future are the empires of the mind." - W. Churchill
-----------------------------------------------------------------------