SparseArrayThe main function for generating a sparse array is SparseArray. This can operate on a matrix to generate its sparse representation. In[1]:=  |
Out[2]=
|
In[3]:=  |
Out[3]//MatrixForm=
|
SparseArray can also take a list of rules showing the values for certain parts. In[4]:=  |
Out[4]=
|
In[5]:=  |
Out[5]//MatrixForm=
|
An equivalent syntax for SparseArray groups the indices and element values into their own lists. In[6]:=  |
Out[6]=
|
The results of the two forms of SparseArray are identical. In[7]:=  |
Out[7]=
|
A fuller discussion of the relative advantages of the two forms is given in the section Rule Inputs for SparseArray. SparseArray can also accept the dimensions of the matrix to be created. Here a 5x5 matrix is created even though the maximum explicit index was {2,3}. In[8]:=  |
Out[8]=
|
In[9]:=  |
Out[9]//MatrixForm=
|
In this example the default value is set to 1; typically the default value is 0. In[10]:=  |
Out[10]=
|
In[11]:=  |
Out[11]//MatrixForm=
|
Allowing the default value to be changed makes many element-wise operations very fast. They just need to work on the elements that are actually present and the default value. In[12]:=  |
Out[12]=
|
In[13]:=  |
Out[13]//MatrixForm=
|
You can also give patterns for the rules. This can often be a convenient way to build structured matrices. You can use the names of the patterns on the right-hand side of the rules. In[14]:=  |
Out[14]=
|
In[15]:=  |
Out[15]//MatrixForm=
|
Mathematica uses symbolic algebraic techniques to simplify certain of the patterns for building sparse arrays. This allows it to construct the sparse array without testing every potential element to see if it is actually present in the array, thus providing a significant saving in computational time. You can use the Mathematica function Random to generate sparse arrays with entries that are pseudorandom numbers. You can also use Random to generate the indices for the sparse array. In this example a 10 10 sparse matrix with at most 50 non-default entries is generated. In[16]:=  |
Out[17]//MatrixForm=
|
If you want to use Random on the right-hand side of the rules in sparse array, or any other expression that will evaluate, you should use RuleDelayed (entered with ⧴) to form the rules. If you do not do this, the same number will be used throughout the sparse matrix. In[18]:=  |
Out[19]//MatrixForm=
|
The general principle is that the rules you use with SparseArray work in the typical way for rules in Mathematica. Rule Inputs for SparseArrayThere are two different ways that rules can be used as input syntax for SparseArray. These are demonstrated below. In[1]:=  |
Both generate identical sparse arrays. In[3]:=  |
Out[3]=
|
The first form, which has many rules, is convenient if you want to mix explicit indices with patterns. It is also more readable for small examples. The second is more efficient, and is preferred if you only have explicit indices, for example, after reading data from a file. This uses SparseArray with many rules to mix explicit values with patterns. In[4]:=  |
Out[5]//MatrixForm=
|
This demonstrates the form of SparseArray that uses only one rule. The timing is measured to demonstrate performance. In[6]:=  |
Out[8]=
|
It is possible to convert the single rule to multiple rules using Thread. This takes more time than generating the sparse array. In[9]:=  |
Out[9]=
|
This uses the multiple rule form of SparseArray. It is slower than the single rule form. In[10]:=  |
Out[10]=
|
One reason why the single rule form of SparseArray is faster is that the indices and elements can use packed arrays, an efficient storage technology. If they are converted from packed arrays then SparseArray is slower as shown below. In[11]:=  |
Out[13]=
|
More information on packed arrays is found in a later section.
|