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}]


Here it is as an ordinary list.

In[2]:= Normal[%]


This specifies a two-dimensional sparse array.

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


Here it is an ordinary list of lists.

In[4]:= Normal[%]


Creating and converting sparse arrays.

This generates a sparse array version of a list.

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


This converts back to an ordinary list.

In[6]:= Normal[%]


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

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


Here is the corresponding ordinary list.

In[8]:= Normal[%]


This shows the rules used in the sparse array.

In[9]:= ArrayRules[%%]


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}]


The result is a identity matrix.

In[11]:= Normal[%]


Here is an identity matrix with an extra element.

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


This makes the whole third column be a.

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


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}]]


You can have rules where values depend on indices.

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


This fills in even-numbered positions with p.

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


You can use patterns involving alternatives.

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


You can also give conditions on patterns.

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


This makes a band-diagonal matrix.

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


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]


Here is the corresponding ordinary list.

In[21]:= Normal[s]


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

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


This part has the default value 0.

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


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


Here is the corresponding ordinary list.

In[25]:= Normal[%]


Dot works directly with sparse array objects.

In[26]:= s . s


You can mix sparse arrays and ordinary lists.

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


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]


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]


Map also operates on individual values.

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