SparseMatrix¶
- class SparseMatrix(*args)¶
Compressed sparse column matrix.
Warning
This class is experimental and likely to be modified in future releases. To use it, import the
openturns.experimentalsubmodule.This class stores a matrix in the compressed sparse column (CSC) format, which is the format expected by sparse solvers: the non-zero coefficients are stored column by column, together with the row index of each coefficient and the offset of the first coefficient of each column.
The constructor from triplets builds the canonical form: the row indices are sorted within each column and the values of duplicate (row, column) coordinates are summed. Row and column indices are checked against the dimensions.
- Parameters:
- nbRowsint, optional
Number of rows. Default is 0.
- nbColumnsint, optional
Number of columns. Default is 0.
- rowIndicessequence of int, optional
Row index of each triplet, used by the constructor from triplets.
- columnIndicessequence of int, optional
Column index of each triplet, used by the constructor from triplets.
- valuessequence of float, optional
Value of each triplet, used by the constructor from triplets.
Methods
FromDenseMatrix(matrix)Conversion from a dense matrix.
Conversion to a dense matrix.
Accessor to the object's name.
Accessor to the column pointer of the compressed sparse column layout.
getName()Accessor to the object's name.
Accessor to the number of columns.
Accessor to the number of stored coefficients.
Accessor to the number of rows.
Accessor to the row indices of the compressed sparse column layout.
Accessor to the values of the compressed sparse column layout.
hasName()Test if the object is named.
Multiplication by a dense matrix given as left operand.
Multiplication by a symmetric matrix given as left operand.
setName(name)Accessor to the object's name.
Transposition.
Notes
The compressed sparse column layout is available through
getColumnPointer(),getRowIndices()andgetValues(). The column pointer has sizeand starts at 0, so that column
holds the coefficients of indices
getColumnPointer()[j]togetColumnPointer()[j + 1] - 1.The stored representation is canonical: the row indices are sorted within each column and each (row, column) coordinate is stored at most once. The coefficients can be read and written by index, and a coefficient which is not stored yet is inserted at its place when it is assigned.
Explicit zeros may be stored: they are kept in the sparse layout, and skipped when the matrix is converted to a dense matrix or handed over to a solver. Use
FromDenseMatrix()to build a sparse matrix from a dense one, which drops the zero coefficients.Examples
>>> import openturns as ot >>> import openturns.experimental as otexp >>> matrix = otexp.SparseMatrix(3, 2, [0, 2, 0], [0, 1, 0], [1.0, 2.0, 3.0]) >>> print(matrix.getNbNonZeros()) 2 >>> print(matrix.getColumnPointer()) [0,1,2] >>> print(matrix.asDenseMatrix()) [[ 4 0 ] [ 0 0 ] [ 0 2 ]] >>> matrix[1, 1] = 7.0 >>> print(matrix.getNbNonZeros()) 3 >>> print(matrix.getRowIndices()) [0,1,2] >>> print(matrix.asDenseMatrix()) [[ 4 0 ] [ 0 7 ] [ 0 2 ]]
- __init__(*args)¶
- static FromDenseMatrix(matrix)¶
Conversion from a dense matrix.
The zero coefficients of the dense matrix are skipped.
- Parameters:
- matrix
Matrix The dense matrix to convert.
- matrix
- Returns:
- sparseMatrix
SparseMatrix The sparse matrix with the same coefficients.
- sparseMatrix
- asDenseMatrix()¶
Conversion to a dense matrix.
- Returns:
- matrix
Matrix The dense matrix with the same coefficients.
- matrix
- getClassName()¶
Accessor to the object’s name.
- Returns:
- class_namestr
The object class name (object.__class__.__name__).
- getColumnPointer()¶
Accessor to the column pointer of the compressed sparse column layout.
- Returns:
- columnPointer
Indices Offset of the first coefficient of each column, of size the number of columns plus one and starting at 0.
- columnPointer
- getName()¶
Accessor to the object’s name.
- Returns:
- namestr
The name of the object.
- getNbColumns()¶
Accessor to the number of columns.
- Returns:
- nbColumnsint
Number of columns.
- getNbNonZeros()¶
Accessor to the number of stored coefficients.
- Returns:
- nbNonZerosint
Number of coefficients stored in the compressed sparse column layout.
- getNbRows()¶
Accessor to the number of rows.
- Returns:
- nbRowsint
Number of rows.
- getRowIndices()¶
Accessor to the row indices of the compressed sparse column layout.
- Returns:
- rowIndices
Indices Row index of each stored coefficient, sorted within each column.
- rowIndices
- getValues()¶
Accessor to the values of the compressed sparse column layout.
- Returns:
- values
Point Value of each stored coefficient, in the same order as
getRowIndices().
- values
- hasName()¶
Test if the object is named.
- Returns:
- hasNamebool
True if the name is not empty.
- multiplyByDenseOnLeft(m)¶
Multiplication by a dense matrix given as left operand.
The sparse matrix is not densified: the product iterates over its stored coefficients only.
- multiplyBySymmetricOnLeft(m)¶
Multiplication by a symmetric matrix given as left operand.
Same as
multiplyByDenseOnLeft(), except that the left operand is read through the symmetric accessor as only its lower triangle is stored.- Parameters:
- matrix
SymmetricMatrix The symmetric left operand, with as many columns as the sparse matrix has rows.
- matrix
- Returns:
- result
Matrix The dense product.
- result
- setName(name)¶
Accessor to the object’s name.
- Parameters:
- namestr
The name of the object.
- transpose()¶
Transposition.
- Returns:
- transpose
SparseMatrix The transposed matrix.
- transpose
OpenTURNS