From A First Course in Linear Algebra

Version 1.08

© 2004.

Licensed under the GNU Free Documentation License.

http://linear.ups.edu/

Matrices are input as lists of lists, since a list is a basic data structure in Mathematica. A matrix is a list of rows, with each row entered as a list. Mathematica uses braces (($\left\{\right.$ , $\left.\right\}$)) to delimit lists. So the input

$$a=\left\{\left\{1,2,3,4\right\},\left\{5,6,7,8\right\},\left\{9,10,11,12\right\}\right\}$$ |

would create a $3\times 4$ matrix named a that is equal to

$$\left[\begin{array}{cccc}\hfill 1\hfill & \hfill 2\hfill & \hfill 3\hfill & \hfill 4\hfill \\ \hfill 5\hfill & \hfill 6\hfill & \hfill 7\hfill & \hfill 8\hfill \\ \hfill 9\hfill & \hfill 10\hfill & \hfill 11\hfill & \hfill 12\hfill \end{array}\right]$$ |

To display a matrix named a “nicely” in Mathematica, type MatrixForm[a] , and the output will be displayed with rows and columns. If you just type a , then you will get a list of lists, like how you input the matrix in the first place.

If a is the name of a matrix in Mathematica, then the command RowReduce[a] will output the reduced row-echelon form of the matrix.

Mathematica will solve a linear system of equations using the LinearSolve[] command. The inputs are a matrix with the coefficients of the variables (but not the column of constants), and a list containing the constant terms of each equation. This will look a bit odd, since the lists in the matrix are rows, but the column of constants is also input as a list and so looks like a row rather than a column. The result will be a single solution (even if there are infinitely many), reported as a list, or the statement that there is no solution. When there are infinitely many, the single solution reported is exactly that solution used in the proof of Theorem RCLS, where the free variables are all set to zero, and the dependent variables come along with values from the final column of the row-reduced matrix.

As an example, Archetype A is

$$\begin{array}{llll}\hfill {x}_{1}-{x}_{2}+2{x}_{3}& =1\phantom{\rule{2em}{0ex}}& \hfill & \phantom{\rule{2em}{0ex}}\\ \hfill 2{x}_{1}+{x}_{2}+{x}_{3}& =8\phantom{\rule{2em}{0ex}}& \hfill & \phantom{\rule{2em}{0ex}}\\ \hfill {x}_{1}+{x}_{2}& =5\phantom{\rule{2em}{0ex}}& \hfill & \phantom{\rule{2em}{0ex}}\end{array}$$To ask Mathematica for a solution, enter

$$LinearSolve\left[\phantom{\rule{0ex}{0ex}}\left\{\left\{1,\phantom{\rule{0em}{0ex}}-1,\phantom{\rule{0em}{0ex}}2\right\},\left\{2,\phantom{\rule{0em}{0ex}}1,\phantom{\rule{0em}{0ex}}1\right\},\left\{1,\phantom{\rule{0em}{0ex}}1,\phantom{\rule{0em}{0ex}}0\right\}\right\},\phantom{\rule{0ex}{0ex}}\left\{1,\phantom{\rule{0em}{0ex}}8,\phantom{\rule{0em}{0ex}}5\right\}\phantom{\rule{0ex}{0ex}}\right]$$ |

and you will get back the single solution

$$\left\{3,\phantom{\rule{0em}{0ex}}2,\phantom{\rule{0em}{0ex}}0\right\}$$ |

We will see later how to coax Mathematica into giving us infinitely many solutions for this system (Computation VFSS.MMA).

Contributed by Robert Beezer

Vectors in Mathematica are represented as lists, written and displayed
horizontally. For example, the vector

$$v=\left[\begin{array}{c}\hfill 1\hfill \\ \hfill 2\hfill \\ \hfill 3\hfill \\ \hfill 4\hfill \end{array}\right]$$ |

would be entered and named via the command

$$v=\left\{1,\phantom{\rule{0em}{0ex}}2,\phantom{\rule{0em}{0ex}}3,\phantom{\rule{0em}{0ex}}4\right\}$$ |

Vector addition and scalar multiplication are then very natural. If u and v are two lists of equal length, then

$$2u+\left(-3\right)v$$ |

will compute the correct vector and return it as a list. If u and v have different sizes, then Mathematica will complain about “objects of unequal length.”

Given a matrix $A$, Mathematica will compute a set of column vectors whose span is the null space of the matrix with the NullSpace[] command. Perhaps not coincidentally, this set is exactly $\left\{\left.{z}_{j}\phantom{\rule{0em}{0ex}}\right|\phantom{\rule{0em}{0ex}}1\le j\le n-r\right\}$. However, Mathematica prefers to output the vectors in the opposite order than one we have chosen. Here’s a small example.

Begin with the $3\times 4$ matrix $A$, and its row-reduced version $B$,

$$\begin{array}{llllllllllll}\hfill A& =\left[\begin{array}{cccc}\hfill 1\hfill & \hfill 2\hfill & \hfill -1\hfill & \hfill 0\hfill \\ \hfill 3\hfill & \hfill 4\hfill & \hfill 1\hfill & \hfill -2\hfill \\ \hfill -1\hfill & \hfill 1\hfill & \hfill -5\hfill & \hfill 3\hfill \end{array}\right]\phantom{\rule{2em}{0ex}}& \hfill & \underset{}{\overset{\text{RREF}}{\to}}\phantom{\rule{2em}{0ex}}& \hfill B& =\left[\begin{array}{cccc}\hfill \text{1}\hfill & \hfill 0\hfill & \hfill 3\hfill & \hfill -2\hfill \\ \hfill 0\hfill & \hfill \text{1}\hfill & \hfill -2\hfill & \hfill 1\hfill \\ \hfill 0\hfill & \hfill 0\hfill & \hfill 0\hfill & \hfill 0\hfill \end{array}\right]\phantom{\rule{2em}{0ex}}& \hfill & \phantom{\rule{2em}{0ex}}& \hfill & \phantom{\rule{2em}{0ex}}& \hfill & \phantom{\rule{2em}{0ex}}\end{array}$$We could extract entries from $B$ to build the vectors ${z}_{1}$ and ${z}_{2}$ according to Theorem SSNS and describe $\mathcal{N}\phantom{\rule{0em}{0ex}}\left(A\right)$ as a span of the set $\left\{{z}_{1},\phantom{\rule{0em}{0ex}}{z}_{2}\right\}$. Instead, if $a$ has been set to $A$, then executing the command NullSpace[a] yields the list of lists (column vectors),

$$\begin{array}{lll}\hfill \left\{\left\{2,-1,0,1\right\},\left\{-3,2,1,0\right\}\right\}& \phantom{\rule{2em}{0ex}}& \hfill \end{array}$$Notice how our ${z}_{1}$ is second in the list. To “correct” this we can use a list-processing command from Mathematica, Reverse[] , as follows,

$$\begin{array}{lll}\hfill \text{Reverse[NullSpace[a]]}& \phantom{\rule{2em}{0ex}}& \hfill \end{array}$$and recieve the output in our preferred order. Give it a try yourself.

Suppose that $A$ is an $m\times n$ matrix and $b\in {\u2102}^{m}$ is a column vector. We might wish to find all of the solutions to the linear system $\mathcal{\mathcal{L}}\mathcal{S}\phantom{\rule{0em}{0ex}}\left(A,\phantom{\rule{0em}{0ex}}b\right)$. Mathematica’s LinearSolve[A, b] will return at most one solution (Computation LS.MMA). However, when the system is consistent, then this one solution reported is exactly the vector $c$, described in the statement of Theorem VFSLS.

The vectors ${u}_{j}$, $1\le j\le n-r$ of Theorem VFSLS are exactly the output of Mathematica’s NullSpace[] command, though Mathematica lists them in the opposite order from the order we have chosen. These are the same vectors listed as ${z}_{j}$, $1\le j\le n-r$ in Theorem SSNS. With $c$ produced from the LinearSolve[] command, and the ${u}_{j}$ coming from the NullSpace[] command we can use Mathematica’s symbolic manipulation commands to create an expression that describes all of the solutions.

Begin with the system $\mathcal{\mathcal{L}}\mathcal{S}\phantom{\rule{0em}{0ex}}\left(A,\phantom{\rule{0em}{0ex}}b\right)$. Row-reduce $A$ (Computation RR.MMA) and identify the free variables by determining the non-pivot columns. Suppose, for the sake of argument, that we have the three free variables ${x}_{3}$, ${x}_{7}$ and ${x}_{8}$. Then the following command will build an expression for an arbitrary solution:

$$\begin{array}{lll}\hfill \text{LinearSolve[A,b]+{x8,x7,x3}.NullSpace[A]}& \phantom{\rule{2em}{0ex}}& \hfill \end{array}$$Be sure to include the “dot” right before the NullSpace[] command — it has the effect of creating a linear combination of the vectors in the null space, using scalars that are symbols reminiscent of the variables.

A concrete example should help here. Suppose we want a solution set for the linear system with coefficient matrix $A$ and vector of constants $b$,

$$\begin{array}{llllllll}\hfill A& =\left[\begin{array}{ccccccc}\hfill 1\hfill & \hfill 2\hfill & \hfill 3\hfill & \hfill -5\hfill & \hfill 1\hfill & \hfill -1\hfill & \hfill 2\hfill \\ \hfill 2\hfill & \hfill 4\hfill & \hfill 0\hfill & \hfill 8\hfill & \hfill -4\hfill & \hfill 1\hfill & \hfill -8\hfill \\ \hfill 3\hfill & \hfill 6\hfill & \hfill 4\hfill & \hfill 0\hfill & \hfill -2\hfill & \hfill 5\hfill & \hfill 7\hfill \end{array}\right]\phantom{\rule{2em}{0ex}}& \hfill b& =\left[\begin{array}{c}\hfill 8\hfill \\ \hfill 1\hfill \\ \hfill -5\hfill \end{array}\right]\phantom{\rule{2em}{0ex}}& \hfill & \phantom{\rule{2em}{0ex}}& \hfill & \phantom{\rule{2em}{0ex}}\end{array}$$If we were to apply Theorem VFSLS, we would extract the components of $c$ and ${u}_{j}$ from the row-reduced version of the augmented matrix of the system (obtained with Mathematica, Computation RR.MMA),

$$\left[\begin{array}{cccccccc}\hfill \text{1}\hfill & \hfill 2\hfill & \hfill 0\hfill & \hfill 4\hfill & \hfill -2\hfill & \hfill 0\hfill & \hfill -5\hfill & \hfill 2\hfill \\ \hfill 0\hfill & \hfill 0\hfill & \hfill \text{1}\hfill & \hfill -3\hfill & \hfill 1\hfill & \hfill 0\hfill & \hfill 3\hfill & \hfill 1\hfill \\ \hfill 0\hfill & \hfill 0\hfill & \hfill 0\hfill & \hfill 0\hfill & \hfill 0\hfill & \hfill \text{1}\hfill & \hfill 2\hfill & \hfill -3\hfill \end{array}\right]$$ |

Instead, we will use this augmented matrix in reduced row-echelon form only to identify the free variables. In this example, we locate the non-pivot columns and see that ${x}_{2}$, ${x}_{4}$, ${x}_{5}$ and ${x}_{7}$ are free. If we have set $a$ to the coefficient matrix and $b$ to the vector of constants, then we execute the Mathematica command,

$$\begin{array}{lll}\hfill \text{LinearSolve[a,b]+{x7,x5,x4,x2}.NullSpace[a]}& \phantom{\rule{2em}{0ex}}& \hfill \end{array}$$As output we obtain the column vector (list),

$$\begin{array}{lll}\hfill \left[\begin{array}{c}\hfill 2-2\text{x2}-4\text{x4}+2\text{x5}+5\text{x7}\hfill \\ \hfill \text{x2}\hfill \\ \hfill 1+3\text{x4}-\text{x5}-3\text{x7}\hfill \\ \hfill \text{x4}\hfill \\ \hfill \text{x5}\hfill \\ \hfill -3-2\text{x7}\hfill \\ \hfill \text{x7}\hfill \end{array}\right]& \phantom{\rule{2em}{0ex}}& \hfill \end{array}$$

Mathematica has a built-in routine that will do the Gram-Schmidt procedure (Theorem GSPCV). The input is a set of vectors, which must be linearly independent. This is written as a list, containing lists that are the vectors. Let a be such a list of lists, containing the vectors ${v}_{i}$, $1\le i\le p$ from the statement of the theorem. You will need to first load the right Mathematica package — execute <<LinearAlgebra‘Orthogonalization‘ to make this happen. Then execute GramSchmidt[a] . The output will be another list of lists containing the vectors ${u}_{i}$, $1\le i\le p$ from the statement of the theorem. Mathematica will complain if you do not provide a linearly independent set as input (try it!).

An example. Suppose our linearly independent set (check this!) is

$$\begin{array}{lll}\hfill S=\left\{\left[\begin{array}{c}\hfill -1\hfill \\ \hfill 4\hfill \\ \hfill 1\hfill \\ \hfill 0\hfill \\ \hfill 3\hfill \end{array}\right],\phantom{\rule{0em}{0ex}}\left[\begin{array}{c}\hfill 0\hfill \\ \hfill 3\hfill \\ \hfill 0\hfill \\ \hfill 3\hfill \\ \hfill -3\hfill \end{array}\right],\phantom{\rule{0em}{0ex}}\left[\begin{array}{c}\hfill -1\hfill \\ \hfill 2\hfill \\ \hfill 0\hfill \\ \hfill -1\hfill \\ \hfill -2\hfill \end{array}\right],\phantom{\rule{0em}{0ex}}\left[\begin{array}{c}\hfill -1\hfill \\ \hfill -2\hfill \\ \hfill -3\hfill \\ \hfill 1\hfill \\ \hfill 4\hfill \end{array}\right],\phantom{\rule{0em}{0ex}}\left[\begin{array}{c}\hfill 1\hfill \\ \hfill 6\hfill \\ \hfill -1\hfill \\ \hfill 4\hfill \\ \hfill 6\hfill \end{array}\right]\right\}& \phantom{\rule{2em}{0ex}}& \hfill \end{array}$$The output of the GramSchmidt[] command will be the set,

$$\begin{array}{lll}\hfill T=\left\{\left[\begin{array}{c}\hfill -\frac{1}{3\sqrt{3}}\hfill \\ \hfill \frac{4}{3\sqrt{3}}\hfill \\ \hfill \frac{1}{3\sqrt{3}}\hfill \\ \hfill 0\hfill \\ \hfill \frac{1}{\sqrt{3}}\hfill \end{array}\right],\phantom{\rule{0em}{0ex}}\left[\begin{array}{c}\hfill \frac{1}{12\sqrt{15}}\hfill \\ \hfill \frac{23}{12\sqrt{15}}\hfill \\ \hfill -\frac{1}{12\sqrt{15}}\hfill \\ \hfill \frac{3\sqrt{\frac{3}{5}}}{4}\hfill \\ \hfill -\frac{\sqrt{\frac{5}{3}}}{2}\hfill \end{array}\right],\phantom{\rule{0em}{0ex}}\left[\begin{array}{c}\hfill -\frac{37}{4\sqrt{685}}\hfill \\ \hfill \frac{29}{4\sqrt{685}}\hfill \\ \hfill -\frac{3}{4\sqrt{685}}\hfill \\ \hfill -\frac{79}{4\sqrt{685}}\hfill \\ \hfill -\frac{5\sqrt{\frac{5}{137}}}{2}\hfill \end{array}\right],\phantom{\rule{0em}{0ex}}\left[\begin{array}{c}\hfill -\frac{337}{2\sqrt{120423}}\hfill \\ \hfill -\frac{37}{6\sqrt{120423}}\hfill \\ \hfill -\frac{1763}{6\sqrt{120423}}\hfill \\ \hfill \frac{337}{6\sqrt{120423}}\hfill \\ \hfill \frac{50}{\sqrt{120423}}\hfill \end{array}\right],\phantom{\rule{0em}{0ex}}\left[\begin{array}{c}\hfill \frac{23}{\sqrt{879}}\hfill \\ \hfill \frac{26}{3\sqrt{879}}\hfill \\ \hfill -\frac{44}{3\sqrt{879}}\hfill \\ \hfill -\frac{23}{3\sqrt{879}}\hfill \\ \hfill \frac{1}{\sqrt{879}}\hfill \end{array}\right]\right\}& \phantom{\rule{2em}{0ex}}& \hfill \end{array}$$Ugly, but true. At this stage, you might just as well be encouraged to think of the Gram-Schmidt procedure as a computational black box, linearly independent set in, orthogonal span-preserving set out.

To check that the output set is orthogonal, we can easily check the orthogonality of individual pairs of vectors. Suppose the output was set equal to b (say via b=GramSchmidt[a] ). We can extract the individual vectors of c as “parts” with syntax like c[[3]] , which would return the third vector in the set. When our vectors have only real number entries, we can accomplish an innerproduct with a “dot.” So, for example, you should discover that c[[3]].c[[5]] will return zero. Try it yourself with another pair of vectors.

Contributed by Robert Beezer

Suppose a is the name of a matrix stored in Mathematica. Then Transpose[a]
will create the transpose of a .

If $A$ and $B$ are matrices defined in Mathematica, then A.B will return the product of the two matrices (notice the dot between the matrices). If $A$ is a matrix and $v$ is a vector, then A.v will return the vector that is the matrix-vector product of $A$ and $v$. In every case the sizes of the matrices and vectors need to be correct.

Some examples:

$$\begin{array}{cc}\left\{\left\{1,\phantom{\rule{0em}{0ex}}2\right\},\phantom{\rule{0em}{0ex}}\left\{3,\phantom{\rule{0em}{0ex}}4\right\}\right\}.\left\{\left\{5,\phantom{\rule{0em}{0ex}}6,\phantom{\rule{0em}{0ex}}7\right\},\phantom{\rule{0em}{0ex}}\left\{8,\phantom{\rule{0em}{0ex}}9,\phantom{\rule{0em}{0ex}}10\right\}\right\}=\left\{\left\{21,\phantom{\rule{0em}{0ex}}24,\phantom{\rule{0em}{0ex}}27\right\},\phantom{\rule{0em}{0ex}}\left\{47,\phantom{\rule{0em}{0ex}}54,\phantom{\rule{0em}{0ex}}61\right\}\right\}& \\ \left\{\left\{1,\phantom{\rule{0em}{0ex}}2\right\},\phantom{\rule{0em}{0ex}}\left\{3,\phantom{\rule{0em}{0ex}}4\right\}\right\}.\left\{\left\{5\right\},\phantom{\rule{0em}{0ex}}\left\{6\right\}\right\}=\left\{\left\{17\right\},\phantom{\rule{0em}{0ex}}\left\{39\right\}\right\}& \\ \left\{\left\{1,\phantom{\rule{0em}{0ex}}2\right\},\phantom{\rule{0em}{0ex}}\left\{3,\phantom{\rule{0em}{0ex}}4\right\}\right\}.\left\{5,\phantom{\rule{0em}{0ex}}6\right\}=\left\{17,\phantom{\rule{0em}{0ex}}39\right\}& \end{array}$$Understanding the difference between the last two examples will go a long way to explaining how some Mathematica constructs work.

If $A$ is a matrix defined in Mathematica, then Inverse[A] will return the inverse of $A$, should it exist. In the case where $A$ does not have an inverse Mathematica will tell you the matrix is singular (see Theorem NI).