|
C++ matrix implementation
|
Matrix implementation, with a series of linear algebra functions. More...
#include <Matrix.hpp>
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... | |
| Matrix & | operator+= (const Matrix &other) |
| Matrix & | operator-= (const Matrix &other) |
| Matrix & | operator*= (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... | |
| T | 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, Matrix > | eigen () |
| Calculates the eigenvalues and eigenvectors of a matrix. More... | |
| pair< Matrix, Matrix > | eigenSymmetric () |
| Calculates the eigenvalues and eigenvectors of a symmetric matrix using the Jacobi eigenvalue algorithm. More... | |
| pair< Matrix, Matrix > | eigenNonSymmetric (bool hes=true) |
| Calculates the eigenvalues and eigenvectors of a on-symmetric matrix. More... | |
| Matrix | WithinClassScatter (Matrix y) |
| Matrix | BetweenClassScatter (Matrix y) |
| T | sum () const |
| bool | isColumn () const |
| bool | isRow () const |
| T | min () const |
| T | 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, Matrix > | eigsort (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... | |
Matrix implementation, with a series of linear algebra functions.
| T | The arithmetic type the matrix will store |
Definition at line 30 of file Matrix.hpp.
| enum Matrix::Axis |
| Enumerator | |
|---|---|
| ALL | |
| ROWS | |
| COLUMNS | |
Definition at line 85 of file Matrix.hpp.
|
inline |
Initializes an empty matrix.
Definition at line 94 of file Matrix.hpp.
|
inline |
Initializes a square matrix.
| dimension | number of rows and columns |
Definition at line 100 of file Matrix.hpp.
|
inline |
Initializes a matrix with a predetermined number of rows and columns.
| rows | number of rows in the matrix |
| cols | number of columns in the matrix |
Definition at line 107 of file Matrix.hpp.
|
inline |
Initializes a matrix with a predetermined number of rows and columns and populates it with data.
| rows | number of rows in the matrix |
| cols | number of columns in the matrix |
| data | a vector containing rows * cols elements to populate the matrix |
Definition at line 117 of file Matrix.hpp.
|
inline |
Definition at line 126 of file Matrix.hpp.
|
inline |
Adds a column at the end of the matrix.
Addition is done inplace.
| values | a column vector containing the values to be added in the new column |
Definition at line 692 of file Matrix.hpp.
|
inline |
Adds a column to the matrix at the given position.
Addition is done inplace.
| values | a column vector containing the values to be added in the new column |
| position | index 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.
|
inline |
Adds a row at the end of the matrix.
Addition is done inplace.
| values | a column vector containing the values to be added in the new row |
Definition at line 698 of file Matrix.hpp.
|
inline |
Adds a row to the matrix at the given position.
Addition is done inplace.
| values | a column vector containing the values to be added in the new row |
| position | index 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.
|
inline |
Returns the adjugate of the current matrix, which is the transpose of its cofactor matrix.
Definition at line 634 of file Matrix.hpp.
|
inline |
Definition at line 1324 of file Matrix.hpp.
|
inline |
Creates a diagonal matrix from a row or column vector.
Definition at line 1010 of file Matrix.hpp.
|
inline |
Calculates the cofactor of a matrix at a given point.
| row | index of the row where the cofactor will be calculated |
| column | index of the column where the cofactor will be calculated |
Definition at line 600 of file Matrix.hpp.
|
inline |
Calculates the cofactor matrix.
Definition at line 620 of file Matrix.hpp.
|
inline |
Checks if the matrix contains a value.
| value | the vaue to look for |
Definition at line 1083 of file Matrix.hpp.
|
inline |
Returns a copy of the matrix.
Definition at line 1027 of file Matrix.hpp.
|
inline |
Counts occurrences of elements in a matrix.
Definition at line 818 of file Matrix.hpp.
|
inline |
Calculates the covariance matrix of the current matrix.
Columns are taken as features.
Definition at line 897 of file Matrix.hpp.
|
inline |
Calculates the determinant of the matrix.
Definition at line 656 of file Matrix.hpp.
|
inlinestatic |
Creates a square matrix with a fixed value on the diagonal.
| size | dimensions of the square matrix |
| value | value to be used in the diagonal |
Definition at line 481 of file Matrix.hpp.
|
inline |
Definition at line 494 of file Matrix.hpp.
|
inline |
Calculates the eigenvalues and eigenvectors of a matrix.
| sort | whether to sort the eigenvalues and eigenvectors |
Definition at line 1168 of file Matrix.hpp.
|
inline |
Calculates the eigenvalues and eigenvectors of a on-symmetric matrix.
| sort | whether to sort the eigenvalues and eigenvectors |
| calculate | the eigenvalues and eigenvectors of a Hessenberg matrix with identical eigenvalues and eigenvectors instead |
Definition at line 1242 of file Matrix.hpp.
|
inline |
Calculates the eigenvalues and eigenvectors of a symmetric matrix using the Jacobi eigenvalue algorithm.
Definition at line 1175 of file Matrix.hpp.
|
inlinestaticprivate |
Sorts eigenvalues by magnitude, sorting their corresponding eigenvectors in te same order.
| eigenvalues | column vector containing eigenvalues |
| eigenvectors | Matrix containing the eigenvectors in its columns. It must have the same number of columns as eigenvalues has rows |
Definition at line 54 of file Matrix.hpp.
|
inlinestatic |
Returns a matrix filled with a single value.
| rows | number of rows in the matrix |
| cols | number of columns in the matrix |
| value | value to be used for initialization |
value Definition at line 472 of file Matrix.hpp.
|
inline |
Selects a subset of either columns or rows of the matrix.
| bin | a 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 |
| columns | true if the filter will select the columns of the matrix, otherwise, rows will be selected |
Definition at line 1098 of file Matrix.hpp.
|
inlinestatic |
Loads CSV data into a matrix.
| path | path of the CSV file |
Definition at line 996 of file Matrix.hpp.
|
inline |
Gets a column from the matrix.
| index | index of the desired column |
Definition at line 946 of file Matrix.hpp.
|
inline |
Selects a subset of columns of the matrix.
| bin | a 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.
|
inline |
Returns the minor of a matrix, which is the determinant of a submatrix where a single row and column are removed.
| row | index of the row to be removed |
| column | index of the column to be removed |
Definition at line 581 of file Matrix.hpp.
|
inline |
Gets a row from the matrix.
| index | index of the desired row |
Definition at line 961 of file Matrix.hpp.
|
inline |
Selects a subset of rows of the matrix.
| bin | a 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.
|
inline |
Executes the Hadamard, or entrywise multiplication between two matrices.
| b | The other matrix |
Definition at line 534 of file Matrix.hpp.
|
inlinestatic |
Returns the identity matrix.
| size | dimensions of the square matrix |
Definition at line 511 of file Matrix.hpp.
|
inline |
Calculates the inverse of the current matrix.
Raises an error if the matrix is singular, that is, its determinant is equal to 0
Definition at line 641 of file Matrix.hpp.
|
inline |
|
inline |
|
inline |
Checks if the matrix is empty or uninitialized.
Definition at line 1089 of file Matrix.hpp.
|
inline |
Definition at line 1312 of file Matrix.hpp.
|
inline |
Definition at line 489 of file Matrix.hpp.
|
inline |
Checks if the matrix is symmetric.
A matrix is symmetric if it is equal to its transpose
Definition at line 1136 of file Matrix.hpp.
|
inline |
Definition at line 1320 of file Matrix.hpp.
|
inline |
Calculates means of a matrix, grouped by classes.
| groups | a column vector containing group assignments |
Definition at line 840 of file Matrix.hpp.
|
inline |
Calculates the mean of the columns of the matrix.
Definition at line 867 of file Matrix.hpp.
|
inline |
Definition at line 1316 of file Matrix.hpp.
|
inline |
Definition at line 1067 of file Matrix.hpp.
|
inline |
|
inline |
Normalizes the column vectors of the matrix.
Normalization is done by dividing each element of a vector by the length of the vector.
Definition at line 1143 of file Matrix.hpp.
|
inline |
|
inlinestatic |
Returns a matrix filled with ones.
| rows | number of rows in the matrix |
| cols | number of columns in the matrix |
Definition at line 519 of file Matrix.hpp.
|
inline |
Definition at line 417 of file Matrix.hpp.
|
inline |
Definition at line 423 of file Matrix.hpp.
|
inline |
Functor used to access elements in the matrix.
| i | row index |
| j | column index |
Definition at line 451 of file Matrix.hpp.
|
inline |
Functor used to access elements in the matrix.
| i | row index |
| j | column index |
Definition at line 460 of file Matrix.hpp.
|
inline |
Matrix multiplication operation.
| b | another matrix |
Definition at line 314 of file Matrix.hpp.
|
inline |
Definition at line 253 of file Matrix.hpp.
|
inline |
Definition at line 365 of file Matrix.hpp.
|
inline |
Matrix addition operation.
| b | another matrix |
Definition at line 273 of file Matrix.hpp.
|
inline |
Definition at line 239 of file Matrix.hpp.
|
inline |
Definition at line 335 of file Matrix.hpp.
|
inline |
Matrix subtraction operation.
| b | another matrix |
Definition at line 293 of file Matrix.hpp.
|
inline |
Matrix negative operation.
Definition at line 432 of file Matrix.hpp.
|
inline |
Definition at line 246 of file Matrix.hpp.
|
inline |
Definition at line 349 of file Matrix.hpp.
|
inline |
Definition at line 260 of file Matrix.hpp.
|
inline |
Definition at line 393 of file Matrix.hpp.
|
inline |
Definition at line 406 of file Matrix.hpp.
|
inline |
Removes a column from the matrix.
Removal is done inplace.
| position | index of the column to be removed. |
Definition at line 772 of file Matrix.hpp.
|
inline |
Reshapes the current matrix.
The operation is done inplace.
| rows | new number of rows |
| cols | new number of columns |
Definition at line 932 of file Matrix.hpp.
|
inline |
Calculates the scatter matrix.
Columns are taken as features.
Definition at line 883 of file Matrix.hpp.
|
inline |
Definition at line 1358 of file Matrix.hpp.
|
inline |
Sorts elements of the matrix inplace.
Definition at line 799 of file Matrix.hpp.
|
inlinestatic |
Sorts the elements of a matrix.
| m | the matrix whose elements will be sorted |
m, with its elements sorted Definition at line 807 of file Matrix.hpp.
|
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.
Definition at line 1036 of file Matrix.hpp.
|
inline |
Standardizes the columns of the matrix, subtracting each element of a column by the mean argument and dividing it by the stds argument.
| means | a column matrix containing the elements that will subtract each column of the original matrix |
| stds | a column matrix containing the elements that will divide each column of the original matrix |
Definition at line 1045 of file Matrix.hpp.
|
inline |
Calculates the standard deviation of the columns of the matrix.
Definition at line 919 of file Matrix.hpp.
|
inline |
Returns a submatrix of the current matrix, removing one row and column of the original matrix.
| row | index of the row to be removed |
| column | index of the column to be removed |
Definition at line 556 of file Matrix.hpp.
|
inline |
Definition at line 1300 of file Matrix.hpp.
|
inline |
Returns the transpose of a matrix.
Definition at line 677 of file Matrix.hpp.
|
inline |
Returns only unique values from the matrix.
Definition at line 782 of file Matrix.hpp.
|
inlineprivate |
Validates if indices are contained inside the matrix.
| row | row index |
| col | column index |
| runtime | error if at least one of the indices is out of bounds |
Definition at line 40 of file Matrix.hpp.
|
inline |
Calculates the variance of the columns of the matrix.
Definition at line 903 of file Matrix.hpp.
|
inlinestatic |
Returns a matrix filled with zeros.
| rows | number of rows in the matrix |
| cols | number of columns in the matrix |
Definition at line 527 of file Matrix.hpp.
|
friend |
Scalar multiplication.
| m | a matrix |
| value | scalar to be multiplied by the matrix |
m and value Definition at line 183 of file Matrix.hpp.
|
friend |
Scalar multiplication.
| m | a matrix |
| value | scalar to be multiplied by the matrix |
m and value Definition at line 200 of file Matrix.hpp.
|
friend |
Scalar addition.
| m | a matrix |
| value | scalar to be added to the matrix |
m and value Definition at line 142 of file Matrix.hpp.
|
friend |
Scalar addition.
| m | a matrix |
| value | scalar to be added to the matrix |
m and value Definition at line 159 of file Matrix.hpp.
|
friend |
Scalar subtraction.
| m | a matrix |
| value | scalar to be subtracted to the matrix |
m and value Definition at line 167 of file Matrix.hpp.
|
friend |
Scalar subtraction.
| m | a matrix |
| value | scalar to be subtracted to the matrix |
m and value Definition at line 175 of file Matrix.hpp.
|
friend |
Scalar division.
| m | a matrix |
| value | scalar to be divide the matrix by |
m by value Definition at line 208 of file Matrix.hpp.
|
friend |
Scalar division.
| value | scalar that will be divided by the matrix |
| m | a matrix |
value by m Definition at line 225 of file Matrix.hpp.
|
friend |
Prints a matrix.
| os | output stream |
| matrix | the matrix to be printed |
Definition at line 977 of file Matrix.hpp.
|
private |
Definition at line 33 of file Matrix.hpp.
|
private |
Definition at line 34 of file Matrix.hpp.
|
private |
Definition at line 32 of file Matrix.hpp.
1.8.14