Mtx Class Reference

#include <yang.h>

List of all members.

Public Member Functions

 Mtx (int n, int m, double **)
 Mtx (int n, int m, double d=0)
 Mtx (const Mtx &)
 ~Mtx ()
Mtxoperator= (const Mtx &)
Mtxoperator+= (const Mtx &)
Mtxoperator-= (const Mtx &)
Vtr operator* (const Vtr &) const
double * operator[] (int i) const
Mtxoperator+ ()
Mtx operator+ (const Mtx &)
int rows () const
int cols () const
void getcol (int i, Vtr &vec) const
void setcol (int i, const Vtr &vec)
void clear ()
int transpose (Mtx &dest) const
Mtx operator* (const Mtx &) const
const double & operator() (int i, int j) const
double & operator() (int i, int j)
int QRdecomp (Mtx &Q, Mtx &R)
int QRdecomp_slow (Mtx &Q, Mtx &R)

Private Attributes

int nrows
int ncols
double ** ets

Friends

Mtx operator- (const Mtx &)
Mtx operator- (const Mtx &, const Mtx &)
Vtr operator* (const Vtr &, const Mtx &)
std::ostream & operator<< (std::ostream &, const Mtx &)

Detailed Description

Definition at line 48 of file yang.h.


Constructor & Destructor Documentation

Mtx::Mtx ( int  n,
int  m,
double **  dbp 
)

Definition at line 137 of file yang.cc.

00137                                    {         // construct from double pointer
00138   nrows = n;
00139   ncols = m;
00140   ets = new double* [nrows];
00141   for (int i =  0; i < nrows; i++) {
00142     ets[i] = new double [ncols];
00143     for (int j = 0; j < ncols; j++) ets[i][j] = dbp[i][j];
00144   }
00145 }

Mtx::Mtx ( int  n,
int  m,
double  d = 0 
)

Definition at line 147 of file yang.cc.

00147                                {              // construct from a double
00148   ets = new double* [nrows = n];
00149   for (int i =  0; i< nrows; i++) {
00150     ets[i] = new double [ncols = m];
00151     for (int j = 0; j < ncols; j++) ets[i][j] = a;
00152   }
00153 }

Mtx::Mtx ( const Mtx mat  ) 

Definition at line 155 of file yang.cc.

00155                         {                      // copy constructor
00156   ets = new double* [nrows = mat.nrows];
00157   for (int i =  0; i< nrows; i++) {
00158     ets[i] = new double [ncols = mat.ncols];
00159     for (int j = 0; j < ncols; j++) ets[i][j] = mat[i][j];
00160   }
00161 }

Mtx::~Mtx (  ) 

Definition at line 163 of file yang.cc.

00163          {                                    // destructor
00164   for (int i = 0; i< nrows; i++) delete[]  ets[i];
00165   delete[] ets;
00166 }


Member Function Documentation

void Mtx::clear (  ) 

Definition at line 251 of file yang.cc.

Referenced by QRdecomp(), and QRdecomp_slow().

00252 {
00253         for (int i = 0; i < nrows; i++)
00254                 for (int j = 0; j < ncols; j++)
00255                         ets[i][j] = 0;
00256 }

int Mtx::cols (  )  const [inline]

Definition at line 74 of file yang.h.

Referenced by operator<<(), and transpose().

00074 {return ncols;}

void Mtx::getcol ( int  i,
Vtr vec 
) const

Definition at line 235 of file yang.cc.

00236 {
00237         if (vec.size() != nrows)
00238                 error("Mtx::getcol(): Bad vector size.");
00239         for (int i = 0; i < nrows; i++)
00240                 vec[i] = ets[i][n];
00241 }

double& Mtx::operator() ( int  i,
int  j 
) [inline]

Definition at line 82 of file yang.h.

00082 {return ets[i][j];}

const double& Mtx::operator() ( int  i,
int  j 
) const [inline]

Definition at line 81 of file yang.h.

00081 {return ets[i][j];}

Mtx Mtx::operator* ( const Mtx mat  )  const

Definition at line 287 of file yang.cc.

00288 {
00289         Mtx tmp(nrows, mat.ncols);
00290 
00291         if (ncols != mat.nrows)
00292                 error("Mtx::op*=: Bad matrix sizes.");
00293 
00294         for (int i = 0; i < nrows; i++)
00295                 for (int j = 0; j < mat.ncols; j++)
00296                         for (int k = 0; k < ncols; k++)
00297                                 tmp(i,j) += ets[i][k] * mat.ets[k][j];
00298 
00299         return tmp;
00300 }

Vtr Mtx::operator* ( const Vtr v  )  const

Definition at line 205 of file yang.cc.

00205                                      {        // matrix-vector multiply
00206   if (ncols != v.size())
00207           error("Mtx::op*(Vtr): Error: Mat. and vec. sizes do not match.");
00208   Vtr tm(nrows);
00209   for (int i = 0; i < nrows; i++)
00210     for (int j = 0; j < ncols; j++) tm[i] += ets[i][j]*v[j];
00211   return tm;
00212 }

Mtx Mtx::operator+ ( const Mtx mat  ) 

Definition at line 199 of file yang.cc.

00199                                   {           // usage: m = m1 + m2
00200  Mtx sum = *this;                               // user-defined copy constructor
00201  sum += mat;                                    // is important here
00202  return sum;                                    // otherwise m1 would be changed
00203 }

Mtx & Mtx::operator+ (  ) 

Definition at line 191 of file yang.cc.

00191                     {                         // usage: mat1 = + mat2;
00192   return *this;
00193 }

Mtx & Mtx::operator+= ( const Mtx mat  ) 

Definition at line 177 of file yang.cc.

00177                                     {         // add-assign
00178   if (nrows != mat.nrows || ncols != mat.ncols) error("bad matrix sizes");
00179   for (int i = 0; i < nrows; i++)
00180     for (int j = 0; j < ncols; j++) ets[i][j] += mat[i][j];
00181   return *this;
00182 }

Mtx & Mtx::operator-= ( const Mtx mat  ) 

Definition at line 184 of file yang.cc.

00184                                     {         // subtract-assign
00185   if (nrows != mat.nrows || ncols != mat.ncols) error("bad matrix sizes");
00186   for (int i = 0; i < nrows; i++)
00187     for (int j = 0; j < ncols; j++) ets[i][j] -= mat[i][j];
00188   return *this;
00189 }

Mtx & Mtx::operator= ( const Mtx mat  ) 

Definition at line 168 of file yang.cc.

00168                                   {           // copy assignment
00169   if (this != &mat) {
00170     if (nrows != mat.nrows || ncols != mat.ncols) error("bad matrix sizes");
00171     for (int i = 0; i < nrows; i++)
00172       for (int j = 0; j < ncols; j++) ets[i][j]  = mat[i][j];
00173   }
00174   return *this;
00175 }

double* Mtx::operator[] ( int  i  )  const [inline]

Definition at line 63 of file yang.h.

00063 { return ets[i]; }      // subscripting, row i

int Mtx::QRdecomp ( Mtx Q,
Mtx R 
)

Definition at line 302 of file yang.cc.

00303 {
00304         if ((Q.nrows != nrows) || (Q.ncols != ncols) ||
00305             (R.nrows != ncols) || (R.ncols != ncols))   {
00306                 std::cerr << "Mtx::QRdecomp(): Error: Bad matrix size.";
00307                 return -1;
00308         }
00309 
00310         double** tmp;
00311         double norm, dot;
00312 
00313         // tmp = A transpose
00314         int tmprows = ncols;
00315         int tmpcols = nrows;
00316         tmp = new double* [tmprows];
00317         for (int i=0; i<tmprows; i++) {
00318                 tmp[i] = new double [tmpcols];
00319                 for (int j=0; j<tmpcols; j++)
00320                         tmp[i][j] = ets[j][i];
00321         }
00322         Mat_DP QT(tmprows,tmpcols);
00323 
00324         R.clear();
00325 
00326         for (int i=0; i<tmprows; i++) {
00327                 norm = 0;
00328                 for (int k=0; k<tmpcols; k++)
00329                         norm += tmp[i][k]*tmp[i][k];
00330                 norm = std::sqrt(norm);
00331                 R.ets[i][i] = norm;
00332                 for (int k=0; k<tmpcols; k++)
00333                         QT.ets[i][k] = tmp[i][k] / norm;
00334 
00335                 for (int j=i+1; j<tmprows; j++) {
00336                         dot = 0;
00337                         for (int k=0; k<tmpcols; k++)
00338                                 dot += QT.ets[i][k]*tmp[j][k];
00339                         R.ets[i][j] = dot;
00340                         for (int k=0; k<tmpcols; k++)
00341                                 tmp[j][k] = tmp[j][k] - dot*QT.ets[i][k];
00342                 }
00343         }
00344 
00345         QT.transpose(Q);
00346 
00347         for (int i=0; i<tmprows; i++)
00348                 delete[] tmp[i];
00349         return 0;
00350 }

int Mtx::QRdecomp_slow ( Mtx Q,
Mtx R 
)

Definition at line 352 of file yang.cc.

00353 {
00354         if ((Q.nrows != nrows) || (Q.ncols != ncols) ||
00355             (R.nrows != ncols) || (R.ncols != ncols))   {
00356                 std::cerr << "Mtx::QRdecomp(): Error: Bad matrix size.";
00357                 return -1;
00358         }
00359 
00360         double** tmp;
00361         double norm, dot;
00362 
00363         tmp = new double* [nrows];
00364         for (int i=0; i<nrows; i++) {
00365                 tmp[i] = new double [ncols];
00366                 for (int j=0; j<ncols; j++)
00367                         tmp[i][j] = ets[i][j];
00368         }
00369 
00370         R.clear();
00371 
00372         for (int i=0; i<ncols; i++) {
00373                 norm = 0;
00374                 for (int k=0; k<nrows; k++)
00375                         norm += tmp[k][i]*tmp[k][i];
00376                 norm = std::sqrt(norm);
00377                 R.ets[i][i] = norm;
00378                 for (int k=0; k<nrows; k++)
00379                         Q.ets[k][i] = tmp[k][i] / norm;
00380 
00381                 for (int j=i+1; j<ncols; j++) {
00382                         dot = 0;
00383                         for (int k=0; k<nrows; k++)
00384                                 dot += Q.ets[k][i]*tmp[k][j];
00385                         R.ets[i][j] = dot;
00386                         for (int k=0; k<nrows; k++)
00387                                 tmp[k][j] = tmp[k][j] - dot*Q.ets[k][i];
00388                 }
00389         }
00390 
00391         for (int i=0; i<nrows; i++)
00392                 delete[] tmp[i];
00393         return 0;
00394 }

int Mtx::rows (  )  const [inline]

Definition at line 73 of file yang.h.

Referenced by operator<<(), and transpose().

00073 {return nrows;}

void Mtx::setcol ( int  i,
const Vtr vec 
)

Definition at line 243 of file yang.cc.

00244 {
00245         if (vec.size() != nrows)
00246                 error("Mtx::getcol(): Bad vector size.");
00247         for (int i = 0; i < nrows; i++)
00248                 ets[i][n] = vec[i];
00249 }

int Mtx::transpose ( Mtx dest  )  const

Definition at line 258 of file yang.cc.

Referenced by QRdecomp().

00259 {
00260         if ((nrows != dest.cols()) || (ncols != dest.rows())) {
00261                 std::cerr << "Mtx::transpose(): Error: Matrix dim."
00262                           << std::endl;
00263                 return -1;
00264         }
00265 
00266         for (int i=0; i<nrows; i++)
00267                 for (int j=0; j<ncols; j++)
00268                         dest(j, i) = ets[i][j];
00269         return 0;
00270 }


Friends And Related Function Documentation

Vtr operator* ( const Vtr v,
const Mtx mat 
) [friend]

Definition at line 214 of file yang.cc.

00215 {
00216         if (v.lenth != mat.nrows)
00217                 error("op*(Vtr, Mtx): Error: Mat. and vec. size do no match.");
00218         Vtr res(mat.ncols, 0.0);
00219         for (int i=0; i<mat.ncols; i++)
00220                 for (int j=0; j<v.lenth; j++)
00221                         res[i] = v.ets[j] * mat.ets[j][i];
00222         return res;
00223 }

Mtx operator- ( const Mtx m1,
const Mtx m2 
) [friend]

Definition at line 226 of file yang.cc.

00226                                              {  // matrix subtract
00227  if(m1.nrows !=m2.nrows || m1.ncols !=m2.ncols)
00228    error("bad matrix sizes");
00229  Mtx sum = m1;
00230  sum -= m2;
00231  return sum;
00232 }

Mtx operator- ( const Mtx mat  )  [friend]

Definition at line 195 of file yang.cc.

00195                                 {               // usage: mat1 = - mat2;
00196   return Mtx(mat.nrows,mat.ncols) - mat;
00197 }

std::ostream& operator<< ( std::ostream &  s,
const Mtx mat 
) [friend]

Definition at line 272 of file yang.cc.

00273 {
00274         for (int i = 0; i < mat.rows(); i++) {
00275                 s << "| ";
00276                 for(int j = 0; j < mat.cols(); j++) {
00277                         s.setf(std::ios_base::fixed, std::ios_base::floatfield);
00278                         s.precision(4);
00279                         s.width(8);
00280                         s << mat.ets[i][j];
00281                 }
00282                 s << " |" << std::endl;
00283         }
00284         return s;
00285 }


Member Data Documentation

double** Mtx::ets [private]
int Mtx::ncols [private]
int Mtx::nrows [private]

The documentation for this class was generated from the following files:
Generated on Wed May 26 16:21:18 2010 for OverSim by  doxygen 1.6.3