Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages   Examples  

CTBdot.cxx

Go to the documentation of this file.
00001 
00006 /*----------------------------------------------------------------------------*/
00007 /* C Tool Box: Designed and implemented by:                                   */
00008 /*    Walter F.J. Mueller   Gesellschaft fuer Schwerionenforschung (GSI)      */
00009 /*                          Planckstrasse 1, D-64291 Darmstadt, Germany       */
00010 /*                  Email:  W.F.J.Mueller@gsi.de                              */
00011 /*                  WWW:    http://www-kp3.gsi.de/www/kp3/people/mueller.html */
00012 /*------------------------------------------+---------------------------------*/
00013 
00014 #include "CTB.hxx"
00015 #include "CTBtraits.hxx"
00016 #include "CTBnum.hxx"
00017 #include "CTBexceptionConformity.hxx"
00018 #include "CTBdot.hxx"
00019 
00020 //------------------------------------------+-----------------------------------
00028 template <class T>
00029 void CTBdot(CTBvector<T>& r, const CTBmatrix<T>& m, const CTBvector<T>& v)
00030 {
00031   CTBint   i_nr = m.NRow();                 // #rows of m
00032   CTBint   i_nc = m.NColumn();              // #cols of m
00033   const T* p_m  = m;                        // running ptr in m
00034   T*       p_r;                             // ptr to result vector
00035 
00036   if (i_nc != v.Size()) 
00037     throw CTBexceptionConformity(i_nc, v.Size(),
00038                                  "CTBdot(CTBvector&,CTBmatrix&,CTBvector&");
00039   
00040   if (r.Size() != i_nr) {                   // resize output if needed
00041     r.Resize(0);                            // zero first to avoid copy
00042     r.Resize(i_nr);
00043   }
00044   
00045   p_r  = r;
00046 
00047   for (CTBint i = 0; i < i_nr; i++) {       // loop over rows
00048     const T* p_v = v;
00049     CTBtraits<T>::Sum_t f_sum = 0.;
00050 
00051     for (CTBint j = 0; j < i_nc; j++) f_sum += *p_m++ * *p_v++;
00052     *p_r++ = f_sum;
00053   }
00054   return;
00055 }
00056 
00057 //------------------------------------------+-----------------------------------
00066 template <class T>
00067 void CTBdot(CTBmatrix<T>& r, const CTBmatrix<T>& m1, const CTBmatrix<T>& m2)
00068 {
00069   CTBint   i_nr = m1.NRow();                // #row of result
00070   CTBint   i_nc = m2.NColumn();             // #col of result
00071   CTBint   i_nk = m1.NColumn();             // length of dot products
00072   const T* p_m2 = m2;                       // ptr to current column in m2
00073   T*       p_r;                             // ptr to current column in r
00074 
00075   if (i_nk != m2.NRow()) 
00076     throw CTBexceptionConformity(i_nk, m2.NRow(),
00077                                  "CTBdot(CTBmatrix&,CTBmatrix&,CTBmatrix&");
00078   
00079   if (r.NRow() != i_nr || r.NColumn() != i_nc) {
00080     r.Resize(0,0);
00081     r.Resize(i_nr,i_nc);
00082   }
00083 
00084   p_r  = r;
00085 
00086   for (CTBint i = 0; i < i_nr; i++) {
00087     const T* p_m1 = m1;                     // running ptr in m1
00088     T*       p_vr = p_r;                    // running ptr to column in r
00089 
00090     for (CTBint j = 0; j < i_nc; j++) {
00091       const T* p_v  = p_m2;                 // running ptr to column in m2
00092       CTBtraits<T>::Sum_t f_sum = 0.;
00093 
00094       for (CTBint k = 0; k < i_nk; k++) {
00095         f_sum += *p_m1++ * *p_v;
00096         p_v   += i_nc;                      // to element in m2 column
00097       }
00098 
00099       *p_vr = f_sum;
00100       p_vr += i_nc;
00101     }
00102 
00103     p_m2++;                                 // point to next column in m2
00104     p_r++;                                  // point to next column in r
00105   }
00106   return;
00107 }
00108 

Generated at Fri Oct 24 18:11:28 2003 for CTBbase by doxygen1.2.9-20010812 written by Dimitri van Heesch, © 1997-2001