Public Member Functions | Friends

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 27 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 50 of file Matrix.h.

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

                                          : m_matrix(NULL) {
        SetSize(rows,cols);
    }
template<class Type >
Matrix< Type >::Matrix ( Const Matrix< Type > &  m ) [inline]

Copy constructor.

Definition at line 55 of file Matrix.h.

References Matrix< Type >::SetSize().

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

Member Function Documentation

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

Return the number of columns in the Matrix.

Definition at line 221 of file Matrix.h.

References Return.

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

{ 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:
rNumber of rows, returned by reference
cNumber of columns, returned by reference

Definition at line 226 of file Matrix.h.

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

{ r = rows(); c = cols(); }
template<class Type >
Bool Matrix< Type >::operator!= ( Const Matrix< Type > &  m ) [inline]

Inequality of two matrices.

Definition at line 112 of file Matrix.h.

References Return.

                                         {
                Return( ! (*this == m) );
        }
template<class Type >
Const Matrix Matrix< Type >::operator* ( Const Real  R ) [inline]

Multiply matrix by a constant Real.

Definition at line 148 of file Matrix.h.

References For, and Return.

                                             {
        Matrix newMatrix(m_lenRow, m_lenCol);
                For (Integer r = 0; r < m_lenRow; r++)
            for (Integer c = 0; c < m_lenCol; c++)
                newMatrix[r][c] = m_matrix[r][c] * R;
        Return newMatrix;
    }
template<class Type >
Const Matrix Matrix< Type >::operator* ( Const Integer  R ) [inline]

Multiply matrix by a constant Integer.

Definition at line 139 of file Matrix.h.

References For, and Return.

                                                {
        Matrix newMatrix(m_lenRow, m_lenCol);
                For (Integer r = 0; r < m_lenRow; r++)
            for (Integer c = 0; c < m_lenCol; c++)
                newMatrix[r][c] = m_matrix[r][c] * R;
        Return newMatrix;
    }
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 193 of file Matrix.h.

References Return.

                                          {
        assert(m_lenCol==m.m_lenRow);
        Matrix newMatrix(m_lenRow,m.m_lenCol);
        for (Integer r = 0; r < m_lenRow; r++) {
            for (Integer c = 0; c < m.m_lenCol; c++) {
                for (Integer i = 0; i < m_lenCol; i++) {
                    newMatrix[r][c]+= m_matrix[r][i] * m[i][c];
                }
            }
        }
        Return newMatrix;
    }
template<class Type >
Const Matrix Matrix< Type >::operator+ ( Const Integer  R ) [inline]

Add constant Integer to matrix.

Definition at line 157 of file Matrix.h.

References For, and Return.

                                                {
        Matrix newMatrix(m_lenRow, m_lenCol);
                For (Integer r = 0; r < m_lenRow; r++)
            for (Integer c = 0; c < m_lenCol; c++)
                newMatrix[r][c] = m_matrix[r][c] + R;
        Return newMatrix;
    }
template<class Type >
Const Matrix Matrix< Type >::operator+ ( Const Real  R ) [inline]

Add constant Real to matrix.

Definition at line 166 of file Matrix.h.

References For, and Return.

                                             {
        Matrix newMatrix(m_lenRow, m_lenCol);
                For (Integer r = 0; r < m_lenRow; r++)
            for (Integer c = 0; c < m_lenCol; c++)
                newMatrix[r][c] = m_matrix[r][c] + R;
        Return newMatrix;
    }
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 118 of file Matrix.h.

References And, For, and Return.

                                                {
        assert(m_lenCol == m.m_lenCol  And m_lenRow == m.m_lenRow);
        Matrix newMatrix(m_lenRow, m_lenCol);
                For (Integer r=0; r < m_lenRow; r++)
            for (Integer c=0; c < m_lenCol;c++)
                newMatrix[r][c] = m_matrix[r][c] + m.m_matrix[r][c];
        Return newMatrix;
    }
template<class Type >
Const Matrix Matrix< Type >::operator- ( Const Real  R ) [inline]

Subtract constant Real from matrix.

Definition at line 184 of file Matrix.h.

References For, and Return.

                                             {
        Matrix newMatrix(m_lenRow, m_lenCol);
                For (Integer r = 0; r < m_lenRow; r++)
            for (Integer c = 0; c < m_lenCol; c++)
                newMatrix[r][c] = m_matrix[r][c] - R;
        Return newMatrix;
    }
template<class Type >
Const Matrix Matrix< Type >::operator- ( Const Integer  R ) [inline]

Subtract constant Integer from matrix.

Definition at line 175 of file Matrix.h.

References For, and Return.

                                                {
        Matrix newMatrix(m_lenRow, m_lenCol);
                For (Integer r = 0; r < m_lenRow; r++)
            for (Integer c = 0; c < m_lenCol; c++)
                newMatrix[r][c] = m_matrix[r][c] - R;
        Return newMatrix;
    }
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 129 of file Matrix.h.

References And, For, and Return.

                                                {
        assert(m_lenCol == m.m_lenCol  And m_lenRow == m.m_lenRow);
        Matrix newMatrix(m_lenRow, m_lenCol);
                For (Integer r = 0; r < m_lenRow; r++)
            for (Integer c = 0; c < m_lenCol; c++)
                newMatrix[r][c] = m_matrix[r][c] - m.m_matrix[r][c];
        Return newMatrix;
    }
template<class Type >
Matrix& Matrix< Type >::operator= ( Const Matrix< Type > &  m ) [inline]

Assignment/copy operator for matrix.

Definition at line 87 of file Matrix.h.

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

                                       {
                If (this == &m)
                        Return *this;
        SetSize(m.m_lenRow, m.m_lenCol);
                for (Integer r=0; r < m_lenRow; r++) {
            m_matrix[r] = List<Type>(m.m_matrix[r]);
                }
        Return *this;
    }
template<class Type >
Bool Matrix< Type >::operator== ( Const Matrix< Type > &  m ) [inline]

Equality of two matrices.

Definition at line 98 of file Matrix.h.

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

                                         {
                // Must have same dimensions
                If (m_lenRow != m.m_lenRow || m_lenCol != m.m_lenCol)
                        Return False;

                // All rows must match
                For (Integer r=0; r < m_lenRow; r++) {
                        If (m_matrix[r] != m.m_matrix[r])
                                Return False;
                }
                Return( True );
        }
template<class Type >
List<Type>& Matrix< Type >::operator[] ( Integer  index ) [inline]

Indexing operator for matrix.

Definition at line 81 of file Matrix.h.

References Return.

                                              {
        assert(index < m_lenRow);
        Return m_matrix[index];
    }
template<class Type >
Void Matrix< Type >::print ( String  s = 0 ) [inline]

Print a row of a matrix.

Parameters:
sString to prefix to matrix

Definition at line 208 of file Matrix.h.

References For, and If.

                                 {
                If (s)
                        cout<<s;
                cout << "{\n";
                For (Integer i = 0; i < m_lenRow; i++)
                        cout << m_matrix[i];
                cout << "}\n";
        }
template<class Type >
Type Matrix< Type >::rows (  ) [inline]

Return the number of rows in the Matrix.

Definition at line 218 of file Matrix.h.

References Return.

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

{ Return m_lenRow; }
template<class Type >
Void Matrix< Type >::SetSize ( Integer  rows,
Integer  cols 
) [inline]

Set the size of a matrix.

Parameters:
rowsNumber of rows
colsNumber of columns

Definition at line 64 of file Matrix.h.

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

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

                                             {
                If (m_matrix != 0) { 
                        delete[] m_matrix;
                        m_matrix = 0;
                }
        If (cols > 0  And rows > 0) {
            m_matrix = new List<Type>[rows];
            for (Integer i = 0; i < rows; i++)
                m_matrix[i].SetListSize(cols);
        }
        Else
            rows = 0;
        m_lenCol = cols;
                m_lenRow = rows;
    }

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 30 of file Matrix.h.

                                                                {
                // os << m.m_lenRow << " " << m.m_lenCol << " " <<endl;
                for (Integer r = 0; r < m.m_lenRow; r++)
                        cout << m.m_matrix[r];
                Return os;
        }
template<class Type >
istream& operator>> ( istream &  is,
Matrix< Type > &  m 
) [friend]

Scan a matrix from stdin.

Definition at line 38 of file Matrix.h.

                                                           {
                Integer rows,cols;
                is >> rows >> cols;
                m.SetSize(rows, cols);
                for (Integer r = 0; r < rows; r++)
                        for (Integer c = 0; c < cols; c++)
                                is >> m[r][c];
                Return is;
                }

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