Creating convenient matrices

In class this week, we’ve been discussing linear independence and linear transformations. There’s not really any new computation in this material: all of the computations boil down to row-reducing matrices (to see if a set of vectors is linearly independent), or multiplying matrices by vectors (to compute a “matrix transformation”). So, this post will be brief, and mention some ways to construct some useful matrices in Sage (with a little Python programming thrown in).

The identity and zero matrices, and the zero vector
Two important matrices, which we’ve seen already and will see more of, are the zero matrix (all entries are zero) and the identity matrix (square, with ones down the diagonal). Sage has special code for creating these.

For the 3×5 zero matrix:
zero_matrix(3,5)

For the 5×5 identity matrix:
identity_matrix(5)

For the length 3 zero vector:
zero_vector(3)

Let’s make sure these have the effect we expect on a vector:
v = vector([1,2,3,4,5])
Z = zero_matrix(3,5)
Z*v
I = identity_matrix(5)
I*v
Z*v == zero_vector(3)

In the last case, Sage says “True”, because it is true that Z*v is equal to the length 3 zero vector. Note the difference between assigning a value to a variable, with a single equal sign (Z = zero_matrix(3,5)) and testing whether two objects are equal, with a double equal sign (Z*v == zero_vector(3)). Think of = as an orders, and == as a question.

Tricks for lists
We’ve been creating vectors by writing vector([1,2,3,4,5]); but what is the [1,2,3,4,5] itself? It’s a Python list — that is, a sequence of objects. You can make lists of anything — lists of vectors, lists of matrices, lists of lists. For example, here is a list of three matrices:
my_list = [matrix([[1,2],[3,4]]), identity_matrix(5),zero_matrix(2,2)]
my_list

(I found the output unreadable, so I turned on “typeset mode”.)

In fact, we have already been using lists of lists, when we write matrix([[1,2],[3,4]]). If you’re new to Python and would like to learn more about lists, try for example Section 3.2 of Dive Into Python.

You can do arithmetic with lists, but it is completely different from arithmetic with vectors. For example, try:

first_list = [1,2,3,4]
second_list = [5,6,7,8]
first_vector = vector(first_list)
second_vector = vector(second_list)
first_list + second_list
first_vector + second_vector
3*first_list
3*first_vector

List arithmetic can be very convenient. For example, here’s another way of defining the length 17 zero vector:
vector(17*[0])
vector(17*[0])==zero_vector(17)

Defining a rotation matrix function Sage (or rather, Python) is a programming language, so we can use it to write new functions. functions begin with the word “def”, and return a value using “return”. Here’s a function that takes in a number x and returns x+2:

def add_two(x):
     return x+2

Try it with:
add_two(17)

In Python, indentation is important: the indentation tells Python where the function starts and ends. Use the tab key to indent as needed. Also, notice the colon after add_two(x):

Our add_two function is not very impressive. Here’s a function which returns the matrix for rotation in the plane by an angle theta:

def rotation_matrix(theta):
     return matrix([[cos(theta),-sin(theta)],[sin(theta),cos(theta)]])

Let’s check that our rotation_matrix gives what we expect for some special theta’s:
rotation_matrix(0)
rotation_matrix(pi/3)
rotation_matrix(pi/2)
rotation_matrix(pi)
rotation_matrix(2*pi)
rotation_matrix(pi/2)*vector([1,0])
rotation_matrix(pi/2)*vector([1,1])

This entry was posted in Uncategorized. Bookmark the permalink. Both comments and trackbacks are currently closed.
Skip to toolbar