getting-started-with-arrays/script.rst
changeset 521 88a01948450d
parent 476 cb587df417bd
equal deleted inserted replaced
520:8249ae9d570a 521:88a01948450d
    18 ..   #. getting started with lists.
    18 ..   #. getting started with lists.
    19      
    19      
    20 ..  Author: Anoop Jacob Thomas <anoop@fossee.in>
    20 ..  Author: Anoop Jacob Thomas <anoop@fossee.in>
    21     Internal Reviewer   : Puneeth 
    21     Internal Reviewer   : Puneeth 
    22     External Reviewer   :
    22     External Reviewer   :
    23     Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
    23     Language Reviewer   : Bhanukiran
       
    24     Checklist OK?       : <11-11-2010,Anand, OK > [2010-10-05]
    24 
    25 
    25 ===========================
    26 ===========================
    26 Getting started with Arrays
    27 Getting started with Arrays
    27 ===========================
    28 ===========================
    28 
    29 
    32 
    33 
    33 Welcome to the spoken tutorial on getting started with arrays.
    34 Welcome to the spoken tutorial on getting started with arrays.
    34 
    35 
    35 {{{ switch to next slide, outline slide }}}
    36 {{{ switch to next slide, outline slide }}}
    36 
    37 
    37 In this tutorial, we will learn about arrays, we will learn how to convert
    38 In this tutorial, we will learn about the data structure called an array, how to convert
    38 a list into an array array operations and also why an array is preferred
    39 a list into an array, operations on arrays and also why an array is preferred
    39 over lists.
    40 to lists.
    40 
    41 
    41 .. #[Puneeth: Fix the grammar above.]
    42 .. #[Puneeth: Fix the grammar above.]
    42 
    43 
    43 {{{ switch to next slide on overview of array }}}
    44 {{{ switch to next slide on overview of array }}}
    44 
    45 
    45 Arrays are homogeneous data structures. Unlike lists, arrays cannot have
    46 Arrays are homogeneous data structures. Unlike lists, arrays cannot have
    46 heterogeneous data elements, that is, it can have only one type of data
    47 heterogeneous data elements, that is, they can have only one type of data 
    47 type, either all integers, or strings, or float, and not a mix.
    48 as their entries, be them all integers, strings, or maybe floats, but not a mix.
    48 
    49 
    49 .. #[Puneeth: Use multiple short sentences, rather than one long sentence
    50 .. #[Puneeth: Use multiple short sentences, rather than one long sentence
    50    I would've written something like this. 
    51    I would've written something like this. 
    51 
    52 
    52    Unlike lists, arrays are homogeneous data structures. They can have only
    53    Unlike lists, arrays are homogeneous data structures. They can have only
    53    type of data, ....]
    54    type of data, ....]
    54 
    55 
    55 Arrays are really fast in mathematical operations when compared to lists,
    56 Arrays of a given length are comparatively much faster in mathematical
    56 because of the same type of data in arrays.
    57 operations than lists of the same length, because of the fact that they are
       
    58 homogeneous data structures.
    57 
    59 
    58 .. #[Puneeth: For what size of an array is that the comparison?
    60 .. #[Puneeth: For what size of an array is that the comparison?
    59 
    61 
    60 {{{ switch to the next slide, creating arrays }}}
    62 {{{ switch to the next slide, creating arrays }}}
    61 
    63 
    75 
    77 
    76 ::
    78 ::
    77 
    79 
    78     a1 = array([1,2,3,4])
    80     a1 = array([1,2,3,4])
    79 
    81 
    80 Notice that here we created a one dimensional array. Also notice the object
    82 Notice that we created a one dimensional array here. Also notice the object
    81 we passed to create an array. We passed a list to create an array. 
    83 we passed to create an array. We passed a list to create an array. 
    82 
    84 
    83 Now let us see how to create a two dimensional array. Pause here and try to
    85 Now let us see how to create a two dimensional array. Pause here and try to
    84 do it yourself before looking at the solution.
    86 do it yourself before looking at the solution.
    85 
    87 
   110     ar = arange(1,9)
   112     ar = arange(1,9)
   111 
   113 
   112 .. #[Puneeth: say, creating the same array as before. for some time I got
   114 .. #[Puneeth: say, creating the same array as before. for some time I got
   113 .. confused .]
   115 .. confused .]
   114 
   116 
   115 And we obtained a single dimensional array with elements from 1 to 8.
   117 And we obtained a one dimensional array with elements from 1 to 8.
   116 
   118 
   117 ::
   119 ::
   118 
   120 
   119     print ar
   121     print ar
   120 
   122 
   121 .. #[Puneeth: be consistent with voice. say, we obtained... or something.]
   123 .. #[Puneeth: be consistent with voice. say, we obtained... or something.]
   122 
   124 
   123 And how can we make it a two dimensional array of order 2 by 4. Pause here
   125 And how can we make it a two dimensional array of order 2 by 4? Pause here
   124 and try to do it yourself, try ``ar.tab`` and find a suitable method for
   126 and try to do it yourself, try ``ar.tab`` and find a suitable method for
   125 that.
   127 that.
   126 
   128 
   127 {{{ switch to next slide, reshape() method }}}
   129 {{{ switch to next slide, reshape() method }}}
   128 
   130 
   152     a3 = array(l1)
   154     a3 = array(l1)
   153 
   155 
   154 
   156 
   155 {{{ switch to the next slide, problem statement of unsolved exercise 1 }}}
   157 {{{ switch to the next slide, problem statement of unsolved exercise 1 }}}
   156 
   158 
   157 Create a three dimensional array of the order (2,2,4).
   159 Create a three dimensional array of the shape (2,2,4).
   158 
   160 
   159 .. #[Puneeth: s/order/shape or size ?]
   161 .. #[Puneeth: s/order/shape or size ?]
   160 
   162 
   161 {{{ switch to the next slide, shape of an array }}}
   163 {{{ switch to the next slide, shape of an array }}}
   162 
   164 
   163 To find the shape of an array we can use the object ``.shape``, let us
   165 To find the shape of an array we can use the method ``.shape``, let us
   164 check the shape of the arrays we have created so far,
   166 check the shape of the arrays we have created so far,
   165 
   167 
   166 .. #[Puneeth: s/object/method ?]
   168 .. #[Puneeth: s/object/method ?]
   167 
   169 
   168 ::
   170 ::
   198 
   200 
   199 ::
   201 ::
   200 
   202 
   201     a4 = array([1,2,3,'a string'])
   203     a4 = array([1,2,3,'a string'])
   202 
   204 
   203 Well, we expected an error as previously I said that an array can have only
   205 Well, we would expect an error as it has been previously mentioned that arrays handle
   204 homogeneous elements, but it didn't give an error. Let us check the values
   206 elements with the same datatype, but it didn't raise an error. Let us check the values
   205 in the new array created. In your IPython terminal type, 
   207 in the new array created. In your IPython terminal type, 
   206 ::
   208 ::
   207 
   209 
   208     a4
   210     a4
   209 
   211 
   216 
   218 
   217 .. #[Puneeth: You may want to mention that float is the default dtype.]
   219 .. #[Puneeth: You may want to mention that float is the default dtype.]
   218 
   220 
   219 {{{ highlight all the array elements one by one using mouse movements }}}
   221 {{{ highlight all the array elements one by one using mouse movements }}}
   220 
   222 
   221 all the elements have been implicitly type casted as string, though our
   223 all the elements have been implicitly type casted as strings, though our
   222 first three elements were integers.
   224 first three elements were meant to be integers.
   223 
   225 
   224 .. #[Puneeth: when I type a4 it says some ``dtype`` etc. I don't understand
   226 .. #[Puneeth: when I type a4 it says some ``dtype`` etc. I don't understand
   225 .. what it is, can you explain? ;)]
   227 .. what it is, can you explain? ;)]
   226 
   228 
   227 {{{ switch to the next slide, identity & zeros methods }}}
   229 {{{ switch to the next slide, identity & zeros methods }}}
   228 
   230 
   229 .. #[Puneeth: something needs to motivate this. why are we suddenly talking
   231 .. #[Puneeth: something needs to motivate this. why are we suddenly talking
   230 .. of an identity matrix?]
   232 .. of an identity matrix?]
   231 
   233 
   232 Now let us see how to create identity matrix, an identity matrix is a
   234 Now let us see how to create an identity matrix of a given size, that is a
   233 square matrix in which all the diagonal elements are one and rest of the
   235 two-dimensional array  in  which all the diagonal elements are ones and rest of the
   234 elements zero. We can create an identity matrix using the method
   236 elements are zeros. We can create an identity matrix using the function
   235 ``identity()``.
   237 ``identity()``.
   236 
   238 
   237 The function ``identity()`` takes an integer argument,
   239 The function ``identity()`` takes an integer argument which specifies the
       
   240 size of the desired matrix,
   238 
   241 
   239 ::
   242 ::
   240 
   243 
   241     identity(3)
   244     identity(3)
   242 
   245 
   243 As you can see the identity method returned a three by three square matrix
   246 As you can see the identity function returned a three by three square matrix
   244 with all the diagonal elements as one and the rest of the elements as zero.
   247 with all the diagonal elements as ones and the rest of the elements as zeros.
   245 
   248 
   246 .. #[Puneeth: You say array here, matrix there -- it's a bit messed up.
   249 .. #[Puneeth: You say array here, matrix there -- it's a bit messed up.
   247 .. Clarify, explicitly.]
   250 .. Clarify, explicitly.]
   248 
   251 
   249 ``zeros()`` function accepts a tuple, which is the order of the array we
   252 ``zeros()`` function accepts a tuple, which is the order of the array that we
   250 want to create, and it generates an array with all elements zero.
   253 want to create, and it generates an array with all elements as zeros.
   251 
   254 
   252 {{{ switch to the next slide, problem statement of solved exercise 1 }}}
   255 {{{ switch to the next slide, problem statement of solved exercise 1 }}}
   253 
   256 
   254 Let us creates an array of the order four by five with all the elements
   257 Let us creates an array of the order four by five with all the elements
   255 zero. We can do it using the method zeros, ::
   258 zero. We can do it using the method zeros, ::