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

sparse_fill2.cpp

Go to the documentation of this file.
00001 // -*- c++ -*- //
00002 /** \file main.cpp  \brief main routines */
00003 /***************************************************************************
00004  -   begin                : 2003-09-05
00005  -   copyright            : (C) 2003 by Gunter Winkler
00006  -   email                : guwi17@gmx.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include <iostream>
00019 
00020 #include <boost/timer.hpp>
00021 
00022 #include <boost/random.hpp>
00023 #include <boost/random/mersenne_twister.hpp>
00024 
00025 #include <boost/numeric/ublas/matrix.hpp>
00026 #include <boost/numeric/ublas/matrix_sparse.hpp>
00027 #include <boost/numeric/ublas/matrix_proxy.hpp>
00028 #include <boost/numeric/ublas/banded.hpp>
00029 #include <boost/numeric/ublas/vector.hpp>
00030 #include <boost/numeric/ublas/vector_proxy.hpp>
00031 #include <boost/numeric/ublas/vector_sparse.hpp>
00032 #include <boost/numeric/ublas/vector_of_vector.hpp>
00033 #include <boost/numeric/ublas/vector_expression.hpp>
00034 #include <boost/numeric/ublas/io.hpp>
00035 #include <boost/numeric/ublas/operation.hpp>
00036 #include <boost/numeric/ublas/lu.hpp>
00037 
00038 
00039 #ifndef NDEBUG
00040 const size_t default_size = 100;
00041 #else
00042 const size_t default_size = 1000;
00043 #endif
00044 
00045 const size_t subsize = 3;
00046 
00047 using namespace boost::numeric::ublas ;
00048 
00049 using boost::mt19937 ;
00050 
00051 using std::cout;
00052 using std::endl;
00053 
00054 template <class MAT>
00055 void test_matrix_fill_plus_assign(MAT& A)
00056 {
00057   typedef typename MAT::size_type  size_type;
00058 
00059   boost::numeric::ublas::vector<size_type>    index(subsize);
00060 
00061   mt19937   mtrand;
00062 
00063   size_type  size = A.size1();
00064   size_type count = size * 5;
00065 
00066   for (size_type n=0; n<count; ++n)     {
00067         for (size_type i=0; i<subsize; ++i) {
00068           size_type  r = mtrand();
00069           index(i) = size_type( (r * double(size)) / double(mtrand.max()) );
00070         }
00071         for (size_type i=0; i<subsize; ++i) {
00072           for (size_type j=0; j<subsize; ++j) {
00073                 A(index(i),index(j)) += (1.0);
00074           }
00075         }
00076   }
00077 
00078   double sum = 0.0;
00079   for (size_type i=0; i<subsize; ++i)
00080     sum += A(i,0);
00081   cout << "sum(column(A,0)) " << sum << endl;
00082 }
00083 
00084 template <class MAT>
00085 void test_matrix_fill_insert(MAT& A)
00086 {
00087   typedef typename MAT::size_type  size_type;
00088 
00089   boost::numeric::ublas::vector<size_type>    index(subsize);
00090 
00091   mt19937   mtrand;
00092 
00093   size_type  size = A.size1();
00094   size_type count = size * 5;
00095 
00096   for (size_type n=0; n<count; ++n)     {
00097         for (size_type i=0; i<subsize; ++i) {
00098           size_type  r = mtrand();
00099           index(i) = size_type( (r * double(size)) / double(mtrand.max()) );
00100         }
00101         for (size_type i=0; i<subsize; ++i) {
00102           for (size_type j=0; j<subsize; ++j) {
00103                 A.insert(index(i),index(j),1.0);
00104           }
00105         }
00106   }
00107 
00108   double sum = 0.0;
00109   for (size_type i=0; i<subsize; ++i)
00110     sum += A(i,0);
00111   cout << "sum(column(A,0)) " << sum << endl;
00112 }
00113 
00114 int main(int argc, char *argv[])
00115 {
00116 
00117   typedef size_t  size_type;
00118 
00119   size_type size = default_size;
00120 
00121   if (argc > 1)
00122         size = ::atoi (argv [1]);
00123 
00124   cout << "size " << size << endl;
00125 
00126   {
00127         coordinate_matrix<double, row_major> A(size,size);
00128         boost::timer t;
00129         t.restart();
00130         test_matrix_fill_insert(A);
00131         cout << "test_matrix_fill_insert coordinate_matrix: " << t.elapsed() << endl;
00132   }
00133 
00134   {
00135         generalized_vector_of_vector< double,
00136       row_major, vector<coordinate_vector<double> > > A(size,size);
00137         boost::timer t;
00138         t.restart();
00139         test_matrix_fill_insert(A);
00140         cout << "test_matrix_fill_insert vector_of_coordinate_vector: " << t.elapsed() << endl;
00141   }
00142 
00143 #ifdef SLOW
00144   {
00145         compressed_matrix<double, row_major> A(size,size);
00146         boost::timer t;
00147         t.restart();
00148         test_matrix_fill_plus_assign(A);
00149         cout << "test_matrix_fill_plus_assign compressed_matrix: " << t.elapsed() << endl;
00150   }
00151 #endif
00152 
00153   {
00154         generalized_vector_of_vector< double,
00155           row_major, vector<compressed_vector<double> > > A(size,size);
00156         boost::timer t;
00157         t.restart();
00158         test_matrix_fill_plus_assign(A);
00159         cout << "test_matrix_fill_plus_assign vector_of_compressed_vector: " << t.elapsed() << endl;
00160   }
00161 
00162 #ifdef SLOW
00163   {
00164         sparse_matrix<double, row_major> A(size,size);
00165         boost::timer t;
00166         t.restart();
00167         test_matrix_fill_plus_assign(A);
00168         cout << "test_matrix_fill_plus_assign sparse_matrix: " << t.elapsed() << endl;
00169   }
00170 #endif
00171 
00172   return EXIT_SUCCESS;
00173 
00174 }

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