TODO : Transform into template for storing something else than double. More...
#include <AMatrix.hpp>
TODO : Transform into template for storing something else than double.
This class is the root of the Matrix organization in gstlearn A matrix is a 2-D organization: it is characterized by its number of rows and its number of columns. Although the user should not bother with this remark, the elements of a matrix processed in 'gstlearn' are stored in a Row-major format. This is to say that the internal rank of an element characterized by its row and column numbers is: (icol * getNRows() + irow)
Since gstlearn version v1.3:
Public Member Functions | |
AMatrix (Id nrow=0, Id ncol=0) | |
AMatrix (const AMatrix &m) | |
AMatrix & | operator= (const AMatrix &m) |
virtual | ~AMatrix () |
String | toString (const AStringFormat *strfmt=nullptr) const override |
Interface to AStringable. | |
virtual bool | isSparse () const =0 |
Functions to be implemented in derived classes. | |
virtual bool | isDense () const =0 |
virtual double | getValue (Id irow, Id icol) const =0 |
virtual void | setValue (Id irow, Id icol, double value)=0 |
virtual void | updValue (Id irow, Id icol, const EOperator &oper, double value)=0 |
virtual void | setColumn (Id icol, const 1 &tab)=0 |
virtual void | setColumnToConstant (Id icol, double value)=0 |
virtual void | setRow (Id irow, const 1 &tab)=0 |
virtual void | setRowToConstant (Id irow, double value)=0 |
virtual void | setDiagonal (const 1 &tab)=0 |
virtual void | setDiagonalToConstant (double value=1.)=0 |
virtual void | addScalar (double v)=0 |
virtual void | addScalarDiag (double v)=0 |
virtual void | prodScalar (double v)=0 |
virtual void | fill (double value)=0 |
virtual void | multiplyRow (const 1 &vec)=0 |
virtual void | multiplyColumn (const 1 &vec)=0 |
virtual void | divideRow (const 1 &vec)=0 |
virtual void | divideColumn (const 1 &vec)=0 |
virtual bool | isSquare (bool printWhyNot=false) const |
virtual bool | isSymmetric (double eps=EPSILON10, bool printWhyNot=false) const |
virtual bool | mustBeSymmetric () const |
virtual void | reset (Id nrows, Id ncols) |
virtual void | resetFromValue (Id nrows, Id ncols, double value) |
Reset the matrix to new dimensions and fill with a new value. | |
virtual void | resetFromArray (Id nrows, Id ncols, const double *tab, bool byCol=true) |
Reset the matrix from an array of double values. | |
virtual void | resetFromVD (Id nrows, Id ncols, const 1 &tab, bool byCol=true) |
Reset the matrix from a vector of double values. | |
virtual void | resetFromVVD (const 1 &tab, bool byCol=true) |
Reset the matrix from an array of double values. | |
virtual void | transposeInPlace () |
virtual AMatrix * | transpose () const |
virtual | getRow (Id irow) const |
virtual | getColumn (Id icol) const |
virtual void | prodMatMatInPlace (const AMatrix *x, const AMatrix *y, bool transposeX=false, bool transposeY=false) |
virtual void | prodNormMatMatInPlace (const AMatrix *a, const AMatrix *m, bool transpose=false) |
virtual void | prodNormMatVecInPlace (const AMatrix *a, const 1 &vec, bool transpose=false) |
virtual void | prodNormMatInPlace (const AMatrix *a, bool transpose=false) |
virtual void | linearCombination (double val1, const AMatrix *mat1, double val2=1., const AMatrix *mat2=nullptr, double val3=1., const AMatrix *mat3=nullptr) |
Perfom the algebraic equation this = val1 * mat1 + val2 * mat2 + val3 * mat3. | |
virtual void | addMat (const AMatrix &y, double cx=1., double cy=1.) |
virtual NF_Triplet | getMatrixToTriplet (Id shiftRow=0, Id shiftCol=0) const |
void | clear () |
void | resize (Id nrows, Id ncols) |
Resize the matrix to new dimensions (this method doesn't change the storage type) | |
bool | isValid (Id irow, Id icol, bool printWhyNot=false) const |
bool | isIdentity (bool printWhyNot=false) const |
void | addValue (Id irow, Id icol, double value) |
bool | isSame (const AMatrix &m, double eps=EPSILON4, bool printWhyNot=false) |
bool | isSameSize (const AMatrix &m) const |
bool | empty () const |
double | compare (const AMatrix &mat) const |
Id | getNRows () const |
Id | getNCols () const |
Id | size () const |
getValues (bool byCol=true) const | |
const & | getDiagonal (Id shift=0) const |
bool | isColumnDefined (Id icol) const |
bool | isRowDefined (Id irow) const |
Id | getNColDefined () const |
Id | getNRowDefined () const |
getColumnByRowRange (Id icol, Id rowFrom, Id rowTo) const | |
bool | isNonNegative (bool verbose=false) const |
prodMatVec (const 1 &x, bool transpose=false) const | |
void | prodMatVecInPlace (const 1 &x, 1 &y, bool transpose=false) const |
void | prodMatVecInPlaceC (const constvect x, vect y, bool transpose=false) const |
void | addProdMatVecInPlaceC (const constvect x, vect y, bool transpose=false) const |
prodVecMat (const 1 &x, bool transpose=false) const | |
void | prodVecMatInPlace (const 1 &x, 1 &y, bool transpose=false) const |
void | prodVecMatInPlaceC (const constvect x, vect y, bool transpose=false) const |
void | addProdVecMatInPlaceC (const constvect x, vect y, bool transpose=false) const |
double | prodVecMatVec (const 1 &x, const 1 &y) const |
void | prodMat (const AMatrix *matY, bool transposeY=false) |
Id | invert () |
Id | solve (const 1 &b, 1 &x) const |
void | dumpElements (const String &title, Id ifrom, Id ito) const |
void | dumpStatistics (const String &title) const |
void | setIdentity (double value=1.) |
void | fillRandom (Id seed=432432, double zeroPercent=0) |
void | setValues (const 1 &values, bool byCol=true) |
double | getMeanByColumn (Id icol) const |
double | getMinimum () const |
double | getMaximum () const |
double | getNormInf () const |
void | copyReduce (const AMatrix *x, const 1 &validRows, const 1 &validCols) |
void | copyElements (const AMatrix &m, double factor=1.) |
void | makePositiveColumn () |
void | dumpRange (const char *title) |
double | operator() (Id row, Id col) const |
double & | operator() (Id row, Id col) |
![]() | |
AStringable () | |
AStringable (const AStringable &r) | |
AStringable & | operator= (const AStringable &r) |
virtual | ~AStringable () |
virtual void | display (const AStringFormat *strfmt=nullptr) const final |
virtual void | display (Id level) const final |
![]() | |
ICloneable () | |
virtual | ~ICloneable () |
virtual ICloneable * | clone () const =0 |
std::shared_ptr< ICloneable > | cloneShared () const |
std::unique_ptr< ICloneable > | cloneUnique () const |
gstlrn::AMatrix::AMatrix | ( | const AMatrix & | m | ) |
|
virtual |
|
virtual |
Add a matrix (multiplied by a constant)
Add the matrix 'y' to the current Matrix
y | Matrix to be added |
cx | Multiplicative parameter for this |
cy | Multiplicative parameter for y |
Reimplemented in gstlrn::MatrixDense, and gstlrn::MatrixSparse.
void gstlrn::AMatrix::addProdMatVecInPlaceC | ( | const constvect | x, |
vect | y, | ||
bool | transpose = false |
||
) | const |
void gstlrn::AMatrix::addProdVecMatInPlaceC | ( | const constvect | x, |
vect | y, | ||
bool | transpose = false |
||
) | const |
|
pure virtual |
Add a value to each matrix component
Implemented in gstlrn::MatrixDense, and gstlrn::MatrixSparse.
|
pure virtual |
Add value to matrix diagonal
Implemented in gstlrn::MatrixDense, and gstlrn::MatrixSparse.
void gstlrn::AMatrix::clear | ( | ) |
double gstlrn::AMatrix::compare | ( | const AMatrix & | mat | ) | const |
Returns the sum of absolute difference between argument and this
void gstlrn::AMatrix::copyElements | ( | const AMatrix & | m, |
double | factor = 1. |
||
) |
Copy the contents of matrix 'm' into 'this' Warning: matrices must have the same dimensions (not checked)
m | Input matrix |
factor | Multiplicative factor (applied to each element) |
void gstlrn::AMatrix::copyReduce | ( | const AMatrix * | x, |
const 1 & | validRows, | ||
const 1 & | validCols | ||
) |
|
pure virtual |
Divide a Matrix column-wise
Implemented in gstlrn::MatrixDense, and gstlrn::MatrixSparse.
|
pure virtual |
Divide a Matrix row-wise
Implemented in gstlrn::MatrixDense, and gstlrn::MatrixSparse.
Dump a specific range of samples from the internal storage
void gstlrn::AMatrix::dumpRange | ( | const char * | title | ) |
void gstlrn::AMatrix::dumpStatistics | ( | const String & | title | ) | const |
Dump statistics on the Matrix
|
inline |
Returns if the current matrix is Empty
|
pure virtual |
Set all the values of the Matrix at once
Implemented in gstlrn::MatrixDense, and gstlrn::MatrixSparse.
void gstlrn::AMatrix::fillRandom | ( | Id | seed = 432432 , |
double | zeroPercent = 0 |
||
) |
|
virtual |
Extract a Column
Reimplemented in gstlrn::MatrixDense.
Extract a portion of a Column
const & gstlrn::AMatrix::getDiagonal | ( | Id | shift = 0 | ) | const |
Extract a Diagonal (main or secondary) of this
|
virtual |
Extract the contents of the matrix
From a matrix of any type, creates the three vectors of the triplet (specific format for creating efficiently a Sparse matrix) It only takes the only non-zero elements of the matrix
Reimplemented in gstlrn::MatrixSparse.
double gstlrn::AMatrix::getMaximum | ( | ) | const |
double gstlrn::AMatrix::getMeanByColumn | ( | Id | icol | ) | const |
double gstlrn::AMatrix::getMinimum | ( | ) | const |
Id gstlrn::AMatrix::getNColDefined | ( | ) | const |
Define the number of defined columns
|
inline |
Returns the number of columns
double gstlrn::AMatrix::getNormInf | ( | ) | const |
Id gstlrn::AMatrix::getNRowDefined | ( | ) | const |
Define the number of defined rows
|
inline |
Returns the number of rows
|
virtual |
Extract a Row
Reimplemented in gstlrn::MatrixDense.
Gets the value at row 'irow' and column 'icol'
Implemented in gstlrn::MatrixDense, and gstlrn::MatrixSparse.
gstlrn::AMatrix::getValues | ( | bool | byCol = true | ) | const |
Returns the contents of the whole matrix as a VectorDouble
Id gstlrn::AMatrix::invert | ( | ) |
Matrix inversion in place
bool gstlrn::AMatrix::isColumnDefined | ( | Id | icol | ) | const |
Checks if a Column is valid (contains a non TEST value)
|
pure virtual |
Returns if the matrix belongs to the MatrixDense class
Implemented in gstlrn::MatrixDense, and gstlrn::MatrixSparse.
bool gstlrn::AMatrix::isIdentity | ( | bool | printWhyNot = false | ) | const |
Check if the matrix is square and Identity
Indicate if the current matrix is the Identity
printWhyNot | Print the message is the answer is false |
bool gstlrn::AMatrix::isNonNegative | ( | bool | verbose = false | ) | const |
Check if the matrix does not contain any negative element
Check if all the elements of a matrix are non-negative
[in] | verbose | True for the verbose option |
bool gstlrn::AMatrix::isRowDefined | ( | Id | irow | ) | const |
Checks if a Row is valid (contains a non TEST value)
bool gstlrn::AMatrix::isSame | ( | const AMatrix & | m, |
double | eps = EPSILON4 , |
||
bool | printWhyNot = false |
||
) |
Check if a matrix is the same as me (norm L1)
Check that Matrix 'm' is equal to the current Matrix
m | Matrix to be compared to the current Matrix |
eps | Epsilon for double equality comparison |
printWhyNot | Print the message is the answer is false |
bool gstlrn::AMatrix::isSameSize | ( | const AMatrix & | m | ) | const |
Check that 'm' has the same dimensions as 'this'
Check that Matrix 'm' share the same dimensions as current one
m | Matrix to be compared to the current Matrix |
|
pure virtual |
Functions to be implemented in derived classes.
Returns if the current matrix is Sparse
Implemented in gstlrn::MatrixDense, and gstlrn::MatrixSparse.
|
virtual |
Check if the matrix is (non empty) square
Indicate if the given matrce is a square matrix
printWhyNot | Print the message is the answer is false |
Reimplemented in gstlrn::MatrixSquare.
|
virtual |
Check if the input matrix is (non empty and square) symmetric
Indicate if the current matrix is symmetric
eps | Epsilon for double equality comparison |
printWhyNot | Print the message is the answer is false |
Reimplemented in gstlrn::MatrixSymmetric.
Indicate if the given indices are valid for the current matrix size
Indicate if the given indices are valid for the current matrix size
irow | Row index |
icol | Column index |
printWhyNot | Print the message is the answer is false |
|
virtual |
Perfom the algebraic equation this = val1 * mat1 + val2 * mat2 + val3 * mat3.
Perform 'this' = 'val1' * 'mat1' + 'val2' * 'mat2' + 'val3' * 'mat3'
val1 | Coefficient of first matrx |
mat1 | First matrix (optional) |
val2 | Coefficient of second matrix |
mat2 | Second matrix (optional) |
val3 | Coefficient of third matrix |
mat3 | Third matrix (optional) |
Reimplemented in gstlrn::MatrixDense, and gstlrn::MatrixSparse.
void gstlrn::AMatrix::makePositiveColumn | ( | ) |
Modify the contents of the matrix so that each column has a positive sum of elements. If this is not the case, simply invert the sign of the column
|
pure virtual |
Multiply a Matrix column-wise
Implemented in gstlrn::MatrixDense, and gstlrn::MatrixSparse.
|
pure virtual |
Multiply a Matrix row-wise
Implemented in gstlrn::MatrixDense, and gstlrn::MatrixSparse.
|
inlinevirtual |
Say if the matrix must be symmetric
Reimplemented in gstlrn::MatrixSymmetric, gstlrn::MatrixDense, and gstlrn::MatrixSquare.
void gstlrn::AMatrix::prodMat | ( | const AMatrix * | matY, |
bool | transposeY = false |
||
) |
Perform 'this' = 'y' %*% 'this' or 'this' %*% 'y'
|
virtual |
Perform 'this' = 'x' * 'y'
Store the product of 'x'(or 't(x)') by 'y' (or 't(y') in this
x | First Matrix |
y | Second matrix |
transposeX | True if first matrix must be transposed |
transposeY | True if second matrix must be transposed |
Reimplemented in gstlrn::MatrixDense, and gstlrn::MatrixSparse.
gstlrn::AMatrix::prodMatVec | ( | const 1 & | x, |
bool | transpose = false |
||
) | const |
Perform 'y' = 'this' * 'x'
void gstlrn::AMatrix::prodMatVecInPlace | ( | const 1 & | x, |
1 & | y, | ||
bool | transpose = false |
||
) | const |
Returns 'y' = 'this' %*% 'x'
x | Input vector |
y | Output vector |
transpose | True if the matrix 'this' must be transposed |
void gstlrn::AMatrix::prodMatVecInPlaceC | ( | const constvect | x, |
vect | y, | ||
bool | transpose = false |
||
) | const |
|
virtual |
Perform 'this' = 't(A)' %*% 'A' or 'A' %*% 't(A)'
Reimplemented in gstlrn::MatrixDense, and gstlrn::MatrixSparse.
|
virtual |
Perform 'this' = 't(A)' %*% 'M' %*% 'A' or 'A' %*% 'M' %*% 't(A)'
Product 't(A)' %*% 'M' %*% 'A' or 'A' %*% 'M' %*% 't(A)' stored in 'this'
a | Matrix A |
m | Matrix M |
transpose | True for first implementation, False for the second |
Reimplemented in gstlrn::MatrixDense, and gstlrn::MatrixSparse.
|
virtual |
Perform 'this' = 't(A)' %*% 'vec' %*% 'A' or 'A' %*% 'vec' %*% 't(A)'
Reimplemented in gstlrn::MatrixDense, and gstlrn::MatrixSparse.
|
pure virtual |
Multiply each matrix component by a value
Implemented in gstlrn::MatrixDense, and gstlrn::MatrixSparse.
gstlrn::AMatrix::prodVecMat | ( | const 1 & | x, |
bool | transpose = false |
||
) | const |
Perform 'y' = 'x' * 'this'
void gstlrn::AMatrix::prodVecMatInPlace | ( | const 1 & | x, |
1 & | y, | ||
bool | transpose = false |
||
) | const |
void gstlrn::AMatrix::prodVecMatInPlaceC | ( | const constvect | x, |
vect | y, | ||
bool | transpose = false |
||
) | const |
double gstlrn::AMatrix::prodVecMatVec | ( | const 1 & | x, |
const 1 & | y | ||
) | const |
Perform x %*% 'this' %*% y
Reimplemented in gstlrn::Table.
|
virtual |
Reset the matrix from an array of double values.
nrows | New number of rows |
ncols | New number of columns |
tab | The array of values |
byCol | True if values are column-major in the array |
Reimplemented in gstlrn::MatrixSparse.
Reset the matrix to new dimensions and fill with a new value.
nrows | New number of rows |
ncols | New number of columns |
value | The new value used to fill the matrix |
Reimplemented in gstlrn::MatrixSparse.
Reset the matrix from a vector of double values.
nrows | New number of rows |
ncols | New number of columns |
tab | The vector of values |
byCol | True if values are column-major in the vector |
Reimplemented in gstlrn::MatrixSparse.
|
virtual |
Reset the matrix from an array of double values.
tab | The array of values |
byCol | True if values are column-major in the array |
Reimplemented in gstlrn::MatrixSparse, gstlrn::MatrixSquare, and gstlrn::MatrixSymmetric.
Resize the matrix to new dimensions (this method doesn't change the storage type)
Modify the dimension of the matrix (if needed)
nrows | New number of rows |
ncols | New number of columns |
|
pure virtual |
Set the contents of a Column
Implemented in gstlrn::MatrixDense, and gstlrn::MatrixSparse.
|
pure virtual |
Set the contents of a Column to a constant value
Implemented in gstlrn::MatrixDense, and gstlrn::MatrixSparse.
|
pure virtual |
Set the contents of the (main) Diagonal
Implemented in gstlrn::MatrixDense, and gstlrn::MatrixSparse.
|
pure virtual |
Set the contents of the (main) Diagonal to a constant value
Implemented in gstlrn::MatrixDense, and gstlrn::MatrixSparse.
void gstlrn::AMatrix::setIdentity | ( | double | value = 1. | ) |
Sets the matrix as Identity
|
pure virtual |
Set the contents of a Row
Implemented in gstlrn::MatrixDense, and gstlrn::MatrixSparse.
|
pure virtual |
Set the contents of a Row to a constant value
Implemented in gstlrn::MatrixDense, and gstlrn::MatrixSparse.
Sets the value at row 'irow' and column 'icol'
Implemented in gstlrn::MatrixDense, and gstlrn::MatrixSparse.
void gstlrn::AMatrix::setValues | ( | const 1 & | values, |
bool | byCol = true |
||
) |
Filling the matrix with an array of values Note that this array is ALWAYS dimensioned to the total number of elements in the matrix. Kept for compatibility with old code where matrix contents was stored as a VectorDouble
values | |
byCol | true for Column major; false for Row Major |
|
inline |
Get the total number of elements of the (full) matrix
Id gstlrn::AMatrix::solve | ( | const 1 & | b, |
1 & | x | ||
) | const |
Solving the Matrix Linear system
|
overridevirtual |
Interface to AStringable.
Reimplemented from gstlrn::AStringable.
Reimplemented in gstlrn::ProjMatrix, gstlrn::MatrixSparse, and gstlrn::Table.
|
virtual |
Transpose the matrix and return it as a copy
Reimplemented in gstlrn::MatrixSparse.
|
virtual |
Transpose the matrix in place
|
pure virtual |
Update the value at row 'irow' and column 'icol'
Implemented in gstlrn::MatrixDense, and gstlrn::MatrixSparse.