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