C++ matrix implementation
Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Friends
Matrix< T, typename > Class Template Reference

Matrix implementation, with a series of linear algebra functions. More...

#include <Matrix.hpp>

Collaboration diagram for Matrix< T, typename >:

Public Types

enum  Axis { ALL, ROWS, COLUMNS }
 

Public Member Functions

size_t nCols () const
 
size_t nRows () const
 
 Matrix ()
 Initializes an empty matrix. More...
 
 Matrix (size_t dimension)
 Initializes a square matrix. More...
 
 Matrix (size_t rows, size_t cols)
 Initializes a matrix with a predetermined number of rows and columns. More...
 
 Matrix (size_t rows, size_t cols, const vector< T > &data)
 Initializes a matrix with a predetermined number of rows and columns and populates it with data. More...
 
template<std::size_t N>
 Matrix (size_t rows, size_t cols, T(&data)[N])
 
Matrix operator+= (double value)
 
Matrix operator-= (double value)
 
Matrix operator*= (double value)
 
Matrix operator/= (double value)
 
Matrix operator+ (const Matrix &b)
 Matrix addition operation. More...
 
Matrix operator- (const Matrix &b)
 Matrix subtraction operation. More...
 
Matrix operator* (const Matrix &b) const
 Matrix multiplication operation. More...
 
Matrixoperator+= (const Matrix &other)
 
Matrixoperator-= (const Matrix &other)
 
Matrixoperator*= (const Matrix &other)
 
Matrix< int > operator== (const T &value)
 
bool operator== (const Matrix &other)
 
Matrix operator!= (const double &value)
 
bool operator!= (const Matrix &other)
 
Matrix operator- ()
 Matrix negative operation. More...
 
T & operator() (size_t i, size_t j)
 Functor used to access elements in the matrix. More...
 
operator() (size_t i, size_t j) const
 Functor used to access elements in the matrix. More...
 
bool isSquare () const
 
Matrix diagonal ()
 
Matrix hadamard (const Matrix &b)
 Executes the Hadamard, or entrywise multiplication between two matrices. More...
 
Matrix submatrix (size_t row, size_t column) const
 Returns a submatrix of the current matrix, removing one row and column of the original matrix. More...
 
double getMinor (size_t row, size_t column) const
 Returns the minor of a matrix, which is the determinant of a submatrix where a single row and column are removed. More...
 
double cofactor (size_t row, size_t column) const
 Calculates the cofactor of a matrix at a given point. More...
 
Matrix cofactorMatrix () const
 Calculates the cofactor matrix. More...
 
Matrix adjugate () const
 Returns the adjugate of the current matrix, which is the transpose of its cofactor matrix. More...
 
Matrix inverse () const
 Calculates the inverse of the current matrix. More...
 
double determinant () const
 Calculates the determinant of the matrix. More...
 
Matrix transpose () const
 Returns the transpose of a matrix. More...
 
void addColumn (Matrix values)
 Adds a column at the end of the matrix. More...
 
void addRow (Matrix values)
 Adds a row at the end of the matrix. More...
 
void addColumn (Matrix values, size_t position)
 Adds a column to the matrix at the given position. More...
 
void addRow (Matrix values, size_t position)
 Adds a row to the matrix at the given position. More...
 
void removeColumn (int position)
 Removes a column from the matrix. More...
 
Matrix unique () const
 Returns only unique values from the matrix. More...
 
void sort ()
 Sorts elements of the matrix inplace. More...
 
Matrix count ()
 Counts occurrences of elements in a matrix. More...
 
Matrix mean (Matrix groups)
 Calculates means of a matrix, grouped by classes. More...
 
Matrix mean ()
 Calculates the mean of the columns of the matrix. More...
 
Matrix scatter ()
 Calculates the scatter matrix. More...
 
Matrix cov ()
 Calculates the covariance matrix of the current matrix. More...
 
Matrix var ()
 Calculates the variance of the columns of the matrix. More...
 
Matrix stdev ()
 Calculates the standard deviation of the columns of the matrix. More...
 
void reshape (size_t rows, size_t cols)
 Reshapes the current matrix. More...
 
Matrix getColumn (size_t index)
 Gets a column from the matrix. More...
 
Matrix getRow (size_t index)
 Gets a row from the matrix. More...
 
Matrix asDiagonal ()
 Creates a diagonal matrix from a row or column vector. More...
 
Matrix copy ()
 Returns a copy of the matrix. More...
 
Matrix standardize ()
 Standardizes the columns of the matrix, subtracting each element of a column by the column mean and dividing it by the standard deviation of the column. More...
 
Matrix standardize (Matrix means, Matrix stds)
 Standardizes the columns of the matrix, subtracting each element of a column by the mean argument and dividing it by the stds argument. More...
 
Matrix minusMean ()
 
bool contains (T value)
 Checks if the matrix contains a value. More...
 
bool isEmpty ()
 Checks if the matrix is empty or uninitialized. More...
 
Matrix filter (const Matrix< int > bin, bool columns=false)
 Selects a subset of either columns or rows of the matrix. More...
 
Matrix getRows (const Matrix< int > bin)
 Selects a subset of rows of the matrix. More...
 
Matrix getColumns (const Matrix< int > bin)
 Selects a subset of columns of the matrix. More...
 
bool isSymmetric ()
 Checks if the matrix is symmetric. More...
 
Matrix normalize ()
 Normalizes the column vectors of the matrix. More...
 
pair< Matrix, Matrixeigen ()
 Calculates the eigenvalues and eigenvectors of a matrix. More...
 
pair< Matrix, MatrixeigenSymmetric ()
 Calculates the eigenvalues and eigenvectors of a symmetric matrix using the Jacobi eigenvalue algorithm. More...
 
pair< Matrix, MatrixeigenNonSymmetric (bool hes=true)
 Calculates the eigenvalues and eigenvectors of a on-symmetric matrix. More...
 
Matrix WithinClassScatter (Matrix y)
 
Matrix BetweenClassScatter (Matrix y)
 
sum () const
 
bool isColumn () const
 
bool isRow () const
 
min () const
 
max () const
 
Matrix< T > apply (function< T(T)> f)
 
Matrix oneHot ()
 
void setRow (size_t index, Matrix< T > row)
 
void setColumn (size_t index, Matrix< T > column)
 
bool isBinary () const
 

Static Public Member Functions

static Matrix fill (size_t rows, size_t cols, double value)
 Returns a matrix filled with a single value. More...
 
static Matrix diagonal (size_t size, double value)
 Creates a square matrix with a fixed value on the diagonal. More...
 
static Matrix identity (size_t size)
 Returns the identity matrix. More...
 
static Matrix ones (size_t rows, size_t cols)
 Returns a matrix filled with ones. More...
 
static Matrix zeros (size_t rows, size_t cols)
 Returns a matrix filled with zeros. More...
 
static Matrix sort (Matrix m)
 Sorts the elements of a matrix. More...
 
static Matrix fromCSV (const string &path)
 Loads CSV data into a matrix. More...
 

Private Member Functions

void validateIndexes (size_t row, size_t col) const
 Validates if indices are contained inside the matrix. More...
 

Static Private Member Functions

static pair< Matrix, Matrixeigsort (Matrix eigenvalues, Matrix eigenvectors)
 Sorts eigenvalues by magnitude, sorting their corresponding eigenvectors in te same order. More...
 

Private Attributes

size_t mRows
 
size_t mCols
 
std::vector< T > mData
 

Friends

Matrix operator+ (const Matrix &m, double value)
 Scalar addition. More...
 
Matrix operator+ (double value, const Matrix &m)
 Scalar addition. More...
 
Matrix operator- (const Matrix &m, double value)
 Scalar subtraction. More...
 
Matrix operator- (double value, const Matrix &m)
 Scalar subtraction. More...
 
Matrix operator* (const Matrix &m, double value)
 Scalar multiplication. More...
 
Matrix operator* (double value, const Matrix &m)
 Scalar multiplication. More...
 
Matrix operator/ (const Matrix &m, double value)
 Scalar division. More...
 
Matrix operator/ (double value, const Matrix &m)
 Scalar division. More...
 
ostream & operator<< (ostream &os, const Matrix &matrix)
 Prints a matrix. More...
 

Detailed Description

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
class Matrix< T, typename >

Matrix implementation, with a series of linear algebra functions.

Template Parameters
TThe arithmetic type the matrix will store

Definition at line 30 of file Matrix.hpp.

Member Enumeration Documentation

◆ Axis

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
enum Matrix::Axis
Enumerator
ALL 
ROWS 
COLUMNS 

Definition at line 85 of file Matrix.hpp.

Constructor & Destructor Documentation

◆ Matrix() [1/5]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix< T, typename >::Matrix ( )
inline

Initializes an empty matrix.

Definition at line 94 of file Matrix.hpp.

◆ Matrix() [2/5]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix< T, typename >::Matrix ( size_t  dimension)
inline

Initializes a square matrix.

Parameters
dimensionnumber of rows and columns

Definition at line 100 of file Matrix.hpp.

◆ Matrix() [3/5]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix< T, typename >::Matrix ( size_t  rows,
size_t  cols 
)
inline

Initializes a matrix with a predetermined number of rows and columns.

Parameters
rowsnumber of rows in the matrix
colsnumber of columns in the matrix

Definition at line 107 of file Matrix.hpp.

◆ Matrix() [4/5]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix< T, typename >::Matrix ( size_t  rows,
size_t  cols,
const vector< T > &  data 
)
inline

Initializes a matrix with a predetermined number of rows and columns and populates it with data.

Parameters
rowsnumber of rows in the matrix
colsnumber of columns in the matrix
dataa vector containing rows * cols elements to populate the matrix

Definition at line 117 of file Matrix.hpp.

◆ Matrix() [5/5]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
template<std::size_t N>
Matrix< T, typename >::Matrix ( size_t  rows,
size_t  cols,
T(&)  data[N] 
)
inline

Definition at line 126 of file Matrix.hpp.

Member Function Documentation

◆ addColumn() [1/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
void Matrix< T, typename >::addColumn ( Matrix< T, typename >  values)
inline

Adds a column at the end of the matrix.

Addition is done inplace.

Parameters
valuesa column vector containing the values to be added in the new column

Definition at line 692 of file Matrix.hpp.

Here is the caller graph for this function:

◆ addColumn() [2/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
void Matrix< T, typename >::addColumn ( Matrix< T, typename >  values,
size_t  position 
)
inline

Adds a column to the matrix at the given position.

Addition is done inplace.

Parameters
valuesa column vector containing the values to be added in the new column
positionindex of the new column. The column at the current position and all columns succeeding it are pushed forward.

Definition at line 706 of file Matrix.hpp.

Here is the call graph for this function:

◆ addRow() [1/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
void Matrix< T, typename >::addRow ( Matrix< T, typename >  values)
inline

Adds a row at the end of the matrix.

Addition is done inplace.

Parameters
valuesa column vector containing the values to be added in the new row

Definition at line 698 of file Matrix.hpp.

Here is the caller graph for this function:

◆ addRow() [2/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
void Matrix< T, typename >::addRow ( Matrix< T, typename >  values,
size_t  position 
)
inline

Adds a row to the matrix at the given position.

Addition is done inplace.

Parameters
valuesa column vector containing the values to be added in the new row
positionindex of the new row. The row at the current position and all rows succeeding it are pushed forward.

Definition at line 740 of file Matrix.hpp.

◆ adjugate()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::adjugate ( ) const
inline

Returns the adjugate of the current matrix, which is the transpose of its cofactor matrix.

Returns
Adjugate of the current matrix

Definition at line 634 of file Matrix.hpp.

Here is the call graph for this function:

◆ apply()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix<T> Matrix< T, typename >::apply ( function< T(T)>  f)
inline

Definition at line 1324 of file Matrix.hpp.

◆ asDiagonal()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::asDiagonal ( )
inline

Creates a diagonal matrix from a row or column vector.

Returns
diagonal matrix generated from the vector

Definition at line 1010 of file Matrix.hpp.

◆ BetweenClassScatter()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::BetweenClassScatter ( Matrix< T, typename >  y)
inline

Definition at line 1285 of file Matrix.hpp.

Here is the call graph for this function:

◆ cofactor()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
double Matrix< T, typename >::cofactor ( size_t  row,
size_t  column 
) const
inline

Calculates the cofactor of a matrix at a given point.

Parameters
rowindex of the row where the cofactor will be calculated
columnindex of the column where the cofactor will be calculated
Returns
cofactor of the matrix at the given position

Definition at line 600 of file Matrix.hpp.

◆ cofactorMatrix()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::cofactorMatrix ( ) const
inline

Calculates the cofactor matrix.

Returns
Cofactor matrix of the current matrix

Definition at line 620 of file Matrix.hpp.

◆ contains()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
bool Matrix< T, typename >::contains ( value)
inline

Checks if the matrix contains a value.

Parameters
valuethe vaue to look for
Returns
true if the matrix contains the value, otherwise false

Definition at line 1083 of file Matrix.hpp.

Here is the caller graph for this function:

◆ copy()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::copy ( )
inline

Returns a copy of the matrix.

Returns
copy of the current matrix

Definition at line 1027 of file Matrix.hpp.

◆ count()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::count ( )
inline

Counts occurrences of elements in a matrix.

Returns
matrix with two columns. The first contains unique instances of the elements in the original matrix. The second column contains occurrences of the elements in the first column.

Definition at line 818 of file Matrix.hpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cov()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::cov ( )
inline

Calculates the covariance matrix of the current matrix.

Columns are taken as features.

Returns
covariance matrix

Definition at line 897 of file Matrix.hpp.

◆ determinant()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
double Matrix< T, typename >::determinant ( ) const
inline

Calculates the determinant of the matrix.

Returns
determinant of the matrix

Definition at line 656 of file Matrix.hpp.

Here is the caller graph for this function:

◆ diagonal() [1/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
static Matrix Matrix< T, typename >::diagonal ( size_t  size,
double  value 
)
inlinestatic

Creates a square matrix with a fixed value on the diagonal.

Parameters
sizedimensions of the square matrix
valuevalue to be used in the diagonal
Returns
square matrix with a fixed value on the diagonal

Definition at line 481 of file Matrix.hpp.

Here is the caller graph for this function:

◆ diagonal() [2/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::diagonal ( )
inline
Returns
diagonal of the square matrix as a column vector

Definition at line 494 of file Matrix.hpp.

◆ eigen()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
pair<Matrix, Matrix> Matrix< T, typename >::eigen ( )
inline

Calculates the eigenvalues and eigenvectors of a matrix.

Parameters
sortwhether to sort the eigenvalues and eigenvectors
Returns
a pair containing eigenvalues in a column vector in the first element of the pair and the correponding eigenvectors in the second element

Definition at line 1168 of file Matrix.hpp.

◆ eigenNonSymmetric()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
pair<Matrix, Matrix> Matrix< T, typename >::eigenNonSymmetric ( bool  hes = true)
inline

Calculates the eigenvalues and eigenvectors of a on-symmetric matrix.

Parameters
sortwhether to sort the eigenvalues and eigenvectors
calculatethe eigenvalues and eigenvectors of a Hessenberg matrix with identical eigenvalues and eigenvectors instead
Returns
a pair containing eigenvalues in a column vector in the first element of the pair and the correponding eigenvectors in the second element

Definition at line 1242 of file Matrix.hpp.

◆ eigenSymmetric()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
pair<Matrix, Matrix> Matrix< T, typename >::eigenSymmetric ( )
inline

Calculates the eigenvalues and eigenvectors of a symmetric matrix using the Jacobi eigenvalue algorithm.

Returns
a pair containing eigenvalues in a column vector in the first element of the pair and the correponding eigenvectors in the second element

Definition at line 1175 of file Matrix.hpp.

Here is the call graph for this function:

◆ eigsort()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
static pair<Matrix, Matrix> Matrix< T, typename >::eigsort ( Matrix< T, typename >  eigenvalues,
Matrix< T, typename >  eigenvectors 
)
inlinestaticprivate

Sorts eigenvalues by magnitude, sorting their corresponding eigenvectors in te same order.

Parameters
eigenvaluescolumn vector containing eigenvalues
eigenvectorsMatrix containing the eigenvectors in its columns. It must have the same number of columns as eigenvalues has rows
Returns

Definition at line 54 of file Matrix.hpp.

Here is the call graph for this function:

◆ fill()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
static Matrix Matrix< T, typename >::fill ( size_t  rows,
size_t  cols,
double  value 
)
inlinestatic

Returns a matrix filled with a single value.

Parameters
rowsnumber of rows in the matrix
colsnumber of columns in the matrix
valuevalue to be used for initialization
Returns
a matrix with all values set to value

Definition at line 472 of file Matrix.hpp.

◆ filter()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::filter ( const Matrix< int >  bin,
bool  columns = false 
)
inline

Selects a subset of either columns or rows of the matrix.

Parameters
bina column vector containing only 0s and 1s, where indices with 1s indicate the indices of the columns/rows that will be returned by the method
columnstrue if the filter will select the columns of the matrix, otherwise, rows will be selected
Returns

Definition at line 1098 of file Matrix.hpp.

Here is the call graph for this function:

◆ fromCSV()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
static Matrix Matrix< T, typename >::fromCSV ( const string &  path)
inlinestatic

Loads CSV data into a matrix.

Parameters
pathpath of the CSV file
Returns
a matrix constructed from the contents of the CSV file

Definition at line 996 of file Matrix.hpp.

◆ getColumn()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::getColumn ( size_t  index)
inline

Gets a column from the matrix.

Parameters
indexindex of the desired column
Returns
column vector containing the values in the given column of the original matrix

Definition at line 946 of file Matrix.hpp.

◆ getColumns()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::getColumns ( const Matrix< int >  bin)
inline

Selects a subset of columns of the matrix.

Parameters
bina column vector containing only 0s and 1s, where indices with 1s indicate the indices of the columns/rows that will be returned by the method

Definition at line 1130 of file Matrix.hpp.

◆ getMinor()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
double Matrix< T, typename >::getMinor ( size_t  row,
size_t  column 
) const
inline

Returns the minor of a matrix, which is the determinant of a submatrix where a single row and column are removed.

Parameters
rowindex of the row to be removed
columnindex of the column to be removed
Returns
minor of the current matrix

Definition at line 581 of file Matrix.hpp.

Here is the call graph for this function:

◆ getRow()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::getRow ( size_t  index)
inline

Gets a row from the matrix.

Parameters
indexindex of the desired row
Returns
column vector containing the values in the given row of the original matrix

Definition at line 961 of file Matrix.hpp.

Here is the caller graph for this function:

◆ getRows()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::getRows ( const Matrix< int >  bin)
inline

Selects a subset of rows of the matrix.

Parameters
bina column vector containing only 0s and 1s, where indices with 1s indicate the indices of the columns/rows that will be returned by the method

Definition at line 1123 of file Matrix.hpp.

◆ hadamard()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::hadamard ( const Matrix< T, typename > &  b)
inline

Executes the Hadamard, or entrywise multiplication between two matrices.

Parameters
bThe other matrix
Returns
result of the Hadamard multiplication of the two matrices

Definition at line 534 of file Matrix.hpp.

◆ identity()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
static Matrix Matrix< T, typename >::identity ( size_t  size)
inlinestatic

Returns the identity matrix.

Parameters
sizedimensions of the square matrix
Returns
identity matrix

Definition at line 511 of file Matrix.hpp.

◆ inverse()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::inverse ( ) const
inline

Calculates the inverse of the current matrix.

Raises an error if the matrix is singular, that is, its determinant is equal to 0

Returns
inverse of the current matrix

Definition at line 641 of file Matrix.hpp.

◆ isBinary()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
bool Matrix< T, typename >::isBinary ( ) const
inline

Definition at line 1370 of file Matrix.hpp.

Here is the call graph for this function:

◆ isColumn()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
bool Matrix< T, typename >::isColumn ( ) const
inline

Definition at line 1308 of file Matrix.hpp.

Here is the caller graph for this function:

◆ isEmpty()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
bool Matrix< T, typename >::isEmpty ( )
inline

Checks if the matrix is empty or uninitialized.

Returns
true if the matrix is empty or uninitialized, otherwise false

Definition at line 1089 of file Matrix.hpp.

◆ isRow()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
bool Matrix< T, typename >::isRow ( ) const
inline

Definition at line 1312 of file Matrix.hpp.

◆ isSquare()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
bool Matrix< T, typename >::isSquare ( ) const
inline

Definition at line 489 of file Matrix.hpp.

◆ isSymmetric()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
bool Matrix< T, typename >::isSymmetric ( )
inline

Checks if the matrix is symmetric.

A matrix is symmetric if it is equal to its transpose

Returns
true if it is symmetric, otherwise false

Definition at line 1136 of file Matrix.hpp.

◆ max()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
T Matrix< T, typename >::max ( ) const
inline

Definition at line 1320 of file Matrix.hpp.

◆ mean() [1/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::mean ( Matrix< T, typename >  groups)
inline

Calculates means of a matrix, grouped by classes.

Parameters
groupsa column vector containing group assignments
Returns
a matrix containing as many columns as there are unique groups. Each column represents the mean of each group. Columns are sorted by the group numbers in ascending order.

Definition at line 840 of file Matrix.hpp.

Here is the call graph for this function:

◆ mean() [2/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::mean ( )
inline

Calculates the mean of the columns of the matrix.

Returns
Column vector containing the means

Definition at line 867 of file Matrix.hpp.

◆ min()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
T Matrix< T, typename >::min ( ) const
inline

Definition at line 1316 of file Matrix.hpp.

◆ minusMean()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::minusMean ( )
inline

Definition at line 1067 of file Matrix.hpp.

◆ nCols()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
size_t Matrix< T, typename >::nCols ( ) const
inline

Definition at line 87 of file Matrix.hpp.

Here is the caller graph for this function:

◆ normalize()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::normalize ( )
inline

Normalizes the column vectors of the matrix.

Normalization is done by dividing each element of a vector by the length of the vector.

Returns
Matrix with each column normalized by its length.

Definition at line 1143 of file Matrix.hpp.

◆ nRows()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
size_t Matrix< T, typename >::nRows ( ) const
inline

Definition at line 89 of file Matrix.hpp.

Here is the caller graph for this function:

◆ oneHot()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::oneHot ( )
inline

Definition at line 1330 of file Matrix.hpp.

Here is the call graph for this function:

◆ ones()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
static Matrix Matrix< T, typename >::ones ( size_t  rows,
size_t  cols 
)
inlinestatic

Returns a matrix filled with ones.

Parameters
rowsnumber of rows in the matrix
colsnumber of columns in the matrix
Returns
matrix filled with ones

Definition at line 519 of file Matrix.hpp.

◆ operator!=() [1/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::operator!= ( const double &  value)
inline

Definition at line 417 of file Matrix.hpp.

◆ operator!=() [2/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
bool Matrix< T, typename >::operator!= ( const Matrix< T, typename > &  other)
inline

Definition at line 423 of file Matrix.hpp.

◆ operator()() [1/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
T& Matrix< T, typename >::operator() ( size_t  i,
size_t  j 
)
inline

Functor used to access elements in the matrix.

Parameters
irow index
jcolumn index
Returns
element in position ij of the matrix

Definition at line 451 of file Matrix.hpp.

◆ operator()() [2/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
T Matrix< T, typename >::operator() ( size_t  i,
size_t  j 
) const
inline

Functor used to access elements in the matrix.

Parameters
irow index
jcolumn index
Returns
element in position ij of the matrix

Definition at line 460 of file Matrix.hpp.

◆ operator*()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::operator* ( const Matrix< T, typename > &  b) const
inline

Matrix multiplication operation.

Parameters
banother matrix
Returns
Result of the multiplication of both matrices

Definition at line 314 of file Matrix.hpp.

◆ operator*=() [1/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::operator*= ( double  value)
inline

Definition at line 253 of file Matrix.hpp.

◆ operator*=() [2/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix& Matrix< T, typename >::operator*= ( const Matrix< T, typename > &  other)
inline

Definition at line 365 of file Matrix.hpp.

◆ operator+()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::operator+ ( const Matrix< T, typename > &  b)
inline

Matrix addition operation.

Parameters
banother matrix
Returns
Result of the addition of both matrices

Definition at line 273 of file Matrix.hpp.

◆ operator+=() [1/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::operator+= ( double  value)
inline

Definition at line 239 of file Matrix.hpp.

◆ operator+=() [2/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix& Matrix< T, typename >::operator+= ( const Matrix< T, typename > &  other)
inline

Definition at line 335 of file Matrix.hpp.

◆ operator-() [1/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::operator- ( const Matrix< T, typename > &  b)
inline

Matrix subtraction operation.

Parameters
banother matrix
Returns
Result of the subtraction of both matrices

Definition at line 293 of file Matrix.hpp.

◆ operator-() [2/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::operator- ( )
inline

Matrix negative operation.

Returns
The negative of the current matrix

Definition at line 432 of file Matrix.hpp.

◆ operator-=() [1/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::operator-= ( double  value)
inline

Definition at line 246 of file Matrix.hpp.

◆ operator-=() [2/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix& Matrix< T, typename >::operator-= ( const Matrix< T, typename > &  other)
inline

Definition at line 349 of file Matrix.hpp.

◆ operator/=()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::operator/= ( double  value)
inline

Definition at line 260 of file Matrix.hpp.

◆ operator==() [1/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix<int> Matrix< T, typename >::operator== ( const T &  value)
inline

Definition at line 393 of file Matrix.hpp.

◆ operator==() [2/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
bool Matrix< T, typename >::operator== ( const Matrix< T, typename > &  other)
inline

Definition at line 406 of file Matrix.hpp.

◆ removeColumn()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
void Matrix< T, typename >::removeColumn ( int  position)
inline

Removes a column from the matrix.

Removal is done inplace.

Parameters
positionindex of the column to be removed.

Definition at line 772 of file Matrix.hpp.

◆ reshape()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
void Matrix< T, typename >::reshape ( size_t  rows,
size_t  cols 
)
inline

Reshapes the current matrix.

The operation is done inplace.

Parameters
rowsnew number of rows
colsnew number of columns

Definition at line 932 of file Matrix.hpp.

◆ scatter()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::scatter ( )
inline

Calculates the scatter matrix.

Columns are taken as features.

Returns
scatter matrix

Definition at line 883 of file Matrix.hpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setColumn()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
void Matrix< T, typename >::setColumn ( size_t  index,
Matrix< T >  column 
)
inline

Definition at line 1358 of file Matrix.hpp.

◆ setRow()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
void Matrix< T, typename >::setRow ( size_t  index,
Matrix< T >  row 
)
inline

Definition at line 1346 of file Matrix.hpp.

Here is the caller graph for this function:

◆ sort() [1/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
void Matrix< T, typename >::sort ( )
inline

Sorts elements of the matrix inplace.

Definition at line 799 of file Matrix.hpp.

Here is the caller graph for this function:

◆ sort() [2/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
static Matrix Matrix< T, typename >::sort ( Matrix< T, typename >  m)
inlinestatic

Sorts the elements of a matrix.

Parameters
mthe matrix whose elements will be sorted
Returns
a matrix with the same shape as m, with its elements sorted

Definition at line 807 of file Matrix.hpp.

◆ standardize() [1/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::standardize ( )
inline

Standardizes the columns of the matrix, subtracting each element of a column by the column mean and dividing it by the standard deviation of the column.

Returns
a new matrix with the columns standardized as described

Definition at line 1036 of file Matrix.hpp.

◆ standardize() [2/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::standardize ( Matrix< T, typename >  means,
Matrix< T, typename >  stds 
)
inline

Standardizes the columns of the matrix, subtracting each element of a column by the mean argument and dividing it by the stds argument.

Parameters
meansa column matrix containing the elements that will subtract each column of the original matrix
stdsa column matrix containing the elements that will divide each column of the original matrix
Returns
a new matrix with the columns standardized as described

Definition at line 1045 of file Matrix.hpp.

Here is the call graph for this function:

◆ stdev()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::stdev ( )
inline

Calculates the standard deviation of the columns of the matrix.

Returns
column vector containing standard deviations

Definition at line 919 of file Matrix.hpp.

◆ submatrix()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::submatrix ( size_t  row,
size_t  column 
) const
inline

Returns a submatrix of the current matrix, removing one row and column of the original matrix.

Parameters
rowindex of the row to be removed
columnindex of the column to be removed
Returns
submatrix of the current matrix, with one less row and column

Definition at line 556 of file Matrix.hpp.

◆ sum()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
T Matrix< T, typename >::sum ( ) const
inline

Definition at line 1300 of file Matrix.hpp.

◆ transpose()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::transpose ( ) const
inline

Returns the transpose of a matrix.

Returns
transpose of the current matrix

Definition at line 677 of file Matrix.hpp.

Here is the caller graph for this function:

◆ unique()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::unique ( ) const
inline

Returns only unique values from the matrix.

Returns
column vector containing the unique values from the matrix

Definition at line 782 of file Matrix.hpp.

Here is the caller graph for this function:

◆ validateIndexes()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
void Matrix< T, typename >::validateIndexes ( size_t  row,
size_t  col 
) const
inlineprivate

Validates if indices are contained inside the matrix.

Parameters
rowrow index
colcolumn index
Exceptions
runtimeerror if at least one of the indices is out of bounds

Definition at line 40 of file Matrix.hpp.

◆ var()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::var ( )
inline

Calculates the variance of the columns of the matrix.

Returns
Column vector containing the variances

Definition at line 903 of file Matrix.hpp.

◆ WithinClassScatter()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix Matrix< T, typename >::WithinClassScatter ( Matrix< T, typename >  y)
inline

Definition at line 1271 of file Matrix.hpp.

Here is the call graph for this function:

◆ zeros()

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
static Matrix Matrix< T, typename >::zeros ( size_t  rows,
size_t  cols 
)
inlinestatic

Returns a matrix filled with zeros.

Parameters
rowsnumber of rows in the matrix
colsnumber of columns in the matrix
Returns
matrix filled with zeros

Definition at line 527 of file Matrix.hpp.

Friends And Related Function Documentation

◆ operator* [1/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix operator* ( const Matrix< T, typename > &  m,
double  value 
)
friend

Scalar multiplication.

Parameters
ma matrix
valuescalar to be multiplied by the matrix
Returns
the result of the scalar multiplication of m and value

Definition at line 183 of file Matrix.hpp.

◆ operator* [2/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix operator* ( double  value,
const Matrix< T, typename > &  m 
)
friend

Scalar multiplication.

Parameters
ma matrix
valuescalar to be multiplied by the matrix
Returns
the result of the scalar multiplication of m and value

Definition at line 200 of file Matrix.hpp.

◆ operator+ [1/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix operator+ ( const Matrix< T, typename > &  m,
double  value 
)
friend

Scalar addition.

Parameters
ma matrix
valuescalar to be added to the matrix
Returns
the result of the scalar addition of m and value

Definition at line 142 of file Matrix.hpp.

◆ operator+ [2/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix operator+ ( double  value,
const Matrix< T, typename > &  m 
)
friend

Scalar addition.

Parameters
ma matrix
valuescalar to be added to the matrix
Returns
the result of the scalar addition of m and value

Definition at line 159 of file Matrix.hpp.

◆ operator- [1/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix operator- ( const Matrix< T, typename > &  m,
double  value 
)
friend

Scalar subtraction.

Parameters
ma matrix
valuescalar to be subtracted to the matrix
Returns
the result of the scalar subtraction of m and value

Definition at line 167 of file Matrix.hpp.

◆ operator- [2/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix operator- ( double  value,
const Matrix< T, typename > &  m 
)
friend

Scalar subtraction.

Parameters
ma matrix
valuescalar to be subtracted to the matrix
Returns
the result of the scalar subtraction of m and value

Definition at line 175 of file Matrix.hpp.

◆ operator/ [1/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix operator/ ( const Matrix< T, typename > &  m,
double  value 
)
friend

Scalar division.

Parameters
ma matrix
valuescalar to be divide the matrix by
Returns
the result of the scalar division of m by value

Definition at line 208 of file Matrix.hpp.

◆ operator/ [2/2]

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
Matrix operator/ ( double  value,
const Matrix< T, typename > &  m 
)
friend

Scalar division.

Parameters
valuescalar that will be divided by the matrix
ma matrix
Returns
the result of the scalar division of value by m

Definition at line 225 of file Matrix.hpp.

◆ operator<<

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
ostream& operator<< ( ostream &  os,
const Matrix< T, typename > &  matrix 
)
friend

Prints a matrix.

Parameters
osoutput stream
matrixthe matrix to be printed
Returns
output stream with the string representation of the matrix

Definition at line 977 of file Matrix.hpp.

Field Documentation

◆ mCols

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
size_t Matrix< T, typename >::mCols
private

Definition at line 33 of file Matrix.hpp.

◆ mData

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
std::vector<T> Matrix< T, typename >::mData
private

Definition at line 34 of file Matrix.hpp.

◆ mRows

template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
size_t Matrix< T, typename >::mRows
private

Definition at line 32 of file Matrix.hpp.


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