Changes made for Goa workshop .
Binary file day1/data/interpolate.png has changed
--- a/day1/session2.tex Sat Oct 31 01:33:41 2009 +0530
+++ b/day1/session2.tex Wed Nov 04 09:40:28 2009 +0530
@@ -260,6 +260,7 @@
\end{lstlisting}
\end{frame}
+%% more on list slicing
\begin{frame}[fragile]
\frametitle{List operations}
\begin{lstlisting}
--- a/day1/session3.tex Sat Oct 31 01:33:41 2009 +0530
+++ b/day1/session3.tex Wed Nov 04 09:40:28 2009 +0530
@@ -581,6 +581,7 @@
\end{itemize}
\end{frame}
+%%making vander without vander, simple matrix
\subsection{Van der Monde matrix generation}
\begin{frame}[fragile]
\frametitle{Van der Monde Matrix}
--- a/day1/session4.tex Sat Oct 31 01:33:41 2009 +0530
+++ b/day1/session4.tex Wed Nov 04 09:40:28 2009 +0530
@@ -128,45 +128,68 @@
\begin{frame}
\frametitle{Matrices: Introduction}
-We looked at the Van der Monde matrix in the previous session,\\
-let us now look at matrices in a little more detail.
+Let us now look at matrices in detail.\\
+\alert{All matrix operations are done using \kwrd{arrays}}
\end{frame}
\subsection{Initializing}
\begin{frame}[fragile]
\frametitle{Matrices: Initializing}
\begin{lstlisting}
-In []: A = matrix([[ 1, 1, 2, -1],
- [ 2, 5, -1, -9],
- [ 2, 1, -1, 3],
- [ 1, -3, 2, 7]])
+In []: A = array([[ 1, 1, 2, -1],
+ [ 2, 5, -1, -9],
+ [ 2, 1, -1, 3],
+ [ 1, -3, 2, 7]])
In []: A
Out[]:
-matrix([[ 1, 1, 2, -1],
- [ 2, 5, -1, -9],
- [ 2, 1, -1, 3],
- [ 1, -3, 2, 7]])
+array([[ 1, 1, 2, -1],
+ [ 2, 5, -1, -9],
+ [ 2, 1, -1, 3],
+ [ 1, -3, 2, 7]])
\end{lstlisting}
\end{frame}
+\begin{frame}[fragile]
+ \frametitle{Accessing elements of matrices}
+\begin{small}
+ \begin{lstlisting}
+In []: C = array([[1,1,2],
+ [2,4,1],
+ [-1,3,7]])
+In []: C[1,2]
+Out[]: 1
+
+In []: C[1]
+Out[]: array([2, 4, 1])
+
+In []: C[1,1] = -2
+In []: C
+Out[]:
+array([[ 1, 1, 2],
+ [ 2, -2, 1],
+ [-1, 3, 7]])
+ \end{lstlisting}
+\end{small}
+\end{frame}
+
\subsection{Basic Operations}
\begin{frame}[fragile]
\frametitle{Transpose of a Matrix}
\begin{lstlisting}
-In []: linalg.transpose(A)
+In []: A.T
Out[]:
-matrix([[ 1, 2, 2, 1],
- [ 1, 5, 1, -3],
- [ 2, -1, -1, 2],
- [-1, -9, 3, 7]])
+array([[ 1, 2, 2, 1],
+ [ 1, 5, 1, -3],
+ [ 2, -1, -1, 2],
+ [-1, -9, 3, 7]])
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Sum of all elements}
\begin{lstlisting}
-In []: linalg.sum(A)
+In []: sum(A)
Out[]: 12
\end{lstlisting}
\end{frame}
@@ -174,41 +197,56 @@
\begin{frame}[fragile]
\frametitle{Matrix Addition}
\begin{lstlisting}
-In []: B = matrix([[3,2,-1,5],
- [2,-2,4,9],
- [-1,0.5,-1,-7],
- [9,-5,7,3]])
-In []: linalg.add(A,B)
+In []: B = array([[3,2,-1,5],
+ [2,-2,4,9],
+ [-1,0.5,-1,-7],
+ [9,-5,7,3]])
+In []: A + B
Out[]:
-matrix([[ 4. , 3. , 1. , 4. ],
- [ 4. , 3. , 3. , 0. ],
- [ 1. , 1.5, -2. , -4. ],
- [ 10. , -8. , 9. , 10. ]])
+array([[ 4. , 3. , 1. , 4. ],
+ [ 4. , 3. , 3. , 0. ],
+ [ 1. , 1.5, -2. , -4. ],
+ [ 10. , -8. , 9. , 10. ]])
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
+\frametitle{Elementwise Multiplication}
+\begin{lstlisting}
+In []: A*B
+Out[]:
+array([[ 3. , 2. , -2. , -5. ],
+ [ 4. , -10. , -4. , -81. ],
+ [ -2. , 0.5, 1. , -21. ],
+ [ 9. , 15. , 14. , 21. ]])
+
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile]
\frametitle{Matrix Multiplication}
\begin{lstlisting}
-In []: linalg.multiply(A, B)
+In []: dot(A,B)
Out[]:
-matrix([[ 3. , 2. , -2. , -5. ],
- [ 4. , -10. , -4. , -81. ],
- [ -2. , 0.5, 1. , -21. ],
- [ 9. , 15. , 14. , 21. ]])
+array([[ -6. , 6. , -6. , -3. ],
+ [-64. , 38.5, -44. , 35. ],
+ [ 36. , -13.5, 24. , 35. ],
+ [ 58. , -26. , 34. , -15. ]])
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Inverse of a Matrix}
+\begin{lstlisting}
+In []: inv(A)
+\end{lstlisting}
\begin{small}
\begin{lstlisting}
-In []: linalg.inv(A)
Out[]:
-matrix([[-0.5 , 0.55, -0.15, 0.7 ],
- [ 0.75, -0.5 , 0.5 , -0.75],
- [ 0.5 , -0.15, -0.05, -0.1 ],
- [ 0.25, -0.25, 0.25, -0.25]])
+array([[-0.5 , 0.55, -0.15, 0.7 ],
+ [ 0.75, -0.5 , 0.5 , -0.75],
+ [ 0.5 , -0.15, -0.05, -0.1 ],
+ [ 0.25, -0.25, 0.25, -0.25]])
\end{lstlisting}
\end{small}
\end{frame}
@@ -217,24 +255,25 @@
\frametitle{Determinant}
\begin{lstlisting}
In []: det(A)
-Out[66]: 80.0
+Out[]: 80.0
\end{lstlisting}
\end{frame}
+%%use S=array(X,Y)
\begin{frame}[fragile]
-\frametitle{Eigen Values and Eigen Matrix}
+\frametitle{Eigenvalues and Eigen Vectors}
\begin{small}
\begin{lstlisting}
-In []: E = matrix([[3,2,4],[2,0,2],[4,2,3]])
+In []: E = array([[3,2,4],[2,0,2],[4,2,3]])
-In []: linalg.eig(E)
+In []: eig(E)
Out[]:
(array([-1., 8., -1.]),
- matrix([[-0.74535599, 0.66666667, -0.1931126 ],
+ array([[-0.74535599, 0.66666667, -0.1931126 ],
[ 0.2981424 , 0.33333333, -0.78664085],
[ 0.59628479, 0.66666667, 0.58643303]]))
-In []: linalg.eigvals(E)
+In []: eigvals(E)
Out[]: array([-1., 8., -1.])
\end{lstlisting}
\end{small}
@@ -243,23 +282,23 @@
\begin{frame}[fragile]
\frametitle{Computing Norms}
\begin{lstlisting}
-In []: linalg.norm(E)
+In []: norm(E)
Out[]: 8.1240384046359608
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
- \frametitle{Single Value Decomposition}
+ \frametitle{Singular Value Decomposition}
\begin{small}
\begin{lstlisting}
-In [76]: linalg.svd(E)
-Out[76]:
-(matrix(
+In []: svd(E)
+Out[]:
+(array(
[[ -6.66666667e-01, -1.23702565e-16, 7.45355992e-01],
[ -3.33333333e-01, -8.94427191e-01, -2.98142397e-01],
[ -6.66666667e-01, 4.47213595e-01, -5.96284794e-01]]),
array([ 8., 1., 1.]),
- matrix([[-0.66666667, -0.33333333, -0.66666667],
+ array([[-0.66666667, -0.33333333, -0.66666667],
[-0. , 0.89442719, -0.4472136 ],
[-0.74535599, 0.2981424 , 0.59628479]]))
\end{lstlisting}
@@ -289,12 +328,12 @@
\frametitle{Solving using Matrices}
Let us now look at how to solve this using \kwrd{matrices}
\begin{lstlisting}
- In []: A = matrix([[3,2,-1],
- [2,-2,4],
- [-1, 0.5, -1]])
- In []: b = matrix([[1], [-2], [0]])
- In []: x = linalg.solve(A, b)
- In []: Ax = dot(A, x)
+ In []: A = array([[3,2,-1],
+ [2,-2,4],
+ [-1, 0.5, -1]])
+ In []: b = array([[1], [-2], [0]])
+ In []: x = solve(A, b)
+ In []: Ax = dot(A,x)
\end{lstlisting}
\end{frame}
@@ -314,9 +353,9 @@
\begin{lstlisting}
In []: Ax
Out[]:
-matrix([[ 1.00000000e+00],
- [ -2.00000000e+00],
- [ 2.22044605e-16]])
+array([[ 1.00000000e+00],
+ [ -2.00000000e+00],
+ [ 2.22044605e-16]])
\end{lstlisting}
\begin{block}{}
The last term in the matrix is actually \alert{0}!\\
@@ -332,7 +371,74 @@
\subsection{Exercises}
\begin{frame}[fragile]
-\frametitle{Problem Set 4: Problem 4.1}
+\frametitle{Problem 1}
+Given the matrix:\\
+\begin{center}
+$\begin{bmatrix}
+-2 & 2 & 3\\
+ 2 & 1 & 6\\
+-1 &-2 & 0\\
+\end{bmatrix}$
+\end{center}
+Find:
+\begin{itemize}
+ \item[i] Transpose
+ \item[ii]Inverse
+ \item[iii]Determinant
+ \item[iv] Eigenvalues and Eigen vectors
+ \item[v] Singular Value decomposition
+\end{itemize}
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{Problem 2}
+Given
+\begin{center}
+A =
+$\begin{bmatrix}
+-3 & 1 & 5 \\
+1 & 0 & -2 \\
+5 & -2 & 4 \\
+\end{bmatrix}$
+, B =
+$\begin{bmatrix}
+0 & 9 & -12 \\
+-9 & 0 & 20 \\
+12 & -20 & 0 \\
+\end{bmatrix}$
+\end{center}
+Find:
+\begin{itemize}
+ \item[i] Sum of A and B
+ \item[ii]Elementwise Product of A and B
+ \item[iii] Matrix product of A and B
+\end{itemize}
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{Solution}
+Sum:
+$\begin{bmatrix}
+-3 & 10 & 7 \\
+-8 & 0 & 18 \\
+17 & -22 & 4 \\
+\end{bmatrix}$
+,\\ Elementwise Product:
+$\begin{bmatrix}
+0 & 9 & -60 \\
+-9 & 0 & -40 \\
+60 & 40 & 0 \\
+\end{bmatrix}$
+,\\ Matrix product:
+$\begin{bmatrix}
+51 & -127 & 56 \\
+-24 & 49 & -12 \\
+66 & -35 & -100 \\
+\end{bmatrix}$
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{Problem 3}
Solve the set of equations:
\begin{align*}
x + y + 2z -w & = 3\\
@@ -345,37 +451,30 @@
\begin{frame}[fragile]
\frametitle{Solution}
-Solution:
-\begin{lstlisting}
+Use \kwrd{solve()}
\begin{align*}
x & = -5\\
y & = 2\\
z & = 3\\
w & = 0\\
\end{align*}
-\end{lstlisting}
-\end{frame}
-
-\begin{frame}[fragile]
-\frametitle{Problem 4.2}
-
\end{frame}
\section{Summary}
\begin{frame}
- \frametitle{Summary}
-So what did we learn??
+ \frametitle{What did we learn??}
\begin{itemize}
\item Matrices
\begin{itemize}
+ \item Accessing elements
\item Transpose
\item Addition
\item Multiplication
\item Inverse of a matrix
\item Determinant
- \item Eigen values and Eigen matrix
+ \item Eigenvalues and Eigen vector
\item Norms
- \item Single Value Decomposition
+ \item Singular Value Decomposition
\end{itemize}
\item Solving linear equations
\end{itemize}
--- a/day1/session5.tex Sat Oct 31 01:33:41 2009 +0530
+++ b/day1/session5.tex Wed Nov 04 09:40:28 2009 +0530
@@ -124,8 +124,25 @@
%% % \pausesections
%% \end{frame}
+\section{\typ{loadtxt}}
-\section{\typ{loadtxt}}
+\begin{frame}[fragile]
+ \frametitle{Array slicing}
+ \begin{lstlisting}
+In []: A = array([[ 1, 1, 2, -1],
+ [ 2, 5, -1, -9],
+ [ 2, 1, -1, 3],
+ [ 1, -3, 2, 7]])
+
+In []: A[:,0]
+Out[]: array([ 1, 2, 2, 1])
+
+In []: A[1:3,1:3]
+Out[]:
+array([[ 5, -1],
+ [ 1, -1]])
+\end{lstlisting}
+\end{frame}
\begin{frame}[fragile]
\frametitle{\typ{loadtxt}}
@@ -134,8 +151,9 @@
\item Each row must have same number of values.
\end{itemize}
\begin{lstlisting}
-In []: L, T = loadtxt('pendulum.txt',
- unpack = True)
+In []: data = loadtxt('pendulum.txt')
+In []: x = data[:, 0]
+In []: y = data[:, 1]
\end{lstlisting}
\end{frame}
@@ -151,6 +169,7 @@
\item It contains x,y position of particle.
\item Plot the given points.
%% \item Interpolate the missing region.
+%% load txt the unpack part.
\end{itemize}
\begin{lstlisting}
In []: x, y = loadtxt('points.txt',
@@ -195,6 +214,8 @@
%% \end{lstlisting}
%% \end{frame}
+%%better example, maybe parabola
+
\begin{frame}[fragile]
\frametitle{Spline Interpolation}
\begin{small}
@@ -223,22 +244,29 @@
\begin{frame}[fragile]
\frametitle{\typ{splev}}
-To Evaluate a B-spline and it's derivatives
+To Evaluate a spline and it's derivatives
\begin{lstlisting}
-In []: Xnew = arange(0.01,3,0.02)
+In []: Xnew = linspace(0.01,3,100)
In []: Ynew = splev(Xnew, tck)
In []: y.shape
Out[]: (40,)
In []: Ynew.shape
-Out[]: (150,)
+Out[]: (100,)
-In []: plot(Xnew, Ynew)
+In []: plot(Xnew, Ynew, '+')
\end{lstlisting}
\end{frame}
+\begin{frame}
+ \frametitle{Interpolated data}
+ \begin{center}
+ \includegraphics[height=2in, interpolate=true]{data/interpolate}
+ \end{center}
+\end{frame}
+
%% \begin{frame}[fragile]
%% \frametitle{Interpolation \ldots}
%% \begin{itemize}
@@ -257,8 +285,8 @@
\end{itemize}
\begin{center}
\begin{tabular}{l l}
-$f(x+h)=f(x)+h.f^{'}(x)$ &Forward \\
-$f(x-h)=f(x)-h.f^{'}(x)$ &Backward
+$f(x+h)=f(x)+hf^{'}(x)$ &Forward \\
+$f(x-h)=f(x)-hf^{'}(x)$ &Backward
\end{tabular}
\end{center}
\end{frame}
@@ -277,7 +305,10 @@
\frametitle{Forward Difference \ldots}
\begin{lstlisting}
In []: fD = (y[1:] - y[:-1]) / deltax
-In []: plot(x, y, x[:-1], fD)
+In []: print len(fD)
+Out[]: 99
+In []: plot(x, y)
+In []: plot(x[:-1], fD)
\end{lstlisting}
\begin{center}
\includegraphics[height=2in, interpolate=true]{data/fwdDiff}
@@ -310,27 +341,18 @@
\frametitle{Example \ldots}
\begin{itemize}
\item Read the file
-\item Obtain an array of x, y
+\item Obtain an array of X, Y(S)
\item Obtain velocity and acceleration
\item use \typ{deltaT = 0.05}
\end{itemize}
\begin{lstlisting}
-In []: X = []
-In []: Y = []
-In []: for line in open('location.txt'):
- .... points = line.split()
- .... X.append(float(points[0]))
- .... Y.append(float(points[1]))
-In []: S = array([X, Y])
+In []: S = loadtxt('location.txt')
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Example \ldots}
-\begin{itemize}
-\item use \typ{deltaT = 0.05}
-\end{itemize}
\begin{lstlisting}
In []: deltaT = 0.05
@@ -345,18 +367,17 @@
\begin{frame}[fragile]
\frametitle{Quadrature}
-\begin{itemize}
-\item We wish to find area under a curve
-\item Area under $(sin(x) + x^2)$ in $(0,1)$
-\item scipy has functions to do that
-\end{itemize}
-\begin{small}
- \typ{In []: from scipy.integrate import quad}
-\end{small}
+
+\emphbar{$\int_0^1(sin(x) + x^2)$}
+
+\typ{In []: from scipy.integrate import quad}
+
\begin{itemize}
\item Inputs - function to integrate, limits
\end{itemize}
\begin{lstlisting}
+%% In []: quad(sin(x)+x**2, 0, 1)
+%% NameError: name 'x' is not defined
In []: x = 0
In []: quad(sin(x)+x**2, 0, 1)
\end{lstlisting}
@@ -406,8 +427,7 @@
\end{lstlisting}
Returns the integral and an estimate of the absolute error in the result.
\begin{itemize}
-\item Look at \typ{dblquad} for Double integrals
-\item Use \typ{tplquad} for Triple integrals
+\item \typ{dblquad}, \typ{tplquad} are available
\end{itemize}
\end{frame}
@@ -419,9 +439,7 @@
\item Functions
\begin{itemize}
\item Definition
- \item Calling
- \item Default Arguments
- \item Keyword Arguments
+ \item Calling
\end{itemize}
\item Quadrature
\end{itemize}
--- a/day1/session6.tex Sat Oct 31 01:33:41 2009 +0530
+++ b/day1/session6.tex Wed Nov 04 09:40:28 2009 +0530
@@ -123,6 +123,7 @@
%% % You might wish to add the option [pausesections]
%% \end{frame}
+%%more exercise on plotting
\section{ODEs}
\begin{frame}[fragile]
@@ -138,7 +139,7 @@
\dot{\theta} &= \omega \\
\dot{\omega} &= -\frac{g}{L}sin(\theta) \\
\text{At}\ t &= 0 : \nonumber \\
- \theta = \theta_0\quad & \&\quad \omega = 0 \nonumber
+ \theta = \theta_0(10^o)\quad & \&\quad \omega = 0\ (Initial\ values)\nonumber
\end{align}
\end{frame}
@@ -149,9 +150,9 @@
\item Define a function as below
\end{itemize}
\begin{lstlisting}
-In []: def pend_int(unknown, t, p):
- .... theta, omega = unknown
- .... g, L = p
+In []: def pend_int(initial, t):
+ .... theta, omega = initial
+ .... g, L = 9.81, 0.2
.... f=[omega, -(g/L)*sin(theta)]
.... return f
....
@@ -162,25 +163,22 @@
\frametitle{Solving ODEs using SciPy \ldots}
\begin{itemize}
\item \typ{t} is the time variable \\
-\item \typ{p} has the constants \\
\item \typ{initial} has the initial values
\end{itemize}
\begin{lstlisting}
In []: t = linspace(0, 10, 101)
-In []: p=(-9.81, 0.2)
In []: initial = [10*2*pi/360, 0]
-\end{lstlisting}
+\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Solving ODEs using SciPy \ldots}
-\begin{small}
- \typ{In []: from scipy.integrate import odeint}
-\end{small}
+%%\begin{small}
+\typ{In []: from scipy.integrate import odeint}
+%%\end{small}
\begin{lstlisting}
In []: pend_sol = odeint(pend_int,
- initial,t,
- args=(p,))
+ initial,t)
\end{lstlisting}
\end{frame}
@@ -308,14 +306,13 @@
\begin{frame}[fragile]
\frametitle{Initial Estimates \ldots}
\begin{lstlisting}
- In []: def our_f(x):
- ....: return cos(x)-x**2
- ....:
- In []: x = linspace(-pi/2, pi/2, 11)
+In []: def our_f(x):
+ ....: return cos(x) - x*x
+ ....:
+In []: x = linspace(-pi/2, pi/2, 11)
+In []: y = our_f(x)
\end{lstlisting}
-\begin{itemize}
-\item Get the intervals of x, where sign changes occur
-\end{itemize}
+Get the intervals of x, where sign changes occur
\end{frame}
\begin{frame}[fragile]
@@ -335,7 +332,6 @@
\frametitle{Scipy Methods - \typ{roots}}
\begin{itemize}
\item Calculates the roots of polynomials
-\item Array of coefficients is the only parameter
\end{itemize}
\begin{lstlisting}
In []: coeffs = [1, 6, 13]
--- a/day1quiz1.tex Sat Oct 31 01:33:41 2009 +0530
+++ b/day1quiz1.tex Wed Nov 04 09:40:28 2009 +0530
@@ -129,7 +129,7 @@
\begin{frame}
\frametitle{\incqno }
-How to read and print each line of file.
+How to read and print each line of a file.
\end{frame}
\begin{frame}
@@ -146,5 +146,15 @@
\end{lstlisting}
\end{frame}
+\begin{frame}
+\frametitle{\incqno }
+Draw a plot with line width 3.
+\end{frame}
+
+\begin{frame}
+\frametitle{\incqno }
+Setting x and y axis limits.
+\end{frame}
+
\end{document}
--- a/day2/session3.tex Sat Oct 31 01:33:41 2009 +0530
+++ b/day2/session3.tex Wed Nov 04 09:40:28 2009 +0530
@@ -95,12 +95,12 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Title page
-\title[]{3D data Visualization}
+\title[3D Plotting]{3D data Visualization}
\author[FOSSEE] {FOSSEE}
\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
-\date[] {1, November 2009\\Day 2, Session 3}
+\date[] {1 November, 2009\\Day 2, Session 5}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -279,10 +279,9 @@
\end{columns}
\begin{lstlisting}
-In []: from numpy import *
In []: t = linspace(0, 2*pi, 50)
In []: u = cos(t) * pi
-in []: x, y, z = sin(u), cos(u), sin(t)
+In []: x, y, z = sin(u), cos(u), sin(t)
\end{lstlisting}
\emphbar{\PythonCode{In []: mlab.points3d(x, y, z)}}
\end{frame}
@@ -319,6 +318,40 @@
\end{frame}
\begin{frame}[fragile]
+ \frametitle{mgrid}
+ \begin{lstlisting}
+In []: mgrid[0:3,0:3]
+Out[]:
+array([[[0, 0, 0],
+ [1, 1, 1],
+ [2, 2, 2]],
+
+ [[0, 1, 2],
+ [0, 1, 2],
+ [0, 1, 2]]])
+
+In []: mgrid[-1:1:5j]
+Out[]: array([-1., -0.5, 0., 0.5, 1.])
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile]
+ \frametitle{Example}
+ \begin{lstlisting}
+In []: x, y = mgrid[-1:1:5j, -1:1:5j]
+In []: z = x*x + y*y
+
+In []: z
+Out[]:
+array([[ 2. , 1.25, 1. , 1.25, 2. ],
+ [ 1.25, 0.5 , 0.25, 0.5 , 1.25],
+ [ 1. , 0.25, 0. , 0.25, 1. ],
+ [ 1.25, 0.5 , 0.25, 0.5 , 1.25],
+ [ 2. , 1.25, 1. , 1.25, 2. ]])
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile]
\myemph{\Large 2D data: \texttt{mlab.mesh}}
\vspace*{0.25in}
@@ -328,7 +361,7 @@
\vspace*{0.25in}
\begin{lstlisting}
-In []: phi, theta = numpy.mgrid[0:pi:20j,
+In []: phi, theta = mgrid[0:pi:20j,
... 0:2*pi:20j]
In []: x = sin(phi)*cos(theta)
In []: y = sin(phi)*sin(theta)
@@ -349,7 +382,7 @@
\pgfimage[width=1.5in]{MEDIA/m2/mlab/contour3d}\\
\end{columns}
\begin{lstlisting}
-In []: x, y, z = ogrid[-5:5:64j,
+In []: x, y, z = mgrid[-5:5:64j,
... -5:5:64j,
... -5:5:64j]
In []: mlab.contour3d(x*x*0.5 + y*y +
--- a/day2/session4.tex Sat Oct 31 01:33:41 2009 +0530
+++ b/day2/session4.tex Wed Nov 04 09:40:28 2009 +0530
@@ -95,14 +95,12 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Title page
-\title[]{Debugging and \\Test Driven Approach}
+\title[Python Development]{Python Development}
\author[FOSSEE] {FOSSEE}
\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
-\date[] {11, October 2009}
-\date[] % (optional)
-
+\date[] {1 November, 2009\\Day 2, Session 3}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\pgfdeclareimage[height=0.75cm]{iitblogo}{iitblogo}
@@ -142,13 +140,147 @@
\maketitle
\end{frame}
+\section{Tests: Getting started}
+\begin{frame}[fragile]
+ \frametitle{gcd revisited!}
+ \begin{itemize}
+ \item Open gcd.py
+ \end{itemize}
+\begin{lstlisting}
+ def gcd(a, b):
+ if a % b == 0:
+ return b
+ return gcd(b, a%b)
+
+ print gcd(15, 65)
+ print gcd(16, 76)
+\end{lstlisting}
+ \begin{itemize}
+ \item python gcd.py
+ \end{itemize}
+\end{frame}
+
+\begin{frame}[fragile]
+ \frametitle{Find lcm using our gcd module}
+ \begin{itemize}
+ \item Open lcm.py
+ \item $lcm = \frac{a*b}{gcd(a,b)}$
+ \end{itemize}
+\begin{lstlisting}
+ from gcd import gcd
+ def lcm(a, b):
+ return (a * b) / gcd(a, b)
+
+ print lcm(14, 56)
+\end{lstlisting}
+ \begin{itemize}
+ \item python lcm.py
+ \end{itemize}
+ \begin{lstlisting}
+5
+4
+56
+ \end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile]
+ \frametitle{Writing stand-alone module}
+Edit gcd.py file to:
+\begin{lstlisting}
+ def gcd(a, b):
+ if a % b == 0:
+ return b
+ return gcd(b, a%b)
+
+ if __name__ == "__main__":
+ print gcd(15, 65)
+ print gcd(16, 76)
+\end{lstlisting}
+ \begin{itemize}
+ \item python gcd.py
+ \item python lcm.py
+ \end{itemize}
+\end{frame}
+
+\begin{frame}[fragile]
+ \frametitle{More use of main}
+ For automating tests.
+ \begin{lstlisting}
+if __name__ == '__main__':
+ for line in open('numbers.txt'):
+ numbers = line.split()
+ x = int(numbers[0])
+ y = int(numbers[1])
+ result = (int(numbers[2]))
+ assert gcd(x, y) == result
+ \end{lstlisting}
+\end{frame}
+
+\section{Coding Style}
+\begin{frame}{Readability and Consistency}
+ \begin{itemize}
+ \item Readability Counts!\\Code is read more often than its written.
+ \item Consistency!
+ \item Know when to be inconsistent.
+ \end{itemize}
+\end{frame}
+
+\begin{frame}[fragile] \frametitle{A question of good style}
+ \begin{lstlisting}
+ amount = 12.68
+ denom = 0.05
+ nCoins = round(amount/denom)
+ rAmount = nCoins * denom
+ \end{lstlisting}
+ \pause
+ \begin{block}{Style Rule \#1}
+ Naming is 80\% of programming
+ \end{block}
+\end{frame}
+
+\begin{frame}[fragile]
+ \frametitle{Code Layout}
+ \begin{itemize}
+ \item Indentation
+ \item Tabs or Spaces??
+ \item Maximum Line Length
+ \item Blank Lines
+ \item Encodings
+ \end{itemize}
+\end{frame}
+
+\begin{frame}{Whitespaces in Expressions}
+ \begin{itemize}
+ \item When to use extraneous whitespaces??
+ \item When to avoid extra whitespaces??
+ \item Use one statement per line
+ \end{itemize}
+\end{frame}
+
+\begin{frame}{Comments}
+ \begin{itemize}
+ \item No comments better than contradicting comments
+ \item Block comments
+ \item Inline comments
+ \end{itemize}
+\end{frame}
+
+\begin{frame}{Docstrings}
+ \begin{itemize}
+ \item When to write docstrings?
+ \item Ending the docstrings
+ \item One liner docstrings
+ \end{itemize}
+More information at PEP8: http://www.python.org/dev/peps/pep-0008/
+\inctime{5}
+\end{frame}
\section{Debugging}
\subsection{Errors and Exceptions}
\begin{frame}[fragile]
\frametitle{Errors}
\begin{lstlisting}
->>> while True print 'Hello world'
+In []: while True print 'Hello world'
\end{lstlisting}
\pause
\begin{lstlisting}
@@ -162,7 +294,7 @@
\begin{frame}[fragile]
\frametitle{Exceptions}
\begin{lstlisting}
->>> print spam
+In []: print spam
\end{lstlisting}
\pause
\begin{lstlisting}
@@ -175,7 +307,7 @@
\begin{frame}[fragile]
\frametitle{Exceptions}
\begin{lstlisting}
->>> 1 / 0
+In []: 1 / 0
\end{lstlisting}
\pause
\begin{lstlisting}
@@ -186,6 +318,44 @@
\end{lstlisting}
\end{frame}
+\begin{frame}[fragile]
+ \frametitle{Handling Exceptions}
+ Python uses \typ{try} and \typ{except} clause.
+ %%Revisiting the raw\_input
+ \begin{lstlisting}
+a = raw_input('Enter number(Q to quit):')
+try:
+ num = int(a)
+ print num
+except:
+ if a == 'Q':
+ print 'Exiting...'
+ else:
+ print 'Wrong input!'
+ \end{lstlisting}
+
+
+\end{frame}
+
+%% \begin{frame}[fragile]
+%% \frametitle{Solving it with \typ{try} and \typ{except}}
+%% \vspace{-0.2in}
+%% \begin{lstlisting}
+%% highest = 0
+%% for record in open('sslc1.txt'):
+%% fields = record.split(';')
+%% try:
+%% total = 0
+%% for score_str in fields[3:8]:
+%% score = int(score_str)
+%% total += score
+%% if total > highest:
+%% highest = total
+%% except:
+%% pass
+%% print highest
+%% \end{lstlisting}
+%% \end{frame}
\subsection{Strategy}
\begin{frame}[fragile]
\frametitle{Debugging effectively}
@@ -209,8 +379,8 @@
\frametitle{Debugging in IPython}
\small
\begin{lstlisting}
-In [1]: import mymodule
-In [2]: mymodule.test()
+In []: import mymodule
+In []: mymodule.test()
---------------------------------------------
NameError Traceback (most recent call last)
<ipython console> in <module>()
@@ -219,7 +389,7 @@
----> 2 print spam
NameError: global name 'spam' is not defined
-In [3]: %debug
+In []: %debug
> mymodule.py(2)test()
0 print spam
ipdb>
@@ -232,19 +402,21 @@
\frametitle{Debugging: Exercise}
\small
\begin{lstlisting}
-import keyword
-f = open('/path/to/file')
+science = {}
+
+for record in open('sslc1.txt'):
+ fields = record.split(';')
+ region_code = fields[0].strip()
-freq = {}
-for line in f:
- words = line.split()
- for word in words:
- key = word.strip(',.!;?()[]: ')
- if keyword.iskeyword(key):
- value = freq[key]
- freq[key] = value + 1
+ score_str = fields[6].strip()
+ score = int(score_str) if score_str != 'AA'
+ else 0
-print freq
+ if score > 90:
+ science[region_code] += 1
+
+pie(science.values(), labels=science.keys())
+savefig('science.png')
\end{lstlisting}
\inctime{10}
\end{frame}
@@ -263,7 +435,7 @@
\section{Test Driven Approach}
\begin{frame}
- \frametitle{Need of Testing!}
+ \frametitle{Need for Testing!}
\begin{itemize}
\item Quality
@@ -293,17 +465,19 @@
\begin{frame}[fragile]
\frametitle{Test for the palindrome: palindrome.py}
\begin{lstlisting}
-from plaindrome import is_palindrome
def test_function_normal_words():
input = "noon"
assert is_palindrome(input) == True
+
+if __name__ == "main'':
+ test_function_normal_words()
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Running the tests.}
\begin{lstlisting}
-$ nosetests test.py
+$ nosetests palindrome.py
.
----------------------------------------------
Ran 1 test in 0.001s
@@ -321,7 +495,7 @@
\end{lstlisting}
\vspace*{0.25in}
Check\\
- \PythonCode{$ nosetests test.py} \\
+ \PythonCode{$ nosetests palindrome.py} \\
\begin{block}{Task}
Tweak the code to pass this test.
\end{block}
@@ -347,37 +521,39 @@
%\end{frame}
%
-\begin{frame}[fragile]
- \frametitle{Exercise}
- Based on Euclid's algorithm:
- \begin{center}
- $gcd(a,b)=gcd(b,b\%a)$
- \end{center}
- gcd function can be written as:
- \begin{lstlisting}
- def gcd(a, b):
- if a%b == 0: return b
- return gcd(b, a%b)
- \end{lstlisting}
- \vspace*{-0.15in}
- \begin{block}{Task}
- \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}
+%% \begin{frame}[fragile]
+%% \frametitle{Exercise}
+%% Based on Euclid's algorithm:
+%% \begin{center}
+%% $gcd(a,b)=gcd(b,b\%a)$
+%% \end{center}
+%% gcd function can be written as:
+%% \begin{lstlisting}
+%% def gcd(a, b):
+%% if a%b == 0: return b
+%% return gcd(b, a%b)
+%% \end{lstlisting}
+%% \vspace*{-0.15in}
+%% \begin{block}{Task}
+%% \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}
-\end{frame}
+%% \inctime{15}
+%% \end{frame}
\begin{frame}
- \frametitle{We have learned}
+ \frametitle{Summary}
+We have coverd:
\begin{itemize}
\item Following and Resolving Error Messages.
\item Exceptions.
+ \item Handling exceptions
\item Approach for Debugging.
\item Writting and running tests.
\end{itemize}
--- a/day2/session5.tex Sat Oct 31 01:33:41 2009 +0530
+++ b/day2/session5.tex Wed Nov 04 09:40:28 2009 +0530
@@ -78,7 +78,7 @@
\author[FOSSEE] {FOSSEE}
\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
-\date[] {1, November 2009\\Day 2}
+\date[] {1 November, 2009\\Day 2, Session 4}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo}
@@ -111,12 +111,13 @@
\end{frame}
\begin{frame}{Problem 1.1}
- The aliquot of a number is defined as: the sum of the \emph{proper} divisors of the number. For example, aliquot(12) = 1 + 2 + 3 + 4 + 6 = 16.\\
+ The aliquot of a number is defined as: the sum of the \emph{proper} divisors of the number. \\For example:
+\center{aliquot(12) = 1 + 2 + 3 + 4 + 6 = 16.}\\
Write a function that returns the aliquot number of a given number.
\end{frame}
\begin{frame}{Problem 1.2}
- A pair of numbers (a, b) is said to be \alert{amicable} if the aliquot number of a is b and the aliquot number of b is a.\\
+ Pair of numbers (a, b) is said to be \alert{amicable} if aliquot number of a is b and aliquot number of b is a.\\
Example: \texttt{220, 284}\\
Write a program that prints all four digit amicable pairs.