Chapter 3: Linear Transformations

MA 237

Richard Hitt

Matrix Transformations

We can watch what happens to the unit square in (R^2)under matrix transformations.  First, we need to describe the unit square parametrically.

x[t_]  :=  
        t  /;  0 ≤ t ≤ 
            1;
y[t_]  :=  
        0  /;  0 ≤ t ≤ 
            1;
x[t_]  :=  
        1  /;  1 ≤ t ≤ 
            2;
y[t_]  :=  
        t - 1  /;  
          1 ≤ t ≤ 2;
x[t_]  :=  
        3 - t  /;  
          2 ≤ t ≤ 3;
y[t_]  :=  
        1  /;  2 ≤ t ≤ 
            3;
x[t_]  :=  
        0  /;  3 ≤ t ≤ 
            4;
y[t_]  :=  4 - t  /;  3 ≤ t ≤ 4

ParametricPlot[{x[t], y[t]}, {t, 0, 4},  
      AspectRatio -> Automatic]

m = {{1,  .1}, { .1, 1}}

ParametricPlot[m . {x[t], y[t]}, {t, 0, 4},  
      AspectRatio -> Automatic]

In order to keep track of what gets transformed where, it would be better to color code the lines.

square = 
{
{RGBColor[1, 0, 0],  
          Line[{{0, 0}, {1, 0}}]}, 
{RGBColor[0, 1, 0],  
          Line[{{1, 0}, {1, 1}}]}, 
{RGBColor[0, 0, 1],  
          Line[{{1, 1}, {0, 1}}]}, 
{RGBColor[0, 0, 0],  
          Line[{{0, 1}, {0, 0}}]}
}

Show[Graphics[square],  AspectRatio -> Automatic]

Image[
        m_] := 
Show[
Graphics[
{
square, 
{
{RGBColor[1, 0, 0],  
                Line[{{0, 0} . Transpose[m], {1, 0} . 
                      Transpose[m]}]}, 
{RGBColor[0, 1, 
                  0],  Line[{{1, 0} . Transpose[m], {1, 1} . 
                      Transpose[m]}]}, 
{RGBColor[0, 0, 
                  1],  Line[{{1, 1} . Transpose[m], {0, 1} . 
                      Transpose[m]}]}, 
{RGBColor[0, 0, 
                  0],  Line[{{0, 1} . Transpose[m], {0, 0} . 
                      Transpose[
                        m]}]}
}
}
], 
AspectRatio -> 
          Automatic
]

m = {{1,  .1}, { .1, 1}}

Image[m]

Multiplication of Matrices

We can set Mathematica so that it displays matrices in a more human readable form using the following trick.

$PrePrint  =  If[MatrixQ[#],  MatrixForm[#],  #] &

So if we enter a matrix like

m = {{1, 2, 3}, {4, 5, 6}}

the result is displayed in matrix form rather than as a list of lists.

square3D = 
{
{RGBColor[1, 0, 0],  
          Line[{{0, 0, 0}, {1, 0, 0}}]}, 
{RGBColor[0, 1, 
            0],  Line[{{1, 0, 0}, {1, 1, 0}}]}, 
{RGBColor[
            0, 0, 1],  
          Line[{{1, 1, 0}, {0, 1, 0}}]}, 
{RGBColor[0, 0, 
            0],  Line[{{0, 1, 0}, {0, 0, 
                0}}]}
}

Show[Graphics3D[square3D], 
      BoxRatios -> {1, 1, 1}]

We can rotate the square by any given angle about the x-axis using the following matrix transformation.

m3D[θ_]  :=  {{1, 0, 0}, {0, 
          Cos[θ], -Sin[θ]}, {0, Sin[θ], 
          Cos[θ]}}

So, for example, if we use a specific angle, we get

m3D[Pi/6]

We can define

Image3D[m_, 
        opts___] := 
Show[
Graphics3D[
{
square3D, 
{
{RGBColor[1, 0, 0],  
                Line[{{0, 0, 0} . Transpose[m], {1, 0, 0} . 
                      Transpose[m]}]}, 
{RGBColor[0, 1, 
                  0],  Line[{{1, 0, 0} . Transpose[m], {1, 1, 0} . 
                      Transpose[m]}]}, 
{RGBColor[0, 0, 
                  1],  Line[{{1, 1, 0} . Transpose[m], {0, 1, 0} . 
                      Transpose[m]}]}, 
{RGBColor[0, 0, 
                  0],  Line[{{0, 1, 0} . Transpose[m], {0, 0, 0} . 
                      Transpose[
                        m]}]}
}
}
], 
opts
]

Image3D[m3D[Pi/6]]

We can spin the square around the x-axis by graphing many different images as θ varies from 0 to 2π.  This can be somewhat automated by loading an animation package.

<< Graphics`Animation`

This package contains an Animate command that we can use to assemble the different graphs in a table.  If you then select the entire table of output by clicking the mouse on the grouping bar on the right, Control-y (or Cell->Animate Selected Graphics) will begin the animation.  The animation controls are in the lower left of the window.

Animate[
      Image3D[m3D[theta], 
        PlotRange -> {{0, 1}, {-1, 1}, {-1, 1}}], {theta, 0, 
        2*Pi - Pi/24, Pi/24}]

Images

We will need the following package.

<< LinearAlgebra`MatrixManipulation`

You may find it useful to use the AppendRows or AppendColumns commands in the MatrixManipulation package.

Clear[M]
M = {{1, 2, 3}, {2, 4, 6}}

We can generate random numbers in the range 0 to 1 using the Random[] function (the range can be adjusted if desired).  So putting three of these Random commands together will generate random vectors in 3-space.  Then we can form their images my multiplying by M.  Since the list will be long, you can suppress the output using a semi-colon at the end.

images = 
        Table[M . {Random[], Random[], Random[]}, {i, 1, 1000}];

ListPlot[images]

This shows that if you take points out of the unit cube in 3-space, their images under M all lie on a straight line going through the origin in the plane.  
Notice that M has rank 1 and a 2-dimensional null space.  These facts can be illustrated as follows.

Write the first column of M as a row vector

col = {1, 2};
Clear[span]
span = 
        Table[Random[]*col,  {k, 1, 100}];
ListPlot[span]

basis = NullSpace[M]

The rows of this matrix form a basis for the nullspace of M, so we can depict random linear combinations of the rows to get a graphical representation of the nullspace.

Show[
Table[
Graphics3D[
Point[
Random[]*basis[[1]]  +  
              Random[]*
                basis[[2]]
], 
Axes -> True, 
AxesLabel -> {x, y, 
              z}
], 
{i, 1, 
          100}
]
]

Inverses

The inverse of a square matrix can be computed with the Inverse command.  We begin with a 5x5 matrix built from random integers selected between -10 and 10.

Clear[M]
M = 
        Table[Random[Integer, {-10, 10}], {i, 1, 5}, {j, 1, 
            5}];
M // MatrixForm

The inverse is:

Inverse[M] // MatrixForm

If you prefer a decimal approximation, try

Inverse[M] // MatrixForm // N

To check that we have the inverse, we can compute:

M . Inverse[M] // MatrixForm
Inverse[M] . M // MatrixForm

Solving Linear Systems

Here is a review of the different methods we have for solving linear systems in the context of Mathematica commands.  Much of this material comes directly from the Mathematica help facility.We begin with a 2x2 coefficient matrix.  Much of this material comes directly from the Mathematica help facility.

m = {{1, 5}, {2, 1}}

This gives two linear equations.

m . {x, y} == {a, b}

You can use Solve directly to solve these equations.

Solve[ %, {x, y} ]

You can also get the vector of solutions by calling LinearSolve. The result is equivalent to the one you get from Solve.

LinearSolve[m, {a, b}]

Another way to solve the equations is to invert the matrix m, and then multiply {a, b} by the inverse. This is not as efficient as using LinearSolve.

Inverse[m] . {a, b}

RowReduce performs a version of Gaussian elimination and can also be used to solve the equations.

RowReduce[{{1, 5, a}, {2, 1, b}}]

If you have a square matrix "m" of maximal rank (and thus nullspace dimension 0), then you can always find a unique solution to the matrix equation "m"."x"="b" for any "b". If, however, the matrix "m" has a non-trivial nullspace, then there may be either no vector, or an infinite number of vectors "x" which satisfy "m"."x"="b" for a particular "b". This occurs when the linear equations embodied in "m" are not independent.  
When "m" has non-trivial nullspace, then it is always possible to find non-zero vectors "x" that satisfy "m"."x"="0". The set of vectors "x" satisfying this equation form the null space of the matrix "m" by definition. Any of these vectors can be expressed as a linear combination of a particular set of basis vectors, which can be obtained using NullSpace[m].

Here is a simple matrix, corresponding to two identical linear equations.

m = {{1, 2}, {1, 2}}

LinearSolve cannot find a solution to the equation "m"."x"="b" in this case.

LinearSolve[m, {a, b}]

There is a single basis vector for the null space of m.

NullSpace[ m ]

Multiplying the basis vector for the null space by m gives the zero vector.

m . %[[1]]

Here is a simple symbolic matrix with determinant zero.

m = {{a, b, c}, {2 a, 2 b, 2 c}, {3 a, 3 b, 3 c}}

The basis for the null space of m contains two vectors. Any linear combination of these vectors gives zero when multiplied by m.

NullSpace[ m ]

An important feature of LinearSolve and NullSpace is that they work with rectangular, as well as square, matrices.  
When you represent a system of linear equations by a matrix equation of the form "m"."x"="b", the number of columns in "m" gives the number of variables, and the number of rows gives the number of equations. There are a number of cases.

({{"\!\(\*StyleBox[\"\\\"Underdetermined\\\"\", \"TI\"]\) ", "number of equations less than the number of variables; no solutions or many solutions may exist "}, {"\!\(\*StyleBox[\"\\\"Overdetermined\\\"\", \"TI\"]\) ", "number of equations more than the number of variables; solutions may or may not exist "}, {"\!\(\*StyleBox[\"\\\"Nonsingular\\\"\", \"TI\"]\) ", "number of independent equations equal to the number of variables, and determinant non-zero; a unique solution exists"}, {"\!\(\*StyleBox[\"\\\"Consistent\\\"\", \"TI\"]\) ", "at least one solution exists "}, {"\!\(\*StyleBox[\"\\\"Inconsistent\\\"\", \"TI\"]\) ", "no solutions exist "}})

Classes of linear systems represented by rectangular matrices.

This asks for the solution to the inconsistent set of equations x = 1 and x = 0.

LinearSolve[{{1}, {1}}, {1, 0}]

This matrix represents two equations, for three variables.

m = {{1, 3, 4}, {2, 1, 3}}

LinearSolve gives one of the possible solutions to this underdetermined set of equations.

v = LinearSolve[m, {1, 1}]

When a matrix represents an underdetermined system of equations, the matrix has a non-trivial null space. In this case, the null space is spanned by a single vector.

NullSpace[m]

If you take the solution you get from LinearSolve, and add any linear combination of the basis vectors for the null space, you still get a solution.

m . (v + 4 %[[1]])

Exercises

Here are the four On Line exercises from section 3.4 in the text translated in Mathematica.

Exercise 1

Let A be the matrix from exercise 2a on page 177.  Use  LinearSolve to solve the system A.X=B where

B = Transpose[{{2.1, 3.2, -4.4}}]

Exercise 2

Read the exercise in the text on page 182 and solve it in Mathematica.

Exercise 3

Again, read the problem and solve in Mathematica.  Be sure to enter the matrix using fractions rather than decimals to maintain the exactness in the calculations.

Exercise 4

In this problem, you may find it useful to measure the magnitude of a vector (also called the norm).  You can build a function for doing this.

Clear[norm,  v]
norm[v_]  :=  Sqrt[v . v]

Now you can work the problem.