--- a/day1/session4.tex Sat Oct 31 01:33:41 2009 +0530
+++ b/day1/session4.tex Wed Nov 04 09:40:28 2009 +0530
@@ -128,45 +128,68 @@
\begin{frame}
\frametitle{Matrices: Introduction}
-We looked at the Van der Monde matrix in the previous session,\\
-let us now look at matrices in a little more detail.
+Let us now look at matrices in detail.\\
+\alert{All matrix operations are done using \kwrd{arrays}}
\end{frame}
\subsection{Initializing}
\begin{frame}[fragile]
\frametitle{Matrices: Initializing}
\begin{lstlisting}
-In []: A = matrix([[ 1, 1, 2, -1],
- [ 2, 5, -1, -9],
- [ 2, 1, -1, 3],
- [ 1, -3, 2, 7]])
+In []: A = array([[ 1, 1, 2, -1],
+ [ 2, 5, -1, -9],
+ [ 2, 1, -1, 3],
+ [ 1, -3, 2, 7]])
In []: A
Out[]:
-matrix([[ 1, 1, 2, -1],
- [ 2, 5, -1, -9],
- [ 2, 1, -1, 3],
- [ 1, -3, 2, 7]])
+array([[ 1, 1, 2, -1],
+ [ 2, 5, -1, -9],
+ [ 2, 1, -1, 3],
+ [ 1, -3, 2, 7]])
\end{lstlisting}
\end{frame}
+\begin{frame}[fragile]
+ \frametitle{Accessing elements of matrices}
+\begin{small}
+ \begin{lstlisting}
+In []: C = array([[1,1,2],
+ [2,4,1],
+ [-1,3,7]])
+In []: C[1,2]
+Out[]: 1
+
+In []: C[1]
+Out[]: array([2, 4, 1])
+
+In []: C[1,1] = -2
+In []: C
+Out[]:
+array([[ 1, 1, 2],
+ [ 2, -2, 1],
+ [-1, 3, 7]])
+ \end{lstlisting}
+\end{small}
+\end{frame}
+
\subsection{Basic Operations}
\begin{frame}[fragile]
\frametitle{Transpose of a Matrix}
\begin{lstlisting}
-In []: linalg.transpose(A)
+In []: A.T
Out[]:
-matrix([[ 1, 2, 2, 1],
- [ 1, 5, 1, -3],
- [ 2, -1, -1, 2],
- [-1, -9, 3, 7]])
+array([[ 1, 2, 2, 1],
+ [ 1, 5, 1, -3],
+ [ 2, -1, -1, 2],
+ [-1, -9, 3, 7]])
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Sum of all elements}
\begin{lstlisting}
-In []: linalg.sum(A)
+In []: sum(A)
Out[]: 12
\end{lstlisting}
\end{frame}
@@ -174,41 +197,56 @@
\begin{frame}[fragile]
\frametitle{Matrix Addition}
\begin{lstlisting}
-In []: B = matrix([[3,2,-1,5],
- [2,-2,4,9],
- [-1,0.5,-1,-7],
- [9,-5,7,3]])
-In []: linalg.add(A,B)
+In []: B = array([[3,2,-1,5],
+ [2,-2,4,9],
+ [-1,0.5,-1,-7],
+ [9,-5,7,3]])
+In []: A + B
Out[]:
-matrix([[ 4. , 3. , 1. , 4. ],
- [ 4. , 3. , 3. , 0. ],
- [ 1. , 1.5, -2. , -4. ],
- [ 10. , -8. , 9. , 10. ]])
+array([[ 4. , 3. , 1. , 4. ],
+ [ 4. , 3. , 3. , 0. ],
+ [ 1. , 1.5, -2. , -4. ],
+ [ 10. , -8. , 9. , 10. ]])
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
+\frametitle{Elementwise Multiplication}
+\begin{lstlisting}
+In []: A*B
+Out[]:
+array([[ 3. , 2. , -2. , -5. ],
+ [ 4. , -10. , -4. , -81. ],
+ [ -2. , 0.5, 1. , -21. ],
+ [ 9. , 15. , 14. , 21. ]])
+
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile]
\frametitle{Matrix Multiplication}
\begin{lstlisting}
-In []: linalg.multiply(A, B)
+In []: dot(A,B)
Out[]:
-matrix([[ 3. , 2. , -2. , -5. ],
- [ 4. , -10. , -4. , -81. ],
- [ -2. , 0.5, 1. , -21. ],
- [ 9. , 15. , 14. , 21. ]])
+array([[ -6. , 6. , -6. , -3. ],
+ [-64. , 38.5, -44. , 35. ],
+ [ 36. , -13.5, 24. , 35. ],
+ [ 58. , -26. , 34. , -15. ]])
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Inverse of a Matrix}
+\begin{lstlisting}
+In []: inv(A)
+\end{lstlisting}
\begin{small}
\begin{lstlisting}
-In []: linalg.inv(A)
Out[]:
-matrix([[-0.5 , 0.55, -0.15, 0.7 ],
- [ 0.75, -0.5 , 0.5 , -0.75],
- [ 0.5 , -0.15, -0.05, -0.1 ],
- [ 0.25, -0.25, 0.25, -0.25]])
+array([[-0.5 , 0.55, -0.15, 0.7 ],
+ [ 0.75, -0.5 , 0.5 , -0.75],
+ [ 0.5 , -0.15, -0.05, -0.1 ],
+ [ 0.25, -0.25, 0.25, -0.25]])
\end{lstlisting}
\end{small}
\end{frame}
@@ -217,24 +255,25 @@
\frametitle{Determinant}
\begin{lstlisting}
In []: det(A)
-Out[66]: 80.0
+Out[]: 80.0
\end{lstlisting}
\end{frame}
+%%use S=array(X,Y)
\begin{frame}[fragile]
-\frametitle{Eigen Values and Eigen Matrix}
+\frametitle{Eigenvalues and Eigen Vectors}
\begin{small}
\begin{lstlisting}
-In []: E = matrix([[3,2,4],[2,0,2],[4,2,3]])
+In []: E = array([[3,2,4],[2,0,2],[4,2,3]])
-In []: linalg.eig(E)
+In []: eig(E)
Out[]:
(array([-1., 8., -1.]),
- matrix([[-0.74535599, 0.66666667, -0.1931126 ],
+ array([[-0.74535599, 0.66666667, -0.1931126 ],
[ 0.2981424 , 0.33333333, -0.78664085],
[ 0.59628479, 0.66666667, 0.58643303]]))
-In []: linalg.eigvals(E)
+In []: eigvals(E)
Out[]: array([-1., 8., -1.])
\end{lstlisting}
\end{small}
@@ -243,23 +282,23 @@
\begin{frame}[fragile]
\frametitle{Computing Norms}
\begin{lstlisting}
-In []: linalg.norm(E)
+In []: norm(E)
Out[]: 8.1240384046359608
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
- \frametitle{Single Value Decomposition}
+ \frametitle{Singular Value Decomposition}
\begin{small}
\begin{lstlisting}
-In [76]: linalg.svd(E)
-Out[76]:
-(matrix(
+In []: svd(E)
+Out[]:
+(array(
[[ -6.66666667e-01, -1.23702565e-16, 7.45355992e-01],
[ -3.33333333e-01, -8.94427191e-01, -2.98142397e-01],
[ -6.66666667e-01, 4.47213595e-01, -5.96284794e-01]]),
array([ 8., 1., 1.]),
- matrix([[-0.66666667, -0.33333333, -0.66666667],
+ array([[-0.66666667, -0.33333333, -0.66666667],
[-0. , 0.89442719, -0.4472136 ],
[-0.74535599, 0.2981424 , 0.59628479]]))
\end{lstlisting}
@@ -289,12 +328,12 @@
\frametitle{Solving using Matrices}
Let us now look at how to solve this using \kwrd{matrices}
\begin{lstlisting}
- In []: A = matrix([[3,2,-1],
- [2,-2,4],
- [-1, 0.5, -1]])
- In []: b = matrix([[1], [-2], [0]])
- In []: x = linalg.solve(A, b)
- In []: Ax = dot(A, x)
+ In []: A = array([[3,2,-1],
+ [2,-2,4],
+ [-1, 0.5, -1]])
+ In []: b = array([[1], [-2], [0]])
+ In []: x = solve(A, b)
+ In []: Ax = dot(A,x)
\end{lstlisting}
\end{frame}
@@ -314,9 +353,9 @@
\begin{lstlisting}
In []: Ax
Out[]:
-matrix([[ 1.00000000e+00],
- [ -2.00000000e+00],
- [ 2.22044605e-16]])
+array([[ 1.00000000e+00],
+ [ -2.00000000e+00],
+ [ 2.22044605e-16]])
\end{lstlisting}
\begin{block}{}
The last term in the matrix is actually \alert{0}!\\
@@ -332,7 +371,74 @@
\subsection{Exercises}
\begin{frame}[fragile]
-\frametitle{Problem Set 4: Problem 4.1}
+\frametitle{Problem 1}
+Given the matrix:\\
+\begin{center}
+$\begin{bmatrix}
+-2 & 2 & 3\\
+ 2 & 1 & 6\\
+-1 &-2 & 0\\
+\end{bmatrix}$
+\end{center}
+Find:
+\begin{itemize}
+ \item[i] Transpose
+ \item[ii]Inverse
+ \item[iii]Determinant
+ \item[iv] Eigenvalues and Eigen vectors
+ \item[v] Singular Value decomposition
+\end{itemize}
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{Problem 2}
+Given
+\begin{center}
+A =
+$\begin{bmatrix}
+-3 & 1 & 5 \\
+1 & 0 & -2 \\
+5 & -2 & 4 \\
+\end{bmatrix}$
+, B =
+$\begin{bmatrix}
+0 & 9 & -12 \\
+-9 & 0 & 20 \\
+12 & -20 & 0 \\
+\end{bmatrix}$
+\end{center}
+Find:
+\begin{itemize}
+ \item[i] Sum of A and B
+ \item[ii]Elementwise Product of A and B
+ \item[iii] Matrix product of A and B
+\end{itemize}
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{Solution}
+Sum:
+$\begin{bmatrix}
+-3 & 10 & 7 \\
+-8 & 0 & 18 \\
+17 & -22 & 4 \\
+\end{bmatrix}$
+,\\ Elementwise Product:
+$\begin{bmatrix}
+0 & 9 & -60 \\
+-9 & 0 & -40 \\
+60 & 40 & 0 \\
+\end{bmatrix}$
+,\\ Matrix product:
+$\begin{bmatrix}
+51 & -127 & 56 \\
+-24 & 49 & -12 \\
+66 & -35 & -100 \\
+\end{bmatrix}$
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{Problem 3}
Solve the set of equations:
\begin{align*}
x + y + 2z -w & = 3\\
@@ -345,37 +451,30 @@
\begin{frame}[fragile]
\frametitle{Solution}
-Solution:
-\begin{lstlisting}
+Use \kwrd{solve()}
\begin{align*}
x & = -5\\
y & = 2\\
z & = 3\\
w & = 0\\
\end{align*}
-\end{lstlisting}
-\end{frame}
-
-\begin{frame}[fragile]
-\frametitle{Problem 4.2}
-
\end{frame}
\section{Summary}
\begin{frame}
- \frametitle{Summary}
-So what did we learn??
+ \frametitle{What did we learn??}
\begin{itemize}
\item Matrices
\begin{itemize}
+ \item Accessing elements
\item Transpose
\item Addition
\item Multiplication
\item Inverse of a matrix
\item Determinant
- \item Eigen values and Eigen matrix
+ \item Eigenvalues and Eigen vector
\item Norms
- \item Single Value Decomposition
+ \item Singular Value Decomposition
\end{itemize}
\item Solving linear equations
\end{itemize}