diff options
Diffstat (limited to 'day2')
-rw-r--r-- | day2/handout.tex | 444 | ||||
-rw-r--r-- | day2/session1.tex | 131 | ||||
-rw-r--r-- | day2/session2.tex | 100 | ||||
-rw-r--r-- | day2/session3.tex | 8 | ||||
-rw-r--r-- | day2/tda.tex | 13 |
5 files changed, 590 insertions, 106 deletions
diff --git a/day2/handout.tex b/day2/handout.tex new file mode 100644 index 0000000..7d56088 --- /dev/null +++ b/day2/handout.tex @@ -0,0 +1,444 @@ +\documentclass[12pt]{article} +\usepackage{amsmath} +\title{Python Workshop\\Problems and Exercises} +\author{Asokan Pichai\\Prabhu Ramachandran} +\begin{document} +\maketitle + +\section{Matrices and Arrays \& 2D Plotting} +\subsection{Matrices and Arrays} +\subsubsection{Basic Numpy} +\begin{verbatim} +# Simple array math example +>>> import numpy as np +>>> a = np.array([1,2,3,4]) +>>> b = np.arange(2,6) +>>> b +array([2,3,4,5]) +>>> a*2 + b + 1 # Basic math! +array([5, 8, 11, 14]) + +# Pi and e are defined. +>>> x = np.linspace(0.0, 10.0, 1000) +>>> x *= 2*np.pi/10 # inplace. +# apply functions to array. +>>> y = np.sin(x) +>>> z = np.exp(y) + +>>> x = np.array([1., 2, 3, 4]) +>>> np.size(x) +4 +>>> x.dtype # What is a.dtype? +dtype('float64') +>>> x.shape +(4,) +>>> print np.rank(x), x.itemsize +1 8 +>>> x[0] = 10 +>>> print x[0], x[-1] +10.0 4.0 + +>>> a = np.array([[ 0, 1, 2, 3], +... [10,11,12,13]]) +>>> a.shape # (rows, columns) +(2, 4) +# Accessing and setting values +>>> a[1,3] +13 +>>> a[1,3] = -1 +>>> a[1] # The second row +array([10,11,12,-1]) + +>>> b=np.array([[0,2,4,2],[1,2,3,4]]) +>>> np.add(a,b,a) +>>> np.sum(x,axis=1) + +>>> np.greater(a,4) +>>> np.sqrt(a) +\end{verbatim} + +\subsubsection{Array Creation} +\begin{verbatim} +>>> np.array([2,3,4]) +array([2, 3, 4]) + +>>> np.linspace(0, 2, 4) +array([0.,0.6666667,1.3333333,2.]) + +>>>np.ones([2,2]) +array([[ 1., 1.], + [ 1., 1.]]) + +>>>a = np.array([[1,2,3],[4,5,6]]) +>>>np.ones_like(a) +array([[1, 1, 1], + [1, 1, 1]]) +\end{verbatim} +\subsubsection{Slicing, Striding Arrays} +\begin{verbatim} +>>> a = np.array([[1,2,3], [4,5,6], + [7,8,9]]) +>>> a[0,1:3] +array([2, 3]) +>>> a[1:,1:] +array([[5, 6], + [8, 9]]) +>>> a[:,2] +array([3, 6, 9]) +>>> a[...,2] +array([3, 6, 9]) + +>>> a[0::2,0::2] +array([[1, 3], + [7, 9]]) +# Slices are references to the +# same memory! +\end{verbatim} +\subsubsection{Random Numbers} +\begin{verbatim} +>>> np.random.rand(3,2) +array([[ 0.96276665, 0.77174861], + [ 0.35138557, 0.61462271], + [ 0.16789255, 0.43848811]]) +>>> np.random.randint(1,100) +42 +\end{verbatim} + +\subsubsection{Problem Set} +\begin{verbatim} + >>> from scipy import misc + >>> A=misc.imread(name) + >>> misc.imshow(A) +\end{verbatim} +\begin{enumerate} + \item Convert an RGB image to Grayscale. $ Y = 0.5R + 0.25G + 0.25B $. + \item Scale the image to 50\%. + \item Introduce some random noise. + \item Smooth the image using a mean filter. + \\\small{Each element in the array is replaced by mean of all the neighbouring elements} + \\\small{How fast does your code run?} +\end{enumerate} + +\subsection{2D Plotting} +\subsubsection{Basic 2D Plotting} +\begin{verbatim} +$ ipython -pylab +>>> x = linspace(0, 2*pi, 1000) +>>> plot(x, sin(x)) +>>> plot(x, sin(x), 'ro') +>>> xlabel(r'$\chi$', color='g') +# LaTeX markup! +>>> ylabel(r'sin($\chi$)', color='r') +>>> title('Simple figure', fontsize=20) +>>> savefig('/tmp/test.eps') +\end{verbatim} +\subsubsection{Tweaking plots} +\begin{verbatim} +# Set properties of objects: +>>> l, = plot(x, sin(x)) +# Why "l,"? +>>> setp(l, linewidth=2.0, color='r') +>>> l.set_linewidth(2.0) +>>> draw() # Redraw. +>>> setp(l) # Print properties. +>>> clf() # Clear figure. +>>> close() # Close figure. +\end{verbatim} + +\subsubsection{Working with text} +\begin{verbatim} +>>> w = arange(-2,2,.1) +>>> plot(w,exp(-(w*w))*cos) +>>> ylabel('$f(\omega)$') +>>> xlabel('$\omega$') +>>> title(r"$f(\omega)=e^{-\omega^2} + cos({\omega^2})$") +>>> annotate('maxima',xy=(0, 1), + xytext=(1, 0.8), + arrowprops=dict( + facecolor='black', + shrink=0.05)) +\end{verbatim} + +\subsubsection{Legends} +\begin{verbatim} +>>> x = linspace(0, 2*np.pi, 1000) +>>> plot(x, cos(5*x), 'r--', + label='cosine') +>>> plot(x, sin(5*x), 'g--', + label='sine') +>>> legend() +# Or use: +>>> legend(['cosine', 'sine']) +\end{verbatim} + +\subsubsection{Multiple figures} +\begin{verbatim} +>>> figure(1) +>>> plot(x, sin(x)) +>>> figure(2) +>>> plot(x, tanh(x)) +>>> figure(1) +>>> title('Easy as 1,2,3') + +\end{verbatim} + +\subsubsection{Problem Set} + \begin{enumerate} + \item Write a function that plots any regular n-gon given n. + \item Consider the logistic map, $f(x) = kx(1-x)$, plot it for + $k=2.5, 3.5$ and $4$ in the same plot. + + \item Consider the iteration $x_{n+1} = f(x_n)$ where $f(x) = + kx(1-x)$. Plot the successive iterates of this process. + \item Plot this using a cobweb plot as follows: + \begin{enumerate} + \item Start at $(x_0, 0)$ + \item Draw line to $(x_i, f(x_i))$; + \item Set $x_{i+1} = f(x_i)$ + \item Draw line to $(x_i, x_i)$ + \item Repeat from 2 for as long as you want + \end{enumerate} +\end{enumerate} + +\section{Advanced Numpy} + +\subsection{Broadcasting} +\begin{verbatim} +>>> a = np.arange(4) +>>> b = np.arange(5) +>>> a+b #Does this work? +>>> a+3 +>>> c=np.array([3]) +>>> a+c #Works! +>>> b+c #But how? +>>> a.shape, b.shape, c.shape + +>>> a = np.arange(4) +>>> a+3 +array([3, 4, 5, 6]) + +>>> x = np.ones((3, 5)) +>>> y = np.ones(8) +>>> (x[..., None] + y).shape +(3, 5, 8) + +\end{verbatim} + +\subsection{Copies \& Views} +\begin{verbatim} +>>> a = np.array([[1,2,3],[4,5,6]]) +>>> b = a +>>> b is a +>>> b[0,0]=0; print a +>>> c = a.view() +>>> c is a +>>> c.base is a +>>> c.flags.owndata +>>> d = a.copy() +>>> d.base is a +>>> d.flags.owndata + +>>> a = np.arange(1,9) +>>> a.shape=3,3 +>>> b = a[0,1:3] +>>> c = a[0::2,0::2] +>>> a.flags.owndata +>>> b.flags.owndata +>>> b.base +>>> c.base is a + +>>> b = a[np.array([0,1,2])] +array([[1, 2, 3], + [4, 5, 6], + [7, 8, 9]]) +>>> b.flags.owndata +>>> abool=np.greater(a,2) +>>> c = a[abool] +>>> c.flags.owndata + +\end{verbatim} + +\section{Scipy} +\subsection{Linear Algebra} +\begin{verbatim} +>>> import scipy as sp +>>> from scipy import linalg +>>> A=sp.mat(np.arange(1,10)) +>>> A.shape=3,3 +>>> linalg.inv(A) +>>> linalg.det(A) +>>> linalg.norm(A) +>>> linalg.expm(A) #logm +>>> linalg.sinm(A) #cosm, tanm, ... + +>>> A = sp.mat(np.arange(1,10)) +>>> A.shape=3,3 +>>> linalg.lu(A) +>>> linalg.eig(A) +>>> linalg.eigvals(A) +\end{verbatim} + +\subsection{Solving Linear Equations} + +\begin{align*} + 3x + 2y - z & = 1 \\ + 2x - 2y + 4z & = -2 \\ + -x + \frac{1}{2}y -z & = 0 +\end{align*} +To Solve this, +\begin{verbatim} +>>> A = sp.mat([[3,2,-1],[2,-2,4] + ,[-1,1/2,-1]]) +>>> B = sp.mat([[1],[-2],[0]]) +>>> linalg.solve(A,B) +\end{verbatim} + +\subsection{Integrate} +\subsubsection{Quadrature} +Calculate the area under $(sin(x) + x^2)$ in the range $(0,1)$ +\begin{verbatim} +>>> def f(x): + return np.sin(x)+x**2 +>>> integrate.quad(f, 0, 1) +\end{verbatim} + +\subsubsection{ODE Integration} +Numerically solve ODEs\\ +\begin{align*} +\frac{dx}{dt} &=-e^{-t}x^2\\ + x(0) &=2 +\end{align*} +\begin{verbatim} +>>> def dx_dt(x,t): +... return -np.exp(-t)*x**2 +>>> x=integrate.odeint(dx_dt, 2, t) +>>> plt.plot(x,t) +\end{verbatim} + +\subsection{Interpolation} +\subsubsection{1D Interpolation} +\begin{verbatim} +>>> from scipy import interpolate +>>> interpolate.interp1d? +>>> x = np.arange(0,2*np.pi,np.pi/4) +>>> y = np.sin(x) +>>> fl = interpolate.interp1d( + x,y,kind='linear') +>>> fc = interpolate.interp1d( + x,y,kind='cubic') +>>> fl(np.pi/3) +>>> fc(np.pi/3) +\end{verbatim} + +\subsubsection{Splines} +Plot the Cubic Spline of $sin(x)$ +\begin{verbatim} +>>> x = np.arange(0,2*np.pi,np.pi/4) +>>> y = np.sin(x) +>>> tck = interpolate.splrep(x,y) +>>> X = np.arange(0,2*np.pi,np.pi/50) +>>> Y = interpolate.splev(X,tck,der=0) +>>> plt.plot(x,y,'o',x,y,X,Y) +>>> plt.show() +\end{verbatim} + +\subsection{Signal \& Image Processing} +Applying a simple median filter +\begin{verbatim} +>>> from scipy import signal, ndimage +>>> from scipy import lena +>>> A=lena().astype('float32') +>>> B=signal.medfilt2d(A) +>>> imshow(B) +\end{verbatim} +Zooming an array - uses spline interpolation +\begin{verbatim} +>>> b=ndimage.zoom(A,0.5) +>>> imshow(b) +\end{verbatim} + +\section{3D Data Visualization} +\subsection{Using mlab} +\begin{verbatim} +>>> from enthought.mayavi import mlab + +>>> mlab.test_<TAB> +>>> mlab.test_contour3d() +>>> mlab.test_contour3d?? +\end{verbatim} + +\subsubsection{Plotting Functions} +\begin{verbatim} +>>> from numpy import * +>>> t = linspace(0, 2*pi, 50) +>>> u = cos(t)*pi +>>> x, y, z = sin(u), cos(u), sin(t) + +>>> x = mgrid[-3:3:100j,-3:3:100j] +>>> z = sin(x*x + y*y) +>>> mlab.surf(x, y, z) +\end{verbatim} + +\subsubsection{Large 2D Data} +\begin{verbatim} +>>> mlab.mesh(x, y, z) + +>>> phi, theta = numpy.mgrid[0:pi:20j, +... 0:2*pi:20j] +>>> x = sin(phi)*cos(theta) +>>> y = sin(phi)*sin(theta) +>>> z = cos(phi) +>>> mlab.mesh(x, y, z, +... representation= +... 'wireframe') +\end{verbatim} + +\subsubsection{Large 3D Data} +\begin{verbatim} +>>> x, y, z = ogrid[-5:5:64j, +... -5:5:64j, +... -5:5:64j] +>>> mlab.contour3d(x*x*0.5 + y*y + + z*z*2) + +>>> mlab.test_quiver3d() +\end{verbatim} + +\subsection{Motivational Problem} +Atmospheric data of temperature over the surface of the earth. Let temperature ($T$) vary linearly with height ($z$)\\ +$T = 288.15 - 6.5z$ + +\begin{verbatim} +lat = linspace(-89, 89, 37) +lon = linspace(0, 360, 37) +z = linspace(0, 100, 11) +\end{verbatim} + +\begin{verbatim} +x, y, z = mgrid[0:360:37j,-89:89:37j, + 0:100:11j] +t = 288.15 - 6.5*z +mlab.contour3d(x, y, z, t) +mlab.outline() +mlab.colorbar() +\end{verbatim} + +\subsection{Lorenz equation} +\begin{eqnarray*} +\frac{d x}{dt} &=& s (y-x)\\ +\frac{d y}{d t} &=& rx -y -xz\\ +\frac{d z}{d t} &=& xy - bz\\ +\end{eqnarray*} + +Let $s=10,$ +$r=28,$ +$b=8./3.$ + +\begin{verbatim} +x, y, z = mgrid[-50:50:20j,-50:50:20j, + -10:60:20j] + +\end{verbatim} + +\end{document} diff --git a/day2/session1.tex b/day2/session1.tex index 53ee07a..357f18c 100644 --- a/day2/session1.tex +++ b/day2/session1.tex @@ -134,10 +134,8 @@ \item Why? \item What: \begin{itemize} - \item An efficient and powerful array type for various common data - types - \item Abstracts out the most commonly used standard operations on - arrays + \item An efficient and powerful array type for various common data types + \item Abstracts out the most commonly used standard operations on arrays \end{itemize} \end{itemize} \end{frame} @@ -146,16 +144,25 @@ \frametitle{Examples of \num} \begin{lstlisting} # Simple array math example ->>> from numpy import * ->>> a = array([1,2,3,4]) ->>> b = array([2,3,4,5]) +>>> import numpy as np +>>> a = np.array([1,2,3,4]) +>>> b = np.arange(2,6) +>>> b +array([2,3,4,5]) >>> a*2 + b + 1 # Basic math! array([5, 8, 11, 14]) +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Examples of \num} +\begin{lstlisting} # Pi and e are defined. ->>> x = linspace(0.0, 10.0, 1000) ->>> x *= 2*pi/10 # inplace. +>>> x = np.linspace(0.0, 10.0, 1000) +>>> x *= 2*np.pi/10 # inplace. # apply functions to array. ->>> y = sin(x) +>>> y = np.sin(x) +>>> z = np.exp(y) \end{lstlisting} \inctime{5} \end{frame} @@ -178,14 +185,14 @@ array([5, 8, 11, 14]) \frametitle{More examples of \num} \vspace*{-8pt} \begin{lstlisting} ->>> x = array([1., 2, 3, 4]) ->>> size(x) +>>> x = np.array([1., 2, 3, 4]) +>>> np.size(x) 4 >>> x.dtype # What is a.dtype? dtype('float64') >>> x.shape (4,) ->>> print rank(x), x.itemsize +>>> print np.rank(x), x.itemsize 1 8 >>> x[0] = 10 >>> print x[0], x[-1] @@ -196,7 +203,7 @@ dtype('float64') \begin{frame}[fragile] \frametitle{Multi-dimensional arrays} \begin{lstlisting} ->>> a = array([[ 0, 1, 2, 3], +>>> a = np.array([[ 0, 1, 2, 3], ... [10,11,12,13]]) >>> a.shape # (rows, columns) (2, 4) @@ -206,78 +213,85 @@ dtype('float64') >>> a[1,3] = -1 >>> a[1] # The second row array([10,11,12,-1]) - \end{lstlisting} \end{frame} + \begin{frame}[fragile] \frametitle{Array math} \begin{itemize} \item Basic \alert{elementwise} math (given two arrays \typ{a, b}): \typ{+, -, *, /, \%} - \item Inplace operators: \typ{a += b}, or \typ{add(a, b, a)} etc. - \item \typ{sum(x, axis=0)}, - \typ{product(x, axis=0)}, - \typ{dot(a, bp)} + \item Inplace operators: \typ{a += b}, or \typ{np.add(a, b, a)} etc. + \item \typ{np.sum(x, axis=0)}, + \typ{np.product(x, axis=0)}, + \typ{np.dot(a, bp)} \end{itemize} +\begin{lstlisting} +>>> b=np.array([[0,2,4,2],[1,2,3,4]]) +>>> np.add(a,b,a) +>>> np.sum(x,axis=1) +\end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Array math cont.} \begin{itemize} - \item Logical operations: \typ{equal (==)}, \typ{not\_equal (!=)}, - \typ{less (<)}, \typ{greater (>)} etc. - \item Trig and other functions: \typ{sin(x),} - \typ{arcsin(x), sinh(x),} - \typ{exp(x), sqrt(x)} etc. + \item Logical operations: \typ{np.equal (==)}, \typ{np.not\_equal (!=)}, + \typ{np.less (<)}, \typ{np.greater (>)} etc. + \item Trig and other functions: \typ{np.sin(x),} + \typ{np.arcsin(x), np.sinh(x),} + \typ{np.exp(x), np.sqrt(x)} etc. \end{itemize} +\begin{lstlisting} +>>> np.greater(a,4) +>>> np.sqrt(a) +\end{lstlisting} \inctime{10} \end{frame} \subsection{Array Creation \& Slicing, Striding Arrays} \begin{frame}[fragile] \frametitle{Array creation functions} - \begin {block}{\typ{array(object, dtype=None, ...)}} - \begin{lstlisting} - >>> array( [2,3,4] ) - array([2, 3, 4]) - \end{lstlisting} - \end {block} - \begin{block}{\typ{linspace(start, stop, num=50, ...)}} - \begin{lstlisting} - >>> linspace( 0, 2, 4 ) - array([0.,0.6666667,1.3333333,2.]) - \end{lstlisting} - \end{block} \begin{itemize} - \item also try \typ{arange} command + \item {\typ{np.array(object,dtype=None,...)} + \begin{lstlisting} +>>> np.array([2,3,4]) +array([2, 3, 4]) + \end{lstlisting} + \item \typ{np.linspace(start,stop,...)} + \begin{lstlisting} +>>> np.linspace(0, 2, 4) +array([0.,0.6666667,1.3333333,2.]) + \end{lstlisting} + \item Also try \typ{np.arange} \end{itemize} \end{frame} \begin{frame}[fragile] \frametitle{Array creation functions cont.} \begin{itemize} - \item \typ{ones(shape, dtype=None, ...)} + \item \typ{np.ones(shape, dtype=None, ...)} \begin{lstlisting} - >>>ones([2,2]) - array([[ 1., 1.], - [ 1., 1.]]) +>>>np.ones([2,2]) +array([[ 1., 1.], + [ 1., 1.]]) \end{lstlisting} - \item \typ{identity(n)} - \item \typ{ones\_like(x)} + \item \typ{np.identity(n)} + \item \typ{np.ones\_like(x)} \begin{lstlisting} - >>>a = array([[1,2,3],[4,5,6]]) - >>>ones_like(a) - array([[1, 1, 1], - [1, 1, 1]]) +>>>a = np.array([[1,2,3],[4,5,6]]) +>>>np.ones_like(a) +array([[1, 1, 1], + [1, 1, 1]]) \end{lstlisting} - \item check out \typ{zeros, zeros\_like, empty} + \item Also try \typ{zeros, zeros\_like, empty} \end{itemize} \end{frame} \begin{frame}[fragile] \frametitle{Slicing arrays} \begin{lstlisting} ->>> a = array([[1,2,3], [4,5,6], +>>> a = np.array([[1,2,3], [4,5,6], [7,8,9]]) >>> a[0,1:3] array([2, 3]) @@ -327,8 +341,8 @@ array([[ 0.96276665, 0.77174861], \item Scale the image to 50\% \item Introduce some random noise \item Smooth the image using a mean filter - \\\small{Take the mean of all the neighbouring elements} - \\\small{How fast can you do it?} + \\\small{Each element in the array is replaced by mean of all the neighbouring elements} + \\\small{How fast does your code run?} \end{enumerate} \inctime{15} \end{frame} @@ -408,7 +422,7 @@ array([[ 0.96276665, 0.77174861], \begin{frame}[fragile] \frametitle{Legends} \begin{lstlisting} ->>> x = linspace(0, 2*pi, 1000) +>>> x = linspace(0, 2*np.pi, 1000) >>> plot(x, cos(5*x), 'r--', label='cosine') >>> plot(x, sin(5*x), 'g--', @@ -435,7 +449,7 @@ array([[ 0.96276665, 0.77174861], \end{frame} \begin{frame}[fragile] - \frametitle{Note: \typ{pylab} in Python scripts} + \frametitle{\typ{pylab} in Python scripts} \begin{lstlisting} import pylab x = pylab.linspace(0, 20, 1000) @@ -444,7 +458,7 @@ pylab.plot(x, pylab.sin(x)) # Can also use: from pylab import linspace, sin, plot \end{lstlisting} -\inctime{5} +\inctime{10} \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -742,7 +756,6 @@ title('triangular head; scale '\ \tiny For details see \url{http://matplotlib.sourceforge.net/screenshots/plotmap.py} \end{center} -\inctime{5} \end{frame} @@ -753,6 +766,7 @@ title('triangular head; scale '\ \item \url{http://matplotlib.sf.net/tutorial.html} \item \url{http://matplotlib.sf.net/screenshots.html} \end{itemize} +\inctime{5} \end{frame} \begin{frame} @@ -787,4 +801,11 @@ title('triangular head; scale '\ \end{columns} \inctime{20} \end{frame} +\begin{frame}{Summary} + \begin{itemize} + \item Basics of Numpy. + \item Array operations. + \item Plotting in 2D. + \end{itemize} +\end{frame} \end{document} diff --git a/day2/session2.tex b/day2/session2.tex index 07514f7..0f471f5 100644 --- a/day2/session2.tex +++ b/day2/session2.tex @@ -116,17 +116,20 @@ \begin{frame} \maketitle \end{frame} + +\section{Advanced Numpy} \begin{frame}[fragile] \frametitle{Broadcasting} Try it! \begin{lstlisting} >>> a = np.arange(4) >>> b = np.arange(5) - >>> a+b + >>> a+b #Does this work? >>> a+3 >>> c=np.array([3]) - >>> a+c - >>> b+c + >>> a+c #Works! + >>> b+c #But how? + >>> a.shape, b.shape, c.shape \end{lstlisting} \begin{itemize} \item Enter Broadcasting! @@ -147,7 +150,7 @@ \includegraphics[height=0.7in, interpolate=true]{data/broadcast_scalar} \end{columns} \begin{itemize} - \item Allows functions to take inputs not of the same shape + \item Allows functions to take inputs that are not of the same shape \item 2 rules - \begin{enumerate} \item 1 is (repeatedly) prepended to shapes of smaller arrays @@ -174,8 +177,9 @@ \begin{frame}[fragile] \frametitle{Copies \& Views} Try it! + \vspace{-0.1in} \begin{lstlisting} - >>> a = np.array([[1,2,3],[4,5,6]]) + >>> a = np.arange(1,9); a.shape=3,3 >>> b = a >>> b is a >>> b[0,0]=0; print a @@ -192,9 +196,8 @@ \begin{frame}[fragile] \frametitle{Copies \& Views} Try it! + \vspace{-0.1in} \begin{lstlisting} - >>> a = np.arange(1,9) - >>> a.shape=3,3 >>> b = a[0,1:3] >>> c = a[0::2,0::2] >>> a.flags.owndata @@ -226,6 +229,8 @@ \inctime{15} \end{frame} +\section{SciPy} +\subsection{Introduction} \begin{frame} {Intro to SciPy} \begin{itemize} @@ -266,6 +271,7 @@ \end{lstlisting} \end{frame} +\subsection{Linear Algebra} \begin{frame}[fragile] \frametitle{Linear Algebra} Try it! @@ -311,6 +317,7 @@ \inctime{15} \end{frame} +\subsection{Integration} \begin{frame}[fragile] \frametitle{Integrate} \begin{itemize} @@ -318,7 +325,7 @@ \item Integrating Functions given fixed samples \item Numerical integrators of ODE systems \end{itemize} - Calculate $\int^1_0(sin(x) + x^2)dx$ + Calculate the area under $(sin(x) + x^2)$ in the range $(0,1)$ \begin{lstlisting} >>> def f(x): return np.sin(x)+x**2 @@ -331,48 +338,50 @@ Numerically solve ODEs\\ \begin{align*} \frac{dx}{dt}&=-e^{-t}x^2\\ - x(0)&=2 + x&=2 \quad at \ t=0 \end{align*} \begin{lstlisting} - def dx_dt(x,t): +>>> def dx_dt(x,t): return -np.exp(-t)*x**2 - - x=integrate.odeint(dx_dt, 2, t) - plt.plot(x,t) +>>> t=np.linspace(0,2,100) +>>> x=integrate.odeint(dx_dt, 2, t) +>>> plt.plot(x,t) \end{lstlisting} \inctime{10} \end{frame} +\subsection{Interpolation} \begin{frame}[fragile] \frametitle{Interpolation} Try it! \begin{lstlisting} - >>> from scipy import interpolate - >>> interpolate.interp1d? - >>> x = np.arange(0,2*np.pi,np.pi/4) - >>> y = np.sin(x) - >>> fl = interpolate.interp1d(x,y,kind='linear') - >>> fc = interpolate.interp1d(x,y,kind='cubic') - >>> fl(np.pi/3) - >>> fc(np.pi/3) +>>> from scipy import interpolate +>>> interpolate.interp1d? +>>> x = np.arange(0,2*np.pi,np.pi/4) +>>> y = np.sin(x) +>>> fl = interpolate.interp1d( + x,y,kind='linear') +>>> fc = interpolate.interp1d( + x,y,kind='cubic') +>>> fl(np.pi/3) +>>> fc(np.pi/3) \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Interpolation - Splines} - Cubic Spline of $sin(x)$ + Plot the Cubic Spline of $sin(x)$ \begin{lstlisting} - x = np.arange(0,2*np.pi,np.pi/4) - y = np.sin(x) - tck = interpolate.splrep(x,y) - X = np.arange(0,2*np.pi,np.pi/50) - Y = interpolate.splev(X,tck,der=0) - plt.plot(x,y,'o',x,y,X,Y) - plt.show() +>>> tck = interpolate.splrep(x,y) +>>> X = np.arange(0,2*np.pi,np.pi/50) +>>> Y = interpolate.splev(X,tck,der=0) +>>> plt.plot(x,y,'o',x,y,X,Y) +>>> plt.show() \end{lstlisting} \inctime{10} \end{frame} +\subsection{Signal Processing} \begin{frame}[fragile] \frametitle{Signal \& Image Processing} \begin{itemize} @@ -393,16 +402,16 @@ \frametitle{Signal \& Image Processing} Applying a simple median filter \begin{lstlisting} - from scipy import signal, ndimage - from scipy import lena - A=lena().astype('float32') - B=signal.medfilt2d(A) - imshow(B) +>>> from scipy import signal, ndimage +>>> from scipy import lena +>>> A=lena().astype('float32') +>>> B=signal.medfilt2d(A) +>>> imshow(B) \end{lstlisting} Zooming an array - uses spline interpolation \begin{lstlisting} - b=ndimage.zoom(A,0.5) - imshow(b) +>>> b=ndimage.zoom(A,0.5) +>>> imshow(b) \end{lstlisting} \inctime{5} \end{frame} @@ -413,10 +422,21 @@ \begin{equation*} \frac{d^2x}{dt^2}+\mu(x^2-1)\frac{dx}{dt}+x= 0 \end{equation*} -\inctime{25} + Make a plot of $\frac{dx}{dt}$ vs. $x$. +\inctime{30} +\end{frame} +\begin{frame}{Summary} + \begin{itemize} + \item Advanced NumPy + \item SciPy + \begin{itemize} + \item Linear Algebra + \item Integration + \item Interpolation + \item Signal and Image processing + \end{itemize} + \end{itemize} \end{frame} - - \end{document} - Numpy arrays (30 mins) @@ -424,5 +444,3 @@ - random number generation. - Image manipulation: jigsaw puzzle. - Monte-carlo integration. - - diff --git a/day2/session3.tex b/day2/session3.tex index 02caa0b..b1221e5 100644 --- a/day2/session3.tex +++ b/day2/session3.tex @@ -192,9 +192,9 @@ \inctime{10} \end{frame} -\section{Tools at your disposal:} +\section{Tools at your disposal} -\subsection{Mayavi2.0} +\subsection{Mayavi2} \begin{frame} \frametitle{Introduction to Mayavi} @@ -280,7 +280,7 @@ \end{frame} \begin{frame}[fragile] - \frametitle{Using mlab:} + \frametitle{Using mlab} \begin{lstlisting} >>> from enthought.mayavi import mlab @@ -288,7 +288,7 @@ \vspace*{0.5in} - \myemph{\Large Try these:} + \myemph{\Large Try these} \vspace*{0.25in} diff --git a/day2/tda.tex b/day2/tda.tex index ae39f4a..664eed3 100644 --- a/day2/tda.tex +++ b/day2/tda.tex @@ -261,7 +261,7 @@ def test_function_ignore_cases_words(): \begin{frame}[fragile] \frametitle{Exercise} - Based on Euclid's theorem: + Based on Euclid's algorithm: $gcd(a,b)=gcd(b,b\%a)$\\ gcd function can be written as: \begin{lstlisting} @@ -269,14 +269,15 @@ def test_function_ignore_cases_words(): if a%b == 0: return b return gcd(b, a%b) \end{lstlisting} + \vspace*{-0.15in} \begin{block}{Task} - For given gcd implementation write - at least two tests. - \end{block} - \begin{block}{Task} - Write a non recursive implementation + \begin{itemize} + \item Write at least + two tests for above mentioned function. + \item Write a non recursive implementation of gcd(), and test it using already written tests. + \end{itemize} \end{block} \inctime{15} |