day2/session2.tex
changeset 37 d5b7be72693b
parent 36 8047755ae660
child 41 64177498937a
equal deleted inserted replaced
36:8047755ae660 37:d5b7be72693b
   111 
   111 
   112 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   112 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   113 % DOCUMENT STARTS
   113 % DOCUMENT STARTS
   114 \begin{document}
   114 \begin{document}
   115 
   115 
       
   116 \begin{frame}
       
   117   \maketitle
       
   118 \end{frame}
       
   119 
   116 \begin{frame}[fragile]
   120 \begin{frame}[fragile]
   117   \frametitle{Broadcasting}
   121   \frametitle{Broadcasting}
   118 
   122   \begin{itemize}
       
   123     \item Used so that functions can take inputs that are not of the same shape.
       
   124     \item 2 rules -
       
   125       \begin{enumerate}
       
   126       \item 1 (repeatedly) pre-pended to shapes of smaller arrays
       
   127       \item Size 1 in a dimension -> Largest size in that dimension
       
   128       \end{enumerate}
       
   129   \end{itemize}
       
   130   \begin{columns}
       
   131     \column{0.65\textwidth}
       
   132     \hspace*{-1.5in}
       
   133     \begin{lstlisting}
       
   134       >>> x = np.arange(4)
       
   135       >>> x+3
       
   136       array([3, 4, 5, 6])
       
   137     \end{lstlisting}
       
   138     \column{0.35\textwidth}
       
   139     \includegraphics[height=0.7in, interpolate=true]{data/broadcast_scalar}
       
   140   \end{columns}
       
   141 \end{frame}
       
   142 
       
   143 \begin{frame}[fragile]
       
   144   \frametitle{Broadcasting in 3D}
       
   145     \begin{lstlisting}
       
   146       >>> x = np.zeros((3, 5))
       
   147       >>> y = np.zeros(8)
       
   148       >>> (x[..., None] + y).shape
       
   149       (3, 5, 8)
       
   150     \end{lstlisting}
       
   151     \begin{figure}
       
   152       \begin{center}
       
   153       \includegraphics[height=1.5in, interpolate=true]{data/array_3x5x8}        
       
   154       \end{center}
       
   155     \end{figure}
   119 \end{frame}
   156 \end{frame}
   120 
   157 
   121 \begin{frame}[fragile]
   158 \begin{frame}[fragile]
   122   \frametitle{Copies \& Views}
   159   \frametitle{Copies \& Views}
   123 
   160   \begin{lstlisting}
       
   161     >>> a = array([[1,2,3], [4,5,6],     
       
   162                    [7,8,9]])
       
   163     >>> a[0,1:3]
       
   164     array([2, 3])
       
   165     >>> a[0::2,0::2]
       
   166     array([[1, 3],
       
   167           [7, 9]])
       
   168   \end{lstlisting}
       
   169   \begin{itemize}
       
   170   \item Slicing and Striding just reference the same memory
       
   171   \item They produce views of the data, not copies
       
   172   \end{itemize}
       
   173 \end{frame}
       
   174 
       
   175 \begin{frame}[fragile]
       
   176   \frametitle{Copies contd \ldots}
       
   177   \begin{lstlisting}
       
   178     >>> a[np.array([0,1,2])]
       
   179     array([[1, 2, 3],
       
   180            [4, 5, 6],
       
   181            [7, 8, 9]])
       
   182   \end{lstlisting}
       
   183   \begin{itemize}
       
   184   \item Index arrays or Boolean arrays produce copies
       
   185   \end{itemize}
   124 \end{frame}
   186 \end{frame}
   125 
   187 
   126 \begin{frame}
   188 \begin{frame}
   127   \frametitle{More Numpy Functions \& Methods}
   189   \frametitle{More Numpy Functions \& Methods}
   128   More functions
   190   More functions
   138     \item \typ{reduce}
   200     \item \typ{reduce}
   139     \item \typ{accumulate}
   201     \item \typ{accumulate}
   140     \item \typ{outer}
   202     \item \typ{outer}
   141     \item \typ{reduceat}
   203     \item \typ{reduceat}
   142   \end{itemize}
   204   \end{itemize}
       
   205 \inctime{5}
   143 \end{frame}
   206 \end{frame}
   144 
   207 
   145 \begin{frame}
   208 \begin{frame}
   146     {Intro to SciPy}
   209     {Intro to SciPy}
   147   \begin{itemize}
   210   \begin{itemize}
   148   \item \url{http://www.scipy.org}
   211   \item \url{http://www.scipy.org}
   149   \item Open source scientific libraries for Python
   212   \item Open source scientific libraries for Python
   150   \item Based on NumPy
   213   \item Based on NumPy
   151     \end{itemize}
   214     \end{itemize}
   152 
       
   153     \inctime{25}
       
   154 \end{frame}
   215 \end{frame}
   155 
   216 
   156 \begin{frame}
   217 \begin{frame}
   157   \frametitle{SciPy}
   218   \frametitle{SciPy}
   158   \begin{itemize}
   219   \begin{itemize}
   172   \end{itemize}
   233   \end{itemize}
   173 \end{frame}
   234 \end{frame}
   174 
   235 
   175 \begin{frame}[fragile]
   236 \begin{frame}[fragile]
   176   \frametitle{Linear Algebra}
   237   \frametitle{Linear Algebra}
   177 
   238   \typ{>>> from scipy import linalg}
   178 \end{frame}
   239   \begin{itemize}
   179 \begin{frame}[fragile]
   240     \item \typ{linalg.det, linalg.norm}
   180   \frametitle{ODEs}
   241     \item \typ{linalg.eig, linalg.lu}
   181 
   242     \item \typ{linalg.expm, linalg.logm}
   182 \end{frame}
   243     \item \typ{linalg.sinm, linalg.sinhm}
       
   244   \end{itemize}
       
   245 \end{frame}
       
   246 
       
   247 \begin{frame}[fragile]
       
   248   \frametitle{Linear Algebra \ldots}
       
   249   \begin{align*}
       
   250     3x + 2y - z  & = 1 \\
       
   251     2x - 2y + 4z  & = -2 \\
       
   252     -x + \frac{1}{2}y -z & = 0
       
   253   \end{align*}
       
   254   \begin{lstlisting}
       
   255     >>> linalg.solve(A,B)
       
   256   \end{lstlisting}
       
   257 \end{frame}
       
   258 
       
   259 \begin{frame}[fragile]
       
   260   \begin{itemize}
       
   261     \item Integrating Functions given function object
       
   262     \item Integrating Functions given fixed samples
       
   263     \item Numerical integrators of ODE systems
       
   264   \end{itemize}
       
   265   \frametitle{Integrate}
       
   266   Calculate $\int^1_0sin(x) + x^2$
       
   267   \begin{lstlisting}
       
   268     >>> def f(x):
       
   269             return np.sin(x)+x**2
       
   270     >>> integrate.quad(f, 0, 1)
       
   271   \end{lstlisting}
       
   272 \end{frame}
       
   273 
       
   274 \begin{frame}[fragile]
       
   275   \frametitle{Integrate \ldots}
       
   276   Numerically solve ODEs\\
       
   277   \begin{align*}
       
   278   \frac{dx}{dt}&=-e^{(-t)}x^2(t)\\ 
       
   279            x(0)&=2    
       
   280   \end{align*}
       
   281   \begin{lstlisting}
       
   282     def dx_dt(x,t):
       
   283         return -np.exp(-t)*x**2
       
   284 
       
   285     x=integrate.odeint(dx_dt, 2, t)
       
   286     plt.plot(x,t)
       
   287   \end{lstlisting}
       
   288 \end{frame}
       
   289 
   183 \begin{frame}[fragile]
   290 \begin{frame}[fragile]
   184   \frametitle{Interpolation}
   291   \frametitle{Interpolation}
   185 
   292   \begin{itemize}
       
   293     \item \typ{interpolate.interp1d, ...}
       
   294     \item \typ{interpolate.splrep, splev}
       
   295   \end{itemize}
       
   296   Cubic Spline of $sin(x)$
       
   297   \begin{lstlisting}
       
   298     x = np.arange(0,2*np.pi,np.pi/8)
       
   299     y = np.sin(x)
       
   300     t = interpolate.splrep(x,y,s=0)
       
   301     X = np.arange(0,2*np.pi,np.pi/50)
       
   302     Y = interpolate.splev(X,t,der=0)
       
   303 
       
   304     plt.plot(x,y,'o',x,y,X,Y)
       
   305     plt.show()
       
   306   \end{lstlisting}
   186 \end{frame}
   307 \end{frame}
   187 
   308 
   188 \begin{frame}[fragile]
   309 \begin{frame}[fragile]
   189   \frametitle{Signal \& Image Processing}
   310   \frametitle{Signal \& Image Processing}
       
   311     \begin{itemize}
       
   312      \item Convolution
       
   313      \item B-splines
       
   314      \item Filtering
       
   315      \item Filter design
       
   316      \item IIR filter design
       
   317      \item Linear Systems
       
   318      \item LTI Reresentations
       
   319      \item Waveforms
       
   320      \item Window functions
       
   321      \item Wavelets
       
   322     \end{itemize}
       
   323 \end{frame}
       
   324 
       
   325 \begin{frame}[fragile]
       
   326   \frametitle{Signal \& Image Processing}
       
   327   Applying a simple median filter
       
   328   \begin{lstlisting}
       
   329     from scipy import signal, ndimage
       
   330     from scipy import lena
       
   331     A=lena().astype('float32')
       
   332     B=signal.medfilt2d(A)
       
   333     imshow(B)
       
   334   \end{lstlisting}
       
   335   Zooming an array - uses spline interpolation
       
   336   \begin{lstlisting}
       
   337     b=ndimage.zoom(A,0.5)
       
   338     imshow(b)
       
   339   \end{lstlisting}
   190 
   340 
   191 \end{frame}
   341 \end{frame}
   192 
   342 
   193 \begin{frame}[fragile]
   343 \begin{frame}[fragile]
   194   \frametitle{Problems}
   344   \frametitle{Problems}
   195 
   345   The Van der Pol oscillator is a type of nonconservative oscillator with nonlinear damping. It evolves in time according to the second order differential equation:
       
   346   \begin{equation*}
       
   347   \frac{d^2x}{dt^2}+\mu(x^2-1)\frac{dx}{dt}+x= 0
       
   348   \end{equation*}
   196 \end{frame}
   349 \end{frame}
   197 
   350 
   198 
   351 
   199 \end{document}
   352 \end{document}
   200 
   353 
   203     - random number generation.
   356     - random number generation.
   204     - Image manipulation: jigsaw puzzle.
   357     - Image manipulation: jigsaw puzzle.
   205     - Monte-carlo integration.
   358     - Monte-carlo integration.
   206 
   359 
   207 
   360 
   208 \subsection{SciPy}
       
   209 
       
   210 \begin{frame}
       
   211   \frametitle{Using \texttt{SciPy}}
       
   212   \begin{itemize}
       
   213   \item SciPy is Open Source software for mathematics, science, and
       
   214     engineering
       
   215   \item \typ{import scipy}
       
   216   \item Built on NumPy
       
   217   \item Provides modules for statistics, optimization, integration,
       
   218     linear algebra, Fourier transforms, signal and image processing,
       
   219     genetic algorithms, ODE solvers, special functions, and more
       
   220   \item Used widely by scientists world over
       
   221   \item Details are beyond the scope of this tutorial
       
   222   \end{itemize}
       
   223 \end{frame}
       
   224