sparse_test


sparse_test, an Octave code which calls sparse(), which is a set of Octave functions for creating and manipulating sparse matrices.

The SPARSE function

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:
i
the row indices of the nonzero elements;
j
the column indices of the nonzero elements;
s
the values of the nonzero elements;
m
the number of rows in the matrix;
n
the number of columns in the matrix;
nz_max
the maximum number of nonzero elements in the matrix; nz_max is commonly omitted, in which case its value is taken from the length of s.
Note that nz_max is commonly omitted, in which case its value is taken from the length of s. Moreover, if you specify nz_max explicitly, or implicitly through the size of s, Octave will allow you to increase the number of nonzero elements at any time. Specifying nz_max correctly, then, is simply a matter of efficiency.

Example: Creating a Sparse Matrix at Once

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  35
      
by 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 );
      

Example: Building a Sparse Matrix in Steps

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) + 1
      
If 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.

Useful Commands

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.

Languages:

sparse_test is available in a MATLAB version and an Octave version and a Python version.

Related Data and Programs:

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).

Reference:

  1. Timothy Davis,
    Direct Methods for Sparse Linear Systems,
    SIAM, 2006,
    ISBN: 0898716136.
  2. John Gilbert, Cleve Moler, Robert Schreiber,
    Sparse Matrices in MATLAB: Design and Implementation,
    SIAM Journal on Matrix Analysis and Applications,
    Volume 13, Number 1, 1992, pages 333-356.
  3. George Lindfield, John Penny,
    Numerical Methods Using MATLAB,
    Second Edition,
    Prentice Hall, 1999,
    ISBN: 0-13-012641-1,
    LC: QA297.P45.
  4. The Mathworks,
    MATLAB Mathematics.

Source Code:


Last revised on 24 September 2022.