--- a/day2/session1.tex Sat Oct 10 08:03:50 2009 +0530
+++ b/day2/session1.tex Mon Oct 12 11:56:07 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}
@@ -125,12 +125,12 @@
% You might wish to add the option [pausesections]
\end{frame}
-\section{Matrices and Arrays}
+\section{Arrays}
\subsection{Basic \typ{numpy}}
\begin{frame}
- \frametitle{The \num module}
+ \frametitle{The \num\ module}
\begin{itemize}
\item Why?
\item What:
@@ -145,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!
@@ -159,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
@@ -181,108 +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}
-
-\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{==}, \typ{!=},
- \typ{<}, \typ{>} 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}
->>> a<4, a!=3
->>> 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,num)}
+ \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}
@@ -293,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])
@@ -302,8 +303,6 @@
[8, 9]])
>>> a[:,2]
array([3, 6, 9])
->>> a[...,2]
-array([3, 6, 9])
\end{lstlisting}
\end{frame}
@@ -321,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}
@@ -355,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}
@@ -379,7 +398,7 @@
>>> savefig('/tmp/test.png')
\end{lstlisting}
\begin{itemize}
- \item Also: PDF, PS, EPS, SVG, PDF
+ \item Also: PS, EPS, SVG, PDF
\end{itemize}
\inctime{5}
\end{frame}
@@ -412,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}
@@ -424,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--',
@@ -773,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]
@@ -785,23 +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 = 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 Sat Oct 10 08:03:50 2009 +0530
+++ b/day2/session2.tex Mon Oct 12 11:56:07 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}
@@ -121,11 +121,11 @@
\begin{frame}[fragile]
\frametitle{Broadcasting}
\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
@@ -141,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}
@@ -177,7 +169,7 @@
\frametitle{Copies \& Views}
\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
@@ -211,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}
@@ -326,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}
@@ -340,8 +330,8 @@
\end{align*}
\begin{lstlisting}
>>> def dx_dt(x,t):
- return -np.exp(-t)*x**2
->>> t = np.linspace(0,2,100)
+ return -exp(-t)*x**2
+>>> t = linspace(0,2,100)
>>> x = integrate.odeint(dx_dt, 2, t)
>>> plt.plot(x,t)
\end{lstlisting}
@@ -354,14 +344,14 @@
\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}
@@ -370,7 +360,7 @@
Plot the Cubic Spline of $sin(x)$
\begin{lstlisting}
>>> tck = interpolate.splrep(x,y)
->>> xs = np.arange(0,2*np.pi,np.pi/50)
+>>> 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()
@@ -383,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}
@@ -409,6 +396,7 @@
\begin{lstlisting}
>>> b = ndimage.zoom(A,0.5)
>>> imshow(b)
+>>> b = ndimage.zoom(A,2)
\end{lstlisting}
\inctime{5}
\end{frame}