/* posted from kyriales at ublas-dev@yahoogroups.com */
#include <iostream>
#include <boost/numeric/ublas/matrix_sparse.hpp>

using std::cout;
using std::endl;

typedef boost::numeric::ublas::sparse_matrix<double> spm_t;

int main() {

  spm_t 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;

  typedef spm_t::iterator1 i1_t;
  typedef spm_t::iterator2 i2_t;

  for (i1_t i1 = M.begin1(); i1 != M.end1(); ++i1) {
    for (i2_t i2 = i1.begin(); i2 != i1.end(); ++i2)
      cout << "(" << i2.index1() << "," << i2.index2()
           << ":" << *i2 << ")  ";
    cout << endl;
  }
  cout << endl;

  for (i2_t i2 = M.begin2(); i2 != M.end2(); ++i2) {
    for (i1_t i1 = i2.begin(); i1 != i2.end(); ++i1)
      cout << "(" << i1.index1() << "," << i1.index2()
           << ":" << *i1 << ")  ";
    cout << endl;
  }
}

/*
Output is:

(0,0:11)  (0,2:13)
(1,0:21)  (1,1:22)
(2,2:33)  (2,4:35)
(3,3:44)
(4,0:52)  (4,4:55)

(0,0:11)  (1,0:21)  (4,0:52)
(1,1:22)
(0,2:13)  (2,2:33)
(3,3:44)
(2,4:35)  (4,4:55)
*/

/* If your compiler complains, try this:

 int main()
 {
     typedef boost::numeric::ublas::matrix<double> Matr;
     Matr a(5,5);

     Matr::iterator1 it1;
     Matr::iterator2 it2;
#ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
     for ( it1 = a.begin1(); it1 != a.end1(); ++it1 )
#else
     for ( it1 = begin(a, iterator1_tag); it1 != end(a, iterator1_tag); ++it1 )
#endif
         for ( it2 = it1.begin(); it2 != it1.end(); ++it2 )
         *it1 = 0.;

     return 0;
 }

*/
