Matrix< Type > Class Template Reference

Matrix class implements a template for operations on matrices. More...

#include <Matrix.h>

List of all members.

Public Member Functions

 Matrix (Integer rows=0, Integer cols=0)
 Default constructor.
 Matrix (Const Matrix &m)
 Copy constructor.
Void SetSize (Integer rows, Integer cols)
 Set the size of a matrix.
List< Type > & operator[] (Integer index)
 Indexing operator for matrix.
Matrixoperator= (Const Matrix &m)
 Assignment/copy operator for matrix.
Bool operator== (Const Matrix &m)
 Equality of two matrices.
Bool operator!= (Const Matrix &m)
 Inequality of two matrices.
Const Matrix operator+ (Const Matrix &m)
 Add two identical matrices.
Const Matrix operator- (Const Matrix &m)
 Subtract two identical matrices.
Const Matrix operator * (Const Integer R)
 Multiply matrix by a constant Integer.
Const Matrix operator * (Const Real R)
 Multiply matrix by a constant Real.
Const Matrix operator+ (Const Integer R)
 Add constant Integer to matrix.
Const Matrix operator+ (Const Real R)
 Add constant Real to matrix.
Const Matrix operator- (Const Integer R)
 Subtract constant Integer from matrix.
Const Matrix operator- (Const Real R)
 Subtract constant Real from matrix.
Const Matrix operator * (Matrix &m)
 Matrix multiplication. Columns of first matrix must be same size as rows of second.
Void print (String s=0)
 Print a row of a matrix.
Type rows ()
 Return the number of rows in the Matrix.
Type cols ()
 Return the number of columns in the Matrix.
Void length (Integer &r, Integer &c)
 Return the number of rows and columns in the Matrix by reference.

Friends

ostream & operator<< (ostream &os, Const Matrix &m)
 Print a matrix to stdout.
istream & operator>> (istream &is, Matrix &m)
 Scan a matrix from stdin.


Detailed Description

template<class Type>
class Matrix< Type >

Matrix class implements a template for operations on matrices.

Definition at line 28 of file Matrix.h.


Constructor & Destructor Documentation

template<class Type>
Matrix< Type >::Matrix ( Integer  rows = 0,
Integer  cols = 0 
) [inline]

Default constructor.

Definition at line 51 of file Matrix.h.

References Matrix< Type >::cols(), Matrix< Type >::rows(), and Matrix< Type >::SetSize().

00051                                           : m_matrix(NULL) {
00052         SetSize(rows,cols);
00053     }

template<class Type>
Matrix< Type >::Matrix ( Const Matrix< Type > &  m  )  [inline]

Copy constructor.

Definition at line 56 of file Matrix.h.

References Matrix< Type >::SetSize().

00056                            : m_matrix(NULL) {
00057         SetSize(m.m_lenRow, m.m_lenCol);
00058         for (Integer r=0; r < m_lenRow; r++)
00059             m_matrix[r]=List<Type>(m.m_matrix[r]);
00060     }


Member Function Documentation

template<class Type>
Type Matrix< Type >::cols (  )  [inline]

Return the number of columns in the Matrix.

Definition at line 222 of file Matrix.h.

References Return.

Referenced by Matrix< Type >::length(), and Matrix< Type >::Matrix().

00222 { Return m_lenCol; }

template<class Type>
Void Matrix< Type >::length ( Integer r,
Integer c 
) [inline]

Return the number of rows and columns in the Matrix by reference.

Parameters:
r Number of rows, returned by reference
c Number of columns, returned by reference

Definition at line 227 of file Matrix.h.

References Matrix< Type >::cols(), and Matrix< Type >::rows().

00227 { r = rows(); c = cols(); }

template<class Type>
Const Matrix Matrix< Type >::operator * ( Matrix< Type > &  m  )  [inline]

Matrix multiplication. Columns of first matrix must be same size as rows of second.

Definition at line 194 of file Matrix.h.

References Matrix< Type >::m_lenCol, Matrix< Type >::m_lenRow, and Return.

00194                                           {
00195         assert(m_lenCol==m.m_lenRow);
00196         Matrix newMatrix(m_lenRow,m.m_lenCol);
00197         for (Integer r = 0; r < m_lenRow; r++) {
00198             for (Integer c = 0; c < m.m_lenCol; c++) {
00199                 for (Integer i = 0; i < m_lenCol; i++) {
00200                     newMatrix[r][c]+= m_matrix[r][i] * m[i][c];
00201                 }
00202             }
00203         }
00204         Return newMatrix;
00205     }

template<class Type>
Const Matrix Matrix< Type >::operator * ( Const Real  R  )  [inline]

Multiply matrix by a constant Real.

Definition at line 149 of file Matrix.h.

References For, and Return.

00149                                              {
00150         Matrix newMatrix(m_lenRow, m_lenCol);
00151                 For (Integer r = 0; r < m_lenRow; r++)
00152             for (Integer c = 0; c < m_lenCol; c++)
00153                 newMatrix[r][c] = m_matrix[r][c] * R;
00154         Return newMatrix;
00155     }

template<class Type>
Const Matrix Matrix< Type >::operator * ( Const Integer  R  )  [inline]

Multiply matrix by a constant Integer.

Definition at line 140 of file Matrix.h.

References For, and Return.

00140                                                 {
00141         Matrix newMatrix(m_lenRow, m_lenCol);
00142                 For (Integer r = 0; r < m_lenRow; r++)
00143             for (Integer c = 0; c < m_lenCol; c++)
00144                 newMatrix[r][c] = m_matrix[r][c] * R;
00145         Return newMatrix;
00146     }

template<class Type>
Bool Matrix< Type >::operator!= ( Const Matrix< Type > &  m  )  [inline]

Inequality of two matrices.

Definition at line 113 of file Matrix.h.

References Return.

00113                                          {
00114                 Return( ! (*this == m) );
00115         }

template<class Type>
Const Matrix Matrix< Type >::operator+ ( Const Real  R  )  [inline]

Add constant Real to matrix.

Definition at line 167 of file Matrix.h.

References For, and Return.

00167                                              {
00168         Matrix newMatrix(m_lenRow, m_lenCol);
00169                 For (Integer r = 0; r < m_lenRow; r++)
00170             for (Integer c = 0; c < m_lenCol; c++)
00171                 newMatrix[r][c] = m_matrix[r][c] + R;
00172         Return newMatrix;
00173     }

template<class Type>
Const Matrix Matrix< Type >::operator+ ( Const Integer  R  )  [inline]

Add constant Integer to matrix.

Definition at line 158 of file Matrix.h.

References For, and Return.

00158                                                 {
00159         Matrix newMatrix(m_lenRow, m_lenCol);
00160                 For (Integer r = 0; r < m_lenRow; r++)
00161             for (Integer c = 0; c < m_lenCol; c++)
00162                 newMatrix[r][c] = m_matrix[r][c] + R;
00163         Return newMatrix;
00164     }

template<class Type>
Const Matrix Matrix< Type >::operator+ ( Const Matrix< Type > &  m  )  [inline]

Add two identical matrices.

Performs element-by-element addition

Definition at line 119 of file Matrix.h.

References And, For, and Return.

00119                                                 {
00120         assert(m_lenCol == m.m_lenCol  And m_lenRow == m.m_lenRow);
00121         Matrix newMatrix(m_lenRow, m_lenCol);
00122                 For (Integer r=0; r < m_lenRow; r++)
00123             for (Integer c=0; c < m_lenCol;c++)
00124                 newMatrix[r][c] = m_matrix[r][c] + m.m_matrix[r][c];
00125         Return newMatrix;
00126     }

template<class Type>
Const Matrix Matrix< Type >::operator- ( Const Real  R  )  [inline]

Subtract constant Real from matrix.

Definition at line 185 of file Matrix.h.

References For, and Return.

00185                                              {
00186         Matrix newMatrix(m_lenRow, m_lenCol);
00187                 For (Integer r = 0; r < m_lenRow; r++)
00188             for (Integer c = 0; c < m_lenCol; c++)
00189                 newMatrix[r][c] = m_matrix[r][c] - R;
00190         Return newMatrix;
00191     }

template<class Type>
Const Matrix Matrix< Type >::operator- ( Const Integer  R  )  [inline]

Subtract constant Integer from matrix.

Definition at line 176 of file Matrix.h.

References For, and Return.

00176                                                 {
00177         Matrix newMatrix(m_lenRow, m_lenCol);
00178                 For (Integer r = 0; r < m_lenRow; r++)
00179             for (Integer c = 0; c < m_lenCol; c++)
00180                 newMatrix[r][c] = m_matrix[r][c] - R;
00181         Return newMatrix;
00182     }

template<class Type>
Const Matrix Matrix< Type >::operator- ( Const Matrix< Type > &  m  )  [inline]

Subtract two identical matrices.

Performs element-by-element subtraction

Definition at line 130 of file Matrix.h.

References And, For, and Return.

00130                                                 {
00131         assert(m_lenCol == m.m_lenCol  And m_lenRow == m.m_lenRow);
00132         Matrix newMatrix(m_lenRow, m_lenCol);
00133                 For (Integer r = 0; r < m_lenRow; r++)
00134             for (Integer c = 0; c < m_lenCol; c++)
00135                 newMatrix[r][c] = m_matrix[r][c] - m.m_matrix[r][c];
00136         Return newMatrix;
00137     }

template<class Type>
Matrix& Matrix< Type >::operator= ( Const Matrix< Type > &  m  )  [inline]

Assignment/copy operator for matrix.

Definition at line 88 of file Matrix.h.

References If, Return, and Matrix< Type >::SetSize().

00088                                        {
00089                 If (this == &m)
00090                         Return *this;
00091         SetSize(m.m_lenRow, m.m_lenCol);
00092                 for (Integer r=0; r < m_lenRow; r++) {
00093             m_matrix[r] = List<Type>(m.m_matrix[r]);
00094                 }
00095         Return *this;
00096     }

template<class Type>
Bool Matrix< Type >::operator== ( Const Matrix< Type > &  m  )  [inline]

Equality of two matrices.

Definition at line 99 of file Matrix.h.

References False, For, If, Return, and True.

00099                                          {
00100                 // Must have same dimensions
00101                 If (m_lenRow != m.m_lenRow || m_lenCol != m.m_lenCol)
00102                         Return False;
00103 
00104                 // All rows must match
00105                 For (Integer r=0; r < m_lenRow; r++) {
00106                         If (m_matrix[r] != m.m_matrix[r])
00107                                 Return False;
00108                 }
00109                 Return( True );
00110         }

template<class Type>
List<Type>& Matrix< Type >::operator[] ( Integer  index  )  [inline]

Indexing operator for matrix.

Definition at line 82 of file Matrix.h.

References Return.

00082                                               {
00083         assert(index < m_lenRow);
00084         Return m_matrix[index];
00085     }

template<class Type>
Void Matrix< Type >::print ( String  s = 0  )  [inline]

Print a row of a matrix.

Parameters:
s String to prefix to matrix

Definition at line 209 of file Matrix.h.

References For, and If.

00209                                  {
00210                 If (s)
00211                         printf(s);
00212                 cout << "{\n";
00213                 For (Integer i = 0; i < m_lenRow; i++)
00214                         cout << m_matrix[i];
00215                 cout << "}\n";
00216         }

template<class Type>
Type Matrix< Type >::rows (  )  [inline]

Return the number of rows in the Matrix.

Definition at line 219 of file Matrix.h.

References Return.

Referenced by Matrix< Type >::length(), and Matrix< Type >::Matrix().

00219 { Return m_lenRow; }

template<class Type>
Void Matrix< Type >::SetSize ( Integer  rows,
Integer  cols 
) [inline]

Set the size of a matrix.

Parameters:
rows Number of rows
cols Number of columns

Definition at line 65 of file Matrix.h.

References And, Else, and If.

Referenced by Matrix< Type >::Matrix(), and Matrix< Type >::operator=().

00065                                              {
00066                 If (m_matrix != 0) { 
00067                         delete[] m_matrix;
00068                         m_matrix = 0;
00069                 }
00070         If (cols > 0  And rows > 0) {
00071             m_matrix = new List<Type>[rows];
00072             for (Integer i = 0; i < rows; i++)
00073                 m_matrix[i].SetListSize(cols);
00074         }
00075         Else
00076             rows = 0;
00077         m_lenCol = cols;
00078                 m_lenRow = rows;
00079     }


Friends And Related Function Documentation

template<class Type>
ostream& operator<< ( ostream &  os,
Const Matrix< Type > &  m 
) [friend]

Print a matrix to stdout.

Definition at line 31 of file Matrix.h.

00031                                                                 {
00032                 // os << m.m_lenRow << " " << m.m_lenCol << " " <<endl;
00033                 for (Integer r = 0; r < m.m_lenRow; r++)
00034                         cout << m.m_matrix[r];
00035                 Return os;
00036         }

template<class Type>
istream& operator>> ( istream &  is,
Matrix< Type > &  m 
) [friend]

Scan a matrix from stdin.

Definition at line 39 of file Matrix.h.

00039                                                            {
00040                 Integer rows,cols;
00041                 is >> rows >> cols;
00042                 m.SetSize(rows, cols);
00043                 for (Integer r = 0; r < rows; r++)
00044                         for (Integer c = 0; c < cols; c++)
00045                                 is >> m[r][c];
00046                 Return is;
00047                 }


The documentation for this class was generated from the following file:
Generated on Fri Sep 8 23:21:13 2006 for MusimatLib by  doxygen 1.4.7