Sparse Arrays: Manipulating Lists

Lists are normally specified in the Wolfram Language 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 the Wolfram System using SparseArray objects.

{e1,e2,}, {{e11,e12,},}, ordinary lists
SparseArray[{pos1->val1,pos2->val2,}]
sparse arrays

Ordinary lists and sparse arrays.

This specifies a sparse array.
In[1]:=
Click for copyable input
Out[1]=
Here it is as an ordinary list.
In[2]:=
Click for copyable input
Out[2]=
This specifies a two-dimensional sparse array.
In[3]:=
Click for copyable input
Out[3]=
Here it is an ordinary list of lists.
In[4]:=
Click for copyable input
Out[4]=
SparseArray[list]sparse array version of list
SparseArray[{pos1->val1,pos2->val2,}]
sparse array with values at positions
SparseArray[{pos1,pos2,}->{val1,val2,}]
the same sparse array
SparseArray[Band[{i,j}]->val]banded sparse array with values val
SparseArray[data,{d1,d2,}]×× sparse array
SparseArray[data,dims,val]sparse array with default value val
Normal[array]ordinary list version of array
ArrayRules[array]position-value rules for array

Creating and converting sparse arrays.

This generates a sparse array version of a list.
In[5]:=
Click for copyable input
Out[5]=
This converts back to an ordinary list.
In[6]:=
Click for copyable input
Out[6]=
This makes a length 7 sparse array with default value .
In[7]:=
Click for copyable input
Out[7]=
Here is the corresponding ordinary list.
In[8]:=
Click for copyable input
Out[8]=
This shows the rules used in the sparse array.
In[9]:=
Click for copyable input
Out[9]=
This creates a banded matrix.
In[10]:=
Click for copyable input
Out[10]//MatrixForm=

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

This specifies a 4×4 sparse array with at every position matching .
In[11]:=
Click for copyable input
Out[11]=
The result is a 4×4 identity matrix.
In[12]:=
Click for copyable input
Out[12]=
Here is an identity matrix with an extra element.
In[13]:=
Click for copyable input
Out[13]=
This makes the whole third column be .
In[14]:=
Click for copyable input
Out[14]=

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[15]:=
Click for copyable input
Out[15]=
You can have rules where values depend on indices.
In[16]:=
Click for copyable input
Out[16]=
This fills in even-numbered positions with .
In[17]:=
Click for copyable input
Out[17]=
You can use patterns involving alternatives.
In[18]:=
Click for copyable input
Out[18]=
You can also give conditions on patterns.
In[19]:=
Click for copyable input
Out[19]=
This makes a band-diagonal matrix.
In[20]:=
Click for copyable input
Out[20]=
Here is another way.
In[21]:=
Click for copyable input
Out[21]=

For many purposes, the Wolfram System 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, the Wolfram System will operate as if you had asked for parts in the corresponding ordinary list.

This generates a sparse array object.
In[22]:=
Click for copyable input
Out[22]=
Here is the corresponding ordinary list.
In[23]:=
Click for copyable input
Out[23]=
Parts of the sparse array are just like parts of the corresponding ordinary list.
In[24]:=
Click for copyable input
Out[24]=
This part has the default value 0.
In[25]:=
Click for copyable input
Out[25]=

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

This gives a sparse array.
In[26]:=
Click for copyable input
Out[26]=
Here is the corresponding ordinary list.
In[27]:=
Click for copyable input
Out[27]=
Dot works directly with sparse array objects.
In[28]:=
Click for copyable input
Out[28]=
You can mix sparse arrays and ordinary lists.
In[29]:=
Click for copyable input
Out[29]=

The Wolfram System 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[30]:=
Click for copyable input
Out[30]=
Here is the underlying optimized expression structure.
In[31]:=
Click for copyable input
Out[31]//InputForm=
Length gives the length of the corresponding ordinary list.
In[32]:=
Click for copyable input
Out[32]=
Map also operates on individual values.
In[33]:=
Click for copyable input
Out[33]=