getting-started-with-arrays/script.rst
author anand
Thu, 11 Nov 2010 00:03:57 +0530
changeset 472 fcdec2d28c9a
parent 393 f99254fc7d70
child 475 2e0b89c0eeb4
permissions -rw-r--r--
Checklist OK for `The other kinds of plots`
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
     1
.. Objectives
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
     2
.. ----------
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
     3
319
e8c02b3c51ac changed headers of scripts(objectives and prerequisites).
Anoop Jacob Thomas<anoop@fossee.in>
parents: 318
diff changeset
     4
.. At the end of this tutorial, you will be able to 
e8c02b3c51ac changed headers of scripts(objectives and prerequisites).
Anoop Jacob Thomas<anoop@fossee.in>
parents: 318
diff changeset
     5
e8c02b3c51ac changed headers of scripts(objectives and prerequisites).
Anoop Jacob Thomas<anoop@fossee.in>
parents: 318
diff changeset
     6
.. 1. Create arrays using data
e8c02b3c51ac changed headers of scripts(objectives and prerequisites).
Anoop Jacob Thomas<anoop@fossee.in>
parents: 318
diff changeset
     7
.. #. Create arrays from lists
e8c02b3c51ac changed headers of scripts(objectives and prerequisites).
Anoop Jacob Thomas<anoop@fossee.in>
parents: 318
diff changeset
     8
.. #. Basic array operations
e8c02b3c51ac changed headers of scripts(objectives and prerequisites).
Anoop Jacob Thomas<anoop@fossee.in>
parents: 318
diff changeset
     9
.. #. Creating identity matrix using ``identity()`` function.
e8c02b3c51ac changed headers of scripts(objectives and prerequisites).
Anoop Jacob Thomas<anoop@fossee.in>
parents: 318
diff changeset
    10
.. #. Learn about ``zeros()``, ``zeros_like()``, ``ones()``,
e8c02b3c51ac changed headers of scripts(objectives and prerequisites).
Anoop Jacob Thomas<anoop@fossee.in>
parents: 318
diff changeset
    11
      ``ones_like()`` functions.
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    12
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    13
.. Prerequisites
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    14
.. -------------
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    15
319
e8c02b3c51ac changed headers of scripts(objectives and prerequisites).
Anoop Jacob Thomas<anoop@fossee.in>
parents: 318
diff changeset
    16
..   1. should have ``ipython`` and ``pylab`` installed. 
e8c02b3c51ac changed headers of scripts(objectives and prerequisites).
Anoop Jacob Thomas<anoop@fossee.in>
parents: 318
diff changeset
    17
..   #. getting started with ``ipython``.
e8c02b3c51ac changed headers of scripts(objectives and prerequisites).
Anoop Jacob Thomas<anoop@fossee.in>
parents: 318
diff changeset
    18
..   #. getting started with lists.
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    19
     
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    20
..  Author: Anoop Jacob Thomas <anoop@fossee.in>
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    21
    Internal Reviewer   : Puneeth 
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    22
    External Reviewer   :
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    23
    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
    24
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
    25
===========================
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    26
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
    27
===========================
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    28
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    29
.. #[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
    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
{{{ 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
    32
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
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
    34
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    35
{{{ 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
    36
393
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
    37
In this tutorial, we will learn about arrays, we will learn how to convert
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
    38
a list into an array array operations and also why an array is preferred
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
    39
over lists.
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    40
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    41
.. #[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
    42
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    43
{{{ 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
    44
393
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
    45
Arrays are homogeneous data structures. Unlike lists, arrays cannot have
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    46
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
    47
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
    48
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    49
.. #[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
    50
   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
    51
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    52
   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
    53
   type of data, ....]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    54
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    55
Arrays are really fast in mathematical operations when compared to lists,
393
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
    56
because of the same type of data in arrays.
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    57
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    58
.. #[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
    59
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    60
{{{ 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
    61
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    62
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
    63
393
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
    64
Run your IPython interpreter with ``-pylab`` option, to load the required
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
    65
modules to work with arrays.
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
    66
{{{ take terminal and run the following command }}}
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
    67
::
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
    68
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
    69
        ipython -pylab
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
    70
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    71
.. #[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
    72
.. is not open?]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    73
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
    74
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
    75
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
    76
::
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
    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
    79
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    80
Notice that here we created a one dimensional array. Also notice the object
393
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
    81
we passed to create an array. We passed a list to create an array. 
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
    82
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
    83
Now let us see how to create a two dimensional array. Pause here and try to
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
    84
do it yourself before looking at the solution.
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
    85
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
    86
{{{ switch to next slide, creating two dimensional arrays }}}
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    87
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    88
.. #[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
    89
.. 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
    90
.. concepts learnt? ]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    91
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    92
.. #[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
    93
.. 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
    94
.. 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
    95
393
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
    96
We create two dimensional array by converting a list of lists to an array
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
    97
as,
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    98
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
    99
::
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   100
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   101
    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
   102
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   103
.. #[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
   104
.. converting a list of lists.]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   105
393
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
   106
Now let us use ``arange()`` function to create the same array as before.
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   107
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
   108
::
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   109
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   110
    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
   111
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   112
.. #[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
   113
.. confused .]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   114
393
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
   115
And we obtained a single dimensional array with elements from 1 to 8.
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   116
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
   117
::
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   118
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   119
    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
   120
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   121
.. #[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
   122
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   123
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
   124
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
   125
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
   126
304
d98f554bbec0 added slides for getting-started-with-arrays.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   127
{{{ switch to next slide, reshape() method }}}
d98f554bbec0 added slides for getting-started-with-arrays.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   128
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   129
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
   130
as,
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   131
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
   132
::
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
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
    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
   135
    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
   136
    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
   137
304
d98f554bbec0 added slides for getting-started-with-arrays.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   138
{{{ 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
   139
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
   140
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
   141
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
   142
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
   143
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
   144
::
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   145
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   146
    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
   147
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   148
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
   149
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
   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
    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
   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
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
{{{ 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
   156
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   157
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
   158
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   159
.. #[Puneeth: s/order/shape or size ?]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   160
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
   161
{{{ 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
   162
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   163
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
   164
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
   165
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   166
.. #[Puneeth: s/object/method ?]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   167
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
   168
::
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   169
393
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
   170
    a2.shape
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
   171
393
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
   172
``a2.shape`` object is a tuple, and it returned a tuple (2, 4).
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   173
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   174
.. #[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
   175
.. 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
   176
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   177
{{{ 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
   178
304
d98f554bbec0 added slides for getting-started-with-arrays.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   179
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
   180
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   181
.. #[Puneeth: solution missing.]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   182
393
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
   183
It can be done as,
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
   184
::
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
   185
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
   186
    a1.shape
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
   187
    a3.shape
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
   188
    ar.shape
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
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
{{{ 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
   191
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   192
.. #[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
   193
.. 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
   194
.. intro, don't explain it there.]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   195
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   196
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
   197
will happen,
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   198
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
   199
::
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
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   201
    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
   202
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   203
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
   204
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
   205
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
   206
::
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   207
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   208
    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
   209
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   210
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
   211
304
d98f554bbec0 added slides for getting-started-with-arrays.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   212
{{{ 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
   213
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   214
.. #[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
   215
.. average guy wouldn't use arrays with strings.]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   216
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   217
.. #[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
   218
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   219
{{{ 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
   220
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   221
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
   222
first three elements were integers.
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   223
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   224
.. #[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
   225
.. 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
   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
{{{ 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
   228
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   229
.. #[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
   230
.. of an identity matrix?]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   231
393
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
   232
Now let us see how to create identity matrix, an identity matrix is a
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
   233
square matrix in which all the diagonal elements are one and rest of the
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
   234
elements zero. We can create an identity matrix using the method
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
   235
``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
   236
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   237
The function ``identity()`` takes an integer argument,
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   238
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
   239
::
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   240
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
    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
   242
393
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
   243
As you can see the identity method returned a three by three square matrix
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   244
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
   245
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   246
.. #[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
   247
.. 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
   248
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   249
``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
   250
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
   251
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   252
{{{ 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
   253
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   254
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
   255
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
   256
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
    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
   258
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
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
   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
{{{ 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
   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
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
   264
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
   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
{{{ 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
   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
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
   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
``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
   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
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
    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
   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
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
   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
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
    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
   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
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
   284
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
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
   286
::
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
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
    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
   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
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
   291
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
   292
::
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
    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
   295
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
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
   297
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
   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 += 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
   301
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
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
   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
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
    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
   306
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   307
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
   308
::
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   309
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   310
   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
   311
   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
   312
   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
   313
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   314
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
   315
::
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   316
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   317
    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
   318
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   319
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
   320
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
   321
304
d98f554bbec0 added slides for getting-started-with-arrays.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   322
{{{ 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
   323
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   324
So this brings us to the end of this tutorial, in this tutorial we covered
393
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
   325
basics of arrays, learned how to create an array, saw how to convert a list
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
   326
to an array, and basic array operations etc.
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   327
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   328
.. #[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
   329
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   330
{{{ 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
   331
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   332
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
   333
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   334
.. 
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   335
   Local Variables:
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   336
   mode: rst
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   337
   indent-tabs-mode: nil
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   338
   sentence-end-double-space: nil
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   339
   fill-column: 75
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   340
   End: