Create sparse matrices for factorization and solving systems.
In the Accelerate framework, Sparse Solvers stores sparse matrices using Compressed Sparse Column (CSC) format. CSC stores a matrix as a series of column vectors where the nonzero entries are specified as
(row-index, value) pairs and the zero entries are omitted.
Routines are provided to convert matrices from other formats to CSC. For more information, see Conversion from Other Formats.
The Accelerate framework supports unsymmetric and symmetric sparse matrices, each of which can also be blocked.
A symmetric matrix is symmetrical along the diagonal from its upper-left to lower-right corners. In other words, a symmetric matrix is equal to its transpose (A=Aᵀ).
A block matrix can be either unsymmetric or symmetric and is broken into sections called blocks. The blocks along the diagonal of a symmetrical block matrix must, themselves, be symmetrical.
Create an Unsymmetric Matrix
In this example of an unsymmetric sparse matrix, empty cells represent zeros:
The first step in creating a matrix is to create two arrays that store the row indices and corresponding values:
In addition to the
(row-index, value) pairs, you need to create a third array that specifies where each column starts. This array requires an additional, final entry that defines the final column's length.
In the following example, the zeroth item in the
values array starts column 0, the third starts column 1, and the seventh starts column 2:
The two structural arrays,
column, are used to create a SparseMatrixStructure instance that describes the matrix's structure. The initializer requires an attributes object, and the default parameters of a
Sparse instance specify an unsymmetric matrix:
With the structure and values defined, you can create a
Create a Symmetric Matrix
In this example of a symmetric sparse matrix, empty cells represent zeros:
Because it is symmetric, the values in the upper triangle of the matrix are redundant and should be excluded from the data passed to the
Sparse initializer. This example shows the excluded values in gray:
As with the unsymmetric example, the
row array specifies the row in the matrix that contains the corresponding item in
values, and the
column array specifies where each column starts in the
In this case, the attributes parameter allows you to specify that the matrix is symmetric and the items in the values array are derived from the lower triangle:
Sparse instance is created using the structure created above and the values from the lower triangle of the matrix:
Create a Block Matrix
You can create sparse matrices that are blocked—separated into blocks that contain multiple values—defining a blockSize greater than 1. The block size is the length of the side of the square block.
Block matrices can be symmetric or unsymmetric. This example shows an unsymmetric sparse matrix with a block size of 3:
The following example shows the code required to create a sparse matrix with the structure and values shown above. The block size of 3 is specified in the SparseMatrixStructure. The values for each block are concatenated in column-major order.
When creating a symmetric matrix with a block size greater than 1, the blocks along the matrix's diagonal must also be symmetric.