Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download
Views: 3768

Elementary Matrices and Inverses

Matrix multiplication in Sage is easy: Define your matrices (don't forget to specify which field to use), then just use * to multiply the matrices. Here are some matrices to play with:

A = matrix(QQ, [[1, 2, 3, 4], [1, 0, 1, 0], [4, 3, 2, 1]]); B = matrix(QQ, [[-1, -2, 0, 4], [5, 4, 3, 2], [0, 0, 0, 1], [3, 2, 1, 0]]); C = matrix(QQ, [[1, 2, 3], [-3, -2, -1], [1, 0, 1], [4, 5, 6]]); show(A) show(B) show(C)
(123410104321)\displaystyle \left(\begin{array}{rrrr} 1 & 2 & 3 & 4 \\ 1 & 0 & 1 & 0 \\ 4 & 3 & 2 & 1 \end{array}\right)
(1204543200013210)\displaystyle \left(\begin{array}{rrrr} -1 & -2 & 0 & 4 \\ 5 & 4 & 3 & 2 \\ 0 & 0 & 0 & 1 \\ 3 & 2 & 1 & 0 \end{array}\right)
(123321101456)\displaystyle \left(\begin{array}{rrr} 1 & 2 & 3 \\ -3 & -2 & -1 \\ 1 & 0 & 1 \\ 4 & 5 & 6 \end{array}\right)

Exercise: Which of the following is defined? What dimensions should the answers be? Check your results by running the Sage commands.

A*B
[21 14 10 11] [-1 -2 0 5] [14 6 10 24]
B*A
Error in lines 1-1 Traceback (most recent call last): File "/projects/a3d70958-1d9a-405a-9d15-eecd3730cfe0/.sagemathcloud/sage_server.py", line 879, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> File "sage/structure/element.pyx", line 2950, in sage.structure.element.Matrix.__mul__ (build/cythonized/sage/structure/element.c:23198) return coercion_model.bin_op(left, right, mul) File "sage/structure/coerce.pyx", line 850, in sage.structure.coerce.CoercionModel_cache_maps.bin_op (build/cythonized/sage/structure/coerce.c:8045) raise TypeError, arith_error_message(x,y,op) TypeError: unsupported operand parent(s) for '*': 'Full MatrixSpace of 4 by 4 dense matrices over Rational Field' and 'Full MatrixSpace of 3 by 4 dense matrices over Rational Field'
C*A
[ 15 11 11 7] [ -9 -9 -13 -13] [ 5 5 5 5] [ 33 26 29 22]

We can make an n×nn \times n identity matrix in sage with the command identity_matrix(QQ, n) (for a rational matrix). Here's a 4×44 \times 4 identity matrix:

ident4 = identity_matrix(QQ, 4); ident4
[1 0 0 0] [0 1 0 0] [0 0 1 0] [0 0 0 1]

As we have seen, multiplication by an identity matrix does not change anything (when the multiplication is defined).

Exercise: Predict what the output of the following should be, and test by running the computations.

A*ident4
[1 2 3 4] [1 0 1 0] [4 3 2 1]
ident4*A
Error in lines 1-1 Traceback (most recent call last): File "/projects/a3d70958-1d9a-405a-9d15-eecd3730cfe0/.sagemathcloud/sage_server.py", line 879, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> File "sage/structure/element.pyx", line 2950, in sage.structure.element.Matrix.__mul__ (build/cythonized/sage/structure/element.c:23198) return coercion_model.bin_op(left, right, mul) File "sage/structure/coerce.pyx", line 850, in sage.structure.coerce.CoercionModel_cache_maps.bin_op (build/cythonized/sage/structure/coerce.c:8045) raise TypeError, arith_error_message(x,y,op) TypeError: unsupported operand parent(s) for '*': 'Full MatrixSpace of 4 by 4 dense matrices over Rational Field' and 'Full MatrixSpace of 3 by 4 dense matrices over Rational Field'
B*ident4
[-1 -2 0 4] [ 5 4 3 2] [ 0 0 0 1] [ 3 2 1 0]
ident4*B
[-1 -2 0 4] [ 5 4 3 2] [ 0 0 0 1] [ 3 2 1 0]
C*ident4
Error in lines 1-1 Traceback (most recent call last): File "/projects/a3d70958-1d9a-405a-9d15-eecd3730cfe0/.sagemathcloud/sage_server.py", line 879, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> File "sage/structure/element.pyx", line 2950, in sage.structure.element.Matrix.__mul__ (build/cythonized/sage/structure/element.c:23198) return coercion_model.bin_op(left, right, mul) File "sage/structure/coerce.pyx", line 850, in sage.structure.coerce.CoercionModel_cache_maps.bin_op (build/cythonized/sage/structure/coerce.c:8045) raise TypeError, arith_error_message(x,y,op) TypeError: unsupported operand parent(s) for '*': 'Full MatrixSpace of 4 by 3 dense matrices over Rational Field' and 'Full MatrixSpace of 4 by 4 dense matrices over Rational Field'
ident4*C
[ 1 2 3] [-3 -2 -1] [ 1 0 1] [ 4 5 6]

Elementary Matrices

When we perform a row operation on an identity matrix, we get a very special matrix referred to a an elementary matrix. Recall that you can perform row operations on matrices in Sage using the following commands: (Each of these operations will replace the matrix mprime with the new matrix which results.)

  • .swap_rows(row1, row2): This interchanges row1 and row2, where row1 and row2 are row numbers.

  • .rescale_row(row, scalar): This multiplies row by the number scalar.

  • .add_multiple_of_row(targetrow, startrow, scalar): Adds scalar times startrow to targetrow.

Important Note: Sage numbers rows (and entries) starting from zero, so what we call row 1, it calls row 0. (I admit that this is confusing for most people staring out, but it is a standard convention in many computer languages.)

One elementary matrix is the permutation matrix, which is the result of swapping two rows. Below we create a 3×33 \times 3 elementary matrix by swapping the first and second rows of the identity matrix:

iswap = identity_matrix(QQ, 3); iswap.swap_rows(0, 1); iswap
[0 1 0] [1 0 0] [0 0 1]

Now watch what happens when we multiply another matrix on the left by the "swapping matrix" iswap. (For this, we will create a matrix M1.) Run the following command:

M1 = matrix(QQ, [[1, 2, 3], [4, 5, 6], [7, 8, 9]]); iswap*M1
[4 5 6] [1 2 3] [7 8 9]

Exercise: Now you try it! Create a matrix (call it iswap2) which will swap rows 2 and 3 of a matrix with 4 rows when multiplied on the left. (You will need to start with a 4×44 \times 4 matrix for the multiplication to work out.) Test it on some matrix with 4 rows. (You could call this M2.)

iswap2=identity_matrix(QQ, 4); iswap2.swap_rows(1,2); m2 = matrix(QQ, [[1, 3, 8, 17, 0, 1], [1, 1, 1, 0, 1, 1], [2, 2, 3, 4, 5, 5], [1, 2, 3, 4, 5, 6]]);
iswap2
[1 0 0 0] [0 0 1 0] [0 1 0 0] [0 0 0 1]
B
[-1 -2 0 4] [ 5 4 3 2] [ 0 0 0 1] [ 3 2 1 0]
B*iswap2
[-1 0 -2 4] [ 5 3 4 2] [ 0 0 0 1] [ 3 1 2 0]
iswap2*m2
[ 1 3 8 17 0 1] [ 2 2 3 4 5 5] [ 1 1 1 0 1 1] [ 1 2 3 4 5 6]
m2
[ 1 3 8 17 0 1] [ 1 1 1 0 1 1] [ 2 2 3 4 5 5] [ 1 2 3 4 5 6]

What about the other two row operations? Let's try multiplying the third row of a 3×33 \times 3 identity matrix by 1/2-1/2, and see what happens when we multiply this matrix on the left of a matrix with three rows, like the matrix M1 above:

iscale = identity_matrix(QQ, 3); iscale.rescale_row(2, -1/2); iscale
[ 1 0 0] [ 0 1 0] [ 0 0 -1/2]
iscale*M1
[ 1 2 3] [ 4 5 6] [-7/2 -4 -9/2]

Exercise: Create a matrix (call it iscale2) which rescales the second row of a matrix with four rows when multiplied on the left, and test it on a matrix with four rows:

iscale2 = identity_matrix(QQ, 4); iscale2.rescale_row(1, 3); iscale2
[1 0 0 0] [0 3 0 0] [0 0 1 0] [0 0 0 1]
m2
[ 1 3 8 17 0 1] [ 1 1 1 0 1 1] [ 2 2 3 4 5 5] [ 1 2 3 4 5 6]
iscale2*m2
[ 1 3 8 17 0 1] [ 3 3 3 0 3 3] [ 2 2 3 4 5 5] [ 1 2 3 4 5 6]

Only one row operation left: We create the matrix iadd by adding 22 times row 11 to row 33, and check what it does when we multiply it on the left. (What do you think it will do?)

iadd = identity_matrix(QQ, 3); iadd.add_multiple_of_row(2, 0, 2); iadd
[1 0 0] [0 1 0] [2 0 1]
iadd*M1
[ 1 2 3] [ 4 5 6] [ 9 12 15]

Exercise: Create a matrix which will add three times the first row to the second row of a four-row matrix:

Now out of curiousity, what do you think the matrix iadd*iscale*iswap?

composition = iadd*iscale*iswap composition*M1
[ 4 5 6] [ 1 2 3] [ 9/2 6 15/2]
composition
[ 0 1 0] [ 1 0 0] [ 0 2 -1/2]

So the punchline is: Elementary matrices can "store" row operations. (Now what would happen if reducing a particular matrix to the identity required first swapping the first two rows, then multiplying the third row by 1/2-1/2, then adding two times row 1 to row 3, like the matrix below?)

M1 = matrix(QQ, [[0, 1, 0], [1, 0, 0], [4, 0, -2]]); show(M1); composition*M1
(010100402)\displaystyle \left(\begin{array}{rrr} 0 & 1 & 0 \\ 1 & 0 & 0 \\ 4 & 0 & -2 \end{array}\right)
[1 0 0] [0 1 0] [0 0 1]

Inverses

We would like to be able to find an inverse matrix for a given matrix, if it exists. That is, given a matrix MM, we want to find a matrix M1M^{-1} such that MM1M M^{-1} and M1MM^{-1} M are both the identity matrix. (This is just like fractions in the real numbers: (21)(2)=(2)(21)=1(2^{-1})(2)= (2) (2^{-1}) = 1. In matrices, it's actually possible to have one or the other of these but not both.)

For example, the two matrices in the last example above (composition and M1) were inverses.

Finding an inverse is not so hard: Just do Gauss-Jordan elimination to reduce the matrix to the identity. "Store" the operations by performing the same operations on the identity matrix, and we'll have the inverse if there is on.

Here's an example of doing this in Sage:

Augment the matrix with an identiy matrix, so that each row operation is carried out on both matrices at once:

M3 = matrix(QQ, [[1, 2, 3], [0,0,2], [1, 1, 1]])
temp = M3.augment(identity_matrix(QQ, 3), subdivide=true); temp
[1 2 3|1 0 0] [0 0 2|0 1 0] [1 1 1|0 0 1]

Now row reduce!

temp.add_multiple_of_row(2, 0, -1); temp
[ 1 2 3| 1 0 0] [ 0 0 2| 0 1 0] [ 0 -1 -2|-1 0 1]
temp.swap_rows(1, 2); temp
[ 1 2 3| 1 0 0] [ 0 -1 -2|-1 0 1] [ 0 0 2| 0 1 0]
temp.rescale_row(2, 1/2); temp.rescale_row(1, -1); temp
[ 1 2 3| 1 0 0] [ 0 1 2| 1 0 -1] [ 0 0 1| 0 1/2 0]
temp.add_multiple_of_row(1, 2, -2); temp.add_multiple_of_row(0, 2, -3); temp
[ 1 2 0| 1 -3/2 0] [ 0 1 -2| 1 -2 -1] [ 0 0 1| 0 1/2 0]
temp.add_multiple_of_row(1, 2, 2); temp
[ 1 2 0| 1 -3/2 0] [ 0 1 0| 1 -1 -1] [ 0 0 1| 0 1/2 0]
temp.add_multiple_of_row(0, 1, -2); temp
[ 1 0 0| -1 1/2 2] [ 0 1 0| 1 -1 -1] [ 0 0 1| 0 1/2 0]

We can extract the first three columns like so:

M3inv = temp.matrix_from_columns([3,4,5]); M3inv
[ -1 1/2 2] [ 1 -1 -1] [ 0 1/2 0]
M3inv*M3
[1 0 0] [0 1 0] [0 0 1]
M3*M3inv
[1 0 0] [0 1 0] [0 0 1]
%md ## Practice Solve the following problems: * Try to find the inverse of this matrix: $\left( \begin{array}{ccc} 0 & 3 & -1 \\ 1 & 0& 1 \\ 1 & -1 & 0 \end{array}\right) $ Check your result by multiplying in both orders. * Try to find the inverse of this matrix: $ \left( \begin{array}{cc} 1 & 1 \\ 2 & 2 \end{array} \right) $ * Without actually solving for the inverse, prove that the two matrices are below are inverses. (There are *two* products you need to check.) $ \left( \begin{array}{ccc} 2 & 0 & -1 \\ 1/2 & 3 & 2 \\ 0 & 0 & 2 \end{array} \right) \qquad \text{and} \qquad \left( \begin{array}{ccc} 1/2 & 0 & 1/4 \\ -1/12 & 1/3 & -3/8 \\ 0 & 0 &1/2 \end{array} \right) $ * Check the products $A B$ and $B A$ for the following matrices. What do you notice? $ A = \left( \begin{array}{ccc} 1 & 0 & 1 \\ 0 & 1 & 0 \end{array} \right) \qquad B = \left( \begin{array}{cc} 2 & 1 \\ 0 & 1 \\ -1 & -1 \end{array}\right) $ * Try creating a *symbolic matrix* using the commands ``var('a, b, c, d'); m = matrix(SR, [[a, b], [c, d]])``. Use this symbolic matrix to solve for a formula for the inverse of a general $2 \times 2$ matrix. (When you have found an inverse matrix ``mi``, try simplifying your expression with the command ``mi.simplify_rationa()`` to reduce any complex fractions which might be in the result.) Compare with Corollary 4.11 on p. 249 of the text.

Practice

Solve the following problems:

  • Try to find the inverse of this matrix: (031101110)\left( \begin{array}{ccc} 0 & 3 & -1 \\ 1 & 0& 1 \\ 1 & -1 & 0 \end{array}\right) Check your result by multiplying in both orders.

  • Try to find the inverse of this matrix: (1122) \left( \begin{array}{cc} 1 & 1 \\ 2 & 2 \end{array} \right)

  • Without actually solving for the inverse, prove that the two matrices are below are inverses. (There are two products you need to check.) (2011/232002)and(1/201/41/121/33/8001/2) \left( \begin{array}{ccc} 2 & 0 & -1 \\ 1/2 & 3 & 2 \\ 0 & 0 & 2 \end{array} \right) \qquad \text{and} \qquad \left( \begin{array}{ccc} 1/2 & 0 & 1/4 \\ -1/12 & 1/3 & -3/8 \\ 0 & 0 &1/2 \end{array} \right)

  • Check the products ABA B and BAB A for the following matrices. What do you notice? A=(101010)B=(210111) A = \left( \begin{array}{ccc} 1 & 0 & 1 \\ 0 & 1 & 0 \end{array} \right) \qquad B = \left( \begin{array}{cc} 2 & 1 \\ 0 & 1 \\ -1 & -1 \end{array}\right)

  • Try creating a symbolic matrix using the commands var('a, b, c, d'); m = matrix(SR, [[a, b], [c, d]]). Use this symbolic matrix to solve for a formula for the inverse of a general 2×22 \times 2 matrix. (When you have found an inverse matrix mi, try simplifying your expression with the command mi.simplify_rationa() to reduce any complex fractions which might be in the result.) Compare with Corollary 4.11 on p. 249 of the text.