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

iterator_chaos.cpp

Go to the documentation of this file.
00001 
00002 #include <iostream>
00003 
00004 #include <boost/numeric/ublas/matrix.hpp>
00005 #include <boost/numeric/ublas/matrix_sparse.hpp>
00006 #include <boost/numeric/ublas/vector.hpp>
00007 #include <boost/numeric/ublas/io.hpp>
00008 
00009 using std::cout;
00010 using std::endl;
00011 
00012 using namespace boost::numeric::ublas;
00013 
00014 int main(int argc, char *argv[])
00015 {
00016 
00017   typedef sparse_matrix<double>  MY_STIMA;
00018 
00019   MY_STIMA M(4,4,16);
00020 
00021   for (int i=0; i<M.size1(); ++i) M(i,i)=1.0;
00022   M(0,0)=3;
00023   M(0,1)=-1;
00024   M(0,2)=-2;
00025   M(1,0)=-1;
00026   M(1,1)=4;
00027   M(1,2)=-2;
00028   M(2,0)=-2;
00029   M(2,1)=-2;
00030   M(2,2)=9;
00031 
00032   cout << "input M = " << M << "\n";
00033 
00034   MY_STIMA::iterator1 r_it1 = M.begin1();
00035   MY_STIMA::iterator2 c_it1 = M.begin2();
00036   while ( (r_it1 != M.end1()) && (c_it1 != M.end2()) ) {
00037         MY_STIMA::iterator1  r_it2 = c_it1.begin();
00038         MY_STIMA::iterator2  c_it2 = r_it1.begin();
00039         MY_STIMA::iterator2  c;
00040 
00041         c = c_it2;
00042         double temp;
00043         while ( (r_it2 != c_it1.end()) && (c != r_it1.end()) ) {
00044           while (c.index2() < r_it2.index1()) { ++c; }
00045           while (c.index2() > r_it2.index1()) { ++r_it2; }
00046           if (c.index2() == r_it2.index1()) {
00047                 cout << r_it2.index1() << ", " << c.index2() << ": " << (*c) << " += " << (*r_it2);
00048                 // working version:
00049                 //   temp = (*r_it2);
00050                 //   (*c) += temp;
00051                 // also working version:
00052                 (*c) += (*r_it2);
00053                 cout << " --> " << (*c) << " ? \n";
00054                 ++ r_it2;
00055                 ++ c;
00056           }
00057         }
00058         ++ r_it1;
00059         ++ c_it1;
00060   }
00061 
00062   cout << "output M = " << M << endl;
00063   cout << "expected output M = [4,4]((6,-2,-4,0),(-3,8,-4,0),(-6,-6,18,0),(0,0,0,2))" << endl;
00064 
00065   return EXIT_SUCCESS;
00066 };

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