Hubbry Logo
Sparse matrixSparse matrixMain
Open search
Sparse matrix
Community hub
Sparse matrix
logo
8 pages, 0 posts
0 subscribers
Be the first to start a discussion here.
Be the first to start a discussion here.
Contribute something
Sparse matrix
Sparse matrix
from Wikipedia
Example of sparse matrix
The above sparse matrix contains only 9 non-zero elements, with 26 zero elements. Its sparsity is 74%, and its density is 26%.
A sparse matrix obtained when solving a finite element problem in two dimensions. The non-zero elements are shown in black.

In numerical analysis and scientific computing, a sparse matrix or sparse array is a matrix in which most of the elements are zero.[1] There is no strict definition regarding the proportion of zero-value elements for a matrix to qualify as sparse but a common criterion is that the number of non-zero elements is roughly equal to the number of rows or columns. By contrast, if most of the elements are non-zero, the matrix is considered dense.[1] The number of zero-valued elements divided by the total number of elements (e.g., m × n for an m × n matrix) is sometimes referred to as the sparsity of the matrix.

Conceptually, sparsity corresponds to systems with few pairwise interactions. For example, consider a line of balls connected by springs from one to the next: this is a sparse system, as only adjacent balls are coupled. By contrast, if the same line of balls were to have springs connecting each ball to all other balls, the system would correspond to a dense matrix. The concept of sparsity is useful in combinatorics and application areas such as network theory and numerical analysis, which typically have a low density of significant data or connections. Large sparse matrices often appear in scientific or engineering applications when solving partial differential equations.

When storing and manipulating sparse matrices on a computer, it is beneficial and often necessary to use specialized algorithms and data structures that take advantage of the sparse structure of the matrix. Specialized computers have been made for sparse matrices,[2] as they are common in the machine learning field.[3] Operations using standard dense-matrix structures and algorithms are slow and inefficient when applied to large sparse matrices as processing and memory are wasted on the zeros. Sparse data is by nature more easily compressed and thus requires significantly less storage. Some very large sparse matrices are infeasible to manipulate using standard dense-matrix algorithms.

Special cases

[edit]

Banded

[edit]

A band matrix is a special class of sparse matrix where the non-zero elements are concentrated near the main diagonal. A band matrix is characterised by its lower and upper bandwidths, which refer to the number of diagonals below and above (respectively) the main diagonal between which all of the non-zero entries are contained.

Formally, the lower bandwidth of a matrix A is the smallest number p such that the entry ai,j vanishes whenever i > j + p. Similarly, the upper bandwidth is the smallest number p such that ai,j = 0 whenever i < jp (Golub & Van Loan 1996, §1.2.1). For example, a tridiagonal matrix has lower bandwidth 1 and upper bandwidth 1. As another example, the following sparse matrix has lower and upper bandwidth both equal to 3. Notice that zeros are represented with dots for clarity.

Matrices with reasonably small upper and lower bandwidth are known as band matrices and often lend themselves to simpler algorithms than general sparse matrices; or one can sometimes apply dense matrix algorithms and gain efficiency simply by looping over a reduced number of indices.

By rearranging the rows and columns of a matrix A it may be possible to obtain a matrix A with a lower bandwidth. A number of algorithms are designed for bandwidth minimization.

Diagonal

[edit]

A diagonal matrix is the extreme case of a banded matrix, with zero upper and lower bandwidth. A diagonal matrix can be stored efficiently by storing just the entries in the main diagonal as a one-dimensional array, so a diagonal n × n matrix requires only n entries in memory.

Symmetric

[edit]

A symmetric sparse matrix arises as the adjacency matrix of an undirected graph; it can be stored efficiently as an adjacency list.

Block diagonal

[edit]

A block-diagonal matrix consists of sub-matrices along its diagonal blocks. A block-diagonal matrix A has the form

where Ak is a square matrix for all k = 1, ..., n.

Use

[edit]

Reducing fill-in

[edit]

The fill-in of a matrix are those entries that change from an initial zero to a non-zero value during the execution of an algorithm. To reduce the memory requirements and the number of arithmetic operations used during an algorithm, it is useful to minimize the fill-in by switching rows and columns in the matrix. The symbolic Cholesky decomposition can be used to calculate the worst possible fill-in before doing the actual Cholesky decomposition.

There are other methods than the Cholesky decomposition in use. Orthogonalization methods (such as QR factorization) are common, for example, when solving problems by least squares methods. While the theoretical fill-in is still the same, in practical terms the "false non-zeros" can be different for different methods. And symbolic versions of those algorithms can be used in the same manner as the symbolic Cholesky to compute worst case fill-in.

Solving sparse matrix equations

[edit]

Both iterative and direct methods exist for sparse matrix solving.

Iterative methods, such as conjugate gradient method and GMRES utilize fast computations of matrix-vector products , where matrix is sparse. The use of preconditioners can significantly accelerate convergence of such iterative methods.

Storage

[edit]

A matrix is typically stored as a two-dimensional array. Each entry in the array represents an element ai,j of the matrix and is accessed by the two indices i and j. Conventionally, i is the row index, numbered from top to bottom, and j is the column index, numbered from left to right. For an m × n matrix, the amount of memory required to store the matrix in this format is proportional to m × n (disregarding the fact that the dimensions of the matrix also need to be stored).

In the case of a sparse matrix, 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 the basic approach. The trade-off is that accessing the individual elements becomes more complex and additional structures are needed to be able to recover the original matrix unambiguously.

Formats can be divided into two groups:

  • Those that support efficient modification, such as DOK (Dictionary of keys), LIL (List of lists), or COO (Coordinate list). These are typically used to construct the matrices.
  • Those that support efficient access and matrix operations, such as CSR (Compressed Sparse Row) or CSC (Compressed Sparse Column).

Dictionary of keys (DOK)

[edit]

DOK consists of a dictionary that maps (row, column)-pairs to the value of the elements. Elements that are missing from the dictionary are taken to be zero. The format is good for incrementally constructing a sparse matrix in random order, but poor for iterating over non-zero values in lexicographical order. One typically constructs a matrix in this format and then converts to another more efficient format for processing.[4]

List of lists (LIL)

[edit]

LIL stores one list per row, with each entry containing the column index and the value. Typically, these entries are kept sorted by column index for faster lookup. This is another format good for incremental matrix construction.[5]

Coordinate list (COO)

[edit]

COO stores a list of (row, column, value) tuples. Ideally, the entries are sorted first by row index and then by column index, to improve random access times. This is another format that is good for incremental matrix construction.[6]

Compressed sparse row (CSR, CRS or Yale format)

[edit]

The compressed sparse row (CSR) or compressed row storage (CRS) or Yale format represents a matrix M by three (one-dimensional) arrays, that respectively contain nonzero values, the extents of rows, and column indices. It is similar to COO, but compresses the row indices, hence the name. This format allows fast row access and matrix-vector multiplications (Mx). The CSR format has been in use since at least the mid-1960s, with the first complete description appearing in 1967.[7]

The CSR format stores a sparse m × n matrix M in row form using three (one-dimensional) arrays (V, COL_INDEX, ROW_INDEX). Let NNZ denote the number of nonzero entries in M. (Note that zero-based indices shall be used here.)

  • The arrays V and COL_INDEX are of length NNZ, and contain the non-zero values and the column indices of those values respectively
  • COL_INDEX contains the column in which the corresponding entry V is located.
  • The array ROW_INDEX is of length m + 1 and encodes the index in V and COL_INDEX where the given row starts. This is equivalent to ROW_INDEX[j] encoding the total number of nonzeros above row j. The last element is NNZ , i.e., the fictitious index in V immediately after the last valid index NNZ − 1.[8]

For example, the matrix is a 4 × 4 matrix with 4 nonzero elements, hence

V         = [ 5 8 3 6 ]
COL_INDEX = [ 0 1 2 1 ]
ROW_INDEX = [ 0 1 2 3 4 ] 

assuming a zero-indexed language.

To extract a row, we first define:

row_start = ROW_INDEX[row]
row_end   = ROW_INDEX[row + 1]

Then we take slices from V and COL_INDEX starting at row_start and ending at row_end.

To extract the row 1 (the second row) of this matrix we set row_start=1 and row_end=2. Then we make the slices V[1:2] = [8] and COL_INDEX[1:2] = [1]. We now know that in row 1 we have one element at column 1 with value 8.

In this case the CSR representation contains 13 entries, compared to 16 in the original matrix. The CSR format saves on memory only when NNZ < (m (n − 1) − 1) / 2.

Another example, the matrix is a 4 × 6 matrix (24 entries) with 8 nonzero elements, so

V         = [ 10 20 30 40 50 60 70 80 ]
COL_INDEX = [  0  1  1  3  2  3  4  5 ]   
ROW_INDEX = [  0  2  4  7  8 ]

The whole is stored as 21 entries: 8 in V, 8 in COL_INDEX, and 5 in ROW_INDEX.

  • ROW_INDEX splits the array V into rows: (10, 20) (30, 40) (50, 60, 70) (80), indicating the index of V (and COL_INDEX) where each row starts and ends;
  • COL_INDEX aligns values in columns: (10, 20, ...) (0, 30, 0, 40, ...)(0, 0, 50, 60, 70, 0) (0, 0, 0, 0, 0, 80).

Note that in this format, the first value of ROW_INDEX is always zero and the last is always NNZ, so they are in some sense redundant (although in programming languages where the array length needs to be explicitly stored, NNZ would not be redundant). Nonetheless, this does avoid the need to handle an exceptional case when computing the length of each row, as it guarantees the formula ROW_INDEX[i + 1] − ROW_INDEX[i] works for any row i. Moreover, the memory cost of this redundant storage is likely insignificant for a sufficiently large matrix.

The (old and new) Yale sparse matrix formats are instances of the CSR scheme. The old Yale format works exactly as described above, with three arrays; the new format combines ROW_INDEX and COL_INDEX into a single array and handles the diagonal of the matrix separately.[9]

For logical adjacency matrices, the data array can be omitted, as the existence of an entry in the row array is sufficient to model a binary adjacency relation.

It is likely known as the Yale format because it was proposed in the 1977 Yale Sparse Matrix Package report from Department of Computer Science at Yale University.[10]

Compressed sparse column (CSC or CCS)

[edit]

CSC is similar to CSR except that values are read first by column, a row index is stored for each value, and column pointers are stored. For example, CSC is (val, row_ind, col_ptr), where val is an array of the (top-to-bottom, then left-to-right) non-zero values of the matrix; row_ind is the row indices corresponding to the values; and, col_ptr is the list of val indexes where each column starts. The name is based on the fact that column index information is compressed relative to the COO format. One typically uses another format (LIL, DOK, COO) for construction. This format is efficient for arithmetic operations, column slicing, and matrix-vector products. This is the traditional format for specifying a sparse matrix in MATLAB (via the sparse function).

Software

[edit]

Many software libraries support sparse matrices, and provide solvers for sparse matrix equations. The following are open-source:

  • PETSc, a large C library, containing many different matrix solvers for a variety of matrix storage formats.
  • Trilinos, a large C++ library, with sub-libraries dedicated to the storage of dense and sparse matrices and solution of corresponding linear systems.
  • Eigen3 is a C++ library that contains several sparse matrix solvers. However, none of them are parallelized.
  • MUMPS (MUltifrontal Massively Parallel sparse direct Solver), written in Fortran90, is a frontal solver.
  • deal.II, a finite element library that also has a sub-library for sparse linear systems and their solution.
  • DUNE, another finite element library that also has a sub-library for sparse linear systems and their solution.
  • Armadillo provides a user-friendly C++ wrapper for BLAS and LAPACK.
  • SciPy provides support for several sparse matrix formats, linear algebra, and solvers.
  • ALGLIB is a C++ and C# library with sparse linear algebra support
  • ARPACK Fortran 77 library for sparse matrix diagonalization and manipulation, using the Arnoldi algorithm
  • SLEPc Library for solution of large scale linear systems and sparse matrices
  • scikit-learn, a Python library for machine learning, provides support for sparse matrices and solvers
  • SparseArrays is a Julia standard library.
  • PSBLAS, software toolkit to solve sparse linear systems supporting multiple formats also on GPU.

History

[edit]

The term sparse matrix was possibly coined by Harry Markowitz who initiated some pioneering work but then left the field.[11]

See also

[edit]

Notes

[edit]

References

[edit]

Further reading

[edit]
Revisions and contributorsEdit on WikipediaRead on Wikipedia
from Grokipedia
A sparse matrix is a matrix in which most elements are zero, typically containing only a small fraction of nonzero entries relative to its total dimensions. These matrices arise frequently in scientific computing, , and applications such as solving partial differential equations, graph representations, and large-scale optimization problems, where dense storage would be inefficient due to the predominance of zeros. The key advantage of sparse matrices lies in their ability to reduce memory usage and computational time by storing and operating only on the nonzero elements, avoiding the overhead of the vast majority of zero values that would otherwise dominate in a full . Common storage formats include the Coordinate (COO) format, which records the row, column, and value of each nonzero entry as a triplet; the Compressed Sparse Row (CSR) format, which groups nonzeros by row using value and column-index arrays along with row pointers; and the Compressed Sparse Column (CSC) format, analogous to CSR but organized by columns. These formats enable efficient access patterns tailored to specific operations, such as matrix-vector multiplication, which can be performed in time proportional to the number of nonzeros rather than the full matrix size. For computations involving sparse matrices, specialized algorithms exploit the sparsity pattern to maintain efficiency, including direct methods like with fill-in minimization to preserve sparsity during factorization, and iterative methods such as the Conjugate Gradient for symmetric positive definite systems or Biconjugate Gradient for general cases. Properties like , diagonal dominance, or further influence algorithm choice and performance, often linking sparse matrix techniques to for ordering and partitioning to optimize solve times in large linear systems.

Fundamentals

Definition

A sparse matrix is an m×nm \times n matrix in which most elements are zero, such that the number of non-zero elements, denoted as nnz, satisfies nnz mn\ll m n. More formally, for square matrices, a matrix is sparse if nnz is proportional to nn rather than n2n^2. In general, a matrix ARm×nA \in \mathbb{R}^{m \times n} is sparse if nnz = O(n1+γ)O(n^{1 + \gamma}) for some γ<1\gamma < 1, assuming mnm \approx n. The density of a matrix is defined as nnzmn\frac{\mathrm{nnz}}{m n}, measuring the proportion of non-zero entries; the sparsity is 11 - density. Matrices are typically considered sparse when this density is low, often much less than 1% non-zero entries in many numerical applications. In practice, even lower densities, such as less than 0.01%, are common in large-scale problems where sparsity exploitation is crucial. Unlike dense matrices, which store all mnm n elements explicitly in memory regardless of their values, sparse matrices use specialized representations that store only the non-zero values and their indices to reduce memory usage. To illustrate, consider a 3×33 \times 3 matrix with only two non-zero entries: (100003000)\begin{pmatrix} 1 & 0 & 0 \\ 0 & 0 & 3 \\ 0 & 0 & 0 \end{pmatrix}
Add your contribution
Related Hubs
Contribute something
User Avatar
No comments yet.