Sparse Matrix |
A sparse matrix is a matrix populated primarily with zeros as elements of the table. If the majority of elements differ from zero, then it is common to refer to the matrix as a dense matrix.
The native data structure for a matrix is a two-dimensional array. Each entry in the array represents an element ai,j of the matrix and can be accessed by the two indices i and j. i indicates the row number (top-to-bottom), while j indicates the column number (left-to-right) of each element in the table. For an m×n matrix, enough memory to store up to (m×n) entries to represent the matrix is needed.
Substantial memory requirement reductions can be realized by storing only the non-zero entries. Depending on the number and distribution of the non-zero entries, different data structures can be used and yield huge savings in memory when compared to a native approach.
DOK (Dictionary of keys) format supports efficient modification and is used to construct the matrix. Once the matrix is constructed, it is converted to a CSR (Compressed Sparse Row) format, which is more efficient for matrix operations.
SparseMatrix is a class of sparse matrix. To create an instance of this class use one of the constructors below:
Constructor | Description | Performance |
---|---|---|
set size | Creates a new sparse matrix with given size. | |
set size and number of non-zero elements | Creates a new sparse matrix with given size and the estimated number of non-zero elements in this matrix. This value is used only to preallocate internal storage. |
SparseMatrix class provides the following methods:
Method | Description | Performance |
---|---|---|
clone | Creates a copy of this matrix. | |
save | Saves SparseMatrix into CSV file. | |
load | Loads SparseMatrix from CSV file. | |
dense representation | Returns dense representation of this matrix. |
The following properties are provided:
Property | Description | Performance |
---|---|---|
rows | The number of rows in the matrix. | |
columns | The number of columns in the matrix. | |
size | Total number of elements in the matrix. | |
nonzeros | Number of nonzero matrix elements. | |
density | The density of the matrix (the ratio of number of nonzero elements and size of the matrix). | |
element | A specific element in the matrix (set or get). |