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