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} |
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} |
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] |