COMP 4560 Computer Graphics for Computer Systems Technology Matrix Multiplication Matrix multiplication plays a major role in the formalism we will set up in the course to carry out geometric and viewing transformations. This document is a brief review of how matrix multiplication is done, with a few examples or exercises for you to practice with if necessary. A matrix is a rectangular table of numbers. For example: 1 6 2 9 A 5 0 3 8 7 5 12 4 is a matrix with three rows and four columns (we would call this a 3 by 4 matrix). Matrices are usually denoted in print by bolded upper case characters. The individual entries in a matrix are called its elements, and are often referred to using a subscript notation indicating the row and column in which they are found. Thus, the '-5' in the above matrix is also called A32, because it is the element in the third row and second column of the matrix. Matrices with the same number of rows and columns are referred to as square matrices. A variety of arithmetic operations are defined for matrices. For this course, multiplication of one matrix by another is the most useful. The product of two matrices, A, and B, is a new third matrix C: A B C (1) nxm mxk nxk For the multiplication to be possible, the number of columns in the first factor must equal the number of rows in the second factor. When this is true, the product can be formed, and the result is a matrix which has the same number of rows as the first factor, and the same number of columns as the second factor. To form element Cjk of the product, we simply multiply together corresponding elements of row j of matrix A and column k of matrix B, and add up the m products formed that way. That is C jk Aj 1B1k Aj 2B2k Aj 3B3k m Ajm Bmk AjL BLk (2) L 1 To form one element of the product requires m multiplications, and there nk elements to be formed, so the entire operation requires nkm multiplications altogether. You could regard the calculation of the element C jk as the "product" of row j of matrix A by column k of matrix B. Since elements of row j of A are multiplied by the corresponding elements of column j of matrix B, you can see why the number of columns in matrix A (which is equal to the number of elements in each row of matrix A) must equal the number of rows in matrix B (which is equal to the number of elements in each column of matrix B). Example: As a numerical example, note that: 7 2 79 50 9 6 9 3 8 4 105 66 1 6 57 30 In detail, this result comes from: David W. Sabo (2000) Matrix Multiplication Page 1 of 3 for the 11-element: for the 12-element: for the 21 element: for the 22 element: for the 32 element: for the 33 element: 7 x 9 + 2 x 8 = 63 + 16 = 79 7 x 6 + 2 x 4 = 42 + 8 = 50 9 x 9 + 3 x 8 = 81 + 24 = 105 9 x 6 + 3 x 4 = 54 + 12 = 66 1 x 9 + 6 x 8 = 9 + 48 = 57 1 x 6 + 6 x 4 = 6 + 24 = 30 (row 1 times column1) (row 1 times column 2) (row 2 times column 1) (row 2 times column 2) (row 3 times column 1) (row 3 times column 2) Remarks i) matrix multiplication is not commutative that is, the order in which the matrices appear in the product is important. For numbers, we know that 2 x 3 is the same as 3 x 2. However, in general, for matrices AB BA First of all, even when AB makes sense and can be calculated, the dimensions of the two matrices may not allow the formation of the product BA (see exercise 2 below). When BA can be formed, in general, it will have different elements than does AB, and may even have a different shape (see exercises 1 and 3 below). ii) matrix multiplication is associative that is, when you have the product of three or more matrices, it does not matter in which order you carry out the multiplications, as long as you keep the matrices in the original order. Thus, to calculate, say, ABC, you can first form AB and then multiply this result from the right by matrix C, or, you can first form BC and then multiply this result by A from the left. The final result will be the same (see exercise 4 below). iii) The square matrix with diagonal elements equal to 1 and all other elements equal to zero is called the identity matrix for multiplication, denoted by I, the upper case "eye". Multiplication of a matrix A by the appropriately dimensioned I from either the right or the left gives the result A. Thus, multiplication by I for matrices is like multiplying by the number 1 in ordinary arithmetic. iv) If A is a square matrix and you can find a second matrix, B, such that AB = I, then the matrix B is called the inverse matrix of A, and is usually denoted by a superscript '-1' as in A-1. For square matrices, A, the inverse matrix A-1, when it exists, works in this way when multiplied from the right or the left. Thus AA-1 = A-1A = I You can see that this means that whatever A accomplishes, A-1 reverses, because the net effect of applying the two matrices in sequence in no change. Since we are using matrix multiplication to carry out geometric manipulations of shapes, the inverse matrix is something like the "undo" operation in computerese. Exercises 7 6 and B 6 1 1. Given A 10 8 , compute AB and BA. 5 7 1 9 7 5 1 9 2. Given A and B 5 4 1 7 6 4 3 3. Given A 9 and B 4 7 2 Page 2 of 3 1 5 5 10 , compute AB and BA. 1 9 2 , compute AB and BA. Matrix Multiplication David W. Sabo (2000) 3 4 2 8 6 4 5 4. Given A , B 7 5 6 9 and C 7 3 8 2 1 0 4 7 1 2 8 , find the product ABC in 6 5 9 0 two different ways. First multiply B by C to form BC, and then multiply this from the left to get ABC. Then, multiply A by B to get AB and then multiply this by C from the right to get ABC. Compare your two final results. 2 3 9 7 75 69 5. Given A 2 8 1 and B 4 4 16 , compute AB and BA. 0 9 2 18 18 10 1 3 4 7 6 8 2 5 and B 2 5 7 , compute AB and BA. 3 5 3 8 10 3 6. Given A 10 7. If you still haven't had enough, notice that the matrices in exercise 4 above have dimensions that allow you to calculate two other triple products: BCA and CAB. Do so. Answers 37 22 40 14 118 52 19 116 , BA cannot be formed because A B B A 1. A B , ; 2. 24 56 65 55 77 23 27 61 you cannot multiply a 3 x 2 matrix by a 4 x 3 matrix. 12 3. A B 36 8 56 9 A B 79 0 21 6 17 97 482 476 63 18 , B A 55 ; 4. B C 75 84 , A B C , 709 326 16 46 14 4 12 32 , you should get the same result for ABC whichever method you use. 26 13 164 5. A B B A 0 0 61 33 6. A B 114 100 35 73 0 0 164 0 these two matrices do commute. 0 164 17 91 49 82 51 , B A 69 19 38 99 11 73 50 237 471 63 110 878 747 376 744 18 232 168 7. B C A 222 288 123 , C A B 731 54 202 257 464 386 218 0 234 117 711 (Note that you don't have to calculate these two products from scratch because you already have BC and AB from the work in Exercise 4.) David W. Sabo (2000) Matrix Multiplication Page 3 of 3