114 \begin{document} |
114 \begin{document} |
115 |
115 |
116 \begin{frame} |
116 \begin{frame} |
117 \maketitle |
117 \maketitle |
118 \end{frame} |
118 \end{frame} |
119 |
|
120 \begin{frame}[fragile] |
119 \begin{frame}[fragile] |
121 \frametitle{Broadcasting} |
120 \frametitle{Broadcasting} |
122 \begin{itemize} |
121 Try it! |
123 \item Used so that functions can take inputs that are not of the same shape. |
122 \begin{lstlisting} |
124 \item 2 rules - |
123 >>> a = np.arange(4) |
125 \begin{enumerate} |
124 >>> b = np.arange(5) |
126 \item 1 (repeatedly) pre-pended to shapes of smaller arrays |
125 >>> a+b |
127 \item Size 1 in a dimension -> Largest size in that dimension |
126 >>> a+3 |
128 \end{enumerate} |
127 >>> c=np.array([3]) |
129 \end{itemize} |
128 >>> a+c |
|
129 >>> b+c |
|
130 \end{lstlisting} |
|
131 \begin{itemize} |
|
132 \item Enter Broadcasting! |
|
133 \end{itemize} |
|
134 \end{frame} |
|
135 |
|
136 \begin{frame}[fragile] |
|
137 \frametitle{Broadcasting} |
130 \begin{columns} |
138 \begin{columns} |
131 \column{0.65\textwidth} |
139 \column{0.65\textwidth} |
132 \hspace*{-1.5in} |
140 \hspace*{-1.5in} |
133 \begin{lstlisting} |
141 \begin{lstlisting} |
134 >>> x = np.arange(4) |
142 >>> a = np.arange(4) |
135 >>> x+3 |
143 >>> a+3 |
136 array([3, 4, 5, 6]) |
144 array([3, 4, 5, 6]) |
137 \end{lstlisting} |
145 \end{lstlisting} |
138 \column{0.35\textwidth} |
146 \column{0.35\textwidth} |
139 \includegraphics[height=0.7in, interpolate=true]{data/broadcast_scalar} |
147 \includegraphics[height=0.7in, interpolate=true]{data/broadcast_scalar} |
140 \end{columns} |
148 \end{columns} |
|
149 \begin{itemize} |
|
150 \item Allows functions to take inputs not of the same shape |
|
151 \item 2 rules - |
|
152 \begin{enumerate} |
|
153 \item 1 is (repeatedly) prepended to shapes of smaller arrays |
|
154 \item Size 1 in a dimension changed to Largest size in that dimension |
|
155 \end{enumerate} |
|
156 \end{itemize} |
141 \end{frame} |
157 \end{frame} |
142 |
158 |
143 \begin{frame}[fragile] |
159 \begin{frame}[fragile] |
144 \frametitle{Broadcasting in 3D} |
160 \frametitle{Broadcasting in 3D} |
145 \begin{lstlisting} |
161 \begin{lstlisting} |
146 >>> x = np.zeros((3, 5)) |
162 >>> x = np.ones((3, 5)) |
147 >>> y = np.zeros(8) |
163 >>> y = np.ones(8) |
148 >>> (x[..., None] + y).shape |
164 >>> (x[..., None] + y).shape |
149 (3, 5, 8) |
165 (3, 5, 8) |
150 \end{lstlisting} |
166 \end{lstlisting} |
151 \begin{figure} |
167 \begin{figure} |
152 \begin{center} |
168 \begin{center} |
155 \end{figure} |
171 \end{figure} |
156 \end{frame} |
172 \end{frame} |
157 |
173 |
158 \begin{frame}[fragile] |
174 \begin{frame}[fragile] |
159 \frametitle{Copies \& Views} |
175 \frametitle{Copies \& Views} |
160 \begin{lstlisting} |
176 Try it! |
161 >>> a = array([[1,2,3], [4,5,6], |
177 \begin{lstlisting} |
162 [7,8,9]]) |
178 >>> a = np.array([[1,2,3],[4,5,6]]) |
163 >>> a[0,1:3] |
179 >>> b = a |
164 array([2, 3]) |
180 >>> b is a |
165 >>> a[0::2,0::2] |
181 >>> b[0,0]=0; print a |
166 array([[1, 3], |
182 >>> c = a.view() |
167 [7, 9]]) |
183 >>> c is a |
|
184 >>> c.base is a |
|
185 >>> c.flags.owndata |
|
186 >>> d = a.copy() |
|
187 >>> d.base is a |
|
188 >>> d.flags.owndata |
|
189 \end{lstlisting} |
|
190 \end{frame} |
|
191 |
|
192 \begin{frame}[fragile] |
|
193 \frametitle{Copies \& Views} |
|
194 Try it! |
|
195 \begin{lstlisting} |
|
196 >>> a = np.arange(1,9) |
|
197 >>> a.shape=3,3 |
|
198 >>> b = a[0,1:3] |
|
199 >>> c = a[0::2,0::2] |
|
200 >>> a.flags.owndata |
|
201 >>> b.flags.owndata |
|
202 >>> b.base |
|
203 >>> c.base is a |
168 \end{lstlisting} |
204 \end{lstlisting} |
169 \begin{itemize} |
205 \begin{itemize} |
170 \item Slicing and Striding just reference the same memory |
206 \item Slicing and Striding just reference the same memory |
171 \item They produce views of the data, not copies |
207 \item They produce views of the data, not copies |
172 \end{itemize} |
208 \end{itemize} |
173 \end{frame} |
209 \end{frame} |
174 |
210 |
175 \begin{frame}[fragile] |
211 \begin{frame}[fragile] |
176 \frametitle{Copies contd \ldots} |
212 \frametitle{Copies contd \ldots} |
177 \begin{lstlisting} |
213 \begin{lstlisting} |
178 >>> a[np.array([0,1,2])] |
214 >>> b = a[np.array([0,1,2])] |
179 array([[1, 2, 3], |
215 array([[1, 2, 3], |
180 [4, 5, 6], |
216 [4, 5, 6], |
181 [7, 8, 9]]) |
217 [7, 8, 9]]) |
182 \end{lstlisting} |
218 >>> b.flags.owndata |
183 \begin{itemize} |
219 >>> abool=np.greater(a,2) |
184 \item Index arrays or Boolean arrays produce copies |
220 >>> c = a[abool] |
|
221 >>> c.flags.owndata |
|
222 \end{lstlisting} |
|
223 \begin{itemize} |
|
224 \item Indexing arrays or Boolean arrays produce copies |
185 \end{itemize} |
225 \end{itemize} |
186 \inctime{15} |
226 \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} |
227 \end{frame} |
208 |
228 |
209 \begin{frame} |
229 \begin{frame} |
210 {Intro to SciPy} |
230 {Intro to SciPy} |
211 \begin{itemize} |
231 \begin{itemize} |
233 \item Uses LAPACK, QUADPACK, ODEPACK, FFTPACK etc. from netlib |
253 \item Uses LAPACK, QUADPACK, ODEPACK, FFTPACK etc. from netlib |
234 \end{itemize} |
254 \end{itemize} |
235 \end{frame} |
255 \end{frame} |
236 |
256 |
237 \begin{frame}[fragile] |
257 \begin{frame}[fragile] |
|
258 \frametitle{SciPy - Functions \& Submodules} |
|
259 \begin{itemize} |
|
260 \item All \typ{numpy} functions are in \typ{scipy} namespace |
|
261 \item Domain specific functions organized into subpackages |
|
262 \item Subpackages need to be imported separately |
|
263 \end{itemize} |
|
264 \begin{lstlisting} |
|
265 >>> from scipy import linalg |
|
266 \end{lstlisting} |
|
267 \end{frame} |
|
268 |
|
269 \begin{frame}[fragile] |
238 \frametitle{Linear Algebra} |
270 \frametitle{Linear Algebra} |
239 \typ{>>> from scipy import linalg} |
271 Try it! |
240 \begin{itemize} |
272 \begin{lstlisting} |
241 \item \typ{linalg.det, linalg.norm} |
273 >>> import scipy as sp |
242 \item \typ{linalg.eig, linalg.lu} |
274 >>> from scipy import linalg |
243 \item \typ{linalg.expm, linalg.logm} |
275 >>> A=sp.mat(np.arange(1,10)) |
244 \item \typ{linalg.sinm, linalg.sinhm} |
276 >>> A.shape=3,3 |
245 \end{itemize} |
277 >>> linalg.inv(A) |
246 \end{frame} |
278 >>> linalg.det(A) |
247 |
279 >>> linalg.norm(A) |
248 \begin{frame}[fragile] |
280 >>> linalg.expm(A) #logm |
249 \frametitle{Linear Algebra \ldots} |
281 >>> linalg.sinm(A) #cosm, tanm, ... |
|
282 \end{lstlisting} |
|
283 \end{frame} |
|
284 |
|
285 \begin{frame}[fragile] |
|
286 \frametitle{Linear Algebra ...} |
|
287 Try it! |
|
288 \begin{lstlisting} |
|
289 >>> A = sp.mat(np.arange(1,10)) |
|
290 >>> A.shape=3,3 |
|
291 >>> linalg.lu(A) |
|
292 >>> linalg.eig(A) |
|
293 >>> linalg.eigvals(A) |
|
294 \end{lstlisting} |
|
295 \end{frame} |
|
296 |
|
297 \begin{frame}[fragile] |
|
298 \frametitle{Solving Linear Equations} |
250 \begin{align*} |
299 \begin{align*} |
251 3x + 2y - z & = 1 \\ |
300 3x + 2y - z & = 1 \\ |
252 2x - 2y + 4z & = -2 \\ |
301 2x - 2y + 4z & = -2 \\ |
253 -x + \frac{1}{2}y -z & = 0 |
302 -x + \frac{1}{2}y -z & = 0 |
254 \end{align*} |
303 \end{align*} |
255 \begin{lstlisting} |
304 To Solve this, |
|
305 \begin{lstlisting} |
|
306 >>> A = sp.mat([[3,2,-1],[2,-2,4] |
|
307 ,[-1,1/2,-1]]) |
|
308 >>> B = sp.mat([[1],[-2],[0]]) |
256 >>> linalg.solve(A,B) |
309 >>> linalg.solve(A,B) |
257 \end{lstlisting} |
310 \end{lstlisting} |
258 \inctime{15} |
311 \inctime{15} |
259 \end{frame} |
312 \end{frame} |
260 |
313 |
261 \begin{frame}[fragile] |
314 \begin{frame}[fragile] |
|
315 \frametitle{Integrate} |
262 \begin{itemize} |
316 \begin{itemize} |
263 \item Integrating Functions given function object |
317 \item Integrating Functions given function object |
264 \item Integrating Functions given fixed samples |
318 \item Integrating Functions given fixed samples |
265 \item Numerical integrators of ODE systems |
319 \item Numerical integrators of ODE systems |
266 \end{itemize} |
320 \end{itemize} |
267 \frametitle{Integrate} |
321 Calculate $\int^1_0(sin(x) + x^2)dx$ |
268 Calculate $\int^1_0sin(x) + x^2$ |
|
269 \begin{lstlisting} |
322 \begin{lstlisting} |
270 >>> def f(x): |
323 >>> def f(x): |
271 return np.sin(x)+x**2 |
324 return np.sin(x)+x**2 |
272 >>> integrate.quad(f, 0, 1) |
325 >>> integrate.quad(f, 0, 1) |
273 \end{lstlisting} |
326 \end{lstlisting} |
290 \inctime{10} |
343 \inctime{10} |
291 \end{frame} |
344 \end{frame} |
292 |
345 |
293 \begin{frame}[fragile] |
346 \begin{frame}[fragile] |
294 \frametitle{Interpolation} |
347 \frametitle{Interpolation} |
295 \begin{itemize} |
348 Try it! |
296 \item \typ{interpolate.interp1d, ...} |
349 \begin{lstlisting} |
297 \item \typ{interpolate.splrep, splev} |
350 >>> from scipy import interpolate |
298 \end{itemize} |
351 >>> interpolate.interp1d? |
|
352 >>> x = np.arange(0,2*np.pi,np.pi/4) |
|
353 >>> y = np.sin(x) |
|
354 >>> fl = interpolate.interp1d(x,y,kind='linear') |
|
355 >>> fc = interpolate.interp1d(x,y,kind='cubic') |
|
356 >>> fl(np.pi/3) |
|
357 >>> fc(np.pi/3) |
|
358 \end{lstlisting} |
|
359 \end{frame} |
|
360 |
|
361 \begin{frame}[fragile] |
|
362 \frametitle{Interpolation - Splines} |
299 Cubic Spline of $sin(x)$ |
363 Cubic Spline of $sin(x)$ |
300 \begin{lstlisting} |
364 \begin{lstlisting} |
301 x = np.arange(0,2*np.pi,np.pi/8) |
365 x = np.arange(0,2*np.pi,np.pi/4) |
302 y = np.sin(x) |
366 y = np.sin(x) |
303 t = interpolate.splrep(x,y,s=0) |
367 tck = interpolate.splrep(x,y) |
304 X = np.arange(0,2*np.pi,np.pi/50) |
368 X = np.arange(0,2*np.pi,np.pi/50) |
305 Y = interpolate.splev(X,t,der=0) |
369 Y = interpolate.splev(X,tck,der=0) |
306 |
|
307 plt.plot(x,y,'o',x,y,X,Y) |
370 plt.plot(x,y,'o',x,y,X,Y) |
308 plt.show() |
371 plt.show() |
309 \end{lstlisting} |
372 \end{lstlisting} |
310 \inctime{10} |
373 \inctime{10} |
311 \end{frame} |
374 \end{frame} |