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

sparse_fill.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(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   cout << "corner value " << A(0,0) << endl;
00079 }
00080 
00081 template <class T, class F, unsigned int IA, class IB, class TA>
00082 void test_matrix_fill(coordinate_matrix<T,F,IA,IB,TA>& A)
00083 {
00084   typedef coordinate_matrix<T,F,IA,IB,TA> MAT;
00085   typedef typename MAT::size_type  size_type;
00086   
00087   boost::numeric::ublas::vector<size_type>    index(subsize);
00088   
00089   mt19937   mtrand;
00090 
00091   size_type  size = A.size1();
00092   size_type count = size * 5;
00093 
00094   for (size_type n=0; n<count; ++n)     {
00095         for (size_type i=0; i<subsize; ++i) {
00096           size_type  r = mtrand();
00097           index(i) = size_type( (r * double(size)) / double(mtrand.max()) );
00098         }
00099         for (size_type i=0; i<subsize; ++i) {
00100           for (size_type j=0; j<subsize; ++j) {
00101                 A.insert(index(i),index(j),1.0);
00102           }
00103         }
00104   }
00105 
00106   cout << "corner value " << A(0,0) << endl;
00107 }
00108 
00109 int main(int argc, char *argv[])
00110 {
00111 
00112   typedef size_t  size_type;
00113 
00114   size_type size = default_size;
00115 
00116   if (argc > 1)
00117         size = ::atoi (argv [1]);
00118 
00119   cout << "size " << size << endl;
00120 
00121   {
00122         coordinate_matrix<double, row_major> A(size,size);
00123         boost::timer t;
00124         t.restart();
00125         test_matrix_fill(A);
00126         cout << "test_matrix_fill coordinate_matrix: " << t.elapsed() << endl;
00127   }
00128 
00129   {
00130         generalized_vector_of_vector< compressed_vector<double>,
00131           row_major, unbounded_array<compressed_vector<double> > > A(size,size);
00132         boost::timer t;
00133         t.restart();
00134         test_matrix_fill(A);
00135         cout << "test_matrix_fill vector_of_compressed_vector: " << t.elapsed() << endl;
00136   }
00137 
00138   if (size <= 3000) {
00139 
00140   {
00141         compressed_matrix<double, row_major> A(size,size);
00142         boost::timer t;
00143         t.restart();
00144         test_matrix_fill(A);
00145         cout << "test_matrix_fill compressed_matrix: " << t.elapsed() << endl;
00146   }
00147 
00148   {
00149         sparse_matrix<double, row_major> A(size,size);
00150         boost::timer t;
00151         t.restart();
00152         test_matrix_fill(A);
00153         cout << "test_matrix_fill sparse_matrix: " << t.elapsed() << endl;
00154   }
00155 
00156 
00157   }
00158 
00159   return EXIT_SUCCESS;
00160 
00161 }

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