Section MMA  Mathematica

From A First Course in Linear Algebra
Version 2.00
© 2004.
Licensed under the GNU Free Documentation License.
http://linear.ups.edu/

Computation Note ME.MMA: Matrix Entry

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 ((  ,  )) to delimit lists. So the input

a = {{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12}}

would create a 3 × 4 matrix named a that is equal to

1 2 3 4 5 6 7 8 9 101112

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.

Computation Note RR.MMA: Row Reduce

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.

Computation Note LS.MMA: Linear Solve

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

x1 x2 + 2x3 = 1 2x1 + x2 + x3 = 8 x1 + x2 = 5

To ask Mathematica for a solution, enter

LinearSolve[{{1, 1,2},{2,1,1},{1,1,0}},{1,8,5}]

and you will get back the single solution

{3,2,0}

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

Computation Note VLC.MMA: Vector Linear Combinations

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

v = 1 2 3 4

would be entered and named via the command

v = {1,2,3,4}

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

2u + (3)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.”

Computation Note NS.MMA: Null Space

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 zj 1 j n r. 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 × 4 matrix A, and its row-reduced version B,

A = 1 21 0 3 4 1 2 115 3  RREF B = 10 3 2 0 12 1 0 0 0 0

We could extract entries from B to build the vectors z1 and z2 according to Theorem SSNS and describe NA as a span of the set z1,z2. Instead, if a has been set to A, then executing the command NullSpace[a] yields the list of lists (column vectors),

{{2,1, 0, 1},{3, 2, 1, 0}}

Notice how our z1 is second in the list. To “correct” this we can use a list-processing command from Mathematica, Reverse[] , as follows,

  Reverse[NullSpace[a]] 

and recieve the output in our preferred order. Give it a try yourself.

Computation Note VFSS.MMA: Vector Form of Solution Set

Suppose that A is an m × n matrix and b m is a column vector. We might wish to find all of the solutions to the linear system SA,b. 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 uj, 1 j 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 zj, 1 j n r in Theorem SSNS. With c produced from the LinearSolve[] command, and the uj 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 SA,b. 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 x3, x7 and x8. Then the following command will build an expression for an arbitrary solution:

  LinearSolve[A,b]+{x8,x7,x3}.NullSpace[A] 

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,

A = 1235 1 1 2 2 40 8 4 1 8 3 64 0 2 5 7 b = 8 1 5

If we were to apply Theorem VFSLS, we would extract the components of c and uj from the row-reduced version of the augmented matrix of the system (obtained with Mathematica, Computation RR.MMA),

120 4 205 2 0 013 1 0 3 1 0 00 0 0 1 2 3

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 x2, x4, x5 and x7 are free. If we have set a to the coefficient matrix and b to the vector of constants, then we execute the Mathematica command,

  LinearSolve[a, b]+{x7, x5, x4, x2}.NullSpace[a] 

As output we obtain the column vector (list),

2 2  x2  4  x4  + 2  x5  + 5  x7   x2  1 + 3  x4  x5  3  x7   x4   x5  3 2  x7   x7 

Computation Note GSP.MMA: Gram-Schmidt Procedure

Mathematica has a built-in routine that will do the Gram-Schmidt procedure (Theorem GSP). 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 vi, 1 i 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 ui, 1 i 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

S = 1 4 1 0 3 , 0 3 0 3 3 , 1 2 0 1 2 , 1 2 3 1 4 , 1 6 1 4 6

The output of the GramSchmidt[] command will be the set,

T = 1 33 4 33 1 33 0 1 3 , 1 1215 23 1215 1 1215 33 5 4 5 3 2 , 37 4685 29 4685 3 4685 79 4685 5 5 137 2 , 337 2120423 37 6120423 1763 6120423 337 6120423 50 120423 , 23 879 26 3879 44 3879 23 3879 1 879

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.

Computation Note TM.MMA: Transpose of a Matrix

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

Computation Note MM.MMA: Matrix Multiplication

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:

{{1,2},{3,4}}.{{5,6,7},{8,9,10}} = {{21,24,27},{47,54,61}} {{1,2},{3,4}}.{{5},{6}} = {{17},{39}} {{1,2},{3,4}}.{5,6} = {17,39}

Understanding the difference between the last two examples will go a long way to explaining how some Mathematica constructs work.

Computation Note MI.MMA: Matrix Inverse

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