This is documentation for Mathematica 5, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)

Documentation / Mathematica / The Mathematica Book / Principles of Mathematica / Manipulating Lists /

2.4.5 Sparse Arrays

Lists are normally specified in Mathematica just by giving explicit lists of their elements. But particularly in working with large arrays, it is often useful instead to be able to say what the values of elements are only at certain positions, with all other elements taken to have a default value, usually zero. You can do this in Mathematica using SparseArray objects.

Ordinary lists and sparse arrays.

This specifies a sparse array.

In[1]:= SparseArray[{2->a, 5->b}]

Out[1]=

Here it is as an ordinary list.

In[2]:= Normal[%]

Out[2]=

This specifies a two-dimensional sparse array.

In[3]:= SparseArray[{{1,2}->a, {3,2}->b, {3,3}->c}]

Out[3]=

Here it is an ordinary list of lists.

In[4]:= Normal[%]

Out[4]=

Creating and converting sparse arrays.

This generates a sparse array version of a list.

In[5]:= SparseArray[{a, b, c, d}]

Out[5]=

This converts back to an ordinary list.

In[6]:= Normal[%]

Out[6]=

This makes a length 7 sparse array with default value x.

In[7]:= SparseArray[{3->a, 5->b}, 7, x]

Out[7]=

Here is the corresponding ordinary list.

In[8]:= Normal[%]

Out[8]=

This shows the rules used in the sparse array.

In[9]:= ArrayRules[%%]

Out[9]=

An important feature of SparseArray is that the positions you specify can be patterns.

This specifies a sparse array with 1 at every position matching {i_, i_}.

In[10]:= SparseArray[{i_, i_} -> 1, {4, 4}]

Out[10]=

The result is a identity matrix.

In[11]:= Normal[%]

Out[11]=

Here is an identity matrix with an extra element.

In[12]:= Normal[SparseArray[{{1, 3}->a, {i_, i_}->1}, {4, 4}]]

Out[12]=

This makes the whole third column be a.

In[13]:= Normal[SparseArray[{{_, 3}->a, {i_, i_}->1}, {4, 4}]]

Out[13]=

You can think of SparseArray[rules] as taking all possible position specifications, then applying rules to determine values in each case. As usual, rules given earlier in the list will be tried first.

This generates a random diagonal matrix.

In[14]:= Normal[SparseArray[{{i_, i_} :> Random[]}, {3, 3}]]

Out[14]=

You can have rules where values depend on indices.

In[15]:= Normal[SparseArray[i_ -> i^2, 10]]

Out[15]=

This fills in even-numbered positions with p.

In[16]:= Normal[SparseArray[{_?EvenQ->p, i_->i^2}, 10]]

Out[16]=

You can use patterns involving alternatives.

In[17]:= Normal[SparseArray[{1|3, 2|4}->a, {4, 4}]]

Out[17]=

You can also give conditions on patterns.

In[18]:= Normal[SparseArray[i_/;3<i<7 -> p, 10]]

Out[18]=

This makes a band-diagonal matrix.

In[19]:= Normal[SparseArray[{{i_, j_} /;
Abs[i - j] < 2 -> i + j}, {5, 5}]]

Out[19]=

For many purposes, Mathematica treats SparseArray objects just like the ordinary lists to which they correspond. Thus, for example, if you ask for parts of a sparse array object, Mathematica will operate as if you had asked for parts in the corresponding ordinary list.

This generates a sparse array object.

In[20]:= s = SparseArray[{2->a, 4->b, 5->c}, 10]

Out[20]=

Here is the corresponding ordinary list.

In[21]:= Normal[s]

Out[21]=

Parts of the sparse array are just like parts of the corresponding ordinary list.

In[22]:= s[[2]]

Out[22]=

This part has the default value 0.

In[23]:= s[[3]]

Out[23]=

Many operations treat SparseArray objects just like ordinary lists. When possible, they give sparse arrays as results.

This gives a sparse array.

In[24]:= 3 s + x

Out[24]=

Here is the corresponding ordinary list.

In[25]:= Normal[%]

Out[25]=

Dot works directly with sparse array objects.

In[26]:= s . s

Out[26]=

You can mix sparse arrays and ordinary lists.

In[27]:= s . Range[10]

Out[27]=

Mathematica represents sparse arrays as expressions with head SparseArray. Whenever a sparse array is evaluated, it is automatically converted to an optimized standard form with structure SparseArray[Automatic, dims, val, ... ].

This structure is, however, rarely evident, since even operations like Length are set up to give results for the corresponding ordinary list, not for the raw SparseArray expression structure.

This generates a sparse array.

In[28]:= t = SparseArray[{1->a, 5->b}, 10]

Out[28]=

Here is the underlying optimized expression structure.

In[29]:= InputForm[%]

Out[29]//InputForm= SparseArray[Automatic, {10}, 0, {1, {{0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2}, {}}, {a, b}}]

Length gives the length of the corresponding ordinary list.

In[30]:= Length[t]

Out[30]=

Map also operates on individual values.

In[31]:= Normal[Map[f, t]]

Out[31]=