Robot Simulator of the Robotics Group for Self-Organization of Control  0.8.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Matrix Class Reference

Matrix type. More...

#include <matrix.h>

Inheritance diagram for Matrix:
Collaboration diagram for Matrix:

Public Member Functions

 Matrix ()
 default constructor: zero matrix (0x0) More...
 
 Matrix (I _m, I _n, const D *_data=0)
 constucts a matrix with the given size. More...
 
 Matrix (I _m, I _n, D def)
 constucts a matrix with the given size and fills it with the default value More...
 
 Matrix (const Matrix &c)
 constucts a instance on the base of a deep copy of the given matrix More...
 
 Matrix (Matrix &&c)
 copy move constructor More...
 
 ~Matrix ()
 
I getM () const
 
I getN () const
 
I size () const
 
D val (I i, I j) const
 
Dval (I i, I j)
 
D valDef0 (I i, I j) const
 
void set (I _m, I _n, const D *_data=0)
 sets the size of the matrix and maybe the data if given (row-wise). More...
 
void set (const D *_data)
 sets the data (row-wise). More...
 
Matrix row (I index) const
 
Matrix rows (I startindex, I endindex) const
 
Matrix column (I index) const
 
Matrix columns (I startindex, I endindex) const
 
int convertToBuffer (D *buffer, I len) const
 stores the content of the matrix (row-wise) in the given buffer More...
 
std::list< DconvertToList () const
 
const DunsafeGetData () const
 returns a pointer to the data. UNSAFE!!! More...
 
bool store (FILE *f) const
 stores the Matrix into the given file stream (same as write) More...
 
bool restore (FILE *f)
 reads a Matrix from the given file stream uses read (or old binary format) More...
 
bool write (FILE *f) const
 writes the Matrix into the given file stream (ascii) More...
 
bool read (FILE *f, bool skipIdentifier=false)
 reads a Matrix from the given file stream (ascii) More...
 
void add (const Matrix &a, const Matrix &b)
 addition: this = a + b More...
 
void add (const Matrix &a, const D &summand)
 
void sub (const Matrix &a, const Matrix &b)
 add scalar to each element More...
 
void mult (const Matrix &a, const Matrix &b)
 multiplication: this = a * b More...
 
void mult (const Matrix &a, const D &fac)
 scaling: this = a * fac More...
 
void exp (const Matrix &a, int exponent)
 exponent, this = a^i, More...
 
bool isNulltimesNull () const
 returns true if matrix is a 0x0 matrix More...
 
bool isVector () const
 returns true if matrix is a vector More...
 
bool equals (const Matrix &a) const
 bytewise comparison (compares data buffer bytewise, which implies that n1*m1 == n2*m2 but not necessarily n1==n2) More...
 
bool hasSameSizeAs (const Matrix &a) const
 
Matrix pseudoInverse (const D &lambda=1e-8) const
 calculates the pseudoinverse, depending on the shape of the matrix the left or right pseudoinverse is used. More...
 
Matrix secureInverse () const
 calculates the secure inverse of a square matrix. More...
 
bool hasNormalEntries () const
 returns true if all entries are normal floating point numbers, otherwise false (e.g. More...
 
Matrix map (D(*fun)(D)) const
 maps the matrix to a new matrix with all elements mapped with the given function More...
 
Matrix mapP (D param, D(*fun)(D, D)) const
 like map but with additional double parameter for the mapping function (first argument of fun is parameter, the second is the value) More...
 
Matrix mapP (void *param, D(*fun)(void *, D)) const
 like map but with additional arbitrary parameter for the mapping function More...
 
Matrix multrowwise (const Matrix &factors) const
 row-wise multiplication More...
 
Matrix multcolwise (const Matrix &factors) const
 column-wise multiplication More...
 
Matrix multMT () const
 optimised multiplication of Matrix with its transposed: M * M^T More...
 
Matrix multTM () const
 optimised multiplication of transpsoed of Matrix with itself: M^T * M More...
 
D elementProduct () const
 returns the product of all elements ( $ \Pi_{ij} m_{ij} $) More...
 
D elementSum () const
 returns the sum of all elements ( $ \sum_{ij} m_{ij} $) More...
 
D norm_sqr () const
 returns the sum of all squares of all elements ( $ \sum_{ij} m_{ij}^2 $) this is also known as the square of the Frobenius norm. More...
 
Matrix above (const Matrix &a) const
 returns a matrix that consists of this matrix above A (number of rows is getM + a.getM()) More...
 
Matrix beside (const Matrix &a) const
 returns a matrix that consists of this left beside A (number of columns is getN + a.getN()) More...
 
Matrixoperator= (const Matrix &c)
 deep copy More...
 
Matrixoperator= (Matrix &&c)
 deep copy move operator More...
 
Matrix operator+ (const Matrix &sum) const
 sum of two matrices More...
 
Matrix operator- (const Matrix &sum) const
 difference of two matrices More...
 
Matrix operator* (const Matrix &fac) const
 matrix product More...
 
Matrix operator* (const D &fac) const
 product with scalar (D) (only right side) More...
 
Matrix operator^ (int exponent) const
 special matrix potence: More...
 
Matrix operator& (const Matrix &b) const
 row-wise multiplication More...
 
Matrixoperator+= (const Matrix &c)
 combined assigment operator (higher performance) More...
 
Matrixoperator-= (const Matrix &c)
 combined assigment operator (higher performance) More...
 
Matrixoperator*= (const Matrix &c)
 combined assigment operator (higher performance) More...
 
Matrixoperator*= (const D &fac)
 combined assigment operator (higher performance) More...
 
Matrixoperator&= (const Matrix &c)
 combined assigment operator (higher performance) More...
 
bool operator== (const Matrix &c) const
 comparison operator (compares elements with tolerance distance of COMPARE_EPS) More...
 
void copy (const Matrix &c)
 performs a deep copy of the given matrix More...
 
MatrixtoTranspose ()
 inplace transpose More...
 
MatrixtoZero ()
 inplace converts matrix to zero matrix More...
 
MatrixtoId ()
 inplace converts matrix to identity (use ^0 to get a copy version of it) More...
 
MatrixtoSum (const Matrix &a)
 inplace addition: this = this + a More...
 
MatrixtoSum (const D &sum)
 inplace addition: this = this + a, where a is a scalar More...
 
MatrixtoDiff (const Matrix &a)
 inplace subtraction: this = this - a More...
 
MatrixtoMult (const Matrix &a)
 inplace multiplication: this = this * a More...
 
MatrixtoMult (const D &fac)
 inplace multiplication with scalar: this = this*fac More...
 
MatrixtoExp (int exponent)
 special inplace matrix power: More...
 
MatrixtoMap (D(*fun)(D))
 inplace mapping of matrix elements (element-wise application) More...
 
MatrixtoMapP (D param, D(*fun)(D, D))
 like toMap, but with an extra double parameter for the mapping function. More...
 
MatrixtoMapP (void *param, D(*fun)(void *, D))
 like toMap, but with an extra arbitrary parameter for the mapping function. More...
 
MatrixtoMap2 (D(*fun)(D, D), const Matrix &b)
 like toMap, but with using 2 matrices More...
 
MatrixtoMap2P (D param, D(*fun)(D, D, D), const Matrix &b)
 like toMap2, but with additional parameter More...
 
MatrixtoMap2P (void *param, D(*fun)(void *, D, D), const Matrix &b)
 like toMap2P, but with arbitrary parameter More...
 
MatrixtoMultrowwise (const Matrix &factors)
 Inplace row-wise multiplication. More...
 
MatrixtoMultcolwise (const Matrix &factors)
 Inplace column-wise multiplication. More...
 
MatrixtoAbove (const Matrix &a)
 sets the matrix a below this matrix More...
 
MatrixtoBeside (const Matrix &a)
 sets the matrix a right beside this matrix More...
 
MatrixtoSort ()
 sorts the matrix (rowwise) More...
 
Matrixreshape (I m, I n)
 reshapes the matrix without destroying the data. More...
 
MatrixpluslambdaI (double lambda=1e-8)
 adds the given value to the diagonal More...
 
MatrixaddRows (I numberRows, const D *_data=0)
 adds one or more rows to the existing matrix and fills it with the given data More...
 
MatrixaddRows (I numberRows, const Matrix &dataMatrix)
 same as toAbove(dataMatrix) More...
 
MatrixaddColumns (I numberColumns, const D *_data=0)
 adds one or more columns to the existing matrix same as toBeside(Matrix(getM, numberColumns, _data)) More...
 
MatrixaddColumns (I numberColumns, const Matrix &dataMatrix)
 same as toBeside(dataMatrix) More...
 
MatrixremoveRows (I numberRows)
 removes one or more rows from the end if an existing matrix (inplace!), same as reshape(getM()-numberRows, getN()); More...
 
MatrixremoveColumns (I numberColumns)
 removes one or more columns from the end of the existing matrix (inplace!) resets the size of the matrix and deletes the appropiate data. More...
 
- Public Member Functions inherited from Storeable
virtual ~Storeable ()
 
bool storeToFile (const char *filename) const
 Provided for convenience. More...
 
bool restoreFromFile (const char *filename)
 Provided for convenience. More...
 

Static Public Member Functions

static Matrix map2 (D(*fun)(D, D), const Matrix &a, const Matrix &b)
 binary map operator for matrices. More...
 
static Matrix map2P (D param, D(*fun)(D, D, D), const Matrix &a, const Matrix &b)
 like map2 but with additional parameter. More...
 
static Matrix map2P (void *param, D(*fun)(void *, D, D), const Matrix &a, const Matrix &b)
 like map2P but with arbitrary paramters (void*) instead of double More...
 

Friends

std::ostream & operator<< (std::ostream &, const Matrix &)
 printing operator: output format: mxn (
row0
..rown
) where rowX is tab seperated list of values More...
 

Detailed Description

Matrix type.

Type D is datatype of matrix elements, which is fixed to double. Type I is the indextype of matrix elements, which is fixed to unsigned int. There are basicly two different types of operation: Inplace operations and copy operations. Please use the latter ones unless you know what you are doing. Just in case of critical performance optimisation use the inplace operations. The most convinient way is to use the overloaded operators (like + * ...). All constructed matrices are initialized with zero elements (unless data is given). All functions perform range checks if in debug mode (i.e. if NDEBUG is not defined). Please use debug version (default) for testing

See Also
examples/matrix/matrixexample.cpp
Author
Georg Martius
Examples:
integration/main.cpp, and matrix/matrixexample.cpp.

Constructor & Destructor Documentation

Matrix ( )
inline

default constructor: zero matrix (0x0)

Matrix ( I  _m,
I  _n,
const D _data = 0 
)

constucts a matrix with the given size.

If _data is null then the matrix is filled with zeros. otherwise matrix will be filled with _data in a row-wise manner. In this case _data must be at least _m*_n elements long

Matrix ( I  _m,
I  _n,
D  def 
)

constucts a matrix with the given size and fills it with the default value

Matrix ( const Matrix c)

constucts a instance on the base of a deep copy of the given matrix

Matrix ( Matrix &&  c)

copy move constructor

~Matrix ( )
inline

Member Function Documentation

Matrix above ( const Matrix a) const

returns a matrix that consists of this matrix above A (number of rows is getM + a.getM())

returns a matrix that consists of b below this

void add ( const Matrix a,
const Matrix b 
)

addition: this = a + b

void add ( const Matrix a,
const D summand 
)
Matrix & addColumns ( I  numberColumns,
const D _data = 0 
)

adds one or more columns to the existing matrix same as toBeside(Matrix(getM, numberColumns, _data))

See Also
toBeside()
Parameters
numberColumnsnumber of columns to add (this extends n)
_datadata to add
Returns
the address of the matrix itself
Matrix & addColumns ( I  numberColumns,
const Matrix dataMatrix 
)

same as toBeside(dataMatrix)

See Also
toBeside()
Parameters
numberColumnsnumber of columns to add (unused)
dataMatrixmatrix which contains the data of the new rows
Returns
the address of the matrix itself
Matrix & addRows ( I  numberRows,
const D _data = 0 
)

adds one or more rows to the existing matrix and fills it with the given data

same as toAbove(Matrix(numberRows,getN(),data))

Parameters
numberRowsnumber of rows to add (this extends m)
_datadata to add
Returns
the address of the matrix itself
Matrix & addRows ( I  numberRows,
const Matrix dataMatrix 
)

same as toAbove(dataMatrix)

Parameters
numberRowsnumber of rows to add (unused)
dataMatrixmatrix which contains the data of the new rows
Returns
the address of the matrix itself
Matrix beside ( const Matrix a) const

returns a matrix that consists of this left beside A (number of columns is getN + a.getN())

returns a matrix that consists of b right beside this

Matrix column ( I  index) const
Returns
column-vector(as Mx1 matrix) containing the index'th column
Examples:
matrix/matrixexample.cpp.
Matrix columns ( I  startindex,
I  endindex 
) const
Returns
submatrix (as MxK matrix) containing column from startindex to endindex inclusively (K=endindex-startindex) indices can be out of bounds, they are clipped in any case
int convertToBuffer ( D buffer,
I  len 
) const

stores the content of the matrix (row-wise) in the given buffer

Parameters
bufferBuffer for storing the elements (should have the length given by len)
lenLength of the provided buffer. In any case only min(len, getM()*getN()) elements are copied.
Returns
number of actually written elements
std::list< D > convertToList ( ) const
Returns
a list of the content of the matrix (row-wise)
void copy ( const Matrix c)
inline

performs a deep copy of the given matrix

D elementProduct ( ) const

returns the product of all elements ( $ \Pi_{ij} m_{ij} $)

returns the product of all elements

D elementSum ( ) const

returns the sum of all elements ( $ \sum_{ij} m_{ij} $)

returns the sum of all elements

bool equals ( const Matrix a) const

bytewise comparison (compares data buffer bytewise, which implies that n1*m1 == n2*m2 but not necessarily n1==n2)

void exp ( const Matrix a,
int  exponent 
)

exponent, this = a^i,

See Also
toExp
I getM ( ) const
inline
Returns
number of rows
I getN ( ) const
inline
Returns
number of columns
bool hasNormalEntries ( ) const

returns true if all entries are normal floating point numbers, otherwise false (e.g.

for nan and inf)

bool hasSameSizeAs ( const Matrix a) const
inline
Returns
true of a and this have the same size
bool isNulltimesNull ( ) const

returns true if matrix is a 0x0 matrix

ACCESSORS ///////////////////////////////////////////////////////////////.

returns true if matrix is a 0x0 matrix

bool isVector ( ) const

returns true if matrix is a vector

Matrix map ( D(*)(D fun) const

maps the matrix to a new matrix with all elements mapped with the given function

Examples:
matrix/matrixexample.cpp.
Matrix map2 ( D(*)(D, D fun,
const Matrix a,
const Matrix b 
)
static

binary map operator for matrices.

The resulting matrix consists of the function values applied to the elements of a and b. In haskell this would something like: map (uncurry . fun) $ zip a b

Matrix map2P ( D  param,
D(*)(D, D, D fun,
const Matrix a,
const Matrix b 
)
static

like map2 but with additional parameter.

The first argument of fun is the parameter and the second and third comes from the matrix elements. In haskell this would something like: map (uncurry . (fun p)) $ zip a b

Matrix map2P ( void *  param,
D(*)(void *, D, D fun,
const Matrix a,
const Matrix b 
)
static

like map2P but with arbitrary paramters (void*) instead of double

Matrix mapP ( D  param,
D(*)(D, D fun 
) const

like map but with additional double parameter for the mapping function (first argument of fun is parameter, the second is the value)

Matrix mapP ( void *  param,
D(*)(void *, D fun 
) const

like map but with additional arbitrary parameter for the mapping function

void mult ( const Matrix a,
const Matrix b 
)

multiplication: this = a * b

void mult ( const Matrix a,
const D fac 
)

scaling: this = a * fac

Matrix multcolwise ( const Matrix factors) const

column-wise multiplication

Parameters
factorscolumn vector (Mx1) of factors, one for each column
Matrix multMT ( ) const

optimised multiplication of Matrix with its transposed: M * M^T

Matrix multrowwise ( const Matrix factors) const

row-wise multiplication

Parameters
factorscolumn vector (Mx1) of factors, one for each row
Matrix multTM ( ) const

optimised multiplication of transpsoed of Matrix with itself: M^T * M

D norm_sqr ( ) const

returns the sum of all squares of all elements ( $ \sum_{ij} m_{ij}^2 $) this is also known as the square of the Frobenius norm.

returns the sum of all elements

Matrix operator& ( const Matrix b) const

row-wise multiplication

Matrix& operator&= ( const Matrix c)
inline

combined assigment operator (higher performance)

Matrix operator* ( const Matrix fac) const

matrix product

Matrix operator* ( const D fac) const

product with scalar (D) (only right side)

product with scalar (double)

Matrix& operator*= ( const Matrix c)
inline

combined assigment operator (higher performance)

Matrix& operator*= ( const D fac)
inline

combined assigment operator (higher performance)

Matrix operator+ ( const Matrix sum) const

sum of two matrices

Matrix& operator+= ( const Matrix c)
inline

combined assigment operator (higher performance)

Matrix operator- ( const Matrix sum) const

difference of two matrices

Matrix& operator-= ( const Matrix c)
inline

combined assigment operator (higher performance)

Matrix& operator= ( const Matrix c)
inline

deep copy

Matrix & operator= ( Matrix &&  c)

deep copy move operator

bool operator== ( const Matrix c) const

comparison operator (compares elements with tolerance distance of COMPARE_EPS)

Matrix operator^ ( int  exponent) const

special matrix potence:

Parameters
exponent-1 -> inverse; 0 -> Identity Matrix; 1 -> itself; T -> Transpose
exp-1 -> inverse; 0 -> Identity Matrix; 1 -> itself; 2-> Matrix*Matrix^T T -> Transpose
Matrix & pluslambdaI ( double  lambda = 1e-8)

adds the given value to the diagonal

Matrix pseudoInverse ( const D lambda = 1e-8) const

calculates the pseudoinverse, depending on the shape of the matrix the left or right pseudoinverse is used.

If the matrix has more columns than rows then we use

\[A^{+} = (A^T A + \lambda \mathbb I)^{-1}A^T\]

otherwise

\[A^{+} = A^T(A A^T + \lambda \mathbb I)^{-1}\]

bool read ( FILE *  f,
bool  skipIdentifier = false 
)

reads a Matrix from the given file stream (ascii)

Matrix & removeColumns ( I  numberColumns)

removes one or more columns from the end of the existing matrix (inplace!) resets the size of the matrix and deletes the appropiate data.

Parameters
numberColumnsnumber of columns to remove (this reduces n)
Returns
the address of the matrix itself
Matrix & removeRows ( I  numberRows)

removes one or more rows from the end if an existing matrix (inplace!), same as reshape(getM()-numberRows, getN());

Parameters
numberRowsnumber of rows to remove (from the end) (this reduces m)
Returns
the address of the matrix itself
Matrix & reshape ( I  m,
I  n 
)

reshapes the matrix without destroying the data.

Remember: The data is stored rowwise.

Only shrinking is allowed i.e. m*n must be lower or equal to getM()*getN()

bool restore ( FILE *  f)
virtual

reads a Matrix from the given file stream uses read (or old binary format)

reads a Matrix from the given file stream ASCII (can load old binary format)

Implements Storeable.

Matrix row ( I  index) const
Returns
row-vector(as 1xN matrix) containing the index'th row
Examples:
matrix/matrixexample.cpp.
Matrix rows ( I  startindex,
I  endindex 
) const
Returns
submatrix (as KxN matrix) containing row from startindex to endindex inclusively (K=stopindex-endindex) indices can be out of bounds, they are clipped in any case
Matrix secureInverse ( ) const

calculates the secure inverse of a square matrix.

If singular then the pseudoinverse is used.

void set ( I  _m,
I  _n,
const D _data = 0 
)

sets the size of the matrix and maybe the data if given (row-wise).

If data=null then the matrix is set to zero

See Also
toZero()
constructor Matrix(m,n,data)
Examples:
matrix/matrixexample.cpp.
void set ( const D _data)

sets the data (row-wise).

Parameters
_dataif null then matrix elements are set to zero otherwise the field MUST have the length should be getM()*getN()
I size ( ) const
inline
Returns
number number elements in the matrix (getM()*getN())
bool store ( FILE *  f) const
virtual

stores the Matrix into the given file stream (same as write)

stores the Matrix into the given file stream ASCII

Implements Storeable.

void sub ( const Matrix a,
const Matrix b 
)

add scalar to each element

subtraction: this = a - b

Matrix & toAbove ( const Matrix a)

sets the matrix a below this matrix

Matrix & toBeside ( const Matrix a)

sets the matrix a right beside this matrix

Matrix & toDiff ( const Matrix a)

inplace subtraction: this = this - a

Matrix & toExp ( int  exponent)

special inplace matrix power:

Parameters
exponent-1 -> inverse; (matrix MUST be SQUARE and NONZERO) 0 -> Identity Matrix; 1 -> itself; n -> n-th power; T -> Transpose
Matrix & toId ( )

inplace converts matrix to identity (use ^0 to get a copy version of it)

Examples:
integration/main.cpp.
Matrix & toMap ( D(*)(D fun)

inplace mapping of matrix elements (element-wise application)

Matrix & toMap2 ( D(*)(D, D fun,
const Matrix b 
)

like toMap, but with using 2 matrices

Matrix & toMap2P ( D  param,
D(*)(D, D, D fun,
const Matrix b 
)

like toMap2, but with additional parameter

Matrix & toMap2P ( void *  param,
D(*)(void *, D, D fun,
const Matrix b 
)

like toMap2P, but with arbitrary parameter

Matrix & toMapP ( D  param,
D(*)(D, D fun 
)

like toMap, but with an extra double parameter for the mapping function.

Matrix & toMapP ( void *  param,
D(*)(void *, D fun 
)

like toMap, but with an extra arbitrary parameter for the mapping function.

Matrix & toMult ( const Matrix a)

inplace multiplication: this = this * a

Matrix & toMult ( const D fac)

inplace multiplication with scalar: this = this*fac

Matrix & toMultcolwise ( const Matrix factors)

Inplace column-wise multiplication.

Parameters
factorscolumn vector of factors, one for each column
Matrix & toMultrowwise ( const Matrix factors)

Inplace row-wise multiplication.

Parameters
factorscolumn vector of factors, one for each row
Matrix & toSort ( )

sorts the matrix (rowwise)

Matrix & toSum ( const Matrix a)

inplace addition: this = this + a

Matrix & toSum ( const D sum)

inplace addition: this = this + a, where a is a scalar

Matrix & toTranspose ( )

inplace transpose

Matrix & toZero ( )

inplace converts matrix to zero matrix

const D* unsafeGetData ( ) const
inline

returns a pointer to the data. UNSAFE!!!

D val ( I  i,
I  j 
) const
inline
Returns
element at position i,j (row, column index)
Examples:
matrix/matrixexample.cpp.
D& val ( I  i,
I  j 
)
inline
Returns
reference to element at position i,j (can be used as left side value)
D valDef0 ( I  i,
I  j 
) const
inline
Returns
element at position i,j (row, column index) and 0 if out of bounds
bool write ( FILE *  f) const

writes the Matrix into the given file stream (ascii)

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  str,
const Matrix mat 
)
friend

printing operator: output format: mxn (
row0
..rown
) where rowX is tab seperated list of values


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