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

Matrix Multiplication
Matrix multiplication is carried out in Mathematica with the function Dot, which is typically entered with a dot short-hand syntax. The operation is fully supported for sparse arrays.
In[1]:=
Out[2]//MatrixForm=
The sparse matrix can be multiplied by itself; the result is also a sparse matrix.
In[3]:=
Out[3]=
In[4]:=
Out[4]//MatrixForm=
This multiplies a sparse matrix by a dense vector. In this case the result is dense.
In[5]:=
Out[5]=
If the vector is made into a sparse array, the result of the multiplication will be sparse.
In[6]:=
Out[6]=
In[7]:=
Out[7]//MatrixForm=
In general the result of matrix multiplication will be sparse if both arguments are sparse. This is not true if the default values are different.
In[8]:=
Out[8]=
What is particularly important about sparse matrix vector multiplication is that it is fast. In this example a 100000
100000 tri-diagonal sparse matrix is multiplied by a dense vector.
In[9]:=
In[11]:=
Out[11]=
Another issue with multiplication of sparse and dense matrices is that there are differences in the speed of computation depending on whether the dense matrix is on the left or the right.
In[12]:=
The first example is slower than the second, even though the result is the same in both cases.
In[14]:=
Out[14]=
In[15]:=
Out[15]=
One possibility to improve the speed is to convert the dense matrix to a sparse representation. Now the result is a sparse matrix.
In[16]:=
Out[17]=
Another solution would be to reverse the order by working with the transpose of each matrix.
In[18]:=
Out[18]=
Outer Product
The outer product is a way to build a higher rank tensor from ones of lower rank. Mathematica provides this functionality with the function Outer. One use of this is to combine two vectors to form a matrix as an outer product. This works for sparse vector input to generate a sparse array.
In[1]:=
Out[3]=
In[4]:=
Out[4]//MatrixForm=