vlazy.cxx

Go to the documentation of this file.
00001 // @(#)root/test:$Id: vlazy.cxx 20882 2007-11-19 11:31:26Z rdm $
00002 // Author: Fons Rademakers   14/11/97
00003 
00004 //
00005 // Sample code showing off a few advanced features
00006 // and comparing them (time-wise) with traditional ones.
00007 //
00008 // Simple example: downsampling a matrix, that is, creating a matrix
00009 // that is 4 times (twice in each dimension) smaller than the original
00010 // matrix, by picking every other sample of the latter.
00011 //
00012 
00013 #include "TStopwatch.h"
00014 #include "TMatrix.h"
00015 #include "TMatrixFLazy.h"
00016 #include "Riostream.h"
00017 
00018 
00019 class do_downsample : public TElementPosActionF {
00020 private:
00021    const TMatrix &fOrigMatrix;
00022    const int row_lwb, col_lwb;
00023    void Operation(Real_t &element) const
00024        { element = fOrigMatrix((fI-row_lwb)*2+row_lwb,(fJ-col_lwb)*2+col_lwb); }
00025 public:
00026    do_downsample(const TMatrix &orig_matrix)
00027       : fOrigMatrix(orig_matrix),
00028         row_lwb(orig_matrix.GetRowLwb()),
00029         col_lwb(orig_matrix.GetColLwb()) { }
00030 };
00031 
00032 // Downsample matrix - new style
00033 class downsample_matrix : public TMatrixFLazy {
00034 private:
00035    const TMatrix &fOrigMatrix;
00036    void FillIn(TMatrixF &m) const;
00037 public:
00038   downsample_matrix(const TMatrix &orig_matrix);
00039 };
00040 
00041 // Just figure out the dimensions of the downsampled (lazy) matrix
00042 downsample_matrix::downsample_matrix(const TMatrix &orig_matrix)
00043         : TMatrixFLazy(orig_matrix.GetRowLwb(),
00044                       (orig_matrix.GetNrows()+1)/2 + orig_matrix.GetRowLwb()-1,
00045                       orig_matrix.GetColLwb(),
00046                       (orig_matrix.GetNcols()+1)/2 + orig_matrix.GetColLwb()-1),
00047                       fOrigMatrix(orig_matrix)
00048 { }
00049 
00050 // "construct" the new matrix (when the lazy matrix is being "rolled out")
00051 void downsample_matrix::FillIn(TMatrixF &m) const
00052 {
00053    do_downsample d(fOrigMatrix);
00054    m.Apply(d);
00055 }
00056 
00057 // Downsample in the traditional style
00058 static TMatrix traditional_downsampling(const TMatrix &orig_matrix)
00059 {
00060    TMatrix smaller_m(orig_matrix.GetRowLwb(),
00061                      (orig_matrix.GetNrows()+1)/2 + orig_matrix.GetRowLwb()-1,
00062                      orig_matrix.GetColLwb(),
00063                      (orig_matrix.GetNcols()+1)/2 + orig_matrix.GetColLwb()-1);
00064 
00065    for (int i = 0; i < smaller_m.GetNrows(); i++)
00066       for (int j = 0; j < smaller_m.GetNcols(); j++)
00067          smaller_m(i+smaller_m.GetRowLwb(),j+smaller_m.GetColLwb()) =
00068          orig_matrix(2*i+smaller_m.GetRowLwb(),2*j+smaller_m.GetColLwb());
00069    return smaller_m;
00070 }
00071 
00072 int main()
00073 {
00074    cout << "\nDownsample matrices using traditional and non-traditional methods"
00075         << endl;
00076 
00077    TStopwatch sw;
00078 
00079    {
00080       cout << "\nMake sure that both methods give the same results" << endl;
00081       TMatrix orig_m = THaarMatrixF(9,201);   // which is a pretty big matrix
00082       TMatrix small1 = traditional_downsampling(orig_m);
00083       TMatrix small2 = downsample_matrix(orig_m);
00084       R__ASSERT( small1 == small2 );
00085    }
00086 
00087    {
00088       cout << "\nClock the traditional downsampling" << endl;
00089       sw.Start();
00090       for (int order = 1; order <= 10; order++) {
00091          TMatrix orig_m = THaarMatrixF(order);   // may be pretty big, btw
00092          for (int count = 0; count < (1<<(12-order)); count++) {
00093             TMatrix small = traditional_downsampling(orig_m);
00094             small(0,0) = 1;                     // just to use the matrix
00095          }
00096       }
00097       cout << "\tIt took " << sw.RealTime()
00098            << " sec to complete the test" << endl;
00099    }
00100 
00101    {
00102       cout << "\nClock the 'new style' downsampling (with lazy matrices)"<< endl;
00103       sw.Start();
00104       for (int order = 1; order <= 10; order++) {
00105          TMatrix orig_m = THaarMatrixF(order);     // may be pretty big, btw
00106          for (int count = 0; count < (1<<(12-order)); count++) {
00107             TMatrix small = downsample_matrix(orig_m);
00108             small(0,0) = 1;                       // just to use the matrix
00109          }
00110       }
00111       cout << "\tIt took " << sw.RealTime()
00112            << " sec to complete the test" << endl;
00113    }
00114    return 0;
00115 }

Generated on Tue Jul 5 15:16:34 2011 for ROOT_528-00b_version by  doxygen 1.5.1