|
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
2 % Tutorial slides on Python. |
|
3 % |
|
4 % Author: Prabhu Ramachandran <prabhu at aero.iitb.ac.in> |
|
5 % Copyright (c) 2005-2008, 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] {Asokan Pichai\\Prabhu Ramachandran} |
|
79 |
|
80 \institute[FOSSEE] {FOSSEE Team} |
|
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 \begin{frame}[fragile] |
|
121 \frametitle{Broadcasting} |
|
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} |
|
156 \end{frame} |
|
157 |
|
158 \begin{frame}[fragile] |
|
159 \frametitle{Copies \& Views} |
|
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} |
|
186 \inctime{15} |
|
187 \end{frame} |
|
188 |
|
189 \begin{frame} |
|
190 \frametitle{More Numpy Functions \& Methods} |
|
191 More functions |
|
192 \begin{itemize} |
|
193 \item \typ{take} |
|
194 \item \typ{choose} |
|
195 \item \typ{where} |
|
196 \item \typ{compress} |
|
197 \item \typ{concatenate} |
|
198 \end{itemize} |
|
199 Ufunc methods |
|
200 \begin{itemize} |
|
201 \item \typ{reduce} |
|
202 \item \typ{accumulate} |
|
203 \item \typ{outer} |
|
204 \item \typ{reduceat} |
|
205 \end{itemize} |
|
206 \inctime{5} |
|
207 \end{frame} |
|
208 |
|
209 \begin{frame} |
|
210 {Intro to SciPy} |
|
211 \begin{itemize} |
|
212 \item \url{http://www.scipy.org} |
|
213 \item Open source scientific libraries for Python |
|
214 \item Based on NumPy |
|
215 \end{itemize} |
|
216 \end{frame} |
|
217 |
|
218 \begin{frame} |
|
219 \frametitle{SciPy} |
|
220 \begin{itemize} |
|
221 \item Provides: |
|
222 \begin{itemize} |
|
223 \item Linear algebra |
|
224 \item Numerical integration |
|
225 \item Fourier transforms |
|
226 \item Signal processing |
|
227 \item Special functions |
|
228 \item Statistics |
|
229 \item Optimization |
|
230 \item Image processing |
|
231 \item ODE solvers |
|
232 \end{itemize} |
|
233 \item Uses LAPACK, QUADPACK, ODEPACK, FFTPACK etc. from netlib |
|
234 \end{itemize} |
|
235 \end{frame} |
|
236 |
|
237 \begin{frame}[fragile] |
|
238 \frametitle{Linear Algebra} |
|
239 \typ{>>> from scipy import linalg} |
|
240 \begin{itemize} |
|
241 \item \typ{linalg.det, linalg.norm} |
|
242 \item \typ{linalg.eig, linalg.lu} |
|
243 \item \typ{linalg.expm, linalg.logm} |
|
244 \item \typ{linalg.sinm, linalg.sinhm} |
|
245 \end{itemize} |
|
246 \end{frame} |
|
247 |
|
248 \begin{frame}[fragile] |
|
249 \frametitle{Linear Algebra \ldots} |
|
250 \begin{align*} |
|
251 3x + 2y - z & = 1 \\ |
|
252 2x - 2y + 4z & = -2 \\ |
|
253 -x + \frac{1}{2}y -z & = 0 |
|
254 \end{align*} |
|
255 \begin{lstlisting} |
|
256 >>> linalg.solve(A,B) |
|
257 \end{lstlisting} |
|
258 \inctime{15} |
|
259 \end{frame} |
|
260 |
|
261 \begin{frame}[fragile] |
|
262 \begin{itemize} |
|
263 \item Integrating Functions given function object |
|
264 \item Integrating Functions given fixed samples |
|
265 \item Numerical integrators of ODE systems |
|
266 \end{itemize} |
|
267 \frametitle{Integrate} |
|
268 Calculate $\int^1_0sin(x) + x^2$ |
|
269 \begin{lstlisting} |
|
270 >>> def f(x): |
|
271 return np.sin(x)+x**2 |
|
272 >>> integrate.quad(f, 0, 1) |
|
273 \end{lstlisting} |
|
274 \end{frame} |
|
275 |
|
276 \begin{frame}[fragile] |
|
277 \frametitle{Integrate \ldots} |
|
278 Numerically solve ODEs\\ |
|
279 \begin{align*} |
|
280 \frac{dx}{dt}&=-e^{(-t)}x^2(t)\\ |
|
281 x(0)&=2 |
|
282 \end{align*} |
|
283 \begin{lstlisting} |
|
284 def dx_dt(x,t): |
|
285 return -np.exp(-t)*x**2 |
|
286 |
|
287 x=integrate.odeint(dx_dt, 2, t) |
|
288 plt.plot(x,t) |
|
289 \end{lstlisting} |
|
290 \inctime{10} |
|
291 \end{frame} |
|
292 |
|
293 \begin{frame}[fragile] |
|
294 \frametitle{Interpolation} |
|
295 \begin{itemize} |
|
296 \item \typ{interpolate.interp1d, ...} |
|
297 \item \typ{interpolate.splrep, splev} |
|
298 \end{itemize} |
|
299 Cubic Spline of $sin(x)$ |
|
300 \begin{lstlisting} |
|
301 x = np.arange(0,2*np.pi,np.pi/8) |
|
302 y = np.sin(x) |
|
303 t = interpolate.splrep(x,y,s=0) |
|
304 X = np.arange(0,2*np.pi,np.pi/50) |
|
305 Y = interpolate.splev(X,t,der=0) |
|
306 |
|
307 plt.plot(x,y,'o',x,y,X,Y) |
|
308 plt.show() |
|
309 \end{lstlisting} |
|
310 \inctime{10} |
|
311 \end{frame} |
|
312 |
|
313 \begin{frame}[fragile] |
|
314 \frametitle{Signal \& Image Processing} |
|
315 \begin{itemize} |
|
316 \item Convolution |
|
317 \item B-splines |
|
318 \item Filtering |
|
319 \item Filter design |
|
320 \item IIR filter design |
|
321 \item Linear Systems |
|
322 \item LTI Reresentations |
|
323 \item Waveforms |
|
324 \item Window functions |
|
325 \item Wavelets |
|
326 \end{itemize} |
|
327 \end{frame} |
|
328 |
|
329 \begin{frame}[fragile] |
|
330 \frametitle{Signal \& Image Processing} |
|
331 Applying a simple median filter |
|
332 \begin{lstlisting} |
|
333 from scipy import signal, ndimage |
|
334 from scipy import lena |
|
335 A=lena().astype('float32') |
|
336 B=signal.medfilt2d(A) |
|
337 imshow(B) |
|
338 \end{lstlisting} |
|
339 Zooming an array - uses spline interpolation |
|
340 \begin{lstlisting} |
|
341 b=ndimage.zoom(A,0.5) |
|
342 imshow(b) |
|
343 \inctime{5} |
|
344 \end{lstlisting} |
|
345 |
|
346 \end{frame} |
|
347 |
|
348 \begin{frame}[fragile] |
|
349 \frametitle{Problems} |
|
350 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: |
|
351 \begin{equation*} |
|
352 \frac{d^2x}{dt^2}+\mu(x^2-1)\frac{dx}{dt}+x= 0 |
|
353 \end{equation*} |
|
354 \inctime{25} |
|
355 \end{frame} |
|
356 |
|
357 |
|
358 \end{document} |
|
359 |
|
360 - Numpy arrays (30 mins) |
|
361 - Matrices |
|
362 - random number generation. |
|
363 - Image manipulation: jigsaw puzzle. |
|
364 - Monte-carlo integration. |
|
365 |
|
366 |