Main Page | Namespace List | Alphabetical List | Class List | File List | Class Members | File Members

Matrix Class Reference

Simple mathematical matrices as used in linear algebra. More...

#include <Matrix.h>

List of all members.

Public Member Functions

Matrix transpose () const
 Transpose the matrix.
Matrix operator~ () const
 Another denotation for transpose.
Contructors, destructor, and conversions
Note:
No need for an explicit destructor nor for an explicit copy constructor: the default ones, automatically provided by C++, are fine.


 Matrix (int nl=0, int nc=0, double x=0.0)
 Constructor (also usable as default constructor).
 Matrix (MVector mv, bool transp=false)
 Constructor creating a Matrix from a MVector.
 operator MVector () const
 Conversion operator creating a MVector from a Matrix.
MVector column (int j) const
 Extract a column.
MVector line (int i) const
 Extract a line.
Assignments
Note:
No need for a copy assignment operator; the default one, automatically provided by C++ is fine.


Matrixoperator+= (const Matrix &mat)
Matrixoperator-= (const Matrix &mat)
Matrixoperator *= (const Matrix &mat)
Accessors
int nlines () const
 Number of lines.
int ncols () const
 Number of columns.
Subscript
Exceptions:
Out_Of_Bounds thrown if index is outside range


MVectoroperator[] (int i)
 A reference to the line at index i (version for a non-constant matrix).
const MVectoroperator[] (int i) const
 A reference to the line at index i (version for a constant matrix).
double & operator() (int i, int j)
 Access to element at indexes (i,j) (version for a non-constant matrix).
double operator() (int i, int j) const
 Access to element at indexes (i,j) (version for aconstant matrix).
double & at (int i, int j)
 Another denotation for at(i,j) (version for a non-constant matrix).
double at (int i, int j) const
 Another denotation for at(i,j) (version for a constant matrix).

Private Attributes

int _nlines
 Number of lines.
int _ncols
 Number of columns.
vector< MVector_lines
 The elements themselves.

Friends

ostream & operator<< (ostream &os, const Matrix &mat)
 IO operation: just display the contents.
Arithmetic operations
Matrix operator+ (const Matrix &mat1, const Matrix &mat2)
 Addition.
Matrix operator- (const Matrix &mat1, const Matrix &mat2)
 Subtraction.
Matrix operator * (const Matrix &mat1, const Matrix &mat2)
 Product.
Relational operations
bool operator== (const Matrix &mat1, const Matrix &mat2)
 Equality.
bool operator!= (const Matrix &mat1, const Matrix &mat2)
 Unequality.

Classes

class  Bad_Dimensions
class  Out_Of_Bounds


Detailed Description

Simple mathematical matrices as used in linear algebra.

A Matrix is implemented as an STL vector (std::vector) of MVector's.

Definition at line 24 of file Matrix.h.


Constructor & Destructor Documentation

Matrix::Matrix int  nl = 0,
int  nc = 0,
double  x = 0.0
 

Constructor (also usable as default constructor).

The matrix is implemented as a std::vector of MVector's. Each MVector represents a line of the matrix. They have all the same size (which is the number of columns).

All MVector's are first created empty, then they are assigned to a temporary MVector (mv) with the correct initial values.

Parameters:
nl number of lines
nc number of columns
x the common value to initialize all components with

Definition at line 25 of file Matrix.cpp.

00026     : _nlines(nl), _ncols(nc), _lines(nl, MVector(nc, x))
00027 {
00028 }

Matrix::Matrix MVector  mv,
bool  transp = false
 

Constructor creating a Matrix from a MVector.

When called with only one parameter, this constructor aims at converting implicitely an MVector into a Matrix.

Parameters:
mv the MVector to convert
transp if true the matrix will be a line-matrix; otherwise it will be a column-one (it uses transpose())

Definition at line 39 of file Matrix.cpp.

References _lines, and transpose().

00040     : _nlines(1),
00041       _ncols(mv.size()),
00042       _lines(_ncols)
00043 {
00044     _lines[0] = mv;
00045     if (transp)
00046         transpose();
00047 }

Here is the call graph for this function:


Member Function Documentation

double Matrix::at int  i,
int  j
const [inline]
 

Another denotation for at(i,j) (version for a constant matrix).

Definition at line 142 of file Matrix.h.

00142 {return (*this)(i, j);}

double& Matrix::at int  i,
int  j
[inline]
 

Another denotation for at(i,j) (version for a non-constant matrix).

Definition at line 140 of file Matrix.h.

00140 {return (*this)(i, j);}

MVector Matrix::column int  j  )  const
 

Extract a column.

Return a copy of the column at index j, as an MVector.

Exceptions:
Out_Of_Bounds no such column

Definition at line 68 of file Matrix.cpp.

References _lines, _ncols, and _nlines.

Referenced by main(), operator *(), operator MVector(), and transpose().

00069 {
00070     if (j < 0 || j >= _ncols) throw Out_Of_Bounds();
00071     MVector mv(_nlines);
00072     for (int i = 0; i < _nlines; ++i)
00073         mv[i] = _lines[i][j];
00074     return mv;
00075 }

MVector Matrix::line int  i  )  const
 

Extract a line.

Return a copy of the line at index i, as an MVector.

Exceptions:
Out_Of_Bounds no such line

Definition at line 82 of file Matrix.cpp.

References _lines, and _nlines.

Referenced by main(), and operator *().

00083 {
00084     if (i < 0 || i >= _nlines) throw Out_Of_Bounds();
00085     return _lines[i];
00086 }

int Matrix::ncols  )  const [inline]
 

Number of columns.

Definition at line 85 of file Matrix.h.

References _ncols.

00085 {return _ncols;}

int Matrix::nlines  )  const [inline]
 

Number of lines.

Definition at line 83 of file Matrix.h.

References _nlines.

00083 {return _nlines;}

Matrix& Matrix::operator *= const Matrix mat  )  [inline]
 

Definition at line 74 of file Matrix.h.

00074 {return *this = *this * mat;}

Matrix::operator MVector  )  const
 

Conversion operator creating a MVector from a Matrix.

This operator creates an MVector from a Matrix, provided that one dimension of the matrix is exactly 1.

Exceptions:
Bad_Dimensions thrown if the above constraint is not fulfilled

Definition at line 55 of file Matrix.cpp.

References _lines, _ncols, _nlines, and column().

00056 {
00057     if (_nlines == 1) return _lines[0];
00058     else if (_ncols == 1)
00059         return column(0);
00060     else throw Bad_Dimensions();
00061 }

Here is the call graph for this function:

double Matrix::operator() int  i,
int  j
const
 

Access to element at indexes (i,j) (version for aconstant matrix).

Definition at line 186 of file Matrix.cpp.

References _lines, _ncols, and _nlines.

00187 {
00188     if (i < 0 || i >= _nlines || j < 0 || j >= _ncols) throw Out_Of_Bounds();
00189     return _lines[i][j];
00190 }

double & Matrix::operator() int  i,
int  j
 

Access to element at indexes (i,j) (version for a non-constant matrix).

Definition at line 180 of file Matrix.cpp.

References _lines, _ncols, and _nlines.

00181 {
00182     if (i < 0 || i >= _nlines || j < 0 || j >= _ncols) throw Out_Of_Bounds();
00183     return _lines[i][j];
00184 }

Matrix& Matrix::operator+= const Matrix mat  )  [inline]
 

Definition at line 72 of file Matrix.h.

00072 {return *this = *this + mat;}

Matrix& Matrix::operator-= const Matrix mat  )  [inline]
 

Definition at line 73 of file Matrix.h.

00073 {return *this = *this - mat;}

const MVector & Matrix::operator[] int  i  )  const
 

A reference to the line at index i (version for a constant matrix).

This operator makes it possible to use double indexing with a Matrix (mat[i][ j])

Definition at line 173 of file Matrix.cpp.

References _lines, and _nlines.

00174 {
00175     if (i < 0 || i >= _nlines) throw Out_Of_Bounds();
00176     return _lines[i];
00177 }

MVector & Matrix::operator[] int  i  ) 
 

A reference to the line at index i (version for a non-constant matrix).

This operator makes it possible to use double indexing with a Matrix (mat[i][ j])

Definition at line 163 of file Matrix.cpp.

References _lines, and _nlines.

00164 {
00165     if (i < 0 || i >= _nlines) throw Out_Of_Bounds();
00166     return _lines[i];
00167 }

Matrix Matrix::operator~  )  const [inline]
 

Another denotation for transpose.

Definition at line 95 of file Matrix.h.

References transpose().

00095 {return transpose();}

Here is the call graph for this function:

Matrix Matrix::transpose  )  const
 

Transpose the matrix.

Returns:
a copy of the matrix, but transposed

Definition at line 91 of file Matrix.cpp.

References _lines, _ncols, _nlines, and column().

Referenced by main(), Matrix(), and operator~().

00092 {
00093     Matrix mat(_ncols, _nlines);
00094     for (int i = 0; i < _ncols; ++i)
00095         mat._lines[i] = column(i);
00096     return mat;    
00097 }

Here is the call graph for this function:


Friends And Related Function Documentation

Matrix operator * const Matrix mat1,
const Matrix mat2
[friend]
 

Product.

Exceptions:
Bad_Dimensions thrown if the matrix dimensionsdo not fit

Definition at line 134 of file Matrix.cpp.

00135 {
00136     if (mat1._ncols != mat2._nlines) 
00137         throw Matrix::Bad_Dimensions();
00138 
00139     int nl = mat1._nlines;
00140     int nc = mat2._ncols;
00141     Matrix mat(nl, nc); 
00142     for (int i = 0; i < nl; ++i)
00143         for (int j = 0; j < nc; ++j)
00144             mat._lines[i][j] = mat1.line(i) * mat2.column(j);
00145     return mat;
00146 }

bool operator!= const Matrix mat1,
const Matrix mat2
[friend]
 

Unequality.

Definition at line 117 of file Matrix.h.

00118     {
00119         return ! (mat1 == mat2);
00120     }

Matrix operator+ const Matrix mat1,
const Matrix mat2
[friend]
 

Addition.

Exceptions:
Bad_Dimensions thrown if the matrix dimensions are different

Definition at line 102 of file Matrix.cpp.

00103 {
00104     if (mat1._nlines != mat2._nlines || mat1._ncols != mat2._ncols) 
00105         throw Matrix::Bad_Dimensions();
00106 
00107     int nl = mat1._nlines;
00108     int nc = mat1._ncols;
00109     Matrix mat(nl, nc);    
00110     for (int i = 0; i < nl; ++i)
00111         mat._lines[i] = mat1._lines[i] + mat2._lines[i];
00112     return mat;
00113 }

Matrix operator- const Matrix mat1,
const Matrix mat2
[friend]
 

Subtraction.

Exceptions:
Bad_Dimensions thrown if the matrix dimensions are different

Definition at line 118 of file Matrix.cpp.

00119 {
00120     if (mat1._nlines != mat2._nlines || mat1._ncols != mat2._ncols) 
00121         throw Matrix::Bad_Dimensions();
00122 
00123     int nl = mat1._nlines;
00124     int nc = mat1._ncols;
00125     Matrix mat(nl, nc);    
00126     for (int i = 0; i < nl; ++i)
00127         mat._lines[i] = mat1._lines[i] - mat2._lines[i];
00128     return mat;
00129 }

ostream& operator<< ostream &  os,
const Matrix mat
[friend]
 

IO operation: just display the contents.

We print each line of the matrix as an MVector on a single line. The whole matrix is also encolsed within square brackets.

Definition at line 196 of file Matrix.cpp.

00197 {
00198     os << "[\n";
00199     for (int i = 0; i < mat._nlines; ++i)
00200         os << "  " << mat._lines[i] << '\n';
00201     os << ']';
00202     return os;
00203 }

bool operator== const Matrix mat1,
const Matrix mat2
[friend]
 

Equality.

Note:
We use MVector equality

Definition at line 151 of file Matrix.cpp.

00152 {
00153     if (mat1._nlines != mat2._nlines) return false;
00154     for (int i = 0; i < mat1._nlines; ++i)
00155         if (mat1._lines[i] != mat2._lines[i]) return false;
00156     return true;
00157 }


Member Data Documentation

vector<MVector> Matrix::_lines [private]
 

The elements themselves.

Definition at line 30 of file Matrix.h.

Referenced by column(), line(), Matrix(), operator *(), operator MVector(), operator()(), operator+(), operator-(), operator<<(), operator==(), operator[](), and transpose().

int Matrix::_ncols [private]
 

Number of columns.

Definition at line 29 of file Matrix.h.

Referenced by column(), ncols(), operator *(), operator MVector(), operator()(), operator+(), operator-(), and transpose().

int Matrix::_nlines [private]
 

Number of lines.

Definition at line 28 of file Matrix.h.

Referenced by column(), line(), nlines(), operator *(), operator MVector(), operator()(), operator+(), operator-(), operator<<(), operator==(), operator[](), and transpose().


The documentation for this class was generated from the following files:
Generated on Mon Dec 12 18:25:45 2005 for Vectors_and_Matrices by  doxygen 1.4.3