|
1 * Matrices |
|
2 *** Outline |
|
3 ***** Introduction |
|
4 ******* Why do we want to do that? |
|
5 ******* We shall use arrays (introduced before) for matrices |
|
6 ******* Arsenal Required |
|
7 ********* working knowledge of arrays |
|
8 ***** Various matrix operations |
|
9 ******* Transpose |
|
10 ******* Sum of all elements |
|
11 ******* Element wise operations |
|
12 ******* Matrix multiplication |
|
13 ******* Inverse of a matrix |
|
14 ******* Determinant |
|
15 ******* eigen values/vectors |
|
16 ******* svd |
|
17 ***** Other things available? |
|
18 *** Script |
|
19 Welcome. |
|
20 |
|
21 In this tutorial, you will learn how to perform some common matrix |
|
22 operations. We shall look at some of the functions available in |
|
23 pylab. Note that, this tutorial just scratches the surface and |
|
24 there is a lot more that can be done. |
|
25 |
|
26 Let's begin with finding the transpose of a matrix. |
|
27 |
|
28 In []: a = array([[ 1, 1, 2, -1], |
|
29 ...: [ 2, 5, -1, -9], |
|
30 ...: [ 2, 1, -1, 3], |
|
31 ...: [ 1, -3, 2, 7]]) |
|
32 |
|
33 In []: a.T |
|
34 |
|
35 Type a, to observe the change in a. |
|
36 In []: a |
|
37 |
|
38 Now we shall look at adding another matrix b, to a. It doesn't |
|
39 require anything special, just use the + operator. |
|
40 |
|
41 In []: b = array([[3, 2, -1, 5], |
|
42 [2, -2, 4, 9], |
|
43 [-1, 0.5, -1, -7], |
|
44 [9, -5, 7, 3]]) |
|
45 In []: a + b |
|
46 |
|
47 What do you expect would be the result, if we used * instead of |
|
48 the + operator? |
|
49 |
|
50 In []: a*b |
|
51 |
|
52 You get an element-wise product of the two arrays and not a matrix |
|
53 product. To get a matrix product, we use the dot function. |
|
54 |
|
55 In []: dot(a, b) |
|
56 |
|
57 The sum function returns the sum of all the elements of the |
|
58 array. |
|
59 |
|
60 In []: sum(a) |
|
61 |
|
62 The inv command returns the inverse of the matrix. |
|
63 In []: inv(a) |
|
64 |
|
65 In []: det(a) |
|
66 |
|
67 In []: eig(a) |
|
68 Returns the eigenvalues and the eigen vectors. |
|
69 |
|
70 In []: eigvals(a) |
|
71 Returns only the eigenvalues. |
|
72 |
|
73 In []: svd(a) |
|
74 Singular Value Decomposition |
|
75 |
|
76 *** Notes |
|
77 |