# HG changeset patch # User vattam@chiquitita # Date 1257409260 -19800 # Node ID 4555c3814dd4275d90c7c9397d84505a618dc1c9 # Parent 71e50184d4827e82b0c00524c1b16d09e9708f1f# Parent c378d1ffb1d14c09cdf4c6eed0b13ee8a41d23aa Merged branches. diff -r 71e50184d482 -r 4555c3814dd4 day1/cheatsheet1.tex --- a/day1/cheatsheet1.tex Tue Nov 03 11:13:38 2009 +0530 +++ b/day1/cheatsheet1.tex Thu Nov 05 13:51:00 2009 +0530 @@ -2,54 +2,43 @@ \title{Interactive Plotting} \author{FOSSEE} \begin{document} -\maketitle +\date{} +\vspace{-1in} +\begin{center} +\LARGE{Interactive Plotting}\\ +\large{FOSSEE} +\end{center} +\section{Starting up...} -\section{Starting up...} \begin{verbatim} $ ipython -pylab \end{verbatim} -Exiting Ipython +Exiting \begin{verbatim} - In [1]: print "Hello, World!" - In [2]: ^D - Do you really want to exit ([y]/n)? y +In [2]: (Ctrl-D)^D +Do you really want to exit ([y]/n)? y \end{verbatim} Breaking out of loops \begin{verbatim} - In [1]: while True: - ...: print "Hello, World!" - ...: - Hello, World! - Hello, World!^C -\end{verbatim} - -\section{First Plot} -\begin{verbatim} - In [2]: x=linspace(0, 2*pi, 50) - - In [3]: plot(x,sin(x)) - Out[3]: [] +In [1]: while True: + ...: print "Hello, World!" + ...: +Hello, World! +Hello, World!(Ctrl-C)^C \end{verbatim} -\section{Labeling} -\begin{verbatim} - In [4]: xlabel('x') - - In [5]: ylabel('sin(x)') -\end{verbatim} - -\section{Another example} +\section{Plotting} \begin{verbatim} -In [6]: clf() - -In [7]: y = linspace(0,10,101) - -In [8]: plot(y, exp(-y)) - -In [9]: xlabel('y') - -In [10]: ylabel(r'$e^{-y}$') +In [1]: x = linspace(0, 2*pi, 50) +In [2]: plot(x,sin(x)) +In [3]: xlabel('x') +In [4]: ylabel('sin(x)') +In [5]: legend(['x', '-x', 'sin(x)', 'xsin(x)']) +In [6]: annotate('origin', xy=(0, 0), xytext=(0, -7), + arrowprops=dict(shrink=0.05)) +In [7]: xlim(5*pi, 5*pi) +In [8]: ylim(5*pi, 5*pi) +In [9]: clf() #Clears the Plot area \end{verbatim} - \end{document} diff -r 71e50184d482 -r 4555c3814dd4 day1/data/annotate.png Binary file day1/data/annotate.png has changed diff -r 71e50184d482 -r 4555c3814dd4 day1/data/interpolate.png Binary file day1/data/interpolate.png has changed diff -r 71e50184d482 -r 4555c3814dd4 day1/data/missing_points.png Binary file day1/data/missing_points.png has changed diff -r 71e50184d482 -r 4555c3814dd4 day1/data/pos_vel_accel.png Binary file day1/data/pos_vel_accel.png has changed diff -r 71e50184d482 -r 4555c3814dd4 day1/links.tex --- a/day1/links.tex Tue Nov 03 11:13:38 2009 +0530 +++ b/day1/links.tex Thu Nov 05 13:51:00 2009 +0530 @@ -1,6 +1,7 @@ \documentclass[12pt]{article} \title{Links and References} -\author{Asokan Pichai\\Prabhu Ramachandran} +\author{FOSSEE} +\date{} \begin{document} \maketitle \begin{itemize} @@ -8,7 +9,9 @@ \item ``may be one of the thinnest programming language books on my shelf, but it's also one of the best.'' -- \emph{Slashdot, AccordianGuy, September 8, 2004}- available at \url{http://diveintopython.org/} \item How to Think Like a Computer Scientist: Learning with Python available at \url{http://www.openbookproject.net/thinkcs/python/english/}\\``The concepts covered here apply to all programming languages and to problem solving in general.'' -- \emph{Guido van Rossum, creator of Python} \item Some assorted articles related to Python \url{http://effbot.org/zone/index.htm} + \item Reference manual to describe the standard libraries that are distributed with Python is available at \url{http://docs.python.org/library/} \item To read more on strings refer to: \\ \url{http://docs.python.org/library/stdtypes.html#string-methods} + \item Some coding conventions for using Python language are available at \\ \url{http://www.python.org/dev/peps/pep-0008/} \item For documentation on IPython refer: \\ \url{http://ipython.scipy.org/moin/Documentation} \item Documentation for Numpy and Scipy is available at: \url{http://docs.scipy.org/doc/} \item For "recipes" or worked examples of commonly-done tasks in SciPy explore: \url{http://www.scipy.org/Cookbook/} diff -r 71e50184d482 -r 4555c3814dd4 day1/session1.tex --- a/day1/session1.tex Tue Nov 03 11:13:38 2009 +0530 +++ b/day1/session1.tex Thu Nov 05 13:51:00 2009 +0530 @@ -49,7 +49,7 @@ } \newcounter{time} \setcounter{time}{0} -\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} +\newcommand{%\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} \newcommand{\typ}[1]{\lstinline{#1}} @@ -77,7 +77,7 @@ \author[FOSSEE] {FOSSEE} \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} -\date[] {31, October 2009\\Day 1, Session 1} +\date[] {31 October, 2009\\Day 1, Session 1} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} @@ -106,7 +106,7 @@ % the following command: %\beamerdefaultoverlayspecification{<+->} -%\includeonlyframes{current,current1,current2,current3,current4,current5,current6} +%%\includeonlyframes{current,current1,current2,current3,current4,current5,current6} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % DOCUMENT STARTS @@ -129,10 +129,10 @@ \item[Session 2] Sat 10:05--11:05 \item[Session 3] Sat 11:20--12:20 \item[Session 4] Sat 12:25--13:25 - \item[Quiz -1] Sat 14:25--14:40 + \item[Quiz 1] Sat 14:25--14:40 \item[Session 5] Sat 14:40--15:40 \item[Session 6] Sat 15:55--16:55 - \item[Quiz -2] Sat 17:00--17:15 + \item[Quiz 2] Sat 17:00--17:15 \end{description} \end{frame} @@ -143,10 +143,10 @@ \item[Session 2] Sun 10:05--11:05 \item[Session 3] Sun 11:20--12:20 \item[Session 4] Sun 12:25--13:25 - \item[Quiz -1] Sun 14:25--14:40 + \item[Quiz 1] Sun 14:25--14:40 \item[Session 5] Sun 14:40--15:40 \item[Session 6] Sun 15:55--16:55 - \item[Quiz -2] Sun 17:00--17:15 + \item[Quiz 2] Sun 17:00--17:15 \end{description} \end{frame} @@ -161,9 +161,9 @@ \begin{block}{Goal: Successful participants will be able to} \begin{itemize} - \item Use Python as plotting, computational toolkit - \item Understand how Python can be used as scripting and problem solving language. - \item Train the students to use Python for the same + \item Use Python as plotting, computational tool + \item Understand how Python can be used as a scripting and problem solving language. + \item Train students for the same \end{itemize} \end{block} \end{frame} @@ -172,14 +172,25 @@ \begin{frame} \frametitle{Checklist} \begin{enumerate} - \item IPython: Type ipython at the command line. Is it available? + \item IPython \item Editor: We recommend scite. - \item Data files: Make sure you have all data files. + \item Data files: + \begin{itemize} + \item \typ{sslc1.txt} + \item \typ{pendulum.txt} + \item \typ{points.txt} + \item \typ{pos.txt} + \end{itemize} + \item Python scripts: + \begin{itemize} + \item \typ{sslc_allreg.py} + \item \typ{sslc_science.py} + \end{itemize} \end{enumerate} \end{frame} \begin{frame}[fragile] -\frametitle{Starting up...} +\frametitle{Starting up \ldots} \begin{block}{} \begin{verbatim} $ ipython -pylab @@ -196,20 +207,20 @@ \end{lstlisting} \end{frame} -\begin{frame}[fragile] -\frametitle{Loops} -Breaking out of loops -\begin{lstlisting} - In []: while True: - ...: print "Hello, World!" - ...: - Hello, World! - Hello, World!^C(Ctrl-C) - ------------------------------------ - KeyboardInterrupt +%% \begin{frame}[fragile] +%% \frametitle{Loops} +%% Breaking out of loops +%% \begin{lstlisting} +%% In []: while True: +%% ...: print "Hello, World!" +%% ...: +%% Hello, World! +%% Hello, World!^C(Ctrl-C) +%% ------------------------------------ +%% KeyboardInterrupt -\end{lstlisting} -\end{frame} +%% \end{lstlisting} +%% \end{frame} \section{Plotting} @@ -224,7 +235,7 @@ \begin{block}{} \begin{small} \begin{lstlisting} -In []: x = linspace(0, 2*pi, 51) +In []: x = linspace(0, 2*pi, 50) In []: plot(x, sin(x)) \end{lstlisting} \end{small} @@ -246,6 +257,7 @@ \begin{block}{\typ{plot(x, y)}} plots \typ{x} and \typ{y} using default line style and color \end{block} +%\inctime{10} \end{frame} \subsection{Decoration} @@ -279,12 +291,11 @@ \begin{lstlisting} In []: clf() #Clears the plot area. -In []: y = linspace(0, 2*pi, 51) +In []: y = linspace(0, 2*pi, 50) In []: plot(y, sin(2*y)) In []: xlabel('y') In []: ylabel('sin(2y)') \end{lstlisting} -\emphbar{By default plots would be overlaid!} \end{frame} \subsection{More decoration} @@ -308,11 +319,10 @@ \begin{frame}[fragile] \frametitle{Legend Placement} - \begin{block}{} \small \begin{lstlisting} -In []: legend(['sin(2y)'], loc='center') +In []: legend(['sin(2y)'], loc = 'center') \end{lstlisting} \end{block} @@ -348,6 +358,7 @@ \begin{center} \includegraphics[height=2in, interpolate=true]{data/loc} \end{center} +%\inctime{10} \end{frame} \begin{frame}[fragile] @@ -361,8 +372,22 @@ \section{Multiple plots} \begin{frame}[fragile] +\frametitle{Overlaid Plots} +\begin{lstlisting} +In []: clf() +In []: plot(y, sin(y)) +In []: plot(y, cos(y)) +In []: xlabel('y') +In []: ylabel('f(y)') +In []: legend(['sin(y)', 'cos(y)']) +\end{lstlisting} +\emphbar{By default plots would be overlaid!} +\end{frame} + +\begin{frame}[fragile] \frametitle{Plotting separate figures} \begin{lstlisting} +In []: clf() In []: figure(1) In []: plot(y, sin(y)) In []: figure(2) @@ -381,23 +406,20 @@ In []: plot(y, sin(y), 'g') In []: clf() -In []: plot(y, sin(y), linewidth=2) +In []: plot(y, sin(y), 'g', linewidth=2) \end{lstlisting} \vspace*{-0.2in} \begin{center} \includegraphics[height=2.2in, interpolate=true]{data/green} \end{center} +%\inctime{10} \end{frame} \begin{frame}[fragile] \frametitle{Annotating} \vspace*{-0.15in} \begin{lstlisting} -In []: annotate('local max', - xy=(1.5, 1), - xytext=(2.5, .8), - arrowprops=dict( - shrink=0.05),) +In []: annotate('local max', xy=(1.5, 1)) \end{lstlisting} \vspace*{-0.2in} \begin{center} @@ -415,7 +437,7 @@ In []: xmax = 2*pi #Set the axes limits In []: xlim(xmin, xmax) -In []: ylim(ymin, ymax) +In []: ylim(ymin-0.2, ymax+0.2) \end{lstlisting} \end{frame} @@ -428,34 +450,29 @@ \item Set axis limits to the range of x \end{enumerate} \begin{lstlisting} -In []: x=linspace(-5*pi, 5*pi, 501) +In []: x=linspace(-5*pi, 5*pi, 500) In []: plot(x, x, 'b') In []: plot(x, -x, 'b') \end{lstlisting} $\vdots$ \end{frame} -\section{Exercises} \begin{frame}[fragile] \frametitle{Review Problem \ldots} -\small{ \begin{lstlisting} In []: plot(x, sin(x), 'g', linewidth=2) -In []: plot(x, x*sin(x), 'r', linewidth=3) +In []: plot(x, x*sin(x), 'r', + linewidth=3) \end{lstlisting} - \begin{lstlisting} -In []: legend(['x', '-x', 'sin(x)', 'xsin(x)']) -In []: annotate('origin', - xy=(0, 0), - xytext=(0, -7), - arrowprops=dict( - shrink=0.05)) -In []: xlim(5*pi, 5*pi) -In []: ylim(5*pi, 5*pi) +In []: legend(['x', '-x', 'sin(x)', + 'xsin(x)']) +In []: annotate('origin', xy = (0, 0)) +In []: xlim(-5*pi, 5*pi) +In []: ylim(-5*pi, 5*pi) \end{lstlisting} -} \end{frame} + \begin{frame} \frametitle{What did we learn?} \begin{itemize} @@ -467,6 +484,7 @@ \begin{block}{Note} \centerline{\alert{Don't Close \typ{IPython}}} \end{block} +%%\inctime{10} \end{frame} \end{document} diff -r 71e50184d482 -r 4555c3814dd4 day1/session2.tex --- a/day1/session2.tex Tue Nov 03 11:13:38 2009 +0530 +++ b/day1/session2.tex Thu Nov 05 13:51:00 2009 +0530 @@ -265,6 +265,7 @@ \alert{\typ{list[initial:final]}} \end{frame} +%% more on list slicing \begin{frame}[fragile] \frametitle{List operations} \begin{lstlisting} diff -r 71e50184d482 -r 4555c3814dd4 day1/session3.tex --- a/day1/session3.tex Tue Nov 03 11:13:38 2009 +0530 +++ b/day1/session3.tex Thu Nov 05 13:51:00 2009 +0530 @@ -527,115 +527,4 @@ \end{lstlisting} \end{frame} -\begin{frame}[fragile] -\frametitle{Least Squares Fit} -\vspace{-0.15in} -\begin{figure} -\includegraphics[width=4in]{data/L-Tsq-Line.png} -\end{figure} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Least Squares Fit} -\vspace{-0.15in} -\begin{figure} -\includegraphics[width=4in]{data/L-Tsq-points.png} -\end{figure} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Least Squares Fit} -\vspace{-0.15in} -\begin{figure} -\includegraphics[width=4in]{data/least-sq-fit.png} -\end{figure} -\end{frame} - -\begin{frame} -\frametitle{Least Square Fit Curve} -\begin{itemize} -\item $T^2$ and $L$ have a linear relationship -\item Hence, Least Square Fit Curve is a line -\item we shall use the \typ{lstsq} function -\end{itemize} -\end{frame} - -\begin{frame}[fragile] -\frametitle{\typ{lstsq}} -\begin{itemize} -\item We need to fit a line through points for the equation $T^2 = m \cdot L+c$ -\item The equation can be re-written as $T^2 = A \cdot p$ -\item where A is - $\begin{bmatrix} - L_1 & 1 \\ - L_2 & 1 \\ - \vdots & \vdots\\ - L_N & 1 \\ - \end{bmatrix}$ - and p is - $\begin{bmatrix} - m\\ - c\\ - \end{bmatrix}$ -\item We need to find $p$ to plot the line -\end{itemize} -\end{frame} - -\subsection{Van der Monde matrix generation} -\begin{frame}[fragile] -\frametitle{Van der Monde Matrix} -\begin{itemize} -\item A is also called a Van der Monde matrix -\item It can be generated using \typ{vander} -\end{itemize} -\begin{lstlisting} -In []: A = vander(L, 2) -\end{lstlisting} -Gives the required Van der Monde matrix -\begin{equation*} - \begin{bmatrix} - l_1 & 1 \\ - l_2 & 1 \\ - \vdots & \vdots\\ - l_N & 1 \\ - \end{bmatrix} -\end{equation*} - -\end{frame} - -\begin{frame}[fragile] -\frametitle{\typ{lstsq} \ldots} -\begin{itemize} -\item Now use the \typ{lstsq} function -\item Along with a lot of things, it returns the least squares solution -\end{itemize} -\begin{lstlisting} -In []: coef, res, r, s = lstsq(A,TSq) -\end{lstlisting} -\end{frame} - -\subsection{Plotting} -\begin{frame}[fragile] -\frametitle{Least Square Fit Line \ldots} -We get the points of the line from \typ{coef} -\begin{lstlisting} -In []: Tline = coef[0]*L + coef[1] -\end{lstlisting} -\begin{itemize} -\item Now plot Tline vs. L, to get the Least squares fit line. -\end{itemize} -\begin{lstlisting} -In []: plot(L, Tline) -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{What did we learn?} - \begin{itemize} - \item Least square fit - \item Van der Monde matrix generation - \item Plotting the least square fit curve - \end{itemize} -\end{frame} - \end{document} diff -r 71e50184d482 -r 4555c3814dd4 day1/session5.tex --- a/day1/session5.tex Tue Nov 03 11:13:38 2009 +0530 +++ b/day1/session5.tex Thu Nov 05 13:51:00 2009 +0530 @@ -74,7 +74,7 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Title page -\title[Calculus]{Python for Science and Engg: Interpolation, Differentiation and Integration} +\title[]{} \author[FOSSEE] {FOSSEE} @@ -125,306 +125,5 @@ %% \end{frame} -\section{\typ{loadtxt}} - -\begin{frame}[fragile] - \frametitle{\typ{loadtxt}} - \begin{itemize} - \item Load data from a text file. - \item Each row must have same number of values. - \end{itemize} -\begin{lstlisting} -In []: data = loadtxt('pendulum.txt') -In []: L = data[:,0] -In []: T = data[:,1] -\end{lstlisting} -\end{frame} - -%% \begin{frame}[fragile] -%% \frametitle{\typ{loadtxt}} -%% \end{frame} - -\section{Interpolation} -\begin{frame}[fragile] -\frametitle{Interpolation} -\begin{itemize} - \item Given data file \typ{points.txt}. - \item It contains x,y position of particle. - \item Plot the given points. -%% \item Interpolate the missing region. -\end{itemize} -\emphbar{Loading data (revisited)} -\begin{lstlisting} -In []: data = loadtxt('points.txt') -In []: data.shape -Out[]: (40, 2) -In []: x = data[:,0] -In []: y = data[:,1] -In []: plot(x, y, '.') -\end{lstlisting} -\end{frame} - -%% \begin{frame}[fragile] -%% \frametitle{Interpolation \ldots} -%% \begin{small} -%% \typ{In []: from scipy.interpolate import interp1d} -%% \end{small} -%% \begin{itemize} -%% \item The \typ{interp1d} function returns a function -%% \begin{lstlisting} -%% In []: f = interp1d(L, T) -%% \end{lstlisting} -%% \item Functions can be assigned to variables -%% \item This function interpolates between known data values to obtain unknown -%% \end{itemize} -%% \end{frame} - -%% \begin{frame}[fragile] -%% \frametitle{Interpolation \ldots} -%% \begin{lstlisting} -%% In []: Ln = arange(0.1,0.99,0.005) -%% # Interpolating! -%% # The new values in range of old data -%% In []: plot(L, T, 'o', Ln, f(Ln), '-') -%% In []: f = interp1d(L, T, kind='cubic') -%% # When kind not specified, it's linear -%% # Others are ... -%% # 'nearest', 'zero', -%% # 'slinear', 'quadratic' -%% \end{lstlisting} -%% \end{frame} - -\begin{frame}[fragile] -\frametitle{Spline Interpolation} -\begin{small} -\begin{lstlisting} -In []: from scipy.interpolate import splrep -In []: from scipy.interpolate import splev -\end{lstlisting} -\end{small} -\begin{itemize} -\item Involves two steps - \begin{enumerate} - \item Find out the spline curve, coefficients - \item Evaluate the spline at new points - \end{enumerate} -\end{itemize} -\end{frame} - -\begin{frame}[fragile] -\frametitle{\typ{splrep}} -To find the spline curve -\begin{lstlisting} -In []: tck = splrep(x, y) -\end{lstlisting} -\typ{tck} contains parameters required for representing the spline curve! -\end{frame} - -\begin{frame}[fragile] -\frametitle{\typ{splev}} -To Evaluate a B-spline and it's derivatives -\begin{lstlisting} -In []: Xnew = arange(0.01,3,0.02) -In []: Ynew = splev(Xnew, tck) - -In []: y.shape -Out[]: (40,) - -In []: Ynew.shape -Out[]: (150,) - -In []: plot(Xnew, Ynew) -\end{lstlisting} - -\end{frame} - -%% \begin{frame}[fragile] -%% \frametitle{Interpolation \ldots} -%% \begin{itemize} -%% \item -%% \end{itemize} -%% \end{frame} - -\section{Differentiation} - -\begin{frame}[fragile] -\frametitle{Numerical Differentiation} -\begin{itemize} -\item Given function $f(x)$ or data points $y=f(x)$ -\item We wish to calculate $f^{'}(x)$ at points $x$ -\item Taylor series - finite difference approximations -\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 -\end{tabular} -\end{center} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Forward Difference} -\begin{lstlisting} -In []: x = linspace(0, 2*pi, 100) -In []: y = sin(x) -In []: deltax = x[1] - x[0] -\end{lstlisting} -Obtain the finite forward difference of y -\end{frame} - -\begin{frame}[fragile] -\frametitle{Forward Difference \ldots} -\begin{lstlisting} -In []: fD = (y[1:] - y[:-1]) / deltax -In []: plot(x, y, x[:-1], fD) -\end{lstlisting} -\begin{center} - \includegraphics[height=2in, interpolate=true]{data/fwdDiff} -\end{center} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Example} -\begin{itemize} -\item Given x, y positions of a particle in \typ{pos.txt} -\item Find velocity \& acceleration in x, y directions -\end{itemize} -\small{ -\begin{center} -\begin{tabular}{| c | c | c |} -\hline -$X$ & $Y$ \\ \hline -0. & 0.\\ \hline -0.25 & 0.47775\\ \hline -0.5 & 0.931\\ \hline -0.75 & 1.35975\\ \hline -1. & 1.764\\ \hline -1.25 & 2.14375\\ \hline -\vdots & \vdots\\ \hline -\end{tabular} -\end{center}} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Example \ldots} -\begin{itemize} -\item Read the file -\item Obtain an array of x, y -\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]) -\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 - -In []: v = (S[:,1:]-S[:,:-1])/deltaT - -In []: a = (v[:,1:]-v[:,:-1])/deltaT -\end{lstlisting} -Try Plotting the position, velocity \& acceleration. -\end{frame} - -\section{Quadrature} - -\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} -\begin{itemize} -\item Inputs - function to integrate, limits -\end{itemize} -\begin{lstlisting} -In []: x = 0 -In []: quad(sin(x)+x**2, 0, 1) -\end{lstlisting} -\begin{small} -\alert{\typ{error:}} -\typ{First argument must be a callable function.} -\end{small} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Functions - Definition} -We have been using them all along. Now let's see how to define them. -\begin{lstlisting} -In []: def f(x): - return sin(x)+x**2 -In []: quad(f, 0, 1) -\end{lstlisting} -\begin{itemize} -\item \typ{def} -\item name -\item arguments -\item \typ{return} -\end{itemize} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Functions - Calling them} -\begin{lstlisting} -In [15]: f() ---------------------------------------- -\end{lstlisting} -\alert{\typ{TypeError:}}\typ{f() takes exactly 1 argument} -\typ{(0 given)} -\begin{lstlisting} -In []: f(0) -Out[]: 0.0 -In []: f(1) -Out[]: 1.8414709848078965 -\end{lstlisting} -More on Functions later \ldots -\end{frame} - -\begin{frame}[fragile] -\frametitle{Quadrature \ldots} -\begin{lstlisting} -In []: quad(f, 0, 1) -\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 -\end{itemize} -\end{frame} - -\begin{frame} - \frametitle{Things we have learned} - \begin{itemize} - \item Interpolation - \item Differentiation - \item Functions - \begin{itemize} - \item Definition - \item Calling - \item Default Arguments - \item Keyword Arguments - \end{itemize} - \item Quadrature - \end{itemize} -\end{frame} - \end{document} diff -r 71e50184d482 -r 4555c3814dd4 day1/session6.tex --- a/day1/session6.tex Tue Nov 03 11:13:38 2009 +0530 +++ b/day1/session6.tex Thu Nov 05 13:51:00 2009 +0530 @@ -73,7 +73,7 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Title page -\title[ODEs \& Root Finding]{Python for Science and Engg:\\ODEs \& Finding Roots} +\title[Solving Equations \& ODEs]{Python for Science and Engg:\\Solving Equations \& ODEs} \author[FOSSEE] {FOSSEE} @@ -123,219 +123,166 @@ %% % You might wish to add the option [pausesections] %% \end{frame} -\section{ODEs} +\section{Solving linear equations} \begin{frame}[fragile] -\frametitle{ODE Integration} -We shall use the simple ODE of a simple pendulum. -\begin{equation*} -\ddot{\theta} = -\frac{g}{L}sin(\theta) -\end{equation*} -\begin{itemize} -\item This equation can be written as a system of two first order ODEs -\end{itemize} -\begin{align} -\dot{\theta} &= \omega \\ -\dot{\omega} &= -\frac{g}{L}sin(\theta) \\ - \text{At}\ t &= 0 : \nonumber \\ - \theta = \theta_0\quad & \&\quad \omega = 0 \nonumber -\end{align} +\frametitle{Solution of equations} +Consider, + \begin{align*} + 3x + 2y - z & = 1 \\ + 2x - 2y + 4z & = -2 \\ + -x + \frac{1}{2}y -z & = 0 + \end{align*} +Solution: + \begin{align*} + x & = 1 \\ + y & = -2 \\ + z & = -2 + \end{align*} \end{frame} \begin{frame}[fragile] -\frametitle{Solving ODEs using SciPy} -\begin{itemize} -\item We use the \typ{odeint} function from scipy to do the integration -\item Define a function as below -\end{itemize} +\frametitle{Solving using Matrices} +Let us now look at how to solve this using \kwrd{matrices} + \begin{lstlisting} + 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} + +\begin{frame}[fragile] +\frametitle{Solution:} \begin{lstlisting} -In []: def pend_int(unknown, t, p): - .... theta, omega = unknown - .... g, L = p - .... f=[omega, -(g/L)*sin(theta)] - .... return f - .... +In []: x +Out[]: +array([[ 1.], + [-2.], + [-2.]]) \end{lstlisting} \end{frame} \begin{frame}[fragile] -\frametitle{Solving ODEs using SciPy \ldots} +\frametitle{Let's check!} +\begin{lstlisting} +In []: Ax +Out[]: +array([[ 1.00000000e+00], + [ -2.00000000e+00], + [ 2.22044605e-16]]) +\end{lstlisting} +\begin{block}{} +The last term in the matrix is actually \alert{0}!\\ +We can use \kwrd{allclose()} to check. +\end{block} +\begin{lstlisting} +In []: allclose(Ax, b) +Out[]: True +\end{lstlisting} +\inctime{15} +\end{frame} + +\subsection{Exercises} + +\begin{frame}[fragile] +\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 \typ{t} is the time variable \\ -\item \typ{p} has the constants \\ -\item \typ{initial} has the initial values + \item[i] Transpose + \item[ii]Inverse + \item[iii]Determinant + \item[iv] Eigenvalues and Eigen vectors + \item[v] Singular Value decomposition \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{frame} \begin{frame}[fragile] -\frametitle{Solving ODEs using SciPy \ldots} -\begin{small} - \typ{In []: from scipy.integrate import odeint} -\end{small} -\begin{lstlisting} -In []: pend_sol = odeint(pend_int, - initial,t, - args=(p,)) -\end{lstlisting} +\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\\ + 2x + 5y - z - 9w & = -3\\ + 2x + y -z + 3w & = -11 \\ + x - 3y + 2z + 7w & = -5\\ +\end{align*} +\inctime{10} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Solution} +Use \kwrd{solve()} +\begin{align*} + x & = -5\\ + y & = 2\\ + z & = 3\\ + w & = 0\\ +\end{align*} \end{frame} \section{Finding Roots} \begin{frame}[fragile] -\frametitle{Roots of $f(x)=0$} -\begin{itemize} -\item Roots --- values of $x$ satisfying $f(x)=0$ -\item $f(x)=0$ may have real or complex roots -\item Presently, let's look at real roots -\end{itemize} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Roots of $f(x)=0$} -\begin{itemize} -\item Given function $cosx-x^2$ -\item First we find \alert{a} root in $(-\pi/2, \pi/2)$ -\item Then we find \alert{all} roots in $(-\pi/2, \pi/2)$ -\end{itemize} -\end{frame} - -%% \begin{frame}[fragile] -%% \frametitle{Fixed Point Method} -%% \begin{enumerate} -%% \item Convert $f(x)=0$ to the form $x=g(x)$ -%% \item Start with an initial value of $x$ and iterate successively -%% \item $x_{n+1}=g(x_n)$ and $x_0$ is our initial guess -%% \item Iterate until $x_{n+1}-x_n \le tolerance$ -%% \end{enumerate} -%% \end{frame} - -%% \begin{frame}[fragile] -%% \frametitle{Fixed Point \dots} -%% \begin{lstlisting} -%% In []: def our_g(x): -%% ....: return sqrt(cos(x)) -%% ....: -%% In []: tolerance = 1e-5 -%% In []: while abs(x1-x0)>tolerance: -%% ....: x0 = x1 -%% ....: x1 = our_g(x1) -%% ....: -%% In []: x0 -%% \end{lstlisting} -%% \end{frame} - -\begin{frame}[fragile] -\frametitle{Bisection Method} -\begin{enumerate} -\item Start with the given interval $(-\pi/2, \pi/2)$ ($(a, b)$) -\item $f(a)\cdot f(b) < 0$ -\item Bisect the interval. $c = \frac{a+b}{2}$ -\item Change the interval to $(a, c)$ if $f(a)\cdot f(c) < 0$ -\item Else, change the interval to $(c, b)$ -\item Go back to 3 until $(b-a) \le$ tolerance -\end{enumerate} -\alert{\typ{tolerance = 1e-5}} -\end{frame} - -%% \begin{frame}[fragile] -%% \frametitle{Bisection \dots} -%% \begin{lstlisting} -%% In []: tolerance = 1e-5 -%% In []: a = -pi/2 -%% In []: b = 0 -%% In []: while b-a > tolerance: -%% ....: c = (a+b)/2 -%% ....: if our_f(a)*our_f(c) < 0: -%% ....: b = c -%% ....: else: -%% ....: a = c -%% ....: -%% \end{lstlisting} -%% \end{frame} - -\begin{frame}[fragile] -\frametitle{Newton-Raphson Method} -\begin{enumerate} -\item Start with an initial guess of x for the root -\item $\Delta x = -f(x)/f^{'}(x)$ -\item $ x \leftarrow x + \Delta x$ -\item Go back to 2 until $|\Delta x| \le$ tolerance -\end{enumerate} -\end{frame} - -%% \begin{frame}[fragile] -%% \frametitle{Newton-Raphson \dots} -%% \begin{lstlisting} -%% In []: def our_df(x): -%% ....: return -sin(x)-2*x -%% ....: -%% In []: delx = 10*tolerance -%% In []: while delx > tolerance: -%% ....: delx = -our_f(x)/our_df(x) -%% ....: x = x + delx -%% ....: -%% ....: -%% \end{lstlisting} -%% \end{frame} - -%% \begin{frame}[fragile] -%% \frametitle{Newton-Raphson \ldots} -%% \begin{itemize} -%% \item What if $f^{'}(x) = 0$? -%% \item Can you write a better version of the Newton-Raphson? -%% \item What if the differential is not easy to calculate? -%% \item Look at Secant Method -%% \end{itemize} -%% \end{frame} - -\begin{frame}[fragile] -\frametitle{Initial Estimates} -\begin{itemize} -\item Given an interval -\item How to find \alert{all} the roots? -\end{itemize} -\begin{enumerate} -\item Check for change of signs of $f(x)$ in the given interval -\item A root lies in the interval where the sign change occurs -\end{enumerate} -\end{frame} - -\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) -\end{lstlisting} -\begin{itemize} -\item Get the intervals of x, where sign changes occur -\end{itemize} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Initial Estimates \ldots} -\begin{lstlisting} -In []: pos = y[:-1]*y[1:] < 0 -In []: rpos = zeros(x.shape, dtype=bool) -In []: rpos[:-1] = pos -In []: rpos[1:] += pos -In []: rts = x[rpos] -\end{lstlisting} -Now use Newton-Raphson? -\end{frame} - - -\begin{frame}[fragile] \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] @@ -360,29 +307,82 @@ \end{lstlisting} \end{frame} +%% \begin{frame}[fragile] +%% \frametitle{Scipy Methods \dots} +%% \begin{small} +%% \begin{lstlisting} +%% In []: from scipy.optimize import fixed_point + +%% In []: from scipy.optimize import bisect + +%% In []: from scipy.optimize import newton +%% \end{lstlisting} +%% \end{small} +%% \end{frame} + +\section{ODEs} \begin{frame}[fragile] -\frametitle{Scipy Methods \dots} -\begin{small} -\begin{lstlisting} -In []: from scipy.optimize import fixed_point +\frametitle{ODE Integration} +We shall use the simple ODE of a simple pendulum. +\begin{equation*} +\ddot{\theta} = -\frac{g}{L}sin(\theta) +\end{equation*} +\begin{itemize} +\item This equation can be written as a system of two first order ODEs +\end{itemize} +\begin{align} +\dot{\theta} &= \omega \\ +\dot{\omega} &= -\frac{g}{L}sin(\theta) \\ + \text{At}\ t &= 0 : \nonumber \\ + \theta = \theta_0(10^o)\quad & \&\quad \omega = 0\ (Initial\ values)\nonumber +\end{align} +\end{frame} -In []: from scipy.optimize import bisect +\begin{frame}[fragile] +\frametitle{Solving ODEs using SciPy} +\begin{itemize} +\item We use the \typ{odeint} function from scipy to do the integration +\item Define a function as below +\end{itemize} +\begin{lstlisting} +In []: def pend_int(initial, t): + .... theta, omega = initial + .... g, L = -9.81, 0.2 + .... f=[omega, -(g/L)*sin(theta)] + .... return f + .... +\end{lstlisting} +\end{frame} -In []: from scipy.optimize import newton +\begin{frame}[fragile] +\frametitle{Solving ODEs using SciPy \ldots} +\begin{itemize} +\item \typ{t} is the time variable \\ +\item \typ{initial} has the initial values +\end{itemize} +\begin{lstlisting} +In []: t = linspace(0, 10, 101) +In []: initial = [10*2*pi/360, 0] +\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{lstlisting} +In []: pend_sol = odeint(pend_int, + initial,t) \end{lstlisting} -\end{small} \end{frame} + \begin{frame} \frametitle{Things we have learned} \begin{itemize} \item Solving ODEs \item Finding Roots - \begin{itemize} - \item Estimating Interval - \item Newton-Raphson - \item Scipy methods - \end{itemize} \end{itemize} \end{frame} diff -r 71e50184d482 -r 4555c3814dd4 day1quiz1.tex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/day1quiz1.tex Thu Nov 05 13:51:00 2009 +0530 @@ -0,0 +1,160 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Tutorial slides on Python. +% +% Author: FOSSEE +% Copyright (c) 2005-2009, FOSSEE Team +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + +\documentclass[14pt,compress]{beamer} + +\mode +{ + \useoutertheme{split} + \setbeamercovered{transparent} +} + +\definecolor{darkgreen}{rgb}{0,0.5,0} + +\usepackage{listings} +\lstset{language=Python, + basicstyle=\ttfamily\bfseries, + commentstyle=\color{red}\itshape, + stringstyle=\color{darkgreen}, + showstringspaces=false, + keywordstyle=\color{blue}\bfseries} + +\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Macros + +\newcounter{qno} +\setcounter{qno}{0} +\newcommand{\incqno}{\addtocounter{qno}{1}{Question \theqno}} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Title page +\title[Basic Python]{Python: Quiz} + +\author[FOSSEE Team] {FOSSEE} + +\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} +\date[] {31, October 2009\\Day 1, Quiz 1} +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + +\begin{document} + +\begin{frame} + \titlepage +\end{frame} + +\begin{frame} + \frametitle{Write your details...} +On the top right hand corner please write down the following: + \begin{itemize} + \item Name: + \item Affliation: + \item Occupation: + \end{itemize} +\end{frame} + +\begin{frame} +\frametitle{\incqno } + A sample line from a Comma Separated Values (CSV) file:\\ + \vspace*{0.2in} + \emph{Rossum, Guido, 42, 56, 34, 54}\\ + \vspace*{0.2in} + What method would you use to separate the line into fields? +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } + \begin{lstlisting} + In [1]: a = [1, 2, 5, 9] + In [2]: a[:-1] + \end{lstlisting} + What is the output? +\end{frame} + +\begin{frame} +\frametitle{\incqno } + How do you combine the two lists \emph{a} and \emph{b}? +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } + \begin{lstlisting} + In [1]: d = { + 'a': 1, + 'b': 2 + } + In [2]: print d['c'] + \end{lstlisting} + What is the output? +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } + \begin{lstlisting} + for x in "abcd": + print x + + a + b + c + d + \end{lstlisting} + How do you get the following output? + \begin{lstlisting} + 0 a + 1 b + 2 c + 3 d + \end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +What would be the result? +\begin{lstlisting} + In [1]: x + array([[0, 1, 2], + [3, 4, 5], + [6, 7, 8]]) + In [2]: x[::-1,:] +\end{lstlisting} +\end{frame} + +\begin{frame} +\frametitle{\incqno } +How to read and print each line of a file. +\end{frame} + +\begin{frame} +\frametitle{\incqno } +How to get list of third column of a data file. +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +What is the output of: +\begin{lstlisting} +In []: x=linspace(0 , 2 * pi) +In []: plot(x, cos(x),'go') +\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} + diff -r 71e50184d482 -r 4555c3814dd4 day1quiz2.tex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/day1quiz2.tex Thu Nov 05 13:51:00 2009 +0530 @@ -0,0 +1,178 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Tutorial slides on Python. +% +% Author: FOSSEE +% Copyright (c) 2005-2009, FOSSEE Team +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + +\documentclass[14pt,compress]{beamer} + +\mode +{ + \useoutertheme{split} + \setbeamercovered{transparent} +} + +\definecolor{darkgreen}{rgb}{0,0.5,0} + +\usepackage{listings} +\lstset{language=Python, + basicstyle=\ttfamily\bfseries, + commentstyle=\color{red}\itshape, + stringstyle=\color{darkgreen}, + showstringspaces=false, + keywordstyle=\color{blue}\bfseries} + +\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Macros + +\newcounter{qno} +\setcounter{qno}{0} +\newcommand{\incqno}{\addtocounter{qno}{1}{Question \theqno}} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Title page +\title[Basic Python]{Python: Quiz} + +\author[FOSSEE Team] {FOSSEE} + +\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} +\date[] {11, October 2009\\Day 2} +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + +\begin{document} + +\begin{frame} + \titlepage +\end{frame} + +\begin{frame} + \frametitle{Write your details...} +On the top right hand corner please write down the following: + \begin{itemize} + \item Name: + \item Affliation: + \item Occupation: + \end{itemize} +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +\begin{lstlisting} + >>> x = array([[1,2,3,4],[3,4,2,5]]) + >>> x.shape + (2, 4) +\end{lstlisting} +Change the shape of \lstinline+x+ to (4,2) +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +\begin{lstlisting} + >>> x = array([[1,2,3,4]]) +\end{lstlisting} +How to change the third element of \lstinline+x+ to 0? +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +What would be the result? +\begin{lstlisting} + >>> y = arange(3) + >>> x = linspace(0,3,3) + >>> x-y +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +\begin{lstlisting} + >>> x = array([0, 1, 2, 3]) + >>> x.shape = 2,2 + >>> x + array([[0, 1], + [2, 3]]) + >>> x[::2,::2] +\end{lstlisting} +What is the output? +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +What would be the result? +\begin{lstlisting} + >>> x + array([[0, 1, 2], + [3, 4, 5], + [6, 7, 8]]) + >>> x[::-1,:] +\end{lstlisting} +Hint: +\begin{lstlisting} + >>> x = arange(9) + >>> x[::-1] + array([8, 7, 6, 5, 4, 3, 2, 1, 0]) +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +\begin{lstlisting} + >>> x + array([[ 0, 1, 2, 3], + [ 4, 5, 6, 7], + [ 8, 9, 10, 11], + [12, 13, 14, 15]]) +\end{lstlisting} +How will you get the following \lstinline+x+? +\begin{lstlisting} + array([[ 5, 7], + [ 9, 11]]) +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +\begin{lstlisting} + >>> x = array(([1,2,3,4],[2,3,4,5])) + >>> x[-2][-3] = 4 + >>> x +\end{lstlisting} +What will be printed? +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +What would be the output? +\begin{lstlisting} + >>> y = arange(4) + >>> x = array(([1,2,3,2],[1,3,6,0])) + >>> x + y +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +\begin{lstlisting} + >>> line = plot(x, sin(x)) +\end{lstlisting} +Use the \lstinline+set_linewidth+ method to set width of \lstinline+line+ to 2. +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +What would be the output? +\begin{lstlisting} + >>> x = arange(9) + >>> y = arange(9.) + >>> x == y +\end{lstlisting} +\end{frame} + + +\end{document} + diff -r 71e50184d482 -r 4555c3814dd4 day2/session3.tex --- a/day2/session3.tex Tue Nov 03 11:13:38 2009 +0530 +++ b/day2/session3.tex Thu Nov 05 13:51:00 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 + diff -r 71e50184d482 -r 4555c3814dd4 day2/session4.tex --- a/day2/session4.tex Tue Nov 03 11:13:38 2009 +0530 +++ b/day2/session4.tex Thu Nov 05 13:51:00 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) in () @@ -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} diff -r 71e50184d482 -r 4555c3814dd4 day2/session5.tex --- a/day2/session5.tex Tue Nov 03 11:13:38 2009 +0530 +++ b/day2/session5.tex Thu Nov 05 13:51:00 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. diff -r 71e50184d482 -r 4555c3814dd4 day2quiz2.tex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/day2quiz2.tex Thu Nov 05 13:51:00 2009 +0530 @@ -0,0 +1,178 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Tutorial slides on Python. +% +% Author: FOSSEE +% Copyright (c) 2005-2009, FOSSEE Team +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + +\documentclass[14pt,compress]{beamer} + +\mode +{ + \useoutertheme{split} + \setbeamercovered{transparent} +} + +\definecolor{darkgreen}{rgb}{0,0.5,0} + +\usepackage{listings} +\lstset{language=Python, + basicstyle=\ttfamily\bfseries, + commentstyle=\color{red}\itshape, + stringstyle=\color{darkgreen}, + showstringspaces=false, + keywordstyle=\color{blue}\bfseries} + +\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Macros + +\newcounter{qno} +\setcounter{qno}{0} +\newcommand{\incqno}{\addtocounter{qno}{1}{Question \theqno}} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Title page +\title[Basic Python]{Python: Quiz} + +\author[FOSSEE Team] {FOSSEE} + +\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} +\date[] {11, October 2009\\Day 2} +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + +\begin{document} + +\begin{frame} + \titlepage +\end{frame} + +\begin{frame} + \frametitle{Write your details...} +On the top right hand corner please write down the following: + \begin{itemize} + \item Name: + \item Affliation: + \item Occupation: + \end{itemize} +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +\begin{lstlisting} + >>> x = array([[1,2,3,4],[3,4,2,5]]) + >>> x.shape + (2, 4) +\end{lstlisting} +Change the shape of \lstinline+x+ to (4,2) +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +\begin{lstlisting} + >>> x = array([[1,2,3,4]]) +\end{lstlisting} +How to change the third element of \lstinline+x+ to 0? +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +What would be the result? +\begin{lstlisting} + >>> y = arange(3) + >>> x = linspace(0,3,3) + >>> x-y +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +\begin{lstlisting} + >>> x = array([0, 1, 2, 3]) + >>> x.shape = 2,2 + >>> x + array([[0, 1], + [2, 3]]) + >>> x[::2,::2] +\end{lstlisting} +What is the output? +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +What would be the result? +\begin{lstlisting} + >>> x + array([[0, 1, 2], + [3, 4, 5], + [6, 7, 8]]) + >>> x[::-1,:] +\end{lstlisting} +Hint: +\begin{lstlisting} + >>> x = arange(9) + >>> x[::-1] + array([8, 7, 6, 5, 4, 3, 2, 1, 0]) +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +\begin{lstlisting} + >>> x + array([[ 0, 1, 2, 3], + [ 4, 5, 6, 7], + [ 8, 9, 10, 11], + [12, 13, 14, 15]]) +\end{lstlisting} +How will you get the following \lstinline+x+? +\begin{lstlisting} + array([[ 5, 7], + [ 9, 11]]) +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +\begin{lstlisting} + >>> x = array(([1,2,3,4],[2,3,4,5])) + >>> x[-2][-3] = 4 + >>> x +\end{lstlisting} +What will be printed? +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +What would be the output? +\begin{lstlisting} + >>> y = arange(4) + >>> x = array(([1,2,3,2],[1,3,6,0])) + >>> x + y +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +\begin{lstlisting} + >>> line = plot(x, sin(x)) +\end{lstlisting} +Use the \lstinline+set_linewidth+ method to set width of \lstinline+line+ to 2. +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +What would be the output? +\begin{lstlisting} + >>> x = arange(9) + >>> y = arange(9.) + >>> x == y +\end{lstlisting} +\end{frame} + + +\end{document} +