chrono::ChSparseMatrix Class Referenceabstract

Description

Base class for all sparse matrices.

#include <ChSparseMatrix.h>

Inheritance diagram for chrono::ChSparseMatrix:

Public Types

enum  SymmetryType { GENERAL, SYMMETRIC_POSDEF, SYMMETRIC_INDEF, STRUCTURAL_SYMMETRIC }
 Symmetry type of the matrix. More...
 

Public Member Functions

 ChSparseMatrix (int nrows=0, int ncols=0, int nnz=0)
 Construct a sparse matrix with nrows and ncols and with nnz non-zero elements. More...
 
 ChSparseMatrix (const ChSparseMatrix &other)
 
int GetNumRows () const
 Get the number of rows of this matrix.
 
int GetNumColumns () const
 Get the number of columns of this matrix.
 
virtual int GetNNZ () const
 Get the number of non-zero elements stored in this matrix.
 
void SetType (SymmetryType type)
 Set the symmetry type for this sparse matrix (default: GENERAL). More...
 
SymmetryType GetType () const
 Return the symmetry type of this matrix.
 
void SetSparsityPatternLock (bool val)
 Enable/disable a lock on the matrix sparsity pattern (default: false).
 
virtual void LoadSparsityPattern (ChSparsityPatternLearner &sparsity_learner)
 (Optional) Force the update of the sparsity pattern More...
 
virtual void SetElement (int insrow, int inscol, double insval, bool overwrite=true)=0
 Set the value of the element with index (insrow, inscol) to insval. More...
 
virtual double GetElement (int row, int col) const =0
 Returns the value of the element with index (row, col). More...
 
virtual void Reset (int row, int col, int nonzeros=0)=0
 
virtual bool Resize (int nrows, int ncols, int nonzeros=0)=0
 
virtual bool Compress ()
 Optional compression method, typically invoked after all elements have been inserted. More...
 
virtual void PasteMatrix (const ChMatrix<> &matra, int insrow, int inscol, bool overwrite=true, bool transp=false)
 Paste a given matrix into this sparse matrix at position (insrow, inscol). More...
 
virtual void PasteClippedMatrix (const ChMatrix<> &matra, int cliprow, int clipcol, int nrows, int ncolumns, int insrow, int inscol, bool overwrite=true)
 Paste a clipped portion of the given matrix into this sparse matrix at position (insrow, inscol). More...
 
virtual int * GetCS_LeadingIndexArray () const
 Return the row|column index array in the CSR|CSC representation of this matrix.
 
virtual int * GetCS_TrailingIndexArray () const
 Return the column|row index array in the CSR|CSC representation of this matrix.
 
virtual double * GetCS_ValueArray () const
 Return the array of matrix values in the CSR|CSC representation of this matrix.
 
virtual void PasteTranspMatrix (const ChMatrix<> &matra, int insrow, int inscol)
 Same as PasteMatrix(), but with overwrite set to true and transp set to true. More...
 
virtual void PasteSumMatrix (const ChMatrix<> &matra, int insrow, int inscol)
 Same as PasteMatrix(), but with overwrite set to false and transp set to false. More...
 
virtual void PasteSumTranspMatrix (const ChMatrix<> &matra, int insrow, int inscol)
 Same as PasteMatrix(), but with overwrite set to false and transp set to true. More...
 
virtual void PasteSumClippedMatrix (const ChMatrix<> &matra, int cliprow, int clipcol, int nrows, int ncolumns, int insrow, int inscol)
 Same as PasteClippedMatrix(), but with overwrite set to false. More...
 

Protected Attributes

int m_num_rows
 number of rows
 
int m_num_cols
 number of columns
 
int m_nnz
 number of non-zero elements
 
SymmetryType m_type = GENERAL
 matrix type
 
bool m_lock = false
 indicate whether or not the matrix sparsity pattern should be locked
 
bool m_update_sparsity_pattern = false
 let the matrix acquire the sparsity pattern
 

Member Enumeration Documentation

Symmetry type of the matrix.

Enumerator
GENERAL 

unsymmetric matrix

SYMMETRIC_POSDEF 

symmetric positive definite

SYMMETRIC_INDEF 

symmetric indefinite

STRUCTURAL_SYMMETRIC 

structurally symmetric

Constructor & Destructor Documentation

chrono::ChSparseMatrix::ChSparseMatrix ( int  nrows = 0,
int  ncols = 0,
int  nnz = 0 
)

Construct a sparse matrix with nrows and ncols and with nnz non-zero elements.

By default, the matrix type is GENERAL (i.e., unsymmetric) and the sparsity pattern is unlocked.

Member Function Documentation

virtual bool chrono::ChSparseMatrix::Compress ( )
virtual

Optional compression method, typically invoked after all elements have been inserted.

Depending on the internal data structures, a derived class may perform additional operations for improved space or speed performance. A typical implementation should respect the sparsity pattern lock status. This function should return true if it makes any modifications.

Reimplemented in chrono::ChCSMatrix.

virtual double chrono::ChSparseMatrix::GetElement ( int  row,
int  col 
) const
pure virtual

Returns the value of the element with index (row, col).

Returns zero if an element is not stored.

Implemented in chrono::ChCSMatrix, chrono::ChLinkedListMatrix, chrono::ChMapMatrix, and chrono::ChSparsityPatternLearner.

virtual void chrono::ChSparseMatrix::LoadSparsityPattern ( ChSparsityPatternLearner sparsity_learner)
virtual

(Optional) Force the update of the sparsity pattern

Depending on the internal data structure, this can highly speed up the insertion of elements in the matrix. Suggested for matrix with dimension >1e5

Reimplemented in chrono::ChCSMatrix.

virtual void chrono::ChSparseMatrix::PasteClippedMatrix ( const ChMatrix<> &  matra,
int  cliprow,
int  clipcol,
int  nrows,
int  ncolumns,
int  insrow,
int  inscol,
bool  overwrite = true 
)
virtual

Paste a clipped portion of the given matrix into this sparse matrix at position (insrow, inscol).

So the clipped portion matra[cliprow : cliprow + nrows][[clipcol : clipcol + ncolumns]] will be copied into this[insrow : insrow + nrows][[inscol : inscol + ncolumns]]

Parameters
[in]matraThe source matrix that will be copied;
[in]cliprowThe row index of the first element of source matrix that will be copied;
[in]clipcolThe column index of the first element of source matrix that will be copied;
[in]nrowsThe number of rows that will be copied;
[in]ncolumnsThe number of columns that will be copied;
[in]insrowThe row index where the first element will be copied;
[in]inscolThe column index where the first element will be copied;
[in]overwriteTells if the copied element will overwrite an existing element or be summed to it.

Reimplemented in chrono::ChLinkedListMatrix.

virtual void chrono::ChSparseMatrix::PasteMatrix ( const ChMatrix<> &  matra,
int  insrow,
int  inscol,
bool  overwrite = true,
bool  transp = false 
)
virtual

Paste a given matrix into this sparse matrix at position (insrow, inscol).

The matrix matra will be copied into this[insrow : insrow + matra.GetRows()][[inscol : inscol + matra.GetColumns()]]

Parameters
[in]matraThe source matrix that will be copied;
[in]insrowThe row index where the first element will be copied;
[in]inscolThe column index where the first element will be copied;
[in]overwriteTells if the copied element will overwrite an existing element or be summed to it;
[in]transpTells if the matra matrix should be copied transposed.

Reimplemented in chrono::ChLinkedListMatrix.

virtual void chrono::ChSparseMatrix::PasteSumClippedMatrix ( const ChMatrix<> &  matra,
int  cliprow,
int  clipcol,
int  nrows,
int  ncolumns,
int  insrow,
int  inscol 
)
virtual

Same as PasteClippedMatrix(), but with overwrite set to false.

The clipped portion of the source matrix will be summed to this matrix.

Reimplemented in chrono::ChLinkedListMatrix.

virtual void chrono::ChSparseMatrix::PasteSumMatrix ( const ChMatrix<> &  matra,
int  insrow,
int  inscol 
)
virtual

Same as PasteMatrix(), but with overwrite set to false and transp set to false.

The source matrix will be summed to the current matrix and not transposed.

Reimplemented in chrono::ChLinkedListMatrix.

virtual void chrono::ChSparseMatrix::PasteSumTranspMatrix ( const ChMatrix<> &  matra,
int  insrow,
int  inscol 
)
virtual

Same as PasteMatrix(), but with overwrite set to false and transp set to true.

The source matrix will be transposed and summed to the this matrix.

Reimplemented in chrono::ChLinkedListMatrix.

virtual void chrono::ChSparseMatrix::PasteTranspMatrix ( const ChMatrix<> &  matra,
int  insrow,
int  inscol 
)
virtual

Same as PasteMatrix(), but with overwrite set to true and transp set to true.

The source matrix will be transposed and pasted into this matrix, overwriting existing elements.

Reimplemented in chrono::ChLinkedListMatrix.

virtual void chrono::ChSparseMatrix::SetElement ( int  insrow,
int  inscol,
double  insval,
bool  overwrite = true 
)
pure virtual

Set the value of the element with index (insrow, inscol) to insval.

Parameters
[in]insrowrow index of the element;
[in]inscolcolumn index of the element;
[in]insvalvalue of the element;
[in]overwritetells if the new element should overwrite an existing element or be summed to it.

Implemented in chrono::ChCSMatrix, chrono::ChLinkedListMatrix, chrono::ChMapMatrix, and chrono::ChSparsityPatternLearner.

void chrono::ChSparseMatrix::SetType ( SymmetryType  type)

Set the symmetry type for this sparse matrix (default: GENERAL).

A derived class should always support GENERAL (i.e. unsymmetric matrices), but is free to perform optimizations for symmetric or structurally symmetric matrices.