NeuroLab
Matrix Class Reference

a matrix class More...

Inheritance diagram for Matrix:

Public Member Functions

 Matrix ()
 default constructor: constructs a zero-dimensional matrix (a scalar).
 
 Matrix (int i)
 constructs a one-dimensional matrix
 
 Matrix (int i, int j)
 constructs a two-dimensional matrix
 
 Matrix (int i, int j, int k)
 constructs a three-dimensional matrix
 
 Matrix (int i, int j, int k, int l)
 constructs a four-dimensional matrix
 
 Matrix (int dimensions, int *sizes)
 constructs an n-dimensional matrix
 
virtual ~Matrix ()
 destructs the matrix
 
MatrixsetName (const string &)
 set name
 
string sName ()
 get name
 
int nDimension ()
 Get number of dimensions.
 
int nSize (int n)
 Get size for dimension.
 
Unit getUnit (int n)
 Unit of dimension n.
 
void setPhysical (int n, Physical p)
 Set physical dimension of dimension n. More...
 
void setPhysical (Physical p)
 Set main physical dimension. More...
 
Physical pPhysical (int n)
 Get physical dimension for dimension n. More...
 
Matrix operator[] (int i)
 retrieve slices or values More...
 
Matrixremove (vector< int >)
 remove one or more dimensions More...
 
void operator++ (int i)
 increment a zero-dimensional matrix
 
void operator= (double d)
 assignment More...
 
Matrixoperator+= (const Matrix &)
 addings More...
 
Matrix operator() (int i, int j)
 retrieve ranges - not implemented yet! More...
 
double to_d ()
 convert to double More...
 
virtual double getSum ()
 Sum of elements. More...
 
double getMaxValue ()
 get maximal value
 
double getMinValue ()
 get minimal value
 
void replaceNan (double d)
 Replaces NaNs. More...
 
virtual Matrixmultiply (double d)
 Multiply with a scalar. More...
 
virtual Matrixadd (double d)
 Add a scalar. More...
 
virtual Matrixexp ()
 Exponential. More...
 
virtual Matrixsqrt ()
 Sqare root for each element. More...
 
Matrixtranspose ()
 Transpose matrix. More...
 
virtual Matrixintegrate ()
 Integrate. More...
 
virtual Matrixdifferentiate ()
 Differentiate. More...
 
Matrixoperator= (const Matrix &m)
 assignment
 
 Matrix (const Matrix &m)
 Copy constructor. More...
 
- Public Member Functions inherited from Physical
 Physical ()
 Construct.
 
 Physical (const Physical &)
 Copy.
 
 Physical (string name)
 Construct.
 
 Physical (string name, string unitPrefix, string unitSymbol)
 Construct.
 
 Physical (string name, Unit unit)
 Construct.
 
virtual string getDescription ()
 Retrive the physical name. More...
 
virtual string getPhysicalDescription ()
 Returns the physical description. More...
 
virtual string getUnitName ()
 Returns the unit name.
 
virtual string getUnitSymbol ()
 Returns the unit name.
 
virtual void setDescription (string name)
 Set the name. More...
 
virtual void setPhysicalDescription (string name)
 Set the name. Same as setDescription(). More...
 
virtual void setUnit (Unit u)
 Set the unit.
 
virtual void setUnitPrefix (int n)
 Set unit prefix. More...
 
virtual Unit getUnit () const
 Retrieve the unit.
 

Friends

ostream & operator<< (ostream &s, const Matrix &m)
 append matrix to a stream
 
Matrix operator- (const Matrix &m0, const Matrix &m1)
 subract two matrices
 
Matrix operator+ (const Matrix &m0, const Matrix &m1)
 add two matrices
 

Detailed Description

a matrix class

Quick matrix class to implement matrices which know their own dimension. Supports any dimension, slicing, subindexing, as well as the matlab-known funktions cumsum, cummult and convolve (not implemented yet). Dimensions 0 up to 4 have shortcuts. The data inside the matrix are physical values and therefore have a unit (use Physical::getUnit() from the Physical class), and each dimension has a separate unit as well (use Matrix::getUnit(n)). Example: if you have voltage data, which you collected over time and length, you have a two-dimensional matrix; the matrix unit should be voltage, and one dimension should have cm, mm or whatever, and the other dimension ms, s etc.

Todo:
Implement Matrix multiplication and the (Matlab-known) functions cumsum, cummult and convolve for n-dimensional matrixes.

Constructor & Destructor Documentation

Matrix::Matrix ( const Matrix m)

Copy constructor.

This creates a shallow copy, where the new Matrix takes over the memory, i.e. copy() is called with deep=false.

Member Function Documentation

virtual Matrix& Matrix::add ( double  d)
virtual

Add a scalar.

Adds a scalar to each element of the matrix. A reference to the matrix itself is returned for convenience, so that you can write things like m.add( a ).multiply( b )

Reimplemented in Graph.

virtual Matrix& Matrix::differentiate ( )
virtual

Differentiate.

This function differentiates over the last dimension of the matrix. A difference-matrix is produced, where m'[0] = 0.0, m'[1] = m[1]-m[0], m'[n] = m[n]-m[n-1]. Doing this on an integrated matrix, m.integrate().differentiate(), restores the original matrix, but m[0] will be lost (set to 0.0).

Reimplemented in Graph.

virtual Matrix& Matrix::exp ( )
virtual

Exponential.

Transforms each element into its exponential. A reference to the matrix itself is returned for convenience, so that you can write things like m.exp().multipliy( a ).add( b )

Reimplemented in Graph.

virtual double Matrix::getSum ( )
virtual

Sum of elements.

This returns a sum of all elements.

Reimplemented in Graph.

virtual Matrix& Matrix::integrate ( )
virtual

Integrate.

This function integrates over the last dimension of the matrix. A cumulative sum is produced, where m'[0] = m[0], m'[1] = m[0]+m[1], m'[n] = m[0] + m[1] + ... + m[n]. Doing this on a differentiated matrix, m.differentiate().integrate(), retrieves the original matrix, shifted so that m[0] = 0.0, and adding the original m[0] restores the original matrix.

Reimplemented in Graph.

virtual Matrix& Matrix::multiply ( double  d)
virtual

Multiply with a scalar.

Multiplies the matrix with a scalar. A reference to the matrix itself is returned for convenience, so that you can write things like m.multipliy(3).add(4)

Reimplemented in Graph.

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

retrieve ranges - not implemented yet!

(Note: these are references to the original matrix data.) Retrieves a matrix from i to j. Example: a = Matrix(6,6,6); (a will be a 6x6x6-Matrix). To get something like a[2..3][4..5][1..2] you write: a(2,3)(4,5)(1,2).

Matrix& Matrix::operator+= ( const Matrix )

addings

Adds another matrix. Shapes must be equal, otherwise nothing happens.

void Matrix::operator= ( double  d)

assignment

This assigns the elements of the matrix. For assigning ranges use a for-loop.

Matrix Matrix::operator[] ( int  i)

retrieve slices or values

(Note: these are references to the original matrix data.)

Physical Matrix::pPhysical ( int  n)

Get physical dimension for dimension n.

Get the physical dimension of a dimension of the matrix.

Parameters
nThe dimension
Matrix& Matrix::remove ( vector< int >  )

remove one or more dimensions

input vector contains removal information: -1 means "do not remove", positive integer n means "remove dimension n". Example: if m is a (2,4,3) matrix, then m.remove([-1,-1,2]) will return a (2,4,2) matrix, where all m[x,x,2] data has been removed.

void Matrix::replaceNan ( double  d)

Replaces NaNs.

Replace all NaN-valued members in the matrix with a given value.

Parameters
dValue which should replace any NaN-valued member.
void Matrix::setPhysical ( int  n,
Physical  p 
)

Set physical dimension of dimension n.

Set a dimension n to a physical dimension, like f.i. 'acceleration'. If the matrix is plotted with a Display object, physical name and unit are shown on the appropriate axis.

Parameters
nDimension
pPhysical dimension
void Matrix::setPhysical ( Physical  p)

Set main physical dimension.

Set the data inside the matrix to a physical dimension, like 'capacity'. If the matrix is plotted with a Display object, physical name and unit are shown on the appropriate axis.

Parameters
pPhysical dimension
virtual Matrix& Matrix::sqrt ( )
virtual

Sqare root for each element.

Transforms each element into its square root. A reference to the matrix itself is returned for convenience, so that you can write things like m.sqrt().multipliy( a ).add( b )

Reimplemented in Graph.

double Matrix::to_d ( )

convert to double

This is mainly useful for zero-dimension matrixes.

Matrix& Matrix::transpose ( )

Transpose matrix.

This changes the matrix data, so that m[x1][x2][x3]...[xn] becomes m[xn][xn-1][xn-2]...[x1]. A reference to the matrix itself is returned for convenience, so that you can write things like m.transpose().multipliy( a ).add( b )