Python/C3/Matrices/English
Visual Cue | Narration |
---|---|
Show Slide 1
Containing title, name of the production team along with the logo of MHRD |
Hello friends and welcome to the tutorial on 'Matrices'. |
Show Slide 2
Leaning objectives |
At the end of this tutorial, you will be able to,
|
Show Slide 3
Pre-requisite slide |
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". |
ipython -pylab | Let us start our ipython interpreter with pylab loaded |
m1 = array([1,2,3,4]) | All matrix operations are done using arrays. Thus all the operations on arrays are valid on matrices also. A matrix may be created as, |
m1.shape | Using the method shape, we can find out the shape or size of the matrix, |
l1 = [[1,2,3,4],[5,6,7,8]]
m2 = array(l1) |
Since it is a one row four column matrix it returned a tuple, one by four.
A list can also be converted to a matrix as follows, |
Pause the video here, try out the following exercise and resume the video. | |
Show Slide 4
Assignment 1 |
Create a two dimensional matrix m3 of order 2 by 4 with elements 5, 6, 7, 8, 9, 10, 11, 12. |
Switch to terminal
m3 = array([[5,6,7,8],[9,10,11,12]]) |
Switch to terminal for solution. m3 can be created as, |
m3 + m2 | Let us now move to matrix operations. We can do matrix addition and subtraction easily. m3+m2 does element by element addition, that is matrix addition. Note that both the matrices should be of the same order. |
m3 - m2 | Similarly,m3-m2 does matrix subtraction, that is element by element subtraction. |
m3 * m2 | Now let us try,matrix multiplication |
dot(m3, m2) | Note that in arrays m3 * m2 does element wise multiplication and not matrix multiplication,
Matrix multiplication in matrices are done using the function dot() |
m1.shape | Due to size mismatch, the multiplication could not be done and it returned an error.
Now let us see an example for matrix multiplication. 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. Thus let us first create two matrices which are compatible for multiplication. |
m4 = array([[1,2],[3,4],[5,6],[7,8]])
dot(m1, m4) |
matrix m1 is of the shape one by four, let us create another one, of the order four by two, |
Thus the dot() function is used for matrix multiplication. | |
Show Slide 5
Recall from arrays |
As we already learnt in arrays, the function identity() which creates an identity matrix of the order n by n, the function zeros() which creates a matrix of the order m by n with all zeros, the function zeros_like() which creates a matrix with zeros with the shape of the matrix passed, the function ones() which creates a matrix of order m by n with all ones, the function ones_like() which creates a matrix with ones with the shape of the matrix passed; all these functions can also be used with matrices. |
Switch to the terminal
print m4 m4.T |
Let us now see, how to find out the transpose of a matrix we can do, |
As you saw, Matrix name dot capital T will give the transpose of a matrix
Pause the video here, try out the following exercise and resume the video. | |
Show Slide 6
Assignment 2:Frobenius norm & inverse |
Find out the Frobenius norm of inverse of a 4 by 4 matrix, the matrix being,
Unexpected indentation. m5 = arange(1,17).reshape(4,4) The Frobenius norm of a matrix is defined as, the square root of the sum of the absolute squares of its elements |
Continue from paused state Switch to the terminal
m5 = arange(1,17).reshape(4,4) print m5 |
Switch to terminal for the solution Let us create the matrix m5 by using the data provided in the question |
im5 = inv(m5) | 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. The Frobenius norm of a matrix is defined as square root of sum of squares of elements in the matrix. The inverse of a matrix can be found using the function inv(A). |
sum = 0
for each in im5.flatten(): sum += each * each print sqrt(sum) |
And the Frobenius norm of the matrix im5 can be found out as, |
Thus we have successfully obtained the Frobenius norm of the matrix m5
Pause the video here, try out the following exercise and resume the video. | |
Show Slide 7
Assignment 3: infinity norm |
Find out the infinity norm of the matrix im5. The infinity norm of a matrix is defined as the maximum value of sum of the absolute of elements in each row. |
Continue from paused state Switch to the terminal
sum_rows = [] for i in im5: sum_rows.append(abs(i).sum()) print max(sum_rows) |
Switch to terminal for the solution |
Show Slide 8
norm() method |
Well! to find the Frobenius norm and Infinity norm we have an even easier method, and let us see that now. |
The norm of a matrix can be found out using the method norm(). | |
Switch to the terminal
norm(im5) |
In order to find out the Frobenius norm of the matrix im5, we do, |
norm(im5,ord=inf) | And to find out the Infinity norm of the matrix im5, we do, |
det(m5) | This is easier when compared to the code we wrote. Read the documentation of norm to read up more about ord and the possible type of norms the norm function produces.
Now let us find out the determinant of a the matrix m5. The determinant of a square matrix can be obtained by using the function det() and the determinant of m5 can be found out as, |
Hence we get the determinant. Let us now move on to eigen vectors and eigen values | |
Show Slide 9
eigen vectors and eigen values |
The eigen values and eigen vector of a square matrix can be computed using the function eig() and eigvals(). |
Switch to the terminal
eig(m5) |
Let us find out the eigen values and eigen vectors of the matrix m5. We find them as, |
eig(m5)[0] | Note that it returned a tuple of two matrices. The first element in the tuple are the eigen values and the second element in the tuple are the eigen vectors. Thus the eigen values are given by, |
eig(m5)[1] | and the eigen vectors are given by, |
eigvals(m5) | The eigen values can also be computed using the function eigvals() as, |
Show Slide 10
Singular value decomposition |
Now let us learn how to do the singular value decomposition or S V D of a matrix.
Suppose M is an m (cross) n matrix, whose entries come from the field K, which is either the field of real numbers or the field of complex numbers. Then there exists a factorization of the form M = USigma V star where U is an (m by m) unitary matrix over K, the matrix Sigma is an (m by n) diagonal matrix with 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. Such a factorization is called the singular-value decomposition of M. |
Switch to the terminal
svd(m5) |
The SVD of matrix m5 can be found as |
Notice that it returned a tuple of 3 elements. The first one U the next one Sigma and the third one V star | |
Show Slide 11
Summary slide |
This brings us to the end of the end of this tutorial. In this tutorial, we have learnt to,
|
Show Slide 12
Self assessment questions slide |
Here are some self assessment questions for you to solve
|
Show Slide 13
Solution of self assessment questions on slide |
And the answers,
|
Show Slide 14
Acknowledgment slide |
Hope you have enjoyed this tutorial and found it useful. Thank you! |