Mathematica Lab 7

MATH 220E Spring 2025

This Mathematica assignment should not be not long nor difficult. It is practice with using Mathematica to do things such as linear transformations and matrix algebra. Complete the problems below in a separate notebook and submit your work to the Mathematica Lab 7 assignment on Moodle.

Linear transformations

We saw how to create functions in Mathematica when we were playing with images in Mathematica Lab 3.  As a reminder, to define the function f(x)=(x^2), enter the following code.

f[x_]:=x^2

We can now evaluate our function f on any input we like:

f[4]

f[a]

Linear transformations T: (ℝ^n)→(ℝ^m) are simply functions that have more inputs (since a vector is made up of several numbers).

Suppose we want to define a linear transformation T:(ℝ^2)→ℝ by T([x,y])=3x+2y. (Keep in mind that ℝ is really (ℝ^1).) We could do this by defining a function T with two inputs (one for each coordinate of a two-dimensional vector) as follows.

T[x_,y_]:=3x+2y

Try out the function we just defined:

T[1,2]

Now, here’s an example of a linear transformation S:(ℝ^2)→(ℝ^3). We will use curly brackets for the output of this function since we want the output to be three values representing a vector in (ℝ^3).

S[x1_,x2_]:={x1+2x2,-3x1+x2,2x1-x2}

Let’s see what happens when we call our function on the input vector [1,2]:

S[1,2]

Multiplying matrices

Let’s review how to multiply matrices in Mathematica.

Caution: When defining a matrix, make sure you do not use store the MatrixForm version of the matrix. MatrixForm provides nice-looking output, but it causes problems if you try to do arithmetic on matrices that are stored using MatrixForm. So, make sure you input matrices using plain syntax of the form {{*,*,*},{*,*,*},...}.

To see this, compare the following computations, in which we attempt to multiply a matrix by 3. Notice what happens when we attempt to multiply 3 times MatrixForm.

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

3A

MatrixForm[3A]

3*MatrixForm[A]

Here’s another matrix we will use later:

B={{1,2,3,4},{2,-1,0,1},{3,1,-2,1}}

MatrixForm[B]

Remember: As we mentioned in class, to compute the matrix product AB, you must type A.B (using a period) rather than AB or A*B.

Here is the correct way to compute the matrix product AB. We can use the postfix operator (that is, //) to apply MatrixForm after computing the product.

A.B//MatrixForm

Since matrix A is 3×3 and matrix B is 3×4, we can compute the product A B (as above), but we cannot compute the product B A. Observe the error message we get if we try to compute B A. (Basically Mathematica tells us that our matrices don’t have sizes that allow them to be multiplied.)

B.A

If you just type AB with no dot, then Mathematica thinks you are referring to a variable named AB. If this variable is not defined in Mathematica’s memory, it colors it blue in the input code cell, and the output is just AB, like this:

AB//MatrixForm

If you use the multiplication symbol *, then Mathematica tries to multiply entries in corresponding positions. This is almost never what we want to do with matrices. It doesn’t work if the matrices have different sizes, instead producing the following error messages:

A*B//MatrixForm

To compute powers of a matrix, use the MatrixPower function. If you type something like A^5, Mathematica will simply raise each entry of the matrix to the 5th power, which isn’t what we want. You could also use the “period” operator many times, but this is cumbersome if the power is high. Observe two correct ways and one incorrect way of computing (A^5).

MatrixPower[A,5]//MatrixForm

A.A.A.A.A//MatrixForm

A^5//MatrixForm

Matrix inverses

As we mentioned in class, the function Inverse computes the inverse of a matrix.

The next matrix has an inverse. Let’s ask Mathematica to compute it.

M1={{1,2},{3,4}};
M1//MatrixForm

Inverse[M1]//MatrixForm

This next matrix does not have an inverse. What happens if we ask Mathematica to compute its inverse?

M2={{1,2},{2,4}};
M2//MatrixForm

Inverse[M2]//MatrixForm

What happens if we ask Mathematica to compute the inverse of a matrix that is not square?

M3={{1,2,3},{4,5,6}};
M3//MatrixForm

Inverse[M3]

Here is one more example: computing the inverse of a 3×3 matrix:

M4={{1,2,3},{4,5,6},{7,8,10}};
M4//MatrixForm

Inverse[M4]//MatrixForm

Random matrices

Mathematica can create matrices filled with random values. The function RandomInteger can create a random matrix with integer values, but we have to tell it what range to use for the random value and what size of matrix to create. Specifically, RandomInteger[{a,b},{m,n}] creates a matrix of m rows and n columns, where each entry is an integer between a and b (inclusive).

Here is an example:

R=RandomInteger[{-10,10},{5,5}]

MatrixForm[R]

Let's ask Mathematica to do some computations with our random matrix:

RowReduce[R]//MatrixForm

Inverse[R]//MatrixForm

The inverse matrix probably contains a lot of fractions. If you want to display these as decimal numbers, use the “numeric” function N, like this:

N[Inverse[R]]//MatrixForm

Note that every time you run RandomInteger, you will get a different matrix. If you close Mathematica, open it up again, and run RandomInteger, you will get a different matrices than what you got this time. (If you want to get the same random matrix again, you should set the “seed” of the random number generator, but that is beyond the scope of this activity.)

Problems

Solve the following problems in a new notebook. Make sure your solution to each problem is clearly labeled. Then submit your notebook to the Mathematica Lab 7 assignment on Moodle.

Create a random integer matrix of size 10×10 and call it R1. Then find the inverse of R1 (if it exists).

Create a random integer matrix of size 10×1000 and call it R2. It’s too big to display, but use the Dimensions function to confirm that it’s the correct size. Then print out its  first column (look back at Lab 2 if you forget how to do this.)

Compute the product (R1)(R2). Again, it’s too big to display, so just print out its dimensions and its first column.

Create a random integer matrix of size 1000×5 and call it R3. Again print out its dimensions.

Compute the product (R1)(R2)(R3) and display it nicely in matrix form. What are the domain and codomain of the linear transformation that multiplies a vector v by (R1)(R2)(R3)?

Create a random 2×2 integer matrix and call it M. Use a loop to calculate all the odd powers of M up to and including the 17th power. Let T be the linear transformation defined T(v)=M v. Is T one-to-one? Is T onto? How do you know?