arrays.txt
author Shantanu <shantanu@fossee.in>
Thu, 15 Apr 2010 15:30:59 +0530
changeset 68 fe5d3fb83597
parent 67 806cca3b7231
child 70 c172d36d9a76
permissions -rw-r--r--
Changes to arrays presentation.
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.
806cca3b7231 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 61
diff changeset
     2
In python all matrix operations are done using arrays.
806cca3b7231 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 61
diff changeset
     3
We have already seen in previous session that how arrays are better suited for certain mathematical operations. In this session we are going to cover more details on using Arrays as matrices, such as, how to create them, how to initialize them, how to manipulate and use them for solving given problem.
54
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
     4
67
806cca3b7231 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 61
diff changeset
     5
First things first, let's start by creating a normal array, type:
806cca3b7231 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 61
diff changeset
     6
a equal to array([5, 8, 10, 13])
54
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
     7
67
806cca3b7231 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 61
diff changeset
     8
let's check the value of a by typing
54
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
     9
a
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
    10
67
806cca3b7231 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 61
diff changeset
    11
Here a is single dimension array, that is it has only one row. Let's now look at creating multi-dimensional arrays by
54
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
    12
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
    13
c = array([[11,12,13], [21,22,23], [31,32,33]])
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
    14
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
    15
both c and a are arrays but with different dimensions or shape
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
    16
we can check shape of arrays by using shape attribute of arrays.
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
    17
a.shape
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
    18
c.shape
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
    19
67
806cca3b7231 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 61
diff changeset
    20
A few other handy array initialization methods are also available to make life easier.
806cca3b7231 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 61
diff changeset
    21
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
    22
b = ones((3, 4))
61
38ef280b1408 Minor changes to arrays.txt.
Shantanu <shantanu@fossee.in>
parents: 56
diff changeset
    23
and b will be
54
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
    24
b
67
806cca3b7231 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 61
diff changeset
    25
similarly, say we already have an array, and we want to create one more array with the same shape and initial values to be one, for that we will use
54
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
    26
d = ones_like(c)
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
    27
and d will be 3x3 array with all values 1
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
    28
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
    29
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 identity matrix(array) of given order
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
    30
i = identity(3)
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
    31
i
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
    32
i = identity(5)
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
    33
i
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
    34
55
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    35
----------------
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    36
Now that we have covered creation of arrays, we will see how to access and change values of particular elements. 
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    37
We created one 3x3 matrix earlier, 
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    38
c
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    39
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    40
to access the element 23 we type
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    41
c[1][2]
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    42
68
fe5d3fb83597 Changes to arrays presentation.
Shantanu <shantanu@fossee.in>
parents: 67
diff changeset
    43
It is the second row, third column of c. Note that index values of arrays also start from 0.
55
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    44
Alternative and popular way of doing this is
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    45
c[1, 2]
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    46
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    47
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
    48
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    49
to access particular row completely we simply skip the column value
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    50
c[1]
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    51
will give us the entire second row.
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
The way by which we access one element of array, we use that itself to assign new value
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    54
c[1, 1] = -22
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    55
c
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    56
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    57
and same thing can be used for entire row
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    58
c[1] = 0
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    59
c
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    60
as we can see, second row all elements are now 0
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    61
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    62
Accessing a row is straight forward we skip column part
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    63
but to access whole column we have to use
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    64
c[:,2]
68
fe5d3fb83597 Changes to arrays presentation.
Shantanu <shantanu@fossee.in>
parents: 67
diff changeset
    65
it returns third column.
fe5d3fb83597 Changes to arrays presentation.
Shantanu <shantanu@fossee.in>
parents: 67
diff changeset
    66
here the ':' part mentioned for row value means entire row.
55
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    67
the c[1] we were using earlier can also be written as c[1,:]
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    68
68
fe5d3fb83597 Changes to arrays presentation.
Shantanu <shantanu@fossee.in>
parents: 67
diff changeset
    69
':' actually takes two value. for any row or column we can mention start:end values, and rows/columns starting for 'start' till 'end' will be returned. Lets try some examples for better understanding
55
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    70
c[0:2,:]
68
fe5d3fb83597 Changes to arrays presentation.
Shantanu <shantanu@fossee.in>
parents: 67
diff changeset
    71
will result in 2x3 array with rows starting from first(0) till second and all columns. Note here that 'end', in our case, '2' would not be included in resulting array.
55
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    72
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    73
c[1:3,:] 
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    74
gives second and third row.
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    75
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    76
similarly we can try this on columns also:
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    77
c[:, 0:2] gives us first two column
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    78
This whole concept of accessing chunks of arrays is known as 'slicing'
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    79
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    80
There is one more interesting and handy feature of slicing. We saw earlier that how only ':' means entire row or column.
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    81
It actually means if we don't specify start and end part of slice default is from zero to end.
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    82
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    83
so
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    84
c[:, :2]
68
fe5d3fb83597 Changes to arrays presentation.
Shantanu <shantanu@fossee.in>
parents: 67
diff changeset
    85
will also give us the first two columns
fe5d3fb83597 Changes to arrays presentation.
Shantanu <shantanu@fossee.in>
parents: 67
diff changeset
    86
and c[:, 1:] will return last columns of c
55
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    87
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    88
c[1:, :2]
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    89
returns first two columns of last two rows
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    90
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    91
Now we will look into one more powerful feature of arrays: 'striding'.
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    92
Striding allows us to jump or skip rows or columns by certain interval. We can specify the step size.
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    93
c[:,:] will give us entire array
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    94
we add one more ':' to row or column part to specify a step size.
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    95
c[:, ::2]
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    96
will give us first and third column. Since step size is two, it start with first column(blank before : means 0) and then we jump one column and then third(blank after : means end)
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    97
similarly 
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    98
c[::2,:] returns 2x3 array with first and third row
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
    99
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
   100
and c[::2, ::2] will give us 2x2 array with first and third row and column 
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
   101
56
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   102
Lets us try to use these concepts of slicing and striding for doing some basic image manipulation
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   103
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   104
pylab has a function imread to read images. We will use '(in)famous' lena image for our experimentation. Its there on desktop. 
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   105
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   106
a = imread('lena.png')
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   107
a is a numpy array with the 'RGB' values of each pixel
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   108
a.shape
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   109
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   110
its a 512x512x3 array.
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   111
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   112
to view the image write
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   113
imshow(a)
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   114
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   115
lets try to crop the image to top left quarter. Since a is a normal array we can use slicing to get top left quarter by
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   116
imshow(a[:255,:255]) (half of 512 is 256)
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   117
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   118
But hat is not 'interesting' part of lena. Lets crop the image so that only her face is visible. for that we will need some rough estimates of pixels. 
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   119
imshow(a)
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   120
now move your mouse cursor over the image, it will give us x, y coordinates where ever we take our cursor. We can get rough estimate of lena's face now cropping to those boundaries is simple
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   121
imshow(a[200:400, 200:400])
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   122
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   123
Next we will try striding on this image. We will resize the image by skipping each alternate pixel. We have already seen how to skip alternate elements so,
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   124
imshow(a[::2, ::2])
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   125
note now the size of image is just 256x256 and still quality of image is not much compromised.
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   126
-------------------------
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   127
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   128
Till now we have covered initializing and accessing elements of arrays. Now we shall concentrate on functions available for arrays. We start this by creating 4x4 array by
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   129
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   130
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
   131
a
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   132
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   133
To get transpose of this matrix write
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   134
a.T
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   135
sum() function returns sum of all the elements of a matrix.
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   136
sum(a)
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   137
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   138
lets create one more array for checking more operations
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   139
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
   140
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   141
+ will take care of matrix additions
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   142
a + b
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   143
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   144
lets try multiplication now, 
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   145
a * b will return element wise product of two matrices.
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   146
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   147
To get matrix product of a and b we use
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   148
dot(a, b)
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   149
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   150
and to get inverse of matrix 
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   151
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   152
inv(a)
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   153
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   154
det(a) returns determinant of matrix 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
we shall create one array e
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   157
e = array([[3,2,4],[2,0,2],[4,2,3]])
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   158
and then to evaluate eigenvalues of array
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   159
eig(a)
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   160
it returns both eigen values and eigen vector of given matrix
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   161
to get only eigen values use
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   162
eigvals(a)
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   163
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   164
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
   165
Initialization
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   166
Slicing
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   167
Striding
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   168
A bit of image processing
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   169
Functions available for arrays
61
38ef280b1408 Minor changes to arrays.txt.
Shantanu <shantanu@fossee.in>
parents: 56
diff changeset
   170
56
86c862b3dbef First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents: 55
diff changeset
   171
Thank you
55
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
   172
1fe734b20950 Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents: 54
diff changeset
   173
----------------
54
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   174
We have seen 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   175
    Welcome to the Tutorial on arrays. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   176
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   177
    As mentioned in [the previous tutorial] arrays are much faster and
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   178
    more efficient. In this tutorial we shall look at creating arrays,
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   179
    accessing elements and changing them. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   180
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   181
    ---
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   182
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   183
    Let's start with creating simple arrays. We've already seen how to
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   184
    convert lists to arrays. Inputting a new array is similar to that. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   185
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   186
    In []: 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   187
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   188
    Type /a/, to see what it is. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   189
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   190
    In []: a
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   191
    
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   192
    We enter a multi-dimensional array this way -
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   193
    
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   194
    In []: c = array([[11,12,13],
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   195
                     [21,22,23],
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   196
                      [31,32,33]])
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   197
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   198
    To see what c is, we just type c in the prompt. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   199
		      
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   200
    In []: c
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   201
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   202
    To see the dimensions of the array c, we use c.shape
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   203
    In []: c.shape 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   204
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   205
    Now let us look at some special methods of creating an
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   206
    array. There are various functions that allow us to create special
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   207
    arrays. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   208
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   209
    The first one we shall look at is, /arange/. /arange/ is similar to
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   210
    the range command, except that it returns an array and accepts
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   211
    float arguments. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   212
    
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   213
    In []: a = arange(10)
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   214
    
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   215
    In []: a
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   216
    This is the array we just created. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   217
    
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   218
    In []: a.shape
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   219
    Note that /a/ is one dimensional and has 10 elements, as expected. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   220
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   221
    We could also use a.shape to change the shape of the array a. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   222
    In []: a.shape = 2,5
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   223
    Note that the total size of new array must be unchanged. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   224
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   225
    We type a, to see what it looks like
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   226
    In []: a
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   227
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   228
    ones command can be used to get an array with all the entries as
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   229
    ones. We pass it the shape of the array that we require. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   230
    
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   231
    In []: 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   232
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   233
    Look at b, by printing it out. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   234
    In []: b 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   235
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   236
    To create an array with all entries as ones, with it's shape
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   237
    similar to an already existing array, we use the ones_like
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   238
    command.  
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   239
    In []: b = ones_like(a)
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   240
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   241
    zeros and zeros_like are similar commands that can give you arrays
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   242
    with all zeros. empty and empty_like give you empty arrays (arrays
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   243
    with no initialization done.)
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   244
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   245
    In []: b = zeros((3, 4))
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   246
    In []: b = zeros_like(a)
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   247
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   248
    The identity command can be used to obtain a square array with
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   249
    ones on the main diagonal. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   250
    
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   251
    In []: identity(3)
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   252
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   253
    To obtain a 2-D array, that is not necessarily square, eye command
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   254
    can be used. Look at the documentation of eye (using eye?) for
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   255
    more info. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   256
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   257
    ---
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   258
    
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   259
    Now that we have learnt how to create arrays, let's move on to
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   260
    accessing elements and changing them. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   261
    
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   262
    Let's work with the c, array which we had already created. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   263
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   264
    In []: c 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   265
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   266
    Let's say we want to access the element 23 in c, we say
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   267
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   268
    In []: c[1][2]
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   269
    Note that this is similar to accessing an element inside a list of
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   270
    lists. Also, note that counting again starts from 0. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   271
    
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   272
    But arrays provide a more convenient way to access the elements. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   273
    In []: c[1, 2]
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   274
    
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   275
    Now, we can also change the element using a simple assignment. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   276
    In []: c[1, 2] = -23
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   277
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   278
    Let's look at accessing more than one elements at a time. We begin
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   279
    with accessing rows. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   280
    In []: c[1] gives us the second row. (counting starts from 0)
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   281
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   282
    To get a column, we use a syntax that is similar to the one used
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   283
    to access a single element. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   284
    In []: c[:,1], gives us the first column. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   285
    
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   286
    The colon specifies that we wish to obtain all elements in that
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   287
    dimension from the array.  
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   288
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   289
    So, we could use a more explicit way to access the second row of
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   290
    the array. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   291
    In []: c[1,:]
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   292
    
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   293
    The colon can be used to access specific portions of the array,
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   294
    similar to the way we do with lists. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   295
    In []: c[1,1:3]
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   296
    Observe that we get the second and third columns from the second
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   297
    row. As with lists, the number after the colon is excluded when
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   298
    slicing a portion of the array. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   299
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   300
    In []: c[1:3,1]
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   301
    Now, we get the second and third rows from the first column. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   302
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   303
    In []: c[1:3,1:3]
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   304
    We get the second and third rows and the second and third
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   305
    columns. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   306
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   307
    The numbers before and after the colons are optional. If the
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   308
    number before the colon is omitted, it is assumed to be zero by
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   309
    default. If the element after the colon is omitted, it is assumed
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   310
    to be until the end. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   311
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   312
    In []: c[1:, 1:]
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   313
    This is essentially similar to the previous example. We are using
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   314
    the default value i.e, the end, instead of specifying 3,
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   315
    explicitly. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   316
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   317
    In []: c[:2, :2]
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   318
    We have omitted specifying the zero before the colon, explicitly. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   319
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   320
    --- 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   321
    
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   322
    You may have observed the similarity of the semi-colon notation to
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   323
    the notation used in lists. As expected, the semi-colon notation
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   324
    also provides a way to specify a jump. This {concept/idea} is
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   325
    termed as Striding. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   326
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   327
    To get every alternate row of c, starting from the first one, we say
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   328
    In []: c[::2,:]
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   329
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   330
    To get every alternate row of c, starting from the second one, we
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   331
    say 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   332
    In []: c[1::2,:]
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   333
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   334
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   335
    In []: c[:,::2]
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   336
    In []: c[::2,::2]
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   337
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   338
    ---
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   339
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   340
    We come to the end of this tutorial on arrays. In this tutorial,
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   341
    you've learnt how to create arrays and access, change elements. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   342
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   343
    Thank you. 
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   344
40
6e3526275a00 Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff changeset
   345
Hello friends and welcome to the second tutorial in the series of spoken tutorials on Python for Scientific computing. 
6e3526275a00 Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff changeset
   346
6e3526275a00 Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff changeset
   347
In the previous tutorial we learnt about arrays and we told you that numpy arrays are faster and more efficient . In this tutorial we shall look at creating arrays, accessing elements and changing them. 
6e3526275a00 Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff changeset
   348
6e3526275a00 Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff changeset
   349
6e3526275a00 Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff changeset
   350
Let's start with creating simple arrays. We've already seen how to convert lists to arrays. Inputting a new array is similarto that. 
6e3526275a00 Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff changeset
   351
54
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   352
On your Ipython terminal type a = array open parenthesis and then open square brackets 5,8,10,13 close square brackets and close parenthesis . This create an array a . You can see what a is by typing a on the terminal .
40
6e3526275a00 Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff changeset
   353
Now we will try to create a multi-dimensional array type in your ipython terminal
6e3526275a00 Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff changeset
   354
c= array open parenthesis , then open square brackets 11,12,13 close square bracket 'comma' start square bracket 21 , 22 ,23close square bracket 'comma' open 31,32,33 close square bracket another close square bracket which closes the first sqaure bracket and parenthesis which closes the first parenthesis . Now to see the dimensions of the array c we will do c.shape . We can see that c is a 3 by 3 matrix .
6e3526275a00 Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff changeset
   355
6e3526275a00 Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff changeset
   356
There are other special methods of creating arrays as well we will now look at them .
6e3526275a00 Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff changeset
   357
The first one is the command arange which is similar to range except that it returns an array.
6e3526275a00 Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff changeset
   358
We will type on our Ipython interpreter a = arange(10). We will see what a is now . Type a . As we can see This returns us an array of one dimension and has 10 elements .  
6e3526275a00 Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff changeset
   359
Ones can be use to get all entries as ones . We can pass it the shape of the array as required .
6e3526275a00 Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff changeset
   360
type b=ones open parenthesis , another open parenthesis , 3,4 , close second parenthesis and close first parenthesis . Look at b , by printing it out .
6e3526275a00 Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff changeset
   361
To create an array with all entries as ones, with it's shape similar to an already existing array, we use the ones_like
54
46a3575919f5 Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents: 40
diff changeset
   362
command.  type b= ones_like in parenthesis a .