Sparse Arrays: Manipulating Lists
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.
| {e1,e2,...}, {{e11,e12,...},...}, ... | ordinary lists |
| SparseArray[{pos1->val1,pos2->val2,...}] |
| sparse arrays |
Ordinary lists and sparse arrays.
This specifies a sparse array.
| Out[1]= |  |
Here it is as an ordinary list.
| Out[2]= |  |
This specifies a two-dimensional sparse array.
| Out[3]= |  |
Here it is an ordinary list of lists.
| 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.
| Out[5]= |  |
This converts back to an ordinary list.
| Out[6]= |  |
This makes a length 7 sparse array with default value

.
| Out[7]= |  |
Here is the corresponding ordinary list.
| Out[8]= |  |
This shows the rules used in the sparse array.
| Out[9]= |  |
This creates a banded matrix.
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

.
| Out[11]= |  |
The result is a 4×4 identity matrix.
| Out[12]= |  |
Here is an identity matrix with an extra element.
| Out[13]= |  |
This makes the whole third column be

.
| 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.
| Out[15]= |  |
You can have rules where values depend on indices.
| Out[16]= |  |
This fills in even-numbered positions with

.
| Out[17]= |  |
You can use patterns involving alternatives.
| Out[18]= |  |
You can also give conditions on patterns.
| Out[19]= |  |
This makes a band-diagonal matrix.
| Out[20]= |  |
| Out[21]= |  |
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.
| Out[22]= |  |
Here is the corresponding ordinary list.
| Out[23]= |  |
Parts of the sparse array are just like parts of the corresponding ordinary list.
| Out[24]= |  |
This part has the default value 0.
| Out[25]= |  |
Many operations treat
SparseArray objects just like ordinary lists. When possible, they give sparse arrays as results.
This gives a sparse array.
| Out[26]= |  |
Here is the corresponding ordinary list.
| Out[27]= |  |
Dot works directly with sparse array objects.
| Out[28]= |  |
You can mix sparse arrays and ordinary lists.
| Out[29]= |  |
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.
| Out[30]= |  |
Here is the underlying optimized expression structure.
Out[31]//InputForm= |
| |  |
Length gives the length of the corresponding ordinary list.
| Out[32]= |  |
Map also operates on individual values.
| Out[33]= |  |