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

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(One intermediate revision by the same user not shown)
Line 18: Line 18:
  
 
Objectives  
 
Objectives  
 
  
  
 
| 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,  
  
* find''' Frobenius''' and '''infinity''' '''norm''' of a '''matrix'''
+
* find''' Frobenius''' and '''infinity norm''' of a '''matrix'''
 
* find '''singular value decomposition''' of a '''matrix'''.
 
* find '''singular value decomposition''' of a '''matrix'''.
 
 
  
 
|-
 
|-
Line 37: Line 34:
 
* '''Python 3.4.3 '''and
 
* '''Python 3.4.3 '''and
 
* '''IPython 5.1.0'''
 
* '''IPython 5.1.0'''
 
 
  
 
|-
 
|-
Line 44: Line 39:
  
 
Pre-requisites  
 
Pre-requisites  
 
  
  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| To practise this tutorial, you should know about
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| To practise this tutorial, you should know about
  
* '''Lists, arrays '''and '''accessing parts of arrays '''and
+
* '''Lists, arrays '''and accessing parts of '''arrays '''and
* performing''' '''basic''' matrix operations'''
+
* performing basic''' matrix operations'''
  
If not, see the relevant Python tutorials on this website.
+
If not, see the relevant '''Python''' tutorials on this website.
  
 
|-
 
|-
Line 61: Line 55:
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| First we will see about '''flatten''' function.
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| First we will see about '''flatten''' function.
  
* '''flatten() '''function returns a copy of the array collapsed into one dimension.
+
* '''flatten() function''' returns a copy of the '''array''', collapsed into one '''dimension'''.
* It''' '''can be used to convert a '''multidimensional matrix''' into a '''single''' '''dimension matrix'''
+
* It can be used to convert a '''multidimensional matrix''' into a '''single dimension matrix'''
 
+
 
+
  
 
|-
 
|-
Line 80: Line 72:
  
  
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 89: Line 81:
  
 
'''a = asmatrix(arange(1,10).reshape(3,3))'''
 
'''a = asmatrix(arange(1,10).reshape(3,3))'''
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Now let us see how to create '''arrays'''
  
  
'''a'''
+
Type '''from numpy import asmatrix,arange'''
  
  
'''a.flatten()'''
+
'''a''''' is equal to''''' asmatrix '''''inside brackets''''' arange '''''inside brackets '''''1''''' comma '''''10''''' dot '''''reshape '''''inside brackets '''''3 '''''comma '''''3'''
  
 +
|-
 +
| 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 '''a'''
  
Highlight '''numpy import asmatrix,arange'''
 
  
 +
'''a.flatten()'''
  
Highlight '''(arange(1,10).reshape(3,3))'''
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Now let us see how to create '''arrays'''
 
  
 
+
Highlight '''numpy import asmatrix,arange'''
Type
+
| 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, '''a'''
 
+
'''from numpy import asmatrix,arange'''
+
 
+
 
+
'''a''''' is equal to''''' asmatrix '''''inside brackets''''' arange '''''inside brackets '''''1''''' comma '''''10''''' dot '''''reshape '''''inside brackets '''''3 '''''comma '''''3'''
+
 
+
 
+
Then type, '''a'''
+
  
  
Line 118: Line 103:
  
  
First we imported''' arange''' function from '''numpy''' module.
+
First we imported''' arange function''' from '''numpy module'''.
  
 
+
|-
Here, we can see '''3 by 3''' matrix is converted into one dimensional 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;"| Highlight '''(arange(1,10).reshape(3,3))'''
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Here, we can see '''3 by 3 matrix''' is converted into one '''dimensional matrix'''.
  
 
|-
 
|-
Line 129: Line 115:
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Next we will see about '''frobenius norm.'''
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Next we will see about '''frobenius norm.'''
  
* It is defined as the [http://mathworld.wolfram.com/SquareRoot.html square root] of the sum of the absolute squares of its elements.
+
It is defined as the '''square root''' of the sum of the '''absolute squares''' of its '''elements'''.
 
+
 
+
  
 
|-
 
|-
Line 144: Line 128:
  
 
Assignment 1: '''Frobenius''' '''norm'''  
 
Assignment 1: '''Frobenius''' '''norm'''  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Find out the '''Frobenius''' '''norm''' of the '''inverse''' of the given 4 by 4 '''matrix'''.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Find out the '''frobenius norm''' of the '''inverse''' of the given '''4 by 4 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;"| Switch to 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 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 156: Line 140:
  
  
Highlight asmatrix(arange(1,17).reshape(4,4))
+
Highlight '''asmatrix(arange(1,17).reshape(4,4))'''
 
+
 
+
Type,
+
 
+
'''<nowiki>m[0,1] = 0</nowiki>'''
+
 
+
'''<nowiki>m[1,</nowiki>3] =0'''
+
 
+
 
+
'''m'''
+
 
+
 
+
  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type
Line 175: Line 147:
  
  
Here, we have used '''asmatrix, arange''' and '''reshape''' functions.
+
Here, we have used '''asmatrix, arange''' and '''reshape functions'''.
  
  
We created a matrix of size 4 by 4 containing elements from 1 to 16.
+
We created a '''matrix''' of size '''4 by 4''' containing '''elements''' from 1 to 16.
  
 +
|-
 +
| 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,
  
Now type,
+
'''<nowiki>m[0,1] = 0</nowiki>'''
 +
 
 +
'''<nowiki>m[1,</nowiki>3] =0'''
 +
| 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,
  
 
'''m '''''inside square brackets '''''0 '''''comma '''''1''''' is equal to''''' 0'''
 
'''m '''''inside square brackets '''''0 '''''comma '''''1''''' is equal to''''' 0'''
Line 187: Line 164:
 
'''m '''''inside square brackets '''''1''''' comma '''''3 '''''is equal to''''' 0'''
 
'''m '''''inside square brackets '''''1''''' comma '''''3 '''''is equal to''''' 0'''
  
 
+
|-
Then type, '''m'''
+
| 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 '''m'''
 +
| 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, '''m'''
  
  
We changed the value of element at row 0 column 1 and row 1 column 3 to 0.
+
We changed the value of '''element''' at row 0 column 1 and row 1 column 3 to 0.
  
 
|-
 
|-
Line 203: Line 181:
  
  
Highlight numpy.linalg import inv, norm
+
Highlight '''numpy.linalg import inv, norm'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| In order to find out the '''Frobenius''' '''norm''' of the inverse of '''matrix''' '''m''', type 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;"| In order to find out the '''Frobenius norm''' of the '''inverse''' of '''matrix m''', type as shown.
  
  
'''norm''' function is available in''' numpy.linalg '''module
+
'''norm function''' is available in''' numpy.linalg module.'''
  
 
|-
 
|-
Line 213: Line 191:
  
 
'''Infinity norm'''
 
'''Infinity norm'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Next, we will see about infinity '''norm''' 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;"| Next, we will see about '''infinity norm''' of a '''matrix.'''
  
  
It is defined as the maximum value of sum of the '''absolute''' value of '''elements''' in each row.
+
It is defined as the maximum value of sum of the '''absolute value''' of '''elements''' in each row.
  
 
|-
 
|-
Line 227: Line 205:
 
|-
 
|-
 
| 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 Assignment 2: Infinity norm
 
| 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 Assignment 2: Infinity norm
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Find the infinity norm of the matrix '''im.'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Find the '''infinity norm''' of the '''matrix  im.'''
  
 
|-
 
|-
 
| 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 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 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 237: Line 215:
  
 
'''from numpy import infnorm(im,ord=inf)'''
 
'''from numpy import infnorm(im,ord=inf)'''
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| To find out the '''Infinity norm''' of the '''matrix im''', type as shown.
  
  
 
+
Here value for '''ord parameter''' is passed as '''inf''' to calculate '''infinity norm'''.
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| To find out the '''Infinity''' '''norm''' of the '''matrix''' '''im''', type as shown.
+
 
+
 
+
Here value for '''ord''' parameter is passed as '''inf''' to calculate '''infinity norm'''.
+
  
 
|-
 
|-
Line 259: Line 234:
  
  
In&nbsp;[https://en.wikipedia.org/wiki/Linear_algebra linear algebra],  
+
In '''linear algebra''', the '''singular value decomposition''' is factorization of '''real''' or '''complex matrix.'''  
 
+
* the&nbsp;'''singular value decomposition'''&nbsp;is factorization&nbsp;of '''real'''&nbsp;or&nbsp;'''complex&nbsp;matrix.'''<br/>
+
 
+
 
+
 
+
  
 
|-
 
|-
Line 278: Line 248:
  
  
Type,
 
  
'''U'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| The '''SVD''' of '''matrix m1''' can be found using '''svd function''' available in the '''numpy.linalg module'''.
  
'''sigma'''
 
  
'''V_conjugate'''
+
Type 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;"| The SVD of '''matrix''' m1 can be found using '''svd '''function available in the '''numpy.linalg '''module.
+
  
 +
'''svd''' returns a '''tuple''' of 3 '''elements'''.
  
Type as shown.
 
  
 +
We have unpacked these values into '''variable U, sigma''' and '''V''''' underscore '''''conjugate.'''
  
'''svd''' returns a tuple of 3 elements.  
+
|-
 +
| 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,
  
 +
'''U'''
  
We have unpacked these values into variable '''U''', '''sigma''' and '''V''''' underscore '''''conjugate.'''
+
'''sigma'''
  
 +
'''V_conjugate'''
  
Type, Capital '''U'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type, Capital '''U'''
  
  
Line 304: Line 275:
  
 
Type, Capital '''V''''' underscore '''''conjugate'''
 
Type, Capital '''V''''' underscore '''''conjugate'''
 +
|-
 +
| 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;"| Narration only
 +
| 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 validate the '''singular value decomposition''' by comparing the product of:
 +
 +
'''U, sigma''' and '''V '''''underscore '''''conjugate '''with''' m1'''
 +
 +
 +
'''sigma''' is a one '''dimensional array''' which contains only the '''diagonal elements''' of the '''matrix'''.
  
 
|-
 
|-
Line 313: Line 292:
  
 
'''smat = zeros((2,3))'''
 
'''smat = zeros((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;"| Type as shown.
  
  
'''smat'''
+
We first convert this '''array''' to a '''matrix'''.
 
+
 
+
'''<nowiki>smat[:2, :2] = diag(sigma)</nowiki>'''
+
 
+
 
+
Type,
+
  
 +
|-
 +
| 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,
 
'''smat'''
 
'''smat'''
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type '''smat'''
  
  
Type,
+
'''smat''' is a 2 by 3 '''zero matrix'''
 
+
'''allclose(m1, U * smat * V_conjugate)'''
+
 
+
 
+
 
+
| 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 validate the singular value decomposition by comparing the product of:
+
 
+
'''U''', '''sigma''' and '''V '''''underscore '''''conjugate '''with''' m1'''
+
 
+
 
+
'''sigma''' is a one dimensional array which contains only the diagonal elements of the matrix.
+
 
+
 
+
Type as shown.
+
 
+
 
+
We first convert this array to a matrix.
+
 
+
 
+
Type,
+
 
+
'''smat'''
+
 
+
 
+
'''smat''' is a 2 by 3 zero matrix
+
 
+
 
+
Now type,
+
  
 +
|-
 +
| 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,
 +
'''<nowiki>smat[:2, :2] = diag(sigma)</nowiki>'''
 +
| 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,
 
'''smat''''' inside square brackets colon '''''2 '''''comma''''' '''''colon '''''2 '''''is equal to''''' diag '''''inside brackets '''''sigma'''
 
'''smat''''' inside square brackets colon '''''2 '''''comma''''' '''''colon '''''2 '''''is equal to''''' diag '''''inside brackets '''''sigma'''
  
 +
|-
 +
| 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 '''smat'''
  
Then type,
+
| 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 '''smat'''
 
+
'''smat'''
+
  
  
 
This replaces values at row 0 column 0 and row 1 column 1 in '''smat '''with values from '''sigma. '''
 
This replaces values at row 0 column 0 and row 1 column 1 in '''smat '''with values from '''sigma. '''
  
 
+
|-
'''smat''' is a '''2 '''by''' 3''' matrix created for multiplication by placing values of '''sigma''' as diagonal elements and zero elsewhere.
+
| 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
 
+
'''allclose(m1, U * smat * V_conjugate)'''
 
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type as shown.
Type as shown.
+
  
  
Line 376: Line 328:
  
  
It means elements in '''m1''' and in product of''' U, sigma '''and''' V''''' underscore '''''conjugate '''are equal.
+
It means '''elements''' in '''m1''' and in product of''' U, sigma '''and''' V''''' underscore '''''conjugate '''are equal.
  
 
|-
 
|-
Line 390: Line 342:
 
In this tutorial, we have learnt to,
 
In this tutorial, we have learnt to,
  
* Calculate the '''norm''' of a '''matrix''' using the function '''norm()'''
+
* Calculate the '''norm''' of a '''matrix''' using the '''function norm()'''
* Calculate '''SVD''' of a '''matrix''' using the '''function''' '''svd()'''
+
* Calculate '''SVD''' of a '''matrix''' using the '''function svd()'''
 
+
 
+
  
 
|-
 
|-
 
| 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
  
Self assessment questions slide
+
Self assessment questions slide.
  
  
Line 406: Line 356:
 
1. '''norm''''' inside brackets '''''A '''''comma '''''ord''''' is equal to inside single quotes '''''fro''' is the same as '''norm '''''inside brackets '''''A'''
 
1. '''norm''''' inside brackets '''''A '''''comma '''''ord''''' is equal to inside single quotes '''''fro''' is the same as '''norm '''''inside brackets '''''A'''
  
True or False
+
True or False.
  
 
|-
 
|-

Latest revision as of 18:37, 7 December 2018

Title of script: Advanced Matrix Operations

Author: Puneeth, Thirumalesh H S, Arun KP

Keywords: Python, IPython, array, matrices, norm, svd, video tutorial


Visual Cue
Narration
Show Slide title Welcome to the spoken tutorial on Advanced matrix operations.
Show Slide

Objectives


In this tutorial, you will learn to,
  • find Frobenius and infinity norm of a matrix
  • find singular value decomposition of a matrix.
Show Slide

System Specifications

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

Pre-requisites


To practise this tutorial, you should know about
  • Lists, arrays and accessing parts of arrays and
  • performing basic matrix operations

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

Show Slide


flatten()

First we will see about flatten function.
  • flatten() function returns a copy of the array, collapsed into one dimension.
  • It can be used to convert a multidimensional matrix into a single dimension matrix
Open terminal Let us start ipython.


Open the terminal.

Type,

ipython3

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 asmatrix,arange


a = asmatrix(arange(1,10).reshape(3,3))

Now let us see how to create arrays


Type from numpy import asmatrix,arange


a is equal to asmatrix inside brackets arange inside brackets 1 comma 10 dot reshape inside brackets 3 comma 3

Type a


a.flatten()


Highlight numpy import asmatrix,arange

Then type, a


Now type, a dot flatten open and close brackets


First we imported arange function from numpy module.

Highlight (arange(1,10).reshape(3,3)) Here, we can see 3 by 3 matrix is converted into one dimensional matrix.
Show Slide

Frobenius norm of a matrix

Next we will see about frobenius norm.

It is defined as the square root of the sum of the absolute squares of its elements.

Pause the video.


Try this exercise and then resume the video.

Show Slide

Assignment 1: Frobenius norm

Find out the frobenius norm of the inverse of the given 4 by 4 matrix.
Switch to terminal Switch back to the terminal for the solution.
Type,

m = asmatrix(arange(1,17).reshape(4,4))


Highlight asmatrix(arange(1,17).reshape(4,4))

Type

m is equal to asmatrix inside brackets arange inside brackets 1 comma 17 dot reshape inside brackets 4 comma 4


Here, we have used asmatrix, arange and reshape functions.


We created a matrix of size 4 by 4 containing elements from 1 to 16.

Type,

m[0,1] = 0

m[1,3] =0

Now type,

m inside square brackets 0 comma 1 is equal to 0

m inside square brackets 1 comma 3 is equal to 0

Type m Then type, m


We changed the value of element at row 0 column 1 and row 1 column 3 to 0.

Type,

from numpy.linalg import inv, norm

im = inv(m)

norm(im)


Highlight numpy.linalg import inv, norm

In order to find out the Frobenius norm of the inverse of matrix m, type as shown.


norm function is available in numpy.linalg module.

Show Slide

Infinity norm

Next, we will see about infinity norm of a matrix.


It is defined as the maximum value of sum of the absolute value of elements in each row.

Pause the video.


Try this exercise and then resume the video.

Slide Assignment 2: Infinity norm Find the infinity norm of the matrix im.
Switch to terminal Switch back to the terminal for the solution.
Type,

from numpy import infnorm(im,ord=inf)

To find out the Infinity norm of the matrix im, type as shown.


Here value for ord parameter is passed as inf to calculate infinity norm.

Type, norm? To know more about norms type norm question mark


Press q to exit.

Show Slide

Singular value decomposition

Next we will see about singular value decomposition.


In linear algebra, the singular value decomposition is factorization of real or complex matrix.

Type,

from numpy import matrix

from numpy.linalg import svd

m1 = matrix([[3,2,2],[2,3,-2]])

U,sigma,V_conjugate = svd(m1)


The SVD of matrix m1 can be found using svd function available in the numpy.linalg module.


Type as shown.

svd returns a tuple of 3 elements.


We have unpacked these values into variable U, sigma and V underscore conjugate.

Type,

U

sigma

V_conjugate

Type, Capital U


Type, sigma


Type, Capital V underscore conjugate

Narration only We can validate the singular value decomposition by comparing the product of:

U, sigma and V underscore conjugate with m1


sigma is a one dimensional array which contains only the diagonal elements of the matrix.

Type,

from numpy import diag,allclose

from numpy.matlib import zeros

smat = zeros((2,3))

Type as shown.


We first convert this array to a matrix.

Type,

smat

Type smat


smat is a 2 by 3 zero matrix

Type,

smat[:2, :2] = diag(sigma)

Now type,

smat inside square brackets colon 2 comma colon 2 is equal to diag inside brackets sigma

Type smat Then type smat


This replaces values at row 0 column 0 and row 1 column 1 in smat with values from sigma.

Type

allclose(m1, U * smat * V_conjugate)

Type as shown.


It returns True.


It means elements in m1 and in product of U, sigma and V underscore conjugate are equal.

Show Slide

Summary


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


In this tutorial, we have learnt to,

  • Calculate the norm of a matrix using the function norm()
  • Calculate SVD of a matrix using the function svd()
Show Slide

Self assessment questions slide.


Here is a self assessment question for you to solve

1. norm inside brackets A comma ord is equal to inside single quotes fro is the same as norm inside brackets A

True or False.

Show Slide

Solution of self assessment questions on slide

And the answer is True since the order is equal to inside single quotes fro stands for Frobenius norm.
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.
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, Nirmala Venkat, Priyacst