Main Page   Class Hierarchy   Alphabetical List   Compound List   Compound Members  

util::Matrix< T > Class Template Reference

#include <Matrix.h>

Inheritance diagram for util::Matrix< T >:

util::Object List of all members.

Detailed Description

template<class T>
class util::Matrix< T >

Matrix is used to present a two-dimensionan array of any data type.

The Matrix class performs the usual matrix operations including addition, subtraction, multiplication, inversion, transpose etc.


Public Methods

 Matrix (int size=1, bool clear=true)
 Create a size-by-size matrix.

 Matrix (int rows, int columns, bool clear=true)
 Create a rows-by-columns size matrix.

 Matrix (const Matrix &other)
 Create a matrix that is an exact copy of another matrix.

template<class U>  Matrix (const Matrix< U > &other)
 Create a matrix that is holds the contents of another matrix typecasted to the type of this matrix.

 Matrix (int rows, int columns, T *data, bool release=false)
 Create a matrix that reads its data from the given array.

 Matrix (int rows, int columns, T firstElement,...)
 Create a matrix with the given number of rows and columns.

 Matrix (const List< T > &vector, bool row=true)
 Create a row or column matrix.

virtual ~Matrix ()
T * getData ()
 Get the data array for this matrix.

const T * getData () const
 Get the data array.

T * releaseData ()
 Take the ownership of the data in the matrix.

void setSize (int rows, int columns)
 Set the size of this matrix.

int getRows () const
 Get the number of rows in a matrix.

int getColumns () const
 Get the number of colums in a matrix.

List< T > getRow (int index) const throw (MatrixException&)
 Get the row vector at index.

List< T > getColumn (int index) const throw (MatrixException&)
 Get the column vector at index.

void setRow (int index, List< T > &row) throw (MatrixException&)
 Set the row vector to index.

void setColumn (int index, List< T > &col) throw (MatrixException&)
 Set the column vector to index.

void removeRow (int index) throw (MatrixException&)
 Remove the row from the matrix at index.

void removeColumn (int index) throw (MatrixException&)
 Remove the column from the matrix at index.

Matrix & fliplr ()
 Flip the matrix in horizontal direction (columns).

Matrix & flipud ()
 Flip the matrix in vertical direction (rows).

Matrix getTranspose () const
 Transpose the matrix.

Matrix & transpose ()
 Transpose the matrix in place.

Matrix getDotProduct (const Matrix &other) const throw (MatrixException&)
 Multiply two matrices item-wise.

Matrix & dotProduct (const Matrix &other) throw (MatrixException&)
 Multiply two matrices item-wise.

double determinant (void) const throw (MatrixException&)
 Determinant of the matrix.

Matrix< double > inverse (void) const throw (MatrixException&)
 Makes the inversion for the matrix.

double cofactor (int row, int col) const throw (MatrixException&)
 Takes the Cofactor from the matrix at place row,col.

bool isSingular (void)
 Tests if matrix is singular.

bool isDiagonal (void)
 Tests if matrix is diagonal.

bool isSymmetric (void)
 Tests if matrix is symmetric.

bool isSkewSymmetric (void)
 Tests if matrix is skew summetric.

void diagonal (void)
 Makes this matrix to identitymatrix.

sum (void) const
 Calculate a sum over all matrix entries.

max (void) const
 Get the maximum value.

min (void) const
 Get the minimum value.

void operator+= (const Matrix &other) throw (MatrixException&)
 Add a matrix to this matrix.

void operator-= (const Matrix &other) throw (MatrixException&)
 Subtract a matrix from this matrix.

void operator *= (const Matrix &other) throw (MatrixException&)
 Multiply this matrix with another matrix.

void operator+= (T value)
 Add a constant value to this matrix.

void operator-= (T value)
 Subtract a constant value to this matrix.

void operator *= (T value)
 Multiply this matrix by a constant value.

void operator/= (T value)
 Divide this matrix by a constant value.

Matrix & operator= (const Matrix &other)
 Set the contents of this matrix to those of another matrix.

template<class U> Matrix & operator= (const Matrix< U > &other)
 Set the contents of this matrix to those of another matrix.

Matrix & operator= (T value)
 Set all entries in this matrix to the given value.

Matrix & operator= (const T *contents)
 Set the contents of this matrix to those stored in the given array.

T & operator() (int row, int column)
 Get an item from the matrix.

operator() (int row, int column) const
 Get an item from the matrix.

Matrix operator() (int row, int column, int rows, int columns) const
 Get a sub-matrix from this matrix.

template<class U>  operator Matrix ()
 Cast the contents of a matrix to another type.

int pivot (int row) throw (MatrixException&)
 Make pivoting starting at the given row.


Protected Methods

void allocate (bool clear=true)
 Allocate memory for a matrix.

template<class U> void copy (const Matrix< U > &other)
 Make a copy of an existing matrix.


Protected Attributes

int _iRows
 The matrix dimensions.

int _iColumns
 The matrix dimensions.

T * _pData
 The data array.


Friends

template<class U> Matrix< U > operator+ (const Matrix< U > &m1, const Matrix< U > &m2) throw (MatrixException&)
 Perform a matrix addition operation.

template<class U> Matrix< U > operator- (const Matrix< U > &m1, const Matrix< U > &m2) throw (MatrixException&)
 Perform a matrix subtraction operation.

template<class U> Matrix< U > operator * (const Matrix< U > &m1, const Matrix< U > &m2) throw (MatrixException&)
 Perform a matrix multiplication operation.

template<class U> bool operator== (const Matrix< U > &m1, const Matrix< U > &m2)
 Compare two matrices.

template<class U> Matrix< U > operator+ (const Matrix< U > &mat, U value)
 Perform a constant value addition operation.

template<class U> Matrix< U > operator- (const Matrix< U > &mat, U value)
 Perform a constant value subtraction operation.

template<class U> Matrix< U > operator * (const Matrix< U > &mat, U value)
 Perform a constant value multiplication operation.

template<class U> Matrix< U > operator/ (const Matrix< U > &mat, U value)
 Perform a constant value division operation.

template<class U> std::ostream & operator<< (std::ostream &sout, const Matrix< U > &m)
 Write a matrix to an output stream.

template<class U> std::istream & operator>> (std::istream &sin, Matrix< U > &m)
 Read a written matrix from an input stream.


Constructor & Destructor Documentation

template<class T>
util::Matrix< T >::Matrix int    size = 1,
bool    clear = true
 

Create a size-by-size matrix.

If size is omitted, a 1-by-1 matrix will be created. Note that the initial contents of a a matrix are always set to zero. This is made by using the = operator with an integer argument of zero. For this reason, the = operator for an integer argument must be defined for the content class of the matrix. Note also that if a matrix is to hold objects of any class type, and if matrix atrithmetic is performed on the matrix, then the =, +, +=, -, -=, *, *=, / and /= operators of the content class must be defined for arguments of the same class type.
Example:

 class MyClass
 {
   ...

 public:
   MyClass& operator=(const MyClass &o1);
   MyClass& operator=(int value);
   void operator+=(const MyClass &o1);
   void operator-=(const MyClass &o1);
   void operator *=(const MyClass &o1);
   void operator/=(const MyClass &o1);
   friend MyClass operator+(const MyClass &o1, const MyClass &o2);
   friend MyClass operator-(const MyClass &o1, const MyClass &o2);
   friend MyClass operator *(const MyClass &o1, const MyClass &o2);
   friend MyClass operator/(const MyClass &o1, const MyClass &o2);

   ...
  };

 ...

 Matrix<MyClass> mat; //invokes operator=(int) with a zero argument
 MyClass mc(123);
 mat(0,0) = mc;
 mat *= mc;
 
Parameters:
size  the size of the matrix
clear  if true, all matrix entries are initially set to zero. Otherwise the contents will be undefined.

template<class T>
util::Matrix< T >::Matrix int    rows,
int    columns,
bool    clear = true
 

Create a rows-by-columns size matrix.

Parameters:
rows  the number of rows
columns  the number of columns
clear  if true, all matrix entries are initially set to zero. Otherwise the contents will be undefined.

template<class T>
util::Matrix< T >::Matrix const Matrix< T > &    other
 

Create a matrix that is an exact copy of another matrix.

(Copy constructor)

Parameters:
mat  the matrix to be cloned

template<class T>
template<class U>
util::Matrix< T >::Matrix const Matrix< U > &    other
 

Create a matrix that is holds the contents of another matrix typecasted to the type of this matrix.

If the matrix to be cloned contains items of any other than the elementary data types, then typecast operator for the wanted type must be defined.
Example:

 Matrix<int> a(2,2);
 a = 1;
 Matrix<float> b(a);
 

This will result in b(0,0) holding 1.0f.
Parameters:
other  the matrix whose contents are to be typecasted and copied to this matrix

template<class T>
util::Matrix< T >::Matrix int    rows,
int    columns,
T *    data,
bool    release = false
 

Create a matrix that reads its data from the given array.

The size of the resulting matrix will be rows x columns. Among other things, this constructor can be used in efficiently transforming a List into a Matrix:

 List<int> lst(5);
 lst.addElements(5,1,2,3,4,5);
 Matrix<int> rowVector(1, 5, lst.releaseData(), true);
 

Note that in the example, lst will be empty after the releaseData() call. Once can create a column matrix by exchanging the number of rows and columns.

Parameters:
rows  the number of rows
columns  the number of columns
T  the data array
release  if true, matrix will take the ownership of the pointer. If false, the contents will be copied.

template<class T>
util::Matrix< T >::Matrix int    rows,
int    columns,
  firstElement,
...   
 

Create a matrix with the given number of rows and columns.

Matrix contents are given as a variable-length parameter list in horizontal raster-scan order. Pay attention that the elements really are of the type you think them to be.

Parameters:
rows  the number of rows
columns  the number of columns
firstElement  the first element in matrix
...  rest of the matrix data
See also:
List::addElements(int,...)

template<class T>
util::Matrix< T >::Matrix const List< T > &    vector,
bool    row = true
[explicit]
 

Create a row or column matrix.

Take the data for the single column from vector.

Parameters:
vector  row/column data
row  if true, create a row matrix. Otherwise create a column matrix.


Member Function Documentation

template<class T>
void util::Matrix< T >::allocate bool    clear = true [inline, protected]
 

Allocate memory for a matrix.

The protected variables rows and columns must be set prior to calling this method.

Parameters:
clear  if true, all matrix entries are set to zero

template<class T>
double util::Matrix< T >::cofactor int    row,
int    col
const throw (MatrixException&)
 

Takes the Cofactor from the matrix at place row,col.

Returns:
The value of cofactor.

template<class T>
double util::Matrix< T >::determinant void    const throw (MatrixException&)
 

Determinant of the matrix.

Returns:
the value of determinat.

template<class T>
void util::Matrix< T >::diagonal void   
 

Makes this matrix to identitymatrix.

(has ones in diagonal and zeros other places). The size of matrix will be minimum of rows or columns, of course square matrix.

template<class T>
Matrix< T > & util::Matrix< T >::dotProduct const Matrix< T > &    other throw (MatrixException&)
 

Multiply two matrices item-wise.

The result is stored in this matrix.

template<class T>
List< T > util::Matrix< T >::getColumn int    index const throw (MatrixException&)
 

Get the column vector at index.

Parameters:
index  the column index
Exceptions:
MatrixException  if index exceeds matrix dimensions

template<class T>
const T* util::Matrix< T >::getData   const [inline]
 

Get the data array.

The actual contents of the matrix are stored in a one-dimensional array of size rows*columns. The data is ordered so that rows can be read sequentially one after another.

Returns:
a pointer to the data array

template<class T>
T* util::Matrix< T >::getData   [inline]
 

Get the data array for this matrix.

The array holds the contens of the matrix from its upper left corner to the lower right one. The data is stored in a horizontal raster-scan manner.

Returns:
the data array

template<class T>
Matrix util::Matrix< T >::getDotProduct const Matrix< T > &    other const throw (MatrixException&) [inline]
 

Multiply two matrices item-wise.

Return the result.

template<class T>
List< T > util::Matrix< T >::getRow int    index const throw (MatrixException&)
 

Get the row vector at index.

Parameters:
index  the row index
Exceptions:
MatrixException  if index exceeds matrix dimensions

template<class T>
Matrix< T > util::Matrix< T >::getTranspose   const
 

Transpose the matrix.

Returns:
a newly created matrix that is a transpose of the current one

template<class T>
Matrix< double > util::Matrix< T >::inverse void    const throw (MatrixException&)
 

Makes the inversion for the matrix.

Returns:
The inverted matrix.

template<class T>
void util::Matrix< T >::operator *=   value
 

Multiply this matrix by a constant value.

Parameters:
value  the value this matrix is to be multiplied with

template<class T>
void util::Matrix< T >::operator *= const Matrix< T > &    other throw (MatrixException&)
 

Multiply this matrix with another matrix.

Parameters:
other  the matrix to multiply this matrix with
Exceptions:
MatrixException  if the dimensions of the two matrices do not match

template<class T>
template<class U>
util::Matrix< T >::operator Matrix< U >  
 

Cast the contents of a matrix to another type.

If the contents of the original matrix are not of any elementary type, then an appropriate typecast operator for the content class must be defined.

template<class T>
Matrix< T > util::Matrix< T >::operator() int    row,
int    column,
int    rows,
int    columns
const
 

Get a sub-matrix from this matrix.

Take care that the dimensions of the matrix are not exceeded.

Parameters:
row  the row of the upper left column of the sub-matrix
column  the column of the upper left column of the sub-matrix
rows  the number of rows to include
columns  the number of columns to include

template<class T>
T util::Matrix< T >::operator() int    row,
int    column
const [inline]
 

Get an item from the matrix.

Parameters:
row  the row index
column  the column index
Returns:
the matrix item at (row,column)

template<class T>
T& util::Matrix< T >::operator() int    row,
int    column
[inline]
 

Get an item from the matrix.

Parameters:
row  the row index
column  the column index
Returns:
the matrix item at (row,column)

template<class T>
void util::Matrix< T >::operator+=   value
 

Add a constant value to this matrix.

Parameters:
value  the value to be added

template<class T>
void util::Matrix< T >::operator+= const Matrix< T > &    other throw (MatrixException&)
 

Add a matrix to this matrix.

Parameters:
other  the matrix to be added
Exceptions:
MatrixException  if the dimensions of the two matrices do not match

template<class T>
void util::Matrix< T >::operator-=   value
 

Subtract a constant value to this matrix.

Parameters:
value  the value to be subtracted

template<class T>
void util::Matrix< T >::operator-= const Matrix< T > &    other throw (MatrixException&)
 

Subtract a matrix from this matrix.

Parameters:
other  the matrix to be subtracted
Exceptions:
MatrixException  if the dimensions of the two matrices do not match

template<class T>
void util::Matrix< T >::operator/=   value
 

Divide this matrix by a constant value.

Parameters:
value  the value this matrix is to be divided with

template<class T>
Matrix< T > & util::Matrix< T >::operator= const T *    contents
 

Set the contents of this matrix to those stored in the given array.

The size of the array must be equal to rows*columns, and rows must be stored sequentially one after another.

Parameters:
contents  the new contents of the matrix in an array

template<class T>
Matrix< T > & util::Matrix< T >::operator=   value
 

Set all entries in this matrix to the given value.

Parameters:
value  the value of each matrix entry

Reimplemented in ConvolutionMask< T >.

template<class T>
template<class U>
Matrix< T > & util::Matrix< T >::operator= const Matrix< U > &    other
 

Set the contents of this matrix to those of another matrix.

Parameters:
other  the matrix to be cloned

template<class T>
Matrix< T > & util::Matrix< T >::operator= const Matrix< T > &    other
 

Set the contents of this matrix to those of another matrix.

Parameters:
other  the matrix to be cloned

template<class T>
int util::Matrix< T >::pivot int    row throw (MatrixException&)
 

Make pivoting starting at the given row.

Parameters:
row  the row to start pivoting at.

template<class T>
T* util::Matrix< T >::releaseData void    [inline]
 

Take the ownership of the data in the matrix.

The size of the matrix will be set to zero, and the internal pointer to NULL. A pointer to the data array is returned.

template<class T>
void util::Matrix< T >::removeColumn int    index throw (MatrixException&)
 

Remove the column from the matrix at index.

Parameters:
index  the column index.
Exceptions:
MatrixException  if index exceeds matrix dimensions

template<class T>
void util::Matrix< T >::removeRow int    index throw (MatrixException&)
 

Remove the row from the matrix at index.

Parameters:
index  the row index.
Exceptions:
MatrixException  if index exceeds matrix dimensions

template<class T>
void util::Matrix< T >::setColumn int    index,
List< T > &    col
throw (MatrixException&)
 

Set the column vector to index.

Parameters:
index  the column index
Exceptions:
MatrixException  if index exceeds matrix dimensions
MatrixException  if sizes differ

template<class T>
void util::Matrix< T >::setRow int    index,
List< T > &    row
throw (MatrixException&)
 

Set the row vector to index.

Parameters:
index  the row index
Exceptions:
MatrixException  if index exceeds matrix dimensions
MatrixException  if sizes differ

template<class T>
void util::Matrix< T >::setSize int    rows,
int    columns
 

Set the size of this matrix.

The initial contents of the newly allocated data array are undefined.

Parameters:
rows  the number of rows
columns  the number of columns

template<class T>
T util::Matrix< T >::sum void    const
 

Calculate a sum over all matrix entries.

Returns:
the sum of all matrix entries


Friends And Related Function Documentation

template<class T>
template<class U>
Matrix<U> operator * const Matrix< U > &    m1,
const Matrix< U > &    m2
throw (MatrixException&) [friend]
 

Perform a matrix multiplication operation.

Exceptions:
MatrixException  if the dimensions of m1 and m2 do not match

template<class T>
template<class U>
Matrix<U> operator+ const Matrix< U > &    m1,
const Matrix< U > &    m2
throw (MatrixException&) [friend]
 

Perform a matrix addition operation.

Exceptions:
MatrixException  if the dimensions of m1 and m2 do not match

template<class T>
template<class U>
Matrix<U> operator- const Matrix< U > &    m1,
const Matrix< U > &    m2
throw (MatrixException&) [friend]
 

Perform a matrix subtraction operation.

Exceptions:
MatrixException  if the dimensions of m1 and m2 do not match


The documentation for this class was generated from the following file:
Documentation generated on 11.09.2003 with Doxygen.
The documentation is copyrighted material.
Copyright © Topi Mäenpää 2003. All rights reserved.