Python/C3/Matrices/English-timed

From Script | Spoken-Tutorial
Revision as of 14:48, 10 July 2014 by Gaurav (Talk | contribs)

Jump to: navigation, search
Time Narration
00:01 Hello friends and welcome to the tutorial on 'Matrices'.
00:05 At the end of this tutorial, you will be able to,
  1. Create matrices using data.
  2. Create matrices from lists.
  3. Do basic matrix operations like addition,multiplication.
  4. 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.
00:31 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".
00:42 Let us start our ipython interpreter by pylab loaded
00:47 Type ipython hypen pylab in terminal
00:52 All matrix operations are done using arrays.
00:55 Thus all the operations on arrays are valid on matrices only.
01:00 A matrix may be created as,
01:02 Type in the terminal m1 = array within bracket and square bracket 1 comma 2 comma 3 comma 4 hit enter.
01:16 Using the method shape , we can find out the shape or size of the matrix,
01:20 Type m1 dot shape and hit enter
01:27 We can see the output
01:29 Since it is a one row four column matrix it returned a tuple, one by four.
01:46 A list can also be converted to a matrix as follows,


01:50 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
02:28 Sorry you have to type l1 array .
02:35 Pause the video here, try out the following exercise and resume the video.
02:43 Create a two dimensional matrix m3 of order 2 by 4 with elements 5, 6, 7, 8, 9, 10, 11, 12.
02:51 Switch to terminal for solution.
02:54 m3 can be created as,
02:56 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
03:31 Let us now move to matrix operations.
03:34 We can do matrix addition and subtraction easily.
03:37 m3+m2 does element by element addition, that is matrix addition.
03:43 Note that both the matrices should be of the same order.
03:47 Type m3+m2 and hit enter so you can see the output.
03:55 Similarly,m3 minus m2 does matrix subtraction, that is element by element subtraction.
04:02 You can try out by typing m3 minus m2
04:09 Now let us try,matrix multiplication
04:13 Type m3 star m2
04:20 Note that in arrays m3 star m2 does element wise multiplication and not matrix multiplication,
04:28 Matrix multiplication in matrices are done using the function dot()
04:37 Type dot within bracket m3 comma m2 and hit enter.
04:47 So we can see error value show in the command.
04:50 Due to size mismatch, the multiplication could not be done and it returned an error.
04:56 Now let us see an example for matrix multiplication.
05:00 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.
05:11 Thus let us first create two matrices which are compatible for multiplication.
05:16 Type m1.shape and hit enter
05:24 matrix m1 is of the shape one by four,
05:28 let us create another one, of the order four by two,
05:33 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
06:10 Thus the dot() function is used for matrix multiplication.
06:15 As we already learnt in arrays, the function identity() which creates an identity matrix of the order n by n,
06:24 the function zeros() which creates a matrix of the order m by n with all zeros,
06:30 the function zeros like function() which creates a matrix with zeros with the shape of the matrix passed,
06:39 the function ones() which creates a matrix of order m by n with all ones,
06:47 the function ones underscore like() which creates a matrix with ones with the shape of the matrix passed;
06:53 all these functions can also be used with matrices.
06:57 So now Let us see, how to find out the transpose of a matrix we can do,
07:03 Type print m4
m4 dot T
07:14 As you saw, Matrix name dot capital T will give the transpose of a matrix
07:21 Pause the video here, try out the following exercise and resume the video.
07:26 Find out the Frobenius norm of inverse of a 4 by 4 matrix, the matrix being,
07:33 m5 = arrange within bracket 1 comma 17 dot reshape to 4 comma 4
07:44 The Frobenius norm of a matrix is defined as, the square root of the sum of the absolute squares of its elements
07:54 Switch to terminal for the solution
07:58 Let us create the matrix m5 by using the data provided in the question
08:03 Type m5 = arrange within bracket 1 comma 17.reshape within square bracket 4 comma 4

Then type print m5

08:20 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.
08:33 The Frobenius norm of a matrix is defined as square root of sum of squares of elements in the matrix.
08:41 The inverse of a matrix can be found using the function inv(A) .
08:47 Type in the terminal im5 = inv within bracket m5
08:57 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

09:52 Thus we have successfully obtained the Frobenius norm of the matrix m5
09:58 Pause the video here, try out the following exercise and resume the video.
10:04 Find out the infinity norm of the matrix im5.
10:08 The infinity norm of a matrix is defined as the maximum value of sum of the absolute of elements in each row.
10:16 Switch to terminal for the solution
10:20 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

11:01 Well! to find out the Frobenius norm and Infinity norm we have an even easier method, and let us see that now.
11:10 The norm of a matrix can be found out using the method norm().
11:19 In order to find out the Frobenius norm of the matrix im5, we do,
11:25 In the terminal type norm within bracket im5 and hit enter
11:34 And to find out the Infinity norm of the matrix im5, we do,
11:39 norm within bracket im5,ord=inf
11:51 This is easier when compared to the code we wrote.
11:55 Read the documentation of norm to read up more about ord and the possible type of norms the norm function produces.
12:04 Now let us find out the determinant of a the matrix m5.
12:11 The determinant of a square matrix can be obtained by using the function det() and the determinant of m5 can be found out as,
12:20 So type det within bracket m5
12:26 Hence we get the determinant.
12:29 Let us now move on to eigen vectors and eigen values
12:34 The eigen values and eigen vector of a square matrix can be computed using the function eig() and eigvals().
12:46 Let us find out the eigen values and eigen vectors of the matrix m5. We find them as,
12:53 Typing eig within bracket m5 in the terminal.
13:02 Note that it returned a tuple of two matrices.
13:06 The first element in the tuple are the eigen values and the second element in the tuple are the eigen vectors.
13:11 Thus eigen values are given by,eig within bracket m5 and in square bracket 0
13:30 and the eigen vectors are given by,eig within bracket m5 within square bracket 1
13:44 The eigen values can also be computed using the function eigvals() as,
13:50 Typing on the terminal eigvals within bracket m5
13:58 Now let us learn how to do the singular value decomposition or S V D of a matrix.
14:06 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.
14:18 Then there exists a factorization of the form

M = U Sigma V star

14:25 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.
14:53 Such a factorization is called the singular-value decomposition of M.
14:58 The SVD of matrix m5 can be found as
15:01 So now open the terminal and type svd within brackets m5
15:09 Notice that it returned a tuple of 3 elements.
15:12 The first one U the next one Sigma and the third one V star
15:19 This brings us to the end of the end of this tutorial.
15:22 In this tutorial, we have learnt to, 1. Create matrices using arrays.
15:25 2. Add,subtract and multiply the elements of matrix.
15:28 3. Find out the inverse of a matrix,using the function inv() .
15:32 4. Use the function det() to find the determinant of a matrix.
15:36 5. Calculate the norm of a matrix using the for loop and also using the function norm()
15:43 6. Finding out the eigen vectors and eigen values of a matrix, using functions eig() and eigvals()
15:50 7. Calculate singular value decomposition(SVD) of a matrix using the function svd() .
15:58 Here are some self assessment questions for you to solve
16:01 1. 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  
16:19 2. eig within bracket A within square bracket 1 and eigvals within bracket A are the same.Is it True or False?
16:31 3. norm within bracket A comma ord= within is equal to fro is the same as norm within bracket A . Is it True or False?
16:43 Look at the answers,
16:47 1. Element wise multiplication between two matrices, A and B is done as, A into B
16:53 2. 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.
17:06 3. 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.
17:22 Hence answer is true.
17:26 Hope you have enjoyed this tutorial and found it useful.
17:30 Thank you!

Contributors and Content Editors

Gaurav, Minal, PoojaMoolya, Sneha