126 |
126 |
127 \section{Matrices} |
127 \section{Matrices} |
128 |
128 |
129 \begin{frame} |
129 \begin{frame} |
130 \frametitle{Matrices: Introduction} |
130 \frametitle{Matrices: Introduction} |
131 We looked at the Van der Monde matrix in the previous session,\\ |
131 Let us now look at matrices in detail.\\ |
132 let us now look at matrices in a little more detail. |
132 \alert{All matrix operations are done using \kwrd{arrays}} |
133 \end{frame} |
133 \end{frame} |
134 |
134 |
135 \subsection{Initializing} |
135 \subsection{Initializing} |
136 \begin{frame}[fragile] |
136 \begin{frame}[fragile] |
137 \frametitle{Matrices: Initializing} |
137 \frametitle{Matrices: Initializing} |
138 \begin{lstlisting} |
138 \begin{lstlisting} |
139 In []: A = matrix([[ 1, 1, 2, -1], |
139 In []: A = array([[ 1, 1, 2, -1], |
140 [ 2, 5, -1, -9], |
140 [ 2, 5, -1, -9], |
141 [ 2, 1, -1, 3], |
141 [ 2, 1, -1, 3], |
142 [ 1, -3, 2, 7]]) |
142 [ 1, -3, 2, 7]]) |
143 In []: A |
143 In []: A |
144 Out[]: |
144 Out[]: |
145 matrix([[ 1, 1, 2, -1], |
145 array([[ 1, 1, 2, -1], |
146 [ 2, 5, -1, -9], |
146 [ 2, 5, -1, -9], |
147 [ 2, 1, -1, 3], |
147 [ 2, 1, -1, 3], |
148 [ 1, -3, 2, 7]]) |
148 [ 1, -3, 2, 7]]) |
149 \end{lstlisting} |
149 \end{lstlisting} |
150 \end{frame} |
150 \end{frame} |
151 |
151 |
152 \subsection{Basic Operations} |
152 \subsection{Basic Operations} |
153 |
153 |
154 \begin{frame}[fragile] |
154 \begin{frame}[fragile] |
155 \frametitle{Transpose of a Matrix} |
155 \frametitle{Transpose of a Matrix} |
156 \begin{lstlisting} |
156 \begin{lstlisting} |
157 In []: linalg.transpose(A) |
157 In []: A.T |
158 Out[]: |
158 Out[]: |
159 matrix([[ 1, 2, 2, 1], |
159 array([[ 1, 2, 2, 1], |
160 [ 1, 5, 1, -3], |
160 [ 1, 5, 1, -3], |
161 [ 2, -1, -1, 2], |
161 [ 2, -1, -1, 2], |
162 [-1, -9, 3, 7]]) |
162 [-1, -9, 3, 7]]) |
163 \end{lstlisting} |
163 \end{lstlisting} |
164 \end{frame} |
164 \end{frame} |
165 |
165 |
166 \begin{frame}[fragile] |
166 \begin{frame}[fragile] |
167 \frametitle{Sum of all elements} |
167 \frametitle{Sum of all elements} |
168 \begin{lstlisting} |
168 \begin{lstlisting} |
169 In []: linalg.sum(A) |
169 In []: sum(A) |
170 Out[]: 12 |
170 Out[]: 12 |
171 \end{lstlisting} |
171 \end{lstlisting} |
172 \end{frame} |
172 \end{frame} |
173 |
173 |
174 \begin{frame}[fragile] |
174 \begin{frame}[fragile] |
175 \frametitle{Matrix Addition} |
175 \frametitle{Matrix Addition} |
176 \begin{lstlisting} |
176 \begin{lstlisting} |
177 In []: B = matrix([[3,2,-1,5], |
177 In []: B = array([[3,2,-1,5], |
178 [2,-2,4,9], |
178 [2,-2,4,9], |
179 [-1,0.5,-1,-7], |
179 [-1,0.5,-1,-7], |
180 [9,-5,7,3]]) |
180 [9,-5,7,3]]) |
181 In []: linalg.add(A,B) |
181 In []: A + B |
182 Out[]: |
182 Out[]: |
183 matrix([[ 4. , 3. , 1. , 4. ], |
183 array([[ 4. , 3. , 1. , 4. ], |
184 [ 4. , 3. , 3. , 0. ], |
184 [ 4. , 3. , 3. , 0. ], |
185 [ 1. , 1.5, -2. , -4. ], |
185 [ 1. , 1.5, -2. , -4. ], |
186 [ 10. , -8. , 9. , 10. ]]) |
186 [ 10. , -8. , 9. , 10. ]]) |
187 \end{lstlisting} |
187 \end{lstlisting} |
188 \end{frame} |
188 \end{frame} |
189 |
189 |
190 \begin{frame}[fragile] |
190 \begin{frame}[fragile] |
|
191 \frametitle{Elementwise Multiplication} |
|
192 \begin{lstlisting} |
|
193 In []: A*B |
|
194 Out[]: |
|
195 array([[ 3. , 2. , -2. , -5. ], |
|
196 [ 4. , -10. , -4. , -81. ], |
|
197 [ -2. , 0.5, 1. , -21. ], |
|
198 [ 9. , 15. , 14. , 21. ]]) |
|
199 |
|
200 \end{lstlisting} |
|
201 \end{frame} |
|
202 |
|
203 \begin{frame}[fragile] |
191 \frametitle{Matrix Multiplication} |
204 \frametitle{Matrix Multiplication} |
192 \begin{lstlisting} |
205 \begin{lstlisting} |
193 In []: linalg.multiply(A, B) |
206 In []: dot(A,B) |
194 Out[]: |
207 Out[]: |
195 matrix([[ 3. , 2. , -2. , -5. ], |
208 array([[ -6. , 6. , -6. , -3. ], |
196 [ 4. , -10. , -4. , -81. ], |
209 [-64. , 38.5, -44. , 35. ], |
197 [ -2. , 0.5, 1. , -21. ], |
210 [ 36. , -13.5, 24. , 35. ], |
198 [ 9. , 15. , 14. , 21. ]]) |
211 [ 58. , -26. , 34. , -15. ]]) |
199 \end{lstlisting} |
212 \end{lstlisting} |
200 \end{frame} |
213 \end{frame} |
201 |
214 |
202 \begin{frame}[fragile] |
215 \begin{frame}[fragile] |
203 \frametitle{Inverse of a Matrix} |
216 \frametitle{Inverse of a Matrix} |
|
217 \begin{lstlisting} |
|
218 In []: inv(A) |
|
219 \end{lstlisting} |
204 \begin{small} |
220 \begin{small} |
205 \begin{lstlisting} |
221 \begin{lstlisting} |
206 In []: linalg.inv(A) |
222 Out[]: |
207 Out[]: |
223 array([[-0.5 , 0.55, -0.15, 0.7 ], |
208 matrix([[-0.5 , 0.55, -0.15, 0.7 ], |
224 [ 0.75, -0.5 , 0.5 , -0.75], |
209 [ 0.75, -0.5 , 0.5 , -0.75], |
225 [ 0.5 , -0.15, -0.05, -0.1 ], |
210 [ 0.5 , -0.15, -0.05, -0.1 ], |
226 [ 0.25, -0.25, 0.25, -0.25]]) |
211 [ 0.25, -0.25, 0.25, -0.25]]) |
|
212 \end{lstlisting} |
227 \end{lstlisting} |
213 \end{small} |
228 \end{small} |
214 \end{frame} |
229 \end{frame} |
215 |
230 |
216 \begin{frame}[fragile] |
231 \begin{frame}[fragile] |
217 \frametitle{Determinant} |
232 \frametitle{Determinant} |
218 \begin{lstlisting} |
233 \begin{lstlisting} |
219 In []: det(A) |
234 In []: det(A) |
220 Out[66]: 80.0 |
235 Out[]: 80.0 |
221 \end{lstlisting} |
236 \end{lstlisting} |
222 \end{frame} |
237 \end{frame} |
223 |
238 |
224 \begin{frame}[fragile] |
239 \begin{frame}[fragile] |
225 \frametitle{Eigen Values and Eigen Matrix} |
240 \frametitle{Eigenvalues and Eigen Vectors} |
226 \begin{small} |
241 \begin{small} |
227 \begin{lstlisting} |
242 \begin{lstlisting} |
228 In []: E = matrix([[3,2,4],[2,0,2],[4,2,3]]) |
243 In []: E = array([[3,2,4],[2,0,2],[4,2,3]]) |
229 |
244 |
230 In []: linalg.eig(E) |
245 In []: eig(E) |
231 Out[]: |
246 Out[]: |
232 (array([-1., 8., -1.]), |
247 (array([-1., 8., -1.]), |
233 matrix([[-0.74535599, 0.66666667, -0.1931126 ], |
248 array([[-0.74535599, 0.66666667, -0.1931126 ], |
234 [ 0.2981424 , 0.33333333, -0.78664085], |
249 [ 0.2981424 , 0.33333333, -0.78664085], |
235 [ 0.59628479, 0.66666667, 0.58643303]])) |
250 [ 0.59628479, 0.66666667, 0.58643303]])) |
236 |
251 |
237 In []: linalg.eigvals(E) |
252 In []: eigvals(E) |
238 Out[]: array([-1., 8., -1.]) |
253 Out[]: array([-1., 8., -1.]) |
239 \end{lstlisting} |
254 \end{lstlisting} |
240 \end{small} |
255 \end{small} |
241 \end{frame} |
256 \end{frame} |
242 |
257 |
243 \begin{frame}[fragile] |
258 \begin{frame}[fragile] |
244 \frametitle{Computing Norms} |
259 \frametitle{Computing Norms} |
245 \begin{lstlisting} |
260 \begin{lstlisting} |
246 In []: linalg.norm(E) |
261 In []: norm(E) |
247 Out[]: 8.1240384046359608 |
262 Out[]: 8.1240384046359608 |
248 \end{lstlisting} |
263 \end{lstlisting} |
249 \end{frame} |
264 \end{frame} |
250 |
265 |
251 \begin{frame}[fragile] |
266 \begin{frame}[fragile] |
252 \frametitle{Single Value Decomposition} |
267 \frametitle{Singular Value Decomposition} |
253 \begin{small} |
268 \begin{small} |
254 \begin{lstlisting} |
269 \begin{lstlisting} |
255 In [76]: linalg.svd(E) |
270 In []: svd(E) |
256 Out[76]: |
271 Out[]: |
257 (matrix( |
272 (array( |
258 [[ -6.66666667e-01, -1.23702565e-16, 7.45355992e-01], |
273 [[ -6.66666667e-01, -1.23702565e-16, 7.45355992e-01], |
259 [ -3.33333333e-01, -8.94427191e-01, -2.98142397e-01], |
274 [ -3.33333333e-01, -8.94427191e-01, -2.98142397e-01], |
260 [ -6.66666667e-01, 4.47213595e-01, -5.96284794e-01]]), |
275 [ -6.66666667e-01, 4.47213595e-01, -5.96284794e-01]]), |
261 array([ 8., 1., 1.]), |
276 array([ 8., 1., 1.]), |
262 matrix([[-0.66666667, -0.33333333, -0.66666667], |
277 array([[-0.66666667, -0.33333333, -0.66666667], |
263 [-0. , 0.89442719, -0.4472136 ], |
278 [-0. , 0.89442719, -0.4472136 ], |
264 [-0.74535599, 0.2981424 , 0.59628479]])) |
279 [-0.74535599, 0.2981424 , 0.59628479]])) |
265 \end{lstlisting} |
280 \end{lstlisting} |
266 \end{small} |
281 \end{small} |
267 \inctime{15} |
282 \inctime{15} |