Sparse Arrays: Linear Algebra

Many large-scale applications of linear algebra involve matrices that have many elements, but comparatively few that are nonzero. You can represent such sparse matrices efficiently in the Wolfram System using (RefLink[SparseArray, paclet:ref/SparseArray]) objects, as discussed in "Sparse Arrays: Manipulating Lists". (RefLink[SparseArray, paclet:ref/SparseArray]) objects work by having lists of rules that specify where nonzero values appear.

Specifying sparse arrays.

As discussed in "Sparse Arrays: Manipulating Lists", you can use patterns to specify collections of elements in sparse arrays. You can also have sparse arrays that correspond to tensors of any rank.

This makes a 50×50 sparse numerical matrix, with 148 nonzero elements:

m=SparseArray[{{30,_}->11.5,{_,30}->21.5,{i_,i_}->i},{50,50}]

This shows a visual representation of the matrix elements:

ArrayPlot[m]

Here are the four largest eigenvalues of the matrix:

Eigenvalues[m,4]

(RefLink[Dot, paclet:ref/Dot]) gives a (RefLink[SparseArray, paclet:ref/SparseArray]) result:

m.m

You can extract parts just like in an ordinary array:

%[[20,20]]

You can apply most standard structural operations directly to (RefLink[SparseArray, paclet:ref/SparseArray]) objects, just as you would to ordinary lists. When the results are sparse, they typically return (RefLink[SparseArray, paclet:ref/SparseArray]) objects.

A few structural operations that can be done directly on (RefLink[SparseArray, paclet:ref/SparseArray]) objects.

This gives the first column of m. It has only 2 nonzero elements:

m[[All,1]]

This adds 3 to each element in the first column of m:

m[[All,1]]=3+m[[All,1]]

Now all the elements in the first column are nonzero:

m[[All,1]]

This gives the rules for the nonzero elements on the second row:

ArrayRules[m[[2]]]

Typical ways to get sparse arrays.

This generates a tridiagonal random matrix:

SparseArray[{i_,j_}/;Abs[i-j]<=1:>RandomReal[],{100,100}]

Even the tenth power of the matrix is still fairly sparse:

MatrixPower[%,10]

This extracts the coefficients as sparse arrays:

s=CoefficientArrays[{c+x-z==0,x+2 y+z==0},{x,y,z}]

Here are the corresponding ordinary arrays:

Normal[%]

This reproduces the original forms:

s[[1]]+s[[2]].{x,y,z}

(RefLink[CoefficientArrays, paclet:ref/CoefficientArrays]) can handle general polynomial equations:

s=CoefficientArrays[{c+x^2-z==0,x^2+2 y+z^2==0},{x,y,z}]

The coefficients of the quadratic part are given in a rank 3 tensor:

Normal[%]

This reproduces the original forms:

s[[1]]+s[[2]].{x,y,z}+s[[3]].{x,y,z}.{x,y,z}

For machine-precision numerical sparse matrices, the Wolfram System supports standard file formats such as Matrix Market (.mtx) and Harwell–Boeing. You can import and export matrices in these formats using (RefLink[Import, paclet:ref/Import]) and (RefLink[Export, paclet:ref/Export]).