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