getting-started-with-arrays/script.rst
author Puneeth Chaganti <punchagan@fossee.in>
Wed, 13 Oct 2010 12:52:11 +0530
changeset 318 a45256cc5404
parent 304 d98f554bbec0
child 319 e8c02b3c51ac
permissions -rw-r--r--
Reviewed getting started with arrays, script.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
     1
.. 4.1 LO: getting started with arrays (2) [anoop] 
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
     2
.. ------------------------------------------------
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
     3
.. * why arrays 
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
     4
..   + speed - simply say 
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
     5
..   + array level operations 
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
     6
.. * creating arrays 
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
     7
..   + direct data 
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
     8
..   + list conversion 
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
     9
..   + homogeneous 
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    10
..   + builtins - identitiy, zeros, 
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    11
.. * array operations 
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    12
..   + =+ - * /= 
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    13
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    14
.. Objectives
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    15
.. ----------
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    16
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    17
.. Clearly state the objectives of the LO (along with RBT level)
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    18
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    19
.. Prerequisites
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    20
.. -------------
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    21
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    22
..   1. Name of LO-1
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    23
..   2. Name of LO-2
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    24
..   3. Name of LO-3
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    25
     
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    26
..  Author: Anoop Jacob Thomas <anoop@fossee.in>
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    27
    Internal Reviewer   : Puneeth 
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    28
    External Reviewer   :
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    29
    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    30
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    31
===========================
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    32
Getting started with Arrays
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    33
===========================
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    34
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    35
.. #[Puneeth: Prerequisites and Objectives are missing. Fill them in]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    36
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    37
{{{ show the welcome slide }}}
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    38
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    39
Welcome to the spoken tutorial on getting started with arrays.
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    40
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    41
{{{ switch to next slide, outline slide }}}
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    42
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    43
In this tutorial, we will learn about arrays, how to convert a list into an
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    44
array and also why an array is preferred over lists. And array operations.
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    45
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    46
.. #[Puneeth: Fix the grammar above.]
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    47
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    48
{{{ switch to next slide on overview of array }}}
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    49
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    50
Arrays are homogeneous data structures, unlike lists, arrays cannot have
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    51
heterogeneous data elements, that is, it can have only one type of data
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    52
type, either all integers, or strings, or float, and not a mix.
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    53
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    54
.. #[Puneeth: Use multiple short sentences, rather than one long sentence
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    55
   I would've written something like this. 
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    56
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    57
   Unlike lists, arrays are homogeneous data structures. They can have only
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    58
   type of data, ....]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    59
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    60
Arrays are really fast in mathematical operations when compared to lists,
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    61
it is at least 80 to 100 times faster than lists.
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    62
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    63
.. #[Puneeth: For what size of an array is that the comparison?
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    64
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    65
{{{ switch to the next slide, creating arrays }}}
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    66
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    67
Now let us see how to create arrays.
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    68
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    69
I am assuming that you have your IPython interpreter running with the
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    70
``-pylab`` option, so that you have the required modules loaded.
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    71
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    72
.. #[Puneeth: 'I am assuming' doesn't sound right. Ask them to open if it
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    73
.. is not open?]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    74
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    75
To create an array we will use the function ``array()`` as,
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    76
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    77
::
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    78
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    79
    a1 = array([1,2,3,4])
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    80
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    81
Notice that here we created a one dimensional array. Also notice the object
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    82
we passed to create an array. Now let us see how to create a two
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    83
dimensional array. Pause here and try to do it yourself before looking at
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    84
the solution.
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    85
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    86
.. #[Puneeth: I don't think this question can be solved by an average
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    87
.. viewer. Questions during the tutorial, should generally be to re-iterate
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    88
.. concepts learnt? ]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    89
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    90
.. #[Puneeth: Also, you didn't even point out that we are converting a
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    91
.. list, using the ``array`` function. Bring the later section about
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    92
.. converting a list, here. A separate section is not necessary, IMHO.]
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    93
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    94
This is how we create two dimensional arrays.
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    95
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    96
::
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    97
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    98
    a2 = array([[1,2,3,4],[5,6,7,8]])
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    99
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   100
.. #[Puneeth: Again, you could explain a bit about the fact that we are
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   101
.. converting a list of lists.]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   102
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   103
Let us see an easy method of creating an array with elements 1 to 8.
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   104
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   105
::
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   106
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   107
    ar = arange(1,9)
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   108
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   109
.. #[Puneeth: say, creating the same array as before. for some time I got
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   110
.. confused .]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   111
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   112
And it created a single dimensional array of elements from 1 to 8.
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   113
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   114
::
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   115
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   116
    print ar
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   117
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   118
.. #[Puneeth: be consistent with voice. say, we obtained... or something.]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   119
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   120
And how can we make it a two dimensional array of order 2 by 4. Pause here
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   121
and try to do it yourself, try ``ar.tab`` and find a suitable method for
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   122
that.
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   123
304
d98f554bbec0 added slides for getting-started-with-arrays.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   124
{{{ switch to next slide, reshape() method }}}
d98f554bbec0 added slides for getting-started-with-arrays.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   125
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   126
We can use the function ``reshape()`` for that purpose and it can be done
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   127
as,
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   128
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   129
::
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   130
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   131
    ar.reshape(2,4)
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   132
    ar.reshape(4,2)
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   133
    ar = ar.reshape(2,4)
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   134
304
d98f554bbec0 added slides for getting-started-with-arrays.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   135
{{{ switch to next slide, creating array from list}}}
d98f554bbec0 added slides for getting-started-with-arrays.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   136
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   137
Now, let us see how to convert a list object to an array. As you have
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   138
already seen, in both of the previous statements we have passed a list, so
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   139
creating an array can be done so, first let us create a list ``l1``
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   140
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   141
::
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   142
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   143
    l1 = [1,2,3,4]
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   144
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   145
Now we can convert the list to an array as, 
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   146
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   147
::
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   148
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   149
    a3 = array(l1)
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   150
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   151
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   152
{{{ switch to the next slide, problem statement of unsolved exercise 1 }}}
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   153
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   154
Create a three dimensional array of the order (2,2,4).
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   155
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   156
.. #[Puneeth: s/order/shape or size ?]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   157
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   158
{{{ switch to the next slide, shape of an array }}}
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   159
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   160
To find the shape of an array we can use the object ``.shape``, let us
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   161
check the shape of the arrays we have created so far,
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   162
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   163
.. #[Puneeth: s/object/method ?]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   164
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   165
::
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   166
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   167
    a1.shape
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   168
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   169
``a1.shape`` object is a tuple, and since a1 is a single dimensional array,
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   170
it returned a tuple (4,).
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   171
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   172
.. #[Puneeth: first show a 2D array, so that it becomes easier to explain.
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   173
.. Also, the word ``tuple`` need not be mentioned. ]
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   174
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   175
{{{ switch to the next slide, unsolved exercise 2 }}}
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   176
304
d98f554bbec0 added slides for getting-started-with-arrays.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   177
Find out the shape of the other arrays that we have created.
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   178
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   179
.. #[Puneeth: solution missing.]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   180
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   181
{{{ Array can have only a single type of data }}}
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   182
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   183
.. #[Puneeth: I guess, this whole section can be skipped. If you want to
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   184
.. keep this, just briefly mention that arrays are homogeneous in the
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   185
.. intro, don't explain it there.]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   186
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   187
Now let us try to create a new array with a mix of elements and see what
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   188
will happen,
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   189
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   190
::
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   191
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   192
    a4 = array([1,2,3,'a string'])
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   193
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   194
Well, we expected an error as previously I said that an array can have only
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   195
homogeneous elements, but it didn't give an error. Let us check the values
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   196
in the new array created. In your IPython terminal type, 
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   197
::
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   198
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   199
    a4
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   200
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   201
Did you notice it,
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   202
304
d98f554bbec0 added slides for getting-started-with-arrays.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   203
{{{ switch to next slide, implicit type casting }}}
d98f554bbec0 added slides for getting-started-with-arrays.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   204
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   205
.. #[Puneeth: typecasting may be unnecessary. (Also too advanced?) an
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   206
.. average guy wouldn't use arrays with strings.]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   207
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   208
.. #[Puneeth: You may want to mention that float is the default dtype.]
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   209
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   210
{{{ highlight all the array elements one by one using mouse movements }}}
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   211
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   212
all the elements have been implicitly type casted as string, though our
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   213
first three elements were integers.
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   214
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   215
.. #[Puneeth: when I type a4 it says some ``dtype`` etc. I don't understand
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   216
.. what it is, can you explain? ;)]
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   217
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   218
{{{ switch to the next slide, identity & zeros methods }}}
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   219
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   220
.. #[Puneeth: something needs to motivate this. why are we suddenly talking
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   221
.. of an identity matrix?]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   222
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   223
An identity matrix is a square matrix in which all the diagonal elements
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   224
are one and rest of the elements zero. We can create an identity matrix
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   225
using the method ``identity()``.
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   226
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   227
The function ``identity()`` takes an integer argument,
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   228
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   229
::
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   230
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   231
    identity(3)
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   232
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   233
As you can see the identity method returned a three by three square array
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   234
with all the diagonal elements as one and the rest of the elements as zero.
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   235
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   236
.. #[Puneeth: You say array here, matrix there -- it's a bit messed up.
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   237
.. Clarify, explicitly.]
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   238
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   239
``zeros()`` function accepts a tuple, which is the order of the array we
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   240
want to create, and it generates an array with all elements zero.
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   241
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   242
{{{ switch to the next slide, problem statement of solved exercise 1 }}}
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   243
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   244
Let us creates an array of the order four by five with all the elements
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   245
zero. We can do it using the method zeros, ::
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   246
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   247
    zeros((4,5))
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   248
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   249
Notice that we passed a tuple to the function zeros.
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   250
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   251
{{{ switch to next slide, learning exercise }}}
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   252
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   253
We learned two functions ``identity()`` and ``zeros()``, find out more
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   254
about the functions ``zeros_like()``, ``ones()``, ``ones_like()``.
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   255
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   256
{{{ switch to next slide, array operations }}}
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   257
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   258
Try the following, first check the value of a1,
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   259
::
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   260
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   261
    a1
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   262
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   263
``a1`` is a single dimensional array, and now try,
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   264
::
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   265
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   266
    a1 * 2
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   267
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   268
It returned a new array with all the elements multiplied by 2.
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   269
::
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   270
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   271
    a1
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   272
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   273
note that the value of a1 still remains the same.
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   274
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   275
Similarly with addition,
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   276
::
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   277
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   278
    a1 + 2
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   279
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   280
it returns a new array, with all the elements summed with two. But
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   281
again notice that the value of a1 has not been changed.
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   282
::
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   283
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   284
    a1
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   285
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   286
You may change the value of a1 by simply assigning the newly returned
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   287
array as,
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   288
::
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   289
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   290
    a1 += 2
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   291
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   292
Notice the change in elements of a,
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   293
::
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   294
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   295
    a
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   296
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   297
We can use all the mathematical operations with arrays, Now let us try this
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   298
::
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   299
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   300
   a1 = array([1,2,3,4])
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   301
   a2 = array([1,2,3,4])
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   302
   a1 + a2
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   303
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   304
Returns an array with element by element addition,
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   305
::
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   306
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   307
    a1 * a2
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   308
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   309
Returns an array with element by element multiplication, notice that it
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   310
does not perform matrix multiplication.
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   311
304
d98f554bbec0 added slides for getting-started-with-arrays.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   312
{{{ switch to next slide, summary slide }}}
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   313
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   314
So this brings us to the end of this tutorial, in this tutorial we covered
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   315
basics of arrays, how to create an array, converting a list to an array,
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   316
basic array operations etc.
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   317
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   318
.. #[Puneeth: s/how to create an array/creating an array]
261
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   319
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   320
{{{ switch to next slide, thank you }}}
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   321
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   322
Thank you!
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   323
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   324
.. 
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   325
   Local Variables:
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   326
   mode: rst
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   327
   indent-tabs-mode: nil
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   328
   sentence-end-double-space: nil
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   329
   fill-column: 75
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   330
   End: