sparse_test, an Octave code which calls sparse(), which is a set of Octave functions for creating and manipulating sparse matrices.
Octave provides a sparse function of the form
matrix = sparse ( i, j, s, m, n, nz_max )This function can be used to create a sparse matrix. The input arguments have the following meaning:
Although this is not the usual way to use the sparse command, the following example should help to understand what is going on. We mean to define the following matrix:
11 12 0 0 15 0 22 23 0 0 31 0 33 34 35by the following commands:
i = [ 1, 1, 1, 2, 2, 3, 3, 3, 3 ]; j = [ 1, 2, 5, 2, 3, 1, 3, 4, 5 ]; s = [ 11, 12, 15, 22, 23, 31, 33, 34, 35 ]; m = 3; n = 5; nz_max = 9; a = sparse ( i, j, s, m, n, nz_max );
Of course, in many applications, the matrix data is only available one piece at a time, or has to be modified as you go. This is easy to do, as well. You may start by defining the matrix to be an "empty" sparse matrix of a particular size, as, for example:
i = []; j = []; s = []; m = 100; n = 100; a = sparse ( i, j, s, m, n );The matrix will be empty, and entries of the matrix are by default equal to zero. Then you can simply reference entries of the matrix as you need them. For instance,
a(1,1) = 3 a(3,8) = a(3,8) + 7 a(4,7) = a(9,5) + 2 * a(8,12) a(4,7) = a(4,7) + 1If you reference, on the right hand side of these equations, a matrix entry that doesn't exist, it is by default zero. If you assign a value on the left hand side to a matrix entry that doesn't exist, a space is created for it, and it is given this value. If the entry already existed, it is overwritten.
In some cases, the sparse matrix structure allows you to ignore the fact that you are using a sparse matrix. We have already seen that you can reference the (i,j) element of the matrix in the same way you would do for a full matrix, and this is true whether you are simply asking to "read" the current value of the element, or to "write" a new value for the element.
A particular example of this is the fact that you can solve a sparse linear system using the same "backslash" formula that you would use if the matrix were full:
x = A \ b;
There are many commands specifically for dealing with a sparse matrix. For our examples, we will be considering
Note that, in a sense, we actually use two formats for sparse matrices. The user communicates by specifying what is known as a sparse triplet, that is, a set of row indices, column indices, and values. But internally, a sparse compressed column format is used, which allows rapid access to matrix entries.
To copy the nonzero entries from a sparse matrix, creating the sparse triplet structure:
[i,j,s] = sparse ( A ); [m,n] = size ( A );Correspondingly, to create the sparse matrix from this data:
A = sparse ( i, j, s, m, n );
When using the sparse matrix format, it is possible to refer to matrix entries directly, using the usual index notation like A(i,j). However, accessing specific entries in this way, whether to initialize, extract, increment, or zero them, is an expensive process. You can extract the sparse triplet information, work on it in the natural way, and then "repacking" it with the sparse() command.
sparse_test is available in a MATLAB version and an Octave version and a Python version.
sparse_ccs, a data directory which contains examples of compressed column storage (CCS), equivalent to MATLAB's sparse format, and a file format suitable for storing such information.
sparse_crs, a data directory which contains a description and examples of the compressed row storage (CRS) format, for storing a sparse matrix, including a way to write the matrix as a set of three files.
st, a data directory which contains examples of the "sparse triplet" format for storing sparse matrices. This format is equivalent to the form in which sparse matrix data is passed into MATLAB's sparse command (although the sparse compressed column format is used internally).