Csci 1802 Computer Systems Matrices Lecture 1 Learning outcomes Be able to add and multiply matrices. Know some uses of matrices in a computing context Matrices -1- Csci 1802 Computer Systems Introduction Vectors and matrices are data structures for collections of data all of the same type Examples of uses of vectors and matrices: 1. Vector of names 2. Matrix of marks Adams Brown Chauhan Davis Edwards 65 70 66 47 58 3. a c Digraph and its 72 75 52 55 60 54 65 57 45 47 66 64 56 35 52 Matrix representation b 0 0 1 0 1 0 1 0 0 0 0 0 0 1 0 0 d In programming vectors are called one-dimensional arrays matrices are called two-dimensional arrays can also go into 3 or more dimensions In this maths module, we’re not going higher than two dimensions. Parallel processors. Computers designed to do certain types of calculations quicker by working on more than one pair of values at a time. e.g. Vector Processors and Array Processors -2- Csci 1802 Computer Systems In the sections that follow, we shall be looking at - Vector and Matrix notation - Arithmetic of Vectors and Matrices - Application to nets - Application to computer graphics - Application to codes (Note: matrix is the singular of matrices i.e. you talk about one matrix or several matrices) Vectors Row Vector [1 4 8 2 4] Column vector 3 5 1 2 To give a vector a name, use underline to indicate that it is a vector. e.g. x=[1243] Vector Arithmetic Addition and Subtraction You can only do addition and subtraction between vectors that are of the same type and with the same number of elements. Examples: 1) [1 3 2 4] + [3 -1 2 6] = 2) 2 6 8 3 1 4 4 4 0 [4 2 4 10] -3- Csci 1802 Computer Systems 3) If 1 1 1 x= and y = 8 3 2 then x+y= 9 2 1 and x-y= 7 4 3 Exercises: 1. x = [1 1 4 2] and y = [1 -1 5 4] and z = [3 3 0 1] Calculate (i) 2. y= x+y (ii) y-z (iii) x+y+z 1 2 3 and z= 2 3 3 - Calculate (i) (ii) y-z -y - z (iii) z + y Vector Notation Labelling elements in vectors: Row vector x [x1 x2 x3 x4 ] -4- Csci 1802 Computer Systems x1 x2 x 3 Column vector x e.g. If x and y are both row vectors having 5 elements then x + y = [x1+y1 x2+y2 x3+y3 x4+y4 x5+y5] The result of z=x+y is zi = xi + yi for i = 1,2, .. n where n is the size of the vector Matrices Matrix Notation Matrices have both rows & columns, so we use 2 subscripts first subscript is the row label second subscript is the column label. e.g. If matrix M is a 3 x 5 matrix (i.e. 3 rows by 5 columns) 1 2 3 4 5 row labels 1 column labels m11 m12 m13 m14 m15 2 3 m21 m31 m22 m32 m23 m33 m24 m34 m25 m35 We use capital letters for the matrix name can also use e.g [aij] to denote matrix A. i.e. A = [aij] A Square Matrix is a matrix which has the same number of rows and columns. -5- Csci 1802 Computer Systems A Diagonal Matrix is a square matrix which has non-zero element values only on the leading diagonal of the matrix and all the other elements are zero. e.g. 3 0 0 0 1 0 0 0 2 A Symmetric Matrix is one where the non-diagonal elements have a 'mirror-image' across the leading diagonal 5 2 1 e.g. 2 8 0 1 0 7 The Transpose of a matrix is the matrix formed by interchanging the rows and the columns. The transpose of A is denoted by A' (or sometimes AT) 1 2 then A' = 0 3 5 6 1 0 5 A = 2 3 6 e.g. If 12.5.2 Exercises 1. State whether each of the following matrices is (a) square, (b) diagonal, (c) symmetric (i) 1 5 7 0 4 0 0 5 6 2. If matrix A = [aij] and is equal to matrix (i) above, what are the values of (a) a22 = 1 4 2 (ii) 4 4 6 2 6 0 (b) 1 3 (iii) 3 1 0 4 a13 = 13 0 (iv) 0 1 (c) -6- a23 = Csci 1802 Computer Systems 3. What is the transpose of A? A 2 4 6 8 = 1 1 2 4 5 3 1 3 A' = Matrix Arithmetic Addition and Subtraction can only add or subtract matrices of the same size & shape simply add or subtract the corresponding elements to get a matrix of the same size and shape as the original ones e.g. 1 4 2 7 3 11 2 2 + 1 4 = 1 6 5 5 9 3 14 2 Thus, the result of C = A + B ( [cij] = [aij] + [bij] ) is cij aij + bij & for i = 1,2, .. n for j = 1,2, .. m ( i,j : 1..n · cij = aij + bij = where n is the number of rows where m is the number of cols ) -7- Csci 1802 Computer Systems Matrix Multiplication only certain sizes and shapes of vectors and matrices can be multiplied together the order in which the matrices or vectors are multiplied is significant the dimensions of the resultant matrix may be different from the original ones the number of columns in the first matrix or vector must be the same as the number of rows in the second matrix or vector Example: To multiply x.M where x is a row vector of 3 elements and M is a 3 x 2 matrix 1 2 2 4 1 0 1 4 11 2 3 The result is a row vector of 2 elements. What we did to obtain each element in the result r was To get r11 take the 1st row of the vector and the 1st column of the matrix, then r11 = x11 . m11 + x12 . m21 + x13 . m31 To get r12 take the 1st row of the vector and the 2nd column of the matrix, then r12 = x11 . m12 + x12 . m22 + x13 . m32 Remember, [m x n] means a matrix with m rows and n columns And for multiplication we go Along the rows of the first matrix Down the columns of the second matrix If the number of columns in the first matrix or vector ISN’T the same as the number of rows in the second matrix or vector they cannot be multiplied together (i.e. they are not compatible) The size of the answer can be predicted: [m x n] [n x p] gives answer [m x p] -8- Csci 1802 Computer Systems Example 1 2 1 1 2 2 3 2 1 2 2 1 3 2 11 10 1 2 3 2 1 2 1 2 2 2 2 1 1 2 2 2 2 2 1 1 2 2 8 9 This layout may help: 1st matrix 1 2 2 1 3 2 11 8 10 9 x 1 2 2 2 1 2 Result 2nd matrix This emphasizes that to get the result for (e.g.) element R21 you use row 2 and column 1 Exercises 1. Where possible do the following multiplications. State which ones are not compatible. (a) 1 1 2 3 1 2 3 1 2 2 (b) 5 (c) 1 0 5 4 1 0 2 2 0 3 2 3 1 2 2 1 2 2 -9- Csci 1802 Computer Systems 2. Multiply the following matrices A.B. Show that B.A gives a different result. 1 2 2 A = 0 1 3 3 1 1 3 0 0 B = 2 1 4 0 3 1 1 2 2 3 0 0 A.B = 0 1 3 2 1 4 = 3 1 1 0 3 1 B.A = 3 0 0 1 2 2 2 1 4 0 1 3 = 0 3 1 3 1 1 Inverse of a Matrix There is a special case for square matrices where A.B = B.A. This is when matrix B is the inverse of matrix A. (It also follows that matrix A is the inverse of matrix B.) The inverse of matrix A is denoted by A-1. A-1 = 1 A The result of the multiplications A.A-1 and A-1.A is the identity matrix I. This is a special type of diagonal matrix where the diagonal elements have the value 1. A . A-1 = A-1. A = I = 1 0 .. 0 0 1 .. 0 . . . . 0 .. 0 1 - 10 - Csci 1802 Computer Systems Note that not all square matrices have an inverse. 12.7.1 Exercise Prove that the following matrices are the inverse of each other by multiplying A.B and B.A and checking that the result in both cases is the identity matrix. A 1 3 3 = 1 4 3 1 3 4 B = 7 3 3 0 1 1 1 0 1 - 11 -