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 Mathematica using SparseArray objects, as discussed in "Sparse Arrays: Manipulating Lists". SparseArray objects work by having lists of rules that specify where nonzero values appear.

SparseArray[list]a SparseArray version of an ordinary list
SparseArray[{{i1,j1}->v1,{i2,j2}->v2,...},{m,n}]
an m×n sparse array with element having value
SparseArray[{{i1,j1},{i2,j2},...}->{v1,v2,...},{m,n}]
the same sparse array
Normal[array]the ordinary list corresponding to a SparseArray

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.
In[1]:=
Click for copyable input
Out[1]=
This shows a visual representation of the matrix elements.
In[2]:=
Click for copyable input
Out[2]=
Here are the four largest eigenvalues of the matrix.
In[3]:=
Click for copyable input
Out[3]=
Dot gives a SparseArray result.
In[4]:=
Click for copyable input
Out[4]=
You can extract parts just like in an ordinary array.
In[5]:=
Click for copyable input
Out[5]=

You can apply most standard structural operations directly to SparseArray objects, just as you would to ordinary lists. When the results are sparse, they typically return SparseArray objects.

Dimensions[m]the dimensions of an array
ArrayRules[m]the rules for nonzero elements in an array
m[[i,j]]element i, j
m[[i]]the i^(th) row
m[[All,j]]the j^(th) column
m[[i,j]]=vreset element i, j

A few structural operations that can be done directly on SparseArray objects.

This gives the first column of . It has only 2 nonzero elements.
In[6]:=
Click for copyable input
Out[6]=
This adds 3 to each element in the first column of .
In[7]:=
Click for copyable input
Out[7]=
Now all the elements in the first column are nonzero.
In[8]:=
Click for copyable input
Out[8]=
This gives the rules for the nonzero elements on the second row.
In[9]:=
Click for copyable input
Out[9]=
SparseArray[rules]generate a sparse array from rules
CoefficientArrays[{eqns1,eqns2,...},{x1,x2,...}]
get arrays of coefficients from equations
Import["file.mtx"]import a sparse array from a file

Typical ways to get sparse arrays.

This generates a tridiagonal random matrix.
In[10]:=
Click for copyable input
Out[10]=
Even the tenth power of the matrix is still fairly sparse.
In[11]:=
Click for copyable input
Out[11]=
This extracts the coefficients as sparse arrays.
In[12]:=
Click for copyable input
Out[12]=
Here are the corresponding ordinary arrays.
In[13]:=
Click for copyable input
Out[13]=
This reproduces the original forms.
In[14]:=
Click for copyable input
Out[14]=
CoefficientArrays can handle general polynomial equations.
In[15]:=
Click for copyable input
Out[15]=
The coefficients of the quadratic part are given in a rank 3 tensor.
In[16]:=
Click for copyable input
Out[16]=
This reproduces the original forms.
In[17]:=
Click for copyable input
Out[17]=

For machine-precision numerical sparse matrices, Mathematica supports standard file formats such as Matrix Market (.mtx) and Harwell-Boeing. You can import and export matrices in these formats using Import and Export.

New to Mathematica? Find your learning path »
Have a question? Ask support »