--- a/day2/session1.tex Mon Oct 12 16:26:02 2009 +0530
+++ b/day2/session1.tex Mon Oct 12 16:29:11 2009 +0530
@@ -72,9 +72,9 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Title page
-\title[]{Matrices and Arrays\\ \& \\2D Plotting}
+\title[]{Arrays\\ \& \\2D Plotting}
-\author[FOSSEE Team] {Asokan Pichai\\Prabhu Ramachandran}
+\author[FOSSEE Team] {FOSSEE}
\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
\date[] {11, October 2009}
@@ -102,6 +102,9 @@
\end{frame}
}
+\newcommand{\num}{\texttt{numpy}}
+
+
% If you wish to uncover everything in a step-wise fashion, uncomment
% the following command:
%\beamerdefaultoverlayspecification{<+->}
@@ -122,14 +125,12 @@
% You might wish to add the option [pausesections]
\end{frame}
-\section{Matrices and Arrays}
+\section{Arrays}
-\subsection{Basic \typ{numpy} }
-
-\newcommand{\num}{\texttt{numpy}}
+\subsection{Basic \typ{numpy}}
\begin{frame}
- \frametitle{The \num\ module}
+ \frametitle{The \num\ module}
\begin{itemize}
\item Why?
\item What:
@@ -144,9 +145,9 @@
\frametitle{Examples of \num}
\begin{lstlisting}
# Simple array math example
->>> import numpy as np
->>> a = np.array([1,2,3,4])
->>> b = np.arange(2,6)
+>>> from numpy import *
+>>> a = array([1,2,3,4])
+>>> b = arange(2,6)
>>> b
array([2,3,4,5])
>>> a*2 + b + 1 # Basic math!
@@ -158,17 +159,85 @@
\frametitle{Examples of \num}
\begin{lstlisting}
# Pi and e are defined.
->>> x = np.linspace(0.0, 10.0, 1000)
->>> x *= 2*np.pi/10 # inplace.
+>>> x = linspace(0.0, 10.0, 1000)
+>>> x *= 2*pi/10 # inplace.
# apply functions to array.
->>> y = np.sin(x)
->>> z = np.exp(y)
+>>> y = sin(x)
+>>> z = exp(y)
\end{lstlisting}
\inctime{5}
\end{frame}
+\begin{frame}[fragile]
+ \frametitle{More examples of \num}
+\vspace*{-8pt}
+\begin{lstlisting}
+>>> x = array([1., 2, 3, 4])
+>>> size(x)
+4
+>>> x.dtype # What is a.dtype?
+dtype('float64')
+>>> x.shape
+(4,)
+>>> print x.itemsize
+8
+>>> x[0] = 10
+>>> print x[0], x[-1]
+10.0 4.0
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile]
+ \frametitle{Multi-dimensional arrays}
+\begin{lstlisting}
+>>> a = array([[ 0, 1, 2, 3],
+... [10,11,12,13]])
+>>> a.shape # (rows, columns)
+(2, 4)
+>>> a.shape=4,2
+# Accessing and setting values
+>>> a[1][3]
+>>> a[1,3]
+>>> a[1,3] = -1
+>>> a[1] # The second row
+\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}
+ \item \typ{sum(x, axis=0)},
+ \typ{product(x, axis=0)},
+ \typ{dot(a, bp)}
+ \end{itemize}
+\begin{lstlisting}
+>>> x = array([[0,2,4,2],[1,2,3,4]])
+>>> sum(x)
+>>> sum(x,axis=1)
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile]
+ \frametitle{Array math cont.}
+ \begin{itemize}
+ \item Logical operations: \typ{==}, \typ{!=},
+ \typ{<}, \typ{>} etc.
+ \item Trig and other functions: \typ{sin(x),}
+ \typ{arcsin(x), sinh(x),}
+ \typ{exp(x), sqrt(x)} etc.
+ \end{itemize}
+\begin{lstlisting}
+>>> a<4, a!=3
+>>> sqrt(a)
+\end{lstlisting}
+\inctime{10}
+\end{frame}
+
\begin{frame}
- \frametitle{Basic concepts}
+ \frametitle{Summary of Concepts}
\begin{itemize}
\item fixed size (\typ{arr.size});
\item Same type (\typ{arr.dtype}) of data
@@ -180,107 +249,41 @@
\end{itemize}
\end{frame}
-
-\begin{frame}[fragile]
- \frametitle{More examples of \num}
-\vspace*{-8pt}
-\begin{lstlisting}
->>> 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
-\end{lstlisting}
-\end{frame}
+\subsection{Array Creation \& Slicing, Striding Arrays}
\begin{frame}[fragile]
- \frametitle{Multi-dimensional arrays}
-\begin{lstlisting}
->>> 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])
-\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{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{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{itemize}
- \item {\typ{np.array(object,dtype=None,...)}
+ \item \typ{array?} \alert{\#Doc string reading}
+ \item \typ{array(object,dtype=None,...)}
\begin{lstlisting}
->>> np.array([2,3,4])
+>>> array([2,3,4])
array([2, 3, 4])
\end{lstlisting}
- \item \typ{np.linspace(start,stop,...)}
+ \item \typ{linspace(start,stop,num)}
\begin{lstlisting}
->>> np.linspace(0, 2, 4)
+>>> linspace(0, 2, 4)
array([0.,0.6666667,1.3333333,2.])
\end{lstlisting}
- \item Also try \typ{np.arange}
+ \item \typ{arange?}
+ \alert{\# float version of range}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Array creation functions cont.}
\begin{itemize}
- \item \typ{np.ones(shape, dtype=None, ...)}
+ \item \typ{ones(shape, dtype=None, ...)}
\begin{lstlisting}
->>>np.ones([2,2])
+>>> ones((2,2))
array([[ 1., 1.],
[ 1., 1.]])
\end{lstlisting}
- \item \typ{np.identity(n)}
- \item \typ{np.ones\_like(x)}
+ \item \typ{identity(n)}
+ \item \typ{ones\_like(x)}
\begin{lstlisting}
->>>a = np.array([[1,2,3],[4,5,6]])
->>>np.ones_like(a)
+>>> a = array([[1,2,3],[4,5,6]])
+>>> ones_like(a)
array([[1, 1, 1],
[1, 1, 1]])
\end{lstlisting}
@@ -291,7 +294,7 @@
\begin{frame}[fragile]
\frametitle{Slicing arrays}
\begin{lstlisting}
->>> a = np.array([[1,2,3], [4,5,6],
+>>> a = array([[1,2,3], [4,5,6],
[7,8,9]])
>>> a[0,1:3]
array([2, 3])
@@ -300,8 +303,6 @@
[8, 9]])
>>> a[:,2]
array([3, 6, 9])
->>> a[...,2]
-array([3, 6, 9])
\end{lstlisting}
\end{frame}
@@ -319,21 +320,41 @@
\begin{frame}[fragile]
\frametitle{Random Numbers}
\begin{lstlisting}
->>> np.random.rand(3,2)
+>>> random.random()
+0.94134734326214331
+>>> random.random(2)
+array([ 0.73955352, 0.49463645])
+>>> random.random(3,2)
array([[ 0.96276665, 0.77174861],
[ 0.35138557, 0.61462271],
[ 0.16789255, 0.43848811]])
->>> np.random.randint(1,100)
-42
\end{lstlisting}
\inctime{15}
\end{frame}
\begin{frame}[fragile]
- \frametitle{Problem Set}
+ \frametitle{Problem}
+ Finite difference
+ \begin{equation*}
+ \frac{sin(x+h)-sin(x)}{h}
+ \end{equation*}
+ \begin{lstlisting}
+ >>> x = linspace(0,2*pi,100)
+ >>> y = sin(x)
+ >>> deltax = x[1]-x[0]
+ \end{lstlisting}
+ \pause
+ \begin{enumerate}
+ \item Given this, get the finite difference of sin in the range 0 to 2*pi
+ \end{enumerate}
+\end{frame}
+
+
+\begin{frame}[fragile]
+ \frametitle{Advanced Problem}
\begin{lstlisting}
>>> from scipy import misc
- >>> A=misc.imread(name)
+ >>> A=misc.imread('filename')
>>> misc.imshow(A)
\end{lstlisting}
\begin{enumerate}
@@ -353,7 +374,7 @@
\begin{frame}
{IPython's \typ{pylab} mode}
-\begin{block}{Immediate use:}
+\begin{block}{Immediate use -}
\typ{\$ ipython -pylab}
\end{block}
\begin{itemize}
@@ -374,10 +395,10 @@
# LaTeX markup!
>>> ylabel(r'sin($\chi$)', color='r')
>>> title('Simple figure', fontsize=20)
->>> savefig('/tmp/test.eps')
+>>> savefig('/tmp/test.png')
\end{lstlisting}
\begin{itemize}
- \item Also: PNG, PDF, PS, EPS, SVG, PDF
+ \item Also: PS, EPS, SVG, PDF
\end{itemize}
\inctime{5}
\end{frame}
@@ -410,11 +431,6 @@
>>> 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{lstlisting}
\end{frame}
@@ -422,7 +438,7 @@
\begin{frame}[fragile]
\frametitle{Legends}
\begin{lstlisting}
->>> x = linspace(0, 2*np.pi, 1000)
+>>> x = linspace(0, 2*pi, 1000)
>>> plot(x, cos(5*x), 'r--',
label='cosine')
>>> plot(x, sin(5*x), 'g--',
@@ -771,11 +787,11 @@
\begin{frame}
\frametitle{Problem Set}
- \begin{enumerate}
- \item Write a function that plots any regular n-gon given \typ{n}.
- \item Consider the logistic map, $f(x) = kx(1-x)$, plot it for
+ \begin{itemize}
+ \item[1] Write a function that plots any regular n-gon given \typ{n}.
+ \item[2] Consider the logistic map, $f(x) = kx(1-x)$, plot it for
$k=2.5, 3.5$ and $4$ in the same plot.
-\end{enumerate}
+\end{itemize}
\end{frame}
\begin{frame}[fragile]
@@ -783,24 +799,29 @@
\begin{columns}
\column{0.6\textwidth}
\small{
- \begin{enumerate}
- \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}}
+ \begin{itemize}
+ \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.
+ \end{itemize}}
\column{0.35\textwidth}
\hspace*{-0.5in}
\includegraphics[height=1.6in, interpolate=true]{data/cobweb}
\end{columns}
+\end{frame}
+
+\begin{frame}
+
+ Plot the cobweb plot as follows:
+ \begin{enumerate}
+ \item Start at $(x_0, 0)$ ($\implies$ i=0)
+ \item Draw a line to $(x_i, f(x_i))$
+ \item Set $x_{i+1} = f(x_i)$
+ \item Draw a line to $(x_{i+1}, x_{i+1})$
+ \item $(i\implies i+1)$
+ \item Repeat from 2 for as long as you want
+ \end{enumerate}
\inctime{20}
\end{frame}
+
\begin{frame}{Summary}
\begin{itemize}
\item Basics of Numpy.
--- a/day2/session2.tex Mon Oct 12 16:26:02 2009 +0530
+++ b/day2/session2.tex Mon Oct 12 16:29:11 2009 +0530
@@ -75,7 +75,7 @@
% Title page
\title[]{Numerical Computing with Numpy \& Scipy}
-\author[FOSSEE Team] {Asokan Pichai\\Prabhu Ramachandran}
+\author[FOSSEE Team] {FOSSEE}
\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
\date[] {11, October 2009}
@@ -120,13 +120,12 @@
\section{Advanced Numpy}
\begin{frame}[fragile]
\frametitle{Broadcasting}
- Try it!
\begin{lstlisting}
- >>> a = np.arange(4)
- >>> b = np.arange(5)
+ >>> a = arange(4)
+ >>> b = arange(5)
>>> a+b #Does this work?
>>> a+3
- >>> c=np.array([3])
+ >>> c = array([3])
>>> a+c #Works!
>>> b+c #But how?
>>> a.shape, b.shape, c.shape
@@ -142,29 +141,21 @@
\column{0.65\textwidth}
\hspace*{-1.5in}
\begin{lstlisting}
- >>> a = np.arange(4)
+ >>> a = arange(4)
>>> a+3
array([3, 4, 5, 6])
\end{lstlisting}
\column{0.35\textwidth}
\includegraphics[height=0.7in, interpolate=true]{data/broadcast_scalar}
\end{columns}
- \begin{itemize}
- \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
- \item Size 1 in a dimension changed to Largest size in that dimension
- \end{enumerate}
- \end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Broadcasting in 3D}
\begin{lstlisting}
- >>> x = np.ones((3, 5))
- >>> y = np.ones(8)
- >>> (x[..., None] + y).shape
+ >>> x = ones((3, 5, 1))
+ >>> y = ones(8)
+ >>> (x + y).shape
(3, 5, 8)
\end{lstlisting}
\begin{figure}
@@ -176,10 +167,9 @@
\begin{frame}[fragile]
\frametitle{Copies \& Views}
- Try it!
\vspace{-0.1in}
\begin{lstlisting}
- >>> a = np.arange(1,9); a.shape=3,3
+ >>> a = arange(1,9); a.shape=3,3
>>> b = a
>>> b is a
>>> b[0,0]=0; print a
@@ -195,7 +185,6 @@
\begin{frame}[fragile]
\frametitle{Copies \& Views}
- Try it!
\vspace{-0.1in}
\begin{lstlisting}
>>> b = a[0,1:3]
@@ -214,12 +203,10 @@
\begin{frame}[fragile]
\frametitle{Copies contd \ldots}
\begin{lstlisting}
- >>> b = a[np.array([0,1,2])]
- array([[1, 2, 3],
- [4, 5, 6],
- [7, 8, 9]])
+ >>> a = arange(1, 10, 2)
+ >>> b = a[array([0,2,3])]
>>> b.flags.owndata
- >>> abool=np.greater(a,2)
+ >>> abool=a>5
>>> c = a[abool]
>>> c.flags.owndata
\end{lstlisting}
@@ -274,12 +261,11 @@
\subsection{Linear Algebra}
\begin{frame}[fragile]
\frametitle{Linear Algebra}
- Try it!
\begin{lstlisting}
>>> import scipy as sp
>>> from scipy import linalg
- >>> A=sp.mat(np.arange(1,10))
- >>> A.shape=3,3
+ >>> A = sp.array(sp.arange(1,10))
+ >>> A.shape = 3,3
>>> linalg.inv(A)
>>> linalg.det(A)
>>> linalg.norm(A)
@@ -290,10 +276,9 @@
\begin{frame}[fragile]
\frametitle{Linear Algebra ...}
- Try it!
\begin{lstlisting}
- >>> A = sp.mat(np.arange(1,10))
- >>> A.shape=3,3
+ >>> A = sp.array(sp.arange(1,10))
+ >>> A.shape = 3,3
>>> linalg.lu(A)
>>> linalg.eig(A)
>>> linalg.eigvals(A)
@@ -302,6 +287,7 @@
\begin{frame}[fragile]
\frametitle{Solving Linear Equations}
+ \vspace{-0.2in}
\begin{align*}
3x + 2y - z & = 1 \\
2x - 2y + 4z & = -2 \\
@@ -309,10 +295,12 @@
\end{align*}
To Solve this,
\begin{lstlisting}
- >>> A = sp.mat([[3,2,-1],[2,-2,4]
+ >>> A = sp.array([[3,2,-1],[2,-2,4]
,[-1,1/2,-1]])
- >>> B = sp.mat([[1],[-2],[0]])
- >>> linalg.solve(A,B)
+ >>> b = sp.array([1,-2,0])
+ >>> x = linalg.solve(A,b)
+ >>> Ax = sp.dot(A,x)
+ >>> sp.allclose(Ax, b)
\end{lstlisting}
\inctime{15}
\end{frame}
@@ -328,7 +316,7 @@
Calculate the area under $(sin(x) + x^2)$ in the range $(0,1)$
\begin{lstlisting}
>>> def f(x):
- return np.sin(x)+x**2
+ return sin(x)+x**2
>>> integrate.quad(f, 0, 1)
\end{lstlisting}
\end{frame}
@@ -342,9 +330,9 @@
\end{align*}
\begin{lstlisting}
>>> def dx_dt(x,t):
- return -np.exp(-t)*x**2
->>> t=np.linspace(0,2,100)
->>> x=integrate.odeint(dx_dt, 2, t)
+ return -exp(-t)*x**2
+>>> t = linspace(0,2,100)
+>>> x = integrate.odeint(dx_dt, 2, t)
>>> plt.plot(x,t)
\end{lstlisting}
\inctime{10}
@@ -353,18 +341,17 @@
\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)
+>>> x = arange(0,2*pi,pi/4)
+>>> y = sin(x)
>>> fl = interpolate.interp1d(
x,y,kind='linear')
>>> fc = interpolate.interp1d(
x,y,kind='cubic')
->>> fl(np.pi/3)
->>> fc(np.pi/3)
+>>> fl(pi/3)
+>>> fc(pi/3)
\end{lstlisting}
\end{frame}
@@ -373,9 +360,9 @@
Plot the Cubic Spline of $sin(x)$
\begin{lstlisting}
>>> 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)
+>>> xs = arange(0,2*pi,pi/50)
+>>> ys = interpolate.splev(X,tck,der=0)
+>>> plt.plot(x,y,'o',x,y,xs,ys)
>>> plt.show()
\end{lstlisting}
\inctime{10}
@@ -386,15 +373,12 @@
\frametitle{Signal \& Image Processing}
\begin{itemize}
\item Convolution
- \item B-splines
\item Filtering
\item Filter design
\item IIR filter design
\item Linear Systems
- \item LTI Reresentations
- \item Waveforms
+ \item LTI Representations
\item Window functions
- \item Wavelets
\end{itemize}
\end{frame}
@@ -404,14 +388,15 @@
\begin{lstlisting}
>>> from scipy import signal, ndimage
>>> from scipy import lena
->>> A=lena().astype('float32')
->>> B=signal.medfilt2d(A)
+>>> 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)
+>>> b = ndimage.zoom(A,0.5)
>>> imshow(b)
+>>> b = ndimage.zoom(A,2)
\end{lstlisting}
\inctime{5}
\end{frame}