day2/session2.tex
changeset 101 919c93e5aa71
parent 97 555237dbce44
equal deleted inserted replaced
100:e0610b591526 101:919c93e5aa71
    73 
    73 
    74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    75 % Title page
    75 % Title page
    76 \title[]{Numerical Computing with Numpy \& Scipy}
    76 \title[]{Numerical Computing with Numpy \& Scipy}
    77 
    77 
    78 \author[FOSSEE Team] {Asokan Pichai\\Prabhu Ramachandran}
    78 \author[FOSSEE Team] {FOSSEE}
    79 
    79 
    80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
    80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
    81 \date[] {11, October 2009}
    81 \date[] {11, October 2009}
    82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    83 
    83 
   118 \end{frame}
   118 \end{frame}
   119 
   119 
   120 \section{Advanced Numpy}
   120 \section{Advanced Numpy}
   121 \begin{frame}[fragile]
   121 \begin{frame}[fragile]
   122   \frametitle{Broadcasting}
   122   \frametitle{Broadcasting}
   123   Try it!
   123   \begin{lstlisting}
   124   \begin{lstlisting}
   124     >>> a = arange(4)
   125     >>> a = np.arange(4)
   125     >>> b = arange(5)
   126     >>> b = np.arange(5)
       
   127     >>> a+b #Does this work?
   126     >>> a+b #Does this work?
   128     >>> a+3
   127     >>> a+3
   129     >>> c=np.array([3])
   128     >>> c = array([3])
   130     >>> a+c #Works!
   129     >>> a+c #Works!
   131     >>> b+c #But how?
   130     >>> b+c #But how?
   132     >>> a.shape, b.shape, c.shape
   131     >>> a.shape, b.shape, c.shape
   133   \end{lstlisting}
   132   \end{lstlisting}
   134   \begin{itemize}
   133   \begin{itemize}
   140   \frametitle{Broadcasting}
   139   \frametitle{Broadcasting}
   141   \begin{columns}
   140   \begin{columns}
   142     \column{0.65\textwidth}
   141     \column{0.65\textwidth}
   143     \hspace*{-1.5in}
   142     \hspace*{-1.5in}
   144     \begin{lstlisting}
   143     \begin{lstlisting}
   145       >>> a = np.arange(4)
   144       >>> a = arange(4)
   146       >>> a+3
   145       >>> a+3
   147       array([3, 4, 5, 6])
   146       array([3, 4, 5, 6])
   148     \end{lstlisting}
   147     \end{lstlisting}
   149     \column{0.35\textwidth}
   148     \column{0.35\textwidth}
   150     \includegraphics[height=0.7in, interpolate=true]{data/broadcast_scalar}
   149     \includegraphics[height=0.7in, interpolate=true]{data/broadcast_scalar}
   151   \end{columns}
   150   \end{columns}
   152   \begin{itemize}
       
   153     \item Allows functions to take inputs that are not of the same shape
       
   154     \item 2 rules -
       
   155       \begin{enumerate}
       
   156       \item 1 is (repeatedly) prepended to shapes of smaller arrays
       
   157       \item Size 1 in a dimension changed to Largest size in that dimension
       
   158       \end{enumerate}
       
   159   \end{itemize}
       
   160 \end{frame}
   151 \end{frame}
   161 
   152 
   162 \begin{frame}[fragile]
   153 \begin{frame}[fragile]
   163   \frametitle{Broadcasting in 3D}
   154   \frametitle{Broadcasting in 3D}
   164     \begin{lstlisting}
   155     \begin{lstlisting}
   165       >>> x = np.ones((3, 5))
   156       >>> x = ones((3, 5, 1))
   166       >>> y = np.ones(8)
   157       >>> y = ones(8)
   167       >>> (x[..., None] + y).shape
   158       >>> (x + y).shape
   168       (3, 5, 8)
   159       (3, 5, 8)
   169     \end{lstlisting}
   160     \end{lstlisting}
   170     \begin{figure}
   161     \begin{figure}
   171       \begin{center}
   162       \begin{center}
   172       \includegraphics[height=1.5in, interpolate=true]{data/array_3x5x8}        
   163       \includegraphics[height=1.5in, interpolate=true]{data/array_3x5x8}        
   174     \end{figure}
   165     \end{figure}
   175 \end{frame}
   166 \end{frame}
   176 
   167 
   177 \begin{frame}[fragile]
   168 \begin{frame}[fragile]
   178   \frametitle{Copies \& Views}
   169   \frametitle{Copies \& Views}
   179   Try it!
       
   180   \vspace{-0.1in}
   170   \vspace{-0.1in}
   181   \begin{lstlisting}
   171   \begin{lstlisting}
   182     >>> a = np.arange(1,9); a.shape=3,3
   172     >>> a = arange(1,9); a.shape=3,3
   183     >>> b = a
   173     >>> b = a
   184     >>> b is a
   174     >>> b is a
   185     >>> b[0,0]=0; print a
   175     >>> b[0,0]=0; print a
   186     >>> c = a.view()
   176     >>> c = a.view()
   187     >>> c is a
   177     >>> c is a
   193   \end{lstlisting}
   183   \end{lstlisting}
   194 \end{frame}
   184 \end{frame}
   195 
   185 
   196 \begin{frame}[fragile]
   186 \begin{frame}[fragile]
   197   \frametitle{Copies \& Views}
   187   \frametitle{Copies \& Views}
   198   Try it!
       
   199   \vspace{-0.1in}
   188   \vspace{-0.1in}
   200   \begin{lstlisting}
   189   \begin{lstlisting}
   201     >>> b = a[0,1:3]
   190     >>> b = a[0,1:3]
   202     >>> c = a[0::2,0::2]
   191     >>> c = a[0::2,0::2]
   203     >>> a.flags.owndata
   192     >>> a.flags.owndata
   212 \end{frame}
   201 \end{frame}
   213 
   202 
   214 \begin{frame}[fragile]
   203 \begin{frame}[fragile]
   215   \frametitle{Copies contd \ldots}
   204   \frametitle{Copies contd \ldots}
   216   \begin{lstlisting}
   205   \begin{lstlisting}
   217     >>> b = a[np.array([0,1,2])]
   206     >>> a = arange(1, 10, 2)
   218     array([[1, 2, 3],
   207     >>> b = a[array([0,2,3])]
   219            [4, 5, 6],
       
   220            [7, 8, 9]])
       
   221     >>> b.flags.owndata
   208     >>> b.flags.owndata
   222     >>> abool=np.greater(a,2)
   209     >>> abool=a>5
   223     >>> c = a[abool]
   210     >>> c = a[abool]
   224     >>> c.flags.owndata
   211     >>> c.flags.owndata
   225   \end{lstlisting}
   212   \end{lstlisting}
   226   \begin{itemize}
   213   \begin{itemize}
   227   \item Indexing arrays or Boolean arrays produce copies
   214   \item Indexing arrays or Boolean arrays produce copies
   272 \end{frame}
   259 \end{frame}
   273 
   260 
   274 \subsection{Linear Algebra}
   261 \subsection{Linear Algebra}
   275 \begin{frame}[fragile]
   262 \begin{frame}[fragile]
   276   \frametitle{Linear Algebra}
   263   \frametitle{Linear Algebra}
   277   Try it!
       
   278   \begin{lstlisting}
   264   \begin{lstlisting}
   279     >>> import scipy as sp
   265     >>> import scipy as sp
   280     >>> from scipy import linalg
   266     >>> from scipy import linalg
   281     >>> A=sp.mat(np.arange(1,10))
   267     >>> A = sp.array(sp.arange(1,10))
   282     >>> A.shape=3,3
   268     >>> A.shape = 3,3
   283     >>> linalg.inv(A)
   269     >>> linalg.inv(A)
   284     >>> linalg.det(A)
   270     >>> linalg.det(A)
   285     >>> linalg.norm(A)
   271     >>> linalg.norm(A)
   286     >>> linalg.expm(A) #logm
   272     >>> linalg.expm(A) #logm
   287     >>> linalg.sinm(A) #cosm, tanm, ...
   273     >>> linalg.sinm(A) #cosm, tanm, ...
   288   \end{lstlisting}
   274   \end{lstlisting}
   289 \end{frame}
   275 \end{frame}
   290 
   276 
   291 \begin{frame}[fragile]
   277 \begin{frame}[fragile]
   292   \frametitle{Linear Algebra ...}
   278   \frametitle{Linear Algebra ...}
   293   Try it!
   279   \begin{lstlisting}
   294   \begin{lstlisting}
   280     >>> A = sp.array(sp.arange(1,10))
   295     >>> A = sp.mat(np.arange(1,10))
   281     >>> A.shape = 3,3
   296     >>> A.shape=3,3
       
   297     >>> linalg.lu(A)
   282     >>> linalg.lu(A)
   298     >>> linalg.eig(A)
   283     >>> linalg.eig(A)
   299     >>> linalg.eigvals(A)
   284     >>> linalg.eigvals(A)
   300   \end{lstlisting}
   285   \end{lstlisting}
   301 \end{frame}
   286 \end{frame}
   302 
   287 
   303 \begin{frame}[fragile]
   288 \begin{frame}[fragile]
   304   \frametitle{Solving Linear Equations}
   289   \frametitle{Solving Linear Equations}
       
   290   \vspace{-0.2in}
   305   \begin{align*}
   291   \begin{align*}
   306     3x + 2y - z  & = 1 \\
   292     3x + 2y - z  & = 1 \\
   307     2x - 2y + 4z  & = -2 \\
   293     2x - 2y + 4z  & = -2 \\
   308     -x + \frac{1}{2}y -z & = 0
   294     -x + \frac{1}{2}y -z & = 0
   309   \end{align*}
   295   \end{align*}
   310   To Solve this, 
   296   To Solve this, 
   311   \begin{lstlisting}
   297   \begin{lstlisting}
   312     >>> A = sp.mat([[3,2,-1],[2,-2,4]
   298     >>> A = sp.array([[3,2,-1],[2,-2,4]
   313                   ,[-1,1/2,-1]])
   299                   ,[-1,1/2,-1]])
   314     >>> B = sp.mat([[1],[-2],[0]])
   300     >>> b = sp.array([1,-2,0])
   315     >>> linalg.solve(A,B)
   301     >>> x = linalg.solve(A,b)
       
   302     >>> Ax = sp.dot(A,x)
       
   303     >>> sp.allclose(Ax, b)
   316   \end{lstlisting}
   304   \end{lstlisting}
   317 \inctime{15}
   305 \inctime{15}
   318 \end{frame}
   306 \end{frame}
   319 
   307 
   320 \subsection{Integration}
   308 \subsection{Integration}
   326     \item Numerical integrators of ODE systems
   314     \item Numerical integrators of ODE systems
   327   \end{itemize}
   315   \end{itemize}
   328   Calculate the area under $(sin(x) + x^2)$ in the range $(0,1)$
   316   Calculate the area under $(sin(x) + x^2)$ in the range $(0,1)$
   329   \begin{lstlisting}
   317   \begin{lstlisting}
   330     >>> def f(x):
   318     >>> def f(x):
   331             return np.sin(x)+x**2
   319             return sin(x)+x**2
   332     >>> integrate.quad(f, 0, 1)
   320     >>> integrate.quad(f, 0, 1)
   333   \end{lstlisting}
   321   \end{lstlisting}
   334 \end{frame}
   322 \end{frame}
   335 
   323 
   336 \begin{frame}[fragile]
   324 \begin{frame}[fragile]
   340   \frac{dx}{dt}&=-e^{-t}x^2\\ 
   328   \frac{dx}{dt}&=-e^{-t}x^2\\ 
   341            x&=2 \quad at \ t=0
   329            x&=2 \quad at \ t=0
   342   \end{align*}
   330   \end{align*}
   343   \begin{lstlisting}
   331   \begin{lstlisting}
   344 >>> def dx_dt(x,t):
   332 >>> def dx_dt(x,t):
   345         return -np.exp(-t)*x**2
   333         return -exp(-t)*x**2
   346 >>> t=np.linspace(0,2,100)
   334 >>> t = linspace(0,2,100)
   347 >>> x=integrate.odeint(dx_dt, 2, t)
   335 >>> x = integrate.odeint(dx_dt, 2, t)
   348 >>> plt.plot(x,t)
   336 >>> plt.plot(x,t)
   349   \end{lstlisting}
   337   \end{lstlisting}
   350 \inctime{10}
   338 \inctime{10}
   351 \end{frame}
   339 \end{frame}
   352 
   340 
   353 \subsection{Interpolation}
   341 \subsection{Interpolation}
   354 \begin{frame}[fragile]
   342 \begin{frame}[fragile]
   355   \frametitle{Interpolation}
   343   \frametitle{Interpolation}
   356   Try it!
       
   357   \begin{lstlisting}
   344   \begin{lstlisting}
   358 >>> from scipy import interpolate
   345 >>> from scipy import interpolate
   359 >>> interpolate.interp1d?
   346 >>> interpolate.interp1d?
   360 >>> x = np.arange(0,2*np.pi,np.pi/4)
   347 >>> x = arange(0,2*pi,pi/4)
   361 >>> y = np.sin(x)
   348 >>> y = sin(x)
   362 >>> fl = interpolate.interp1d(
   349 >>> fl = interpolate.interp1d(
   363             x,y,kind='linear')
   350             x,y,kind='linear')
   364 >>> fc = interpolate.interp1d(
   351 >>> fc = interpolate.interp1d(
   365              x,y,kind='cubic')
   352              x,y,kind='cubic')
   366 >>> fl(np.pi/3)
   353 >>> fl(pi/3)
   367 >>> fc(np.pi/3)
   354 >>> fc(pi/3)
   368   \end{lstlisting}
   355   \end{lstlisting}
   369 \end{frame}
   356 \end{frame}
   370 
   357 
   371 \begin{frame}[fragile]
   358 \begin{frame}[fragile]
   372   \frametitle{Interpolation - Splines}
   359   \frametitle{Interpolation - Splines}
   373   Plot the Cubic Spline of $sin(x)$
   360   Plot the Cubic Spline of $sin(x)$
   374   \begin{lstlisting}
   361   \begin{lstlisting}
   375 >>> tck = interpolate.splrep(x,y)
   362 >>> tck = interpolate.splrep(x,y)
   376 >>> X = np.arange(0,2*np.pi,np.pi/50)
   363 >>> xs = arange(0,2*pi,pi/50)
   377 >>> Y = interpolate.splev(X,tck,der=0)
   364 >>> ys = interpolate.splev(X,tck,der=0)
   378 >>> plt.plot(x,y,'o',x,y,X,Y)
   365 >>> plt.plot(x,y,'o',x,y,xs,ys)
   379 >>> plt.show()
   366 >>> plt.show()
   380   \end{lstlisting}
   367   \end{lstlisting}
   381 \inctime{10}
   368 \inctime{10}
   382 \end{frame}
   369 \end{frame}
   383 
   370 
   384 \subsection{Signal Processing}
   371 \subsection{Signal Processing}
   385 \begin{frame}[fragile]
   372 \begin{frame}[fragile]
   386   \frametitle{Signal \& Image Processing}
   373   \frametitle{Signal \& Image Processing}
   387     \begin{itemize}
   374     \begin{itemize}
   388      \item Convolution
   375      \item Convolution
   389      \item B-splines
       
   390      \item Filtering
   376      \item Filtering
   391      \item Filter design
   377      \item Filter design
   392      \item IIR filter design
   378      \item IIR filter design
   393      \item Linear Systems
   379      \item Linear Systems
   394      \item LTI Reresentations
   380      \item LTI Representations
   395      \item Waveforms
       
   396      \item Window functions
   381      \item Window functions
   397      \item Wavelets
       
   398     \end{itemize}
   382     \end{itemize}
   399 \end{frame}
   383 \end{frame}
   400 
   384 
   401 \begin{frame}[fragile]
   385 \begin{frame}[fragile]
   402   \frametitle{Signal \& Image Processing}
   386   \frametitle{Signal \& Image Processing}
   403   Applying a simple median filter
   387   Applying a simple median filter
   404   \begin{lstlisting}
   388   \begin{lstlisting}
   405 >>> from scipy import signal, ndimage
   389 >>> from scipy import signal, ndimage
   406 >>> from scipy import lena
   390 >>> from scipy import lena
   407 >>> A=lena().astype('float32')
   391 >>> A = lena().astype('float32')
   408 >>> B=signal.medfilt2d(A)
   392 >>> B = signal.medfilt2d(A)
   409 >>> imshow(B)
   393 >>> imshow(B)
   410   \end{lstlisting}
   394   \end{lstlisting}
   411   Zooming an array - uses spline interpolation
   395   Zooming an array - uses spline interpolation
   412   \begin{lstlisting}
   396   \begin{lstlisting}
   413 >>> b=ndimage.zoom(A,0.5)
   397 >>> b = ndimage.zoom(A,0.5)
   414 >>> imshow(b)
   398 >>> imshow(b)
       
   399 >>> b = ndimage.zoom(A,2)
   415   \end{lstlisting}
   400   \end{lstlisting}
   416     \inctime{5}
   401     \inctime{5}
   417 \end{frame}
   402 \end{frame}
   418 
   403 
   419 \begin{frame}[fragile]
   404 \begin{frame}[fragile]