Difference between revisions of "Python-3.4.3/C3/Basic-Matrix-Operations/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(2 intermediate revisions by 2 users not shown)
Line 23: Line 23:
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| In this tutorial, you will learn to,  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| In this tutorial, you will learn to,  
  
* Create '''matrices''' from lists
+
* Create '''matrices''' from '''lists'''
* Perform basic '''matrix''' operations like
+
* Perform basic '''matrix operations''' like
 
** addition
 
** addition
 
** subtraction and  
 
** subtraction and  
 
** multiplication
 
** multiplication
* Perform operations to find out  
+
* Perform '''operations''' to find out  
 
+
** '''determinant''' of a '''matrix'''  
* '''determinant''' of a '''matrix'''  
+
** '''inverse''' of a '''matrix'''
* '''inverse''' of a '''matrix'''
+
** '''Eigen values''' and '''Eigen vectors''' of a '''matrix '''
* '''eigenvalues''' and '''eigenvectors''' of a '''matrix '''
+
 
+
 
+
  
 
|-
 
|-
Line 45: Line 42:
 
* '''Python 3.4.3'''
 
* '''Python 3.4.3'''
 
* '''IPython 5.1.0'''
 
* '''IPython 5.1.0'''
 
 
  
 
|-
 
|-
Line 58: Line 53:
  
 
* '''Lists'''
 
* '''Lists'''
* '''arrays '''and '''accessing parts of arrays '''and
+
* '''Arrays '''and '''accessing parts of arrays '''and
* theoretical knowledge of '''matrix operations'''
+
* Theoretical knowledge of '''matrix operations'''
  
 
If not, see the relevant '''Python''' tutorials on this website.
 
If not, see the relevant '''Python''' tutorials on this website.
Line 65: Line 60:
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Slide:
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Slide:
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| * In python, we create a matrix using '''numpy matrix class.'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"|  
* Matrix operations can be done using '''numpy''' operators and functions.
+
* In '''Python''', we create a '''matrix''' using '''numpy matrix class.'''
 
+
* '''Matrix operations''' can be done using '''numpy operators''' and '''functions'''.
 
+
  
 
|-
 
|-
Line 84: Line 78:
  
 
Press''' Enter'''
 
Press''' Enter'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type '''ipython3 '''and press Enter.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type '''ipython3 '''and press '''Enter'''.
  
  
From here onwards remember to press the '''Enter''' key after typing every command on the terminal.
+
From here onwards, remember to press the '''Enter''' key after typing every '''command''' on the '''terminal'''.
  
 
|-
 
|-
Line 94: Line 88:
  
 
'''from numpy import matrix'''
 
'''from numpy import matrix'''
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Let us create a '''matrix m1.'''
  
'''m1 = matrix<nowiki>([1,2,3,4])</nowiki>'''
 
  
'''m1'''
+
Type '''from numpy import matrix'''
  
Point to the output
+
|-
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Let us create a matrix''' m1.'''
+
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type,'''m1 = matrix<nowiki>([1,2,3,4])</nowiki>'''
 
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Then type,
 
+
Type,
+
 
+
'''from numpy import matrix'''
+
 
+
 
+
Then type,
+
  
 
'''m1 '''''is equal to''''' matrix '''''inside brackets inside square brackets '''''1''''' comma '''''2''''' comma '''''3''''' comma '''''4'''
 
'''m1 '''''is equal to''''' matrix '''''inside brackets inside square brackets '''''1''''' comma '''''2''''' comma '''''3''''' comma '''''4'''
 
+
|-
 
+
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type,'''m1'''
Now type,
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Now type '''m1'''
 
+
|-
'''m1'''
+
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Point to the output
 
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| This creates a '''matrix''' with one row and four columns.
 
+
This creates a matrix with one row and four columns.
+
  
 
|-
 
|-
Line 127: Line 112:
  
 
Highlight the output
 
Highlight the output
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| This can be verified by typing
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| This can be verified by typing '''m1.shape'''
 
+
'''m1.shape'''
+
  
  
Line 142: Line 125:
  
 
'''print(m2)'''
 
'''print(m2)'''
 
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| A '''list''' can also be converted to a '''matrix''' as follows,  
 
+
Highlight the output
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| A list can also be converted to a '''matrix''' as follows,  
+
  
  
 
Type as shown.
 
Type as shown.
  
 
+
|-
You can see the matrix '''m2''' with values from list '''l1.'''
+
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Highlight the output
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| You can see the '''matrix m2''' with values from '''list l1.'''
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Slide:'''asmatrix'''
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Slide:'''asmatrix'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| * To convert an array to a matrix, use the '''asmatrix''' method in '''numpy '''module.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"|  
* We can use '''arange '''and '''reshape methods '''to generate an array.  
+
* To convert an '''array''' to a '''matrix''', use the '''asmatrix method''' in '''numpy module'''.
 
+
* We can use '''arange '''and '''reshape methods '''to generate an '''array'''.  
 
+
  
 
|-
 
|-
Line 174: Line 154:
  
  
'''arange''' is a method available in''' numpy.'''
+
'''arange''' is a '''method''' available in''' numpy.'''
  
  
Here it returns an array of evenly spaced values between '''1 '''and''' 9.'''
+
Here it returns an '''array''' of evenly spaced values between '''1 '''and''' 9.'''
  
  
'''reshape''' is used to change the shape of the array to 2 rows and 4 columns.
+
'''reshape''' is used to change the shape of the '''array''' to 2 rows and 4 columns.
  
  
'''asmatrix '''is a method available in '''numpy '''and it''' '''interprets the input as a matrix.
+
'''asmatrix '''is a '''method''' available in '''numpy '''and it interprets the input as a '''matrix'''.
  
 
|-
 
|-
Line 196: Line 176:
  
 
Assignment 1
 
Assignment 1
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Create a two dimensional '''matrix''' '''m3 '''of '''shape''' 2 by 4 with the elements 5, 6, 7, 8, 9, 10, 11, 12.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Create a two dimensional '''matrix m3 '''of '''shape''' 2 by 4 with the '''elements''' 5, 6, 7, 8, 9, 10, 11, 12.
  
  
Line 203: Line 183:
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Switch to the terminal
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Switch to the terminal
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Switch back to the terminal for the solution.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Switch back to the '''terminal''' for the solution.
  
 
|-
 
|-
Line 217: Line 197:
  
  
Type,''' m3'''
+
Type''' m3'''
  
 
You can see the required output.
 
You can see the required output.
Line 225: Line 205:
  
 
'''m3 + m2'''
 
'''m3 + m2'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Next let us see some '''matrix''' operations.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Next let us see some '''matrix operations'''.
  
  
Line 231: Line 211:
  
  
It performs element by element addition, that is '''matrix''' addition.
+
It performs '''element''' by '''element''' addition, that is '''matrix''' addition.
  
  
Line 243: Line 223:
  
  
It performs '''matrix''' subtraction, that is element by element subtraction.
+
It performs '''matrix''' subtraction, that is '''element''' by '''element''' subtraction.
  
  
Line 252: Line 232:
  
 
'''6.5 * m2'''
 
'''6.5 * m2'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Now we can multiply a scalar i.e a number by a matrix as shown.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Now we can multiply a '''scalar''' i.e a number by a '''matrix''' as shown.
  
 
|-
 
|-
Line 264: Line 244:
  
  
We get a '''tuple''' '''(2, 4).'''
+
We get a '''tuple (2, 4).'''
  
  
'''Matrix''' '''m2''' is of the shape, two by four,
+
'''Matrix m2''' is of the shape, two by four.
  
 
|-
 
|-
Line 284: Line 264:
  
 
'''m4.shape'''
 
'''m4.shape'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Now to check the shape, type,
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Now to check the shape, type '''m4.shape'''
 
+
'''m4.shape'''
+
  
  
Line 297: Line 275:
  
 
Highlight the output
 
Highlight the output
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| The''' multiplication operator''' ''asterisk ''is used for '''matrix''' multiplication.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| The multiplication '''operator ''asterisk '''''is used for '''matrix''' multiplication.
  
  
Type,
+
Type '''m2 '''''asterisk''''' m4'''
 
+
'''m2 '''''asterisk''''' m4'''
+
  
  
Line 317: Line 293:
  
  
To see the content of m4, type  
+
To see the content of '''m4''', type '''print''''' inside brackets '''''m4'''
 
+
'''print''''' inside brackets '''''m4'''
+
  
 
|-
 
|-
Line 333: Line 307:
  
  
As you saw, '''m4''' dot''' capital T''' will give the transpose of a '''matrix.'''
+
As you saw, '''m4''' dot''' capital T''' will give the '''transpose''' of a '''matrix.'''
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Show Slide:Determinant of a matrix
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Show Slide:Determinant of a matrix
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| The '''determinant''' of a square '''matrix''' is obtained by using the function '''det() '''in''' numpy.linalg''' module.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| We can get the  '''determinant''' of a '''square matrix''' by using the '''function det() '''in''' numpy.linalg module'''.
  
 
|-
 
|-
Line 352: Line 326:
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Switch to the terminal for solution.  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Switch to the terminal for solution.  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Switch to the terminal for the solution.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Switch to the '''terminal''' for the solution.
  
 
|-
 
|-
Line 365: Line 339:
  
  
The '''determinant''' of '''m5''' can be found by issuing the '''command,'''
+
The '''determinant''' of '''m5''' can be found by issuing the '''command'''
  
 
'''det '''''inside brackets '''''m5'''
 
'''det '''''inside brackets '''''m5'''
  
  
We get determinant of '''m5''' as output.
+
We get '''determinant''' of '''m5''' as output.
  
 
|-
 
|-
Line 376: Line 350:
  
 
'''Inverse''' of a '''matrix'''
 
'''Inverse''' of a '''matrix'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| The '''inverse''' of a square '''matrix''' can be obtained using '''inv() function '''in''' numpy.linalg '''module.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| We can get the  '''inverse''' of a '''square matrix''' by using '''inv() function '''in''' numpy.linalg module'''.
  
 
|-
 
|-
Line 395: Line 369:
  
  
Then to see the the inverse, type
+
Then to see the the '''inverse''', type '''im5'''
 
+
'''im5'''
+
  
 
|-
 
|-
Line 414: Line 386:
  
 
Highlight '''allclose'''
 
Highlight '''allclose'''
 
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type '''from numpy import eye,allclose'''
 
+
Type,
+
 
+
'''eye?'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type,
+
 
+
'''from numpy import eye,allclose'''
+
  
  
Line 432: Line 397:
  
  
We know that multiplication of a matrix with its inverse gives the '''identity matrix'''.
+
We know that multiplication of a '''matrix''' with its '''inverse''' gives the '''identity matrix'''.
  
  
'''Identity matrix''' is created using '''eye()''' function. It is present in the '''numpy''' module.
+
'''Identity matrix''' is created using '''eye() function'''. It is present in the '''numpy module'''.
  
  
Here '''asmatrix '''''inside brackets '''''eye '''''inside brackets '''''3''' gives identity matrix of size 3'''.'''
+
Here '''asmatrix '''''inside brackets '''''eye '''''inside brackets '''''3''' gives '''identity matrix''' of size 3.
  
  
'''allclose '''is a function that returns '''True''' if two arrays are element-wise equal.
+
'''allclose '''is a '''function''' that returns '''True''' if two '''arrays''' are '''element'''-wise equal.
  
 +
|-
 +
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type,
  
To know more about these, we will check the documentation.
+
'''eye?'''
  
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| To know more about these, we will check the documentation.
  
Type the function name followed by a question mark in IPython console.
 
  
 +
Type the '''function''' name followed by a question mark in '''IPython console.'''
  
Type,
 
  
'''eye''''' question mark''
+
Type '''eye''''' question mark''
  
  
Line 458: Line 425:
  
  
It is a good practice to read documentation of new functions that you come across.
+
It is a good practice to read documentation of new '''functions''' that you come across.
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Show Slide
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Show Slide
  
'''eigenvectors''' and '''eigenvalues'''
+
'''Eigen vectors''' and '''Eigen values'''
  
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Let us now move onto '''eigenvectors''' and '''eigenvalues'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Let us now move onto '''Eigen vectors''' and '''Eigen values.'''
  
  
Line 476: Line 443:
 
* '''eigvals '''''inside brackets '''''A '''gives its '''eigenvalues'''  
 
* '''eigvals '''''inside brackets '''''A '''gives its '''eigenvalues'''  
  
'''eig''' and '''eigvals''' functions are present in '''numpy.linalg''' module.
+
'''eig''' and '''eigvals functions''' are present in '''numpy.linalg module'''.
  
 
|-
 
|-
Line 487: Line 454:
 
'''m6=asmatrix(diag((1, 2, 3)))'''
 
'''m6=asmatrix(diag((1, 2, 3)))'''
  
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Let us find out the '''eigenvalues''' and '''eigenvectors''' of the '''matrix m6'''.
  
Type,
+
 
 +
Type as shown.
 +
 
 +
|-
 +
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type,
  
 
'''eig(m6)'''
 
'''eig(m6)'''
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Now to see the value, type,'''eig '''''inside brackets '''''m6'''
  
 +
|-
 +
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Highlight '''diag((1, 2, 3)))'''
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| '''diag '''''inside brackets again inside brackets '''''1 '''''comma''''' 2 '''''comma '''''3 '''
  
Highlight '''diag((1, 2, 3)))'''
+
creates a '''diagonal matrix''' with 1,2,3 as diagonal '''elements''' and '''0 '''elsewhere .
  
 
+
|-
Highlight
+
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Highlight
  
 
'''<nowiki>(array([1., 2., 3.]), matrix([[1., 0., 0.],</nowiki>'''
 
'''<nowiki>(array([1., 2., 3.]), matrix([[1., 0., 0.],</nowiki>'''
Line 503: Line 479:
  
 
'''<nowiki>[0., 0., 1.]]))</nowiki>'''
 
'''<nowiki>[0., 0., 1.]]))</nowiki>'''
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"|'''diag() function '''is present in''' numpy module.'''
  
  
Put box to array
+
Note that '''eig '''''inside brackets '''''m6''' returned a '''tuple''' of one '''array''' and one '''matrix'''.
  
 +
|-
 +
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Put box to array
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"|The first '''element''' in the '''tuple''' is an '''array''' of three '''eigen values'''.
  
Put box to matrix
+
|-
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Let us find out the '''eigenvalues''' and '''eigenvectors''' of the '''matrix''' '''m6'''.
+
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Put box to matrix
 
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"|The second '''element''' in the '''tuple''' is a '''matrix''' of three '''eigen vectors'''.  
 
+
Type as shown.
+
 
+
 
+
Now to see the value, type,'''eig '''''inside brackets '''''m6'''
+
 
+
 
+
'''diag '''''inside brackets again inside brackets '''''1 '''''comma''''' 2 '''''comma '''''3 '''creates a '''diagonal matrix with 1,2,3''' as diagonal elements and '''0 '''elsewhere .
+
 
+
 
+
'''diag() function '''is present in''' numpy '''module
+
 
+
 
+
Note that '''eig '''''inside brackets '''''m6''' returned a tuple of one array and one matrix.
+
 
+
 
+
The first element in the tuple is an array of three eigenvalues.
+
 
+
 
+
The second element in the tuple is a matrix of three eigenvectors.  
+
  
 
|-
 
|-
Line 538: Line 498:
  
 
'''eig_value'''
 
'''eig_value'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| To get '''eigenvalues '''type,'''eig''''' underscore '''''value''''' is equal to''''' eig''''' inside brackets '''''m6 '''''inside square brackets '''''0'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| To get '''eigen values '''type,'''eig''''' underscore '''''value''''' is equal to''''' eig''''' inside brackets '''''m6 '''''inside square brackets '''''0'''
  
  
Then type,
+
Then type '''eig '''''underscore '''''value'''
 
+
'''eig '''''underscore '''''value'''
+
  
  
Line 552: Line 510:
  
 
'''<nowiki>eig_vector = eig(m6)[1]</nowiki>'''
 
'''<nowiki>eig_vector = eig(m6)[1]</nowiki>'''
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| To get '''eigen vectors '''type,'''eig '''''underscore '''''vector '''''is equal to''''' eig''''' inside brackets '''''m6 '''''inside square brackets '''''1'''
 +
 +
|-
 +
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type,
  
 
'''eig_vector'''
 
'''eig_vector'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| To get '''eigenvectors '''type,'''eig '''''underscore '''''vector '''''is equal to''''' eig''''' inside brackets '''''m6 '''''inside square brackets '''''1'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Then type '''eig '''''underscore '''''vector'''
  
  
Then type,
+
'''eig '''''underscore '''''vector '''contains '''eigen vector'''.
 
+
'''eig '''''underscore '''''vector'''
+
 
+
 
+
'''eig '''''underscore '''''vector '''contains eigenvector.
+
  
 
|-
 
|-
Line 570: Line 527:
  
 
'''eig_value1 = eigvals(m6)'''
 
'''eig_value1 = eigvals(m6)'''
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| The '''eigen values''' can also be computed using '''eigvals() function'''.
  
  
Then type,
+
Type as shown.
  
'''eig_value1'''
+
|-
 +
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Then type '''eig_value1'''
  
  
Show both the outputs
+
Show both the outputs.
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| The '''eigenvalues''' can also be computed using '''eigvals() '''function.
+
  
 
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Then type '''eig '''''underscore '''''value1'''
Type as shown.
+
 
+
 
+
Then type,
+
 
+
'''eig '''''underscore '''''value1'''
+
  
 
You can see that, '''eig '''''underscore '''''value '''and''' eig '''''underscore '''''value1 '''are same.
 
You can see that, '''eig '''''underscore '''''value '''and''' eig '''''underscore '''''value1 '''are same.
Line 603: Line 555:
  
 
* Create '''matrices''' using '''arrays'''
 
* Create '''matrices''' using '''arrays'''
* Add, subtract and multiply matrices
+
* Add, subtract and multiply '''matrices'''
* Take scalar multiple of a matrix
+
* Take '''scalar''' multiple of a '''matrix'''
* Use the '''function''' '''det()''' to find the '''determinant''' of a '''matrix'''
+
* Use the '''function det()''' to find the '''determinant''' of a '''matrix'''
* Find out the '''inverse''' of a '''matrix''',using the function '''inv() '''
+
* Find out the '''inverse''' of a '''matrix''' using the '''function inv() '''
* Find out the '''eigenvectors''' and '''eigenvalues''' of a '''matrix''', using the functions '''eig()''' and '''eigvals()'''
+
* Find out the '''eigen vectors''' and '''eigen values''' of a '''matrix''', using the '''functions eig()''' and '''eigvals()'''
 
+
 
+
  
 
|-
 
|-
Line 620: Line 570:
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Here are some self assessment questions for you to solve
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Here are some self assessment questions for you to solve
  
# A and B are two matrix objects of appropriate sizes. Which one of the below is correct for Matrix multiplication?
+
# A and B are two '''matrix objects''' of appropriate sizes. Which one of the below is correct for '''matrix''' multiplication?
# '''eig '''''inside brackets '''''A''''' inside square brackets '''''1''' and '''eigvals '''''inside brackets '''''A''' are the same. True or False
+
# '''eig '''''inside brackets '''''A''''' inside square brackets '''''1''' and '''eigvals '''''inside brackets '''''A''' are the same. True or False?
 
+
 
+
  
 
|-
 
|-
Line 634: Line 582:
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| And the answers,
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| And the answers,
  
# Matrix multiplication between A and B is done by, A ''asterisk'' B
+
# '''Matrix''' multiplication between A and B is done by, A ''asterisk'' B
# False. '''eig '''''inside brackets '''''A''''' inside square brackets '''''0''' and '''eigvals '''''inside brackets '''''A''' are same, that is both will give the '''eigenvalues''' of '''matrix''' A.
+
# False. '''eig '''''inside brackets '''''A''''' inside square brackets '''''0''' and '''eigvals '''''inside brackets '''''A''' are same, that is both will give the '''eigen values''' of '''matrix''' A.
 
+
 
+
  
 
|-
 
|-

Latest revision as of 18:26, 7 December 2018

Title of script: Basic Matrix Operations

Author: Puneeth, Thirumalesh H S, Arun KP

Keywords: Python, IPython, matrices, determinant, reshape, arange, eigen values, eigen vectors, transpose of matrix


Visual Cue
Narration
Show Slide title Welcome to the spoken tutorial on Basic Matrix Operations.
Show Slide

Objectives


In this tutorial, you will learn to,
  • Create matrices from lists
  • Perform basic matrix operations like
    • addition
    • subtraction and
    • multiplication
  • Perform operations to find out
    • determinant of a matrix
    • inverse of a matrix
    • Eigen values and Eigen vectors of a matrix
Show Slide

System Specifications

To record this tutorial, I am using
  • Ubuntu Linux 16.04 operating system
  • Python 3.4.3
  • IPython 5.1.0
Show Slide

Pre-requisites


To practise this tutorial, you should have basic knowledge about
  • Lists
  • Arrays and accessing parts of arrays and
  • Theoretical knowledge of matrix operations

If not, see the relevant Python tutorials on this website.

Slide:
  • In Python, we create a matrix using numpy matrix class.
  • Matrix operations can be done using numpy operators and functions.
Type,

ipython3

Let us start ipython.


Open the terminal.

Type ipython3


Press Enter

Type ipython3 and press Enter.


From here onwards, remember to press the Enter key after typing every command on the terminal.

Type,


from numpy import matrix

Let us create a matrix m1.


Type from numpy import matrix

Type,m1 = matrix([1,2,3,4]) Then type,

m1 is equal to matrix inside brackets inside square brackets 1 comma 2 comma 3 comma 4

Type,m1 Now type m1
Point to the output This creates a matrix with one row and four columns.
Type,

m1.shape


Highlight the output

This can be verified by typing m1.shape


This gives the output as (1, 4)

Type,

l1 = [[1,2,3,4],[5,6,7,8]]

m2 = matrix(l1)

print(m2)

A list can also be converted to a matrix as follows,


Type as shown.

Highlight the output You can see the matrix m2 with values from list l1.
Slide:asmatrix
  • To convert an array to a matrix, use the asmatrix method in numpy module.
  • We can use arange and reshape methods to generate an array.
Highlight according to narration


Type,

from numpy import asmatrix,arange

m2_array = asmatrix(arange(1,9).reshape(2,4))

m2_array

Type as shown.


arange is a method available in numpy.


Here it returns an array of evenly spaced values between 1 and 9.


reshape is used to change the shape of the array to 2 rows and 4 columns.


asmatrix is a method available in numpy and it interprets the input as a matrix.

Pause the video.


Try this exercise and then resume the video.

Show Slide

Assignment 1

Create a two dimensional matrix m3 of shape 2 by 4 with the elements 5, 6, 7, 8, 9, 10, 11, 12.


Hint: Use arange() and reshape() methods and asmatrix() function.

Switch to the terminal Switch back to the terminal for the solution.
Type,

m3 = asmatrix(arange(5,13).reshape(2,4))


Type, m3

Type,

m3 is equal to asmatrix inside brackets arange inside brackets 5 comma 13 dot reshape inside brackets 2 comma 4


Type m3

You can see the required output.

Type,

m3 + m2

Next let us see some matrix operations.


Type, m3 plus m2


It performs element by element addition, that is matrix addition.


Note that both the matrices should be of the same shape.

Type,

m3 - m2

Similarly, type m3 minus m2


It performs matrix subtraction, that is element by element subtraction.


Note that both the matrices should be of the same shape.

Type,

6.5 * m2

Now we can multiply a scalar i.e a number by a matrix as shown.
Type,


m2.shape

Next we will check the size of m2 by typing,

m2.shape.


We get a tuple (2, 4).


Matrix m2 is of the shape, two by four.

Type,

m4 = asmatrix(arange(1,9).reshape(4,2))

Let us create another matrix, of the order 4 by 2.


Type,

m4 is equal to asmatrix inside brackets arange inside brackets 1 comma 9 dot reshape inside brackets 4 comma 2

Type

m4.shape

Now to check the shape, type m4.shape


We get (4,2) as the shape of m4.

Type,

m2 * m4

Highlight the output

The multiplication operator asterisk is used for matrix multiplication.


Type m2 asterisk m4


Now we get output as multiplication of m2 and m4.

Type,

print (m4)


Let us now see, how to find out the transpose of a matrix.


To see the content of m4, type print inside brackets m4

Type,

print(m4.T)


Point to the output

Now type,

print inside brackets m4 dot capital T


As you saw, m4 dot capital T will give the transpose of a matrix.

Show Slide:Determinant of a matrix We can get the determinant of a square matrix by using the function det() in numpy.linalg module.
Pause the video.


Try this exercise and resume the video.

Show Slide: Exercise Find out the determinant of this 3 by 3 matrix.
Switch to the terminal for solution. Switch to the terminal for the solution.
Type,

from numpy.linalg import det

m5 = matrix([[2,-3,1],[2,0,-1],[1,4,5]])

det(m5)

Type as shown.


The determinant of m5 can be found by issuing the command

det inside brackets m5


We get determinant of m5 as output.

Show Slide

Inverse of a matrix

We can get the inverse of a square matrix by using inv() function in numpy.linalg module.
Type,

from numpy.linalg import inv

im5 = inv(m5)


Type,

im5

Let us find the inverse of the matrix m5.


Type as shown.


Then to see the the inverse, type im5

Type,

from numpy import eye,allclose

allclose(im5 * m5, asmatrix(eye(3)))


Highlight eye


Highlight asmatrix(eye(3)))


Highlight allclose

Type from numpy import eye,allclose


Then type,

allclose inside brackets im5 asterisk m5 comma asmatrix inside brackets eye inside brackets 3


This returns True.


We know that multiplication of a matrix with its inverse gives the identity matrix.


Identity matrix is created using eye() function. It is present in the numpy module.


Here asmatrix inside brackets eye inside brackets 3 gives identity matrix of size 3.


allclose is a function that returns True if two arrays are element-wise equal.

Type,

eye?

To know more about these, we will check the documentation.


Type the function name followed by a question mark in IPython console.


Type eye question mark


To quit the documentation, press q.


It is a good practice to read documentation of new functions that you come across.

Show Slide

Eigen vectors and Eigen values


Let us now move onto Eigen vectors and Eigen values.


Given a square matrix A

  • eig inside brackets A inside square brackets 0 gives its eigenvalues
  • eig inside brackets A inside square brackets 1 gives its eigenvector
  • eigvals inside brackets A gives its eigenvalues

eig and eigvals functions are present in numpy.linalg module.

Type,

from numpy import diag

from numpy.linalg import eig

m6=asmatrix(diag((1, 2, 3)))

Let us find out the eigenvalues and eigenvectors of the matrix m6.


Type as shown.

Type,

eig(m6)

Now to see the value, type,eig inside brackets m6
Highlight diag((1, 2, 3))) diag inside brackets again inside brackets 1 comma 2 comma 3

creates a diagonal matrix with 1,2,3 as diagonal elements and 0 elsewhere .

Highlight

(array([1., 2., 3.]), matrix([[1., 0., 0.],

[0., 1., 0.],

[0., 0., 1.]]))

diag() function is present in numpy module.


Note that eig inside brackets m6 returned a tuple of one array and one matrix.

Put box to array The first element in the tuple is an array of three eigen values.
Put box to matrix The second element in the tuple is a matrix of three eigen vectors.
Type,

eig_value = eig(m6)[0]

eig_value

To get eigen values type,eig underscore value is equal to eig inside brackets m6 inside square brackets 0


Then type eig underscore value


As you can see eig underscore value contains eigenvalues.

Type,

eig_vector = eig(m6)[1]

To get eigen vectors type,eig underscore vector is equal to eig inside brackets m6 inside square brackets 1
Type,

eig_vector

Then type eig underscore vector


eig underscore vector contains eigen vector.

Type,

from numpy.linalg import eigvals

eig_value1 = eigvals(m6)

The eigen values can also be computed using eigvals() function.


Type as shown.

Then type eig_value1


Show both the outputs.

Then type eig underscore value1

You can see that, eig underscore value and eig underscore value1 are same.

Show Slide

Summary


This brings us to the end of this tutorial. Let us summarize.


In this tutorial, we have learnt to,

  • Create matrices using arrays
  • Add, subtract and multiply matrices
  • Take scalar multiple of a matrix
  • Use the function det() to find the determinant of a matrix
  • Find out the inverse of a matrix using the function inv()
  • Find out the eigen vectors and eigen values of a matrix, using the functions eig() and eigvals()
Show Slide

Self assessment questions


Here are some self assessment questions for you to solve
  1. A and B are two matrix objects of appropriate sizes. Which one of the below is correct for matrix multiplication?
  2. eig inside brackets A inside square brackets 1 and eigvals inside brackets A are the same. True or False?
Show Slide 13

Solution of self assessment questions


And the answers,
  1. Matrix multiplication between A and B is done by, A asterisk B
  2. False. eig inside brackets A inside square brackets 0 and eigvals inside brackets A are same, that is both will give the eigen values of matrix A.
Show Slide Forum Please post your timed queries in this forum.
Show Slide Fossee Forum Please post your general queries on Python in this forum.
Show slide TBC FOSSEE team coordinates the TBC project.
Show Slide

Acknowledgment

http://spoken-tutorial.org

Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.

For more details, visit this website.

Previous slide This is Priya from IIT Bombay signing off.

Thanks for watching.

Contributors and Content Editors

Nancyvarkey, Priyacst