Merged branches.
authorvattam@chiquitita
Thu, 05 Nov 2009 13:51:00 +0530
changeset 276 4555c3814dd4
parent 275 71e50184d482 (diff)
parent 273 c378d1ffb1d1 (current diff)
child 278 5d680ab63dde
Merged branches.
day1/session2.tex
--- a/day1/session2.tex	Thu Nov 05 13:13:58 2009 +0530
+++ b/day1/session2.tex	Thu Nov 05 13:51:00 2009 +0530
@@ -1,4 +1,4 @@
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %Tutorial slides on Python.
 %
 % Author: FOSSEE
@@ -124,12 +124,9 @@
 \end{frame}
 
 \begin{frame}
-\frametitle{Why we didn't close the IPython??}
-\begin{itemize}
-  \item IPython provides a convenient feature
-  \item To go back, edit, and re-run commands
-  \item But when you close, this is lost
-\end{itemize}
+\frametitle{Why we didn't close IPython?}
+  IPython provides a convenient feature to go back, edit, and re-run commands.\\
+  \alert{But when you close, all this is lost.}
 \end{frame}
 
 \begin{frame}
@@ -148,7 +145,7 @@
 \begin{frame}[fragile]
 \frametitle{Python Scripts}
 \begin{itemize}
-\item Put all commands used in review problem into a file. 
+\item Put commands used in review problem into file. 
 \item use hist command of IPython.
 \end{itemize}
 \begin{lstlisting}
@@ -161,17 +158,18 @@
 \frametitle{Python Scripts\ldots}
   \begin{itemize}
     \item Open a new file in an \alert{editor}
-    \item Copy and paste required lines from the output of \typ{\%hist -n}
+    \item Copy and paste from the output of \typ{\%hist -n}
     \item Save the file as \typ{sine_plot.py}
   \end{itemize}
   \begin{itemize}
-  \item run the file in IPython using \typ{\%run sine_plot.py}\\
+  \item run the file in IPython using \typ{\%run -i sine_plot.py}\\
   \end{itemize}
 \end{frame}
 
 \begin{frame}[fragile]
 \frametitle{Why would I plot f(x)?}
 How often do we plot analytical functions?\\We plot experimental data more.
+\begin{small}
 \begin{lstlisting}
 In []: x = [0, 1, 2, 3]
 
@@ -179,14 +177,21 @@
 
 In []: plot(x, y)
 Out[]: [<matplotlib.lines.Line2D object at 0xa73aa8c>]
+
+In []: xlabel('X')
+Out[]: <matplotlib.text.Text object at 0x986e9ac>
+
+In []: ylabel('Y')
+Out[]: <matplotlib.text.Text object at 0x98746ec>
 \end{lstlisting}
+\end{small}
 \end{frame}
 
 \begin{frame}[fragile]
 \begin{figure}
 \includegraphics[width=3.5in]{data/straightline.png}
 \end{figure}
-\alert{Is this what you have??}
+\alert{Is this what you have?}
 \end{frame}
 
 \begin{frame}[fragile]
@@ -197,11 +202,11 @@
 \begin{lstlisting}
   In []: clf()
 
-  In []: plot(L, TSq, 'o')
+  In []: plot(x, y, 'o')
   Out[]: [<matplotlib.lines.Line2D object at 0xac17e0c>]
 
   In []: clf()
-  In []: plot(L, TSq, '.')
+  In []: plot(x, y, '.')
   Out[]: [<matplotlib.lines.Line2D object at 0xac17e0c>]
 \end{lstlisting}
 \end{frame}
@@ -216,8 +221,8 @@
 \begin{frame}[fragile]
 \frametitle{Additional Plotting Attributes}
 \begin{itemize}
-  \item \kwrd{'o'} - Dots
-  \item \kwrd{'.'} - Smaller Dots
+  \item \kwrd{'o'} - Filled circles
+  \item \kwrd{'.'} - Small Dots
   \item \kwrd{'-'} - Lines
   \item \kwrd{'- -'} - Dashed lines
 \end{itemize}
@@ -226,14 +231,14 @@
 \section{Lists}
 \begin{frame}[fragile]
   \frametitle{How to create the data?}
-What were \typ{x} and \typ{y}??\\
+What were \typ{x} and \typ{y}?\\
 \begin{center}
 \alert{\typ{lists!!}}
 \end{center}
 \begin{lstlisting}
 In []: mtlist = [] #Empty List
 
-In []: lst = [1,2,3,4,5] 
+In []: lst = [ 1, 2, 3, 4, 5] 
 \end{lstlisting}
 \end{frame}
 
@@ -248,31 +253,31 @@
 \begin{frame}[fragile]
   \frametitle{List: Slicing}
   \begin{block}{Remember\ldots}
-	\kwrd{In []: lst = [1,2,3,4,5]}
+	\kwrd{In []: lst = [ 1, 2, 3, 4, 5]}
   \end{block}
-\alert{\typ{list[initial:final:step]}}
 \begin{lstlisting}
 In []: lst[1:3]  # A slice.
 Out[]: [2, 3]
 
 In []: lst[1:-1]
-Out[]: [2, 3]
+Out[]: [2, 3, 4]
 \end{lstlisting}
+\alert{\typ{list[initial:final]}}
 \end{frame}
 
 %% more on list slicing
 \begin{frame}[fragile]
 \frametitle{List operations}
 \begin{lstlisting}
-In []: anthrlst = [6,7,8,9]
-In []: lnglst = lst + anthrlst
+In []: a = [ 6, 7, 8, 9]
+In []: b = lst + a
 
-In []: lnglst
+In []: b
 Out[]: [1, 2, 3, 4, 5, 6, 7, 8, 9]
 
 In []: lst.append(6)
 In []: lst
-Out[]: [1, 2, 3, 4, 5, 6]
+Out[]: [ 1, 2, 3, 4, 5, 6]
 \end{lstlisting}
 %\inctime{10}
 \end{frame}
@@ -333,7 +338,7 @@
 In []: plot(L, TSq)
 Out[]: [<matplotlib.lines.Line2D object at 0xa5b05ac>]
 \end{lstlisting}
-This gives the list \kwrd{TSq} which is the list of squares of T values.
+This gives \kwrd{TSq} which is the list of squares of T values.
 \end{frame}
 
 \begin{frame}[fragile]
@@ -343,31 +348,11 @@
 \end{frame}
 
 \begin{frame}[fragile]
-\frametitle{More of \texttt{for}}
-\begin{itemize}
-\item Used to iterate over lists
-\item Let us look at another example.
-\end{itemize}
+\frametitle{What about larger data sets?}
+\alert{Data is usually present in a file!} \\
+Lets look at the \typ{pendulum.txt} file.
 \begin{lstlisting}
-In []: lst = [1,2,3,4,5,6]
-In []: for num in lst:
- ....:     print num, num*num
- ....:    
-1 1
-2 4
-3 9
-4 16
-5 25
-6 36
-\end{lstlisting}
-\end{frame}
-
-\begin{frame}[fragile]
-\frametitle{What about larger data sets??}
-\alert{Data is usually present in a file!} \\
-Lets look at the pendulum.txt file.
-\begin{lstlisting}
-$cat data/pendulum.txt 
+$ cat pendulum.txt 
 1.0000e-01 6.9004e-01
 1.1000e-01 6.9497e-01
 1.2000e-01 7.4252e-01
@@ -379,18 +364,16 @@
 \end{frame}
 
 \begin{frame}[fragile]
-\frametitle{Reading pendulum.txt}
+\frametitle{Reading \typ{pendulum.txt}}
 \begin{itemize}
-  \item We now wish to repeat the plot using the values from a file
-  \item Given a file containing L vs. T values 
-  \item Column1 - L; Column2 - T  
-  \item Read the file
-  \item Plot points for L vs. $T^2$ 
+  \item Let us generate a plot from the data file
+  \item File contains L vs. T values 
+  \item L - Column1; T - Column2
 \end{itemize}
 \end{frame}
 
 \begin{frame}[fragile]
-\frametitle{Reading pendulum.txt}
+\frametitle{Reading \typ{pendulum.txt}}
 \begin{lstlisting}
 In []: L = []
 In []: T = []
@@ -401,12 +384,12 @@
 \end{lstlisting}
 \begin{itemize}
 \item We now have two lists L and T
-\item Now, Repeat previous steps for plotting
+\item Now, repeat previous steps for plotting
 \end{itemize}
 \end{frame}
 
 \begin{frame}[fragile]
-\frametitle{Plotting from pendulum.txt}
+\frametitle{Plotting from \typ{pendulum.txt}}
 \begin{lstlisting}
 In []: TSq = []
 
@@ -427,9 +410,9 @@
   \frametitle{Reading files \ldots}
 \typ{In []: for line in open('pendulum.txt'):}
 \begin{itemize}
-\item opening file `pendulum.txt'
-\item iterating through the file by reading each line into variable \typ{line}
-\item \typ{line} is a \kwrd{string} variable
+\item opening file `\typ{pendulum.txt}'
+\item reading the file line by line
+\item \typ{line} is a \kwrd{string}
 \end{itemize}
 \end{frame}
 
@@ -448,9 +431,9 @@
 \begin{frame}[fragile]
 \frametitle{Strings and \typ{split()}}
   \begin{lstlisting}
-In []: line = 'hello world'
+In []: greet = 'hello world'
 
-In []: line.split()
+In []: greet.split()
 Out[]: ['hello', 'world']
   \end{lstlisting}
 This is what happens with \typ{line}
@@ -470,20 +453,49 @@
   \end{lstlisting}
 But, we need floating point numbers
   \begin{lstlisting}
-In []: t = float(point[0])
+In []: t = float(points[0])
 
 In []: type(t)
 Out[]: <type 'float'>
   \end{lstlisting}
 \end{frame}
 
+\begin{frame}[fragile]
+\frametitle{Let's review the code}
+\begin{small}
+\begin{lstlisting}
+In []: L = []
+In []: T = []
+In []: for line in open('pendulum.txt'):
+  ....     points = line.split()
+  ....     L.append(float(points[0]))
+  ....     T.append(float(points[1]))
+
+In []: TSq = []
+
+In []: for t in T:
+ ....:     TSq.append(t*t)
+
+In []: plot(L, TSq, '.')
+\end{lstlisting}
+\end{small}
+\end{frame}
+
+\begin{frame}[fragile]
+\begin{figure}
+\includegraphics[width=3.5in]{data/L-Tsq.png}
+\end{figure}
+\end{frame}
+
 \section {Summary}
-\begin{frame}
-\frametitle{Summary}
-So what did we learn in this session??
+\begin{frame}[fragile]
+\frametitle{What did we learn?}
 \begin{itemize}
-  \item Creating and running Python scripts
-  \item Plotting points and Plotting attributes
+  \item \kwrd{\%hist -n}
+  \item Python scripts
+  \item \kwrd{\%run -i}
+  \item Plotting points
+  \item Plot attributes
   \item Lists
   \item \kwrd{for}
   \item Reading files
--- a/day1/session4.tex	Thu Nov 05 13:13:58 2009 +0530
+++ b/day1/session4.tex	Thu Nov 05 13:51:00 2009 +0530
@@ -74,7 +74,7 @@
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 % Title page
-\title[Matrices \& Equations]{Python for Science and Engg: Matrices, Least Square Fit, \& Solution of equations}
+\title[Matrices \& Equations]{Python for Science and Engg: Matrices \& Solution of equations}
 
 \author[FOSSEE] {FOSSEE}
 
@@ -128,9 +128,11 @@
 
 \begin{frame}
 \frametitle{Matrices: Introduction}
+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}
@@ -147,104 +149,6 @@
 \end{lstlisting}
 \end{frame}
 
-\begin{frame}[fragile]
-  \frametitle{Accessing elements}
-  \begin{lstlisting}
-In []: C = array([[1,1,2],
-                  [2,4,1],
-                  [-1,3,7]])
-
-In []: C[1][2]
-Out[]: 1
-
-In []: C[1,2]
-Out[]: 1
-
-In []: C[1]
-Out[]: array([2, 4, 1])
-  \end{lstlisting}
-\end{frame}
-
-\begin{frame}[fragile]
-  \frametitle{Changing elements}
-  \begin{small}
-  \begin{lstlisting}
-In []: C[1,1] = -2
-In []: C
-Out[]: 
-array([[ 1,  1,  2],
-       [ 2, -2,  1],
-       [-1,  3,  7]])
-
-In []: C[1] = [0,0,0]
-In []: C
-Out[]: 
-array([[ 1,  1,  2],
-       [ 0,  0,  0],
-       [-1,  3,  7]])
-  \end{lstlisting}
-  \end{small}
-How to change one \alert{column}?
-\end{frame}
-
-\begin{frame}[fragile]
-  \frametitle{Slicing}
-\begin{small}
-  \begin{lstlisting}
-In []: C[:,1]
-Out[]: array([1, 0, 3])
-
-In []: C[1,:]
-Out[]: array([0, 0, 0])
-
-In []: C[0:2,:]
-Out[]: 
-array([[1, 1, 2],
-       [0, 0, 0]])
-
-In []: C[1:3,:]
-Out[]: 
-array([[ 0,  0,  0],
-       [-1,  3,  7]])
-  \end{lstlisting}
-\end{small}
-\end{frame}
-
-\begin{frame}[fragile]
-  \frametitle{Slicing \ldots}
-\begin{small}
-  \begin{lstlisting}
-In []: C[:2,:]
-Out[]: 
-array([[1, 1, 2],
-       [0, 0, 0]])
-
-In []: C[1:,:]
-Out[]: 
-array([[ 0,  0,  0],
-       [-1,  3,  7]])
-
-In []: C[1:,:2]
-Out[]: 
-array([[ 0,  0],
-       [-1,  3]])
-  \end{lstlisting}
-
-\end{small}
-\end{frame}
-
-\begin{frame}[fragile]
-  \frametitle{Striding}
-  \begin{lstlisting}
-  \end{lstlisting}
-\end{frame}
-
-\begin{frame}[fragile]
-  \frametitle{Slicing \& Striding Exercises}
-  \begin{lstlisting}
-  \end{lstlisting}
-\end{frame}
-
 \subsection{Basic Operations}
 
 \begin{frame}[fragile]
@@ -332,7 +236,6 @@
 \end{lstlisting}
 \end{frame}
 
-%%use S=array(X,Y)
 \begin{frame}[fragile]
 \frametitle{Eigenvalues and Eigen Vectors}
 \begin{small}
@@ -352,128 +255,185 @@
 \end{small}
 \end{frame}
 
-%% \begin{frame}[fragile]
-%% \frametitle{Computing Norms}
-%% \begin{lstlisting}
-%% In []: norm(E)
-%% Out[]: 8.1240384046359608
-%% \end{lstlisting}
-%% \end{frame}
+\begin{frame}[fragile]
+\frametitle{Computing Norms}
+\begin{lstlisting}
+In []: norm(E)
+Out[]: 8.1240384046359608
+\end{lstlisting}
+\end{frame}
 
-%% \begin{frame}[fragile]
-%%   \frametitle{Singular Value Decomposition}
-%%   \begin{small}
-%%   \begin{lstlisting}
-%% 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.]),
-%%  array([[-0.66666667, -0.33333333, -0.66666667],
-%%         [-0.        ,  0.89442719, -0.4472136 ],
-%%         [-0.74535599,  0.2981424 ,  0.59628479]]))
-%%   \end{lstlisting}
-%%   \end{small}
-%% \inctime{15}
-%% \end{frame}
+\begin{frame}[fragile]
+  \frametitle{Singular Value Decomposition}
+  \begin{small}
+  \begin{lstlisting}
+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.]),
+ array([[-0.66666667, -0.33333333, -0.66666667],
+        [-0.        ,  0.89442719, -0.4472136 ],
+        [-0.74535599,  0.2981424 ,  0.59628479]]))
+  \end{lstlisting}
+  \end{small}
+\inctime{15}
+\end{frame}
 
-\section{Least Squares Fit}
+\section{Solving linear equations}
+
 \begin{frame}[fragile]
-\frametitle{$L$ vs. $T^2$}
-\vspace{-0.15in}
-\begin{figure}
-\includegraphics[width=4in]{data/L-Tsq-points.png}
-\end{figure}
+\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{$L$ vs. $T^2$}
-\vspace{-0.15in}
-\begin{figure}
-\includegraphics[width=4in]{data/L-Tsq-Line.png}
-\end{figure}
+\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 []: x
+Out[]: 
+array([[ 1.],
+       [-2.],
+       [-2.]])
+\end{lstlisting}
 \end{frame}
 
 \begin{frame}[fragile]
-\frametitle{Least Squares Fit}
-\vspace{-0.15in}
-\begin{figure}
-\includegraphics[width=4in]{data/least-sq-fit.png}
-\end{figure}
+\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}
 
-\begin{frame}
-\frametitle{Least Square Fit Curve}
+\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 $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
+  \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{\typ{lstsq}}
+\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 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
+  \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{Generating $A$}
-\begin{lstlisting}
-In []: A = array([L, ones_like(L)])
-In []: A = A.T
-\end{lstlisting}
-%% \begin{itemize}
-%% \item A is also called a Van der Monde matrix
-%% \item It can also be generated using \typ{vander}
-%% \end{itemize}
-%% \begin{lstlisting}
-%% In []: A = vander(L, 2)
-%% \end{lstlisting}
+\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{\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 []: result = lstsq(A,TSq)
-In []: coef = result[0]
-\end{lstlisting}
+\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}
 
-\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}
+\frametitle{Solution}
+Use \kwrd{solve()}
+\begin{align*}
+  x & = -5\\
+  y & = 2\\
+  z & = 3\\
+  w & = 0\\
+\end{align*}
 \end{frame}
 
 \section{Summary}
@@ -482,7 +442,6 @@
   \begin{itemize}
   \item Matrices
     \begin{itemize}
-      \item Accessing elements
       \item Transpose
       \item Addition
       \item Multiplication
@@ -492,7 +451,6 @@
       \item Norms
       \item Singular Value Decomposition
     \end{itemize}
-  \item Least Square Curve fitting
   \item Solving linear equations
   \end{itemize}
 \end{frame}