day1/session4.tex
changeset 214 19592f802dde
parent 213 ce62706cf870
child 232 b9748af050c4
equal deleted inserted replaced
213:ce62706cf870 214:19592f802dde
   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,2,3],
   139 In []: A = ([[5, 2, 4], 
   140                      [4,5,6],
   140             [-3, 6, 2], 
   141                      [7,8,9]])
   141             [3, -3, 1]])
   142 
   142 
   143   In []: a
   143 In []: A
   144   Out[]: 
   144 Out[]: [[5, 2, 4], 
   145   matrix([[1, 2, 3],
   145         [-3, 6, 2], 
   146          [4, 5, 6],
   146         [3, -3, 1]]
   147          [7, 8, 9]])
       
   148 \end{lstlisting}
   147 \end{lstlisting}
   149 \end{frame}
   148 \end{frame}
   150 
   149 
   151 \subsection{Basic Operations}
   150 \subsection{Basic Operations}
       
   151 
       
   152 \begin{frame}[fragile]
       
   153 \frametitle{Transpose of a Matrix}
       
   154 \begin{lstlisting}
       
   155 In []: linalg.transpose(A)
       
   156 Out[]: 
       
   157 matrix([[ 5, -3,  3],
       
   158         [ 2,  6, -3],
       
   159         [ 4,  2,  1]])
       
   160 \end{lstlisting}
       
   161 \end{frame}
       
   162 
       
   163 \begin{frame}[fragile]
       
   164   \frametitle{Sum of all elements}
       
   165   \begin{lstlisting}
       
   166 In []: linalg.sum(A)
       
   167 Out[]: 17
       
   168   \end{lstlisting}
       
   169 \end{frame}
       
   170 
       
   171 \begin{frame}[fragile]
       
   172   \frametitle{Matrix Addition}
       
   173   \begin{lstlisting}
       
   174 In []: B = matrix([[3,2,-1],
       
   175                    [2,-2,4],
       
   176                    [-1, 0.5, -1]])
       
   177 
       
   178 In []: linalg.add(A, B)
       
   179 Out[]: 
       
   180 matrix([[ 8. ,  4. ,  3. ],
       
   181         [-1. ,  4. ,  6. ],
       
   182         [ 2. , -2.5,  0. ]])
       
   183   \end{lstlisting}
       
   184 \end{frame}
       
   185 
       
   186 \begin{frame}[fragile]
       
   187 \frametitle{Matrix Multiplication}
       
   188 \begin{lstlisting}
       
   189 In []: linalg.multiply(A, B)
       
   190 Out[]: 
       
   191 matrix([[ 15. ,   4. ,  -4. ],
       
   192         [ -6. , -12. ,   8. ],
       
   193         [ -3. ,  -1.5,  -1. ]])
       
   194 \end{lstlisting}
       
   195 \end{frame}
       
   196 
   152 \begin{frame}[fragile]
   197 \begin{frame}[fragile]
   153 \frametitle{Inverse of a Matrix}
   198 \frametitle{Inverse of a Matrix}
   154 
       
   155 \begin{small}
   199 \begin{small}
   156 \begin{lstlisting}
   200 \begin{lstlisting}
   157 In []: linalg.inv(A)
   201 In []: linalg.inv(A)
   158 Out[]: 
   202 Out[]: 
   159 matrix([[ 0.07734807,  0.01657459,  0.32044199],
   203 array([[ 0.28571429, -0.33333333, -0.47619048],
   160         [ 0.09944751, -0.12154696, -0.01657459],
   204        [ 0.21428571, -0.16666667, -0.52380952],
   161         [-0.02762431, -0.07734807,  0.17127072]])
   205        [-0.21428571,  0.5       ,  0.85714286]])
   162 
       
   163 \end{lstlisting}
   206 \end{lstlisting}
   164 \end{small}
   207 \end{small}
   165 \end{frame}
   208 \end{frame}
   166 
   209 
   167 \begin{frame}[fragile]
   210 \begin{frame}[fragile]
   168 \frametitle{Determinant}
   211 \frametitle{Determinant}
   169 \begin{lstlisting}
   212 \begin{lstlisting}
   170   In []: linalg.det(a)
   213 In []: det(A)
   171   Out[]: -9.5171266700777579e-16
   214 Out[]: 42.0
   172 \end{lstlisting}
       
   173 \end{frame}
       
   174 
       
   175 \begin{frame}[fragile]
       
   176 \frametitle{Computing Norms}
       
   177 \begin{lstlisting}
       
   178   In []: linalg.norm(a)
       
   179   Out[]: 16.881943016134134
       
   180 \end{lstlisting}
   215 \end{lstlisting}
   181 \end{frame}
   216 \end{frame}
   182 
   217 
   183 \begin{frame}[fragile]
   218 \begin{frame}[fragile]
   184 \frametitle{Eigen Values and Eigen Matrix}
   219 \frametitle{Eigen Values and Eigen Matrix}
   185 \begin{small}
   220 \begin{small}
   186 \begin{lstlisting}
   221 \begin{lstlisting}
   187   In []: linalg.eigvals(a)
   222 In []: linalg.eig(A)
   188   Out[]: array([1.61168440e+01, -1.11684397e+00, -1.22196337e-15])
   223 Out[]: 
   189 
   224 (array([ 7.,  2.,  3.]),
   190   In []: linalg.eig(a)
   225  matrix([[-0.57735027,  0.42640143,  0.37139068],
   191   Out[]: 
   226         [ 0.57735027,  0.63960215,  0.74278135],
   192   (array([ 1.61168440e+01, -1.11684397e+00, -1.22196337e-15]),
   227         [-0.57735027, -0.63960215, -0.55708601]]))
   193    matrix([[-0.23197069, -0.78583024,  0.40824829],
       
   194           [-0.52532209, -0.08675134, -0.81649658],
       
   195           [-0.8186735 ,  0.61232756,  0.40824829]]))
       
   196 \end{lstlisting}
   228 \end{lstlisting}
   197 \end{small}
   229 \end{small}
       
   230 \end{frame}
       
   231 
       
   232 \begin{frame}[fragile]
       
   233 \frametitle{Computing Norms}
       
   234 \begin{lstlisting}
       
   235   In []: linalg.norm(A)
       
   236   Out[]: 10.63014581273465
       
   237 \end{lstlisting}
       
   238 \end{frame}
       
   239 
       
   240 \begin{frame}[fragile]
       
   241   \frametitle{Single Value Decomposition}
       
   242   \begin{small}
       
   243   \begin{lstlisting}
       
   244 In []: linalg.svd(A)
       
   245 Out[]: 
       
   246 (matrix([[-0.13391246, -0.94558684, -0.29653495],
       
   247         [ 0.84641267, -0.26476432,  0.46204486],
       
   248         [-0.51541542, -0.18911737,  0.83581192]]),
       
   249  array([ 7.96445022,  7.        ,  0.75334767]),
       
   250  matrix([[-0.59703387,  0.79815896,  0.08057807],
       
   251         [-0.64299905, -0.41605821, -0.64299905],
       
   252         [-0.47969029, -0.43570384,  0.7616163 ]]))
       
   253   \end{lstlisting}
       
   254   \end{small}
   198 \end{frame}
   255 \end{frame}
   199 
   256 
   200 \section{Solving linear equations}
   257 \section{Solving linear equations}
   201 
   258 
   202 \begin{frame}[fragile]
   259 \begin{frame}[fragile]
   217 
   274 
   218 \begin{frame}[fragile]
   275 \begin{frame}[fragile]
   219 \frametitle{Solving using Matrices}
   276 \frametitle{Solving using Matrices}
   220 Let us now look at how to solve this using \kwrd{matrices}
   277 Let us now look at how to solve this using \kwrd{matrices}
   221   \begin{lstlisting}
   278   \begin{lstlisting}
   222     In []: A = matrix([[3,2,-1],[2,-2,4],[-1, 0.5, -1]])
   279     In []: A = matrix([[3,2,-1],
       
   280                        [2,-2,4],
       
   281                        [-1, 0.5, -1]])
   223     In []: b = matrix([[1], [-2], [0]])
   282     In []: b = matrix([[1], [-2], [0]])
   224     In []: x = linalg.solve(A, b)
   283     In []: x = linalg.solve(A, b)
   225     In []: Ax = dot(A, x)
   284     In []: Ax = dot(A, x)
   226     In []: allclose(Ax, b)
   285     In []: allclose(Ax, b)
   227     Out[]: True
   286     Out[]: True
   249 \begin{frame}
   308 \begin{frame}
   250   \frametitle{Summary}
   309   \frametitle{Summary}
   251 So what did we learn??
   310 So what did we learn??
   252   \begin{itemize}
   311   \begin{itemize}
   253   \item Matrices
   312   \item Matrices
       
   313     \begin{itemize}
       
   314       \item Transpose
       
   315       \item Addition
       
   316       \item Multiplication
       
   317       \item Inverse of a matrix
       
   318       \item Determinant
       
   319       \item Eigen values and Eigen matrix
       
   320       \item Norms
       
   321       \item Single Value Decomposition
       
   322     \end{itemize}
   254   \item Solving linear equations
   323   \item Solving linear equations
   255   \end{itemize}
   324   \end{itemize}
   256 \end{frame}
   325 \end{frame}
   257 
   326 
   258 \end{document}
   327 \end{document}
   259