SPtMatrix< T > Class Template Reference

#include <SPT_Matrix.h>

Collaboration diagram for SPtMatrix< T >:

Collaboration graph
[legend]

List of all members.

Public Member Functions

void DisplayElements () const
Get (int, int) const
int GetNoOfCols () const
int GetNoOfRows () const
void GetNoOfRowsAndCols (int &, int &) const
SPtMatrix< T > GetTranspose () const
SPtMatrix< T > operator* (const SPtMatrix< T > &) const
SPtMatrix< T > & operator*= (double)
SPtMatrix< T > & operator+= (const SPtMatrix< T > &)
SPtMatrix< T > operator- () const
SPtMatrix< T > & operator-= (const SPtMatrix< T > &)
SPtMatrix< T > & operator/= (double)
SPtMatrix< T > & operator= (const SPtMatrix< T > &)
void Set (int, int, T)
bool SetToIdentity ()
 SPtMatrix (const SPtPoint3< T > &)
 SPtMatrix (const SPtVector3< T > &)
 SPtMatrix (const SPtMatrix< T > &)
 SPtMatrix (int=3, int=3)
SPtMatrix< T > & Transpose ()
 ~SPtMatrix ()

Public Attributes

int m_iCols
int m_iRows
T ** m_tElement

Private Member Functions

bool CheckRanges (int, int) const
void CopyElements (const SPtMatrix< T > &)
void DeleteMemory ()

Friends

class MatrixOperations
SPtMatrix< T > operator* (double s, const SPtMatrix< T > &M)
SPtMatrix< T > operator+ (const SPtMatrix< T > &A, const SPtMatrix< T > &B)
SPtMatrix< T > operator- (const SPtMatrix< T > &A, const SPtMatrix< T > &B)
ostream & operator<< (ostream &output, const SPtMatrix< T > &M)


Detailed Description

template<typename T>
class SPtMatrix< T >

Definition at line 33 of file SPT_Matrix.h.


Constructor & Destructor Documentation

template<typename T>
SPtMatrix< T >::SPtMatrix ( int  r = 3,
int  c = 3 
) [inline]

Definition at line 162 of file SPT_Matrix.h.

00162                                         : m_iRows( r ), m_iCols( c )
00163 {
00164     m_tElement = new T*[ m_iRows ];
00165     for ( r = 0; r < m_iRows; r++ ) {
00166         m_tElement[ r ] = new T[ m_iCols ];
00167         for ( c = 0; c < m_iCols; c++ ) {
00168             m_tElement[r][c] = T();
00169         }
00170     }
00171 }

template<typename T>
SPtMatrix< T >::SPtMatrix ( const SPtMatrix< T > &  M  )  [inline]

Definition at line 176 of file SPT_Matrix.h.

00177 {
00178     CopyElements( M );
00179 }

template<typename T>
SPtMatrix< T >::SPtMatrix ( const SPtVector3< T > &  V  )  [inline]

Definition at line 184 of file SPT_Matrix.h.

00185 {
00186     SPtMatrix( 3, 1 );
00187     this->m_tElement[0] = V.GetX();
00188     this->m_tElement[1] = V.GetY();
00189     this->m_tElement[2] = V.GetZ();
00190 }

template<typename T>
SPtMatrix< T >::SPtMatrix ( const SPtPoint3< T > &  P  )  [inline]

Definition at line 195 of file SPT_Matrix.h.

00196 {
00197     SPtMatrix( 3, 1 );
00198     this->m_tElement[0] = P.GetX();
00199     this->m_tElement[1] = P.GetY();
00200     this->m_tElement[2] = P.GetZ();
00201 }

template<typename T>
SPtMatrix< T >::~SPtMatrix (  )  [inline]

Definition at line 206 of file SPT_Matrix.h.

00207 {
00208     DeleteMemory();
00209 }


Member Function Documentation

template<typename T>
bool SPtMatrix< T >::CheckRanges ( int  r,
int  c 
) const [inline, private]

Definition at line 454 of file SPT_Matrix.h.

00455 {
00456     assert( 0 <= r && r < m_iRows );
00457     assert( 0 <= c && c < m_iCols );
00458 
00459     return true;
00460 }

template<typename T>
void SPtMatrix< T >::CopyElements ( const SPtMatrix< T > &  M  )  [inline, private]

Definition at line 476 of file SPT_Matrix.h.

00477 {
00478     int r, c;
00479 
00480     // copy the data members from M
00481     m_iRows = M.m_iRows;
00482     m_iCols = M.m_iCols;
00483     m_tElement = new T*[ m_iRows ];
00484     for ( r = 0; r < m_iRows; r++ ) {
00485         m_tElement[ r ] = new T[ m_iCols ];
00486         for ( c = 0; c < m_iCols; c++ ) {
00487             m_tElement[ r ][ c ] = M.m_tElement[ r ][ c ];
00488         }
00489     }
00490 }

template<typename T>
void SPtMatrix< T >::DeleteMemory (  )  [inline, private]

Definition at line 465 of file SPT_Matrix.h.

00466 {
00467     for ( int r = 0; r < m_iRows; r++ ) {
00468         delete [] m_tElement[ r ];
00469     }
00470     delete [] m_tElement;
00471 }

template<typename T>
void SPtMatrix< T >::DisplayElements (  )  const [inline]

Definition at line 216 of file SPT_Matrix.h.

00217 {
00218     int r, c;
00219 
00220     cout << "A(n) " << m_iRows << " x " << m_iCols << " Matrix \n";
00221     cout.precision(10);
00222     for ( r = 0; r < m_iRows; r++ ) {
00223         cout << "| ";
00224         for ( c = 0; c < m_iCols; c++ ) {
00225             cout.width( 20 );   cout << m_tElement[r][c];
00226         }
00227         cout << " |" << endl;
00228     }
00229     cout.precision(6);
00230 } // END Fn Member: DisplayElements()

template<typename T>
T SPtMatrix< T >::Get ( int  r,
int  c 
) const [inline]

Definition at line 236 of file SPT_Matrix.h.

00237 {
00238     if ( CheckRanges( r, c ) ) {
00239         return m_tElement[ r ][ c ];
00240     }
00241     else {
00242         return -777;
00243     }
00244 
00245 } // END Fn Member: Get()

template<typename T>
int SPtMatrix< T >::GetNoOfCols (  )  const [inline]

Definition at line 286 of file SPT_Matrix.h.

00287 {
00288     return m_iCols;
00289 } // END Fn Member: GetNoOfCols()

template<typename T>
int SPtMatrix< T >::GetNoOfRows (  )  const [inline]

Definition at line 278 of file SPT_Matrix.h.

00279 {
00280     return m_iRows;
00281 } // END Fn Member: GetNoOfRows()

template<typename T>
void SPtMatrix< T >::GetNoOfRowsAndCols ( int &  r,
int &  c 
) const [inline]

Definition at line 294 of file SPT_Matrix.h.

00295 {
00296     r = m_iRows;
00297     c = m_iCols;
00298 } // END Fn Member: GetNoOfRowsAndCols()

template<typename T>
SPtMatrix< T > SPtMatrix< T >::GetTranspose (  )  const [inline]

Definition at line 304 of file SPT_Matrix.h.

00305 {
00306     SPtMatrix< T > temp( m_iCols, m_iRows );
00307 
00308     for ( int r = 0; r < temp.m_iRows; r++ ) {
00309         for ( int c = 0; c < temp.m_iCols; c++ ) {
00310             temp.m_tElement[r][c] = this->m_tElement[c][r];
00311         }
00312     }
00313 
00314     return temp;
00315 }

template<typename T>
SPtMatrix< T > SPtMatrix< T >::operator* ( const SPtMatrix< T > &  M  )  const [inline]

Definition at line 415 of file SPT_Matrix.h.

00416 {
00417     // The rows# of the first matrix must equals the cols# of the second matrix
00418     if ( this->m_iCols == M.m_iRows ) {
00419         SPtMatrix< T > temp( this->m_iRows, M.m_iCols );
00420         if ( temp.m_iRows < temp.m_iCols ) {
00421             for ( int r = 0; r < temp.m_iRows; r++ ) {
00422                 for ( int c = 0; c < temp.m_iCols; c++ ) {
00423                     for ( int k = 0; k < M.m_iRows; k++ ) {
00424                         temp.m_tElement[r][c] += this->m_tElement[r][k] * M.m_tElement[k][c];
00425                     }
00426                     //if ( fabs(temp.m_tElement[r][c]) < 5E-20 ) temp.m_tElement[r][c] = 0;
00427                 }
00428             }
00429         }
00430         else {
00431             for ( int c = 0; c < temp.m_iCols; c++ ) {
00432                 for ( int r = 0; r < temp.m_iRows; r++ ) {
00433                     for ( int k = 0; k < M.m_iRows; k++ ) {
00434                         temp.m_tElement[r][c] += this->m_tElement[r][k] * M.m_tElement[k][c];
00435                     }
00436                     //if ( fabs(temp.m_tElement[r][c]) < 5E-20 ) temp.m_tElement[r][c] = 0;
00437                 }
00438             }
00439         }
00440 
00441         return temp;
00442     }
00443 
00444     else return NULL;
00445 }

template<typename T>
SPtMatrix< T > & SPtMatrix< T >::operator*= ( double  s  )  [inline]

Definition at line 387 of file SPT_Matrix.h.

00388 {
00389     for ( int r = 0; r < m_iRows; r++ ) {
00390         for ( int c = 0; c < m_iCols; c++ ) {
00391             this->m_tElement[r][c] *= s;
00392         }
00393     }
00394 
00395     return *this;
00396 }

template<typename T>
SPtMatrix< T > & SPtMatrix< T >::operator+= ( const SPtMatrix< T > &  M  )  [inline]

Definition at line 355 of file SPT_Matrix.h.

00356 {
00357     if ( m_iRows == M.m_iRows && m_iCols == M.m_iCols ) {
00358         for ( int r = 0; r < m_iRows; r++ ) {
00359             for ( int c = 0; c < m_iCols; c++ ) {
00360                 this->m_tElement[r][c] += M.m_tElement[r][c];
00361             }
00362         }
00363     }
00364 
00365     return *this;
00366 }

template<typename T>
SPtMatrix< T > SPtMatrix< T >::operator- (  )  const [inline]

Definition at line 345 of file SPT_Matrix.h.

00346 {
00347     SPtMatrix< T > temp( *this );
00348     temp *= -1.0;
00349     return temp;
00350 }

template<typename T>
SPtMatrix< T > & SPtMatrix< T >::operator-= ( const SPtMatrix< T > &  M  )  [inline]

Definition at line 371 of file SPT_Matrix.h.

00372 {
00373     if ( m_iRows == M.m_iRows && m_iCols == M.m_iCols ) {
00374         for ( int r = 0; r < m_iRows; r++ ) {
00375             for ( int c = 0; c < m_iCols; c++ ) {
00376                 this->m_tElement[r][c] -= M.m_tElement[r][c];
00377             }
00378         }
00379     }
00380 
00381     return *this;
00382 }

template<typename T>
SPtMatrix< T > & SPtMatrix< T >::operator/= ( double  s  )  [inline]

Definition at line 401 of file SPT_Matrix.h.

00402 {
00403     for ( int r = 0; r < m_iRows; r++ ) {
00404         for ( int c = 0; c < m_iCols; c++ ) {
00405             this->m_tElement[r][c] /= s;
00406         }
00407     }
00408 
00409     return *this;
00410 }

template<typename T>
SPtMatrix< T > & SPtMatrix< T >::operator= ( const SPtMatrix< T > &  M  )  [inline]

Definition at line 331 of file SPT_Matrix.h.

00332 {
00333     // check self assignment
00334     if ( this != &M ) {
00335         DeleteMemory();         // delete the space
00336         CopyElements( M );      // copy all elements from M matrix
00337     }
00338 
00339     return *this;
00340 }

template<typename T>
void SPtMatrix< T >::Set ( int  r,
int  c,
value 
) [inline]

Definition at line 251 of file SPT_Matrix.h.

00252 {
00253     if ( CheckRanges( r, c ) ) {
00254         m_tElement[ r ][ c ] = value;
00255     }
00256 } // END Fn Member: Set()

template<typename T>
bool SPtMatrix< T >::SetToIdentity (  )  [inline]

Definition at line 261 of file SPT_Matrix.h.

00262 {
00263     if ( m_iRows != m_iCols )   return false;
00264     for ( int r = 0; r < m_iRows; r++ ) {
00265         for ( int c = 0; c < m_iCols; c++ ) {
00266             this->m_tElement[r][c] = T( 0 );
00267         }
00268     }
00269     for ( int i = 0; i < m_iRows; i++ ) {
00270         this->m_tElement[i][i] = T( 1 );
00271     }
00272     return true;
00273 } // END Fn Member: SetToIdentity()

template<typename T>
SPtMatrix< T > & SPtMatrix< T >::Transpose (  )  [inline]

Definition at line 319 of file SPT_Matrix.h.

00320 {
00321     *this = this->GetTranspose();
00322 
00323     return *this;
00324 }


Friends And Related Function Documentation

template<typename T>
friend class MatrixOperations [friend]

Definition at line 90 of file SPT_Matrix.h.

template<typename T>
SPtMatrix< T > operator* ( double  s,
const SPtMatrix< T > &  M 
) [friend]

Definition at line 55 of file SPT_Matrix.h.

00056     {
00057         SPtMatrix< double > temp( M );
00058         temp *= s;
00059         return temp;
00060     }

template<typename T>
SPtMatrix< T > operator+ ( const SPtMatrix< T > &  A,
const SPtMatrix< T > &  B 
) [friend]

Definition at line 76 of file SPT_Matrix.h.

00077     {
00078         // Both matrices have to be the same order!
00079         if ( A.m_iRows != B.m_iRows || A.m_iCols != B.m_iCols )     return NULL;
00080 
00081         SPtMatrix< T > temp( A );
00082         for ( int r = 0; r < temp.m_iRows; r++ ) {
00083             for ( int c = 0; c < temp.m_iCols; c++ ) {
00084                 temp.m_tElement[r][c] += B.m_tElement[r][c];
00085             }
00086         }
00087 
00088         return temp;
00089     }

template<typename T>
SPtMatrix< T > operator- ( const SPtMatrix< T > &  A,
const SPtMatrix< T > &  B 
) [friend]

Definition at line 62 of file SPT_Matrix.h.

00063     {
00064         // Both matrices have to be the same order!
00065         if ( A.m_iRows != B.m_iRows || A.m_iCols != B.m_iCols )     return NULL;
00066 
00067         SPtMatrix< T > temp( A );
00068         for ( int r = 0; r < temp.m_iRows; r++ ) {
00069             for ( int c = 0; c < temp.m_iCols; c++ ) {
00070                 temp.m_tElement[r][c] -= B.m_tElement[r][c];
00071             }
00072         }
00073 
00074         return temp;
00075     }

template<typename T>
ostream& operator<< ( ostream &  output,
const SPtMatrix< T > &  M 
) [friend]

Definition at line 37 of file SPT_Matrix.h.

00038     {
00039         int r, c;
00040 
00041         output << "A(n) " << m_iRows << " x " << m_iCols << " Matrix \n";
00042         output.precision(10);
00043         for ( r = 0; r < m_iRows; r++ ) {
00044             output << "| ";
00045             for ( c = 0; c < m_iCols; c++ ) {
00046                 output.width( 20 ); output << m_tElement[r][c];
00047             }
00048             output << " |" << endl;
00049         }
00050         output.precision(6);
00051         
00052         return output;
00053     }


Member Data Documentation

template<typename T>
int SPtMatrix< T >::m_iCols

Definition at line 95 of file SPT_Matrix.h.

template<typename T>
int SPtMatrix< T >::m_iRows

Definition at line 95 of file SPT_Matrix.h.

template<typename T>
T** SPtMatrix< T >::m_tElement

Definition at line 94 of file SPT_Matrix.h.


The documentation for this class was generated from the following file:

Generated on Mon Oct 13 11:45:57 2008 for TAPs by  doxygen 1.5.6