philsupertramp/game-math
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
Matrix< T > Class Template Reference

#include <Matrix.h>

Public Member Functions

 Matrix (T val, size_t rowCount, size_t colCount, size_t elementDimension=1)
 
 Matrix (T *val, size_t colCount)
 
 Matrix ()
 
 Matrix (const std::initializer_list< std::initializer_list< T > > &lst)
 
 Matrix (const std::initializer_list< std::initializer_list< std::initializer_list< T > > > &lst)
 
 Matrix (Matrix const &other)
 
template<typename V >
 Matrix (const Matrix< V > &other)
 
 ~Matrix ()
 
size_t rows () const
 
size_t columns () const
 
size_t elements () const
 
size_t elements_total () const
 
Determinant () const
 
constexpr Matrix< T > Transpose () const
 
Matrix< T > HorizontalConcat (const Matrix< T > &other)
 
bool operator== (const Matrix< T > &rhs) const
 
bool operator!= (const Matrix< T > &rhs) const
 
bool operator< (const Matrix< T > &rhs) const
 
bool operator> (const Matrix< T > &rhs) const
 
bool IsVector () const
 
void assertSize (const Matrix< T > &other) const
 
bool elementWiseCompare (const Matrix< T > &rhs) const
 
Matrix< T > operator= (const Matrix< T > &other)
 
Matrix< T > Apply (const std::function< T(T)> &fun) const
 
MatrixHadamardMulti (const Matrix &other)
 
Matrix< T > & KroneckerMulti (const Matrix< T > &other)
 
sumElements () const
 
Matrix< T > sum (size_t axis) const
 
Matrix< T > & operator*= (T rhs)
 
Matrix< T > & operator+= (const Matrix< T > &rhs)
 
Matrix< T > & operator-= (const Matrix< T > &rhs)
 
T & operator() (size_t row, size_t column, size_t elem=0)
 
T & operator() (size_t row, size_t column, size_t elem=0) const
 
Matrix< T > operator() (size_t row)
 
Matrix< T > operator() (size_t row) const
 
T & operator* ()
 
T & operator* () const
 
void SetColumn (size_t index, const Matrix< T > &other)
 
void SetRow (size_t index, const Matrix< T > &other)
 
void Resize (size_t rows, size_t cols, size_t elementSize=1)
 
int GetIndex (size_t row, size_t col, size_t elem=0) const
 
Matrix GetSlice (size_t rowStart) const
 
Matrix GetSlice (size_t rowStart, size_t rowEnd) const
 
Matrix GetSlice (size_t rowStart, size_t rowEnd, size_t colStart) const
 
Matrix GetSlice (size_t rowStart, size_t rowEnd, size_t colStart, size_t colEnd) const
 
void SetSlice (const size_t &row_start, const size_t &row_end, const size_t &col_start, const size_t &col_end, const Matrix< T > &slice)
 
void SetSlice (const size_t &row_start, const Matrix< T > &slice)
 
Matrix< T > GetComponents (const size_t &index) const
 
Matrix< T > GetSlicesByIndex (const Matrix< size_t > &indices) const
 

Static Public Member Functions

static Matrix Random (size_t rows, size_t columns, size_t element_size=1, double minValue=0.0, double maxValue=1.0)
 
static Matrix Normal (size_t rows, size_t columns, double mu, double sigma)
 

Private Member Functions

bool HasDet () const
 

Private Attributes

size_t _rows = 0
 number rows More...
 
size_t _columns = 0
 number columns More...
 
size_t _element_size = 0
 number elements More...
 
T * _data = nullptr
 ongoing array representing data More...
 
size_t _dataSize = 0
 total number of elements More...
 
bool needsFree = false
 

Friends

std::ostream & operator<< (std::ostream &ostr, const Matrix &m)
 

Detailed Description

template<typename T = double>
class Matrix< T >

Represents a multi dimensional Matrix of data with type T

The class is capable of regular matrix operations, including matrix - vector calculation. It also holds several helper methods to calculate data science specific products or other common operations.

Template Parameters
T

Element access using matrix(row, column, element) notation

Accessed elements can be modified!

Examples
TestMatrix.cpp, ds/TestAdalineGD.cpp, ds/TestAdalineSGD.cpp, ds/TestKNN.cpp, ds/TestLogRegSGD.cpp, ds/TestNCC.cpp, ds/TestPerceptron.cpp, ds/preprocessing/TestScaler.cpp, numerics/TestFractals.cpp, numerics/TestUtils.cpp, numerics/analysis/TestDifferentiation.cpp, numerics/analysis/TestSupportValues.cpp, numerics/lin_alg/TestBackwardSub.cpp, numerics/lin_alg/TestForwardSub.cpp, numerics/lin_alg/TestGaussSeidel.cpp, numerics/lin_alg/TestLU.cpp, numerics/lin_alg/TestNewton.cpp, numerics/lin_alg/TestQR.cpp, numerics/lin_alg/TestSVD.cpp, numerics/ode/TestExplicitEuler.cpp, numerics/ode/TestODE45.cpp, numerics/ode/TestODEBDF2.cpp, numerics/ode/TestODETrapez.cpp, plot/TestPlot.cpp, and statistics/TestProbability.cpp.

Constructor & Destructor Documentation

◆ Matrix() [1/7]

template<typename T = double>
Matrix< T >::Matrix ( val,
size_t  rowCount,
size_t  colCount,
size_t  elementDimension = 1 
)
inlineexplicit

Default constructor

Parameters
valdefault value for all elements
rowCountnumber of rows
colCountnumber of columns
elementDimensionnumber of dimensions per element

◆ Matrix() [2/7]

template<typename T = double>
Matrix< T >::Matrix ( T *  val,
size_t  colCount 
)
inlineexplicit

Vector-assignment, assigns colCount values of type T

Parameters
valpointer representation of array
colCountnumber of elements within val

◆ Matrix() [3/7]

template<typename T = double>
Matrix< T >::Matrix ( )
inline

overwritten default constructor

◆ Matrix() [4/7]

template<typename T = double>
Matrix< T >::Matrix ( const std::initializer_list< std::initializer_list< T > > &  lst)
inline

Constructor using initializer_list's

Parameters
lst

◆ Matrix() [5/7]

template<typename T = double>
Matrix< T >::Matrix ( const std::initializer_list< std::initializer_list< std::initializer_list< T > > > &  lst)
inline

Constructor using multi-dimensional initializer_list's

Parameters
lst

◆ Matrix() [6/7]

template<typename T = double>
Matrix< T >::Matrix ( Matrix< T > const &  other)
inline

Deep copy constructor

Parameters
other

◆ Matrix() [7/7]

template<typename T = double>
template<typename V >
Matrix< T >::Matrix ( const Matrix< V > &  other)
inline

Conversion constructor to convert Matrix into other type V

Parameters
otherthe matrix to use
Returns
given matrix casted to type V

◆ ~Matrix()

template<typename T = double>
Matrix< T >::~Matrix ( )
inline

Default destructor, doesn't do anything

Member Function Documentation

◆ Apply()

template<typename T = double>
Matrix< T > Matrix< T >::Apply ( const std::function< T(T)> &  fun) const
inline

Apply given function to Matrix

Parameters
funelement-wise function to apply
Returns
fun(this)
Examples
TestMatrix.cpp, numerics/analysis/TestDifferentiation.cpp, numerics/analysis/TestSupportValues.cpp, numerics/lin_alg/TestSVD.cpp, and plot/TestPlot.cpp.

◆ assertSize()

template<typename T = double>
void Matrix< T >::assertSize ( const Matrix< T > &  other) const
inline

Helper to check for equal dimensions

Parameters
other
Examples
TestMatrix.cpp.

◆ columns()

template<typename T = double>
size_t Matrix< T >::columns ( ) const
inline

columns getter

Returns
Examples
TestMatrix.cpp, and numerics/lin_alg/TestForwardSub.cpp.

◆ Determinant()

template<typename T = double>
T Matrix< T >::Determinant ( ) const
inline

Calculates Determinant

Returns

◆ elements()

template<typename T = double>
size_t Matrix< T >::elements ( ) const
inline

elements getter

Returns

◆ elements_total()

template<typename T = double>
size_t Matrix< T >::elements_total ( ) const
inline

getter for total number of elements inside matrix

Returns
number of matrix elements

◆ elementWiseCompare()

template<typename T = double>
bool Matrix< T >::elementWiseCompare ( const Matrix< T > &  rhs) const
inline

Element-wise comparison

Parameters
rhs
Returns

◆ GetComponents()

template<typename T = double>
Matrix< T > Matrix< T >::GetComponents ( const size_t &  index) const
inline

returns 1D-Matrix from given index

Parameters
indexof elements
Returns

◆ GetIndex()

template<typename T = double>
int Matrix< T >::GetIndex ( size_t  row,
size_t  col,
size_t  elem = 0 
) const
inline
Parameters
row

\[\in [0, rows() - 1]\]

col

\[\in [0, columns() - 1]\]

elem

\[\in [0, elements() - 1]\]

Returns
elem + col * elements() + row * columns() * elements()
Examples
TestMatrix.cpp.

◆ GetSlice() [1/4]

template<typename T = double>
Matrix Matrix< T >::GetSlice ( size_t  rowStart) const
inline

◆ GetSlice() [2/4]

template<typename T = double>
Matrix Matrix< T >::GetSlice ( size_t  rowStart,
size_t  rowEnd 
) const
inline

◆ GetSlice() [3/4]

template<typename T = double>
Matrix Matrix< T >::GetSlice ( size_t  rowStart,
size_t  rowEnd,
size_t  colStart 
) const
inline

◆ GetSlice() [4/4]

template<typename T = double>
Matrix Matrix< T >::GetSlice ( size_t  rowStart,
size_t  rowEnd,
size_t  colStart,
size_t  colEnd 
) const
inline

Returns a slice of given dimension from the matrix

Parameters
rowStartrow start index
rowEndrow end index
colStartcolumn start index
colEndcolumn end index
Returns
sub-matrix

◆ GetSlicesByIndex()

template<typename T = double>
Matrix< T > Matrix< T >::GetSlicesByIndex ( const Matrix< size_t > &  indices) const
inline

◆ HadamardMulti()

template<typename T = double>
Matrix & Matrix< T >::HadamardMulti ( const Matrix< T > &  other)
inline

Hadamard Multiplication Z[i][j] = A[i][j] * B[i][j]

Parameters
other
Returns
Examples
TestMatrix.cpp.

◆ HasDet()

template<typename T = double>
bool Matrix< T >::HasDet ( ) const
inlineprivate

Helper to test if Matrix can have a determinant

Returns

◆ HorizontalConcat()

template<typename T = double>
Matrix< T > Matrix< T >::HorizontalConcat ( const Matrix< T > &  other)
inline

Horizontal matrix concatenation

Parameters
otherMatrix with same number of rows, dimension n1, m2
Returns
concatenated matrix of [this, other] with dimension n1, m1 + m2
Examples
TestMatrix.cpp.

◆ IsVector()

template<typename T = double>
bool Matrix< T >::IsVector ( ) const
inline

Helper to determine whether given matrix is a vector.

Returns
boolean, true if matrix is vector, else false

◆ KroneckerMulti()

template<typename T = double>
Matrix< T > & Matrix< T >::KroneckerMulti ( const Matrix< T > &  other)
inline

A form of matrix multiplication For explicit reference please consult https://en.wikipedia.org/wiki/Kronecker_product

Parameters
otherright hand side with same dimension
Returns
resulting matrix with same dimension
Examples
TestMatrix.cpp.

◆ Normal()

template<typename T = double>
static Matrix Matrix< T >::Normal ( size_t  rows,
size_t  columns,
double  mu,
double  sigma 
)
inlinestatic

Generates normal distributed squared matrix

Parameters
rows
columns
Returns
data set of normal distributed data
Examples
numerics/lin_alg/TestSVD.cpp.

◆ operator!=()

template<typename T = double>
bool Matrix< T >::operator!= ( const Matrix< T > &  rhs) const
inline

not-equal operator

Parameters
rhs
Returns

◆ operator()() [1/4]

template<typename T = double>
Matrix< T > Matrix< T >::operator() ( size_t  row)
inline

row getter

no in-place editing, creates new object! use SetRow instead

Parameters
rowindex of row to get
Returns
row elements

◆ operator()() [2/4]

template<typename T = double>
Matrix< T > Matrix< T >::operator() ( size_t  row) const
inline

const row-getter

Parameters
rowindex of row
Returns
row values

◆ operator()() [3/4]

template<typename T = double>
T & Matrix< T >::operator() ( size_t  row,
size_t  column,
size_t  elem = 0 
)
inline

element access

Parameters
rowrow index
columncolumn index
elemelement index
Returns
value at given address

◆ operator()() [4/4]

template<typename T = double>
T & Matrix< T >::operator() ( size_t  row,
size_t  column,
size_t  elem = 0 
) const
inline

const element-access

Parameters
rowrow index
columncolumn index
elemelement index
Returns
const value at given address

◆ operator*() [1/2]

template<typename T = double>
T & Matrix< T >::operator* ( )
inline

pointer operator

Returns

◆ operator*() [2/2]

template<typename T = double>
T & Matrix< T >::operator* ( ) const
inline

const pointer operator

Returns

◆ operator*=()

template<typename T = double>
Matrix< T > & Matrix< T >::operator*= ( rhs)
inline

Matrix-Constant-Multiplication

Parameters
rhs
Returns

◆ operator+=()

template<typename T = double>
Matrix< T > & Matrix< T >::operator+= ( const Matrix< T > &  rhs)
inline

Matrix-Addition

Parameters
rhs
Returns

◆ operator-=()

template<typename T = double>
Matrix< T > & Matrix< T >::operator-= ( const Matrix< T > &  rhs)
inline

Matrix-Subtraction

Parameters
rhs
Returns

◆ operator<()

template<typename T = double>
bool Matrix< T >::operator< ( const Matrix< T > &  rhs) const
inline

◆ operator=()

template<typename T = double>
Matrix< T > Matrix< T >::operator= ( const Matrix< T > &  other)
inline

Assignment-operator.

careful! actually overrides different sized matrices, just like other languages (python, matlab)

Parameters
other
Returns

◆ operator==()

template<typename T = double>
bool Matrix< T >::operator== ( const Matrix< T > &  rhs) const
inline

OPERATORS comparison operator

Parameters
rhs
Returns

◆ operator>()

template<typename T = double>
bool Matrix< T >::operator> ( const Matrix< T > &  rhs) const
inline

◆ Random()

template<typename T = double>
static Matrix Matrix< T >::Random ( size_t  rows,
size_t  columns,
size_t  element_size = 1,
double  minValue = 0.0,
double  maxValue = 1.0 
)
inlinestatic

Generates a random matrix

Parameters
rowsnumber of rows in target matrix
columnsnumber of columns in target matrix
element_size
minValue
maxValue
Returns
matrix of dimension rows, columns initialized with random values from minValue to maxValue
Examples
TestMatrix.cpp.

◆ Resize()

template<typename T = double>
void Matrix< T >::Resize ( size_t  rows,
size_t  cols,
size_t  elementSize = 1 
)
inline

Resizes a matrix

Parameters
rowstarget number of rows
colstarget number of columns
elementSizetarget number of elements per cell
Examples
TestMatrix.cpp.

◆ rows()

template<typename T = double>
size_t Matrix< T >::rows ( ) const
inline

row getter

Returns
Examples
TestMatrix.cpp.

◆ SetColumn()

template<typename T = double>
void Matrix< T >::SetColumn ( size_t  index,
const Matrix< T > &  other 
)
inline

Column setter

Parameters
indexcolumn index to set
othernew values

◆ SetRow()

template<typename T = double>
void Matrix< T >::SetRow ( size_t  index,
const Matrix< T > &  other 
)
inline

Row setter

Parameters
indexrow index to set
otherholds new row elements

◆ SetSlice() [1/2]

template<typename T = double>
void Matrix< T >::SetSlice ( const size_t &  row_start,
const Matrix< T > &  slice 
)
inline

Helper method to automatically resolve dimensions through slice

◆ SetSlice() [2/2]

template<typename T = double>
void Matrix< T >::SetSlice ( const size_t &  row_start,
const size_t &  row_end,
const size_t &  col_start,
const size_t &  col_end,
const Matrix< T > &  slice 
)
inline

◆ sum()

template<typename T = double>
Matrix< T > Matrix< T >::sum ( size_t  axis) const
inline

Calculates element wise sum of sub-elements along given axis

Parameters
axisaxis index (0: rows, 1: columns) to calculate sum on
Returns
vector of element wise sums along given axis

◆ sumElements()

template<typename T = double>
T Matrix< T >::sumElements ( ) const
inline

Calculates sum of all elements

Returns
element sum

◆ Transpose()

template<typename T = double>
constexpr Matrix< T > Matrix< T >::Transpose ( ) const
inlineconstexpr

Creates transposed matrix of this

Returns
Examples
TestMatrix.cpp, numerics/lin_alg/TestSVD.cpp, and statistics/TestProbability.cpp.

Friends And Related Function Documentation

◆ operator<<

template<typename T = double>
std::ostream & operator<< ( std::ostream &  ostr,
const Matrix< T > &  m 
)
friend

ostream operator, beatified representation

Parameters
ostr
m
Returns

Member Data Documentation

◆ _columns

template<typename T = double>
size_t Matrix< T >::_columns = 0
private

number columns

◆ _data

template<typename T = double>
T* Matrix< T >::_data = nullptr
private

ongoing array representing data

◆ _dataSize

template<typename T = double>
size_t Matrix< T >::_dataSize = 0
private

total number of elements

◆ _element_size

template<typename T = double>
size_t Matrix< T >::_element_size = 0
private

number elements

◆ _rows

template<typename T = double>
size_t Matrix< T >::_rows = 0
private

number rows

◆ needsFree

template<typename T = double>
bool Matrix< T >::needsFree = false
private

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