chrono::ChLinkedListMatrix Class Reference

Description

This class defines a sparse matrix, implemented using linked lists of non-zero elements in each row.

#include <ChLinkedListMatrix.h>

Inheritance diagram for chrono::ChLinkedListMatrix:
Collaboration diagram for chrono::ChLinkedListMatrix:

Public Member Functions

 ChLinkedListMatrix ()
 Default constructor.
 
 ChLinkedListMatrix (int nrows, int ncols, double fill_in=SPM_DEF_FULLNESS)
 Create a sparse matrix with given dimensions and fill-in.
 
 ChLinkedListMatrix (const ChMatrix<> &mat)
 Create a sparse matrix from a given dense matrix.
 
 ChLinkedListMatrix (const ChLinkedListMatrix &other)
 Copy constructor.
 
 ~ChLinkedListMatrix ()
 Destructor.
 
void CopyToMatrix (ChMatrix<> &mat)
 Copy this sparse matrix to a dense matrix.
 
ChMelementSetElement (int row, int col, double val, ChMelement *guess)
 Optimized SetElement, returning the fetched Melement*.
 
ChMelementGetElement (int row, int col, double *val, ChMelement *guess)
 Optimized GetElement, returning the fetched Melement*.
 
void MoreBuffer (double inflate)
 Add another buffer in buffer list, bigger of last (max total buffer size = col x rows) as in newbuffer = lastbuffer * inflate (use inflate >1, suggested 2) [mostly used internally].
 
virtual bool Resize (int nrows, int ncols, int nonzeros=0) override
 Overloadings of standard matrix functions.
 
void Reset ()
 Reset to null matrix.
 
virtual void Reset (int nrows, int ncols, int nonzeros=0) override
 Reset to null matrix and (if needed) changes the size.
 
void ResetBlocks (int nrows, int ncols)
 If size changes, is like the above, otherwise just sets the elements to zero.
 
virtual void SetElement (int row, int col, double elem, bool overwrite=true) override
 Set the value of the element with index (insrow, inscol) to insval. More...
 
virtual double GetElement (int row, int col) const override
 Returns the value of the element with index (row, col). More...
 
virtual void PasteMatrix (const ChMatrix<> &matra, int insrow, int inscol, bool overwrite, bool transp) override
 Paste a given matrix into this sparse matrix at position (insrow, inscol). More...
 
virtual void PasteTranspMatrix (const ChMatrix<> &matra, int insrow, int inscol) override
 Same as PasteMatrix(), but with overwrite set to true and transp set to true. More...
 
virtual void PasteClippedMatrix (const ChMatrix<> &matra, int cliprow, int clipcol, int nrows, int ncolumns, int insrow, int inscol, bool overwrite) override
 Paste a clipped portion of the given matrix into this sparse matrix at position (insrow, inscol). More...
 
virtual void PasteSumClippedMatrix (const ChMatrix<> &matra, int cliprow, int clipcol, int nrows, int ncolumns, int insrow, int inscol) override
 Same as PasteClippedMatrix(), but with overwrite set to false. More...
 
virtual void PasteSumMatrix (const ChMatrix<> &matra, int insrow, int inscol) override
 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) override
 Same as PasteMatrix(), but with overwrite set to false and transp set to true. More...
 
void PasteMatrix (const ChLinkedListMatrix &matra, int insrow, int inscol)
 
void PasteTranspMatrix (const ChLinkedListMatrix &matra, int insrow, int inscol)
 
void MatrScale (double factor)
 
void Neg ()
 
int Setup_LU ()
 Perform in-place LU factorization with partial (row) pivoting. More...
 
void Solve_LU (const ChMatrix<> &b, ChMatrix<> &x)
 Solve the system A*x = b, using an existing LU factorization.
 
int SolveGeneral (const ChMatrix<> &b, ChMatrix<> &x)
 Solve the general system A*x = b. More...
 
int Setup_LDL ()
 Perform in-place LDL factorization with full pivoting. More...
 
void Solve_LDL (const ChMatrix<> &b, ChMatrix<> &x)
 Solve the symmetric system A*x=b, using an existing LDL factorization.
 
int SolveSymmetric (const ChMatrix<> &b, ChMatrix<> &x)
 Solve the symmetric system A*x = b. More...
 
const std::vector< int > & GetPivots () const
 Get the pivot indexes after the last factorization.
 
double GetDeterminant () const
 Get matrix determinant. More...
 
void StreamOUTsparseMatlabFormat (ChStreamOutAscii &mstream)
 Method to allow serializing transient data into in ASCII stream (e.g., a file) as a Matlab sparse matrix format; each row in file has three elements: {row, column, value}. More...
 
void StreamOUT (ChStreamOutAscii &mstream)
 Write first few rows and columns to the console. More...
 
ChMelementGetElarrayDereferenced ()
 Used to convert to CSR3 format (ChCSMatrix). More...
 
template<typename reserveSizeType >
void CountNonZeros (reserveSizeType &reserveSize, int offset=0)
 Used to convert to CSR3 format (ChCSMatrix). More...
 
- Public Member Functions inherited from 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. 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 bool Compress ()
 Optional compression method, typically invoked after all elements have been inserted. 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.
 

Additional Inherited Members

- Public Types inherited from chrono::ChSparseMatrix
enum  SymmetryType { GENERAL, SYMMETRIC_POSDEF, SYMMETRIC_INDEF, STRUCTURAL_SYMMETRIC }
 Symmetry type of the matrix. More...
 
- Protected Attributes inherited from chrono::ChSparseMatrix
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 Function Documentation

template<typename reserveSizeType >
void chrono::ChLinkedListMatrix::CountNonZeros ( reserveSizeType &  reserveSize,
int  offset = 0 
)

Used to convert to CSR3 format (ChCSMatrix).

Returns a pointer to an array/vector of type reserveSizeType. reserveSizeType[i] gives the number of non-zero elements in the i-th row; reserveSize must have the same row-dimension as ChLinkedListMatrix instance.

double chrono::ChLinkedListMatrix::GetDeterminant ( ) const

Get matrix determinant.

Available after a factorization.

ChMelement* chrono::ChLinkedListMatrix::GetElarrayDereferenced ( )

Used to convert to CSR3 format (ChCSMatrix).

Returns the array to 1st column elements.

double chrono::ChLinkedListMatrix::GetElement ( int  row,
int  col 
) const
overridevirtual

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

Returns zero if an element is not stored.

Implements chrono::ChSparseMatrix.

void chrono::ChLinkedListMatrix::PasteClippedMatrix ( const ChMatrix<> &  matra,
int  cliprow,
int  clipcol,
int  nrows,
int  ncolumns,
int  insrow,
int  inscol,
bool  overwrite 
)
overridevirtual

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 from chrono::ChSparseMatrix.

void chrono::ChLinkedListMatrix::PasteMatrix ( const ChMatrix<> &  matra,
int  insrow,
int  inscol,
bool  overwrite,
bool  transp 
)
overridevirtual

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 from chrono::ChSparseMatrix.

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

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

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

Reimplemented from chrono::ChSparseMatrix.

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

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 from chrono::ChSparseMatrix.

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

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 from chrono::ChSparseMatrix.

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

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 from chrono::ChSparseMatrix.

void chrono::ChLinkedListMatrix::SetElement ( int  insrow,
int  inscol,
double  insval,
bool  overwrite = true 
)
overridevirtual

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.

Implements chrono::ChSparseMatrix.

int chrono::ChLinkedListMatrix::Setup_LDL ( )

Perform in-place LDL factorization with full pivoting.

Note that this factorization can only be performed for symmetric matrices. During the factorization, only the upper triangular part of A is accessed.

int chrono::ChLinkedListMatrix::Setup_LU ( )

Perform in-place LU factorization with partial (row) pivoting.

The matrix determinant is calculated as a by-product of the factorization and can be obtained with GetDeterminant().

int chrono::ChLinkedListMatrix::SolveGeneral ( const ChMatrix<> &  b,
ChMatrix<> &  x 
)

Solve the general system A*x = b.

Note that the matrix is modified in-place to contain the LU factors.

int chrono::ChLinkedListMatrix::SolveSymmetric ( const ChMatrix<> &  b,
ChMatrix<> &  x 
)

Solve the symmetric system A*x = b.

Note that the matrix is modified in-place to contain the LU factors.

void chrono::ChLinkedListMatrix::StreamOUT ( ChStreamOutAscii mstream)

Write first few rows and columns to the console.

Method to allow serializing transient data into in ASCII format.

void chrono::ChLinkedListMatrix::StreamOUTsparseMatlabFormat ( ChStreamOutAscii mstream)

Method to allow serializing transient data into in ASCII stream (e.g., a file) as a Matlab sparse matrix format; each row in file has three elements: {row, column, value}.

Note: the row and column indexes start from 1.