Sparse Matrices¶
Sparse matrices support much of the same set of operations as dense matrices. The following functions are specific to sparse matrices.
-
sparse(I, J, V[, m, n, combine])¶ Create a sparse matrix
Sof dimensionsm x nsuch thatS[I[k], J[k]] = V[k]. Thecombinefunction is used to combine duplicates. Ifmandnare not specified, they are set tomax(I)andmax(J)respectively. If thecombinefunction is not supplied, duplicates are added by default.
-
sparsevec(I, V[, m, combine])¶ Create a sparse matrix
Sof sizem x 1such thatS[I[k]] = V[k]. Duplicates are combined using thecombinefunction, which defaults to+if it is not provided. In julia, sparse vectors are really just sparse matrices with one column. Given Julia’s Compressed Sparse Columns (CSC) storage format, a sparse column matrix with one column is sparse, whereas a sparse row matrix with one row ends up being dense.
-
sparsevec(D::Dict[, m]) Create a sparse matrix of size
m x 1where the row values are keys from the dictionary, and the nonzero values are the values from the dictionary.
-
issparse(S)¶ Returns
trueifSis sparse, andfalseotherwise.
-
sparse(A) Convert a dense matrix
Ainto a sparse matrix.
-
sparsevec(A) Convert a dense vector
Ainto a sparse matrix of sizem x 1. In julia, sparse vectors are really just sparse matrices with one column.
-
full(S)¶ Convert a sparse matrix
Sinto a dense matrix.
-
nnz(A)¶ Returns the number of stored (filled) elements in a sparse matrix.
-
spzeros(m, n)¶ Create an empty sparse matrix of size
m x n.
-
spones(S)¶ Create a sparse matrix with the same structure as that of
S, but with every nonzero element having the value1.0.
-
speye(type, m[, n])¶ Create a sparse identity matrix of specified type of size
m x m. In casenis supplied, create a sparse identity matrix of sizem x n.
-
spdiagm(B, d[, m, n])¶ Construct a sparse diagonal matrix.
Bis a tuple of vectors containing the diagonals anddis a tuple containing the positions of the diagonals. In the case the input contains only one diagonaly,Bcan be a vector (instead of a tuple) anddcan be the diagonal position (instead of a tuple), defaulting to 0 (diagonal). Optionally,mandnspecify the size of the resulting sparse matrix.
-
sprand(m, n, p[, rng])¶ Create a random
mbynsparse matrix, in which the probability of any element being nonzero is independently given byp(and hence the mean density of nonzeros is also exactlyp). Nonzero values are sampled from the distribution specified byrng. The uniform distribution is used in caserngis not specified.
-
sprandn(m, n, p)¶ Create a random
mbynsparse matrix with the specified (independent) probabilitypof any entry being nonzero, where nonzero values are sampled from the normal distribution.
-
sprandbool(m, n, p)¶ Create a random
mbynsparse boolean matrix with the specified (independent) probabilitypof any entry beingtrue.
-
etree(A[, post])¶ Compute the elimination tree of a symmetric sparse matrix
Afromtriu(A)and, optionally, its post-ordering permutation.
-
symperm(A, p)¶ Return the symmetric permutation of A, which is
A[p,p]. A should be symmetric and sparse, where only the upper triangular part of the matrix is stored. This algorithm ignores the lower triangular part of the matrix. Only the upper triangular part of the result is returned as well.
-
nonzeros(A)¶ Return a vector of the structural nonzero values in sparse matrix
A. This includes zeros that are explicitly stored in the sparse matrix. The returned vector points directly to the internal nonzero storage ofA, and any modifications to the returned vector will mutateAas well.