Matrix algebra 1

This week, we talked about matrix addition, scalar multiplication, matrix multiplication, and the transpose. These all work pretty much how you would expect in Sage:

Let’s create some matrices to play with:
%typeset_mode True
M = matrix([[1,2],[3,4],[5,6]])
N = matrix([[3,1],[4,1],[5,9]])
P = matrix([[2,7],[1,8]])
I = identity_matrix(3)
Z = zero_matrix(3,2)
R = random_matrix(ZZ, 2, 2)
[M, N, P, I, R]

We’ve played with all of the commands except random_matrix, which produces a pseudo-random matrix of a given size. In that command, the ZZ says we want the entries to be integers: Z stands for Zahlen, German for “numbers”. (The function random_matrix has lots of options; if you want a description, type “random_matrix?” and press return.) The last line is just so we get a printout of the matrices.

Now, we can do some arithmetic:
M + N
M - N
3*M
M + P
M + Z
M * R
I * M
R * M
M * N

Some of those lines gave errors; hopefully you know why.

To get the transpose of M:
M.transpose()

Let’s test some matrix arithmetic rules, using random matrices (like the book suggests in problems 36 – 39 in Section 2.1).

A = random_matrix(ZZ,3,3)
B = random_matrix(ZZ,3,3)
[A, B]
A*B
B*A
A*B == B*A
A*(3*B)
3*(A*B)
A.transpose()*B.transpose()
(A*B).transpose()
B.transpose() * A.transpose()
(A*B).transpose() == A.transpose()*B.transpose()
(A*B).transpose() == B.transpose()*A.transpose()

Here’s a screenshot:

Blog3aBlog3b

Of course, your answers will look different: you should have different random matrices. Given that, though, was the behavior what you expected? (If not, review the book or come and talk to me—Sage is right.)

One last word of caution: if you’re not sure what order Sage will apply operations, use parentheses. For example, A*B.transpose() and (A*B).transpose() are different: the transpose takes precedence over the multiplication. (Try it and check.)

Post a Comment

Your email address is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Skip to toolbar