WarpTwin
Documentation for WarpTwin models and classes.
Loading...
Searching...
No Matches
clockwerk::Matrix< R, C > Class Template Reference

Matrix math implementation. More...

#include <Matrix.hpp>

Public Member Functions

 Matrix ()
 Default constructor to initialize a matrix to all zeroes for ease of use.
 Matrix (floating_point elements)
 Constructor to initialize a matrix with all the same element.
 Matrix (const floating_point(&initial)[R][C])
 Matrix (const Matrix< R, C > &initial)
 Matrix (const std::array< std::array< floating_point, C >, R > &initial)
 Array constructor for the matrix class. Initializes from std::array.
 ~Matrix ()
int16 str (char *output, size_t size) const
 Function to dump information on matrix.
int16 fromStr (const char *val)
 Set value of matrix from string in same format as str().
int16 set (uint32 row, uint32 col, const floating_point &value)
 Function to set a single value in the matrix.
int16 get (uint32 row, uint32 col, floating_point &result) const
 Function to get a single value in the matrix.
void setFromArray (const floating_point *start_ptr, bool column_major=false)
 Function to set the values of the matrix row-wise.
void getAsArray (floating_point *start_ptr, bool column_major=false) const
 Function to get the values of the matrix row-wise.
floating_point get (uint32 row, uint32 col) const
 Function to get a single value in the matrix.
void getCopy (Matrix< R, C > &result) const
 Function to get a copy of the matrix.
Matrix< R, C > & operator= (const Matrix< R, C > &other)
 Equals operator overload for matrix.
floating_point * operator[] (uint32 idx)
 Function to return a matrix row or vector value.
std::pair< uint32, uint32 > size () const
 Function to get the size of the matrix.
void max (floating_point &result, std::pair< uint32, uint32 > &index) const
 Function to return the maximum value in the matrix.
void min (floating_point &result, std::pair< uint32, uint32 > &index) const
 Function to return the minimum value in the matrix.
int16 det (floating_point &result) const
 Function to return the determinant of the matrix.
int16 chol (Matrix< R, C > &retval) const
 Take the cholesky decomposition of this matrix.
Matrix< R, C > chol () const
int16 inverse (Matrix< R, C > &result) const
 Function to return the inverse of the matrix.
Matrix< R, C > inverse () const
int16 pseudoinverse (Matrix< C, R > &result) const
Matrix< C, R > pseudoinverse () const
void transpose (Matrix< C, R > &result) const
 Function to return the transpose of the matrix.
Matrix< C, R > transpose () const
int16 trace (floating_point &result) const
 Function to return the trace of the matrix.
void setToZeros ()
 Function to set all elements of the matrix to zero.
int16 identity ()
 Function to set matrix to identity, if it is a square matrix.
int16 eye ()

Public Attributes

std::array< std::array< floating_point, C >, R > values

Protected Member Functions

int16 _checkLookupBoundaries (uint32 start_r, uint32 end_r, uint32 start_c, uint32 end_c) const
int16 _LUPDecompose (floating_point *A[R], uint32 P[R+1]) const
 Function to take a 2-d matrix represented by A and decompose it into LU form.

Detailed Description

template<uint32 R, uint32 C>
class clockwerk::Matrix< R, C >

Matrix math implementation.

This file defines a base class for computational matrix math. It is designed for use in real-time, embedded mode or with simulation. As such, it contains certain limitations on dynamic allocation to make it real-time compliant.

Note: The naming convention observed for matrix math is as follows:

  • Capital letters represent matrices
  • Lowercase letters represent scalars
  • All vectors are 1-dimensional matrices for the purpose of math
  • Pass by reference return values are all named "result"
  • The letter used in math indicates the order of operations - A/a comes first, then B/b. A/a or B/b always represent the "other" value in an operation

Important: The matrix class is templated such that it can be used with a generic type floating_point, but should only be used on native types, as other implementations could result in undefined behavior.

Constructor & Destructor Documentation

◆ Matrix() [1/5]

template<uint32 R, uint32 C>
clockwerk::Matrix< R, C >::Matrix ( )

Default constructor to initialize a matrix to all zeroes for ease of use.

◆ Matrix() [2/5]

template<uint32 R, uint32 C>
clockwerk::Matrix< R, C >::Matrix ( floating_point elements)

Constructor to initialize a matrix with all the same element.

◆ Matrix() [3/5]

template<uint32 R, uint32 C>
clockwerk::Matrix< R, C >::Matrix ( const floating_point(&) initial[R][C])

Constructor for Matrix class with initialization. Initializes matrix to values passed in via array

◆ Matrix() [4/5]

template<uint32 R, uint32 C>
clockwerk::Matrix< R, C >::Matrix ( const Matrix< R, C > & initial)

Copy constructor for Matrix class. Copies data from matrix object to the current instance

◆ Matrix() [5/5]

template<uint32 R, uint32 C>
clockwerk::Matrix< R, C >::Matrix ( const std::array< std::array< floating_point, C >, R > & initial)

Array constructor for the matrix class. Initializes from std::array.

◆ ~Matrix()

template<uint32 R, uint32 C>
clockwerk::Matrix< R, C >::~Matrix ( )
inline

Destructor – doesn't do anything because we don't dynamically allocate

Member Function Documentation

◆ _checkLookupBoundaries()

template<uint32 R, uint32 C>
int16 clockwerk::Matrix< R, C >::_checkLookupBoundaries ( uint32 start_r,
uint32 end_r,
uint32 start_c,
uint32 end_c ) const
protected

Function to check, given a set of submatrix boundaries, that those boundaries are valid for the current matrix

◆ _LUPDecompose()

template<uint32 R, uint32 C>
int16 clockwerk::Matrix< R, C >::_LUPDecompose ( floating_point * A[R],
uint32 P[R+1] ) const
protected

Function to take a 2-d matrix represented by A and decompose it into LU form.

Parameters
AA 2-d matrix represented as a double pointer After ops A contains a copy of both matrices L-E and U as A=(L-E)+U such that P*A=L*U.
PThe permutation matrix to hold info on matrix changes
Returns
Error code corresponding to codes in clockwerkerrors.h
Note
Code borrowed/updated from sample C code on wikipedia here: https://en.wikipedia.org/wiki/LU_decomposition

◆ chol() [1/2]

template<uint32 R, uint32 C>
Matrix< R, C > clockwerk::Matrix< R, C >::chol ( ) const
inline

◆ chol() [2/2]

template<uint32 R, uint32 C>
int16 clockwerk::Matrix< R, C >::chol ( Matrix< R, C > & retval) const

Take the cholesky decomposition of this matrix.

Parameters
retvalImplicit return of calculation result
Returns
Error code corresponding to success/failure
Note
Must be positive semidefinite matrix! Use this if you know what you're doing.
Because of this the direct return is super dangerous. Be very careful.

◆ det()

template<uint32 R, uint32 C>
int16 clockwerk::Matrix< R, C >::det ( floating_point & result) const

Function to return the determinant of the matrix.


Operations to return important matrix information

Parameters
resultPBR return of determinant
Returns
Integer value corresponding to error codes in clockwerkerrrors.h

◆ eye()

template<uint32 R, uint32 C>
int16 clockwerk::Matrix< R, C >::eye ( )
inline

◆ fromStr()

template<uint32 R, uint32 C>
int16 clockwerk::Matrix< R, C >::fromStr ( const char * val)

Set value of matrix from string in same format as str().

Parameters
valThe value to set matrix from of the form [[1,2][3,4][5,6]]
Returns
Error code corresponding to success/failure

◆ get() [1/2]

template<uint32 R, uint32 C>
floating_point clockwerk::Matrix< R, C >::get ( uint32 row,
uint32 col ) const

Function to get a single value in the matrix.

Parameters
rowThe row index
colThe column index
Returns
Matrix value at index
Note
Does not bounds check and is unsafe for embedded operations

◆ get() [2/2]

template<uint32 R, uint32 C>
int16 clockwerk::Matrix< R, C >::get ( uint32 row,
uint32 col,
floating_point & result ) const

Function to get a single value in the matrix.

Parameters
rowThe row index
colThe column index
valuePBR return of the value in the matrix
Returns
Integer value corresponding to error codes in clockwerkerrrors.h

◆ getAsArray()

template<uint32 R, uint32 C>
void clockwerk::Matrix< R, C >::getAsArray ( floating_point * start_ptr,
bool column_major = false ) const

Function to get the values of the matrix row-wise.

Parameters
start_ptrThe data address at which write should begin.
column_majorFlag indicating whether matrix should output column major. Default is false
Note
start_ptr must represent an array or similar type of at least R*C values
Not protected. May segfault if not used properly
The matrix [1 2; 3 4] would output as an array [1 2 3 4]

◆ getCopy()

template<uint32 R, uint32 C>
void clockwerk::Matrix< R, C >::getCopy ( Matrix< R, C > & result) const

Function to get a copy of the matrix.

Parameters
resultPBR return of a copy of this matrix

◆ identity()

template<uint32 R, uint32 C>
int16 clockwerk::Matrix< R, C >::identity ( )

Function to set matrix to identity, if it is a square matrix.

Returns
Error code corresponding to success/failure

◆ inverse() [1/2]

template<uint32 R, uint32 C>
Matrix< R, C > clockwerk::Matrix< R, C >::inverse ( ) const
inline

◆ inverse() [2/2]

template<uint32 R, uint32 C>
int16 clockwerk::Matrix< R, C >::inverse ( Matrix< R, C > & result) const

Function to return the inverse of the matrix.

Parameters
resultPBR return of matrix inverse
Returns
Integer value corresponding to error codes in clockwerkerrrors.h

◆ max()

template<uint32 R, uint32 C>
void clockwerk::Matrix< R, C >::max ( floating_point & result,
std::pair< uint32, uint32 > & index ) const

Function to return the maximum value in the matrix.

Parameters
resultPBR return of maximum value

◆ min()

template<uint32 R, uint32 C>
void clockwerk::Matrix< R, C >::min ( floating_point & result,
std::pair< uint32, uint32 > & index ) const

Function to return the minimum value in the matrix.

Parameters
resultPBR return of minimum value

◆ operator=()

template<uint32 R, uint32 C>
Matrix< R, C > & clockwerk::Matrix< R, C >::operator= ( const Matrix< R, C > & other)

Equals operator overload for matrix.

◆ operator[]()

template<uint32 R, uint32 C>
floating_point * clockwerk::Matrix< R, C >::operator[] ( uint32 idx)

Function to return a matrix row or vector value.

Parameters
idxThe row index to return
Returns
A pointer to the row element of the matrix. Nullptr if out of range

◆ pseudoinverse() [1/2]

template<uint32 R, uint32 C>
Matrix< C, R > clockwerk::Matrix< R, C >::pseudoinverse ( ) const
inline

◆ pseudoinverse() [2/2]

template<uint32 R, uint32 C>
int16 clockwerk::Matrix< R, C >::pseudoinverse ( Matrix< C, R > & result) const

◆ set()

template<uint32 R, uint32 C>
int16 clockwerk::Matrix< R, C >::set ( uint32 row,
uint32 col,
const floating_point & value )

Function to set a single value in the matrix.


Getters and setters

Parameters
rowThe row index
colThe column index
valueThe value to set the matrix to
Returns
Integer value corresponding to error codes in clockwerkerrrors.h

◆ setFromArray()

template<uint32 R, uint32 C>
void clockwerk::Matrix< R, C >::setFromArray ( const floating_point * start_ptr,
bool column_major = false )

Function to set the values of the matrix row-wise.

Parameters
start_ptrThe data address at which read should begin.
column_majorFlag indicating whether matrix should input column major. Default is false
Note
start_ptr must represent an array or similar type of at least R*C values
Not protected. May segfault if not used properly
The array [1 2 3 4] would read into a matrix as [1 2; 3 4]

◆ setToZeros()

template<uint32 R, uint32 C>
void clockwerk::Matrix< R, C >::setToZeros ( )

Function to set all elements of the matrix to zero.

◆ size()

template<uint32 R, uint32 C>
std::pair< uint32, uint32 > clockwerk::Matrix< R, C >::size ( ) const
inline

Function to get the size of the matrix.


Operations to get matrix info

◆ str()

template<uint32 R, uint32 C>
int16 clockwerk::Matrix< R, C >::str ( char * output,
size_t size ) const

Function to dump information on matrix.

Function to dump information on matrix to string

◆ trace()

template<uint32 R, uint32 C>
int16 clockwerk::Matrix< R, C >::trace ( floating_point & result) const

Function to return the trace of the matrix.

Parameters
resultPBR return of matrix trace
Returns
Integer value corresponding to error codes in clockwerkerrrors.h

◆ transpose() [1/2]

template<uint32 R, uint32 C>
Matrix< C, R > clockwerk::Matrix< R, C >::transpose ( ) const

◆ transpose() [2/2]

template<uint32 R, uint32 C>
void clockwerk::Matrix< R, C >::transpose ( Matrix< C, R > & result) const

Function to return the transpose of the matrix.

Parameters
resultPBR return of matrix transpose
Note
Overloaded for return as well

Member Data Documentation

◆ values

template<uint32 R, uint32 C>
std::array<std::array<floating_point, C>, R> clockwerk::Matrix< R, C >::values

The actual values held by the matrix – a two dimensional array of values indexed as (row, column). NOTE: Public for ease of access and speed (no need to use setter/getter), and functions in Matrix and Safemath libraries are tested safe. Behavior using matrices outside of clockwerk libraries should use setter/getter, rather than direct access, for safety.


The documentation for this class was generated from the following file:
  • /Users/mickey/Documents/Projects/warptwin/warpos/clockwerk/src/core/Matrix.hpp