1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
2 % Tutorial slides on Python. |
|
3 % |
|
4 % Author: Prabhu Ramachandran <prabhu at aero.iitb.ac.in> |
|
5 % Copyright (c) 2005-2009, Prabhu Ramachandran |
|
6 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
7 |
|
8 \documentclass[14pt,compress]{beamer} |
|
9 %\documentclass[draft]{beamer} |
|
10 %\documentclass[compress,handout]{beamer} |
|
11 %\usepackage{pgfpages} |
|
12 %\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm] |
|
13 |
|
14 % Modified from: generic-ornate-15min-45min.de.tex |
|
15 \mode<presentation> |
|
16 { |
|
17 \usetheme{Warsaw} |
|
18 \useoutertheme{split} |
|
19 \setbeamercovered{transparent} |
|
20 } |
|
21 |
|
22 \usepackage[english]{babel} |
|
23 \usepackage[latin1]{inputenc} |
|
24 %\usepackage{times} |
|
25 \usepackage[T1]{fontenc} |
|
26 |
|
27 % Taken from Fernando's slides. |
|
28 \usepackage{ae,aecompl} |
|
29 \usepackage{mathpazo,courier,euler} |
|
30 \usepackage[scaled=.95]{helvet} |
|
31 |
|
32 \definecolor{darkgreen}{rgb}{0,0.5,0} |
|
33 |
|
34 \usepackage{listings} |
|
35 \lstset{language=Python, |
|
36 basicstyle=\ttfamily\bfseries, |
|
37 commentstyle=\color{red}\itshape, |
|
38 stringstyle=\color{darkgreen}, |
|
39 showstringspaces=false, |
|
40 keywordstyle=\color{blue}\bfseries} |
|
41 |
|
42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
43 % Macros |
|
44 \setbeamercolor{emphbar}{bg=blue!20, fg=black} |
|
45 \newcommand{\emphbar}[1] |
|
46 {\begin{beamercolorbox}[rounded=true]{emphbar} |
|
47 {#1} |
|
48 \end{beamercolorbox} |
|
49 } |
|
50 \newcounter{time} |
|
51 \setcounter{time}{0} |
|
52 \newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} |
|
53 |
|
54 \newcommand{\typ}[1]{\lstinline{#1}} |
|
55 |
|
56 \newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } |
|
57 |
|
58 %%% This is from Fernando's setup. |
|
59 % \usepackage{color} |
|
60 % \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} |
|
61 % % Use and configure listings package for nicely formatted code |
|
62 % \usepackage{listings} |
|
63 % \lstset{ |
|
64 % language=Python, |
|
65 % basicstyle=\small\ttfamily, |
|
66 % commentstyle=\ttfamily\color{blue}, |
|
67 % stringstyle=\ttfamily\color{orange}, |
|
68 % showstringspaces=false, |
|
69 % breaklines=true, |
|
70 % postbreak = \space\dots |
|
71 % } |
|
72 |
|
73 |
|
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
75 % Title page |
|
76 \title[]{Numerical Computing with Numpy \& Scipy} |
|
77 |
|
78 \author[FOSSEE Team] {FOSSEE} |
|
79 |
|
80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} |
|
81 \date[] {11, October 2009} |
|
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
83 |
|
84 %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} |
|
85 %\logo{\pgfuseimage{iitmlogo}} |
|
86 |
|
87 |
|
88 %% Delete this, if you do not want the table of contents to pop up at |
|
89 %% the beginning of each subsection: |
|
90 \AtBeginSubsection[] |
|
91 { |
|
92 \begin{frame}<beamer> |
|
93 \frametitle{Outline} |
|
94 \tableofcontents[currentsection,currentsubsection] |
|
95 \end{frame} |
|
96 } |
|
97 |
|
98 \AtBeginSection[] |
|
99 { |
|
100 \begin{frame}<beamer> |
|
101 \frametitle{Outline} |
|
102 \tableofcontents[currentsection,currentsubsection] |
|
103 \end{frame} |
|
104 } |
|
105 |
|
106 % If you wish to uncover everything in a step-wise fashion, uncomment |
|
107 % the following command: |
|
108 %\beamerdefaultoverlayspecification{<+->} |
|
109 |
|
110 %\includeonlyframes{current,current1,current2,current3,current4,current5,current6} |
|
111 |
|
112 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
113 % DOCUMENT STARTS |
|
114 \begin{document} |
|
115 |
|
116 \begin{frame} |
|
117 \maketitle |
|
118 \end{frame} |
|
119 |
|
120 \section{Advanced Numpy} |
|
121 \begin{frame}[fragile] |
|
122 \frametitle{Broadcasting} |
|
123 \begin{lstlisting} |
|
124 >>> a = arange(4) |
|
125 >>> b = arange(5) |
|
126 >>> a+b #Does this work? |
|
127 >>> a+3 |
|
128 >>> c = array([3]) |
|
129 >>> a+c #Works! |
|
130 >>> b+c #But how? |
|
131 >>> a.shape, b.shape, c.shape |
|
132 \end{lstlisting} |
|
133 \begin{itemize} |
|
134 \item Enter Broadcasting! |
|
135 \end{itemize} |
|
136 \end{frame} |
|
137 |
|
138 \begin{frame}[fragile] |
|
139 \frametitle{Broadcasting} |
|
140 \begin{columns} |
|
141 \column{0.65\textwidth} |
|
142 \hspace*{-1.5in} |
|
143 \begin{lstlisting} |
|
144 >>> a = arange(4) |
|
145 >>> a+3 |
|
146 array([3, 4, 5, 6]) |
|
147 \end{lstlisting} |
|
148 \column{0.35\textwidth} |
|
149 \includegraphics[height=0.7in, interpolate=true]{data/broadcast_scalar} |
|
150 \end{columns} |
|
151 \end{frame} |
|
152 |
|
153 \begin{frame}[fragile] |
|
154 \frametitle{Broadcasting in 3D} |
|
155 \begin{lstlisting} |
|
156 >>> x = ones((3, 5, 1)) |
|
157 >>> y = ones(8) |
|
158 >>> (x + y).shape |
|
159 (3, 5, 8) |
|
160 \end{lstlisting} |
|
161 \begin{figure} |
|
162 \begin{center} |
|
163 \includegraphics[height=1.5in, interpolate=true]{data/array_3x5x8} |
|
164 \end{center} |
|
165 \end{figure} |
|
166 \end{frame} |
|
167 |
|
168 \begin{frame}[fragile] |
|
169 \frametitle{Copies \& Views} |
|
170 \vspace{-0.1in} |
|
171 \begin{lstlisting} |
|
172 >>> a = arange(1,9); a.shape=3,3 |
|
173 >>> b = a |
|
174 >>> b is a |
|
175 >>> b[0,0]=0; print a |
|
176 >>> c = a.view() |
|
177 >>> c is a |
|
178 >>> c.base is a |
|
179 >>> c.flags.owndata |
|
180 >>> d = a.copy() |
|
181 >>> d.base is a |
|
182 >>> d.flags.owndata |
|
183 \end{lstlisting} |
|
184 \end{frame} |
|
185 |
|
186 \begin{frame}[fragile] |
|
187 \frametitle{Copies \& Views} |
|
188 \vspace{-0.1in} |
|
189 \begin{lstlisting} |
|
190 >>> b = a[0,1:3] |
|
191 >>> c = a[0::2,0::2] |
|
192 >>> a.flags.owndata |
|
193 >>> b.flags.owndata |
|
194 >>> b.base |
|
195 >>> c.base is a |
|
196 \end{lstlisting} |
|
197 \begin{itemize} |
|
198 \item Slicing and Striding just reference the same memory |
|
199 \item They produce views of the data, not copies |
|
200 \end{itemize} |
|
201 \end{frame} |
|
202 |
|
203 \begin{frame}[fragile] |
|
204 \frametitle{Copies contd \ldots} |
|
205 \begin{lstlisting} |
|
206 >>> a = arange(1, 10, 2) |
|
207 >>> b = a[array([0,2,3])] |
|
208 >>> b.flags.owndata |
|
209 >>> abool=a>5 |
|
210 >>> c = a[abool] |
|
211 >>> c.flags.owndata |
|
212 \end{lstlisting} |
|
213 \begin{itemize} |
|
214 \item Indexing arrays or Boolean arrays produce copies |
|
215 \end{itemize} |
|
216 \inctime{15} |
|
217 \end{frame} |
|
218 |
|
219 \section{SciPy} |
|
220 \subsection{Introduction} |
|
221 \begin{frame} |
|
222 {Intro to SciPy} |
|
223 \begin{itemize} |
|
224 \item \url{http://www.scipy.org} |
|
225 \item Open source scientific libraries for Python |
|
226 \item Based on NumPy |
|
227 \end{itemize} |
|
228 \end{frame} |
|
229 |
|
230 \begin{frame} |
|
231 \frametitle{SciPy} |
|
232 \begin{itemize} |
|
233 \item Provides: |
|
234 \begin{itemize} |
|
235 \item Linear algebra |
|
236 \item Numerical integration |
|
237 \item Fourier transforms |
|
238 \item Signal processing |
|
239 \item Special functions |
|
240 \item Statistics |
|
241 \item Optimization |
|
242 \item Image processing |
|
243 \item ODE solvers |
|
244 \end{itemize} |
|
245 \item Uses LAPACK, QUADPACK, ODEPACK, FFTPACK etc. from netlib |
|
246 \end{itemize} |
|
247 \end{frame} |
|
248 |
|
249 \begin{frame}[fragile] |
|
250 \frametitle{SciPy - Functions \& Submodules} |
|
251 \begin{itemize} |
|
252 \item All \typ{numpy} functions are in \typ{scipy} namespace |
|
253 \item Domain specific functions organized into subpackages |
|
254 \item Subpackages need to be imported separately |
|
255 \end{itemize} |
|
256 \begin{lstlisting} |
|
257 >>> from scipy import linalg |
|
258 \end{lstlisting} |
|
259 \end{frame} |
|
260 |
|
261 \subsection{Linear Algebra} |
|
262 \begin{frame}[fragile] |
|
263 \frametitle{Linear Algebra} |
|
264 \begin{lstlisting} |
|
265 >>> import scipy as sp |
|
266 >>> from scipy import linalg |
|
267 >>> A = sp.array(sp.arange(1,10)) |
|
268 >>> A.shape = 3,3 |
|
269 >>> linalg.inv(A) |
|
270 >>> linalg.det(A) |
|
271 >>> linalg.norm(A) |
|
272 >>> linalg.expm(A) #logm |
|
273 >>> linalg.sinm(A) #cosm, tanm, ... |
|
274 \end{lstlisting} |
|
275 \end{frame} |
|
276 |
|
277 \begin{frame}[fragile] |
|
278 \frametitle{Linear Algebra ...} |
|
279 \begin{lstlisting} |
|
280 >>> A = sp.array(sp.arange(1,10)) |
|
281 >>> A.shape = 3,3 |
|
282 >>> linalg.lu(A) |
|
283 >>> linalg.eig(A) |
|
284 >>> linalg.eigvals(A) |
|
285 \end{lstlisting} |
|
286 \end{frame} |
|
287 |
|
288 \begin{frame}[fragile] |
|
289 \frametitle{Solving Linear Equations} |
|
290 \vspace{-0.2in} |
|
291 \begin{align*} |
|
292 3x + 2y - z & = 1 \\ |
|
293 2x - 2y + 4z & = -2 \\ |
|
294 -x + \frac{1}{2}y -z & = 0 |
|
295 \end{align*} |
|
296 To Solve this, |
|
297 \begin{lstlisting} |
|
298 >>> A = sp.array([[3,2,-1],[2,-2,4] |
|
299 ,[-1,1/2,-1]]) |
|
300 >>> b = sp.array([1,-2,0]) |
|
301 >>> x = linalg.solve(A,b) |
|
302 >>> Ax = sp.dot(A,x) |
|
303 >>> sp.allclose(Ax, b) |
|
304 \end{lstlisting} |
|
305 \inctime{15} |
|
306 \end{frame} |
|
307 |
|
308 \subsection{Integration} |
|
309 \begin{frame}[fragile] |
|
310 \frametitle{Integrate} |
|
311 \begin{itemize} |
|
312 \item Integrating Functions given function object |
|
313 \item Integrating Functions given fixed samples |
|
314 \item Numerical integrators of ODE systems |
|
315 \end{itemize} |
|
316 Calculate the area under $(sin(x) + x^2)$ in the range $(0,1)$ |
|
317 \begin{lstlisting} |
|
318 >>> def f(x): |
|
319 return sin(x)+x**2 |
|
320 >>> integrate.quad(f, 0, 1) |
|
321 \end{lstlisting} |
|
322 \end{frame} |
|
323 |
|
324 \begin{frame}[fragile] |
|
325 \frametitle{Integrate \ldots} |
|
326 Numerically solve ODEs\\ |
|
327 \begin{align*} |
|
328 \frac{dx}{dt}&=-e^{-t}x^2\\ |
|
329 x&=2 \quad at \ t=0 |
|
330 \end{align*} |
|
331 \begin{lstlisting} |
|
332 >>> def dx_dt(x,t): |
|
333 return -exp(-t)*x**2 |
|
334 >>> t = linspace(0,2,100) |
|
335 >>> x = integrate.odeint(dx_dt, 2, t) |
|
336 >>> plt.plot(x,t) |
|
337 \end{lstlisting} |
|
338 \inctime{10} |
|
339 \end{frame} |
|
340 |
|
341 \subsection{Interpolation} |
|
342 \begin{frame}[fragile] |
|
343 \frametitle{Interpolation} |
|
344 \begin{lstlisting} |
|
345 >>> from scipy import interpolate |
|
346 >>> interpolate.interp1d? |
|
347 >>> x = arange(0,2*pi,pi/4) |
|
348 >>> y = sin(x) |
|
349 >>> fl = interpolate.interp1d( |
|
350 x,y,kind='linear') |
|
351 >>> fc = interpolate.interp1d( |
|
352 x,y,kind='cubic') |
|
353 >>> fl(pi/3) |
|
354 >>> fc(pi/3) |
|
355 \end{lstlisting} |
|
356 \end{frame} |
|
357 |
|
358 \begin{frame}[fragile] |
|
359 \frametitle{Interpolation - Splines} |
|
360 Plot the Cubic Spline of $sin(x)$ |
|
361 \begin{lstlisting} |
|
362 >>> tck = interpolate.splrep(x,y) |
|
363 >>> xs = arange(0,2*pi,pi/50) |
|
364 >>> ys = interpolate.splev(X,tck,der=0) |
|
365 >>> plt.plot(x,y,'o',x,y,xs,ys) |
|
366 >>> plt.show() |
|
367 \end{lstlisting} |
|
368 \inctime{10} |
|
369 \end{frame} |
|
370 |
|
371 \subsection{Signal Processing} |
|
372 \begin{frame}[fragile] |
|
373 \frametitle{Signal \& Image Processing} |
|
374 \begin{itemize} |
|
375 \item Convolution |
|
376 \item Filtering |
|
377 \item Filter design |
|
378 \item IIR filter design |
|
379 \item Linear Systems |
|
380 \item LTI Representations |
|
381 \item Window functions |
|
382 \end{itemize} |
|
383 \end{frame} |
|
384 |
|
385 \begin{frame}[fragile] |
|
386 \frametitle{Signal \& Image Processing} |
|
387 Applying a simple median filter |
|
388 \begin{lstlisting} |
|
389 >>> from scipy import signal, ndimage |
|
390 >>> from scipy import lena |
|
391 >>> A = lena().astype('float32') |
|
392 >>> B = signal.medfilt2d(A) |
|
393 >>> imshow(B) |
|
394 \end{lstlisting} |
|
395 Zooming an array - uses spline interpolation |
|
396 \begin{lstlisting} |
|
397 >>> b = ndimage.zoom(A,0.5) |
|
398 >>> imshow(b) |
|
399 >>> b = ndimage.zoom(A,2) |
|
400 \end{lstlisting} |
|
401 \inctime{5} |
|
402 \end{frame} |
|
403 |
|
404 \begin{frame}[fragile] |
|
405 \frametitle{Problems} |
|
406 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: |
|
407 \begin{equation*} |
|
408 \frac{d^2x}{dt^2}+\mu(x^2-1)\frac{dx}{dt}+x= 0 |
|
409 \end{equation*} |
|
410 Make a plot of $\frac{dx}{dt}$ vs. $x$. |
|
411 \inctime{30} |
|
412 \end{frame} |
|
413 \begin{frame}{Summary} |
|
414 \begin{itemize} |
|
415 \item Advanced NumPy |
|
416 \item SciPy |
|
417 \begin{itemize} |
|
418 \item Linear Algebra |
|
419 \item Integration |
|
420 \item Interpolation |
|
421 \item Signal and Image processing |
|
422 \end{itemize} |
|
423 \end{itemize} |
|
424 \end{frame} |
|
425 \end{document} |
|
426 |
|
427 - Numpy arrays (30 mins) |
|
428 - Matrices |
|
429 - random number generation. |
|
430 - Image manipulation: jigsaw puzzle. |
|
431 - Monte-carlo integration. |
|