arrays.txt
author asokan <asokan@fossee.in>
Tue, 18 May 2010 15:40:17 +0530
changeset 126 2eac725a5766
parent 125 27ddf1255daa
permissions -rw-r--r--
changes to array.txt
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
67
806cca3b7231 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 61
diff changeset
     1
Hello friends and welcome to this tutorial on Matrices.
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
     2
67
806cca3b7231 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 61
diff changeset
     3
In python all matrix operations are done using arrays.
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
     4
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
     5
We saw in the previous session that arrays are better suited for mathematical operations. We saw this in the context of simple statistical functions such as mean.
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
     6
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
     7
In this session we shall see how to perform efficient matrix operations using arrays. We will create arrays, initialize them, manipulate them and perform simple image processing using them. For this tutorial we shall need the lena.png image. Hope you have the image with you. 
54
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
     8
72
7d4ed969a942 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 70
diff changeset
     9
Let's now start off. As you can see our lena image is on the desktop, so first let's navigate to the desktop by cd Desktop.
7d4ed969a942 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 70
diff changeset
    10
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    11
Let's now start Ipython, using the command python -pylab
72
7d4ed969a942 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 70
diff changeset
    12
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    13
First things first, let's start by creating a normal array. 
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    14
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    15
Type:
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    16
p equal to array([5, 8, 10, 13])
54
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
    17
67
806cca3b7231 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 61
diff changeset
    18
let's check the value of a by typing
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    19
p
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    20
Note how python displays an array, compared to a list.
54
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
    21
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    22
Here p is single dimension array, that is it has only one row. Let us now create a multi-dimensional array.
54
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
    23
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    24
Type:
54
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
    25
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    26
q = array([[11,12,13], [21,22,23], [31,32,33]])
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    27
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    28
both p and q are arrays but with different dimensions or shape
54
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
    29
we can check shape of arrays by using shape attribute of arrays.
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    30
p.shape
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    31
q.shape
54
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
    32
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    33
A few array initialization methods are also available to make life easier;
67
806cca3b7231 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 61
diff changeset
    34
say we want to create an array of size 3x4 with all the elements initialized to 1, we use
54
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
    35
b = ones((3, 4))
69
5452f6b11fe6 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 67
diff changeset
    36
and b is
54
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
    37
b
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    38
similarly, suppose we already have an array, and we want to create another array with the same shape but with initial values equal to one, for eg, to get an array similar in shape to the array 'q' but with all elements as 1 we type:
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    39
d = ones_like(q)
69
5452f6b11fe6 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 67
diff changeset
    40
and d is a 3x3 array with all values equal to 1
54
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
    41
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    42
Similarly there are functions like zeros and zeros_like which initialize array with all values being 0. One more useful function available is 'identity', it create unit matrix of given order
54
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
    43
i = identity(3)
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
    44
i
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
    45
69
5452f6b11fe6 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 67
diff changeset
    46
Note that identity takes just one argument since identity matrix is always a square matrix.
5452f6b11fe6 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 67
diff changeset
    47
55
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    48
----------------
69
5452f6b11fe6 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 67
diff changeset
    49
Now that we have covered creation of arrays, we shall see how to access and change values of particular elements. 
5452f6b11fe6 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 67
diff changeset
    50
Remember we created a 3x3 matrix earlier, 
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    51
q
55
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    52
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    53
to access the element 23 we type
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    54
q[1][2]
55
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    55
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    56
It is at the second row of the third column of the matrix/array q. Note that index values of arrays also start from 0.
69
5452f6b11fe6 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 67
diff changeset
    57
Alternatively, the more popular way of doing the same is
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    58
q[1, 2]
55
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    59
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    60
here ',' is used as separator for row and column value. Similarly any value from the array can be accessed.
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    61
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    62
to access particular row completely we specify the row value alone: 
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    63
q[1]
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    64
This gives us the entire second row. 
55
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    65
69
5452f6b11fe6 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 67
diff changeset
    66
We can assign a new value to an element, the same way we accessed it. For eg., 
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    67
q[1, 1] = -22
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    68
q
55
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    69
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    70
One of the most powerful aspects of a high-level language like python is the way it supports matrix operations. We can use them like we do it maths rather than think of them as elements like a programmer.
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    71
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    72
For example to change a whole row, we type:
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    73
q[1] = 0
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    74
q
69
5452f6b11fe6 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 67
diff changeset
    75
as we can see, all elements of the second row are now 0
55
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    76
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    77
In order to access a column, we need to syntactically indicate that the number given is the column index rather than the row index. In other words we need a placeholder for the row position. We cannot use space. That is we cannot say q[, 1] as that would be a syntax error ( q[m, n] being the method to access an _element_).
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    78
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    79
We have to say all rows and a column(s) by extending the slice notation seen earlier.
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    80
q[:,2]
69
5452f6b11fe6 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 67
diff changeset
    81
returns the third column.
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    82
Here the ':' part specifies the row numbers of the slice; as we have seen before by leaving the from and to parts of the slice empty we effectively say ALL. Thus q[:, n] stands for a matrix which is the submatrix obtained by taking all rows and  column n+1.
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    83
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    84
The row reference q[1] can also be written as q[1,:]
55
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    85
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    86
As we have seen ':' takes two values namely start and end. As before rows or columns starting from 'start' till 'end' --excluding end-- will be returned. Lets try some examples:
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    87
q [0:2,:]
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    88
results in a matrix which is rows 0 and 1 and all columns. Note here that 'end', in this case, '2' will not be included in resulting matrix.
55
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    89
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    90
Similarly q[1:3,:] 
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    91
gives second and third rows.
55
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    92
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    93
q[:, 0:2] gives us first two columns
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    94
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    95
This manner of accessing chunks of arrays is also known as 'slicing'. Since the idea is the same as slicing for lists the name is also the same.
55
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    96
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    97
As noted the default values for slices carry over.
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    98
Thus
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
    99
q[:, :2]
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   100
gives us the first two columns
55
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
   101
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   102
q[:, 1:] 
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   103
returns all columns excluding the 0th column.
55
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
   104
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   105
q[1:, :2]
69
5452f6b11fe6 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 67
diff changeset
   106
returns first two columns of all rows excepting the 0th row.
55
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
   107
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   108
When slicing lists we saw the idea of striding. Recall that if L is a list,
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   109
L[start : stop : step],
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   110
produces a new list whose first element is L[start] and has all elements whose index is start + n * step; stop signals the last index _before_ which we should stop.
55
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
   111
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   112
Matrices also support striding--that is skip, rows or columns by a certain interval. 
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   113
We add one more ':' to row or column part to specify a step size.
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   114
Let us type 
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   115
q[:, ::2]
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   116
and see what is shown.
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   117
The first colon specifies that we pick all rows, the comma signals that we start specifying columns. The empty places indicate defaults. That is start from the 0th and go to the end. The presence of step--in this case 2--tells us that we will select alternate columns. Thus we q[:, ::2] extracts all rows and alternate columns starting with 0th column.
55
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
   118
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   119
q[::2,:]
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   120
returns a 2x3 matrix with the first and the third row.
56
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   121
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   122
q[::2, ::2] 
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   123
gives us a 2x2 array with the first and the third rows and first and third columns. 
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   124
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   125
Lets us use slicing and striding for doing some basic image manipulation
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   126
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   127
pylab has a function named imread to read images. We shall use lena.png image for our experimentation. Its there on desktop. 
56
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   128
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   129
a = imread('lena.png')
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   130
Now a is an array with the RGB and Alpha channel values of each pixel
56
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   131
a.shape
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   132
tells us that 
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   133
it is an 512x512x4 array.
56
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   134
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   135
to view the image write
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   136
imshow(a)
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   137
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   138
lets try to crop the image to top left quarter. Since a is an array we can use slicing to get the top left quarter by
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   139
imshow(a[:256,:256]) (half of 512 is 256)
56
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   140
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   141
Let's crop the image so that only her face is visible. And to do that we'll need some rough estimates of the coordinates of the face.  
56
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   142
imshow(a)
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   143
now move your mouse pointer over the image, it gives us x, y coordinates of the mouse pointer's current location. With this we can get rough estimate of lena's face. We observe that Lena's face begins from somewhere around 200, 200 and ends at 400, 400. Now cropping to these boundaries is simple
56
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   144
imshow(a[200:400, 200:400])
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   145
69
5452f6b11fe6 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 67
diff changeset
   146
Next we shall try striding on this image. We shall resize the image by skipping alternate pixels. We have already seen how to skip alternate elements so,
56
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   147
imshow(a[::2, ::2])
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   148
note that the size of image is just 256x256. 
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   149
------------------
56
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   150
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   151
Till now we have covered initializing and accessing elements of arrays. Now we shall look at other manipulations for arrays. We start by creating a 4x4 array 
56
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   152
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   153
a = array([[ 1, 1, 2, -1],[ 2, 5, -1, -9], [ 2, 1, -1, 3], [ 1, -3, 2, 7]])
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   154
a
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   155
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   156
To get transpose of this matrix write
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   157
a.T
69
5452f6b11fe6 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 67
diff changeset
   158
5452f6b11fe6 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 67
diff changeset
   159
The sum() function returns sum of all the elements of a matrix.
56
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   160
sum(a)
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   161
69
5452f6b11fe6 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 67
diff changeset
   162
let's create one more array for checking more operations
56
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   163
b = array([[3,2,-1,5], [2,-2,4,9], [-1,0.5,-1,-7], [9,-5,7,3]])
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   164
69
5452f6b11fe6 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 67
diff changeset
   165
+ takes care of matrix additions
56
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   166
a + b
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   167
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   168
lets try multiplication now, 
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   169
a * b returns a new matrix whose elements are products of corresponding elements. THIS IS NOT matrix multiplication.
56
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   170
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   171
To get the usual product of matrices a and b we use
56
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   172
dot(a, b)
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   173
69
5452f6b11fe6 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 67
diff changeset
   174
To get the inverse of a matrix we use,
56
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   175
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   176
inv(a)
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   177
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   178
det(a) returns determinant of matrix a
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   179
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   180
we shall create an matrix e
56
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   181
e = array([[3,2,4],[2,0,2],[4,2,3]])
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   182
and then to evaluate eigenvalues of the same. 
69
5452f6b11fe6 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 67
diff changeset
   183
eig(e)
125
27ddf1255daa changes to array.txt
asokan <asokan@fossee.in>
parents: 75
diff changeset
   184
returns both eigen values and eigen vector of given matrix
56
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   185
to get only eigen values use
69
5452f6b11fe6 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 67
diff changeset
   186
eigvals(e)
56
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   187
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   188
This brings us to end of this session. We have covered Matrices
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   189
Initialization
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   190
Slicing
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   191
Striding
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   192
A bit of image processing
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   193
Functions available for arrays
61
38ef280b1408 Minor changes to arrays.txt.
Shantanu <shantanu@fossee.in>
parents: 56
diff changeset
   194
56
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   195
Thank you
55
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
   196