Main Page | Namespace List | Data Structures | File List | Namespace Members | Data Fields | Globals

ex_sparse1.cpp

Go to the documentation of this file.
00001 /* posted from kyriales at ublas-dev@yahoogroups.com */
00002 #include <iostream>
00003 #include <boost/numeric/ublas/matrix_sparse.hpp>
00004 
00005 using std::cout;
00006 using std::endl;
00007 
00008 typedef boost::numeric::ublas::sparse_matrix<double> spm_t;
00009 
00010 int main() {
00011 
00012   spm_t M (5,5);
00013   M(0,0) = 11; M(0,2) = 13;
00014   M(1,0) = 21; M(1,1) = 22;
00015   M(2,2) = 33; M(2,4) = 35;
00016   M(3,3) = 44;
00017   M(4,0) = 52; M(4,4) = 55;
00018 
00019   typedef spm_t::iterator1 i1_t;
00020   typedef spm_t::iterator2 i2_t;
00021 
00022   for (i1_t i1 = M.begin1(); i1 != M.end1(); ++i1) {
00023     for (i2_t i2 = i1.begin(); i2 != i1.end(); ++i2)
00024       cout << "(" << i2.index1() << "," << i2.index2()
00025            << ":" << *i2 << ")  ";
00026     cout << endl;
00027   }
00028   cout << endl;
00029 
00030   for (i2_t i2 = M.begin2(); i2 != M.end2(); ++i2) {
00031     for (i1_t i1 = i2.begin(); i1 != i2.end(); ++i1)
00032       cout << "(" << i1.index1() << "," << i1.index2()
00033            << ":" << *i1 << ")  ";
00034     cout << endl;
00035   }
00036 }
00037 
00038 /*
00039 Output is:
00040 
00041 (0,0:11)  (0,2:13)
00042 (1,0:21)  (1,1:22)
00043 (2,2:33)  (2,4:35)
00044 (3,3:44)
00045 (4,0:52)  (4,4:55)
00046 
00047 (0,0:11)  (1,0:21)  (4,0:52)
00048 (1,1:22)
00049 (0,2:13)  (2,2:33)
00050 (3,3:44)
00051 (2,4:35)  (4,4:55)
00052 */
00053 
00054 /* If your compiler complains, try this:
00055 
00056  int main()
00057  {
00058      typedef boost::numeric::ublas::matrix<double> Matr;
00059      Matr a(5,5);
00060 
00061      Matr::iterator1 it1;
00062      Matr::iterator2 it2;
00063 #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
00064      for ( it1 = a.begin1(); it1 != a.end1(); ++it1 )
00065 #else
00066      for ( it1 = begin(a, iterator1_tag); it1 != end(a, iterator1_tag); ++it1 )
00067 #endif
00068          for ( it2 = it1.begin(); it2 != it1.end(); ++it2 )
00069          *it1 = 0.;
00070 
00071      return 0;
00072  }
00073 
00074 */

Generated on Wed Oct 1 14:41:00 2003 for Sample Code by doxygen 1.3.2