Documentation / Mathematica / Add-ons & Links / Standard Packages / LinearAlgebra /

LinearAlgebra`MatrixManipulation`

This package includes functions for composing and separating matrices using rows, columns, and submatrices. All of the definitions involve simple combinations of built-in functions. Also included are functions for constructing a variety of special matrices.

Functions for combining matrices.

This loads the package.

In[1]:= << LinearAlgebra`MatrixManipulation`

Define a matrix.

In[2]:= a = {{a11, a12}, {a21, a22}}; MatrixForm[a]

Out[2]//MatrixForm=

Define a second matrix.

In[3]:= b = {{b11, b12}, {b21, b22}}; MatrixForm[b]

Out[3]//MatrixForm=

This constructs a matrix by combining the columns of the two matrices.

In[4]:= AppendColumns[a, b]

Out[4]=

Here is the same matrix displayed using MatrixForm.

In[5]:= MatrixForm[%]

Out[5]//MatrixForm=

A matrix can also be constructed by combining the rows of these matrices.

In[6]:= AppendRows[a, b]//MatrixForm

Out[6]//MatrixForm=

Here is a matrix constructed from submatrices a and b.

In[7]:= BlockMatrix[{{a, b}, {b, {{0, 0},
{0, 0}}}}]//MatrixForm

Out[7]//MatrixForm=

Functions for picking out pieces of matrices.

Here is a matrix.

In[8]:= mat = Array[m, {4, 4}]; MatrixForm[mat]

Out[8]//MatrixForm=

This takes the last two rows of the matrix. The same result is obtained with Take[mat, -2] or mat[[3, 4]].

In[9]:= TakeRows[mat, -2]//MatrixForm

Out[9]//MatrixForm=

This takes the second and third columns of the matrix. The same result is obtained with Take[mat, All, 2, 3] or mat[[All, 2, 3]].

In[10]:= TakeColumns[mat, {2, 3}]//MatrixForm

Out[10]//MatrixForm=

Here are the first three columns. The same result is obtained with Take[mat, All, 3] or mat[[All, 1, 2, 3]].

In[11]:= TakeColumns[mat, 3]//MatrixForm

Out[11]//MatrixForm=

This takes the submatrix between the element at position 2, 3 and the element at position 4, 4. The same result is obtained with Take[mat, 2, 4, 3, 4] or mat[[2, 3, 4, 3, 4]].

In[12]:= TakeMatrix[mat, {2, 3}, {4, 4}]//MatrixForm

Out[12]//MatrixForm=

Here is the same submatrix, specified as a matrix starting with the element at position 2, 3.

In[13]:= SubMatrix[mat, {2, 3}, {3, 2}]//MatrixForm

Out[13]//MatrixForm=

Square matrix test.

You can test whether matrices a, b, and mat are square.

In[14]:= Map[SquareMatrixQ, {a, b, mat}]

Out[14]=

Special matrices.

Here is an upper diagonal matrix with elements f[i, j].

In[15]:= UpperDiagonalMatrix[f, 3]//MatrixForm

Out[15]//MatrixForm=

Matrix elements can be specified using a pure function.

In[16]:= LowerDiagonalMatrix[#1 + #2 &, 3]//MatrixForm

Out[16]//MatrixForm=

Tridiagonal matrices can be constructed with a similar syntax.

In[17]:= TridiagonalMatrix[Plus, 4]//MatrixForm

Out[17]//MatrixForm=

Here is a Hilbert matrix.

In[18]:= HilbertMatrix[2, 4]//MatrixForm

Out[18]//MatrixForm=

The elements of the Hankel matrix can be given as a list.

In[19]:= HankelMatrix[{w, x, y, z}]//MatrixForm

Out[19]//MatrixForm=

The Hankel matrix can be filled with nonzero values by giving the final row. This also allows a non-square matrix to be generated. Note that the last element of the first column and the first element of the final row must be identical.

In[20]:= HankelMatrix[{w, x, y, z}, {z, m, n}]//MatrixForm

Out[20]//MatrixForm=

Creating matrices from a set of linear equations.

This extracts the matrix of coefficients and the vector of right-hand sides from a list of linear equations.

In[21]:= LinearEquationsToMatrices[
{a11 x + a12 y == c1,
a21 x + a22 y == c2}, {x, y}]

Out[21]=

Polar decomposition of a matrix.

This computes the polar decomposition of a matrix, then extracts the matrices u and s.

In[22]:= (mat = {{2., 0, 0}, {3., 4., 0}, {4., 5., 6.}};
{u, s} = PolarDecomposition[mat])

Out[22]=

This gives the identity matrix.

In[23]:= u . Transpose[Conjugate[u]] // Chop

Out[23]=

This gives the original matrix.

In[24]:= u . s // Chop

Out[24]=

LU decomposition of a matrix.

This computes the LU decomposition of a matrix, returning a combination of the lower and upper triangular matrices, a pivot permutation vector, and a condition number for the matrix.

In[25]:= {lu, p, cn} = LUDecomposition[mat]

Out[25]=

This gives the lower and upper triangular matrices explicitly.

In[26]:= {l, u} = LUMatrices[lu]

Out[26]=

You need to permute the original matrix to verify the decomposition.

In[27]:= mat[[p]] - l.u // Chop

Out[27]=

Functions related to Norm.

Mathematica defines functionality for computing norms of vectors and matrices in the kernel function Norm. The package extends this functionality by adding some useful utilities.

For the norm of the inverse of a matrix, you can explicitly compute the norm of the inverse of mat, but asymptotically, it is about three times faster to work with the LU decomposition of mat. Hence, either mat or the LU decomposition of mat may be an argument to InverseMatrixNorm.

Here is a random matrix.

In[28]:= dat = Table[Random[], {500}, {500}];

The 2-norm of the inverse can be computed directly.

In[29]:= Norm[Inverse[dat]]//Timing

Out[29]=

The LU-decomposition can be generated.

In[30]:= decomp = LUDecomposition[dat];

If we have the LU-decomposition already available, computing the norm of the inverse can be faster than the direct call by using InverseMatrixNorm.

In[31]:= InverseMatrixNorm[decomp, 2]//Timing

Out[31]=

Displaying matrix structure.

Matrix structure can be graphically displayed via the MatrixPlot functionality. The array is shown as a grid of black and white cells, by default representing zero-valued cells as white, and non-zero values as black. The center value can also be specified explicitly.

Display the structure of a random array.

In[32]:= MatrixPlot[Table[Random[Integer], {50},{50}]]

Out[32]=

Set up a sparse array.

In[33]:= idsparse = SparseArray[IdentityMatrix[100]]

Out[33]=

MatrixPlot is effective in displaying sparse array structure.

In[34]:= MatrixPlot[idsparse]

Out[34]=

The center value can be set to a specific value.

In[35]:= MatrixPlot[idsparse, 1]

Out[35]=

Options specific to MatrixPlot.

MatrixPlot accept the usual range of options for a graphics function. In addition, it takes the MaxMatrixSize option, specifying a maximum display size to use for the array. By default, this is set to . If the matrix dimensions are larger than this size, the matrix is downsampled to this size or less so that the output graphic size can be controlled. In this case, a darkened cell indicates that at least one of the covered cells has a non-central value. The MaxMatrixSize may be set to Infnity to prevent downsampling before display.

Display the sparse identity matrix with a maximum size of .

In[36]:= MatrixPlot[idsparse, MaxMatrixSize -> 4]

Out[36]=

Note that when displaying a matrix in this fashion, sampling effects from the graphic rendering or the downsampling procedure can distort the structure. Showing a graphic at a number of pixels that is not commensurate with the matrix dimensions can lead to fuzziness or other artifacts in the graphic, while downsampling effects can make a matrix seem denser than it really is.

When using a display size smaller than the actual matrix dimensions, the count of non-central values can be displayed via a color function.

Load statistical distributions.

In[37]:= <<Statistics`

Generate an array of and with varying distribution.

In[38]:= mat = Table[
Random[BernoulliDistribution[m * n]],
{n, 0, 1, 0.01}, {m, 0, 1, 0.01}];

Display the array.

In[39]:= MatrixPlot[mat]

Out[39]=

Display with a small matrix size. Note that the density of non-zero values is now obscured.

In[40]:= MatrixPlot[mat, MaxMatrixSize -> 20]

Out[40]=

Display with shading indicating the density of non-zero elements.

In[41]:= MatrixPlot[mat,
MaxMatrixSize -> 20,
ColorFunction -> (GrayLevel[1 - #] &),
ColorFunctionScaling -> True]

Out[41]=

The functionality can be applied to symbolic matrices as well as numeric matrices.

Display structure of a matrix composed of True and False.

In[42]:= MatrixPlot[Table[IntegerQ[Sqrt[n * m]],
{n, 10}, {m, 10}],
False]

Out[42]=