This section describes the sparse matrix storage schemes available in Pysparse. It also covers sparse matrix creation, population and conversion.
The linked-list format allows insertion and lookup of nonzero elements in moderate time and without having to move too much data around. Internally, the nonzero entries of a matrix are stored row by row in a linked list. Within a given row, column indices are sorted in ascending order.
In Pysparse, matrices in linked-list format are created by using the ll_mat class.
This format resembles a sorted version of the coordinate format but with a data structure that lends itself to fast insertion, removal and lookup.
Typically, a new matrix should be created as an ll_mat and populated. If necessary, it can then be converted to compressed sparse row or sparse skyline format using the to_csr() and to_sss() methods.
The data structure for a matrix in linked-list format has the following components:
Here n is the number of rows of the matrix and nalloc is number of allocated elements in the arrays val, col and link. Note that the number of nonzero entries stored is less than or equal to nalloc, but the val, col and link arrays can be enlarged dynamically if necessary.
In CSR format, a sparse matrix is represented via three arrays:
Here n is the number of rows of the matrix and nnz is its number of nonzero entries.
This format is particularly interesting for computing matrix-vector products. Even though the order of the entries is not prescribed in this format, we sort the entries of each row by ascending column indices. This enables us to use more efficient algorithms for certain operations.
The SSS format is closely related to the CSR format. It is often used for sparse symmetric matrices. The diagonal is stored in a separate (full) vector and the strict lower triangle is stored in CSR format:
Here n is the order of the matrix and nnz is the number of nonzero entries in the strict lower triangle.
We sort the entries of each row by ascending column indices, like we do with the CSR format. The SSS format has the advantage over the CSR format, that it requires roughly half of the storage space and that the matrix-vector multiplication can be implemented more efficiently