--- a/day1/session4.tex Thu Nov 05 13:51:00 2009 +0530
+++ b/day1/session4.tex Thu Nov 05 14:07:26 2009 +0530
@@ -74,7 +74,7 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Title page
-\title[Matrices \& Equations]{Python for Science and Engg: Matrices \& Solution of equations}
+\title[Matrices \& Equations]{Python for Science and Engg: Matrices, Least Square Fit, \& Solution of equations}
\author[FOSSEE] {FOSSEE}
@@ -128,11 +128,9 @@
\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}
@@ -149,6 +147,104 @@
\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]
@@ -236,6 +332,7 @@
\end{lstlisting}
\end{frame}
+%%use S=array(X,Y)
\begin{frame}[fragile]
\frametitle{Eigenvalues and Eigen Vectors}
\begin{small}
@@ -255,185 +352,144 @@
\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}
+
+\section{Least Squares Fit}
\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{Solving linear equations}
-
-\begin{frame}[fragile]
-\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*}
+\frametitle{$L$ vs. $T^2$}
+\vspace{-0.15in}
+\begin{figure}
+\includegraphics[width=4in]{data/L-Tsq-points.png}
+\end{figure}
\end{frame}
\begin{frame}[fragile]
-\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}
+\frametitle{$L$ vs. $T^2$}
+\vspace{-0.15in}
+\begin{figure}
+\includegraphics[width=4in]{data/L-Tsq-Line.png}
+\end{figure}
\end{frame}
\begin{frame}[fragile]
-\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}
+\frametitle{Least Squares Fit}
+\vspace{-0.15in}
+\begin{figure}
+\includegraphics[width=4in]{data/least-sq-fit.png}
+\end{figure}
\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{frame}
+\frametitle{Least Square Fit Curve}
\begin{itemize}
- \item[i] Transpose
- \item[ii]Inverse
- \item[iii]Determinant
- \item[iv] Eigenvalues and Eigen vectors
- \item[v] Singular Value decomposition
+\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{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:
+\frametitle{\typ{lstsq}}
\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
+\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}
\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}
+\frametitle{Generating $A$}
+\begin{lstlisting}
+In []: A = array([L, ones_like(L)])
+In []: A = A.T
+\end{lstlisting}
+\begin{small}
+\begin{block}{}
+ \begin{lstlisting}
+In []: ones((3,5))
+Out[]:
+array([[ 1., 1., 1., 1., 1.],
+ [ 1., 1., 1., 1., 1.],
+ [ 1., 1., 1., 1., 1.]])
+
+In []: ones_like([1, 2, 3, 4, 5])
+Out[]: array([1, 1, 1, 1, 1])
+ \end{lstlisting}
+Also available \alert{\typ{zeros, zeros_like, empty, empty_like}}
+\end{block}
+\end{small}
+
+%% \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}
\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}
+\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}
\end{frame}
+\subsection{Plotting}
\begin{frame}[fragile]
-\frametitle{Solution}
-Use \kwrd{solve()}
-\begin{align*}
- x & = -5\\
- y & = 2\\
- z & = 3\\
- w & = 0\\
-\end{align*}
+\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}
\section{Summary}
@@ -442,16 +498,17 @@
\begin{itemize}
\item Matrices
\begin{itemize}
+ \item Accessing elements
\item Transpose
\item Addition
\item Multiplication
\item Inverse of a matrix
\item Determinant
\item Eigenvalues and Eigen vector
- \item Norms
- \item Singular Value Decomposition
+ %% \item Norms
+ %% \item Singular Value Decomposition
\end{itemize}
- \item Solving linear equations
+ \item Least Square Curve fitting
\end{itemize}
\end{frame}