132 \end{frame} |
132 \end{frame} |
133 |
133 |
134 \begin{frame}[fragile] |
134 \begin{frame}[fragile] |
135 \frametitle{Matrices: Initializing} |
135 \frametitle{Matrices: Initializing} |
136 \begin{lstlisting} |
136 \begin{lstlisting} |
137 In []: A = array([[ 1, 1, 2, -1], |
137 In []: c = array([[1,1,2], |
138 [ 2, 5, -1, -9], |
138 [2,4,1], |
139 [ 2, 1, -1, 3], |
139 [-1,3,7]]) |
140 [ 1, -3, 2, 7]]) |
140 |
141 In []: A |
141 In []: c |
142 Out[]: |
142 Out[]: |
143 array([[ 1, 1, 2, -1], |
143 array([[1,1,2], |
144 [ 2, 5, -1, -9], |
144 [2,4,1], |
145 [ 2, 1, -1, 3], |
145 [-1,3,7]]) |
146 [ 1, -3, 2, 7]]) |
|
147 \end{lstlisting} |
146 \end{lstlisting} |
148 \end{frame} |
147 \end{frame} |
149 |
148 |
150 \begin{frame}[fragile] |
149 \begin{frame}[fragile] |
151 \frametitle{Initializing some special matrices} |
150 \frametitle{Initializing some special matrices} |
171 |
170 |
172 |
171 |
173 \begin{frame}[fragile] |
172 \begin{frame}[fragile] |
174 \frametitle{Accessing elements} |
173 \frametitle{Accessing elements} |
175 \begin{lstlisting} |
174 \begin{lstlisting} |
176 In []: C = array([[1,1,2], |
175 In []: c |
177 [2,4,1], |
176 Out[]: |
178 [-1,3,7]]) |
177 array([[1,1,2], |
179 |
178 [2,4,1], |
180 In []: C[1][2] |
179 [-1,3,7]]) |
|
180 |
|
181 In []: c[1][2] |
181 Out[]: 1 |
182 Out[]: 1 |
182 |
183 In []: c[1,2] |
183 In []: C[1,2] |
|
184 Out[]: 1 |
184 Out[]: 1 |
185 |
185 In []: c[1] |
186 In []: C[1] |
|
187 Out[]: array([2, 4, 1]) |
186 Out[]: array([2, 4, 1]) |
188 \end{lstlisting} |
187 \end{lstlisting} |
189 \end{frame} |
188 \end{frame} |
190 |
189 |
191 \begin{frame}[fragile] |
190 \begin{frame}[fragile] |
192 \frametitle{Changing elements} |
191 \frametitle{Changing elements} |
193 \begin{small} |
192 \begin{small} |
194 \begin{lstlisting} |
193 \begin{lstlisting} |
195 In []: C[1,1] = -2 |
194 In []: c[1,1] = -2 |
196 In []: C |
195 In []: c |
197 Out[]: |
196 Out[]: |
198 array([[ 1, 1, 2], |
197 array([[ 1, 1, 2], |
199 [ 2, -2, 1], |
198 [ 2, -2, 1], |
200 [-1, 3, 7]]) |
199 [-1, 3, 7]]) |
201 |
200 |
202 In []: C[1] = [0,0,0] |
201 In []: c[1] = [0,0,0] |
203 In []: C |
202 In []: c |
204 Out[]: |
203 Out[]: |
205 array([[ 1, 1, 2], |
204 array([[ 1, 1, 2], |
206 [ 0, 0, 0], |
205 [ 0, 0, 0], |
207 [-1, 3, 7]]) |
206 [-1, 3, 7]]) |
208 \end{lstlisting} |
207 \end{lstlisting} |
301 |
300 |
302 \begin{frame}[fragile] |
301 \begin{frame}[fragile] |
303 \frametitle{Solutions} |
302 \frametitle{Solutions} |
304 \begin{small} |
303 \begin{small} |
305 \begin{lstlisting} |
304 \begin{lstlisting} |
306 In []: imshow(A[:256,:256]) |
305 In []: imshow(a[:256,:256]) |
307 Out[]: <matplotlib.image.AxesImage object at 0xb6f658c> |
306 Out[]: <matplotlib.image.AxesImage object at 0xb6f658c> |
308 |
307 |
309 In []: imshow(A[200:400,200:400]) |
308 In []: imshow(a[200:400,200:400]) |
310 Out[]: <matplotlib.image.AxesImage object at 0xb757c2c> |
309 Out[]: <matplotlib.image.AxesImage object at 0xb757c2c> |
311 |
310 |
312 In []: imshow(A[::2,::2]) |
311 In []: imshow(a[::2,::2]) |
313 Out[]: <matplotlib.image.AxesImage object at 0xb765c8c> |
312 Out[]: <matplotlib.image.AxesImage object at 0xb765c8c> |
314 \end{lstlisting} |
313 \end{lstlisting} |
315 \end{small} |
314 \end{small} |
316 \end{frame} |
315 \end{frame} |
317 |
316 |
318 \begin{frame}[fragile] |
317 \begin{frame}[fragile] |
319 \frametitle{Transpose of a Matrix} |
318 \frametitle{Transpose of a Matrix} |
320 \begin{lstlisting} |
319 \begin{lstlisting} |
321 In []: A.T |
320 In []: a.T |
322 Out[]: |
321 Out[]: |
323 array([[ 1, 2, 2, 1], |
322 array([[ 1, 2, 2, 1], |
324 [ 1, 5, 1, -3], |
323 [ 1, 5, 1, -3], |
325 [ 2, -1, -1, 2], |
324 [ 2, -1, -1, 2], |
326 [-1, -9, 3, 7]]) |
325 [-1, -9, 3, 7]]) |
328 \end{frame} |
327 \end{frame} |
329 |
328 |
330 \begin{frame}[fragile] |
329 \begin{frame}[fragile] |
331 \frametitle{Sum of all elements} |
330 \frametitle{Sum of all elements} |
332 \begin{lstlisting} |
331 \begin{lstlisting} |
333 In []: sum(A) |
332 In []: sum(a) |
334 Out[]: 12 |
333 Out[]: 12 |
335 \end{lstlisting} |
334 \end{lstlisting} |
336 \end{frame} |
335 \end{frame} |
337 |
336 |
338 \begin{frame}[fragile] |
337 \begin{frame}[fragile] |
339 \frametitle{Matrix Addition} |
338 \frametitle{Matrix Addition} |
340 \begin{lstlisting} |
339 \begin{lstlisting} |
341 In []: B = array([[3,2,-1,5], |
340 In []: b = array([[3,2,-1,5], |
342 [2,-2,4,9], |
341 [2,-2,4,9], |
343 [-1,0.5,-1,-7], |
342 [-1,0.5,-1,-7], |
344 [9,-5,7,3]]) |
343 [9,-5,7,3]]) |
345 In []: A + B |
344 In []: a + b |
346 Out[]: |
345 Out[]: |
347 array([[ 4. , 3. , 1. , 4. ], |
346 array([[ 4. , 3. , 1. , 4. ], |
348 [ 4. , 3. , 3. , 0. ], |
347 [ 4. , 3. , 3. , 0. ], |
349 [ 1. , 1.5, -2. , -4. ], |
348 [ 1. , 1.5, -2. , -4. ], |
350 [ 10. , -8. , 9. , 10. ]]) |
349 [ 10. , -8. , 9. , 10. ]]) |
352 \end{frame} |
351 \end{frame} |
353 |
352 |
354 \begin{frame}[fragile] |
353 \begin{frame}[fragile] |
355 \frametitle{Elementwise Multiplication} |
354 \frametitle{Elementwise Multiplication} |
356 \begin{lstlisting} |
355 \begin{lstlisting} |
357 In []: A*B |
356 In []: a*b |
358 Out[]: |
357 Out[]: |
359 array([[ 3. , 2. , -2. , -5. ], |
358 array([[ 3. , 2. , -2. , -5. ], |
360 [ 4. , -10. , -4. , -81. ], |
359 [ 4. , -10. , -4. , -81. ], |
361 [ -2. , 0.5, 1. , -21. ], |
360 [ -2. , 0.5, 1. , -21. ], |
362 [ 9. , 15. , 14. , 21. ]]) |
361 [ 9. , 15. , 14. , 21. ]]) |
365 \end{frame} |
364 \end{frame} |
366 |
365 |
367 \begin{frame}[fragile] |
366 \begin{frame}[fragile] |
368 \frametitle{Matrix Multiplication} |
367 \frametitle{Matrix Multiplication} |
369 \begin{lstlisting} |
368 \begin{lstlisting} |
370 In []: dot(A,B) |
369 In []: dot(a, b) |
371 Out[]: |
370 Out[]: |
372 array([[ -6. , 6. , -6. , -3. ], |
371 array([[ -6. , 6. , -6. , -3. ], |
373 [-64. , 38.5, -44. , 35. ], |
372 [-64. , 38.5, -44. , 35. ], |
374 [ 36. , -13.5, 24. , 35. ], |
373 [ 36. , -13.5, 24. , 35. ], |
375 [ 58. , -26. , 34. , -15. ]]) |
374 [ 58. , -26. , 34. , -15. ]]) |
393 \end{frame} |
392 \end{frame} |
394 |
393 |
395 \begin{frame}[fragile] |
394 \begin{frame}[fragile] |
396 \frametitle{Determinant} |
395 \frametitle{Determinant} |
397 \begin{lstlisting} |
396 \begin{lstlisting} |
398 In []: det(A) |
397 In []: det(a) |
399 Out[]: 80.0 |
398 Out[]: 80.0 |
400 \end{lstlisting} |
399 \end{lstlisting} |
401 \end{frame} |
400 \end{frame} |
402 |
401 |
403 %%use S=array(X,Y) |
402 %%use S=array(X,Y) |
404 \begin{frame}[fragile] |
403 \begin{frame}[fragile] |
405 \frametitle{Eigenvalues and Eigen Vectors} |
404 \frametitle{Eigenvalues and Eigen Vectors} |
406 \begin{small} |
405 \begin{small} |
407 \begin{lstlisting} |
406 \begin{lstlisting} |
408 In []: E = array([[3,2,4],[2,0,2],[4,2,3]]) |
407 In []: e = array([[3,2,4],[2,0,2],[4,2,3]]) |
409 |
408 |
410 In []: eig(E) |
409 In []: eig(e) |
411 Out[]: |
410 Out[]: |
412 (array([-1., 8., -1.]), |
411 (array([-1., 8., -1.]), |
413 array([[-0.74535599, 0.66666667, -0.1931126 ], |
412 array([[-0.74535599, 0.66666667, -0.1931126 ], |
414 [ 0.2981424 , 0.33333333, -0.78664085], |
413 [ 0.2981424 , 0.33333333, -0.78664085], |
415 [ 0.59628479, 0.66666667, 0.58643303]])) |
414 [ 0.59628479, 0.66666667, 0.58643303]])) |
416 |
415 |
417 In []: eigvals(E) |
416 In []: eigvals(e) |
418 Out[]: array([-1., 8., -1.]) |
417 Out[]: array([-1., 8., -1.]) |
419 \end{lstlisting} |
418 \end{lstlisting} |
420 \end{small} |
419 \end{small} |
421 \end{frame} |
420 \end{frame} |
422 |
421 |
423 %% \begin{frame}[fragile] |
422 %% \begin{frame}[fragile] |
424 %% \frametitle{Computing Norms} |
423 %% \frametitle{Computing Norms} |
425 %% \begin{lstlisting} |
424 %% \begin{lstlisting} |
426 %% In []: norm(E) |
425 %% In []: norm(e) |
427 %% Out[]: 8.1240384046359608 |
426 %% Out[]: 8.1240384046359608 |
428 %% \end{lstlisting} |
427 %% \end{lstlisting} |
429 %% \end{frame} |
428 %% \end{frame} |
430 |
429 |
431 %% \begin{frame}[fragile] |
430 %% \begin{frame}[fragile] |
432 %% \frametitle{Singular Value Decomposition} |
431 %% \frametitle{Singular Value Decomposition} |
433 %% \begin{small} |
432 %% \begin{small} |
434 %% \begin{lstlisting} |
433 %% \begin{lstlisting} |
435 %% In []: svd(E) |
434 %% In []: svd(e) |
436 %% Out[]: |
435 %% Out[]: |
437 %% (array( |
436 %% (array( |
438 %% [[ -6.66666667e-01, -1.23702565e-16, 7.45355992e-01], |
437 %% [[ -6.66666667e-01, -1.23702565e-16, 7.45355992e-01], |
439 %% [ -3.33333333e-01, -8.94427191e-01, -2.98142397e-01], |
438 %% [ -3.33333333e-01, -8.94427191e-01, -2.98142397e-01], |
440 %% [ -6.66666667e-01, 4.47213595e-01, -5.96284794e-01]]), |
439 %% [ -6.66666667e-01, 4.47213595e-01, -5.96284794e-01]]), |