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} |
|
151 |
|
152 \begin{frame}[fragile] |
|
153 \frametitle{Accessing elements of matrices} |
|
154 \begin{small} |
|
155 \begin{lstlisting} |
|
156 In []: C = array([[1,1,2], |
|
157 [2,4,1], |
|
158 [-1,3,7]]) |
|
159 In []: C[1,2] |
|
160 Out[]: 1 |
|
161 |
|
162 In []: C[1] |
|
163 Out[]: array([2, 4, 1]) |
|
164 |
|
165 In []: C[1,1] = -2 |
|
166 In []: C |
|
167 Out[]: |
|
168 array([[ 1, 1, 2], |
|
169 [ 2, -2, 1], |
|
170 [-1, 3, 7]]) |
|
171 \end{lstlisting} |
|
172 \end{small} |
150 \end{frame} |
173 \end{frame} |
151 |
174 |
152 \subsection{Basic Operations} |
175 \subsection{Basic Operations} |
153 |
176 |
154 \begin{frame}[fragile] |
177 \begin{frame}[fragile] |
155 \frametitle{Transpose of a Matrix} |
178 \frametitle{Transpose of a Matrix} |
156 \begin{lstlisting} |
179 \begin{lstlisting} |
157 In []: linalg.transpose(A) |
180 In []: A.T |
158 Out[]: |
181 Out[]: |
159 matrix([[ 1, 2, 2, 1], |
182 array([[ 1, 2, 2, 1], |
160 [ 1, 5, 1, -3], |
183 [ 1, 5, 1, -3], |
161 [ 2, -1, -1, 2], |
184 [ 2, -1, -1, 2], |
162 [-1, -9, 3, 7]]) |
185 [-1, -9, 3, 7]]) |
163 \end{lstlisting} |
186 \end{lstlisting} |
164 \end{frame} |
187 \end{frame} |
165 |
188 |
166 \begin{frame}[fragile] |
189 \begin{frame}[fragile] |
167 \frametitle{Sum of all elements} |
190 \frametitle{Sum of all elements} |
168 \begin{lstlisting} |
191 \begin{lstlisting} |
169 In []: linalg.sum(A) |
192 In []: sum(A) |
170 Out[]: 12 |
193 Out[]: 12 |
171 \end{lstlisting} |
194 \end{lstlisting} |
172 \end{frame} |
195 \end{frame} |
173 |
196 |
174 \begin{frame}[fragile] |
197 \begin{frame}[fragile] |
175 \frametitle{Matrix Addition} |
198 \frametitle{Matrix Addition} |
176 \begin{lstlisting} |
199 \begin{lstlisting} |
177 In []: B = matrix([[3,2,-1,5], |
200 In []: B = array([[3,2,-1,5], |
178 [2,-2,4,9], |
201 [2,-2,4,9], |
179 [-1,0.5,-1,-7], |
202 [-1,0.5,-1,-7], |
180 [9,-5,7,3]]) |
203 [9,-5,7,3]]) |
181 In []: linalg.add(A,B) |
204 In []: A + B |
182 Out[]: |
205 Out[]: |
183 matrix([[ 4. , 3. , 1. , 4. ], |
206 array([[ 4. , 3. , 1. , 4. ], |
184 [ 4. , 3. , 3. , 0. ], |
207 [ 4. , 3. , 3. , 0. ], |
185 [ 1. , 1.5, -2. , -4. ], |
208 [ 1. , 1.5, -2. , -4. ], |
186 [ 10. , -8. , 9. , 10. ]]) |
209 [ 10. , -8. , 9. , 10. ]]) |
187 \end{lstlisting} |
210 \end{lstlisting} |
188 \end{frame} |
211 \end{frame} |
189 |
212 |
190 \begin{frame}[fragile] |
213 \begin{frame}[fragile] |
|
214 \frametitle{Elementwise Multiplication} |
|
215 \begin{lstlisting} |
|
216 In []: A*B |
|
217 Out[]: |
|
218 array([[ 3. , 2. , -2. , -5. ], |
|
219 [ 4. , -10. , -4. , -81. ], |
|
220 [ -2. , 0.5, 1. , -21. ], |
|
221 [ 9. , 15. , 14. , 21. ]]) |
|
222 |
|
223 \end{lstlisting} |
|
224 \end{frame} |
|
225 |
|
226 \begin{frame}[fragile] |
191 \frametitle{Matrix Multiplication} |
227 \frametitle{Matrix Multiplication} |
192 \begin{lstlisting} |
228 \begin{lstlisting} |
193 In []: linalg.multiply(A, B) |
229 In []: dot(A,B) |
194 Out[]: |
230 Out[]: |
195 matrix([[ 3. , 2. , -2. , -5. ], |
231 array([[ -6. , 6. , -6. , -3. ], |
196 [ 4. , -10. , -4. , -81. ], |
232 [-64. , 38.5, -44. , 35. ], |
197 [ -2. , 0.5, 1. , -21. ], |
233 [ 36. , -13.5, 24. , 35. ], |
198 [ 9. , 15. , 14. , 21. ]]) |
234 [ 58. , -26. , 34. , -15. ]]) |
199 \end{lstlisting} |
235 \end{lstlisting} |
200 \end{frame} |
236 \end{frame} |
201 |
237 |
202 \begin{frame}[fragile] |
238 \begin{frame}[fragile] |
203 \frametitle{Inverse of a Matrix} |
239 \frametitle{Inverse of a Matrix} |
|
240 \begin{lstlisting} |
|
241 In []: inv(A) |
|
242 \end{lstlisting} |
204 \begin{small} |
243 \begin{small} |
205 \begin{lstlisting} |
244 \begin{lstlisting} |
206 In []: linalg.inv(A) |
245 Out[]: |
207 Out[]: |
246 array([[-0.5 , 0.55, -0.15, 0.7 ], |
208 matrix([[-0.5 , 0.55, -0.15, 0.7 ], |
247 [ 0.75, -0.5 , 0.5 , -0.75], |
209 [ 0.75, -0.5 , 0.5 , -0.75], |
248 [ 0.5 , -0.15, -0.05, -0.1 ], |
210 [ 0.5 , -0.15, -0.05, -0.1 ], |
249 [ 0.25, -0.25, 0.25, -0.25]]) |
211 [ 0.25, -0.25, 0.25, -0.25]]) |
|
212 \end{lstlisting} |
250 \end{lstlisting} |
213 \end{small} |
251 \end{small} |
214 \end{frame} |
252 \end{frame} |
215 |
253 |
216 \begin{frame}[fragile] |
254 \begin{frame}[fragile] |
217 \frametitle{Determinant} |
255 \frametitle{Determinant} |
218 \begin{lstlisting} |
256 \begin{lstlisting} |
219 In []: det(A) |
257 In []: det(A) |
220 Out[66]: 80.0 |
258 Out[]: 80.0 |
221 \end{lstlisting} |
259 \end{lstlisting} |
222 \end{frame} |
260 \end{frame} |
223 |
261 |
224 \begin{frame}[fragile] |
262 %%use S=array(X,Y) |
225 \frametitle{Eigen Values and Eigen Matrix} |
263 \begin{frame}[fragile] |
|
264 \frametitle{Eigenvalues and Eigen Vectors} |
226 \begin{small} |
265 \begin{small} |
227 \begin{lstlisting} |
266 \begin{lstlisting} |
228 In []: E = matrix([[3,2,4],[2,0,2],[4,2,3]]) |
267 In []: E = array([[3,2,4],[2,0,2],[4,2,3]]) |
229 |
268 |
230 In []: linalg.eig(E) |
269 In []: eig(E) |
231 Out[]: |
270 Out[]: |
232 (array([-1., 8., -1.]), |
271 (array([-1., 8., -1.]), |
233 matrix([[-0.74535599, 0.66666667, -0.1931126 ], |
272 array([[-0.74535599, 0.66666667, -0.1931126 ], |
234 [ 0.2981424 , 0.33333333, -0.78664085], |
273 [ 0.2981424 , 0.33333333, -0.78664085], |
235 [ 0.59628479, 0.66666667, 0.58643303]])) |
274 [ 0.59628479, 0.66666667, 0.58643303]])) |
236 |
275 |
237 In []: linalg.eigvals(E) |
276 In []: eigvals(E) |
238 Out[]: array([-1., 8., -1.]) |
277 Out[]: array([-1., 8., -1.]) |
239 \end{lstlisting} |
278 \end{lstlisting} |
240 \end{small} |
279 \end{small} |
241 \end{frame} |
280 \end{frame} |
242 |
281 |
243 \begin{frame}[fragile] |
282 \begin{frame}[fragile] |
244 \frametitle{Computing Norms} |
283 \frametitle{Computing Norms} |
245 \begin{lstlisting} |
284 \begin{lstlisting} |
246 In []: linalg.norm(E) |
285 In []: norm(E) |
247 Out[]: 8.1240384046359608 |
286 Out[]: 8.1240384046359608 |
248 \end{lstlisting} |
287 \end{lstlisting} |
249 \end{frame} |
288 \end{frame} |
250 |
289 |
251 \begin{frame}[fragile] |
290 \begin{frame}[fragile] |
252 \frametitle{Single Value Decomposition} |
291 \frametitle{Singular Value Decomposition} |
253 \begin{small} |
292 \begin{small} |
254 \begin{lstlisting} |
293 \begin{lstlisting} |
255 In [76]: linalg.svd(E) |
294 In []: svd(E) |
256 Out[76]: |
295 Out[]: |
257 (matrix( |
296 (array( |
258 [[ -6.66666667e-01, -1.23702565e-16, 7.45355992e-01], |
297 [[ -6.66666667e-01, -1.23702565e-16, 7.45355992e-01], |
259 [ -3.33333333e-01, -8.94427191e-01, -2.98142397e-01], |
298 [ -3.33333333e-01, -8.94427191e-01, -2.98142397e-01], |
260 [ -6.66666667e-01, 4.47213595e-01, -5.96284794e-01]]), |
299 [ -6.66666667e-01, 4.47213595e-01, -5.96284794e-01]]), |
261 array([ 8., 1., 1.]), |
300 array([ 8., 1., 1.]), |
262 matrix([[-0.66666667, -0.33333333, -0.66666667], |
301 array([[-0.66666667, -0.33333333, -0.66666667], |
263 [-0. , 0.89442719, -0.4472136 ], |
302 [-0. , 0.89442719, -0.4472136 ], |
264 [-0.74535599, 0.2981424 , 0.59628479]])) |
303 [-0.74535599, 0.2981424 , 0.59628479]])) |
265 \end{lstlisting} |
304 \end{lstlisting} |
266 \end{small} |
305 \end{small} |
267 \inctime{15} |
306 \inctime{15} |