gstlearn  0.2.1
Geostatistics & Machine Learning toolbox
AMatrix Class Referenceabstract

TODO : Transform into template for storing something else from double. More...

#include <AMatrix.hpp>

Inheritance diagram for AMatrix:
AStringable ICloneable AMatrixSquare MatrixRectangular MatrixSquareGeneral MatrixSquareSymmetric Table MatrixSquareDiagonal MatrixSquareDiagonalCst

Public Member Functions

virtual ~AMatrix ()
 
void init (int nrows, int ncols, bool sparse=false)
 
int getNRows () const
 
int getNCols () const
 
int getNTotal () const
 
virtual double getValue (int irow, int icol) const
 
virtual double getValue (int rank) const
 
virtual double & getValueRef (int irow, int icol)
 
VectorDouble getValues () const
 
void getValuesAsTriplets (VectorInt &irows, VectorInt &icols, VectorDouble &values) const
 
VectorDouble getDiagonal (int shift=0) const
 
VectorDouble getRow (int irow) const
 
VectorDouble getColumn (int icol) const
 
void add (int irow, int icol, double value)
 
void add (const AMatrix &tab, double value=1.)
 
void subtract (const AMatrix &tab, double value=1.)
 
virtual void setColumn (int icol, const VectorDouble &tab)
 
virtual void setRow (int irow, const VectorDouble &tab)
 
virtual void setDiagonal (const VectorDouble &tab)
 
virtual void setDiagonal (double value=1.)
 
const cs * getCs () const
 
Triplet getCsToTriplet (bool flag_from_1=false) const
 
bool isSameSize (const AMatrix &m) const
 
bool isSparse () const
 
bool isEmpty () const
 
virtual bool isSquare (bool printWhyNot=false) const
 
virtual bool isValid (int irow, int icol, bool printWhyNot=false) const
 
virtual bool isIdentity (bool printWhyNot=false) const
 
virtual bool isSymmetric (bool printWhyNot=false) const
 
virtual bool isDiagonal (bool printWhyNot=false) const
 
virtual bool isDiagCst (bool printWhyNot=false) const
 
virtual bool mustBeSymmetric () const
 
virtual bool mustBeDiagonal () const
 
virtual bool mustBeDiagCst () const
 
bool isSame (const AMatrix &m, double eps=EPSILON10)
 
void reset (int nrows, int ncols, bool sparse=false)
 
void reset (int nrows, int ncols, double value, bool sparse=false)
 
void reset (int nrows, int ncols, const double *tab, bool sparse=false)
 
void reset (int nrows, int ncols, const VectorDouble &tab, bool sparse=false, bool flagByRow=true)
 
void reset (const VectorVectorDouble &tab, bool flagByRow=true)
 
double compare (const AMatrix &mat) const
 
void toSparseInPlace ()
 
AMatrixtoSparse () const
 
virtual void transposeInPlace ()
 
virtual AMatrixtranspose () const
 
virtual void addScalar (double v)
 
virtual void addScalarDiag (double v)
 
virtual void prodScalar (double v)
 
void prodVector (const double *inv, double *outv) const
 
void prodVector (const VectorDouble &inv, VectorDouble &outv) const
 
void multiplyRow (const VectorDouble &vec)
 
void multiplyColumn (const VectorDouble &vec)
 
void divideRow (const VectorDouble &vec)
 
void divideColumn (const VectorDouble &vec)
 
virtual void addMatrix (const AMatrix &y)
 
virtual void prodMatrix (const AMatrix &x, const AMatrix &y)
 
virtual void linearCombination (double cx, double cy, const AMatrix &y)
 
int invert ()
 
int solve (const VectorDouble &b, VectorDouble &x) const
 
void dumpElements (const String &title, int ifrom, int ito) const
 
virtual String toString (const AStringFormat *strfmt=nullptr) const override
 
virtual void setValue (int irow, int icol, double value)
 
virtual void setValue (int rank, double value)
 
void setIdentity (double value=1.)
 
void fill (double value)
 
void fillRandom (int seed=432432, double zeroPercent=0.1)
 
void setValuesOldStyle (const double *values, bool byCol=true)
 
void setValues (const VectorDouble &values, bool byCol=true)
 
void setValuesByArrays (const VectorInt &irows, const VectorInt &icols, const VectorDouble &values)
 
double getMeanByColumn (int icol) const
 
double getMinimum () const
 
double getMaximum () const
 
void copyReduce (const AMatrix *x, const VectorInt &validRows, const VectorInt &validCols)
 
double operator() (int row, int col) const
 
double & operator() (int row, int col)
 
- Public Member Functions inherited from AStringable
 AStringable ()
 
 AStringable (const AStringable &r)
 
AStringableoperator= (const AStringable &r)
 
virtual ~AStringable ()
 
virtual void display (const AStringFormat *strfmt=nullptr) const final
 
virtual void display (int level) const final
 
- Public Member Functions inherited from ICloneable
 ICloneable ()
 
virtual ~ICloneable ()
 
virtual ICloneableclone () const =0
 

Protected Member Functions

 AMatrix (int nrow=0, int ncol=0, bool sparse=false)
 
 AMatrix (const cs *A)
 
 AMatrix (const AMatrix &m)
 
AMatrixoperator= (const AMatrix &m)
 
virtual bool _isPhysicallyPresent (int, int) const
 
virtual bool _isCompatible (const AMatrix &m) const =0
 
virtual void _allocate ()=0
 
virtual void _deallocate ()=0
 
virtual int _getMatrixSize () const =0
 
virtual void _setValue (int rank, double value)=0
 
virtual void _setValue (int irow, int icol, double value)=0
 
virtual void _setValues (const double *values, bool byCol)=0
 
virtual double _getValue (int irow, int icol) const =0
 
virtual double _getValue (int rank) const =0
 
virtual double & _getValueRef (int irow, int icol)=0
 
virtual void _transposeInPlace ()=0
 
virtual void _prodVector (const double *inv, double *outv) const =0
 
virtual int _invert ()=0
 
virtual int _solve (const VectorDouble &b, VectorDouble &x) const =0
 
virtual void _clearContents ()
 
void _setNCols (int ncols)
 
void _setNRows (int nrows)
 
bool _isNumbersValid (int nrows, int ncols) const
 
bool _isColumnValid (int icol) const
 
bool _isRowValid (int irow) const
 
bool _isIndexValid (int irow, int icol) const
 
bool _isRowVectorConsistent (const VectorDouble &tab)
 
bool _isColVectorConsistent (const VectorDouble &tab)
 
bool _isVectorSizeConsistent (int nrows, int ncols, const VectorDouble &tab)
 
bool _isRankValid (int rank) const
 
void _clear ()
 

Private Member Functions

void _setSparse (bool sparse)
 
void _initiateSparse ()
 
void _recopySparse (const cs *cs)
 
void _deallocateSparse ()
 
void _forbiddenForSparse (const String &func) const
 

Private Attributes

int _nRows
 
int _nCols
 
bool _sparse
 
cs * _csMatrix
 

Detailed Description

TODO : Transform into template for storing something else from double.

Matrix

Constructor & Destructor Documentation

◆ AMatrix() [1/3]

AMatrix::AMatrix ( int  nrow = 0,
int  ncol = 0,
bool  sparse = false 
)
protected

◆ AMatrix() [2/3]

AMatrix::AMatrix ( const cs *  A)
protected

◆ AMatrix() [3/3]

AMatrix::AMatrix ( const AMatrix m)
protected

◆ ~AMatrix()

AMatrix::~AMatrix ( )
virtual

Member Function Documentation

◆ _allocate()

virtual void AMatrix::_allocate ( )
protectedpure virtual

◆ _clear()

void AMatrix::_clear ( )
protected

◆ _clearContents()

virtual void AMatrix::_clearContents ( )
inlineprotectedvirtual

Reimplemented in Table.

◆ _deallocate()

virtual void AMatrix::_deallocate ( )
protectedpure virtual

◆ _deallocateSparse()

void AMatrix::_deallocateSparse ( )
private

◆ _forbiddenForSparse()

void AMatrix::_forbiddenForSparse ( const String func) const
private

◆ _getMatrixSize()

virtual int AMatrix::_getMatrixSize ( ) const
protectedpure virtual

◆ _getValue() [1/2]

virtual double AMatrix::_getValue ( int  irow,
int  icol 
) const
protectedpure virtual

◆ _getValue() [2/2]

virtual double AMatrix::_getValue ( int  rank) const
protectedpure virtual

◆ _getValueRef()

virtual double& AMatrix::_getValueRef ( int  irow,
int  icol 
)
protectedpure virtual

◆ _initiateSparse()

void AMatrix::_initiateSparse ( )
private

This strange function instantiate a sparse matrix with given dimensions filled with zeroes. It should be an empty matrix... But this does not make sense. Therefore it is created by setting a single element at the lower bottom size of the matrix ... filled with a zero.

◆ _invert()

virtual int AMatrix::_invert ( )
protectedpure virtual

◆ _isColumnValid()

bool AMatrix::_isColumnValid ( int  icol) const
protected

◆ _isColVectorConsistent()

bool AMatrix::_isColVectorConsistent ( const VectorDouble tab)
protected

◆ _isCompatible()

virtual bool AMatrix::_isCompatible ( const AMatrix m) const
protectedpure virtual

◆ _isIndexValid()

bool AMatrix::_isIndexValid ( int  irow,
int  icol 
) const
protected

◆ _isNumbersValid()

bool AMatrix::_isNumbersValid ( int  nrows,
int  ncols 
) const
protected

◆ _isPhysicallyPresent()

virtual bool AMatrix::_isPhysicallyPresent ( int  ,
int   
) const
inlineprotectedvirtual

Say if (irow, icol) is stored physically or not

Reimplemented in MatrixSquareDiagonal, MatrixSquareDiagonalCst, and MatrixSquareSymmetric.

◆ _isRankValid()

bool AMatrix::_isRankValid ( int  rank) const
protected

◆ _isRowValid()

bool AMatrix::_isRowValid ( int  irow) const
protected

◆ _isRowVectorConsistent()

bool AMatrix::_isRowVectorConsistent ( const VectorDouble tab)
protected

◆ _isVectorSizeConsistent()

bool AMatrix::_isVectorSizeConsistent ( int  nrows,
int  ncols,
const VectorDouble tab 
)
protected

◆ _prodVector()

virtual void AMatrix::_prodVector ( const double *  inv,
double *  outv 
) const
protectedpure virtual

◆ _recopySparse()

void AMatrix::_recopySparse ( const cs *  cs)
private

◆ _setNCols()

void AMatrix::_setNCols ( int  ncols)
inlineprotected

◆ _setNRows()

void AMatrix::_setNRows ( int  nrows)
inlineprotected

◆ _setSparse()

void AMatrix::_setSparse ( bool  sparse)
inlineprivate

◆ _setValue() [1/2]

virtual void AMatrix::_setValue ( int  rank,
double  value 
)
protectedpure virtual

◆ _setValue() [2/2]

virtual void AMatrix::_setValue ( int  irow,
int  icol,
double  value 
)
protectedpure virtual

◆ _setValues()

virtual void AMatrix::_setValues ( const double *  values,
bool  byCol 
)
protectedpure virtual

◆ _solve()

virtual int AMatrix::_solve ( const VectorDouble b,
VectorDouble x 
) const
protectedpure virtual

◆ _transposeInPlace()

virtual void AMatrix::_transposeInPlace ( )
protectedpure virtual

◆ add() [1/2]

void AMatrix::add ( int  irow,
int  icol,
double  value 
)

Add a value to a matrix term

◆ add() [2/2]

void AMatrix::add ( const AMatrix tab,
double  value = 1. 
)

Add a matrix to this

Perform: this += 'value' * 'tab'

Parameters
tabCurrent matrix
valueMultiplicative coefficient (default = 1)

◆ addMatrix()

void AMatrix::addMatrix ( const AMatrix y)
virtual

Add a matrix to this component by component

Add the matrix 'y' to the current Matrix

Parameters
yMatrix to be added

◆ addScalar()

void AMatrix::addScalar ( double  v)
virtual

Add a value to each matrix component

Parameters
vAdd a scalar value to all terms of the current matrix

Reimplemented in MatrixSquareDiagonal, and MatrixSquareDiagonalCst.

◆ addScalarDiag()

void AMatrix::addScalarDiag ( double  v)
virtual

Add value to matrix diagonal

Parameters
vAdd constant value to the diagonal of the current Matrix

Reimplemented in MatrixSquareDiagonalCst.

◆ compare()

double AMatrix::compare ( const AMatrix mat) const

Returns the sum of absolute difference between argument and this

◆ copyReduce()

void AMatrix::copyReduce ( const AMatrix x,
const VectorInt validRows,
const VectorInt validCols 
)

◆ divideColumn()

void AMatrix::divideColumn ( const VectorDouble vec)

Divide a Matrix column-wise

◆ divideRow()

void AMatrix::divideRow ( const VectorDouble vec)

Divide a Matrix row-wise

◆ dumpElements()

void AMatrix::dumpElements ( const String title,
int  ifrom,
int  ito 
) const

Dump a specific range of samples from the internal storage

◆ fill()

void AMatrix::fill ( double  value)

Set all the values of the Matrix at once

Fill 'this' with the constant 'value'

Parameters
valueConstant value used for filling 'this'

◆ fillRandom()

void AMatrix::fillRandom ( int  seed = 432432,
double  zeroPercent = 0.1 
)

◆ getColumn()

VectorDouble AMatrix::getColumn ( int  icol) const

Extract a Column

◆ getCs()

const cs* AMatrix::getCs ( ) const
inline

Returns a pointer to the Sparse storage

◆ getCsToTriplet()

Triplet AMatrix::getCsToTriplet ( bool  flag_from_1 = false) const

◆ getDiagonal()

VectorDouble AMatrix::getDiagonal ( int  shift = 0) const

Extract a Diagonal (main or secondary) of this

◆ getMaximum()

double AMatrix::getMaximum ( ) const

◆ getMeanByColumn()

double AMatrix::getMeanByColumn ( int  icol) const

◆ getMinimum()

double AMatrix::getMinimum ( ) const

◆ getNCols()

int AMatrix::getNCols ( ) const
inline

Returns the number of columns

◆ getNRows()

int AMatrix::getNRows ( ) const
inline

Returns the number of rows

◆ getNTotal()

int AMatrix::getNTotal ( ) const
inline

Get the total number of elements of the (full) matrix

◆ getRow()

VectorDouble AMatrix::getRow ( int  irow) const

Extract a Row

◆ getValue() [1/2]

double AMatrix::getValue ( int  irow,
int  icol 
) const
virtual

Gets the value at row 'irow' and column 'icol'

◆ getValue() [2/2]

double AMatrix::getValue ( int  rank) const
virtual

Gets the value at rank 'rank'

◆ getValueRef()

double & AMatrix::getValueRef ( int  irow,
int  icol 
)
virtual

Gets a reference to the value at row 'irow' and column 'icol'

◆ getValues()

VectorDouble AMatrix::getValues ( ) const

Returns the contents of the whole matrix as a VectorDouble

◆ getValuesAsTriplets()

void AMatrix::getValuesAsTriplets ( VectorInt irows,
VectorInt icols,
VectorDouble values 
) const

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

Parameters
irowsOutput array of row indices
icolsOutput array of column indices
valuesOutput array of non-zero values

TODO : use cs_sparce corresponding function

◆ init()

void AMatrix::init ( int  nrows,
int  ncols,
bool  sparse = false 
)

◆ invert()

int AMatrix::invert ( )

Matrix inversion in place

◆ isDiagCst()

bool AMatrix::isDiagCst ( bool  printWhyNot = false) const
virtual

Check if the contents of the matrix is constant and diagonal

◆ isDiagonal()

bool AMatrix::isDiagonal ( bool  printWhyNot = false) const
virtual

Check if the matrix is (non empty) diagonal

Reimplemented in MatrixSquareDiagonal.

◆ isEmpty()

bool AMatrix::isEmpty ( ) const
inline

Returns if the current matrix is Empty

◆ isIdentity()

bool AMatrix::isIdentity ( bool  printWhyNot = false) const
virtual

Check if the matrix is square and Identity

◆ isSame()

bool AMatrix::isSame ( const AMatrix m,
double  eps = EPSILON10 
)

Check if a matrix is the same as me (norm L1)

◆ isSameSize()

bool AMatrix::isSameSize ( const AMatrix m) const

Check that both matrix have the same number of rows and columns

Check that Matrix 'm' share the same dimensions as current one

Parameters
mMatrix to be compared to the current Matrix
Returns
true if 'm' has same dimensions as the current Matrix

◆ isSparse()

bool AMatrix::isSparse ( ) const
inline

Returns if the current matrix is Sparse

◆ isSquare()

bool AMatrix::isSquare ( bool  printWhyNot = false) const
virtual

Check if the matrix is (non empty) square

Reimplemented in MatrixSquareGeneral.

◆ isSymmetric()

bool AMatrix::isSymmetric ( bool  printWhyNot = false) const
virtual

Check if the input matrix is (non empty and square) symmetric

Reimplemented in MatrixSquareSymmetric.

◆ isValid()

bool AMatrix::isValid ( int  irow,
int  icol,
bool  printWhyNot = false 
) const
virtual

Indicate if the given indices are valid for the current matrix size

Indicate if the given indices are valid for the current matrix size

Parameters
irowRow index
icolColumn index
printWhyNotPrint the message is the answer if false
Returns
true if indices are valid for the current matrix size

Reimplemented in MatrixSquareDiagonalCst, and MatrixSquareDiagonal.

◆ linearCombination()

void AMatrix::linearCombination ( double  cx,
double  cy,
const AMatrix y 
)
virtual

Linear combination of matrices

Updates the current Matrix as a linear combination of matrices as follows: this <- cx * this + cy * y

Parameters
cxCoefficient applied to the current Matrix
cyCoefficient applied to the Matrix 'y'
ySecond Matrix in the Linear combination

◆ multiplyColumn()

void AMatrix::multiplyColumn ( const VectorDouble vec)

Multiply a Matrix column-wise

◆ multiplyRow()

void AMatrix::multiplyRow ( const VectorDouble vec)

Multiply a Matrix row-wise

◆ mustBeDiagCst()

virtual bool AMatrix::mustBeDiagCst ( ) const
inlinevirtual

Say if the matrix must be diagonal constant

Reimplemented in MatrixSquareDiagonal, MatrixSquareSymmetric, MatrixSquareGeneral, MatrixSquareDiagonalCst, and MatrixRectangular.

◆ mustBeDiagonal()

virtual bool AMatrix::mustBeDiagonal ( ) const
inlinevirtual

Say if the matrix must be diagonal

Reimplemented in MatrixSquareDiagonal, MatrixSquareSymmetric, MatrixSquareGeneral, and MatrixRectangular.

◆ mustBeSymmetric()

virtual bool AMatrix::mustBeSymmetric ( ) const
inlinevirtual

Say if the matrix must be symmetric

Reimplemented in MatrixSquareSymmetric, MatrixSquareGeneral, and MatrixRectangular.

◆ operator()() [1/2]

double AMatrix::operator() ( int  row,
int  col 
) const
inline

Get value operator override

◆ operator()() [2/2]

double& AMatrix::operator() ( int  row,
int  col 
)
inline

Set value operator override

◆ operator=()

AMatrix & AMatrix::operator= ( const AMatrix m)
protected

◆ prodMatrix()

void AMatrix::prodMatrix ( const AMatrix x,
const AMatrix y 
)
virtual

Multiply a matrix by another and store the result in the current matrix

Store the product of 'x' by 'y' in this

Parameters
xFirst Matrix
ySecond matrix

◆ prodScalar()

void AMatrix::prodScalar ( double  v)
virtual

Multiply each matrix component by a value

Parameters
vMultiply all the terms of the matrix by the scalar 'v'

◆ prodVector() [1/2]

void AMatrix::prodVector ( const double *  inv,
double *  outv 
) const

Product of the Matrix by a vector (on its right)

Parameters
invInput vector
outvOutput vector obtained by multiplying 'inv' by current Matrix

◆ prodVector() [2/2]

void AMatrix::prodVector ( const VectorDouble inv,
VectorDouble outv 
) const

◆ reset() [1/5]

void AMatrix::reset ( int  nrows,
int  ncols,
bool  sparse = false 
)

◆ reset() [2/5]

void AMatrix::reset ( int  nrows,
int  ncols,
double  value,
bool  sparse = false 
)

◆ reset() [3/5]

void AMatrix::reset ( int  nrows,
int  ncols,
const double *  tab,
bool  sparse = false 
)

◆ reset() [4/5]

void AMatrix::reset ( int  nrows,
int  ncols,
const VectorDouble tab,
bool  sparse = false,
bool  flagByRow = true 
)

◆ reset() [5/5]

void AMatrix::reset ( const VectorVectorDouble tab,
bool  flagByRow = true 
)

◆ setColumn()

void AMatrix::setColumn ( int  icol,
const VectorDouble tab 
)
virtual

Set the contents of a Column

Reimplemented in MatrixSquareDiagonalCst, and MatrixSquareDiagonal.

◆ setDiagonal() [1/2]

void AMatrix::setDiagonal ( const VectorDouble tab)
virtual

Set the contents of the (main) Diagonal

Reimplemented in MatrixSquareDiagonalCst.

◆ setDiagonal() [2/2]

void AMatrix::setDiagonal ( double  value = 1.)
virtual

◆ setIdentity()

void AMatrix::setIdentity ( double  value = 1.)

Sets the matrix as Identity

◆ setRow()

void AMatrix::setRow ( int  irow,
const VectorDouble tab 
)
virtual

Set the contents of a Row

Reimplemented in MatrixSquareDiagonalCst, and MatrixSquareDiagonal.

◆ setValue() [1/2]

void AMatrix::setValue ( int  irow,
int  icol,
double  value 
)
virtual

Sets the value at row 'irow' and column 'icol'

◆ setValue() [2/2]

void AMatrix::setValue ( int  rank,
double  value 
)
virtual

Sets the value at rank 'rank'

◆ setValues()

void AMatrix::setValues ( const VectorDouble 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

Parameters
values
byColtrue for Column major; false for Row Major

◆ setValuesByArrays()

void AMatrix::setValuesByArrays ( const VectorInt irows,
const VectorInt icols,
const VectorDouble values 
)

◆ setValuesOldStyle()

void AMatrix::setValuesOldStyle ( const double *  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 double* array

Parameters
valuesInput array (Dimension: nrow * ncol)
byColtrue for Column major; false for Row Major

◆ solve()

int AMatrix::solve ( const VectorDouble b,
VectorDouble x 
) const

Solving the Matrix Linear system

◆ subtract()

void AMatrix::subtract ( const AMatrix tab,
double  value = 1. 
)

Subtract a matrix to this

◆ toSparse()

AMatrix * AMatrix::toSparse ( ) const

Transform any matrix in a Sparse format

◆ toSparseInPlace()

void AMatrix::toSparseInPlace ( )

Transform the current matrix (any format) in a Sparse format in place

◆ toString()

String AMatrix::toString ( const AStringFormat strfmt = nullptr) const
overridevirtual

Conversion to a string

Reimplemented from AStringable.

Reimplemented in MatrixSquareSymmetric, Table, MatrixSquareDiagonal, and MatrixSquareDiagonalCst.

◆ transpose()

AMatrix * AMatrix::transpose ( ) const
virtual

Transpose the matrix and return it as a copy

◆ transposeInPlace()

void AMatrix::transposeInPlace ( )
virtual

Transpose the matrix in place

Reimplemented in MatrixSquareDiagonalCst, and MatrixSquareDiagonal.

Member Data Documentation

◆ _csMatrix

cs* AMatrix::_csMatrix
private

◆ _nCols

int AMatrix::_nCols
private

◆ _nRows

int AMatrix::_nRows
private

◆ _sparse

bool AMatrix::_sparse
private

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