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.experimental submodule.

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.

asDenseMatrix()

Conversion to a dense matrix.

getClassName()

Accessor to the object's name.

getColumnPointer()

Accessor to the column pointer of the compressed sparse column layout.

getName()

Accessor to the object's name.

getNbColumns()

Accessor to the number of columns.

getNbNonZeros()

Accessor to the number of stored coefficients.

getNbRows()

Accessor to the number of rows.

getRowIndices()

Accessor to the row indices of the compressed sparse column layout.

getValues()

Accessor to the values of the compressed sparse column layout.

hasName()

Test if the object is named.

multiplyByDenseOnLeft(m)

Multiplication by a dense matrix given as left operand.

multiplyBySymmetricOnLeft(m)

Multiplication by a symmetric matrix given as left operand.

setName(name)

Accessor to the object's name.

transpose()

Transposition.

Notes

The compressed sparse column layout is available through getColumnPointer(), getRowIndices() and getValues(). The column pointer has size n + 1 and starts at 0, so that column j holds the coefficients of indices getColumnPointer()[j] to getColumnPointer()[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:
matrixMatrix

The dense matrix to convert.

Returns:
sparseMatrixSparseMatrix

The sparse matrix with the same coefficients.

asDenseMatrix()

Conversion to a dense matrix.

Returns:
matrixMatrix

The dense matrix with the same coefficients.

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:
columnPointerIndices

Offset of the first coefficient of each column, of size the number of columns plus one and starting at 0.

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:
rowIndicesIndices

Row index of each stored coefficient, sorted within each column.

getValues()

Accessor to the values of the compressed sparse column layout.

Returns:
valuesPoint

Value of each stored coefficient, in the same order as getRowIndices().

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.

Parameters:
matrixMatrix

The dense left operand, with as many columns as the sparse matrix has rows.

Returns:
resultMatrix

The dense product.

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:
matrixSymmetricMatrix

The symmetric left operand, with as many columns as the sparse matrix has rows.

Returns:
resultMatrix

The dense product.

setName(name)

Accessor to the object’s name.

Parameters:
namestr

The name of the object.

transpose()

Transposition.

Returns:
transposeSparseMatrix

The transposed matrix.

Examples using the class

Unit commitment

Unit commitment