Merged Mainline and Madhu branches.
--- a/day1/Session-1.tex Fri Oct 09 13:20:08 2009 +0530
+++ b/day1/Session-1.tex Fri Oct 09 13:20:23 2009 +0530
@@ -1,5 +1,5 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-% Tutorial slides on Python.
+%Tutorial slides on Python.
%
% Author: Prabhu Ramachandran <prabhu at aero.iitb.ac.in>
% Copyright (c) 2005-2009, Prabhu Ramachandran
@@ -33,7 +33,7 @@
\usepackage{listings}
\lstset{language=Python,
- basicstyle=\ttfamily,
+ basicstyle=\ttfamily\bfseries,
commentstyle=\color{red}\itshape,
stringstyle=\color{darkgreen},
showstringspaces=false,
@@ -134,12 +134,16 @@
% * Add slides on reference counting.
\section{Agenda}
-\begin{frame}{About the Workshop}
+\begin{frame}{About the Workshop, Day 1}
\begin{description}
\item[Day 1, Session 1] Sat 09:30--11:00
\item[Day 1, Session 2] Sat 11:15--12:45
\item[Day 1, Session 3] Sat 13:45--15:15
\item[Day 1, Session 4] Sat 15:30--17:00
+ \end{description}
+\end{frame}
+\begin{frame}{About the Workshop, Day 2}
+ \begin{description}
\item[Day 2, Quiz] Sun 09:00--09:30
\item[Day 2, Session 1] Sun 09:30--11:00
\item[Day 2, Session 2] Sun 11:15--12:45
@@ -151,21 +155,22 @@
\begin{frame}{About the Workshop}
\begin{block}{Intended Audience}
\begin{itemize}
- \item Aimed at Engg., Mathematics and Science teachers.
+ \item Engg., Mathematics and Science teachers.
\item Interested students from similar streams.
\end{itemize}
\end{block}
- \begin{block}{Goal}
- Successful participants will be able to use python as their scripting and problem solving language.
+ \begin{block}{Goal:}
+ Successful participants will be able to
+ \begin{itemize}
+ \item use Python as their scripting and problem solving language.
+ \item train the students to use Python for the same
+ \end{itemize}
\end{block}
\end{frame}
\begin{frame}{Checklist}
- \begin {block}{Live Python}
- Have you booted using the Live Python DVD?
- \end {block}
- \begin{block}{python}
+ \begin{block}{python}
Type python at the command line. Do you see version 2.5 or later?
\end{block}
\begin{block}{IPython}
@@ -308,7 +313,7 @@
\end{center}
\end{frame}
-\begin{frame}{More exercises?}
+\begin{frame}{More exercises}
\begin{center}
\begin{block}{Round sums}
How to round a number to the nearest 5 paise?\\
@@ -508,7 +513,7 @@
\end{block}
\end{frame}
-\begin{frame}[fragile]{Surprise! strings!!}
+\begin{frame}[fragile]{String formatting}
\begin{lstlisting}
In [11]: x, y = 1, 1.2
In [12]: 'x is %s, y is %s' %(x, y)
@@ -523,10 +528,15 @@
\end{frame}
\begin{frame}
- {Interlude}
+ {A classic problem}
\begin{block}
- {A classic problem}
- How to interchange values of two variables? Please note that the type of either variable is unknown and it is not necessary that both be of the same type even!
+ {Interchange values}
+ How to interchange values of two variables?
+ \end{block}
+ \pause
+ \begin{block}{Note:}
+ This Python idiom works for all types of variables.\\
+They need not be of the same type!
\end{block}
\inctime{30}
\end{frame}
@@ -568,10 +578,9 @@
\begin{itemize}
\item aka scripts
\item use your editor
- \item Note that white space is the way to specify blocks!
\item extension \typ{.py}
\item run with \texttt{python hello.py} at the command line
- \item in IPython\ldots
+ \item in IPython using \kwrd{\%run}
\end{itemize}
\end{frame}
@@ -627,10 +636,11 @@
\begin{frame}{So what have we learnt so far?}
\begin{itemize}
\item The interactive interpreter
- \item Basic Data Types-Numbers
- \item \typ{if/elif/else, while}
+ \item Basic Data Types
+ \item Creating and running a Python script
+ \item \kwrd{if/elif/else}
\item Simple IO
- \item Creating and running a Python script
+ \item Basic Looping with \kwrd{while}
\end{itemize}
\end{frame}
\end{document}
--- a/day1/Session-3.tex Fri Oct 09 13:20:08 2009 +0530
+++ b/day1/Session-3.tex Fri Oct 09 13:20:23 2009 +0530
@@ -33,7 +33,7 @@
\usepackage{listings}
\lstset{language=Python,
- basicstyle=\ttfamily,
+ basicstyle=\ttfamily\bfseries,
commentstyle=\color{red}\itshape,
stringstyle=\color{darkgreen},
showstringspaces=false,
@@ -86,12 +86,12 @@
%% Delete this, if you do not want the table of contents to pop up at
-%% the beginning of each subsection:
-\AtBeginSubsection[]
+%% the beginning of each section:
+\AtBeginSection[]
{
\begin{frame}<beamer>
\frametitle{Outline}
- \tableofcontents[currentsection,currentsubsection]
+ \tableofcontents[currentsection]
\end{frame}
}
@@ -110,19 +110,15 @@
\titlepage
\end{frame}
-\begin{frame}
- \frametitle{Outline}
- \tableofcontents
- % You might wish to add the option [pausesections]
-\end{frame}
+\section{\typ{for}, Lists and Tuples}
-\section{Python}
-
-\subsection{Problem Set based on Lists and Tuples}
-
-\begin{frame}
- {Problem set 3}
- As you can guess, idea is to use \kwrd{for}!
+\begin{frame}{Quick Recap}
+ \begin{itemize}
+ \item List indexing and slicing
+ \item The \kwrd{range()} function
+ \item \kwrd{for}
+ \item Iterating lists and tuples using \kwrd{for} and \kwrd{range()}
+ \end{itemize}
\end{frame}
\begin{frame}{Problem 3.1}
@@ -149,7 +145,7 @@
\end{frame}
\begin{frame}[fragile]
- \frametitle{Problem 3.4a (optional)}
+ \frametitle{Problem 3.4}
Use the \typ{linspace} function and generate a list of N tuples of the form\\
\typ{[($x_1$,f($x_1$)),($x_2$,f($x_2$)),\ldots,($x_N$,f($x_N$))]}\\for the following functions,\begin{itemize}
@@ -159,7 +155,7 @@
\end{frame}
\begin{frame}[fragile]
- \frametitle{Problem 3.4b (optional)}
+ \frametitle{Problem 3.5}
Using the tuples generated earlier, determine the intervals where the roots of the functions lie.
@@ -170,7 +166,7 @@
% TIME: 15 m, running 185m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\subsection{IO}
+\section{Parsing}
\begin{frame}[fragile]
\frametitle{Simple tokenizing and parsing}
@@ -190,6 +186,8 @@
\end{lstlisting}
\end{frame}
+\section{Input/Output}
+
\begin{frame}[fragile]
\frametitle{File handling}
\begin{lstlisting}
@@ -213,13 +211,59 @@
\begin{frame}[fragile]
\frametitle{File and \kwrd{for}}
\begin{lstlisting}
->>> f = open('/path/to/file_name')
+$ cat dummyfile
+One 1
+Two 2
+Three 3
+Four 4
+Five 5
+Six 6
+Seven 7
+Eight 8
+Nine 9
+Ten 10
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile]
+ \frametitle{File and \kwrd{for}}
+\begin{lstlisting}
+>>> f = open('dummyfile')
>>> for line in f:
... print line
...
\end{lstlisting}
\end{frame}
+\begin{frame}[fragile]
+ \frametitle{File and \kwrd{for}}
+\begin{lstlisting}
+In [1]: f = open('dummyfile')
+
+In [2]: for line in f:
+ ...: print line
+ ...:
+\end{lstlisting}
+
+\begin{columns}
+ \column{0.3\textwidth}
+
+\begin{lstlisting}
+One 1
+
+Two 2
+
+Three 3
+\end{lstlisting}
+ \column{0.6\textwidth}
+\pause
+\begin{block}{What happens when ...}
+the \kwrd{print line} is replaced by \kwrd{print line,}
+\end{block}
+\end{columns}
+\ldots
+\end{frame}
+
\begin{frame}{Problem 4.2}
The given file has lakhs of records in the form:\\
\typ{RGN;ID;NAME;MARK1;\ldots;MARK5;TOTAL;PFW}\\
@@ -239,7 +283,7 @@
% TIME: 30 m, running 215m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\subsection{Modules}
+\section{Modules}
\begin{frame}[fragile]
{Modules}
@@ -347,14 +391,15 @@
\inctime{25}
\end{frame}
+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% TIME: 25 m, running 230m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\subsection{Coding Style in Python}
+\section{Coding Style}
\begin{frame}{Readability and Consistency}
\begin{itemize}
- \item Readability Counts!-Code is read more often than its written.
+ \item Readability Counts!\\Code is read more often than its written.
\item Consistency!
\item Know when to be inconsistent.
\end{itemize}
@@ -393,12 +438,16 @@
\item Ending the docstrings
\item One liner docstrings
\end{itemize}
+More information at PEP8: http://www.python.org/dev/peps/pep-0008/
\inctime{10}
\end{frame}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% TIME: 10 m, running 240m
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\subsection{Objects}
-\begin{frame}{Objects in Python}
+\section{Objects}
+\begin{frame}{Objects in general}
\begin{itemize}
\item What is an Object? (Types and classes)
\item identity
@@ -407,8 +456,35 @@
\end{itemize}
\end{frame}
+\begin{frame}{Almost everything is an Object!}
+ \begin{itemize}
+ \item \typ{list}
+ \item \typ{tuple}
+ \item \typ{string}
+ \item \typ{dictionary}
+ \item \typ{function}
+ \item Of course, user defined class objects!
+ \end{itemize}
+\end {frame}
+
+\begin{frame}{Using Objects}
+ \begin{itemize}
+ \item Creating Objects: Initialization
+ \item Object Manipulation: Object methods and ``.'' operator
+ \end{itemize}
+\end{frame}
+
\begin{frame}[fragile]
- \frametitle{Why are they useful?}
+ \frametitle{IPython Examples}
+ \begin{lstlisting}
+In [1]: l = [] #Initialising a list object
+In [2]: l?
+In [3]: l.<tab>
+ \end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile]
+ \frametitle{Objects provide consistency}
\small
\begin{lstlisting}
for element in (1, 2, 3):
@@ -430,9 +506,17 @@
\inctime{10}
\end{frame}
-\begin{frame}
- \frametitle{What did we learn?}
- \tableofcontents
- % You might wish to add the option [pausesections]
+\section{Summary}
+
+\begin{frame}{What have we learnt so far?}
+ \begin{itemize}
+ \item Operating on lists and tuples using \kwrd{for}
+ \item Simple string tokenizing and parsing
+ \item Writing to and Reading from files using \kwrd{for}
+ \item Using and writing Python Modules
+ \item Coding Style
+ \item Objects in Python
+ \end{itemize}
\end{frame}
+
\end{document}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/day1/dummyfile Fri Oct 09 13:20:23 2009 +0530
@@ -0,0 +1,10 @@
+One 1
+Two 2
+Three 3
+Four 4
+Five 5
+Six 6
+Seven 7
+Eight 8
+Nine 9
+Ten 10
\ No newline at end of file
--- a/day1/handout.tex Fri Oct 09 13:20:08 2009 +0530
+++ b/day1/handout.tex Fri Oct 09 13:20:23 2009 +0530
@@ -670,4 +670,12 @@
squares = [i*i for i in range(1, 100)
if i % 10 in [1, 2, 5, 7]]
\end{verbatim}
+\newpage
+\section{Further Reference:}
+\begin{itemize}
+ \item Most referred and trusted material for learning \emph{Python} language is available at docs.python.org/tutorial/
+ \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 diveintopython.org/
+ \item How to Think Like a Computer Scientist: Learning with Python available at 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}
+\end{itemize}
\end{document}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/day1/links.tex Fri Oct 09 13:20:23 2009 +0530
@@ -0,0 +1,22 @@
+\documentclass[12pt]{article}
+\title{Links and References}
+\author{Asokan Pichai\\Prabhu Ramachandran}
+\begin{document}
+\maketitle
+\begin{itemize}
+ \item Most referred and trusted material for learning \emph{Python} language is available at \url{http://docs.python.org/tutorial/}
+ \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 To read more on strings refer to: \\ \url{http://docs.python.org/library/stdtypes.html#string-methods}
+ \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/}
+ \item User Guide for Mayavi is the best place to look for Mayavi Documentation, available at: \\ \url{http://code.enthought.com/projects/mayavi/docs/development/html/mayavi/}
+ \item Explore examples and plots based on matplotlib at \\ \url{http://matplotlib.sourceforge.net/examples/index.html}
+ \item One stop go for Sage is \url{http://www.sagemath.org/doc/}
+ \item Central page for all SymPy’s documentation is at \\ \url{http://docs.sympy.org/}
+ \item For videos from basics to advanced Python check out: \\ \url{http://www.archive.org/search.php?query=scipy09}
+
+\end{itemize}
+\end{document}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/day2/handout.tex Fri Oct 09 13:20:23 2009 +0530
@@ -0,0 +1,444 @@
+\documentclass[12pt]{article}
+\usepackage{amsmath}
+\title{Python Workshop\\Problems and Exercises}
+\author{Asokan Pichai\\Prabhu Ramachandran}
+\begin{document}
+\maketitle
+
+\section{Matrices and Arrays \& 2D Plotting}
+\subsection{Matrices and Arrays}
+\subsubsection{Basic Numpy}
+\begin{verbatim}
+# Simple array math example
+>>> import numpy as np
+>>> a = np.array([1,2,3,4])
+>>> b = np.arange(2,6)
+>>> b
+array([2,3,4,5])
+>>> a*2 + b + 1 # Basic math!
+array([5, 8, 11, 14])
+
+# Pi and e are defined.
+>>> x = np.linspace(0.0, 10.0, 1000)
+>>> x *= 2*np.pi/10 # inplace.
+# apply functions to array.
+>>> y = np.sin(x)
+>>> z = np.exp(y)
+
+>>> x = np.array([1., 2, 3, 4])
+>>> np.size(x)
+4
+>>> x.dtype # What is a.dtype?
+dtype('float64')
+>>> x.shape
+(4,)
+>>> print np.rank(x), x.itemsize
+1 8
+>>> x[0] = 10
+>>> print x[0], x[-1]
+10.0 4.0
+
+>>> a = np.array([[ 0, 1, 2, 3],
+... [10,11,12,13]])
+>>> a.shape # (rows, columns)
+(2, 4)
+# Accessing and setting values
+>>> a[1,3]
+13
+>>> a[1,3] = -1
+>>> a[1] # The second row
+array([10,11,12,-1])
+
+>>> b=np.array([[0,2,4,2],[1,2,3,4]])
+>>> np.add(a,b,a)
+>>> np.sum(x,axis=1)
+
+>>> np.greater(a,4)
+>>> np.sqrt(a)
+\end{verbatim}
+
+\subsubsection{Array Creation}
+\begin{verbatim}
+>>> np.array([2,3,4])
+array([2, 3, 4])
+
+>>> np.linspace(0, 2, 4)
+array([0.,0.6666667,1.3333333,2.])
+
+>>>np.ones([2,2])
+array([[ 1., 1.],
+ [ 1., 1.]])
+
+>>>a = np.array([[1,2,3],[4,5,6]])
+>>>np.ones_like(a)
+array([[1, 1, 1],
+ [1, 1, 1]])
+\end{verbatim}
+\subsubsection{Slicing, Striding Arrays}
+\begin{verbatim}
+>>> a = np.array([[1,2,3], [4,5,6],
+ [7,8,9]])
+>>> a[0,1:3]
+array([2, 3])
+>>> a[1:,1:]
+array([[5, 6],
+ [8, 9]])
+>>> a[:,2]
+array([3, 6, 9])
+>>> a[...,2]
+array([3, 6, 9])
+
+>>> a[0::2,0::2]
+array([[1, 3],
+ [7, 9]])
+# Slices are references to the
+# same memory!
+\end{verbatim}
+\subsubsection{Random Numbers}
+\begin{verbatim}
+>>> np.random.rand(3,2)
+array([[ 0.96276665, 0.77174861],
+ [ 0.35138557, 0.61462271],
+ [ 0.16789255, 0.43848811]])
+>>> np.random.randint(1,100)
+42
+\end{verbatim}
+
+\subsubsection{Problem Set}
+\begin{verbatim}
+ >>> from scipy import misc
+ >>> A=misc.imread(name)
+ >>> misc.imshow(A)
+\end{verbatim}
+\begin{enumerate}
+ \item Convert an RGB image to Grayscale. $ Y = 0.5R + 0.25G + 0.25B $.
+ \item Scale the image to 50\%.
+ \item Introduce some random noise.
+ \item Smooth the image using a mean filter.
+ \\\small{Each element in the array is replaced by mean of all the neighbouring elements}
+ \\\small{How fast does your code run?}
+\end{enumerate}
+
+\subsection{2D Plotting}
+\subsubsection{Basic 2D Plotting}
+\begin{verbatim}
+$ ipython -pylab
+>>> x = linspace(0, 2*pi, 1000)
+>>> plot(x, sin(x))
+>>> plot(x, sin(x), 'ro')
+>>> xlabel(r'$\chi$', color='g')
+# LaTeX markup!
+>>> ylabel(r'sin($\chi$)', color='r')
+>>> title('Simple figure', fontsize=20)
+>>> savefig('/tmp/test.eps')
+\end{verbatim}
+\subsubsection{Tweaking plots}
+\begin{verbatim}
+# Set properties of objects:
+>>> l, = plot(x, sin(x))
+# Why "l,"?
+>>> setp(l, linewidth=2.0, color='r')
+>>> l.set_linewidth(2.0)
+>>> draw() # Redraw.
+>>> setp(l) # Print properties.
+>>> clf() # Clear figure.
+>>> close() # Close figure.
+\end{verbatim}
+
+\subsubsection{Working with text}
+\begin{verbatim}
+>>> w = arange(-2,2,.1)
+>>> plot(w,exp(-(w*w))*cos)
+>>> ylabel('$f(\omega)$')
+>>> xlabel('$\omega$')
+>>> title(r"$f(\omega)=e^{-\omega^2}
+ cos({\omega^2})$")
+>>> annotate('maxima',xy=(0, 1),
+ xytext=(1, 0.8),
+ arrowprops=dict(
+ facecolor='black',
+ shrink=0.05))
+\end{verbatim}
+
+\subsubsection{Legends}
+\begin{verbatim}
+>>> x = linspace(0, 2*np.pi, 1000)
+>>> plot(x, cos(5*x), 'r--',
+ label='cosine')
+>>> plot(x, sin(5*x), 'g--',
+ label='sine')
+>>> legend()
+# Or use:
+>>> legend(['cosine', 'sine'])
+\end{verbatim}
+
+\subsubsection{Multiple figures}
+\begin{verbatim}
+>>> figure(1)
+>>> plot(x, sin(x))
+>>> figure(2)
+>>> plot(x, tanh(x))
+>>> figure(1)
+>>> title('Easy as 1,2,3')
+
+\end{verbatim}
+
+\subsubsection{Problem Set}
+ \begin{enumerate}
+ \item Write a function that plots any regular n-gon given n.
+ \item Consider the logistic map, $f(x) = kx(1-x)$, plot it for
+ $k=2.5, 3.5$ and $4$ in the same plot.
+
+ \item Consider the iteration $x_{n+1} = f(x_n)$ where $f(x) =
+ kx(1-x)$. Plot the successive iterates of this process.
+ \item Plot this using a cobweb plot as follows:
+ \begin{enumerate}
+ \item Start at $(x_0, 0)$
+ \item Draw line to $(x_i, f(x_i))$;
+ \item Set $x_{i+1} = f(x_i)$
+ \item Draw line to $(x_i, x_i)$
+ \item Repeat from 2 for as long as you want
+ \end{enumerate}
+\end{enumerate}
+
+\section{Advanced Numpy}
+
+\subsection{Broadcasting}
+\begin{verbatim}
+>>> a = np.arange(4)
+>>> b = np.arange(5)
+>>> a+b #Does this work?
+>>> a+3
+>>> c=np.array([3])
+>>> a+c #Works!
+>>> b+c #But how?
+>>> a.shape, b.shape, c.shape
+
+>>> a = np.arange(4)
+>>> a+3
+array([3, 4, 5, 6])
+
+>>> x = np.ones((3, 5))
+>>> y = np.ones(8)
+>>> (x[..., None] + y).shape
+(3, 5, 8)
+
+\end{verbatim}
+
+\subsection{Copies \& Views}
+\begin{verbatim}
+>>> a = np.array([[1,2,3],[4,5,6]])
+>>> b = a
+>>> b is a
+>>> b[0,0]=0; print a
+>>> c = a.view()
+>>> c is a
+>>> c.base is a
+>>> c.flags.owndata
+>>> d = a.copy()
+>>> d.base is a
+>>> d.flags.owndata
+
+>>> a = np.arange(1,9)
+>>> a.shape=3,3
+>>> b = a[0,1:3]
+>>> c = a[0::2,0::2]
+>>> a.flags.owndata
+>>> b.flags.owndata
+>>> b.base
+>>> c.base is a
+
+>>> b = a[np.array([0,1,2])]
+array([[1, 2, 3],
+ [4, 5, 6],
+ [7, 8, 9]])
+>>> b.flags.owndata
+>>> abool=np.greater(a,2)
+>>> c = a[abool]
+>>> c.flags.owndata
+
+\end{verbatim}
+
+\section{Scipy}
+\subsection{Linear Algebra}
+\begin{verbatim}
+>>> import scipy as sp
+>>> from scipy import linalg
+>>> A=sp.mat(np.arange(1,10))
+>>> A.shape=3,3
+>>> linalg.inv(A)
+>>> linalg.det(A)
+>>> linalg.norm(A)
+>>> linalg.expm(A) #logm
+>>> linalg.sinm(A) #cosm, tanm, ...
+
+>>> A = sp.mat(np.arange(1,10))
+>>> A.shape=3,3
+>>> linalg.lu(A)
+>>> linalg.eig(A)
+>>> linalg.eigvals(A)
+\end{verbatim}
+
+\subsection{Solving Linear Equations}
+
+\begin{align*}
+ 3x + 2y - z & = 1 \\
+ 2x - 2y + 4z & = -2 \\
+ -x + \frac{1}{2}y -z & = 0
+\end{align*}
+To Solve this,
+\begin{verbatim}
+>>> A = sp.mat([[3,2,-1],[2,-2,4]
+ ,[-1,1/2,-1]])
+>>> B = sp.mat([[1],[-2],[0]])
+>>> linalg.solve(A,B)
+\end{verbatim}
+
+\subsection{Integrate}
+\subsubsection{Quadrature}
+Calculate the area under $(sin(x) + x^2)$ in the range $(0,1)$
+\begin{verbatim}
+>>> def f(x):
+ return np.sin(x)+x**2
+>>> integrate.quad(f, 0, 1)
+\end{verbatim}
+
+\subsubsection{ODE Integration}
+Numerically solve ODEs\\
+\begin{align*}
+\frac{dx}{dt} &=-e^{-t}x^2\\
+ x(0) &=2
+\end{align*}
+\begin{verbatim}
+>>> def dx_dt(x,t):
+... return -np.exp(-t)*x**2
+>>> x=integrate.odeint(dx_dt, 2, t)
+>>> plt.plot(x,t)
+\end{verbatim}
+
+\subsection{Interpolation}
+\subsubsection{1D Interpolation}
+\begin{verbatim}
+>>> from scipy import interpolate
+>>> interpolate.interp1d?
+>>> x = np.arange(0,2*np.pi,np.pi/4)
+>>> y = np.sin(x)
+>>> fl = interpolate.interp1d(
+ x,y,kind='linear')
+>>> fc = interpolate.interp1d(
+ x,y,kind='cubic')
+>>> fl(np.pi/3)
+>>> fc(np.pi/3)
+\end{verbatim}
+
+\subsubsection{Splines}
+Plot the Cubic Spline of $sin(x)$
+\begin{verbatim}
+>>> x = np.arange(0,2*np.pi,np.pi/4)
+>>> y = np.sin(x)
+>>> tck = interpolate.splrep(x,y)
+>>> X = np.arange(0,2*np.pi,np.pi/50)
+>>> Y = interpolate.splev(X,tck,der=0)
+>>> plt.plot(x,y,'o',x,y,X,Y)
+>>> plt.show()
+\end{verbatim}
+
+\subsection{Signal \& Image Processing}
+Applying a simple median filter
+\begin{verbatim}
+>>> from scipy import signal, ndimage
+>>> from scipy import lena
+>>> A=lena().astype('float32')
+>>> B=signal.medfilt2d(A)
+>>> imshow(B)
+\end{verbatim}
+Zooming an array - uses spline interpolation
+\begin{verbatim}
+>>> b=ndimage.zoom(A,0.5)
+>>> imshow(b)
+\end{verbatim}
+
+\section{3D Data Visualization}
+\subsection{Using mlab}
+\begin{verbatim}
+>>> from enthought.mayavi import mlab
+
+>>> mlab.test_<TAB>
+>>> mlab.test_contour3d()
+>>> mlab.test_contour3d??
+\end{verbatim}
+
+\subsubsection{Plotting Functions}
+\begin{verbatim}
+>>> from numpy import *
+>>> t = linspace(0, 2*pi, 50)
+>>> u = cos(t)*pi
+>>> x, y, z = sin(u), cos(u), sin(t)
+
+>>> x = mgrid[-3:3:100j,-3:3:100j]
+>>> z = sin(x*x + y*y)
+>>> mlab.surf(x, y, z)
+\end{verbatim}
+
+\subsubsection{Large 2D Data}
+\begin{verbatim}
+>>> mlab.mesh(x, y, z)
+
+>>> phi, theta = numpy.mgrid[0:pi:20j,
+... 0:2*pi:20j]
+>>> x = sin(phi)*cos(theta)
+>>> y = sin(phi)*sin(theta)
+>>> z = cos(phi)
+>>> mlab.mesh(x, y, z,
+... representation=
+... 'wireframe')
+\end{verbatim}
+
+\subsubsection{Large 3D Data}
+\begin{verbatim}
+>>> x, y, z = ogrid[-5:5:64j,
+... -5:5:64j,
+... -5:5:64j]
+>>> mlab.contour3d(x*x*0.5 + y*y +
+ z*z*2)
+
+>>> mlab.test_quiver3d()
+\end{verbatim}
+
+\subsection{Motivational Problem}
+Atmospheric data of temperature over the surface of the earth. Let temperature ($T$) vary linearly with height ($z$)\\
+$T = 288.15 - 6.5z$
+
+\begin{verbatim}
+lat = linspace(-89, 89, 37)
+lon = linspace(0, 360, 37)
+z = linspace(0, 100, 11)
+\end{verbatim}
+
+\begin{verbatim}
+x, y, z = mgrid[0:360:37j,-89:89:37j,
+ 0:100:11j]
+t = 288.15 - 6.5*z
+mlab.contour3d(x, y, z, t)
+mlab.outline()
+mlab.colorbar()
+\end{verbatim}
+
+\subsection{Lorenz equation}
+\begin{eqnarray*}
+\frac{d x}{dt} &=& s (y-x)\\
+\frac{d y}{d t} &=& rx -y -xz\\
+\frac{d z}{d t} &=& xy - bz\\
+\end{eqnarray*}
+
+Let $s=10,$
+$r=28,$
+$b=8./3.$
+
+\begin{verbatim}
+x, y, z = mgrid[-50:50:20j,-50:50:20j,
+ -10:60:20j]
+
+\end{verbatim}
+
+\end{document}
--- a/day2/session1.tex Fri Oct 09 13:20:08 2009 +0530
+++ b/day2/session1.tex Fri Oct 09 13:20:23 2009 +0530
@@ -134,10 +134,8 @@
\item Why?
\item What:
\begin{itemize}
- \item An efficient and powerful array type for various common data
- types
- \item Abstracts out the most commonly used standard operations on
- arrays
+ \item An efficient and powerful array type for various common data types
+ \item Abstracts out the most commonly used standard operations on arrays
\end{itemize}
\end{itemize}
\end{frame}
@@ -146,16 +144,25 @@
\frametitle{Examples of \num}
\begin{lstlisting}
# Simple array math example
->>> from numpy import *
->>> a = array([1,2,3,4])
->>> b = array([2,3,4,5])
+>>> import numpy as np
+>>> a = np.array([1,2,3,4])
+>>> b = np.arange(2,6)
+>>> b
+array([2,3,4,5])
>>> a*2 + b + 1 # Basic math!
array([5, 8, 11, 14])
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile]
+ \frametitle{Examples of \num}
+\begin{lstlisting}
# Pi and e are defined.
->>> x = linspace(0.0, 10.0, 1000)
->>> x *= 2*pi/10 # inplace.
+>>> x = np.linspace(0.0, 10.0, 1000)
+>>> x *= 2*np.pi/10 # inplace.
# apply functions to array.
->>> y = sin(x)
+>>> y = np.sin(x)
+>>> z = np.exp(y)
\end{lstlisting}
\inctime{5}
\end{frame}
@@ -178,14 +185,14 @@
\frametitle{More examples of \num}
\vspace*{-8pt}
\begin{lstlisting}
->>> x = array([1., 2, 3, 4])
->>> size(x)
+>>> x = np.array([1., 2, 3, 4])
+>>> np.size(x)
4
>>> x.dtype # What is a.dtype?
dtype('float64')
>>> x.shape
(4,)
->>> print rank(x), x.itemsize
+>>> print np.rank(x), x.itemsize
1 8
>>> x[0] = 10
>>> print x[0], x[-1]
@@ -196,7 +203,7 @@
\begin{frame}[fragile]
\frametitle{Multi-dimensional arrays}
\begin{lstlisting}
->>> a = array([[ 0, 1, 2, 3],
+>>> a = np.array([[ 0, 1, 2, 3],
... [10,11,12,13]])
>>> a.shape # (rows, columns)
(2, 4)
@@ -206,78 +213,85 @@
>>> a[1,3] = -1
>>> a[1] # The second row
array([10,11,12,-1])
-
\end{lstlisting}
\end{frame}
+
\begin{frame}[fragile]
\frametitle{Array math}
\begin{itemize}
\item Basic \alert{elementwise} math (given two arrays \typ{a, b}):
\typ{+, -, *, /, \%}
- \item Inplace operators: \typ{a += b}, or \typ{add(a, b, a)} etc.
- \item \typ{sum(x, axis=0)},
- \typ{product(x, axis=0)},
- \typ{dot(a, bp)}
+ \item Inplace operators: \typ{a += b}, or \typ{np.add(a, b, a)} etc.
+ \item \typ{np.sum(x, axis=0)},
+ \typ{np.product(x, axis=0)},
+ \typ{np.dot(a, bp)}
\end{itemize}
+\begin{lstlisting}
+>>> b=np.array([[0,2,4,2],[1,2,3,4]])
+>>> np.add(a,b,a)
+>>> np.sum(x,axis=1)
+\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Array math cont.}
\begin{itemize}
- \item Logical operations: \typ{equal (==)}, \typ{not\_equal (!=)},
- \typ{less (<)}, \typ{greater (>)} etc.
- \item Trig and other functions: \typ{sin(x),}
- \typ{arcsin(x), sinh(x),}
- \typ{exp(x), sqrt(x)} etc.
+ \item Logical operations: \typ{np.equal (==)}, \typ{np.not\_equal (!=)},
+ \typ{np.less (<)}, \typ{np.greater (>)} etc.
+ \item Trig and other functions: \typ{np.sin(x),}
+ \typ{np.arcsin(x), np.sinh(x),}
+ \typ{np.exp(x), np.sqrt(x)} etc.
\end{itemize}
+\begin{lstlisting}
+>>> np.greater(a,4)
+>>> np.sqrt(a)
+\end{lstlisting}
\inctime{10}
\end{frame}
\subsection{Array Creation \& Slicing, Striding Arrays}
\begin{frame}[fragile]
\frametitle{Array creation functions}
- \begin {block}{\typ{array(object, dtype=None, ...)}}
- \begin{lstlisting}
- >>> array( [2,3,4] )
- array([2, 3, 4])
- \end{lstlisting}
- \end {block}
- \begin{block}{\typ{linspace(start, stop, num=50, ...)}}
- \begin{lstlisting}
- >>> linspace( 0, 2, 4 )
- array([0.,0.6666667,1.3333333,2.])
- \end{lstlisting}
- \end{block}
\begin{itemize}
- \item also try \typ{arange} command
+ \item {\typ{np.array(object,dtype=None,...)}
+ \begin{lstlisting}
+>>> np.array([2,3,4])
+array([2, 3, 4])
+ \end{lstlisting}
+ \item \typ{np.linspace(start,stop,...)}
+ \begin{lstlisting}
+>>> np.linspace(0, 2, 4)
+array([0.,0.6666667,1.3333333,2.])
+ \end{lstlisting}
+ \item Also try \typ{np.arange}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Array creation functions cont.}
\begin{itemize}
- \item \typ{ones(shape, dtype=None, ...)}
+ \item \typ{np.ones(shape, dtype=None, ...)}
\begin{lstlisting}
- >>>ones([2,2])
- array([[ 1., 1.],
- [ 1., 1.]])
+>>>np.ones([2,2])
+array([[ 1., 1.],
+ [ 1., 1.]])
\end{lstlisting}
- \item \typ{identity(n)}
- \item \typ{ones\_like(x)}
+ \item \typ{np.identity(n)}
+ \item \typ{np.ones\_like(x)}
\begin{lstlisting}
- >>>a = array([[1,2,3],[4,5,6]])
- >>>ones_like(a)
- array([[1, 1, 1],
- [1, 1, 1]])
+>>>a = np.array([[1,2,3],[4,5,6]])
+>>>np.ones_like(a)
+array([[1, 1, 1],
+ [1, 1, 1]])
\end{lstlisting}
- \item check out \typ{zeros, zeros\_like, empty}
+ \item Also try \typ{zeros, zeros\_like, empty}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Slicing arrays}
\begin{lstlisting}
->>> a = array([[1,2,3], [4,5,6],
+>>> a = np.array([[1,2,3], [4,5,6],
[7,8,9]])
>>> a[0,1:3]
array([2, 3])
@@ -327,8 +341,8 @@
\item Scale the image to 50\%
\item Introduce some random noise
\item Smooth the image using a mean filter
- \\\small{Take the mean of all the neighbouring elements}
- \\\small{How fast can you do it?}
+ \\\small{Each element in the array is replaced by mean of all the neighbouring elements}
+ \\\small{How fast does your code run?}
\end{enumerate}
\inctime{15}
\end{frame}
@@ -408,7 +422,7 @@
\begin{frame}[fragile]
\frametitle{Legends}
\begin{lstlisting}
->>> x = linspace(0, 2*pi, 1000)
+>>> x = linspace(0, 2*np.pi, 1000)
>>> plot(x, cos(5*x), 'r--',
label='cosine')
>>> plot(x, sin(5*x), 'g--',
@@ -435,7 +449,7 @@
\end{frame}
\begin{frame}[fragile]
- \frametitle{Note: \typ{pylab} in Python scripts}
+ \frametitle{\typ{pylab} in Python scripts}
\begin{lstlisting}
import pylab
x = pylab.linspace(0, 20, 1000)
@@ -444,7 +458,7 @@
# Can also use:
from pylab import linspace, sin, plot
\end{lstlisting}
-\inctime{5}
+\inctime{10}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -742,7 +756,6 @@
\tiny
For details see \url{http://matplotlib.sourceforge.net/screenshots/plotmap.py}
\end{center}
-\inctime{5}
\end{frame}
@@ -753,6 +766,7 @@
\item \url{http://matplotlib.sf.net/tutorial.html}
\item \url{http://matplotlib.sf.net/screenshots.html}
\end{itemize}
+\inctime{5}
\end{frame}
\begin{frame}
@@ -787,4 +801,11 @@
\end{columns}
\inctime{20}
\end{frame}
+\begin{frame}{Summary}
+ \begin{itemize}
+ \item Basics of Numpy.
+ \item Array operations.
+ \item Plotting in 2D.
+ \end{itemize}
+\end{frame}
\end{document}
--- a/day2/session2.tex Fri Oct 09 13:20:08 2009 +0530
+++ b/day2/session2.tex Fri Oct 09 13:20:23 2009 +0530
@@ -116,17 +116,20 @@
\begin{frame}
\maketitle
\end{frame}
+
+\section{Advanced Numpy}
\begin{frame}[fragile]
\frametitle{Broadcasting}
Try it!
\begin{lstlisting}
>>> a = np.arange(4)
>>> b = np.arange(5)
- >>> a+b
+ >>> a+b #Does this work?
>>> a+3
>>> c=np.array([3])
- >>> a+c
- >>> b+c
+ >>> a+c #Works!
+ >>> b+c #But how?
+ >>> a.shape, b.shape, c.shape
\end{lstlisting}
\begin{itemize}
\item Enter Broadcasting!
@@ -147,7 +150,7 @@
\includegraphics[height=0.7in, interpolate=true]{data/broadcast_scalar}
\end{columns}
\begin{itemize}
- \item Allows functions to take inputs not of the same shape
+ \item Allows functions to take inputs that are not of the same shape
\item 2 rules -
\begin{enumerate}
\item 1 is (repeatedly) prepended to shapes of smaller arrays
@@ -174,8 +177,9 @@
\begin{frame}[fragile]
\frametitle{Copies \& Views}
Try it!
+ \vspace{-0.1in}
\begin{lstlisting}
- >>> a = np.array([[1,2,3],[4,5,6]])
+ >>> a = np.arange(1,9); a.shape=3,3
>>> b = a
>>> b is a
>>> b[0,0]=0; print a
@@ -192,9 +196,8 @@
\begin{frame}[fragile]
\frametitle{Copies \& Views}
Try it!
+ \vspace{-0.1in}
\begin{lstlisting}
- >>> a = np.arange(1,9)
- >>> a.shape=3,3
>>> b = a[0,1:3]
>>> c = a[0::2,0::2]
>>> a.flags.owndata
@@ -226,6 +229,8 @@
\inctime{15}
\end{frame}
+\section{SciPy}
+\subsection{Introduction}
\begin{frame}
{Intro to SciPy}
\begin{itemize}
@@ -266,6 +271,7 @@
\end{lstlisting}
\end{frame}
+\subsection{Linear Algebra}
\begin{frame}[fragile]
\frametitle{Linear Algebra}
Try it!
@@ -311,6 +317,7 @@
\inctime{15}
\end{frame}
+\subsection{Integration}
\begin{frame}[fragile]
\frametitle{Integrate}
\begin{itemize}
@@ -318,7 +325,7 @@
\item Integrating Functions given fixed samples
\item Numerical integrators of ODE systems
\end{itemize}
- Calculate $\int^1_0(sin(x) + x^2)dx$
+ Calculate the area under $(sin(x) + x^2)$ in the range $(0,1)$
\begin{lstlisting}
>>> def f(x):
return np.sin(x)+x**2
@@ -331,48 +338,50 @@
Numerically solve ODEs\\
\begin{align*}
\frac{dx}{dt}&=-e^{-t}x^2\\
- x(0)&=2
+ x&=2 \quad at \ t=0
\end{align*}
\begin{lstlisting}
- def dx_dt(x,t):
+>>> def dx_dt(x,t):
return -np.exp(-t)*x**2
-
- x=integrate.odeint(dx_dt, 2, t)
- plt.plot(x,t)
+>>> t=np.linspace(0,2,100)
+>>> x=integrate.odeint(dx_dt, 2, t)
+>>> plt.plot(x,t)
\end{lstlisting}
\inctime{10}
\end{frame}
+\subsection{Interpolation}
\begin{frame}[fragile]
\frametitle{Interpolation}
Try it!
\begin{lstlisting}
- >>> from scipy import interpolate
- >>> interpolate.interp1d?
- >>> x = np.arange(0,2*np.pi,np.pi/4)
- >>> y = np.sin(x)
- >>> fl = interpolate.interp1d(x,y,kind='linear')
- >>> fc = interpolate.interp1d(x,y,kind='cubic')
- >>> fl(np.pi/3)
- >>> fc(np.pi/3)
+>>> from scipy import interpolate
+>>> interpolate.interp1d?
+>>> x = np.arange(0,2*np.pi,np.pi/4)
+>>> y = np.sin(x)
+>>> fl = interpolate.interp1d(
+ x,y,kind='linear')
+>>> fc = interpolate.interp1d(
+ x,y,kind='cubic')
+>>> fl(np.pi/3)
+>>> fc(np.pi/3)
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Interpolation - Splines}
- Cubic Spline of $sin(x)$
+ Plot the Cubic Spline of $sin(x)$
\begin{lstlisting}
- x = np.arange(0,2*np.pi,np.pi/4)
- y = np.sin(x)
- tck = interpolate.splrep(x,y)
- X = np.arange(0,2*np.pi,np.pi/50)
- Y = interpolate.splev(X,tck,der=0)
- plt.plot(x,y,'o',x,y,X,Y)
- plt.show()
+>>> tck = interpolate.splrep(x,y)
+>>> X = np.arange(0,2*np.pi,np.pi/50)
+>>> Y = interpolate.splev(X,tck,der=0)
+>>> plt.plot(x,y,'o',x,y,X,Y)
+>>> plt.show()
\end{lstlisting}
\inctime{10}
\end{frame}
+\subsection{Signal Processing}
\begin{frame}[fragile]
\frametitle{Signal \& Image Processing}
\begin{itemize}
@@ -393,16 +402,16 @@
\frametitle{Signal \& Image Processing}
Applying a simple median filter
\begin{lstlisting}
- from scipy import signal, ndimage
- from scipy import lena
- A=lena().astype('float32')
- B=signal.medfilt2d(A)
- imshow(B)
+>>> from scipy import signal, ndimage
+>>> from scipy import lena
+>>> A=lena().astype('float32')
+>>> B=signal.medfilt2d(A)
+>>> imshow(B)
\end{lstlisting}
Zooming an array - uses spline interpolation
\begin{lstlisting}
- b=ndimage.zoom(A,0.5)
- imshow(b)
+>>> b=ndimage.zoom(A,0.5)
+>>> imshow(b)
\end{lstlisting}
\inctime{5}
\end{frame}
@@ -413,10 +422,21 @@
\begin{equation*}
\frac{d^2x}{dt^2}+\mu(x^2-1)\frac{dx}{dt}+x= 0
\end{equation*}
-\inctime{25}
+ Make a plot of $\frac{dx}{dt}$ vs. $x$.
+\inctime{30}
\end{frame}
-
-
+\begin{frame}{Summary}
+ \begin{itemize}
+ \item Advanced NumPy
+ \item SciPy
+ \begin{itemize}
+ \item Linear Algebra
+ \item Integration
+ \item Interpolation
+ \item Signal and Image processing
+ \end{itemize}
+ \end{itemize}
+\end{frame}
\end{document}
- Numpy arrays (30 mins)
@@ -424,5 +444,3 @@
- random number generation.
- Image manipulation: jigsaw puzzle.
- Monte-carlo integration.
-
-
--- a/day2/session3.tex Fri Oct 09 13:20:08 2009 +0530
+++ b/day2/session3.tex Fri Oct 09 13:20:23 2009 +0530
@@ -192,9 +192,9 @@
\inctime{10}
\end{frame}
-\section{Tools at your disposal:}
+\section{Tools at your disposal}
-\subsection{Mayavi2.0}
+\subsection{Mayavi2}
\begin{frame}
\frametitle{Introduction to Mayavi}
@@ -280,7 +280,7 @@
\end{frame}
\begin{frame}[fragile]
- \frametitle{Using mlab:}
+ \frametitle{Using mlab}
\begin{lstlisting}
>>> from enthought.mayavi import mlab
@@ -288,7 +288,7 @@
\vspace*{0.5in}
- \myemph{\Large Try these:}
+ \myemph{\Large Try these}
\vspace*{0.25in}
--- a/day2/tda.tex Fri Oct 09 13:20:08 2009 +0530
+++ b/day2/tda.tex Fri Oct 09 13:20:23 2009 +0530
@@ -261,7 +261,7 @@
\begin{frame}[fragile]
\frametitle{Exercise}
- Based on Euclid's theorem:
+ Based on Euclid's algorithm:
$gcd(a,b)=gcd(b,b\%a)$\\
gcd function can be written as:
\begin{lstlisting}
@@ -269,14 +269,15 @@
if a%b == 0: return b
return gcd(b, a%b)
\end{lstlisting}
+ \vspace*{-0.15in}
\begin{block}{Task}
- For given gcd implementation write
- at least two tests.
- \end{block}
- \begin{block}{Task}
- Write a non recursive implementation
+ \begin{itemize}
+ \item Write at least
+ two tests for above mentioned function.
+ \item Write a non recursive implementation
of gcd(), and test it using already
written tests.
+ \end{itemize}
\end{block}
\inctime{15}