Robotsystem of the Robot Group Leipzig: Matrix Class Reference

- Main Page
- Namespaces
- Classes
- Files
- Directories
- Examples

- Search for |

- Alphabetical List
- Class List
- Class Hierarchy
- Class Members

# Matrix Class Reference

Matrix type. More...

`#include <matrix.h>`

Inherits Storeable.

Inheritance diagram for Matrix:

[legend]
Collaboration diagram for Matrix:

[legend]
List of all members.

## Public Member Functions
| Matrix ()
| default constructor: zero matrix (0x0)
| Matrix (unsigned short _m, unsigned short _n, const D *_data=0)
| constucts a matrix with the given size.
| Matrix (const Matrix &c)
| constucts a instance on the base of a deep copy of the given matrix
| ~Matrix ()
unsigned short | getM () const
unsigned short | getN () const
D | val (unsigned short i, unsigned short j) const
D & | val (unsigned short i, unsigned short j)
D | valDef0 (short i, short j) const
void | set (unsigned short _m, unsigned short _n, const D *_data=0)
| sets the size of the matrix and maybe the data if given (row-wise).
void | set (const D *_data)
| sets the data (row-wise).
Matrix | row (unsigned short index) const
Matrix | column (unsigned short index) const
Matrix | columns (unsigned short startindex, unsigned short endindex) const
int | convertToBuffer (D *buffer, unsigned int len) const
| stores the content of the matrix (row-wise) in the given buffer
std::list< D > | convertToList () const
bool | store (FILE *f) const
| stores the Matrix into the given file stream (binary)
bool | restore (FILE *f)
| reads a Matrix from the given file stream (binary)
bool | write (FILE *f) const
| writes the Matrix into the given file stream (ascii)
bool | read (FILE *f)
| reads a Matrix from the given file stream (ascii)
void | add (const Matrix &a, const Matrix &b)
| addition: this = a + b
void | sub (const Matrix &a, const Matrix &b)
| subtraction: this = a - b
void | mult (const Matrix &a, const Matrix &b)
| multiplication: this = a * b
void | mult (const Matrix &a, const D &fac)
| scaling: this = a * fac
bool | isNulltimesNull ()
| returns true if matrix is a 0x0 matrix
Matrix | map (D(*fun)(D)) const
| maps the matrix to a new matrix with all elements mapped with the given function
Matrix | mapP (void *param, D(*fun)(void *, D)) const
| like map but with additional parameter for the mapping function
Matrix | multrowwise (const Matrix &factors) const
| row-wise multiplication
Matrix | multcolwise (const Matrix &factors) const
| column-wise multiplication
Matrix | multMT () const
| optimised multiplication of Matrix with its transposed: M * M^T
Matrix | multTM () const
| optimised multiplication of transpsoed of Matrix with itself: M^T * M
D | elementProduct () const
| returns the product of all elements
D | elementSum () const
| returns the sum of all elements
Matrix | above (const Matrix &a) const
| returns a matrix that consists of b below this
Matrix & | operator= (const Matrix &c)
| deep copy
Matrix | operator+ (const Matrix &sum) const
Matrix | operator- (const Matrix &sum) const
Matrix | operator * (const Matrix &fac) const
| matrix product
Matrix | operator * (const D &fac) const
| product with scalar (double)
Matrix | operator^ (int exponent) const
| special matrix potence:
Matrix & | operator+= (const Matrix &c)
| performant combined assigment operators
Matrix & | operator-= (const Matrix &c)
Matrix & | operator *= (const Matrix &c)
Matrix & | operator *= (const D &fac)
bool | operator== (const Matrix &c) const
| comparison operator (compares elements with tolerance distance of COMPARE_EPS)
void | copy (const Matrix &c)
| performs a deep copy of the given matrix
void | toTranspose ()
| inplace transpose
void | toZero ()
| inplace converts matrix to zero matrix
void | toId ()
| inplace converts matrix to identity (use ^0 to get a copy version of it)
void | toSum (const Matrix &a)
| inplace addition: this = this + a
void | toDiff (const Matrix &a)
| inplace subtraction: this = this - a
void | toMult (const D &fac)
| inplace multiplication with scalar: this = this*fac
void | toExp (int exponent)
| special inplace matrix potence:
void | toMap (D(*fun)(D))
| inplace mapping of matrix elements (element-wise application)
void | toMapP (void *param, D(*fun)(void *, D))
| like toMap, but with an extra parameter for the mapping function.
void | toMultrowwise (const Matrix &factors)
| Inplace row-wise multiplication.
void | toMultcolwise (const Matrix &factors)
| Inplace column-wise multiplication.
void | toAbove (const Matrix &a)
| sets the matrix a below (this) matrix
## Static Public Member Functions
static Matrix | map2 (D(*fun)(D, D), const Matrix &a, const Matrix &b)
| binary map operator for matrices.
## Friends
std::ostream & | operator<< (std::ostream &, const Matrix &)
| printing operator: output format: mxn ( row0 .

## Detailed Description

Matrix type.

Type D is datatype of matrix elements, which is fixed to double. 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 initialised with zero elements (unless data is given). All functions perform range checks if in debug mode (NDEBUG is not defined). Please use -lmatrix_debug for testing.

Author:
  Georg Martius

Examples:

  integration/main.cpp, main.cpp, matrix/matrixexample.cpp, and sphererobot3masses.cpp.

## Constructor & Destructor Documentation

Matrix | ( |  | ) | `[inline]`

default constructor: zero matrix (0x0)

Matrix | ( | unsigned short | _m,
|  | unsigned short | _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 | ( | const Matrix & | c | ) |

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

~Matrix | ( |  | ) | `[inline]`

## Member Function Documentation

Matrix above | ( | const Matrix & | a | ) | const

returns a matrix that consists of b below this

void add | ( | const Matrix & | a,
|  | const Matrix & | b |
| ) |  |  |

addition: this = a + b

Matrix column | ( | unsigned short | index | ) | const

Returns:
  column-vector(as Nx1 matrix) containing the index'th column

Examples:
  matrix/matrixexample.cpp, and sphererobot3masses.cpp.

Matrix columns | ( | unsigned short | startindex,
|  | unsigned short | endindex |
| ) |  |  | const

Returns:
  submatrix (as NxK matrix) containing column from startindex to endindex inclusively (K=stopindex-endindex) indices can be out of bounds, they are clipped in any case

int convertToBuffer | ( | D * | buffer,
|  | unsigned int | len |
| ) |  |  | const

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

Parameters:

  | buffer | Buffer for storing the elements (should have the length given by len)
  | len | Length 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

D elementSum | ( |  | ) | const

returns the sum of all elements

unsigned short getM | ( |  | ) | const`[inline]`

Returns:
  number of rows

unsigned short getN | ( |  | ) | const`[inline]`

Returns:
  number of columns

bool isNulltimesNull | ( |  | ) |

returns true if matrix is a 0x0 matrix

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.

static Matrix map2 | ( | D(*)(D, D) | fun,
|  | const Matrix & | a,
|  | const Matrix & | b |
| ) |  |  | `[inline, 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 mapP | ( | void * | param,
|  | D(*)(void *, D) | fun |
| ) |  |  | const

like map but with additional parameter for the mapping function

void mult | ( | const Matrix & | a,
|  | const D & | fac |
| ) |  |  |

scaling: this = a * fac

void mult | ( | const Matrix & | a,
|  | const Matrix & | b |
| ) |  |  |

multiplication: this = a * b

Matrix multcolwise | ( | const Matrix & | factors | ) | const

column-wise multiplication

Parameters:

  | factors | column 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:

  | factors | column vector (Mx1) of factors, one for each row

Examples:
  matrix/matrixexample.cpp.

Matrix multTM | ( |  | ) | const

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

Matrix operator * | ( | const D & | fac | ) | const

product with scalar (double)

Matrix operator * | ( | const Matrix & | fac | ) | const

matrix product

Matrix& operator *= | ( | const D & | fac | ) | `[inline]`

Matrix& operator *= | ( | const Matrix & | c | ) | `[inline]`

Matrix operator+ | ( | const Matrix & | sum | ) | const

Matrix& operator+= | ( | const Matrix & | c | ) | `[inline]`

performant combined assigment operators

Matrix operator- | ( | const Matrix & | sum | ) | const

Matrix& operator-= | ( | const Matrix & | c | ) | `[inline]`

Matrix& operator= | ( | const Matrix & | c | ) | `[inline]`

deep copy

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

bool read | ( | FILE * | f | ) |

reads a Matrix from the given file stream (ascii)

bool restore | ( | FILE * | f | ) | `[virtual]`

reads a Matrix from the given file stream (binary)

Implements Storeable.

Matrix row | ( | unsigned short | index | ) | const

Returns:
  row-vector(as 1xN matrix) containing the index'th row

Examples:
  matrix/matrixexample.cpp.

void set | ( | const D * | _data | ) |

sets the data (row-wise).

Parameters:

  | _data | if null then matrix elements are set to zero otherwise the field MUST have the length should be getM()*getN()

void set | ( | unsigned short | _m,
|  | unsigned short | _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.

bool store | ( | FILE * | f | ) | const`[virtual]`

stores the Matrix into the given file stream (binary)

Implements Storeable.

void sub | ( | const Matrix & | a,
|  | const Matrix & | b |
| ) |  |  |

subtraction: this = a - b

void toAbove | ( | const Matrix & | a | ) |

sets the matrix a below (this) matrix

void toDiff | ( | const Matrix & | a | ) | `[inline]`

inplace subtraction: this = this - a

void toExp | ( | int | exponent | ) |

special inplace matrix potence:

Parameters:

  | exponent | -1 -> inverse; (matrix MUST be SQUARE and NONZERO) 0 -> Identity Matrix; 1 -> itself; T -> Transpose

void toId | ( |  | ) |

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

Examples:
  integration/main.cpp, and main.cpp.

void toMap | ( | D(*)(D) | fun | ) |

inplace mapping of matrix elements (element-wise application)

void toMapP | ( | void * | param,
|  | D(*)(void *, D) | fun |
| ) |  |  |

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

void toMult | ( | const D & | fac | ) |

inplace multiplication with scalar: this = this*fac

void toMultcolwise | ( | const Matrix & | factors | ) |

Inplace column-wise multiplication.

Parameters:

  | factors | column vector of factors, one for each column

void toMultrowwise | ( | const Matrix & | factors | ) |

Inplace row-wise multiplication.

Parameters:

  | factors | column vector of factors, one for each row

void toSum | ( | const Matrix & | a | ) | `[inline]`

inplace addition: this = this + a

void toTranspose | ( |  | ) |

inplace transpose

void toZero | ( |  | ) |

inplace converts matrix to zero matrix

D& val | ( | unsigned short | i,
|  | unsigned short | j |
| ) |  |  | `[inline]`

Returns:
  reference to element at position i,j (can be used as left side value)

D val | ( | unsigned short | i,
|  | unsigned short | j |
| ) |  |  | const`[inline]`

Returns:
  element at position i,j (row, column index)

Examples:
  matrix/matrixexample.cpp.

D valDef0 | ( | short | i,
|  | short | 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:

- matrix.h
- matrix.cpp