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

Converting Equations to Sparse Arrays
Matrices are so important in many areas of science and technology because they are an efficient way to represent linear systems of equations. Mathematica is unique among technical computing systems in that it combines very efficient ways to work with matrices and also with the equations that the matrices represent. It is easy to go from a matrix to a system of equations and back. Here, a sparse matrix is multiplied by a vector of the unknowns and a system of equations is formed.
In[1]:=
Out[2]=
In[3]:=
Out[3]//MatrixForm=
These equations can be solved with the algebraic equation solver, Solve.
In[4]:=
Out[4]=
However, it is a bit more involved to go from the equation to the matrix representation. Mathematica provides the function CoefficientArrays to make this transformation easier. This takes the equations that were generated above; the result is a list of a sparse vector and a sparse matrix.
In[5]:=
Out[5]=
You can see the details of the sparse arrays with MatrixForm.
In[6]:=
Out[6]=
CoefficientArrays is general and will work for non-linear as well as linear polynomial equations.
In[7]:=
Out[7]=
When the input is a non-linear polynomial, the result will include higher rank tensors.
In[8]:=
Out[8]=
You can still regain the original expressions with dot products.
In[9]:=
Out[9]=
The ability to work directly with systems of linear equations can be very advantageous for certain applications. For example, generating finite difference solutions. This is demonstrated in the example Finite Difference Solutions.