matrix_op.h

Go to the documentation of this file.
00001 #ifndef MATRIX_OP_H 
00002 #define MATRIX_OP_H
00003 
00004 #include "TestTimer.h"
00005 
00006 // define functions for matrix operations
00007 
00008 //#define DEBUG
00009 //#ifndef NLOOP
00010 //#define NLOOP 1000000
00011 //#endif
00012 #include <vector>
00013 
00014 using namespace ROOT::Math;
00015 
00016 std::vector<float> gV; 
00017 
00018 void initValues() { 
00019   gV.reserve(10*NLOOP); 
00020   TRandom3 r; 
00021   std::cout << "init smearing vector ";
00022   for (int l = 0; l < 10*NLOOP; l++)    
00023     {
00024       gV.push_back( r.Rndm() );  
00025     } 
00026   std::cout << " with size  " << gV.size() << std::endl;
00027 
00028 }
00029 
00030 
00031 // vector assignment
00032 template<class V> 
00033 void testVeq(const V & v, double & time, V & result) {  
00034   V vtmp = v; 
00035   test::Timer t(time,"V=V ");
00036   for (int l = 0; l < 10*NLOOP; l++)    
00037     {
00038       vtmp[0] = gV[l];
00039       result = vtmp;  
00040     }
00041 }
00042 
00043 // matrix assignmnent
00044 template<class M> 
00045 void testMeq(const M & m, double & time, M & result) {  
00046   M mtmp = m;
00047   test::Timer t(time,"M=M ");
00048   for (int l = 0; l < NLOOP; l++)       
00049     {
00050       mtmp(0,0) = gV[l];
00051       result = mtmp;  
00052     }
00053 }
00054 
00055 
00056 
00057 // vector sum 
00058 template<class V> 
00059 void testVad(const V & v1, const V & v2, double & time, V & result) {  
00060   V vtmp = v2; 
00061   test::Timer t(time,"V+V ");
00062   for (int l = 0; l < 10*NLOOP; l++)    
00063     {
00064       vtmp[0] = gV[l]; 
00065       result = v1 + vtmp;  
00066     }
00067 }
00068 
00069 // matrix sum 
00070 template<class M> 
00071 void testMad(const M & m1, const M & m2, double & time, M & result) {  
00072   M mtmp = m2;
00073   test::Timer t(time,"M+M ");;
00074   for (int l = 0; l < NLOOP; l++)       
00075     {
00076       mtmp(0,0) = gV[l]; 
00077       result = m1; result += mtmp;  
00078       //M tmp = m1 + mtmp;
00079       //result = tmp;  
00080     }
00081 }
00082 
00083 // vector * constant
00084 template<class V> 
00085 void testVscale(const V & v1, double a, double & time, V & result) {  
00086   V vtmp = v1; 
00087   test::Timer t(time,"a*V ");;
00088   for (int l = 0; l < NLOOP; l++)       
00089     {
00090       vtmp[0] = gV[l];
00091       result = a * vtmp;   // v1 * a does not exist in ROOT   
00092     }
00093 }
00094 
00095 
00096 // matrix * constant
00097 template<class M> 
00098 void testMscale(const M & m1, double a, double & time, M & result) {  
00099   M mtmp = m1;
00100   test::Timer t(time,"a*M ");;
00101   for (int l = 0; l < NLOOP; l++)       
00102     {
00103       mtmp(0,0) = gV[l];
00104       //result = mtmp * a;  
00105       result = mtmp; result *= a;
00106     }
00107 }
00108 
00109 
00110 // simple Matrix vector op
00111 template<class M, class V> 
00112 void testMV(const M & mat, const V & v, double & time, V & result) {  
00113   V vtmp = v; 
00114   test::Timer t(time,"M*V ");
00115   for (int l = 0; l < NLOOP; l++)       
00116     {
00117       vtmp[0] = gV[l];
00118       result = mat * vtmp;  
00119     }
00120 }
00121 
00122 // general matrix vector op
00123 template<class M, class V> 
00124 void testGMV(const M & mat, const V & v1, const V & v2, double & time, V & result) {  
00125   V vtmp = v1; 
00126   test::Timer t(time,"M*V+");
00127   for (int l = 0; l < NLOOP; l++)       
00128     {
00129       vtmp[0] = gV[l];
00130       result = mat * vtmp + v2; 
00131     }
00132 }
00133 
00134 
00135 // general matrix matrix op 
00136 template<class A, class B, class C> 
00137 void testMM(const A & a, const B & b, const C & c, double & time, C & result) {  
00138   B btmp = b; 
00139   test::Timer t(time,"M*M ");
00140   for (int l = 0; l < NLOOP; l++)       
00141     {
00142       btmp(0,0) = gV[l];
00143       result = a * btmp + c;
00144     }
00145 }
00146 
00147 
00148 
00149 
00150 // specialized functions (depending on the package) 
00151 
00152 //smatrix
00153 template<class V> 
00154 double testDot_S(const V & v1, const V & v2, double & time) {  
00155   V vtmp = v2; 
00156   test::Timer t(time,"dot ");
00157   double result=0; 
00158   for (int l = 0; l < 10*NLOOP; l++)    
00159     {
00160       vtmp[0] = gV[l];
00161       result = Dot(v1,vtmp);  
00162     }
00163   return result; 
00164 }
00165 
00166 
00167 // double testDot_S(const std::vector<V*> & w1, const std::vector<V*> & w2, double & time) {  
00168 //   test::Timer t(time,"dot ");
00169 //   double result=0; 
00170 //   for (int l = 0; l < NLOOP; l++)    
00171 //     {
00172 //       V & v1 = *w1[l]; 
00173 //       V & v2 = *w2[l]; 
00174 //       result = Dot(v1,v2);  
00175 //     }
00176 //   return result; 
00177 // }
00178 
00179 template<class M, class V> 
00180 double testInnerProd_S(const M & a, const V & v, double & time) {  
00181   V vtmp = v; 
00182   test::Timer t(time,"prod");
00183   double result=0; 
00184   for (int l = 0; l < NLOOP; l++)       
00185     {
00186       vtmp[0] =  gV[l];
00187       result = Similarity(vtmp,a);  
00188     }
00189   return result; 
00190 }
00191 
00192 //inversion
00193 template<class M> 
00194 void  testInv_S( const M & m,  double & time, M& result){
00195   M mtmp = m;
00196   test::Timer t(time,"inv ");
00197   int ifail = 0;
00198   for (int l = 0; l < NLOOP; l++)       
00199     {
00200       mtmp(0,0) = gV[l]; 
00201       //result = mtmp.InverseFast(ifail);
00202       result = mtmp.Inverse(ifail);
00203       // assert(ifail == 0);
00204     }
00205 }
00206 
00207 
00208 // general matrix matrix op
00209 template<class A, class B, class C> 
00210 void testATBA_S(const A & a, const B & b, double & time, C & result) {  
00211   B btmp = b;
00212   test::Timer t(time,"At*M*A");
00213   for (int l = 0; l < NLOOP; l++)       
00214     {
00215       //result = Transpose(a) * b * a;  
00216       //result = a * b * Transpose(a);  
00217       //result = a * b * a;  
00218       btmp(0,0) = gV[l];
00219       //      A at = Transpose(a);
00220       C tmp = btmp * Transpose(a);
00221       result = a * tmp; 
00222     }
00223 }
00224 
00225 // general matrix matrix op
00226 template<class A, class B, class C> 
00227 void testATBA_S2(const A & a, const B & b, double & time, C & result) {  
00228   B btmp = b;
00229 
00230   test::Timer t(time,"At*M*A");
00231   for (int l = 0; l < NLOOP; l++)       
00232     {
00233       //result = Transpose(a) * b * a;  
00234       //result = a * b * Transpose(a);  
00235       //result = a * b * a;  
00236       btmp(0,0) = gV[l];
00237       result  = SimilarityT(a,b);
00238       //result = a * result; 
00239     }
00240 }
00241 
00242 template<class A, class C> 
00243 void testMT_S(const A & a, double & time, C & result) {  
00244   A atmp = a;
00245   test::Timer t(time,"Transp");
00246   for (int l = 0; l < NLOOP; l++)       
00247     {
00248       //result = Transpose(a) * b * a;  
00249       //result = a * b * Transpose(a);  
00250       //result = a * b * a;  
00251       atmp(0,0) = gV[l];
00252       result  = Transpose(atmp);
00253     }
00254 }
00255 
00256 /////////////////////////////////////
00257 // for root
00258 //////////////////////////////////
00259 
00260 // simple Matrix vector op
00261 template<class M, class V> 
00262 void testMV_T(const M & mat, const V & v, double & time, V & result) {
00263   V vtmp = v; 
00264   test::Timer t(time,"M*V ");
00265   for (int l = 0; l < NLOOP; l++)
00266     {
00267       vtmp[0] = gV[l];
00268       Add(result,0.0,mat,vtmp);
00269     }
00270 } 
00271   
00272 // general matrix vector op
00273 template<class M, class V> 
00274 void testGMV_T(const M & mat, const V & v1, const V & v2, double & time, V & result) {
00275   V vtmp = v1;
00276   test::Timer t(time,"M*V+");
00277   for (int l = 0; l < NLOOP; l++)
00278     {
00279       vtmp[0] = gV[l];
00280       memcpy(result.GetMatrixArray(),v2.GetMatrixArray(),v2.GetNoElements()*sizeof(Double_t));
00281       Add(result,1.0,mat,vtmp);
00282     }
00283 }
00284 
00285 // general matrix matrix op
00286 template<class A, class B, class C> 
00287 void testMM_T(const A & a, const B & b, const C & c, double & time, C & result) {
00288   B btmp = b; 
00289   test::Timer t(time,"M*M ");
00290   for (int l = 0; l < NLOOP; l++)
00291     {
00292       btmp(0,0) = gV[l];
00293       result.Mult(a,btmp);
00294       result += c;
00295     }
00296 } 
00297 
00298 // matrix sum
00299 template<class M> 
00300 void testMad_T(const M & m1, const M & m2, double & time, M & result) {
00301   M mtmp = m2;
00302   test::Timer t(time,"M+M ");
00303   for (int l = 0; l < NLOOP; l++)
00304     {
00305       mtmp(0,0) = gV[l];
00306       result.Plus(m1,mtmp);
00307     }
00308 }
00309 
00310 template<class A, class B, class C> 
00311 void testATBA_T(const A & a, const B & b, double & time, C & result) {
00312   B btmp = b;
00313   test::Timer t(time,"At*M*A");
00314   C tmp = a;
00315   for (int l = 0; l < NLOOP; l++)
00316     {
00317       btmp(0,0) = gV[l]; 
00318       tmp.Mult(a,btmp);
00319       result.MultT(tmp,a);
00320     }
00321 }
00322 
00323 template<class V> 
00324 double testDot_T(const V & v1, const V & v2, double & time) {  
00325   V vtmp = v2;
00326   test::Timer t(time,"dot ");
00327   double result=0; 
00328   for (int l = 0; l < 10*NLOOP; l++)    
00329     {
00330       vtmp[0] = gV[l];
00331       result = Dot(v1,vtmp);
00332     }
00333   return result; 
00334 }
00335 
00336 template<class M, class V> 
00337 double testInnerProd_T(const M & a, const V & v, double & time) {  
00338   V vtmp = v; 
00339   test::Timer t(time,"prod");
00340   double result=0; 
00341   for (int l = 0; l < NLOOP; l++) { 
00342     vtmp[0] =  gV[l];
00343     result = a.Similarity(vtmp);
00344   }
00345   return result; 
00346 }
00347 
00348 //inversion 
00349 template<class M> 
00350 void  testInv_T(const M & m,  double & time, M& result){ 
00351   M mtmp = m;
00352   test::Timer t(time,"inv ");
00353   for (int l = 0; l < NLOOP; l++)       
00354     {
00355       mtmp(0,0) = gV[l]; 
00356       memcpy(result.GetMatrixArray(),mtmp.GetMatrixArray(),mtmp.GetNoElements()*sizeof(Double_t));
00357       result.InvertFast(); 
00358     }
00359 }
00360 
00361 template<class M> 
00362 void  testInv_T2(const M & m,  double & time, M& result){ 
00363   M mtmp = m;
00364   test::Timer t(time,"inv2");
00365   for (int l = 0; l < NLOOP; l++)       
00366     {
00367       memcpy(result.GetMatrixArray(),mtmp.GetMatrixArray(),mtmp.GetNoElements()*sizeof(Double_t));
00368       result.InvertFast();  
00369     }
00370 }
00371 
00372 
00373 // vector sum
00374 template<class V> 
00375 void testVad_T(const V & v1, const V & v2, double & time, V & result) {
00376   V vtmp = v2; 
00377   test::Timer t(time,"V+V ");;
00378   for (int l = 0; l < 10*NLOOP; l++)
00379     {
00380       vtmp[0] = gV[l];
00381       result.Add(v1,vtmp);
00382     }
00383 }
00384 
00385 // vector * constant
00386 template<class V> 
00387 void testVscale_T(const V & v1, double a, double & time, V & result) {
00388   V vtmp = v1; 
00389   test::Timer t(time,"a*V ");;
00390   for (int l = 0; l < NLOOP; l++)
00391     {
00392       // result = a * v1;
00393       result.Zero();
00394       vtmp[0] = gV[l];
00395       Add(result,a,vtmp);
00396     }
00397 }
00398 
00399 // general matrix matrix op
00400 template<class A, class B, class C> 
00401 void testATBA_T2(const A & a, const B & b, double & time, C & result) {  
00402   B btmp = b;
00403   test::Timer t(time,"At*M*A");
00404   for (int l = 0; l < NLOOP; l++)       
00405     {
00406       btmp(0,0) = gV[l]; 
00407       memcpy(result.GetMatrixArray(),btmp.GetMatrixArray(),btmp.GetNoElements()*sizeof(Double_t));
00408       result.Similarity(a); 
00409     }
00410 }
00411 
00412 // matrix * constant
00413 template<class M>
00414 void testMscale_T(const M & m1, double a, double & time, M & result) {
00415   M mtmp = m1;
00416   test::Timer t(time,"a*M ");;
00417   for (int l = 0; l < NLOOP; l++)
00418     {
00419       //result = a * m1;
00420       result.Zero();
00421       mtmp(0,0) = gV[l];
00422       Add(result,a,mtmp);
00423     }
00424 }
00425 
00426 template<class A, class C> 
00427 void testMT_T(const A & a, double & time, C & result) {  
00428   A atmp = a;
00429   test::Timer t(time,"Transp");
00430   for (int l = 0; l < NLOOP; l++)       
00431     {
00432       atmp(0,0) = gV[l];
00433       result.Transpose(atmp);
00434     }
00435 }
00436 
00437 //////////////////////////////////////////// 
00438 // for clhep
00439 ////////////////////////////////////////////
00440 
00441 //smatrix
00442 template<class V> 
00443 double testDot_C(const V & v1, const V & v2, double & time) {  
00444   V vtmp =  v2;
00445   test::Timer t(time,"dot ");
00446   double result=0; 
00447   for (int l = 0; l < 10*NLOOP; l++)    
00448     {
00449       vtmp[0] = gV[l];
00450       result = dot(v1,vtmp);  
00451     }
00452   return result; 
00453 }
00454 
00455 template<class M, class V> 
00456 double testInnerProd_C(const M & a, const V & v, double & time) {  
00457   V vtmp = v; 
00458   test::Timer t(time,"prod");
00459   double result=0; 
00460   for (int l = 0; l < NLOOP; l++)       
00461     {
00462       vtmp[0] = gV[l];
00463       V tmp = a*vtmp; 
00464       result = dot(vtmp,tmp);
00465     }
00466   return result; 
00467 }
00468 
00469 
00470 // matrix assignmnent(index starts from 1)
00471 template<class M> 
00472 void testMeq_C(const M & m, double & time, M & result) {  
00473   M mtmp = m;
00474   test::Timer t(time,"M=M ");
00475   for (int l = 0; l < NLOOP; l++)       
00476     {
00477       mtmp(1,1) = gV[l];
00478       result = mtmp;  
00479     }
00480 }
00481 
00482 // matrix sum 
00483 template<class M> 
00484 void testMad_C(const M & m1, const M & m2, double & time, M & result) {  
00485   M mtmp = m2;
00486   test::Timer t(time,"M+M ");;
00487   for (int l = 0; l < NLOOP; l++)       
00488     {
00489       mtmp(1,1) = gV[l]; 
00490       result = m1; result += mtmp;  
00491     }
00492 }
00493 
00494 
00495 // matrix * constant
00496 template<class M> 
00497 void testMscale_C(const M & m1, double a, double & time, M & result) {  
00498   M mtmp = m1;
00499   test::Timer t(time,"a*M ");;
00500   for (int l = 0; l < NLOOP; l++)       
00501     {
00502       mtmp(1,1) = gV[l];
00503       result = mtmp * a;  
00504     }
00505 }
00506 
00507 
00508 // clhep matrix matrix op (index starts from 1)
00509 template<class A, class B, class C> 
00510 void testMM_C(const A & a, const B & b, const C & c, double & time, C & result) {  
00511   B btmp = b; 
00512   test::Timer t(time,"M*M ");
00513   for (int l = 0; l < NLOOP; l++)       
00514     {
00515       btmp(1,1) = gV[l];
00516       result = a * btmp + c;
00517     }
00518 }
00519 
00520 
00521 //inversion
00522 template<class M> 
00523 void  testInv_C( const M & a,  double & time, M& result){ 
00524   M mtmp = a;
00525   test::Timer t(time,"inv ");
00526   int ifail = 0; 
00527   for (int l = 0; l < NLOOP; l++)       
00528     {
00529       mtmp(1,1) = gV[l]; 
00530       result = mtmp.inverse(ifail); 
00531       if (ifail) {std::cout <<"error inverting" << mtmp << std::endl; return; } 
00532     }
00533 }
00534 
00535 // general matrix matrix op
00536 template<class A, class B, class C> 
00537 void testATBA_C(const A & a, const B & b, double & time, C & result) {  
00538   B btmp = b;
00539   test::Timer t(time,"At*M*A");
00540   for (int l = 0; l < NLOOP; l++)       
00541     {
00542       btmp(1,1) = gV[l];
00543       //result = a.T() * b * a;  
00544       result = a * btmp * a.T();  
00545     }
00546 }
00547 
00548 
00549 template<class A, class B, class C> 
00550 void testATBA_C2(const A & a, const B & b, double & time, C & result) { 
00551   B btmp = b; 
00552   test::Timer t(time,"At*M*A");
00553   for (int l = 0; l < NLOOP; l++)       
00554     {
00555       btmp(1,1) = gV[l]; 
00556       result = btmp.similarity(a); 
00557     }
00558 }
00559 
00560 
00561 template<class A, class C> 
00562 void testMT_C(const A & a, double & time, C & result) {  
00563   A atmp = a;
00564   test::Timer t(time,"Transp");
00565   for (int l = 0; l < NLOOP; l++)       
00566     {
00567       atmp(1,1) = gV[l];
00568       result  = atmp.T();
00569     }
00570 }
00571 
00572 
00573 #endif   

Generated on Tue Jul 5 14:43:11 2011 for ROOT_528-00b_version by  doxygen 1.5.1