Narration 1 00:00:01 --> 00:00:04 Hello friends and welcome to the tutorial on 'Matrices'. 2 00:00:05 --> 00:00:30 At the end of this tutorial, you will be able to,

Create matrices using data. Create matrices from lists. Do basic matrix operations like addition,multiplication. Perform operations to find out the -- - inverse of a matrix. - determinant of a matrix. - eigen values and eigen vectors of a matrix. - norm of a matrix. - singular value decomposition of a matrix.

3 00:00:31 --> 00:00:41 Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with lists", "Getting started with arrays", "Accessing parts of arrays". 4 00:00:42 --> 00:00:46 Let us start our ipython interpreter by pylab loaded 5 00:00:47 --> 00:00:51 Type ipython hypen pylab in terminal 6 00:00:52 --> 00:00:54 All matrix operations are done using arrays. 7 00:00:55 --> 00:00:59 Thus all the operations on arrays are valid on matrices only. 8 00:01:00 --> 00:01:01 A matrix may be created as, 9 00:01:02 --> 00:01:15 Type in the terminal m1 = array within bracket and square bracket 1 comma 2 comma 3 comma 4 hit enter. 10 00:01:16 --> 00:01:19 Using the method shape , we can find out the shape or size of the matrix, 11 00:01:20 --> 00:01:26 Type m1 dot shape and hit enter 12 00:01:27 --> 00:01:28 We can see the output 13 00:01:29 --> 00:01:45 Since it is a one row four column matrix it returned a tuple, one by four. 14 00:01:46 --> 00:01:49 A list can also be converted to a matrix as follows, 15 00:01:50 --> 00:02:27 Type l1 = within square bracket square bracket 1 comma 2 comma 3 comma 4 comma in another square bracket 5 comma 6 comma 7 comma 8. Type m2 = array within bracket 11 16 00:02:28 --> 00:02:34 Sorry you have to type l1 array . 17 00:02:35 --> 00:02:42 Pause the video here, try out the following exercise and resume the video. 18 00:02:43 --> 00:02:50 Create a two dimensional matrix m3 of order 2 by 4 with elements 5, 6, 7, 8, 9, 10, 11, 12. 19 00:02:51 --> 00:02:53 Switch to terminal for solution. 20 00:02:54 --> 00:02:55 m3 can be created as, 21 00:02:56 --> 00:03:30 Type m3 = array within closing bracket inside square bracket square bracket 5 comma 6 comma 7 comma 8 comma in another square bracket 9 comma 10 comma 11 comma 12 22 00:03:31 --> 00:03:33 Let us now move to matrix operations. 23 00:03:34 --> 00:03:36 We can do matrix addition and subtraction easily. 24 00:03:37 --> 00:03:42 m3+m2 does element by element addition, that is matrix addition. 25 00:03:43 --> 00:03:46 Note that both the matrices should be of the same order. 26 00:03:47 --> 00:03:54 Type m3+m2 and hit enter so you can see the output. 27 00:03:55 --> 00:04:01 Similarly,m3 minus m2 does matrix subtraction, that is element by element subtraction. 28 00:04:02 --> 00:04:08 You can try out by typing m3 minus m2 29 00:04:09 --> 00:04:12 Now let us try,matrix multiplication 30 00:04:13 --> 00:04:19 Type m3 star m2 31 00:04:20 --> 00:04:27 Note that in arrays m3 star m2 does element wise multiplication and not matrix multiplication, 32 00:04:28 --> 00:04:36 Matrix multiplication in matrices are done using the function dot() 33 00:04:37 --> 00:04:46 Type dot within bracket m3 comma m2 and hit enter. 34 00:04:47 --> 00:04:49 So we can see error value show in the command. 35 00:04:50 --> 00:04:55 Due to size mismatch, the multiplication could not be done and it returned an error. 36 00:04:56 --> 00:04:59 Now let us see an example for matrix multiplication. 37 00:05:00 --> 00:05:10 For doing matrix multiplication we need to have two matrices of the order n by m and m by r and the resulting matrix will be of the order n by r. 38 00:05:11 --> 00:05:15 Thus let us first create two matrices which are compatible for multiplication. 39 00:05:16 --> 00:05:23 Type m1.shape and hit enter 40 00:05:24 --> 00:05:27 matrix m1 is of the shape one by four, 41 00:05:28 --> 00:05:32 let us create another one, of the order four by two, 42 00:05:33 --> 00:06:09 So type m4 = array in closing bracket within square bracket within square bracket 1 comma 2 comma within square bracket 3 comma 4 comma within square bracket 5 comma 6 within square bracket comma within square bracket 7 comma 8. Type dot within bracket m1 comma m4 43 00:06:10 --> 00:06:14 Thus the dot() function is used for matrix multiplication. 44 00:06:15 --> 00:06:23 As we already learnt in arrays, the function identity() which creates an identity matrix of the order n by n, 45 00:06:24 --> 00:06:29 the function zeros() which creates a matrix of the order m by n with all zeros, 46 00:06:30 --> 00:06:38 the function zeros like function() which creates a matrix with zeros with the shape of the matrix passed, 47 00:06:39 --> 00:06:46 the function ones() which creates a matrix of order m by n with all ones, 48 00:06:47 --> 00:06:52 the function ones underscore like() which creates a matrix with ones with the shape of the matrix passed; 49 00:06:53 --> 00:06:56 all these functions can also be used with matrices. 50 00:06:57 --> 00:07:02 So now Let us see, how to find out the transpose of a matrix we can do, 51 00:07:03 --> 00:07:13 Type print m4, m4 dot T 52 00:07:14 --> 00:07:20 As you saw, Matrix name dot capital T will give the transpose of a matrix 53 00:07:21 --> 00:07:25 Pause the video here, try out the following exercise and resume the video. 54 00:07:26 --> 00:07:32 Find out the Frobenius norm of inverse of a 4 by 4 matrix, the matrix being, 55 00:07:33 --> 00:07:43 m5 = arrange within bracket 1 comma 17 dot reshape to 4 comma 4 56 00:07:44 --> 00:07:53 The Frobenius norm of a matrix is defined as, the square root of the sum of the absolute squares of its elements 57 00:07:54 --> 00:07:57 Switch to terminal for the solution 58 00:07:58 --> 00:08:02 Let us create the matrix m5 by using the data provided in the question 59 00:08:03 --> 00:08:19 Type m5 = arrange within bracket 1 comma 17.reshape within square bracket 4 comma 4. Then type print m5 60 00:08:20 --> 00:08:32 The inverse of a matrix A, A raise to minus one, is also called the reciprocal matrix, such that A multiplied by A inverse will give 1. 61 00:08:33 --> 00:08:40 The Frobenius norm of a matrix is defined as square root of sum of squares of elements in the matrix. 62 00:08:41 --> 00:08:46 The inverse of a matrix can be found using the function inv(A) . 63 00:08:47 --> 00:08:56 Type in the terminal im5 = inv within bracket m5 64 00:08:57 --> 00:09:51 And the Frobenius norm of the matrix im5 can be found out as, sum = 0

for each in im5 dot flatten function(): sum plus= each star each print sqrt within bracket sum

65 00:09:52 --> 00:09:57 Thus we have successfully obtained the Frobenius norm of the matrix m5 66 00:09:58 --> 00:10:03 Pause the video here, try out the following exercise and resume the video. 67 00:10:04 --> 00:10:07 Find out the infinity norm of the matrix im5. 68 00:10:08 --> 00:10:15 The infinity norm of a matrix is defined as the maximum value of sum of the absolute of elements in each row. 69 00:10:16 --> 00:10:19 Switch to terminal for the solution 70 00:10:20 --> 00:11:00 sum underscore rows = square bracket

for i in im5 colon sum underscore rows.append within bracket abs within bracket i.sum() print max within square bracket sum underscore rows

71 00:11:01 --> 00:11:09 Well! to find out the Frobenius norm and Infinity norm we have an even easier method, and let us see that now. 72 00:11:10 --> 00:11:18 The norm of a matrix can be found out using the method norm(). 73 00:11:19 --> 00:11:24 In order to find out the Frobenius norm of the matrix im5, we do, 74 00:11:25 --> 00:11:33 In the terminal type norm within bracket im5 and hit enter 75 00:11:34 --> 00:11:38 And to find out the Infinity norm of the matrix im5, we do, 76 00:11:39 --> 00:11:50 norm within bracket im5,ord=inf 77 00:11:51 --> 00:11:54 This is easier when compared to the code we wrote. 78 00:11:55 --> 00:12:03 Read the documentation of norm to read up more about ord and the possible type of norms the norm function produces. 79 00:12:04 --> 00:12:10 Now let us find out the determinant of a the matrix m5. 80 00:12:11 --> 00:12:19 The determinant of a square matrix can be obtained by using the function det() and the determinant of m5 can be found out as, 81 00:12:20 --> 00:12:25 So type det within bracket m5 82 00:12:26 --> 00:12:28 Hence we get the determinant. 83 00:12:29 --> 00:12:33 Let us now move on to eigen vectors and eigen values 84 00:12:34 --> 00:12:45 The eigen values and eigen vector of a square matrix can be computed using the function eig() and eigvals(). 85 00:12:46 --> 00:12:52 Let us find out the eigen values and eigen vectors of the matrix m5. We find them as, 86 00:12:53 --> 00:13:01 Typing eig within bracket m5 in the terminal. 87 00:13:02 --> 00:13:05 Note that it returned a tuple of two matrices. 88 00:13:06 --> 00:13:10 The first element in the tuple are the eigen values and the second element in the tuple are the eigen vectors. 89 00:13:11 --> 00:13:29 Thus eigen values are given by,eig within bracket m5 and in square bracket 0 90 00:13:30 --> 00:13:43 and the eigen vectors are given by,eig within bracket m5 within square bracket 1 91 00:13:44 --> 00:13:49 The eigen values can also be computed using the function eigvals() as, 92 00:13:50 --> 00:13:57 Typing on the terminal eigvals within bracket m5 93 00:13:58 --> 00:14:05 Now let us learn how to do the singular value decomposition or S V D of a matrix. 94 00:14:06 --> 00:14:17 Suppose M is an m (cross) in matrix, whose entries come from the field K, which is either the field of real numbers or the field of complex numbers. 95 00:14:18 --> 00:14:24 Then there exists a factorization of the form

M = U Sigma V star

96 00:14:25 --> 00:14:52 where U is an (m by m) unitary matrix over K, the matrix Sigma is an (m by n) diagonal matrix and the non-negative real numbers on the diagonal, and V* is an (n by n) unitary matrix over K,which denotes the conjugate transpose of V. 97 00:14:53 --> 00:14:57 Such a factorization is called the singular-value decomposition of M. 98 00:14:58 --> 00:15:00 The SVD of matrix m5 can be found as 99 00:15:01 --> 00:15:08 So now open the terminal and type svd within brackets m5 100 00:15:09 --> 00:15:11 Notice that it returned a tuple of 3 elements. 101 00:15:12 --> 00:15:18 The first one U the next one Sigma and the third one V star 102 00:15:19 --> 00:15:21 This brings us to the end of the end of this tutorial. 103 00:15:22 --> 00:15:24 In this tutorial, we have learnt to, Create matrices using arrays. 104 00:15:25 --> 00:15:27 Add,subtract and multiply the elements of matrix. 105 00:15:28 --> 00:15:31 Find out the inverse of a matrix,using the function inv() . 106 00:15:32 --> 00:15:35 Use the function det() to find the determinant of a matrix. 107 00:15:36 --> 00:15:42 Calculate the norm of a matrix using the for loop and also using the function norm() 108 00:15:43 --> 00:15:49 Finding out the eigen vectors and eigen values of a matrix, using functions eig() and eigvals() 109 00:15:50 --> 00:15:57 Calculate singular value decomposition(SVD) of a matrix using the function svd() . 110 00:15:58 --> 00:16:00 Here are some self assessment questions for you to solve 111 00:16:01 --> 00:16:18 A and B are two array objects. Element wise multiplication in matrices are done by,

A * B multiply within bracket A comma B dot within bracket A comma B element underscore multiply within bracket A comma B

112 00:16:19 --> 00:16:30 eig within bracket A within square bracket 1 and eigvals within bracket A are the same.Is it True or False? 113 00:16:31 --> 00:16:42 norm within bracket A comma ord= within is equal to fro is the same as norm within bracket A . Is it True or False? 114 00:16:43 --> 00:16:46 Look at the answers, 115 00:16:47 --> 00:16:52 Element wise multiplication between two matrices, A and B is done as, A into B 116 00:16:53 --> 00:17:05 False. eig within bracket A within square bracket 0and eigvals within bracket A are same, that is both will give the eigen values of matrix A. 117 00:17:06 --> 00:17:21 norm within bracket A comma ord=is equal to fro and norm(A) are same, since the order=is equal to fro stands for Frobenius norm. 118 00:17:22 --> 00:17:25 Hence answer is true. 119 00:17:26 --> 00:17:29 Hope you have enjoyed this tutorial and found it useful. 120 00:17:30 --> 00:17:35 Thank you!