day1/session4.tex
changeset 263 8a4a1e5aec85
parent 253 e446ed7287d7
child 268 f978ddc90960
equal deleted inserted replaced
262:9664eddee075 263:8a4a1e5aec85
   126 
   126 
   127 \section{Matrices}
   127 \section{Matrices}
   128 
   128 
   129 \begin{frame}
   129 \begin{frame}
   130 \frametitle{Matrices: Introduction}
   130 \frametitle{Matrices: Introduction}
   131 We looked at the Van der Monde matrix in the previous session,\\ 
   131 Let us now look at matrices in detail.\\
   132 let us now look at matrices in a little more detail.
   132 \alert{All matrix operations are done using \kwrd{arrays}}
   133 \end{frame}
   133 \end{frame}
   134 
   134 
   135 \subsection{Initializing}
   135 \subsection{Initializing}
   136 \begin{frame}[fragile]
   136 \begin{frame}[fragile]
   137 \frametitle{Matrices: Initializing}
   137 \frametitle{Matrices: Initializing}
   138 \begin{lstlisting}
   138 \begin{lstlisting}
   139 In []: A = matrix([[ 1,  1,  2, -1],
   139 In []: A = array([[ 1,  1,  2, -1],
   140                    [ 2,  5, -1, -9],
   140                   [ 2,  5, -1, -9],
   141                    [ 2,  1, -1,  3],
   141                   [ 2,  1, -1,  3],
   142                    [ 1, -3,  2,  7]])
   142                   [ 1, -3,  2,  7]])
   143 In []: A
   143 In []: A
   144 Out[]: 
   144 Out[]: 
   145 matrix([[ 1,  1,  2, -1],
   145 array([[ 1,  1,  2, -1],
   146         [ 2,  5, -1, -9],
   146        [ 2,  5, -1, -9],
   147         [ 2,  1, -1,  3],
   147        [ 2,  1, -1,  3],
   148         [ 1, -3,  2,  7]])
   148        [ 1, -3,  2,  7]])
   149 \end{lstlisting}
   149 \end{lstlisting}
       
   150 \end{frame}
       
   151 
       
   152 \begin{frame}[fragile]
       
   153   \frametitle{Accessing elements of matrices}
       
   154 \begin{small}
       
   155   \begin{lstlisting}
       
   156 In []: C = array([[1,1,2],
       
   157                   [2,4,1],
       
   158                   [-1,3,7]])
       
   159 In []: C[1,2]
       
   160 Out[]: 1
       
   161 
       
   162 In []: C[1]
       
   163 Out[]: array([2, 4, 1])
       
   164 
       
   165 In []: C[1,1] = -2
       
   166 In []: C
       
   167 Out[]: 
       
   168 array([[ 1,  1,  2],
       
   169        [ 2, -2,  1],
       
   170        [-1,  3,  7]])
       
   171   \end{lstlisting}
       
   172 \end{small}
   150 \end{frame}
   173 \end{frame}
   151 
   174 
   152 \subsection{Basic Operations}
   175 \subsection{Basic Operations}
   153 
   176 
   154 \begin{frame}[fragile]
   177 \begin{frame}[fragile]
   155 \frametitle{Transpose of a Matrix}
   178 \frametitle{Transpose of a Matrix}
   156 \begin{lstlisting}
   179 \begin{lstlisting}
   157 In []: linalg.transpose(A)
   180 In []: A.T
   158 Out[]:
   181 Out[]:
   159 matrix([[ 1,  2,  2,  1],
   182 array([[ 1,  2,  2,  1],
   160         [ 1,  5,  1, -3],
   183        [ 1,  5,  1, -3],
   161         [ 2, -1, -1,  2],
   184        [ 2, -1, -1,  2],
   162         [-1, -9,  3,  7]])
   185        [-1, -9,  3,  7]])
   163 \end{lstlisting}
   186 \end{lstlisting}
   164 \end{frame}
   187 \end{frame}
   165 
   188 
   166 \begin{frame}[fragile]
   189 \begin{frame}[fragile]
   167   \frametitle{Sum of all elements}
   190   \frametitle{Sum of all elements}
   168   \begin{lstlisting}
   191   \begin{lstlisting}
   169 In []: linalg.sum(A)
   192 In []: sum(A)
   170 Out[]: 12
   193 Out[]: 12
   171   \end{lstlisting}
   194   \end{lstlisting}
   172 \end{frame}
   195 \end{frame}
   173 
   196 
   174 \begin{frame}[fragile]
   197 \begin{frame}[fragile]
   175   \frametitle{Matrix Addition}
   198   \frametitle{Matrix Addition}
   176   \begin{lstlisting}
   199   \begin{lstlisting}
   177 In []: B = matrix([[3,2,-1,5],
   200 In []: B = array([[3,2,-1,5],
   178                    [2,-2,4,9],
   201                   [2,-2,4,9],
   179                    [-1,0.5,-1,-7],
   202                   [-1,0.5,-1,-7],
   180                    [9,-5,7,3]])
   203                   [9,-5,7,3]])
   181 In []: linalg.add(A,B)
   204 In []: A + B
   182 Out[]: 
   205 Out[]: 
   183 matrix([[  4. ,   3. ,   1. ,   4. ],
   206 array([[  4. ,   3. ,   1. ,   4. ],
   184         [  4. ,   3. ,   3. ,   0. ],
   207        [  4. ,   3. ,   3. ,   0. ],
   185         [  1. ,   1.5,  -2. ,  -4. ],
   208        [  1. ,   1.5,  -2. ,  -4. ],
   186         [ 10. ,  -8. ,   9. ,  10. ]])
   209        [ 10. ,  -8. ,   9. ,  10. ]])
   187   \end{lstlisting}
   210   \end{lstlisting}
   188 \end{frame}
   211 \end{frame}
   189 
   212 
   190 \begin{frame}[fragile]
   213 \begin{frame}[fragile]
       
   214 \frametitle{Elementwise Multiplication}
       
   215 \begin{lstlisting}
       
   216 In []: A*B
       
   217 Out[]: 
       
   218 array([[  3. ,   2. ,  -2. ,  -5. ],
       
   219        [  4. , -10. ,  -4. , -81. ],
       
   220        [ -2. ,   0.5,   1. , -21. ],
       
   221        [  9. ,  15. ,  14. ,  21. ]])
       
   222 
       
   223 \end{lstlisting}
       
   224 \end{frame}
       
   225 
       
   226 \begin{frame}[fragile]
   191 \frametitle{Matrix Multiplication}
   227 \frametitle{Matrix Multiplication}
   192 \begin{lstlisting}
   228 \begin{lstlisting}
   193 In []: linalg.multiply(A, B)
   229 In []: dot(A,B)
   194 Out[]: 
   230 Out[]: 
   195 matrix([[  3. ,   2. ,  -2. ,  -5. ],
   231 array([[ -6. ,   6. ,  -6. ,  -3. ],
   196         [  4. , -10. ,  -4. , -81. ],
   232        [-64. ,  38.5, -44. ,  35. ],
   197         [ -2. ,   0.5,   1. , -21. ],
   233        [ 36. , -13.5,  24. ,  35. ],
   198         [  9. ,  15. ,  14. ,  21. ]])
   234        [ 58. , -26. ,  34. , -15. ]])
   199 \end{lstlisting}
   235 \end{lstlisting}
   200 \end{frame}
   236 \end{frame}
   201 
   237 
   202 \begin{frame}[fragile]
   238 \begin{frame}[fragile]
   203 \frametitle{Inverse of a Matrix}
   239 \frametitle{Inverse of a Matrix}
       
   240 \begin{lstlisting}
       
   241 In []: inv(A)
       
   242 \end{lstlisting}
   204 \begin{small}
   243 \begin{small}
   205 \begin{lstlisting}
   244 \begin{lstlisting}
   206 In []: linalg.inv(A)
   245 Out[]: 
   207 Out[]: 
   246 array([[-0.5 ,  0.55, -0.15,  0.7 ],
   208 matrix([[-0.5 ,  0.55, -0.15,  0.7 ],
   247        [ 0.75, -0.5 ,  0.5 , -0.75],
   209         [ 0.75, -0.5 ,  0.5 , -0.75],
   248        [ 0.5 , -0.15, -0.05, -0.1 ],
   210         [ 0.5 , -0.15, -0.05, -0.1 ],
   249        [ 0.25, -0.25,  0.25, -0.25]])
   211         [ 0.25, -0.25,  0.25, -0.25]])
       
   212 \end{lstlisting}
   250 \end{lstlisting}
   213 \end{small}
   251 \end{small}
   214 \end{frame}
   252 \end{frame}
   215 
   253 
   216 \begin{frame}[fragile]
   254 \begin{frame}[fragile]
   217 \frametitle{Determinant}
   255 \frametitle{Determinant}
   218 \begin{lstlisting}
   256 \begin{lstlisting}
   219 In []: det(A)
   257 In []: det(A)
   220 Out[66]: 80.0
   258 Out[]: 80.0
   221 \end{lstlisting}
   259 \end{lstlisting}
   222 \end{frame}
   260 \end{frame}
   223 
   261 
   224 \begin{frame}[fragile]
   262 %%use S=array(X,Y)
   225 \frametitle{Eigen Values and Eigen Matrix}
   263 \begin{frame}[fragile]
       
   264 \frametitle{Eigenvalues and Eigen Vectors}
   226 \begin{small}
   265 \begin{small}
   227 \begin{lstlisting}
   266 \begin{lstlisting}
   228 In []: E = matrix([[3,2,4],[2,0,2],[4,2,3]])
   267 In []: E = array([[3,2,4],[2,0,2],[4,2,3]])
   229 
   268 
   230 In []: linalg.eig(E)
   269 In []: eig(E)
   231 Out[]: 
   270 Out[]: 
   232 (array([-1.,  8., -1.]),
   271 (array([-1.,  8., -1.]),
   233  matrix([[-0.74535599,  0.66666667, -0.1931126 ],
   272  array([[-0.74535599,  0.66666667, -0.1931126 ],
   234         [ 0.2981424 ,  0.33333333, -0.78664085],
   273         [ 0.2981424 ,  0.33333333, -0.78664085],
   235         [ 0.59628479,  0.66666667,  0.58643303]]))
   274         [ 0.59628479,  0.66666667,  0.58643303]]))
   236 
   275 
   237 In []: linalg.eigvals(E)
   276 In []: eigvals(E)
   238 Out[]: array([-1.,  8., -1.])
   277 Out[]: array([-1.,  8., -1.])
   239 \end{lstlisting}
   278 \end{lstlisting}
   240 \end{small}
   279 \end{small}
   241 \end{frame}
   280 \end{frame}
   242 
   281 
   243 \begin{frame}[fragile]
   282 \begin{frame}[fragile]
   244 \frametitle{Computing Norms}
   283 \frametitle{Computing Norms}
   245 \begin{lstlisting}
   284 \begin{lstlisting}
   246 In []: linalg.norm(E)
   285 In []: norm(E)
   247 Out[]: 8.1240384046359608
   286 Out[]: 8.1240384046359608
   248 \end{lstlisting}
   287 \end{lstlisting}
   249 \end{frame}
   288 \end{frame}
   250 
   289 
   251 \begin{frame}[fragile]
   290 \begin{frame}[fragile]
   252   \frametitle{Single Value Decomposition}
   291   \frametitle{Singular Value Decomposition}
   253   \begin{small}
   292   \begin{small}
   254   \begin{lstlisting}
   293   \begin{lstlisting}
   255 In [76]: linalg.svd(E)
   294 In []: svd(E)
   256 Out[76]: 
   295 Out[]: 
   257 (matrix(
   296 (array(
   258 [[ -6.66666667e-01,  -1.23702565e-16,   7.45355992e-01],
   297 [[ -6.66666667e-01,  -1.23702565e-16,   7.45355992e-01],
   259  [ -3.33333333e-01,  -8.94427191e-01,  -2.98142397e-01],
   298  [ -3.33333333e-01,  -8.94427191e-01,  -2.98142397e-01],
   260  [ -6.66666667e-01,   4.47213595e-01,  -5.96284794e-01]]),
   299  [ -6.66666667e-01,   4.47213595e-01,  -5.96284794e-01]]),
   261  array([ 8.,  1.,  1.]),
   300  array([ 8.,  1.,  1.]),
   262  matrix([[-0.66666667, -0.33333333, -0.66666667],
   301  array([[-0.66666667, -0.33333333, -0.66666667],
   263         [-0.        ,  0.89442719, -0.4472136 ],
   302         [-0.        ,  0.89442719, -0.4472136 ],
   264         [-0.74535599,  0.2981424 ,  0.59628479]]))
   303         [-0.74535599,  0.2981424 ,  0.59628479]]))
   265   \end{lstlisting}
   304   \end{lstlisting}
   266   \end{small}
   305   \end{small}
   267 \inctime{15}
   306 \inctime{15}
   287 
   326 
   288 \begin{frame}[fragile]
   327 \begin{frame}[fragile]
   289 \frametitle{Solving using Matrices}
   328 \frametitle{Solving using Matrices}
   290 Let us now look at how to solve this using \kwrd{matrices}
   329 Let us now look at how to solve this using \kwrd{matrices}
   291   \begin{lstlisting}
   330   \begin{lstlisting}
   292     In []: A = matrix([[3,2,-1],
   331     In []: A = array([[3,2,-1],
   293                        [2,-2,4],
   332                       [2,-2,4],                   
   294                        [-1, 0.5, -1]])
   333                       [-1, 0.5, -1]])
   295     In []: b = matrix([[1], [-2], [0]])
   334     In []: b = array([[1], [-2], [0]])
   296     In []: x = linalg.solve(A, b)
   335     In []: x = solve(A, b)
   297     In []: Ax = dot(A, x)
   336     In []: Ax = dot(A,x)
   298   \end{lstlisting}
   337   \end{lstlisting}
   299 \end{frame}
   338 \end{frame}
   300 
   339 
   301 \begin{frame}[fragile]
   340 \begin{frame}[fragile]
   302 \frametitle{Solution:}
   341 \frametitle{Solution:}
   312 \begin{frame}[fragile]
   351 \begin{frame}[fragile]
   313 \frametitle{Let's check!}
   352 \frametitle{Let's check!}
   314 \begin{lstlisting}
   353 \begin{lstlisting}
   315 In []: Ax
   354 In []: Ax
   316 Out[]: 
   355 Out[]: 
   317 matrix([[  1.00000000e+00],
   356 array([[  1.00000000e+00],
   318         [ -2.00000000e+00],
   357        [ -2.00000000e+00],
   319         [  2.22044605e-16]])
   358        [  2.22044605e-16]])
   320 \end{lstlisting}
   359 \end{lstlisting}
   321 \begin{block}{}
   360 \begin{block}{}
   322 The last term in the matrix is actually \alert{0}!\\
   361 The last term in the matrix is actually \alert{0}!\\
   323 We can use \kwrd{allclose()} to check.
   362 We can use \kwrd{allclose()} to check.
   324 \end{block}
   363 \end{block}
   330 \end{frame}
   369 \end{frame}
   331 
   370 
   332 \subsection{Exercises}
   371 \subsection{Exercises}
   333 
   372 
   334 \begin{frame}[fragile]
   373 \begin{frame}[fragile]
   335 \frametitle{Problem Set 4: Problem 4.1}
   374 \frametitle{Problem 1}
       
   375 Given the matrix:\\
       
   376 \begin{center}
       
   377 $\begin{bmatrix}
       
   378 -2 & 2 & 3\\
       
   379  2 & 1 & 6\\
       
   380 -1 &-2 & 0\\
       
   381 \end{bmatrix}$
       
   382 \end{center}
       
   383 Find:
       
   384 \begin{itemize}
       
   385   \item[i] Transpose
       
   386   \item[ii]Inverse
       
   387   \item[iii]Determinant
       
   388   \item[iv] Eigenvalues and Eigen vectors
       
   389   \item[v] Singular Value decomposition
       
   390 \end{itemize}
       
   391 \end{frame}
       
   392 
       
   393 \begin{frame}[fragile]
       
   394 \frametitle{Problem 2}
       
   395 Given 
       
   396 \begin{center}
       
   397 A = 
       
   398 $\begin{bmatrix}
       
   399 -3 & 1 & 5 \\
       
   400 1 & 0 & -2 \\
       
   401 5 & -2 & 4 \\
       
   402 \end{bmatrix}$
       
   403 , B = 
       
   404 $\begin{bmatrix}
       
   405 0 & 9 & -12 \\
       
   406 -9 & 0 & 20 \\
       
   407 12 & -20 & 0 \\
       
   408 \end{bmatrix}$
       
   409 \end{center}
       
   410 Find:
       
   411 \begin{itemize}
       
   412   \item[i] Sum of A and B
       
   413   \item[ii]Elementwise Product of A and B
       
   414   \item[iii] Matrix product of A and B
       
   415 \end{itemize}
       
   416 \end{frame}
       
   417 
       
   418 \begin{frame}[fragile]
       
   419 \frametitle{Solution}
       
   420 Sum: 
       
   421 $\begin{bmatrix}
       
   422 -3 & 10 & 7 \\
       
   423 -8 & 0 & 18 \\
       
   424 17 & -22 & 4 \\
       
   425 \end{bmatrix}$
       
   426 ,\\ Elementwise Product:
       
   427 $\begin{bmatrix}
       
   428 0 & 9 & -60 \\
       
   429 -9 & 0 & -40 \\
       
   430 60 & 40 & 0 \\
       
   431 \end{bmatrix}$
       
   432 ,\\ Matrix product:
       
   433 $\begin{bmatrix}
       
   434 51 & -127 & 56 \\
       
   435 -24 & 49 & -12 \\
       
   436 66 & -35 & -100 \\
       
   437 \end{bmatrix}$
       
   438 \end{frame}
       
   439 
       
   440 \begin{frame}[fragile]
       
   441 \frametitle{Problem 3}
   336 Solve the set of equations:
   442 Solve the set of equations:
   337 \begin{align*}
   443 \begin{align*}
   338   x + y + 2z -w & = 3\\
   444   x + y + 2z -w & = 3\\
   339   2x + 5y - z - 9w & = -3\\
   445   2x + 5y - z - 9w & = -3\\
   340   2x + y -z + 3w & = -11 \\
   446   2x + y -z + 3w & = -11 \\
   343 \inctime{10}
   449 \inctime{10}
   344 \end{frame}
   450 \end{frame}
   345 
   451 
   346 \begin{frame}[fragile]
   452 \begin{frame}[fragile]
   347 \frametitle{Solution}
   453 \frametitle{Solution}
   348 Solution:
   454 Use \kwrd{solve()}
   349 \begin{lstlisting}
       
   350 \begin{align*}
   455 \begin{align*}
   351   x & = -5\\
   456   x & = -5\\
   352   y & = 2\\
   457   y & = 2\\
   353   z & = 3\\
   458   z & = 3\\
   354   w & = 0\\
   459   w & = 0\\
   355 \end{align*}
   460 \end{align*}
   356 \end{lstlisting}
       
   357 \end{frame}
       
   358 
       
   359 \begin{frame}[fragile]
       
   360 \frametitle{Problem 4.2}
       
   361 
       
   362 \end{frame}
   461 \end{frame}
   363 
   462 
   364 \section{Summary}
   463 \section{Summary}
   365 \begin{frame}
   464 \begin{frame}
   366   \frametitle{Summary}
   465   \frametitle{What did we learn??}
   367 So what did we learn??
       
   368   \begin{itemize}
   466   \begin{itemize}
   369   \item Matrices
   467   \item Matrices
   370     \begin{itemize}
   468     \begin{itemize}
       
   469       \item Accessing elements
   371       \item Transpose
   470       \item Transpose
   372       \item Addition
   471       \item Addition
   373       \item Multiplication
   472       \item Multiplication
   374       \item Inverse of a matrix
   473       \item Inverse of a matrix
   375       \item Determinant
   474       \item Determinant
   376       \item Eigen values and Eigen matrix
   475       \item Eigenvalues and Eigen vector
   377       \item Norms
   476       \item Norms
   378       \item Single Value Decomposition
   477       \item Singular Value Decomposition
   379     \end{itemize}
   478     \end{itemize}
   380   \item Solving linear equations
   479   \item Solving linear equations
   381   \end{itemize}
   480   \end{itemize}
   382 \end{frame}
   481 \end{frame}
   383 
   482