307
|
1 |
% Created 2010-10-12 Tue 14:28
|
|
2 |
\documentclass[presentation]{beamer}
|
|
3 |
\usepackage[latin1]{inputenc}
|
|
4 |
\usepackage[T1]{fontenc}
|
|
5 |
\usepackage{fixltx2e}
|
|
6 |
\usepackage{graphicx}
|
|
7 |
\usepackage{longtable}
|
|
8 |
\usepackage{float}
|
|
9 |
\usepackage{wrapfig}
|
|
10 |
\usepackage{soul}
|
|
11 |
\usepackage{t1enc}
|
|
12 |
\usepackage{textcomp}
|
|
13 |
\usepackage{marvosym}
|
|
14 |
\usepackage{wasysym}
|
|
15 |
\usepackage{latexsym}
|
|
16 |
\usepackage{amssymb}
|
|
17 |
\usepackage{hyperref}
|
|
18 |
\tolerance=1000
|
|
19 |
\usepackage[english]{babel} \usepackage{ae,aecompl}
|
|
20 |
\usepackage{mathpazo,courier,euler} \usepackage[scaled=.95]{helvet}
|
|
21 |
\usepackage{listings}
|
|
22 |
\lstset{language=Python, basicstyle=\ttfamily\bfseries,
|
|
23 |
commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen},
|
|
24 |
showstringspaces=false, keywordstyle=\color{blue}\bfseries}
|
|
25 |
\providecommand{\alert}[1]{\textbf{#1}}
|
|
26 |
|
|
27 |
\title{Matrices}
|
|
28 |
\author{FOSSEE}
|
|
29 |
\date{}
|
|
30 |
|
|
31 |
\usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent}
|
|
32 |
\begin{document}
|
|
33 |
|
|
34 |
\maketitle
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
\begin{frame}
|
|
45 |
\frametitle{Outline}
|
|
46 |
\label{sec-1}
|
|
47 |
|
|
48 |
\begin{itemize}
|
|
49 |
\item Creating Matrices
|
|
50 |
|
|
51 |
\begin{itemize}
|
|
52 |
\item using direct data
|
|
53 |
\item converting a list
|
|
54 |
\end{itemize}
|
|
55 |
|
|
56 |
\item Matrix operations
|
|
57 |
\item Inverse of matrix
|
|
58 |
\item Determinant of matrix
|
|
59 |
\item Eigen values and Eigen vectors of matrices
|
|
60 |
\item Norm of matrix
|
|
61 |
\item Singular Value Decomposition of matrices
|
|
62 |
\end{itemize}
|
|
63 |
\end{frame}
|
|
64 |
\begin{frame}[fragile]
|
|
65 |
\frametitle{Creating a matrix}
|
|
66 |
\label{sec-2}
|
|
67 |
|
|
68 |
\begin{itemize}
|
|
69 |
\item Creating a matrix using direct data
|
|
70 |
\end{itemize}
|
|
71 |
|
|
72 |
\begin{verbatim}
|
|
73 |
In []: m1 = matrix([1, 2, 3, 4])
|
|
74 |
\end{verbatim}
|
|
75 |
|
|
76 |
\begin{itemize}
|
|
77 |
\item Creating a matrix using lists
|
|
78 |
\end{itemize}
|
|
79 |
|
|
80 |
\begin{verbatim}
|
|
81 |
In []: l1 = [[1,2,3,4],[5,6,7,8]]
|
|
82 |
In []: m2 = matrix(l1)
|
|
83 |
\end{verbatim}
|
|
84 |
|
|
85 |
\begin{itemize}
|
|
86 |
\item A matrix is basically an array
|
|
87 |
\end{itemize}
|
|
88 |
|
|
89 |
\begin{verbatim}
|
|
90 |
In []: m3 = array([[5,6,7,8],[9,10,11,12]])
|
|
91 |
\end{verbatim}
|
|
92 |
\end{frame}
|
|
93 |
\begin{frame}[fragile]
|
|
94 |
\frametitle{Matrix operations}
|
|
95 |
\label{sec-3}
|
|
96 |
|
|
97 |
\begin{itemize}
|
|
98 |
\item Element-wise addition (both matrix should be of order \texttt{mXn})
|
|
99 |
\begin{verbatim}
|
|
100 |
In []: m3 + m2
|
|
101 |
\end{verbatim}
|
|
102 |
|
|
103 |
\item Element-wise subtraction (both matrix should be of order \texttt{mXn})
|
|
104 |
\begin{verbatim}
|
|
105 |
In []: m3 - m2
|
|
106 |
\end{verbatim}
|
|
107 |
|
|
108 |
\end{itemize}
|
|
109 |
\end{frame}
|
|
110 |
\begin{frame}[fragile]
|
|
111 |
\frametitle{Matrix Multiplication}
|
|
112 |
\label{sec-4}
|
|
113 |
|
|
114 |
\begin{itemize}
|
|
115 |
\item Matrix Multiplication
|
|
116 |
\begin{verbatim}
|
|
117 |
In []: m3 * m2
|
|
118 |
Out []: ValueError: objects are not aligned
|
|
119 |
\end{verbatim}
|
|
120 |
|
|
121 |
\item Element-wise multiplication using \texttt{multiply()}
|
|
122 |
\begin{verbatim}
|
|
123 |
multiply(m3, m2)
|
|
124 |
\end{verbatim}
|
|
125 |
|
|
126 |
\end{itemize}
|
|
127 |
\end{frame}
|
|
128 |
\begin{frame}[fragile]
|
|
129 |
\frametitle{Matrix Multiplication (cont'd)}
|
|
130 |
\label{sec-5}
|
|
131 |
|
|
132 |
\begin{itemize}
|
|
133 |
\item Create two compatible matrices of order \texttt{nXm} and \texttt{mXr}
|
|
134 |
\begin{verbatim}
|
|
135 |
In []: m1.shape
|
|
136 |
\end{verbatim}
|
|
137 |
|
|
138 |
|
|
139 |
\begin{itemize}
|
|
140 |
\item matrix m1 is of order \texttt{1 X 4}
|
|
141 |
\end{itemize}
|
|
142 |
|
|
143 |
\item Creating another matrix of order \texttt{4 X 2}
|
|
144 |
\begin{verbatim}
|
|
145 |
In []: m4 = matrix([[1,2],[3,4],[5,6],[7,8]])
|
|
146 |
\end{verbatim}
|
|
147 |
|
|
148 |
\item Matrix multiplication
|
|
149 |
\begin{verbatim}
|
|
150 |
In []: m1 * m4
|
|
151 |
\end{verbatim}
|
|
152 |
|
|
153 |
\end{itemize}
|
|
154 |
\end{frame}
|
|
155 |
\begin{frame}
|
|
156 |
\frametitle{Recall from \texttt{array}}
|
|
157 |
\label{sec-6}
|
|
158 |
|
|
159 |
\begin{itemize}
|
|
160 |
\item The functions
|
|
161 |
|
|
162 |
\begin{itemize}
|
|
163 |
\item \texttt{identity(n)} -
|
|
164 |
creates an identity matrix of order \texttt{nXn}
|
|
165 |
\item \texttt{zeros((m,n))} -
|
|
166 |
creates a matrix of order \texttt{mXn} with 0's
|
|
167 |
\item \texttt{zeros\_like(A)} -
|
|
168 |
creates a matrix with 0's similar to the shape of matrix \texttt{A}
|
|
169 |
\item \texttt{ones((m,n))}
|
|
170 |
creates a matrix of order \texttt{mXn} with 1's
|
|
171 |
\item \texttt{ones\_like(A)}
|
|
172 |
creates a matrix with 1's similar to the shape of matrix \texttt{A}
|
|
173 |
\end{itemize}
|
|
174 |
|
|
175 |
\end{itemize}
|
|
176 |
|
|
177 |
Can also be used with matrices
|
|
178 |
\end{frame}
|
|
179 |
\begin{frame}[fragile]
|
|
180 |
\frametitle{More matrix operations}
|
|
181 |
\label{sec-7}
|
|
182 |
|
|
183 |
Transpose of a matrix
|
|
184 |
\begin{verbatim}
|
|
185 |
In []: m4.T
|
|
186 |
\end{verbatim}
|
|
187 |
\end{frame}
|
|
188 |
\begin{frame}[fragile]
|
|
189 |
\frametitle{Exercise 1 : Frobenius norm \& inverse}
|
|
190 |
\label{sec-8}
|
|
191 |
|
|
192 |
Find out the Frobenius norm of inverse of a \texttt{4 X 4} matrix.
|
|
193 |
\begin{verbatim}
|
|
194 |
|
|
195 |
\end{verbatim}
|
|
196 |
|
|
197 |
The matrix is
|
|
198 |
\begin{verbatim}
|
|
199 |
m5 = matrix(arange(1,17).reshape(4,4))
|
|
200 |
\end{verbatim}
|
|
201 |
|
|
202 |
\begin{itemize}
|
|
203 |
\item Inverse of A,
|
|
204 |
|
|
205 |
\begin{itemize}
|
|
206 |
\item $A^{-1} = inv(A)$
|
|
207 |
\end{itemize}
|
|
208 |
|
|
209 |
\item Frobenius norm is defined as,
|
|
210 |
|
|
211 |
\begin{itemize}
|
|
212 |
\item $||A||_F = [\sum_{i,j} abs(a_{i,j})^2]^{1/2}$
|
|
213 |
\end{itemize}
|
|
214 |
|
|
215 |
\end{itemize}
|
|
216 |
\end{frame}
|
|
217 |
\begin{frame}[fragile]
|
|
218 |
\frametitle{Exercise 2: Infinity norm}
|
|
219 |
\label{sec-9}
|
|
220 |
|
|
221 |
Find the infinity norm of the matrix \texttt{im5}
|
|
222 |
\begin{verbatim}
|
|
223 |
|
|
224 |
\end{verbatim}
|
|
225 |
|
|
226 |
\begin{itemize}
|
|
227 |
\item Infinity norm is defined as,
|
|
228 |
$max([\sum_{i} abs(a_{i})^2])$
|
|
229 |
\end{itemize}
|
|
230 |
\end{frame}
|
|
231 |
\begin{frame}[fragile]
|
|
232 |
\frametitle{\texttt{norm()} method}
|
|
233 |
\label{sec-10}
|
|
234 |
|
|
235 |
\begin{itemize}
|
|
236 |
\item Frobenius norm
|
|
237 |
\begin{verbatim}
|
|
238 |
In []: norm(im5)
|
|
239 |
\end{verbatim}
|
|
240 |
|
|
241 |
\item Infinity norm
|
|
242 |
\begin{verbatim}
|
|
243 |
In []: norm(im5, ord=inf)
|
|
244 |
\end{verbatim}
|
|
245 |
|
|
246 |
\end{itemize}
|
|
247 |
\end{frame}
|
|
248 |
\begin{frame}[fragile]
|
|
249 |
\frametitle{Determinant}
|
|
250 |
\label{sec-11}
|
|
251 |
|
|
252 |
Find out the determinant of the matrix m5
|
|
253 |
\begin{verbatim}
|
|
254 |
|
|
255 |
\end{verbatim}
|
|
256 |
|
|
257 |
\begin{itemize}
|
|
258 |
\item determinant can be found out using
|
|
259 |
|
|
260 |
\begin{itemize}
|
|
261 |
\item \texttt{det(A)} - returns the determinant of matrix \texttt{A}
|
|
262 |
\end{itemize}
|
|
263 |
|
|
264 |
\end{itemize}
|
|
265 |
\end{frame}
|
|
266 |
\begin{frame}[fragile]
|
|
267 |
\frametitle{eigen values \& eigen vectors}
|
|
268 |
\label{sec-12}
|
|
269 |
|
|
270 |
Find out the eigen values and eigen vectors of the matrix \texttt{m5}.
|
|
271 |
\begin{verbatim}
|
|
272 |
|
|
273 |
\end{verbatim}
|
|
274 |
|
|
275 |
\begin{itemize}
|
|
276 |
\item eigen values and vectors can be found out using
|
|
277 |
\begin{verbatim}
|
|
278 |
In []: eig(m5)
|
|
279 |
\end{verbatim}
|
|
280 |
|
|
281 |
returns a tuple of \emph{eigen values} and \emph{eigen vectors}
|
|
282 |
\item \emph{eigen values} in tuple
|
|
283 |
|
|
284 |
\begin{itemize}
|
|
285 |
\item \texttt{In []: eig(m5)[0]}
|
|
286 |
\end{itemize}
|
|
287 |
|
|
288 |
\item \emph{eigen vectors} in tuple
|
|
289 |
|
|
290 |
\begin{itemize}
|
|
291 |
\item \texttt{In []: eig(m5)[1]}
|
|
292 |
\end{itemize}
|
|
293 |
|
|
294 |
\item Computing \emph{eigen values} using \texttt{eigvals()}
|
|
295 |
\begin{verbatim}
|
|
296 |
In []: eigvals(m5)
|
|
297 |
\end{verbatim}
|
|
298 |
|
|
299 |
\end{itemize}
|
|
300 |
\end{frame}
|
|
301 |
\begin{frame}[fragile]
|
|
302 |
\frametitle{Singular Value Decomposition (\texttt{svd})}
|
|
303 |
\label{sec-13}
|
|
304 |
|
|
305 |
$M = U \Sigma V^*$
|
|
306 |
\begin{itemize}
|
|
307 |
\item U, an \texttt{mXm} unitary matrix over K.
|
|
308 |
\item $\Sigma$
|
|
309 |
, an \texttt{mXn} diagonal matrix with non-negative real numbers on diagonal.
|
|
310 |
\item $V^*$
|
|
311 |
, an \texttt{nXn} unitary matrix over K, denotes the conjugate transpose of V.
|
|
312 |
\item SVD of matrix \texttt{m5} can be found out as,
|
|
313 |
\end{itemize}
|
|
314 |
|
|
315 |
\begin{verbatim}
|
|
316 |
In []: svd(m5)
|
|
317 |
\end{verbatim}
|
|
318 |
\end{frame}
|
|
319 |
\begin{frame}
|
|
320 |
\frametitle{Summary}
|
|
321 |
\label{sec-14}
|
|
322 |
|
|
323 |
\begin{itemize}
|
|
324 |
\item Matrices
|
|
325 |
|
|
326 |
\begin{itemize}
|
|
327 |
\item creating matrices
|
|
328 |
\end{itemize}
|
|
329 |
|
|
330 |
\item Matrix operations
|
|
331 |
\item Inverse (\texttt{inv()})
|
|
332 |
\item Determinant (\texttt{det()})
|
|
333 |
\item Norm (\texttt{norm()})
|
|
334 |
\item Eigen values \& vectors (\texttt{eig(), eigvals()})
|
|
335 |
\item Singular Value Decomposition (\texttt{svd()})
|
|
336 |
\end{itemize}
|
|
337 |
\end{frame}
|
|
338 |
\begin{frame}
|
|
339 |
\frametitle{Thank you!}
|
|
340 |
\label{sec-15}
|
|
341 |
|
|
342 |
\begin{block}{}
|
|
343 |
\begin{center}
|
|
344 |
This spoken tutorial has been produced by the
|
|
345 |
\textcolor{blue}{FOSSEE} team, which is funded by the
|
|
346 |
\end{center}
|
|
347 |
\begin{center}
|
|
348 |
\textcolor{blue}{National Mission on Education through \\
|
|
349 |
Information \& Communication Technology \\
|
|
350 |
MHRD, Govt. of India}.
|
|
351 |
\end{center}
|
|
352 |
\end{block}
|
|
353 |
|
|
354 |
|
|
355 |
\end{frame}
|
|
356 |
|
|
357 |
\end{document}
|