Wolfram ResearchPRODUCTSPURCHASEFOR USERSCOMPANYOUR SITES
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.

Documentation  / Mathematica / Built-in Functions  / Advanced Documentation / Linear Algebra / Sparse Arrays /

 

Basic Operations

The basic object for representing a sparse matrix in Mathematica is a SparseArray.

A SparseArray object can be created by giving a list of those elements that are nonzero.

In[1]:=

Out[1]=

By default, a sparse matrix will print with a special output format. You can see the matrix that the sparse array represents by using MatrixForm.

In[2]:=

Out[2]//MatrixForm=

Operations on sparse matrices are all equivalent to the operations on dense matrices. For example, arithmetic is supported and a sparse array is the result.

In[3]:=

Out[3]=

In[4]:=

Out[4]//MatrixForm=

Listable operations also work on sparse arrays to thread over all elements.

In[5]:=

Out[5]=

In[6]:=

Out[6]//MatrixForm=

All combinations of matrix multiplication using the function Dot are supported. This demonstrates the dot product of two sparse arrays.

In[7]:=

Out[7]=

In[8]:=

Out[8]//MatrixForm=

This demonstrates the dot product of a sparse array with a dense vector.

In[9]:=

Out[9]=

The dot product of a sparse array with a dense matrix is supported.

In[10]:=

Out[10]=

Sparse representations are useful because they do not store every element. If one particular value, typically this is zero, appears many times in the sparse array, it can be much more efficient if only elements that are different from this common value are stored. The default output format shows the number of non-default elements and the dimensions.

In[11]:=

Out[11]=

If a single number is added to the sparse array, it is added to all elements and also to the default element, which was zero. Now that the default element is no longer zero but 1.5, it is shown in the output.

In[12]:=

Out[12]=

If the N command is applied to a sparse matrix, it works on all the elements. This builds an example 3Cross3 sparse matrix.

In[13]:=

Out[14]//MatrixForm=

When N is applied to the sparse matrix, the result is a sparse matrix with elements (including the default element) that are all approximate machine numbers.

In[15]:=

Out[15]//MatrixForm=

Here N with a precision argument is applied to the matrix. This generates a sparse matrix of approximate real numbers with 20 digits of precision. Note that N[0,20] is still 0.

In[16]:=

Out[16]//MatrixForm=

SparseArray

The 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 10Cross10 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 SparseArray

There 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.

Identity and Diagonal Sparse Matrices

Mathematica does not provide special functions for generating sparse identity or sparse diagonal matrices because they are so easy to build with SparseArray.

This generates a 4Cross4 sparse identity matrix.

In[1]:=

Out[2]//MatrixForm=

This generates a 4Cross4 sparse diagonal matrix.

In[3]:=

Out[5]//MatrixForm=

If you want to compute with floating point numbers it can be advantageous to use matrices that contain floating point entries; this is described in more detail in the section Performance: Matrix Contents. If you start with an integer matrix, such as the identity matrix generated previously, it can be converted to a floating point matrix by using the function N. This is shown in the following.

In[6]:=

Out[7]//MatrixForm=

An alternative would be to create the initial sparse identity matrix using floating point entries.

In[8]:=

Out[9]//MatrixForm=

The different types of matrix that Mathematica can work with are described in more detail in the section Matrix Types.

Normal

To convert from a sparse array to the dense object that it represents, you can use Normal.

In[1]:=

Out[1]=

In[2]:=

Out[2]=

Of course, it is very straightforward to make a sparse array that cannot be represented on your system. For example, the following sparse matrix only has two nonzero elements, but it has 2499999998 zero elements.

In[3]:=

Out[3]=

If this is converted to a dense matrix, an exception is thrown because it is not possible to represent this on typical computers.

In[4]:=

Out[4]=

ArrayRules

SparseArray can accept a list of rules to form a sparse array. These rules hold the indices and values of nonzero elements. In the following example, the element at position {2,1} has the value 5. ArrayRules generates the rules for a sparse array.

In[1]:=

Out[1]=

ArrayRules returns the rules that represent the sparse array. It returns a rule of blank patterns {_,_} to show the default value.

In[2]:=

Out[2]=

When a scalar is added to the sparse array, the default value is changed. For example, here the default value is 5.

In[3]:=

Out[3]=

ArrayRules can take a second argument, which is the default value shown on output. Here, a default rule with value 5 is used. Because there is only one element with this value, the list of rules is much longer.

In[4]:=

Out[4]=

ArrayRules is a useful way to get information about a sparse array such as the non-default elements or the default value. This example shows how to get the indices of the non-default elements.

In[5]:=

Out[5]=



Any questions about topics on this page? Click here to get an individual response.Buy NowMore Information


 © 2009 Wolfram Research, Inc.  Terms of Use  Privacy Policy |
Sign up for our newsletter: