156 |
157 |
157 \begin{frame}[fragile] |
158 \begin{frame}[fragile] |
158 \frametitle{Examples of \num} |
159 \frametitle{Examples of \num} |
159 \begin{lstlisting} |
160 \begin{lstlisting} |
160 # Pi and e are defined. |
161 # Pi and e are defined. |
161 >>> x = np.linspace(0.0, 10.0, 1000) |
162 >>> x = linspace(0.0, 10.0, 1000) |
162 >>> x *= 2*np.pi/10 # inplace. |
163 >>> x *= 2*pi/10 # inplace. |
163 # apply functions to array. |
164 # apply functions to array. |
164 >>> y = np.sin(x) |
165 >>> y = sin(x) |
165 >>> z = np.exp(y) |
166 >>> z = exp(y) |
166 \end{lstlisting} |
167 \end{lstlisting} |
167 \inctime{5} |
168 \inctime{5} |
168 \end{frame} |
169 \end{frame} |
169 |
170 |
|
171 \begin{frame}[fragile] |
|
172 \frametitle{More examples of \num} |
|
173 \vspace*{-8pt} |
|
174 \begin{lstlisting} |
|
175 >>> x = array([1., 2, 3, 4]) |
|
176 >>> size(x) |
|
177 4 |
|
178 >>> x.dtype # What is a.dtype? |
|
179 dtype('float64') |
|
180 >>> x.shape |
|
181 (4,) |
|
182 >>> print x.itemsize |
|
183 8 |
|
184 >>> x[0] = 10 |
|
185 >>> print x[0], x[-1] |
|
186 10.0 4.0 |
|
187 \end{lstlisting} |
|
188 \end{frame} |
|
189 |
|
190 \begin{frame}[fragile] |
|
191 \frametitle{Multi-dimensional arrays} |
|
192 \begin{lstlisting} |
|
193 >>> a = array([[ 0, 1, 2, 3], |
|
194 ... [10,11,12,13]]) |
|
195 >>> a.shape # (rows, columns) |
|
196 (2, 4) |
|
197 >>> a.shape=4,2 |
|
198 # Accessing and setting values |
|
199 >>> a[1][3] |
|
200 >>> a[1,3] |
|
201 >>> a[1,3] = -1 |
|
202 >>> a[1] # The second row |
|
203 \end{lstlisting} |
|
204 \end{frame} |
|
205 |
|
206 \begin{frame}[fragile] |
|
207 \frametitle{Array math} |
|
208 \begin{itemize} |
|
209 \item Basic \alert{elementwise} math (given two arrays \typ{a, b}): |
|
210 \typ{+, -, *, /, \%} |
|
211 \item Inplace operators: \typ{a += b} |
|
212 \item \typ{sum(x, axis=0)}, |
|
213 \typ{product(x, axis=0)}, |
|
214 \typ{dot(a, bp)} |
|
215 \end{itemize} |
|
216 \begin{lstlisting} |
|
217 >>> x = array([[0,2,4,2],[1,2,3,4]]) |
|
218 >>> sum(x) |
|
219 >>> sum(x,axis=1) |
|
220 \end{lstlisting} |
|
221 \end{frame} |
|
222 |
|
223 \begin{frame}[fragile] |
|
224 \frametitle{Array math cont.} |
|
225 \begin{itemize} |
|
226 \item Logical operations: \typ{==}, \typ{!=}, |
|
227 \typ{<}, \typ{>} etc. |
|
228 \item Trig and other functions: \typ{sin(x),} |
|
229 \typ{arcsin(x), sinh(x),} |
|
230 \typ{exp(x), sqrt(x)} etc. |
|
231 \end{itemize} |
|
232 \begin{lstlisting} |
|
233 >>> a<4, a!=3 |
|
234 >>> sqrt(a) |
|
235 \end{lstlisting} |
|
236 \inctime{10} |
|
237 \end{frame} |
|
238 |
170 \begin{frame} |
239 \begin{frame} |
171 \frametitle{Basic concepts} |
240 \frametitle{Summary of Concepts} |
172 \begin{itemize} |
241 \begin{itemize} |
173 \item fixed size (\typ{arr.size}); |
242 \item fixed size (\typ{arr.size}); |
174 \item Same type (\typ{arr.dtype}) of data |
243 \item Same type (\typ{arr.dtype}) of data |
175 \item arbitrary dimensionality |
244 \item arbitrary dimensionality |
176 \item \typ{arr.shape}: size in each dimension |
245 \item \typ{arr.shape}: size in each dimension |
178 \item \alert{Note:} By default array operations are performed |
247 \item \alert{Note:} By default array operations are performed |
179 \alert{elementwise} |
248 \alert{elementwise} |
180 \end{itemize} |
249 \end{itemize} |
181 \end{frame} |
250 \end{frame} |
182 |
251 |
183 |
|
184 \begin{frame}[fragile] |
|
185 \frametitle{More examples of \num} |
|
186 \vspace*{-8pt} |
|
187 \begin{lstlisting} |
|
188 >>> x = np.array([1., 2, 3, 4]) |
|
189 >>> np.size(x) |
|
190 4 |
|
191 >>> x.dtype # What is a.dtype? |
|
192 dtype('float64') |
|
193 >>> x.shape |
|
194 (4,) |
|
195 >>> print np.rank(x), x.itemsize |
|
196 1 8 |
|
197 >>> x[0] = 10 |
|
198 >>> print x[0], x[-1] |
|
199 10.0 4.0 |
|
200 \end{lstlisting} |
|
201 \end{frame} |
|
202 |
|
203 \begin{frame}[fragile] |
|
204 \frametitle{Multi-dimensional arrays} |
|
205 \begin{lstlisting} |
|
206 >>> a = np.array([[ 0, 1, 2, 3], |
|
207 ... [10,11,12,13]]) |
|
208 >>> a.shape # (rows, columns) |
|
209 (2, 4) |
|
210 # Accessing and setting values |
|
211 >>> a[1,3] |
|
212 13 |
|
213 >>> a[1,3] = -1 |
|
214 >>> a[1] # The second row |
|
215 array([10,11,12,-1]) |
|
216 \end{lstlisting} |
|
217 \end{frame} |
|
218 |
|
219 \begin{frame}[fragile] |
|
220 \frametitle{Array math} |
|
221 \begin{itemize} |
|
222 \item Basic \alert{elementwise} math (given two arrays \typ{a, b}): |
|
223 \typ{+, -, *, /, \%} |
|
224 \item Inplace operators: \typ{a += b}, or \typ{np.add(a, b, a)} etc. |
|
225 \item \typ{np.sum(x, axis=0)}, |
|
226 \typ{np.product(x, axis=0)}, |
|
227 \typ{np.dot(a, bp)} |
|
228 \end{itemize} |
|
229 \begin{lstlisting} |
|
230 >>> b=np.array([[0,2,4,2],[1,2,3,4]]) |
|
231 >>> np.add(a,b,a) |
|
232 >>> np.sum(x,axis=1) |
|
233 \end{lstlisting} |
|
234 \end{frame} |
|
235 |
|
236 \begin{frame}[fragile] |
|
237 \frametitle{Array math cont.} |
|
238 \begin{itemize} |
|
239 \item Logical operations: \typ{np.equal (==)}, \typ{np.not\_equal (!=)}, |
|
240 \typ{np.less (<)}, \typ{np.greater (>)} etc. |
|
241 \item Trig and other functions: \typ{np.sin(x),} |
|
242 \typ{np.arcsin(x), np.sinh(x),} |
|
243 \typ{np.exp(x), np.sqrt(x)} etc. |
|
244 \end{itemize} |
|
245 \begin{lstlisting} |
|
246 >>> np.greater(a,4) |
|
247 >>> np.sqrt(a) |
|
248 \end{lstlisting} |
|
249 \inctime{10} |
|
250 \end{frame} |
|
251 |
|
252 \subsection{Array Creation \& Slicing, Striding Arrays} |
252 \subsection{Array Creation \& Slicing, Striding Arrays} |
|
253 |
253 \begin{frame}[fragile] |
254 \begin{frame}[fragile] |
254 \frametitle{Array creation functions} |
255 \frametitle{Array creation functions} |
255 \begin{itemize} |
256 \begin{itemize} |
256 \item {\typ{np.array(object,dtype=None,...)} |
257 \item \typ{array?} \alert{\#Doc string reading} |
|
258 \item \typ{array(object,dtype=None,...)} |
257 \begin{lstlisting} |
259 \begin{lstlisting} |
258 >>> np.array([2,3,4]) |
260 >>> array([2,3,4]) |
259 array([2, 3, 4]) |
261 array([2, 3, 4]) |
260 \end{lstlisting} |
262 \end{lstlisting} |
261 \item \typ{np.linspace(start,stop,...)} |
263 \item \typ{linspace(start,stop,num)} |
262 \begin{lstlisting} |
264 \begin{lstlisting} |
263 >>> np.linspace(0, 2, 4) |
265 >>> linspace(0, 2, 4) |
264 array([0.,0.6666667,1.3333333,2.]) |
266 array([0.,0.6666667,1.3333333,2.]) |
265 \end{lstlisting} |
267 \end{lstlisting} |
266 \item Also try \typ{np.arange} |
268 \item \typ{arange?} |
|
269 \alert{\# float version of range} |
267 \end{itemize} |
270 \end{itemize} |
268 \end{frame} |
271 \end{frame} |
269 |
272 |
270 \begin{frame}[fragile] |
273 \begin{frame}[fragile] |
271 \frametitle{Array creation functions cont.} |
274 \frametitle{Array creation functions cont.} |
272 \begin{itemize} |
275 \begin{itemize} |
273 \item \typ{np.ones(shape, dtype=None, ...)} |
276 \item \typ{ones(shape, dtype=None, ...)} |
274 \begin{lstlisting} |
277 \begin{lstlisting} |
275 >>>np.ones([2,2]) |
278 >>> ones((2,2)) |
276 array([[ 1., 1.], |
279 array([[ 1., 1.], |
277 [ 1., 1.]]) |
280 [ 1., 1.]]) |
278 \end{lstlisting} |
281 \end{lstlisting} |
279 \item \typ{np.identity(n)} |
282 \item \typ{identity(n)} |
280 \item \typ{np.ones\_like(x)} |
283 \item \typ{ones\_like(x)} |
281 \begin{lstlisting} |
284 \begin{lstlisting} |
282 >>>a = np.array([[1,2,3],[4,5,6]]) |
285 >>> a = array([[1,2,3],[4,5,6]]) |
283 >>>np.ones_like(a) |
286 >>> ones_like(a) |
284 array([[1, 1, 1], |
287 array([[1, 1, 1], |
285 [1, 1, 1]]) |
288 [1, 1, 1]]) |
286 \end{lstlisting} |
289 \end{lstlisting} |
287 \item Also try \typ{zeros, zeros\_like, empty} |
290 \item Also try \typ{zeros, zeros\_like, empty} |
288 \end{itemize} |
291 \end{itemize} |
289 \end{frame} |
292 \end{frame} |
290 |
293 |
291 \begin{frame}[fragile] |
294 \begin{frame}[fragile] |
292 \frametitle{Slicing arrays} |
295 \frametitle{Slicing arrays} |
293 \begin{lstlisting} |
296 \begin{lstlisting} |
294 >>> a = np.array([[1,2,3], [4,5,6], |
297 >>> a = array([[1,2,3], [4,5,6], |
295 [7,8,9]]) |
298 [7,8,9]]) |
296 >>> a[0,1:3] |
299 >>> a[0,1:3] |
297 array([2, 3]) |
300 array([2, 3]) |
298 >>> a[1:,1:] |
301 >>> a[1:,1:] |
299 array([[5, 6], |
302 array([[5, 6], |
300 [8, 9]]) |
303 [8, 9]]) |
301 >>> a[:,2] |
304 >>> a[:,2] |
302 array([3, 6, 9]) |
305 array([3, 6, 9]) |
303 >>> a[...,2] |
|
304 array([3, 6, 9]) |
|
305 \end{lstlisting} |
306 \end{lstlisting} |
306 \end{frame} |
307 \end{frame} |
307 |
308 |
308 \begin{frame}[fragile] |
309 \begin{frame}[fragile] |
309 \frametitle{Striding arrays} |
310 \frametitle{Striding arrays} |
317 \end{frame} |
318 \end{frame} |
318 |
319 |
319 \begin{frame}[fragile] |
320 \begin{frame}[fragile] |
320 \frametitle{Random Numbers} |
321 \frametitle{Random Numbers} |
321 \begin{lstlisting} |
322 \begin{lstlisting} |
322 >>> np.random.rand(3,2) |
323 >>> random.random() |
|
324 0.94134734326214331 |
|
325 >>> random.random(2) |
|
326 array([ 0.73955352, 0.49463645]) |
|
327 >>> random.random(3,2) |
323 array([[ 0.96276665, 0.77174861], |
328 array([[ 0.96276665, 0.77174861], |
324 [ 0.35138557, 0.61462271], |
329 [ 0.35138557, 0.61462271], |
325 [ 0.16789255, 0.43848811]]) |
330 [ 0.16789255, 0.43848811]]) |
326 >>> np.random.randint(1,100) |
|
327 42 |
|
328 \end{lstlisting} |
331 \end{lstlisting} |
329 \inctime{15} |
332 \inctime{15} |
330 \end{frame} |
333 \end{frame} |
331 |
334 |
332 \begin{frame}[fragile] |
335 \begin{frame}[fragile] |
333 \frametitle{Problem Set} |
336 \frametitle{Problem} |
|
337 Finite difference |
|
338 \begin{equation*} |
|
339 \frac{sin(x+h)-sin(x)}{h} |
|
340 \end{equation*} |
|
341 \begin{lstlisting} |
|
342 >>> x = linspace(0,2*pi,100) |
|
343 >>> y = sin(x) |
|
344 >>> deltax = x[1]-x[0] |
|
345 \end{lstlisting} |
|
346 \pause |
|
347 \begin{enumerate} |
|
348 \item Given this, get the finite difference of sin in the range 0 to 2*pi |
|
349 \end{enumerate} |
|
350 \end{frame} |
|
351 |
|
352 |
|
353 \begin{frame}[fragile] |
|
354 \frametitle{Advanced Problem} |
334 \begin{lstlisting} |
355 \begin{lstlisting} |
335 >>> from scipy import misc |
356 >>> from scipy import misc |
336 >>> A=misc.imread(name) |
357 >>> A=misc.imread('filename') |
337 >>> misc.imshow(A) |
358 >>> misc.imshow(A) |
338 \end{lstlisting} |
359 \end{lstlisting} |
339 \begin{enumerate} |
360 \begin{enumerate} |
340 \item Convert an RGB image to Grayscale. $ Y = 0.5R + 0.25G + 0.25B $ |
361 \item Convert an RGB image to Grayscale. $ Y = 0.5R + 0.25G + 0.25B $ |
341 \item Scale the image to 50\% |
362 \item Scale the image to 50\% |
372 >>> plot(x, sin(x), 'ro') |
393 >>> plot(x, sin(x), 'ro') |
373 >>> xlabel(r'$\chi$', color='g') |
394 >>> xlabel(r'$\chi$', color='g') |
374 # LaTeX markup! |
395 # LaTeX markup! |
375 >>> ylabel(r'sin($\chi$)', color='r') |
396 >>> ylabel(r'sin($\chi$)', color='r') |
376 >>> title('Simple figure', fontsize=20) |
397 >>> title('Simple figure', fontsize=20) |
377 >>> savefig('/tmp/test.eps') |
398 >>> savefig('/tmp/test.png') |
378 \end{lstlisting} |
399 \end{lstlisting} |
379 \begin{itemize} |
400 \begin{itemize} |
380 \item Also: PNG, PDF, PS, EPS, SVG, PDF |
401 \item Also: PS, EPS, SVG, PDF |
381 \end{itemize} |
402 \end{itemize} |
382 \inctime{5} |
403 \inctime{5} |
383 \end{frame} |
404 \end{frame} |
384 |
405 |
385 \subsection{Plots - Lines, Labels and Legends} |
406 \subsection{Plots - Lines, Labels and Legends} |
408 >>> plot(w,exp(-(w*w))*cos) |
429 >>> plot(w,exp(-(w*w))*cos) |
409 >>> ylabel('$f(\omega)$') |
430 >>> ylabel('$f(\omega)$') |
410 >>> xlabel('$\omega$') |
431 >>> xlabel('$\omega$') |
411 >>> title(r"$f(\omega)=e^{-\omega^2} |
432 >>> title(r"$f(\omega)=e^{-\omega^2} |
412 cos({\omega^2})$") |
433 cos({\omega^2})$") |
413 >>> annotate('maxima',xy=(0, 1), |
|
414 xytext=(1, 0.8), |
|
415 arrowprops=dict( |
|
416 facecolor='black', |
|
417 shrink=0.05)) |
|
418 \end{lstlisting} |
434 \end{lstlisting} |
419 |
435 |
420 \end{frame} |
436 \end{frame} |
421 |
437 |
422 \begin{frame}[fragile] |
438 \begin{frame}[fragile] |
423 \frametitle{Legends} |
439 \frametitle{Legends} |
424 \begin{lstlisting} |
440 \begin{lstlisting} |
425 >>> x = linspace(0, 2*np.pi, 1000) |
441 >>> x = linspace(0, 2*pi, 1000) |
426 >>> plot(x, cos(5*x), 'r--', |
442 >>> plot(x, cos(5*x), 'r--', |
427 label='cosine') |
443 label='cosine') |
428 >>> plot(x, sin(5*x), 'g--', |
444 >>> plot(x, sin(5*x), 'g--', |
429 label='sine') |
445 label='sine') |
430 >>> legend() |
446 >>> legend() |
769 \inctime{5} |
785 \inctime{5} |
770 \end{frame} |
786 \end{frame} |
771 |
787 |
772 \begin{frame} |
788 \begin{frame} |
773 \frametitle{Problem Set} |
789 \frametitle{Problem Set} |
774 \begin{enumerate} |
790 \begin{itemize} |
775 \item Write a function that plots any regular n-gon given \typ{n}. |
791 \item[1] Write a function that plots any regular n-gon given \typ{n}. |
776 \item Consider the logistic map, $f(x) = kx(1-x)$, plot it for |
792 \item[2] Consider the logistic map, $f(x) = kx(1-x)$, plot it for |
777 $k=2.5, 3.5$ and $4$ in the same plot. |
793 $k=2.5, 3.5$ and $4$ in the same plot. |
778 \end{enumerate} |
794 \end{itemize} |
779 \end{frame} |
795 \end{frame} |
780 |
796 |
781 \begin{frame}[fragile] |
797 \begin{frame}[fragile] |
782 \frametitle{Problem Set} |
798 \frametitle{Problem Set} |
783 \begin{columns} |
799 \begin{columns} |
784 \column{0.6\textwidth} |
800 \column{0.6\textwidth} |
785 \small{ |
801 \small{ |
786 \begin{enumerate} |
802 \begin{itemize} |
787 \item Consider the iteration $x_{n+1} = f(x_n)$ where $f(x) = |
803 \item[3] Consider the iteration $x_{n+1} = f(x_n)$ where $f(x) = kx(1-x)$. Plot the successive iterates of this process as explained below. |
788 kx(1-x)$. Plot the successive iterates of this process. |
804 \end{itemize}} |
789 \item Plot this using a cobweb plot as follows: |
|
790 \begin{enumerate} |
|
791 \item Start at $(x_0, 0)$ |
|
792 \item Draw line to $(x_i, f(x_i))$; |
|
793 \item Set $x_{i+1} = f(x_i)$ |
|
794 \item Draw line to $(x_i, x_i)$ |
|
795 \item Repeat from 2 for as long as you want |
|
796 \end{enumerate} |
|
797 \end{enumerate}} |
|
798 \column{0.35\textwidth} |
805 \column{0.35\textwidth} |
799 \hspace*{-0.5in} |
806 \hspace*{-0.5in} |
800 \includegraphics[height=1.6in, interpolate=true]{data/cobweb} |
807 \includegraphics[height=1.6in, interpolate=true]{data/cobweb} |
801 \end{columns} |
808 \end{columns} |
|
809 \end{frame} |
|
810 |
|
811 \begin{frame} |
|
812 |
|
813 Plot the cobweb plot as follows: |
|
814 \begin{enumerate} |
|
815 \item Start at $(x_0, 0)$ ($\implies$ i=0) |
|
816 \item Draw a line to $(x_i, f(x_i))$ |
|
817 \item Set $x_{i+1} = f(x_i)$ |
|
818 \item Draw a line to $(x_{i+1}, x_{i+1})$ |
|
819 \item $(i\implies i+1)$ |
|
820 \item Repeat from 2 for as long as you want |
|
821 \end{enumerate} |
802 \inctime{20} |
822 \inctime{20} |
803 \end{frame} |
823 \end{frame} |
|
824 |
804 \begin{frame}{Summary} |
825 \begin{frame}{Summary} |
805 \begin{itemize} |
826 \begin{itemize} |
806 \item Basics of Numpy. |
827 \item Basics of Numpy. |
807 \item Array operations. |
828 \item Array operations. |
808 \item Plotting in 2D. |
829 \item Plotting in 2D. |