day1/session3.tex
branchscipyin2010
changeset 444 a1117e03f98a
parent 443 ca37cf69cd18
child 446 b9d07ebd783b
equal deleted inserted replaced
443:ca37cf69cd18 444:a1117e03f98a
    72 % }
    72 % }
    73 
    73 
    74 
    74 
    75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    76 % Title page
    76 % Title page
    77 \title[Arrays]{Python for Science and Engg: Arrays}
    77 \title[Arrays]{Python for Science and Engg: \\Arrays}
    78 
    78 
    79 \author[FOSSEE] {FOSSEE}
    79 \author[FOSSEE] {FOSSEE}
    80 
    80 
    81 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
    81 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
    82 \date[] {SciPy 2010, Tutorials}
    82 \date[] {SciPy 2010, Tutorials}
   121 \begin{frame}
   121 \begin{frame}
   122   \frametitle{Outline}
   122   \frametitle{Outline}
   123   \tableofcontents
   123   \tableofcontents
   124 %  \pausesections
   124 %  \pausesections
   125 \end{frame}
   125 \end{frame}
   126 
   126 \section{Motivation}
   127 \section{Matrices}
   127 
   128 
   128 \begin{frame}[fragile]
   129 \begin{frame}
   129   \frametitle{Why arrays?}
   130 \frametitle{Matrices: Introduction}
   130   \begin{itemize}
   131 \alert{All matrix operations are done using \kwrd{arrays}}
   131   \item Speed!
   132 \end{frame}
   132   \item Convenience
   133 
   133   \item Easier to handle multi-dimensional data
   134 \begin{frame}[fragile]
   134   \end{itemize}
   135 \frametitle{Matrices: Initializing}
   135 \end{frame}
       
   136 
       
   137 \begin{frame}[fragile]
       
   138   \frametitle{Speed}
       
   139     \begin{lstlisting}
       
   140 In []: a = random(1000000) 
       
   141 # array with a million random elements
       
   142 In []: b = []
       
   143 In []: for each in a:
       
   144   ...:     b.append(sin(each))
       
   145   ...:     
       
   146   ...:     
       
   147 In []: sin(a)
       
   148   \end{lstlisting}
       
   149 \end{frame}
       
   150 
       
   151 \begin{frame}[fragile]
       
   152   \frametitle{Convenience}
       
   153 The pendulum problem could've been solved as below::
       
   154     \begin{lstlisting}
       
   155 In []: L, T = loadtxt('pendulum.txt', 
       
   156                       unpack=True)
       
   157 In []: tsq = T*T
       
   158 In []: plot (L, tsq, '.')
       
   159   \end{lstlisting}
       
   160 \end{frame}
       
   161 
       
   162 \section{Initializing}
       
   163 \begin{frame}[fragile]
       
   164 \frametitle{Initializing}
   136 \begin{lstlisting}
   165 \begin{lstlisting}
   137 In []: c = array([[11,12,13],
   166 In []: c = array([[11,12,13],
   138                   [21,22,23],
   167                   [21,22,23],
   139                   [31,32,33]])
   168                   [31,32,33]])
   140 
   169 
   145        [31, 32, 33]])
   174        [31, 32, 33]])
   146 \end{lstlisting}
   175 \end{lstlisting}
   147 \end{frame}
   176 \end{frame}
   148 
   177 
   149 \begin{frame}[fragile]
   178 \begin{frame}[fragile]
   150 \frametitle{Initializing some special matrices}
   179 \frametitle{Some special arrays}
   151 \begin{small}
   180 \begin{small}
   152   \begin{lstlisting}
   181   \begin{lstlisting}
   153 In []: ones((3,5))
   182 In []: ones((3,5))
   154 Out[]: 
   183 Out[]: 
   155 array([[ 1.,  1.,  1.,  1.,  1.],
   184 array([[ 1.,  1.,  1.,  1.,  1.],
   186 
   215 
   187 In []: c[1]
   216 In []: c[1]
   188 Out[]: array([21, 22, 23])
   217 Out[]: array([21, 22, 23])
   189   \end{lstlisting}
   218   \end{lstlisting}
   190   \end{small}
   219   \end{small}
       
   220 Similar to \kwrd{lists} but improved!
   191 \end{frame}
   221 \end{frame}
   192 
   222 
   193 \begin{frame}[fragile]
   223 \begin{frame}[fragile]
   194   \frametitle{Changing elements}
   224   \frametitle{Changing elements}
   195   \begin{small}
   225   \begin{small}
   207 array([[11, 12, 13],
   237 array([[11, 12, 13],
   208        [ 0,  0,  0],
   238        [ 0,  0,  0],
   209        [31, 32, 33]])
   239        [31, 32, 33]])
   210   \end{lstlisting}
   240   \end{lstlisting}
   211   \end{small}
   241   \end{small}
   212 How do you access one \alert{column}?
   242 How do you access one \alert{column}? -- Enter Slicing!
   213 \end{frame}
   243 \end{frame}
   214 
   244 
   215 \begin{frame}[fragile]
   245 \section{Slicing \& Striding}
   216   \frametitle{Slicing}
   246 
       
   247 \begin{frame}[fragile]
       
   248   \frametitle{Slicing: Lists}
       
   249   \begin{block}{Define a list}
       
   250 	\kwrd{In []: p = [ 2, 3, 5, 7, 11, 13]}
       
   251   \end{block}
       
   252 \begin{lstlisting}
       
   253 In []: p[1:3]
       
   254 Out[]: [3, 5]
       
   255 \end{lstlisting}
       
   256 \emphbar{A slice}
       
   257 \begin{lstlisting}
       
   258 In []: p[0:-1]
       
   259 Out[]: [2, 3, 5, 7, 11]
       
   260 In []: p[:]
       
   261 Out[]: [2, 3, 5, 7, 11, 13]
       
   262 \end{lstlisting}
       
   263 \end{frame}
       
   264 
       
   265 \begin{frame}[fragile]
       
   266   \frametitle{Striding: Lists}
       
   267 \emphbar{Striding over \typ{p}}
       
   268 \begin{lstlisting}
       
   269 In []: p[::2]
       
   270 Out[]: [2, 5, 11]
       
   271 In []: p[1::2]
       
   272 Out[]: [3, 7, 13]
       
   273 In []: p[1:-1:2]
       
   274 Out[]: [3, 7]
       
   275 In []: p[::3]
       
   276 Out[]: [2, 7]
       
   277 \end{lstlisting}
       
   278 \alert{\typ{list[initial:final:step]}}
       
   279 \end{frame}
       
   280 
       
   281 \begin{frame}[fragile]
       
   282   \frametitle{Slicing \& Striding: Lists}
       
   283   What is the output of the following?
       
   284 \begin{lstlisting}
       
   285 In []: p[1::4]
       
   286 
       
   287 In []: p[1:-1:3]
       
   288 \end{lstlisting}
       
   289 \end{frame}
       
   290 
       
   291 
       
   292 \begin{frame}[fragile]
       
   293   \frametitle{Slicing: \typ{arrays}}
   217 \begin{small}
   294 \begin{small}
   218   \begin{lstlisting}
   295   \begin{lstlisting}
   219 In []: c[:,1]
   296 In []: c[:,1]
   220 Out[]: array([12,  0, 32])
   297 Out[]: array([12,  0, 32])
   221 
   298 
   234   \end{lstlisting}
   311   \end{lstlisting}
   235 \end{small}
   312 \end{small}
   236 \end{frame}
   313 \end{frame}
   237 
   314 
   238 \begin{frame}[fragile]
   315 \begin{frame}[fragile]
   239   \frametitle{Slicing \ldots}
   316   \frametitle{Slicing: \typ{arrays} \ldots}
   240 \begin{small}
   317 \begin{small}
   241   \begin{lstlisting}
   318   \begin{lstlisting}
   242 In []: c[:2,:]
   319 In []: c[:2,:]
   243 Out[]: 
   320 Out[]: 
   244 array([[11, 12, 13],
   321 array([[11, 12, 13],
   257 
   334 
   258 \end{small}
   335 \end{small}
   259 \end{frame}
   336 \end{frame}
   260 
   337 
   261 \begin{frame}[fragile]
   338 \begin{frame}[fragile]
   262   \frametitle{Striding}
   339   \frametitle{Striding: \typ{arrays}}
   263   \begin{small}
   340   \begin{small}
   264   \begin{lstlisting}
   341   \begin{lstlisting}
   265 In []: c[::2,:]
   342 In []: c[::2,:]
   266 Out[]: 
   343 Out[]: 
   267 array([[11, 12, 13],
   344 array([[11, 12, 13],
   280   \end{lstlisting}
   357   \end{lstlisting}
   281   \end{small}
   358   \end{small}
   282 \end{frame}
   359 \end{frame}
   283 
   360 
   284 \begin{frame}[fragile]
   361 \begin{frame}[fragile]
   285   \frametitle{Shape of a matrix}
   362   \frametitle{Shape of an \typ{array}}
   286   \begin{lstlisting}
   363   \begin{lstlisting}
   287 In []: c
   364 In []: c
   288 Out[]: 
   365 Out[]: 
   289 array([[11, 12, 13],
   366 array([[11, 12, 13],
   290        [ 0,  0,  0],
   367        [ 0,  0,  0],
   291        [31, 32, 33]])
   368        [31, 32, 33]])
   292 
   369 
   293 In []: c.shape
   370 In []: c.shape
   294 Out[]: (3, 3)
   371 Out[]: (3, 3)
   295   \end{lstlisting}
   372   \end{lstlisting}
   296 \emphbar{Shape specifies shape or dimensions of a matrix}
   373 \emphbar{Shape specifies shape or dimensions of an array}
   297 \end{frame}
   374 \end{frame}
   298 
   375 
   299 \begin{frame}[fragile]
   376 \begin{frame}[fragile]
   300   \frametitle{Elementary image processing}
   377   \frametitle{Elementary image processing}
   301 \begin{small}
   378 \begin{small}
   333 Out[]: <matplotlib.image.AxesImage object at 0xb765c8c>
   410 Out[]: <matplotlib.image.AxesImage object at 0xb765c8c>
   334   \end{lstlisting}
   411   \end{lstlisting}
   335 \end{small}
   412 \end{small}
   336 \end{frame}
   413 \end{frame}
   337 
   414 
   338 \begin{frame}[fragile]
   415 \section{Operations on \typ{arrays}}
   339 \frametitle{Transpose of a Matrix}
   416 \begin{frame}[fragile]
   340 \begin{lstlisting}
   417   \frametitle{Operations: Addition}
   341 In []: a = array([[ 1,  1,  2, -1],
   418   Operations on arrays, as already mentioned, are \alert{element-wise}
   342   ...:            [ 2,  5, -1, -9],
   419   \begin{lstlisting}
   343   ...:            [ 2,  1, -1,  3],
   420 In []: a = array([[-3,2.5],
   344   ...:            [ 1, -3,  2,  7]])
   421                   [2.5,2]])
   345 
   422 
   346 In []: a.T
   423 In []: b = array([[3,2],
   347 Out[]:
   424                   [2,-2]])
   348 array([[ 1,  2,  2,  1],
   425 
   349        [ 1,  5,  1, -3],
       
   350        [ 2, -1, -1,  2],
       
   351        [-1, -9,  3,  7]])
       
   352 \end{lstlisting}
       
   353 \end{frame}
       
   354 
       
   355 \begin{frame}[fragile]
       
   356   \frametitle{Matrix Addition}
       
   357   \begin{lstlisting}
       
   358 In []: b = array([[3,2,-1,5],
       
   359                   [2,-2,4,9],
       
   360                   [-1,0.5,-1,-7],
       
   361                   [9,-5,7,3]])
       
   362 In []: a + b
   426 In []: a + b
   363 Out[]: 
   427 Out[]: 
   364 array([[  4. ,   3. ,   1. ,   4. ],
   428 array([[ 0. ,  4.5],
   365        [  4. ,   3. ,   3. ,   0. ],
   429        [ 4.5,  0. ]])
   366        [  1. ,   1.5,  -2. ,  -4. ],
       
   367        [ 10. ,  -8. ,   9. ,  10. ]])
       
   368   \end{lstlisting}
   430   \end{lstlisting}
   369 \end{frame}
   431 \end{frame}
   370 
   432 
   371 \begin{frame}[fragile]
   433 \begin{frame}[fragile]
   372 \frametitle{Elementwise Multiplication}
   434 \frametitle{Elementwise Multiplication}
   373 \begin{lstlisting}
   435 \begin{lstlisting}
   374 In []: a*b
   436 In []: a*b
   375 Out[]: 
   437 Out[]: 
   376 array([[  3. ,   2. ,  -2. ,  -5. ],
   438 array([[-9.,  5.],
   377        [  4. , -10. ,  -4. , -81. ],
   439        [ 5., -4.]])
   378        [ -2. ,   0.5,   1. , -21. ],
   440 \end{lstlisting}
   379        [  9. ,  15. ,  14. ,  21. ]])
   441 \end{frame}
   380 
   442 
   381 \end{lstlisting}
   443 \begin{frame}[fragile]
   382 \end{frame}
   444 \frametitle{Matrix Operations using \typ{arrays}}
   383 
   445 
   384 \begin{frame}[fragile]
   446 We can perform various matrix operations on \kwrd{arrays}\\ 
   385 \frametitle{Matrix Multiplication}
   447 A few are listed below.
   386 \begin{lstlisting}
   448 
   387 In []: dot(a, b)
   449 \vspace{-0.2in}
   388 Out[]: 
   450 
   389 array([[ -6. ,   6. ,  -6. ,  -3. ],
   451 \begin{center}
   390        [-64. ,  38.5, -44. ,  35. ],
   452 \begin{tabular}{lll}
   391        [ 36. , -13.5,  24. ,  35. ],
   453  Operation                    &  How?           &  Example           \\
   392        [ 58. , -26. ,  34. , -15. ]])
   454 \hline
   393 \end{lstlisting}
   455  Transpose                    &  \typ{.T}       &  \typ{A.T}         \\
   394 \end{frame}
   456  Product                      &  \typ{dot}      &  \typ{dot(A, B)}   \\
   395 
   457  Inverse                      &  \typ{inv}      &  \typ{inv(A)}      \\
   396 \begin{frame}[fragile]
   458  Determinant                  &  \typ{det}      &  \typ{det(A)}      \\
   397 \frametitle{Inverse of a Matrix}
   459  Sum of all elements          &  \typ{sum}      &  \typ{sum(A)}      \\
   398 \begin{lstlisting}
   460  Eigenvalues                  &  \typ{eigvals}  &  \typ{eigvals(A)}  \\
   399 
   461  Eigenvalues \& Eigenvectors  &  \typ{eig}      &  \typ{eig(A)}      \\
   400 \end{lstlisting}
   462  Norms                        &  \typ{norm}     &  \typ{norm(A)}     \\
   401 \begin{small}
   463  SVD                          &  \typ{svd}      &  \typ{svd(A)}      \\
   402 \begin{lstlisting}
   464 \end{tabular}
   403 In []: inv(a)
   465 \end{center}
   404 Out[]: 
   466 
   405 array([[-0.5 ,  0.55, -0.15,  0.7 ],
       
   406        [ 0.75, -0.5 ,  0.5 , -0.75],
       
   407        [ 0.5 , -0.15, -0.05, -0.1 ],
       
   408        [ 0.25, -0.25,  0.25, -0.25]])
       
   409 \end{lstlisting}
       
   410 \end{small}
       
   411 \emphbar{Try this: \typ{I = dot(a, inv(a))}}
       
   412 \end{frame}
       
   413 
       
   414 \begin{frame}[fragile]
       
   415 \frametitle{Determinant and sum of all elements}
       
   416 \begin{lstlisting}
       
   417 In []: det(a)
       
   418 Out[]: 80.0
       
   419 \end{lstlisting}
       
   420   \begin{lstlisting}
       
   421 In []: sum(a)
       
   422 Out[]: 12
       
   423   \end{lstlisting}
       
   424 
       
   425 \end{frame}
       
   426 
       
   427 %%use S=array(X,Y)
       
   428 \begin{frame}[fragile]
       
   429 \frametitle{Eigenvalues and Eigen Vectors}
       
   430 \begin{small}
       
   431 \begin{lstlisting}
       
   432 In []: e = array([[3,2,4],[2,0,2],[4,2,3]])
       
   433 
       
   434 In []: eig(e)
       
   435 Out[]: 
       
   436 (array([-1.,  8., -1.]),
       
   437  array([[-0.74535599,  0.66666667, -0.1931126 ],
       
   438         [ 0.2981424 ,  0.33333333, -0.78664085],
       
   439         [ 0.59628479,  0.66666667,  0.58643303]]))
       
   440 
       
   441 In []: eigvals(e)
       
   442 Out[]: array([-1.,  8., -1.])
       
   443 \end{lstlisting}
       
   444 \end{small}
       
   445 \end{frame}
       
   446 
       
   447 \begin{frame}[fragile]
       
   448 \frametitle{Computing Norms}
       
   449 \begin{lstlisting}
       
   450 In []: norm(e)
       
   451 Out[]: 8.1240384046359608
       
   452 \end{lstlisting}
       
   453 \end{frame}
       
   454 
       
   455 \begin{frame}[fragile]
       
   456   \frametitle{Singular Value Decomposition}
       
   457   \begin{small}
       
   458   \begin{lstlisting}
       
   459 In []: svd(e)
       
   460 Out[]: 
       
   461 (array(
       
   462 [[ -6.66666667e-01,  -1.23702565e-16,   7.45355992e-01],
       
   463  [ -3.33333333e-01,  -8.94427191e-01,  -2.98142397e-01],
       
   464  [ -6.66666667e-01,   4.47213595e-01,  -5.96284794e-01]]),
       
   465  array([ 8.,  1.,  1.]),
       
   466  array([[-0.66666667, -0.33333333, -0.66666667],
       
   467         [-0.        ,  0.89442719, -0.4472136 ],
       
   468         [-0.74535599,  0.2981424 ,  0.59628479]]))
       
   469   \end{lstlisting}
       
   470   \end{small}
       
   471 \end{frame}
   467 \end{frame}
   472 
   468 
   473 \section{Summary}
   469 \section{Summary}
   474 \begin{frame}
   470 \begin{frame}
   475   \frametitle{What did we learn?}
   471   \frametitle{What did we learn?}
   476   \begin{itemize}
   472   \begin{itemize}
   477   \item Matrices
   473   \item Arrays
   478     \begin{itemize}
   474     \begin{itemize}
   479       \item Initializing
   475     \item Initializing
   480       \item Accessing elements
   476     \item Accessing elements
   481       \item Slicing and Striding
   477     \item Slicing \& Striding
   482       \item Transpose
   478     \item Element-wise Operations
   483       \item Addition
   479     \item Matrix Operations
   484       \item Multiplication
       
   485       \item Inverse of a matrix
       
   486       \item Determinant
       
   487       \item Eigenvalues and Eigen vector
       
   488       \item Singular Value Decomposition
       
   489     \end{itemize}
   480     \end{itemize}
   490   \item Least Square Curve fitting
       
   491   \end{itemize}
   481   \end{itemize}
   492 \end{frame}
   482 \end{frame}
   493 
   483 
   494 \end{document}
   484 \end{document}
   495 
   485