Boost logo

Boost Users :

From: Nikos Platis (nplatis_at_[hidden])
Date: 2005-03-02 13:22:50


Hi,

I am relatively new to Boost and I find it an excellent C++ library.
During my experiments with the library, I tried the sparse matrix
classes offered by uBLAS. 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

PS. I post here instead of the uBLAS Yahoo Group because I am still
waiting for membership approval...

----------------------------------------------------
#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
-----------------------------------------------------------------------

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net