day2/session2.tex
changeset 97 555237dbce44
parent 96 a749db24e73b
equal deleted inserted replaced
96:a749db24e73b 97:555237dbce44
    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 
   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   \begin{lstlisting}
   123   \begin{lstlisting}
   124     >>> a = np.arange(4)
   124     >>> a = arange(4)
   125     >>> b = np.arange(5)
   125     >>> b = arange(5)
   126     >>> a+b #Does this work?
   126     >>> a+b #Does this work?
   127     >>> a+3
   127     >>> a+3
   128     >>> c=np.array([3])
   128     >>> c = array([3])
   129     >>> a+c #Works!
   129     >>> a+c #Works!
   130     >>> b+c #But how?
   130     >>> b+c #But how?
   131     >>> a.shape, b.shape, c.shape
   131     >>> a.shape, b.shape, c.shape
   132   \end{lstlisting}
   132   \end{lstlisting}
   133   \begin{itemize}
   133   \begin{itemize}
   139   \frametitle{Broadcasting}
   139   \frametitle{Broadcasting}
   140   \begin{columns}
   140   \begin{columns}
   141     \column{0.65\textwidth}
   141     \column{0.65\textwidth}
   142     \hspace*{-1.5in}
   142     \hspace*{-1.5in}
   143     \begin{lstlisting}
   143     \begin{lstlisting}
   144       >>> a = np.arange(4)
   144       >>> a = arange(4)
   145       >>> a+3
   145       >>> a+3
   146       array([3, 4, 5, 6])
   146       array([3, 4, 5, 6])
   147     \end{lstlisting}
   147     \end{lstlisting}
   148     \column{0.35\textwidth}
   148     \column{0.35\textwidth}
   149     \includegraphics[height=0.7in, interpolate=true]{data/broadcast_scalar}
   149     \includegraphics[height=0.7in, interpolate=true]{data/broadcast_scalar}
   150   \end{columns}
   150   \end{columns}
   151   \begin{itemize}
       
   152     \item Allows functions to take inputs that are not of the same shape
       
   153     \item 2 rules -
       
   154       \begin{enumerate}
       
   155       \item 1 is (repeatedly) prepended to shapes of smaller arrays
       
   156       \item Size 1 in a dimension changed to Largest size in that dimension
       
   157       \end{enumerate}
       
   158   \end{itemize}
       
   159 \end{frame}
   151 \end{frame}
   160 
   152 
   161 \begin{frame}[fragile]
   153 \begin{frame}[fragile]
   162   \frametitle{Broadcasting in 3D}
   154   \frametitle{Broadcasting in 3D}
   163     \begin{lstlisting}
   155     \begin{lstlisting}
   164       >>> x = np.ones((3, 5))
   156       >>> x = ones((3, 5, 1))
   165       >>> y = np.ones(8)
   157       >>> y = ones(8)
   166       >>> (x[..., None] + y).shape
   158       >>> (x + y).shape
   167       (3, 5, 8)
   159       (3, 5, 8)
   168     \end{lstlisting}
   160     \end{lstlisting}
   169     \begin{figure}
   161     \begin{figure}
   170       \begin{center}
   162       \begin{center}
   171       \includegraphics[height=1.5in, interpolate=true]{data/array_3x5x8}        
   163       \includegraphics[height=1.5in, interpolate=true]{data/array_3x5x8}        
   175 
   167 
   176 \begin{frame}[fragile]
   168 \begin{frame}[fragile]
   177   \frametitle{Copies \& Views}
   169   \frametitle{Copies \& Views}
   178   \vspace{-0.1in}
   170   \vspace{-0.1in}
   179   \begin{lstlisting}
   171   \begin{lstlisting}
   180     >>> a = np.arange(1,9); a.shape=3,3
   172     >>> a = arange(1,9); a.shape=3,3
   181     >>> b = a
   173     >>> b = a
   182     >>> b is a
   174     >>> b is a
   183     >>> b[0,0]=0; print a
   175     >>> b[0,0]=0; print a
   184     >>> c = a.view()
   176     >>> c = a.view()
   185     >>> c is a
   177     >>> c is a
   209 \end{frame}
   201 \end{frame}
   210 
   202 
   211 \begin{frame}[fragile]
   203 \begin{frame}[fragile]
   212   \frametitle{Copies contd \ldots}
   204   \frametitle{Copies contd \ldots}
   213   \begin{lstlisting}
   205   \begin{lstlisting}
   214     >>> b = a[np.array([0,1,2])]
   206     >>> a = arange(1, 10, 2)
   215     array([[1, 2, 3],
   207     >>> b = a[array([0,2,3])]
   216            [4, 5, 6],
       
   217            [7, 8, 9]])
       
   218     >>> b.flags.owndata
   208     >>> b.flags.owndata
   219     >>> abool=np.greater(a,2)
   209     >>> abool=a>5
   220     >>> c = a[abool]
   210     >>> c = a[abool]
   221     >>> c.flags.owndata
   211     >>> c.flags.owndata
   222   \end{lstlisting}
   212   \end{lstlisting}
   223   \begin{itemize}
   213   \begin{itemize}
   224   \item Indexing arrays or Boolean arrays produce copies
   214   \item Indexing arrays or Boolean arrays produce copies
   324     \item Numerical integrators of ODE systems
   314     \item Numerical integrators of ODE systems
   325   \end{itemize}
   315   \end{itemize}
   326   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)$
   327   \begin{lstlisting}
   317   \begin{lstlisting}
   328     >>> def f(x):
   318     >>> def f(x):
   329             return np.sin(x)+x**2
   319             return sin(x)+x**2
   330     >>> integrate.quad(f, 0, 1)
   320     >>> integrate.quad(f, 0, 1)
   331   \end{lstlisting}
   321   \end{lstlisting}
   332 \end{frame}
   322 \end{frame}
   333 
   323 
   334 \begin{frame}[fragile]
   324 \begin{frame}[fragile]
   338   \frac{dx}{dt}&=-e^{-t}x^2\\ 
   328   \frac{dx}{dt}&=-e^{-t}x^2\\ 
   339            x&=2 \quad at \ t=0
   329            x&=2 \quad at \ t=0
   340   \end{align*}
   330   \end{align*}
   341   \begin{lstlisting}
   331   \begin{lstlisting}
   342 >>> def dx_dt(x,t):
   332 >>> def dx_dt(x,t):
   343         return -np.exp(-t)*x**2
   333         return -exp(-t)*x**2
   344 >>> t = np.linspace(0,2,100)
   334 >>> t = linspace(0,2,100)
   345 >>> x = integrate.odeint(dx_dt, 2, t)
   335 >>> x = integrate.odeint(dx_dt, 2, t)
   346 >>> plt.plot(x,t)
   336 >>> plt.plot(x,t)
   347   \end{lstlisting}
   337   \end{lstlisting}
   348 \inctime{10}
   338 \inctime{10}
   349 \end{frame}
   339 \end{frame}
   352 \begin{frame}[fragile]
   342 \begin{frame}[fragile]
   353   \frametitle{Interpolation}
   343   \frametitle{Interpolation}
   354   \begin{lstlisting}
   344   \begin{lstlisting}
   355 >>> from scipy import interpolate
   345 >>> from scipy import interpolate
   356 >>> interpolate.interp1d?
   346 >>> interpolate.interp1d?
   357 >>> x = np.arange(0,2*np.pi,np.pi/4)
   347 >>> x = arange(0,2*pi,pi/4)
   358 >>> y = np.sin(x)
   348 >>> y = sin(x)
   359 >>> fl = interpolate.interp1d(
   349 >>> fl = interpolate.interp1d(
   360             x,y,kind='linear')
   350             x,y,kind='linear')
   361 >>> fc = interpolate.interp1d(
   351 >>> fc = interpolate.interp1d(
   362              x,y,kind='cubic')
   352              x,y,kind='cubic')
   363 >>> fl(np.pi/3)
   353 >>> fl(pi/3)
   364 >>> fc(np.pi/3)
   354 >>> fc(pi/3)
   365   \end{lstlisting}
   355   \end{lstlisting}
   366 \end{frame}
   356 \end{frame}
   367 
   357 
   368 \begin{frame}[fragile]
   358 \begin{frame}[fragile]
   369   \frametitle{Interpolation - Splines}
   359   \frametitle{Interpolation - Splines}
   370   Plot the Cubic Spline of $sin(x)$
   360   Plot the Cubic Spline of $sin(x)$
   371   \begin{lstlisting}
   361   \begin{lstlisting}
   372 >>> tck = interpolate.splrep(x,y)
   362 >>> tck = interpolate.splrep(x,y)
   373 >>> xs = np.arange(0,2*np.pi,np.pi/50)
   363 >>> xs = arange(0,2*pi,pi/50)
   374 >>> ys = interpolate.splev(X,tck,der=0)
   364 >>> ys = interpolate.splev(X,tck,der=0)
   375 >>> plt.plot(x,y,'o',x,y,xs,ys)
   365 >>> plt.plot(x,y,'o',x,y,xs,ys)
   376 >>> plt.show()
   366 >>> plt.show()
   377   \end{lstlisting}
   367   \end{lstlisting}
   378 \inctime{10}
   368 \inctime{10}
   381 \subsection{Signal Processing}
   371 \subsection{Signal Processing}
   382 \begin{frame}[fragile]
   372 \begin{frame}[fragile]
   383   \frametitle{Signal \& Image Processing}
   373   \frametitle{Signal \& Image Processing}
   384     \begin{itemize}
   374     \begin{itemize}
   385      \item Convolution
   375      \item Convolution
   386      \item B-splines
       
   387      \item Filtering
   376      \item Filtering
   388      \item Filter design
   377      \item Filter design
   389      \item IIR filter design
   378      \item IIR filter design
   390      \item Linear Systems
   379      \item Linear Systems
   391      \item LTI Reresentations
   380      \item LTI Representations
   392      \item Waveforms
       
   393      \item Window functions
   381      \item Window functions
   394      \item Wavelets
       
   395     \end{itemize}
   382     \end{itemize}
   396 \end{frame}
   383 \end{frame}
   397 
   384 
   398 \begin{frame}[fragile]
   385 \begin{frame}[fragile]
   399   \frametitle{Signal \& Image Processing}
   386   \frametitle{Signal \& Image Processing}
   407   \end{lstlisting}
   394   \end{lstlisting}
   408   Zooming an array - uses spline interpolation
   395   Zooming an array - uses spline interpolation
   409   \begin{lstlisting}
   396   \begin{lstlisting}
   410 >>> b = ndimage.zoom(A,0.5)
   397 >>> b = ndimage.zoom(A,0.5)
   411 >>> imshow(b)
   398 >>> imshow(b)
       
   399 >>> b = ndimage.zoom(A,2)
   412   \end{lstlisting}
   400   \end{lstlisting}
   413     \inctime{5}
   401     \inctime{5}
   414 \end{frame}
   402 \end{frame}
   415 
   403 
   416 \begin{frame}[fragile]
   404 \begin{frame}[fragile]