arrays.org
changeset 8 e5194abc864f
parent 4 4dee50d4804b
child 10 a63d7dcba725
equal deleted inserted replaced
7:9794cc414498 8:e5194abc864f
       
     1 * Arrays
       
     2 *** Outline
       
     3 ***** Introduction
       
     4 ******* Why do we want to do that?
       
     5 ******* We shall use arrays (introduced before) for matrices
       
     6 ******* Arsenal Required
       
     7 ********* working knowledge of lists
       
     8 ***** Initializing matrices
       
     9 ******* Entering element-wise
       
    10 ******* using special functions
       
    11 ***** Accessing and Changing elements
       
    12 ******* Slicing
       
    13 ******* Striding
       
    14 ***** {{Simple operations??}}
       
    15 *** Script
       
    16     Welcome to the Tutorial on arrays. 
       
    17 
       
    18     As mentioned in [the previous tutorial] arrays are much faster and
       
    19     more efficient. In this tutorial we shall look at creating arrays,
       
    20     accessing elements and changing them. 
       
    21 
       
    22     ---
       
    23 
       
    24     Let's start with creating simple arrays. We've already seen how to
       
    25     convert lists to arrays. Entering an array is similar to that. 
       
    26 
       
    27     a = array([5, 8, 10, 13])
       
    28     
       
    29     We enter a multi-dimensional array this way -
       
    30     
       
    31     In []: c = array([[11,12,13],
       
    32                       [21,22,23],
       
    33                       [31,32,33]])
       
    34 
       
    35     To see what c is, we just type c in the prompt. 
       
    36 		      
       
    37     In []: c
       
    38 
       
    39     To see the dimensions of the array c, we use c.shape
       
    40     In []: c.shape 
       
    41 
       
    42     Now let us look at some special methods of creating an
       
    43     array. There are various functions that allow us to create special
       
    44     arrays. 
       
    45 
       
    46     The first one we shall look at is, arange. arange is similar to
       
    47     the range command, except that it returns an array and accepts
       
    48     float arguments. [is range covered?]
       
    49     
       
    50     In []: a = arange(10)
       
    51     
       
    52     In []: a
       
    53     This is the array we just created. 
       
    54     
       
    55     In []: a.shape
       
    56     Note that a is one dimensional and has 10 elements, as expected. 
       
    57 
       
    58     We could also use a.shape to change the shape of the array a. 
       
    59     In []: a.shape = 2,5
       
    60     Note that the total size(number of elements) of new array must 
       
    61     be unchanged. 
       
    62 
       
    63     We check re-shaped 'a' by
       
    64     In []: a
       
    65 
       
    66     'ones' function can be used to get an array with all the entries as
       
    67     1s. We pass it the shape of the required array. For ex. 
       
    68     
       
    69     In []: b = ones((3, 4))
       
    70 
       
    71     b is 3(cross)4 array with all 1s
       
    72     In []: b 
       
    73 
       
    74     To create an array with all entries as ones, with a shape similar to
       
    75     an already existing array, we use the ones_like function.
       
    76     In []: b = ones_like(a)
       
    77 
       
    78     zeros and zeros_like are similar commands that can give you arrays
       
    79     with all zeros. empty and empty_like give you empty arrays (arrays
       
    80     with no initialization done.)
       
    81 
       
    82     {Do an up arrow and quickly show them a couple of examples?}
       
    83 
       
    84     The identity command can be used to obtain a square array with
       
    85     ones on the main diagonal. 
       
    86     
       
    87     In []: identity(3)
       
    88 
       
    89     To obtain a 2-D array, that is not necessarily square, eye command
       
    90     can be used. Look at the documentation of eye (using eye?) for
       
    91     more info. 
       
    92 
       
    93     ---
       
    94     
       
    95     Now that we have learnt how to create arrays, lets move on to
       
    96     accessing elements and changing them. 
       
    97     
       
    98     Let's work with the c, array which we had already created. 
       
    99 
       
   100     In []: c 
       
   101 
       
   102     Let's say we want to access the element 23 in c(second row 
       
   103     third column), we say
       
   104 
       
   105     In []: c[1][2]
       
   106     Note that this is similar to accessing an element inside a list of
       
   107     lists. Also, note that counting again starts from 0. 
       
   108     
       
   109     Additionally arrays provide a more convenient way to access the 
       
   110     elements. 
       
   111     In []: c[1, 2]
       
   112     
       
   113     Now, we can also change the element using a simple assignment. 
       
   114     In []: c[1, 2] = -23
       
   115 
       
   116     Let's look at accessing more than one elements at a time. We begin
       
   117     with accessing rows. 
       
   118     In []: c[1] gives us the second row. (counting starts from 0)
       
   119 
       
   120     To get a column, we use a syntax that is similar to the one used
       
   121     to access a single element. 
       
   122     In []: c[:,1], gives us the first column. 
       
   123     
       
   124     The colon is a way to tell python to get all elements in that
       
   125     dimension from the array. 
       
   126 
       
   127     So, we could use a more explicit way to access a row of a
       
   128     In []: c[1,:]
       
   129     
       
   130     We could use the colon to access specific portions of an array. 
       
   131     In []: c[1,1:2]
       
   132     second column, from second row(1) till third(2) and excluding it
       
   133     In []: c[1:2,1]
       
   134     In []: c[1:2,1:2]
       
   135     ...
       
   136     [Oh, by the way this is termed as slicing. :)]
       
   137     {How many examples should we show here?}
       
   138 
       
   139     --- 
       
   140     
       
   141     You may have observed the similarity of the semi-colon notation to
       
   142     the range command. As expected, the semi-colon notation also
       
   143     provides a way to specify a jump. This {concept/idea} is termed as
       
   144     Striding. 
       
   145 
       
   146     To get every alternate row of c, starting from the first one, we say
       
   147     In []: c[::2,:]
       
   148 
       
   149     To get every alternate row of c, starting from the second one, we
       
   150     say
       
   151     In []: c[1::2,:]
       
   152 
       
   153 
       
   154     In []: c[:,::2]
       
   155     In []: c[::2,::2]
       
   156     {How many examples should we show here?}
       
   157 
       
   158     ---
       
   159 
       
   160     We come to the end of this tutorial on arrays. In this tutorial,
       
   161     you've learnt how to create arrays and access, change elements. 
       
   162 
       
   163     Thank you. 
       
   164 
       
   165 *** Notes