| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

MagMat.cpp

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <iomanip>
00003 #include "MagMat.h"
00004 
00005 
00006 MagMat::MagMat() {
00007 
00008   Ncol = 0; Nrow = 0; Nele = 0; 
00009   ptr_data = 0;  // set pointer to null
00010 
00011 }
00012 
00013 
00014 MagMat::MagMat(int N, int M) {
00015 
00016   Nrow = N; Ncol = M; Nele = N*M; 
00017   ptr_data = new double[Nele];
00018 
00019   for( int i=0; i<Nrow; i++ ) {
00020     for( int j=0; j<Ncol; j++ ) {
00021 
00022       *(ptr_data+i*Ncol+j) = 0.;
00023 
00024     } 
00025   }
00026 
00027 }
00028 
00029 MagMat::MagMat(const MagMat& m) {
00030   Nrow = m.Nrow; Ncol = m.Ncol; Nele = Nrow*Ncol; 
00031   ptr_data = new double[Nele];
00032   copy(m);
00033 }
00034 
00035 
00036 MagMat::~MagMat()
00037 {delete [] ptr_data;}
00038 
00039 
00040 void MagMat::copy(const MagMat&  m) {
00041 
00042   int n = Nrow <= m.Nrow ? Nrow : m.Nrow;
00043   int l = Ncol <= m.Ncol ? Ncol : m.Ncol;
00044 
00045   for( int i=0; i<n; i++ ) {
00046     for( int j=0; j<l; j++ ) {
00047 
00048       *(ptr_data+i*Ncol+j) = *(m.ptr_data+i*m.Ncol+j);
00049 
00050     } 
00051   }
00052 
00053 }
00054 
00055 
00056 
00057 
00058 MagMat&  MagMat::operator=(const MagMat& m) {
00059 
00060   if(( Nrow!=0 || Ncol!=0) && (Nrow != m.Nrow || Ncol != m.Ncol ))
00061      { std::cerr << "MagMat MagMat Assignment: size do not match!" << std::endl; return *this; }
00062 
00063   if ( ptr_data != m.ptr_data ) {
00064     reSize(m.Nrow, m.Ncol);
00065     copy(m);
00066   };
00067   return (*this);
00068 
00069 }
00070 
00071 
00072 
00073 MagMat MagMat::operator+(const MagMat& m) {
00074 
00075   if( Nrow != m.Nrow || Ncol != m.Ncol )
00076     { std::cerr << "operator+: size do not match!" << std::endl; return *this; }
00077 
00078   MagMat b( Nrow, Ncol );
00079 
00080   for( int i=0; i<Nrow; i++ ) {
00081     for( int j=0; j<Ncol; j++ ) {
00082 
00083       *(b.ptr_data+i*b.Ncol+j) = *(ptr_data+i*Ncol+j)+m[i][j];
00084 
00085     }
00086   }
00087 
00088   return b;
00089 } 
00090 
00091 MagMat MagMat::operator+(const MagMat& m) const {
00092 
00093   if( Nrow != m.Nrow || Ncol != m.Ncol )
00094     { std::cerr << "operator+: size do not match!" << std::endl; return *this; }
00095 
00096   MagMat b( Nrow, Ncol );
00097 
00098   for( int i=0; i<Nrow; i++ ) {
00099     for( int j=0; j<Ncol; j++ ) {
00100 
00101       *(b.ptr_data+i*b.Ncol+j) = *(ptr_data+i*Ncol+j)+m[i][j];
00102 
00103     }
00104   }
00105 
00106   return b;
00107 } 
00108 
00109 MagMat&  MagMat::operator+=(const MagMat& m) {
00110 
00111  if( Nrow != m.Nrow || Ncol != m.Ncol )
00112    { std::cerr << "operator+=: size do not match!" << std::endl; return *this; }
00113 
00114   for( int i=0; i<Nrow; i++ ) {
00115     for( int j=0; j<Ncol; j++ ) {
00116 
00117      *(ptr_data+i*Ncol+j)  += *(m.ptr_data+i*m.Ncol+j);
00118 
00119     };
00120   };
00121 
00122   return *this;
00123 }
00124 
00125 MagMat   MagMat::operator-(const MagMat& m) {
00126   if( Nrow != m.Nrow || Ncol != m.Ncol )
00127     { std::cerr << "operator-: size do not match!" << std::endl; return *this; }
00128 
00129   MagMat b( Nrow, Ncol );
00130 
00131   for( int i=0; i<Nrow; i++ ) {
00132     for( int j=0; j<Ncol; j++ ) {
00133 
00134       *(b.ptr_data+i*b.Ncol+j) = *(ptr_data+i*Ncol+j)-m[i][j];
00135 
00136     }
00137   }
00138 
00139   return b;
00140 }
00141 
00142 MagMat MagMat::operator-(const MagMat& m) const {
00143   if( Nrow != m.Nrow || Ncol != m.Ncol )
00144     { std::cerr << "operator-: size do not match!" << std::endl; return *this; }
00145 
00146   MagMat b( Nrow, Ncol );
00147 
00148   for( int i=0; i<Nrow; i++ ) {
00149     for( int j=0; j<Ncol; j++ ) {
00150 
00151       *(b.ptr_data+i*b.Ncol+j) = *(ptr_data+i*Ncol+j)-m[i][j];
00152 
00153     }
00154   }
00155 
00156   return b;
00157 }
00158 
00159 MagMat&  MagMat::operator-=(const MagMat& m) {
00160 
00161  if( Nrow != m.Nrow || Ncol != m.Ncol )
00162    { std::cerr << "operator-=: size do not match!" << std::endl; return *this; }
00163 
00164   for( int i=0; i<Nrow; i++ ) {
00165     for( int j=0; j<Ncol; j++ ) {
00166 
00167      *(ptr_data+i*Ncol+j)  -= *(m.ptr_data+i*m.Ncol+j);
00168 
00169     };
00170   };
00171 
00172   return *this;
00173 }
00174 
00175 MagMat MagMat::operator*(const MagMat& m) {
00176 
00177   if( Ncol != m.Nrow ) {  std::cerr << "operator*: size do not match!" << std::endl;
00178   return m; }
00179 
00180   MagMat b( Nrow, m.Ncol);
00181 
00182 
00183   for( int i=0; i<Nrow; i++ ) {
00184     for( int j=0; j<m.Ncol; j++ ) {
00185       for( int k=0; k<m.Nrow; k++) b[i][j]+= *(ptr_data+i*Ncol+k)*(m[k][j]);
00186     }
00187   }
00188 
00189   return b;
00190 
00191 }
00192 
00193 MagMat MagMat::operator*(const MagMat& m) const {
00194 
00195   if( Ncol != m.Nrow ) {  std::cerr << "operator*: size do not match!" << std::endl;
00196   return m; }
00197 
00198   MagMat b( Nrow, m.Ncol);
00199 
00200 
00201   for( int i=0; i<Nrow; i++ ) {
00202     for( int j=0; j<m.Ncol; j++ ) {
00203       for( int k=0; k<m.Nrow; k++) b[i][j]+= *(ptr_data+i*Ncol+k)*(m[k][j]);
00204     }
00205   }
00206 
00207   return b;
00208 
00209 }
00210 
00211 
00212 
00213 MagMat&   MagMat::operator*=(const double& d) {
00214 
00215  double*  p = ptr_data+Nele;
00216 
00217   while (p > ptr_data) *(--p) *= d;
00218 
00219   return *this;
00220 
00221 }
00222 
00223 
00224 
00225 // transposition
00226 
00227 
00228 MagMat MagMat::T() const {
00229   MagMat b(Ncol,Nrow);
00230   for( int i=0; i<b.Nrow; i++ ) {
00231     for( int j=0; j<b.Ncol; j++ ) {
00232        b[i][j]= *(ptr_data+j*Ncol+i);
00233     }
00234   }
00235   return b;
00236 }
00237 
00238 
00239 
00240 
00241 
00242 
00243 // reSize
00244 
00245 void MagMat::reSize(int Nnew, int Mnew) {
00246 
00247  
00248   if ( Nnew != Nrow || Mnew != Ncol ) {
00249 
00250     double*  p = ptr_data;
00251     Nele = Nnew*Mnew;
00252     ptr_data = new double[Nele];
00253     int Nrow_old = Nrow;
00254     int Ncol_old = Ncol;
00255 
00256     Nrow = Nnew;
00257     Ncol = Mnew;
00258     int k = Nrow <= Nrow_old ? Nrow : Nrow_old;
00259     int l = Ncol <= Ncol_old ? Ncol : Ncol_old;
00260 
00261     for( int i=0; i<k; i++ ) {
00262       for( int j=0; j<l; j++ ) {
00263         *(ptr_data+i*Ncol+j) = *(p+i*Ncol_old+j);
00264       }
00265     }
00266 
00267 
00268     delete [] p;
00269   }
00270 
00271 
00272 
00273 }
00274 
00275 
00276 
00277 int MagMat::nrow() const {
00278 
00279   return Nrow;
00280 
00281 }
00282 
00283 int MagMat::ncol() const {
00284 
00285   return Ncol;
00286 
00287 }
00288 
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Mon Apr 11 20:04:54 2011 for Magnet by doxygen 1.4.7