gstlearn  1.0.0
CCC
MatrixSparse Class Reference

#include <MatrixSparse.hpp>

Inheritance diagram for MatrixSparse:
AMatrix AStringable ICloneable

Public Member Functions

 MatrixSparse (int nrow=0, int ncol=0)
 
 MatrixSparse (const cs *A)
 
 MatrixSparse (const MatrixSparse &m)
 
MatrixSparseoperator= (const MatrixSparse &m)
 
virtual ~MatrixSparse ()
 
void init (int nrows, int ncols)
 Cloneable interface. More...
 
void getValuesAsTriplets (VectorInt &irows, VectorInt &icols, VectorDouble &values) const
 
const cs * getCs () const
 
Triplet getCsToTriplet (bool flag_from_1=false) const
 
bool isSparse () const
 
void reset (int nrows, int ncols)
 
void reset (int nrows, int ncols, double value)
 
void reset (int nrows, int ncols, const double *tab, bool byCol=true)
 
void reset (int nrows, int ncols, const VectorDouble &tab, bool byCol=true)
 
void reset (const VectorVectorDouble &tab, bool byCol=true)
 
virtual MatrixSparsetranspose () const override
 
virtual void addScalar (double v) override
 
virtual void addScalarDiag (double v) override
 
virtual void prodScalar (double v) override
 
void addMatrix (const MatrixSparse &y)
 
void prodMatrix (const MatrixSparse &x, const MatrixSparse &y)
 
void linearCombination (double cx, double cy, const MatrixSparse &y)
 
void dumpElements (const String &title, int ifrom, int ito) const
 
virtual String toString (const AStringFormat *strfmt=nullptr) const override
 
void fill (double value)
 
void fillRandom (int seed=432432, double zeroPercent=0.1)
 
void setValuesByArrays (const VectorInt &irows, const VectorInt &icols, const VectorDouble &values) override
 
- Public Member Functions inherited from AMatrix
virtual ~AMatrix ()
 
void init (int nrows, int ncols)
 
void reset (int nrows, int ncols, double value=0.)
 
void resetFromArray (int nrows, int ncols, const double *tab, bool byCol=true)
 
void resetFromVD (int nrows, int ncols, const VectorDouble &tab, bool byCol=true)
 
void resetFromVVD (const VectorVectorDouble &tab, bool byCol=true)
 
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.)
 
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
 
virtual void transposeInPlace ()
 
virtual double getValue (int irow, int icol) const
 
virtual double & getValueRef (int irow, int icol)
 
virtual void setValue (int irow, int icol, double value)
 
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)
 
bool isSame (const AMatrix &m, double eps=EPSILON10)
 
bool isSameSize (const AMatrix &m) const
 
bool isEmpty () const
 
double compare (const AMatrix &mat) const
 
int getNRows () const
 
int getNCols () const
 
int getNTotal () const
 
VectorDouble getValues (bool byCol=true) const
 
VectorDouble getDiagonal (int shift=0) const
 
VectorDouble getRow (int irow) const
 
VectorDouble getColumn (int icol) const
 
bool isColumnDefined (int icol) const
 
bool isRowDefined (int irow) const
 
int getNumberColumnDefined () const
 
int getNumberRowDefined () 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.)
 
void getValuesAsTriplets (VectorInt &irows, VectorInt &icols, VectorDouble &values) const
 
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)
 
double quadraticMatrix (const VectorDouble &x, const VectorDouble &y)
 
int invert ()
 
int solve (const VectorDouble &b, VectorDouble &x) const
 
void dumpElements (const String &title, int ifrom, int ito) const
 
void setIdentity (double value=1.)
 
void fill (double value)
 
void fillRandom (int seed=432432, double zeroPercent=0.1)
 
void setValues (const VectorDouble &values, bool byCol=true)
 
double getMeanByColumn (int icol) const
 
double getMinimum () const
 
double getMaximum () const
 
void copyReduce (const AMatrix *x, const VectorInt &activeRows, const VectorInt &activeCols)
 
void setFlagCheckAddress (bool flagCheckAddress)
 
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
 

Public Attributes

 DECLARE_TOTL
 Has a specific implementation in the Target language. More...
 

Protected Member Functions

virtual double & _getValueRef (int irow, int icol) override
 
bool _isPhysicallyPresent (int irow, int icol) const
 
bool _isCompatible (const AMatrix &m) const override
 
void _allocate () override
 
void _deallocate () override
 
virtual int _getMatrixSize () const override
 
virtual void _setValue (int rank, double value) override
 
virtual void _setValue (int irow, int icol, double value) override
 
virtual void _setValues (const double *values, bool byCol) override
 
virtual double _getValue (int rank) const override
 
virtual double _getValue (int irow, int icol) const override
 
virtual void _transposeInPlace () override
 
virtual void _prodVector (const double *inv, double *outv) const override
 
virtual int _invert () override
 
virtual int _solve (const VectorDouble &b, VectorDouble &x) const override
 
virtual void _clearContents ()
 
virtual int _getIndexToRank (int irow, int icol) const override
 
void _clear ()
 
- Protected Member Functions inherited from AMatrix
 AMatrix (int nrow=0, int ncol=0)
 
 AMatrix (const AMatrix &m)
 
AMatrixoperator= (const AMatrix &m)
 
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 ()
 
void _fillFromVVD (const VectorVectorDouble &X)
 
bool _getFlagCheckAddress () const
 

Private Member Functions

void _initiateSparse ()
 
void _recopySparse (const cs *cs)
 
void _forbiddenForSparse (const String &func) const
 

Private Attributes

cs * _csMatrix
 

Detailed Description

Sparse Matrix

Constructor & Destructor Documentation

◆ MatrixSparse() [1/3]

MatrixSparse::MatrixSparse ( int  nrow = 0,
int  ncol = 0 
)

◆ MatrixSparse() [2/3]

MatrixSparse::MatrixSparse ( const cs *  A)

◆ MatrixSparse() [3/3]

MatrixSparse::MatrixSparse ( const MatrixSparse m)

◆ ~MatrixSparse()

MatrixSparse::~MatrixSparse ( )
virtual

Member Function Documentation

◆ _allocate()

void MatrixSparse::_allocate ( )
overrideprotectedvirtual

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.

Implements AMatrix.

◆ _clear()

void MatrixSparse::_clear ( )
protected

◆ _clearContents()

virtual void MatrixSparse::_clearContents ( )
inlineprotectedvirtual

Reimplemented from AMatrix.

◆ _deallocate()

void MatrixSparse::_deallocate ( )
overrideprotectedvirtual

Implements AMatrix.

◆ _forbiddenForSparse()

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

◆ _getIndexToRank()

int MatrixSparse::_getIndexToRank ( int  irow,
int  icol 
) const
overrideprotectedvirtual

Implements AMatrix.

◆ _getMatrixSize()

int MatrixSparse::_getMatrixSize ( ) const
overrideprotectedvirtual

Returns the number of elements actually stored as members in subsequent classes

Implements AMatrix.

◆ _getValue() [1/2]

double MatrixSparse::_getValue ( int  irow,
int  icol 
) const
overrideprotectedvirtual

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

Implements AMatrix.

◆ _getValue() [2/2]

double MatrixSparse::_getValue ( int  rank) const
overrideprotectedvirtual

Gets the value for rank 'rank'

Implements AMatrix.

◆ _getValueRef()

double & MatrixSparse::_getValueRef ( int  irow,
int  icol 
)
overrideprotectedvirtual

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

Implements AMatrix.

◆ _initiateSparse()

void MatrixSparse::_initiateSparse ( )
private

◆ _invert()

int MatrixSparse::_invert ( )
overrideprotectedvirtual

Implements AMatrix.

◆ _isCompatible()

bool MatrixSparse::_isCompatible ( const AMatrix m) const
inlineoverrideprotectedvirtual

Implements AMatrix.

◆ _isPhysicallyPresent()

bool MatrixSparse::_isPhysicallyPresent ( int  ,
int   
) const
inlineprotectedvirtual

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

Reimplemented from AMatrix.

◆ _prodVector()

void MatrixSparse::_prodVector ( const double *  inv,
double *  outv 
) const
overrideprotectedvirtual
Parameters
invInput vector
outvOutput vector obtained by multiplying 'inv' by current Matrix

Implements AMatrix.

◆ _recopySparse()

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

◆ _setValue() [1/2]

void MatrixSparse::_setValue ( int  irow,
int  icol,
double  value 
)
overrideprotectedvirtual

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

Implements AMatrix.

◆ _setValue() [2/2]

void MatrixSparse::_setValue ( int  rank,
double  value 
)
overrideprotectedvirtual

Implements AMatrix.

◆ _setValues()

void MatrixSparse::_setValues ( const double *  values,
bool  byCol 
)
overrideprotectedvirtual

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

Reimplemented from AMatrix.

◆ _solve()

int MatrixSparse::_solve ( const VectorDouble b,
VectorDouble x 
) const
overrideprotectedvirtual

Implements AMatrix.

◆ _transposeInPlace()

void MatrixSparse::_transposeInPlace ( )
overrideprotectedvirtual

Implements AMatrix.

◆ addMatrix()

void MatrixSparse::addMatrix ( const MatrixSparse y)

Add a matrix to this component by component

Add the matrix 'y' to the current Matrix

Parameters
yMatrix to be added

◆ addScalar()

void MatrixSparse::addScalar ( double  v)
overridevirtual

Add a value to each matrix component

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

Reimplemented from AMatrix.

◆ addScalarDiag()

void MatrixSparse::addScalarDiag ( double  v)
overridevirtual

Add value to matrix diagonal

Parameters
vAdd constant value to the diagonal of the current Matrix

Reimplemented from AMatrix.

◆ dumpElements()

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

Dump a specific range of samples from the internal storage

◆ fill()

void MatrixSparse::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 MatrixSparse::fillRandom ( int  seed = 432432,
double  zeroPercent = 0.1 
)

◆ getCs()

const cs* MatrixSparse::getCs ( ) const
inline

Returns a pointer to the Sparse storage

◆ getCsToTriplet()

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

◆ getValuesAsTriplets()

void MatrixSparse::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 MatrixSparse::init ( int  nrows,
int  ncols 
)

Cloneable interface.

◆ isSparse()

bool MatrixSparse::isSparse ( ) const
inlinevirtual

Returns if the current matrix is Sparse

Reimplemented from AMatrix.

◆ linearCombination()

void MatrixSparse::linearCombination ( double  cx,
double  cy,
const MatrixSparse y 
)

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

◆ operator=()

MatrixSparse & MatrixSparse::operator= ( const MatrixSparse m)

◆ prodMatrix()

void MatrixSparse::prodMatrix ( const MatrixSparse x,
const MatrixSparse y 
)

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 MatrixSparse::prodScalar ( double  v)
overridevirtual

Multiply each matrix component by a value

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

Reimplemented from AMatrix.

◆ reset() [1/5]

void MatrixSparse::reset ( const VectorVectorDouble tab,
bool  byCol = true 
)

◆ reset() [2/5]

void MatrixSparse::reset ( int  nrows,
int  ncols 
)

◆ reset() [3/5]

void MatrixSparse::reset ( int  nrows,
int  ncols,
const double *  tab,
bool  byCol = true 
)

◆ reset() [4/5]

void MatrixSparse::reset ( int  nrows,
int  ncols,
const VectorDouble tab,
bool  byCol = true 
)

◆ reset() [5/5]

void MatrixSparse::reset ( int  nrows,
int  ncols,
double  value 
)

◆ setValuesByArrays()

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

Set a set of values simultaneously from an input array

Reimplemented from AMatrix.

◆ toString()

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

Conversion to a string

Reimplemented from AMatrix.

◆ transpose()

MatrixSparse * MatrixSparse::transpose ( ) const
overridevirtual

Transpose the matrix and return it as a copy

Reimplemented from AMatrix.

Member Data Documentation

◆ _csMatrix

cs* MatrixSparse::_csMatrix
private

◆ DECLARE_TOTL

MatrixSparse::DECLARE_TOTL

Has a specific implementation in the Target language.


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