getting-started-with-arrays/script.rst
author bhanu
Thu, 11 Nov 2010 02:02:06 +0530
changeset 475 2e0b89c0eeb4
parent 393 f99254fc7d70
child 476 cb587df417bd
permissions -rw-r--r--
Laguage check Done for `getting started with arrays`
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   :
475
2e0b89c0eeb4 Laguage check Done for `getting started with arrays`
bhanu
parents: 393
diff changeset
    23
    Language Reviewer   : Bhanukiran
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    24
    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
    25
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
    26
===========================
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
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
    28
===========================
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    29
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    30
.. #[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
    31
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
    32
{{{ 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
    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
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
    35
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
{{{ 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
    37
475
2e0b89c0eeb4 Laguage check Done for `getting started with arrays`
bhanu
parents: 393
diff changeset
    38
In this tutorial, we will learn about the data structure called an array, how to convert
2e0b89c0eeb4 Laguage check Done for `getting started with arrays`
bhanu
parents: 393
diff changeset
    39
a list into an array, operations on arrays and also why an array is preferred
2e0b89c0eeb4 Laguage check Done for `getting started with arrays`
bhanu
parents: 393
diff changeset
    40
to lists.
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    41
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    42
.. #[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
    43
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
{{{ 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
    45
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
    46
Arrays are homogeneous data structures. Unlike lists, arrays cannot have
475
2e0b89c0eeb4 Laguage check Done for `getting started with arrays`
bhanu
parents: 393
diff changeset
    47
heterogeneous data elements, that is, they can have only one type of data 
2e0b89c0eeb4 Laguage check Done for `getting started with arrays`
bhanu
parents: 393
diff changeset
    48
as their entries, be them all integers, strings, or maybe floats, but not a mix.
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    49
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    50
.. #[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
    51
   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
    52
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    53
   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
    54
   type of data, ....]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    55
475
2e0b89c0eeb4 Laguage check Done for `getting started with arrays`
bhanu
parents: 393
diff changeset
    56
Arrays of a given length are comparatively much faster in mathematical
2e0b89c0eeb4 Laguage check Done for `getting started with arrays`
bhanu
parents: 393
diff changeset
    57
operations than lists of the same length, because of the fact that they are
2e0b89c0eeb4 Laguage check Done for `getting started with arrays`
bhanu
parents: 393
diff changeset
    58
homogeneous data structures.
318
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
.. #[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
    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
{{{ 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
    63
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
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
    65
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
    66
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
    67
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
    68
{{{ 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
    69
::
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
    70
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
    71
        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
    72
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    73
.. #[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
    74
.. is not open?]
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
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
    77
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
    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
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
    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
    81
475
2e0b89c0eeb4 Laguage check Done for `getting started with arrays`
bhanu
parents: 393
diff changeset
    82
Notice that we created a one dimensional array here. 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
    83
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
    84
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
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
    86
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
    87
f99254fc7d70 Made changes to script, getting started with arrays, as suggested in internal review.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 319
diff changeset
    88
{{{ 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
    89
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    90
.. #[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
    91
.. 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
    92
.. concepts learnt? ]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    93
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
    94
.. #[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
    95
.. 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
    96
.. 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
    97
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
    98
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
    99
as,
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   100
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
   101
::
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
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
    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
   104
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   105
.. #[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
   106
.. converting a list of lists.]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   107
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
   108
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
   109
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
   110
::
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
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
    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
   113
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   114
.. #[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
   115
.. confused .]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   116
475
2e0b89c0eeb4 Laguage check Done for `getting started with arrays`
bhanu
parents: 393
diff changeset
   117
And we obtained a one 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
   118
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
   119
::
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
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   121
    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
   122
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   123
.. #[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
   124
475
2e0b89c0eeb4 Laguage check Done for `getting started with arrays`
bhanu
parents: 393
diff changeset
   125
And how can we make it a two dimensional array of order 2 by 4? Pause here
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   126
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
   127
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
   128
304
d98f554bbec0 added slides for getting-started-with-arrays.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   129
{{{ switch to next slide, reshape() method }}}
d98f554bbec0 added slides for getting-started-with-arrays.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   130
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   131
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
   132
as,
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   133
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
   134
::
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
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.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
    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
   138
    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
   139
304
d98f554bbec0 added slides for getting-started-with-arrays.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   140
{{{ 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
   141
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
   142
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
   143
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
   144
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
   145
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
   146
::
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
    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
   149
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   150
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
   151
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
   152
::
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
    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
   155
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
{{{ 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
   158
475
2e0b89c0eeb4 Laguage check Done for `getting started with arrays`
bhanu
parents: 393
diff changeset
   159
Create a three dimensional array of the shape (2,2,4).
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
   160
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   161
.. #[Puneeth: s/order/shape or size ?]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   162
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
   163
{{{ 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
   164
475
2e0b89c0eeb4 Laguage check Done for `getting started with arrays`
bhanu
parents: 393
diff changeset
   165
To find the shape of an array we can use the method ``.shape``, let us
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
   166
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
   167
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   168
.. #[Puneeth: s/object/method ?]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   169
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
   170
::
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
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
   173
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
   174
``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
   175
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   176
.. #[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
   177
.. 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
   178
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   179
{{{ 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
   180
304
d98f554bbec0 added slides for getting-started-with-arrays.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   181
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
   182
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   183
.. #[Puneeth: solution missing.]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   184
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
   185
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
   186
::
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
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
    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
   189
    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
   190
    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
   191
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
   192
{{{ 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
   193
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   194
.. #[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
   195
.. 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
   196
.. intro, don't explain it there.]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   197
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   198
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
   199
will happen,
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   200
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
   201
::
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
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   203
    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
   204
475
2e0b89c0eeb4 Laguage check Done for `getting started with arrays`
bhanu
parents: 393
diff changeset
   205
Well, we would expect an error as it has been previously mentioned that arrays handle
2e0b89c0eeb4 Laguage check Done for `getting started with arrays`
bhanu
parents: 393
diff changeset
   206
elements with the same datatype, but it didn't raise an error. Let us check the values
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   207
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
   208
::
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
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   210
    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
   211
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   212
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
   213
304
d98f554bbec0 added slides for getting-started-with-arrays.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   214
{{{ 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
   215
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   216
.. #[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
   217
.. average guy wouldn't use arrays with strings.]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   218
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   219
.. #[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
   220
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   221
{{{ 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
   222
475
2e0b89c0eeb4 Laguage check Done for `getting started with arrays`
bhanu
parents: 393
diff changeset
   223
all the elements have been implicitly type casted as strings, though our
2e0b89c0eeb4 Laguage check Done for `getting started with arrays`
bhanu
parents: 393
diff changeset
   224
first three elements were meant to be integers.
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   225
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   226
.. #[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
   227
.. 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
   228
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
{{{ 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
   230
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   231
.. #[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
   232
.. of an identity matrix?]
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   233
475
2e0b89c0eeb4 Laguage check Done for `getting started with arrays`
bhanu
parents: 393
diff changeset
   234
Now let us see how to create an identity matrix of a given size, that is a
2e0b89c0eeb4 Laguage check Done for `getting started with arrays`
bhanu
parents: 393
diff changeset
   235
two-dimensional array  in  which all the diagonal elements are ones and rest of the
2e0b89c0eeb4 Laguage check Done for `getting started with arrays`
bhanu
parents: 393
diff changeset
   236
elements are zeros. We can create an identity matrix using the function
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
   237
``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
   238
475
2e0b89c0eeb4 Laguage check Done for `getting started with arrays`
bhanu
parents: 393
diff changeset
   239
The function ``identity()`` takes an integer argument which specifies the
2e0b89c0eeb4 Laguage check Done for `getting started with arrays`
bhanu
parents: 393
diff changeset
   240
size of the desired matrix,
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   241
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
   242
::
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   243
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   244
    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
   245
475
2e0b89c0eeb4 Laguage check Done for `getting started with arrays`
bhanu
parents: 393
diff changeset
   246
As you can see the identity function returned a three by three square matrix
2e0b89c0eeb4 Laguage check Done for `getting started with arrays`
bhanu
parents: 393
diff changeset
   247
with all the diagonal elements as ones and the rest of the elements as 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
   248
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   249
.. #[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
   250
.. 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
   251
475
2e0b89c0eeb4 Laguage check Done for `getting started with arrays`
bhanu
parents: 393
diff changeset
   252
``zeros()`` function accepts a tuple, which is the order of the array that we
2e0b89c0eeb4 Laguage check Done for `getting started with arrays`
bhanu
parents: 393
diff changeset
   253
want to create, and it generates an array with all elements as 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
   254
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   255
{{{ 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
   256
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   257
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
   258
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
   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
    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
   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
   262
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
   263
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
{{{ 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
   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
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
   267
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
   268
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
{{{ 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
   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
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
   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
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
    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
   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`` 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
   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
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
    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
   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
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
   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
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
   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
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
   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
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
    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
   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
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
   294
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
   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
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
    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
   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
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
   300
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
   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
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
    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
   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
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
   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
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
    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
   309
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   310
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
   311
::
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
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
   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
   314
   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
   315
   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
   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
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
   318
::
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
    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
   321
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   322
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
   323
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
   324
304
d98f554bbec0 added slides for getting-started-with-arrays.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   325
{{{ 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
   326
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   327
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
   328
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
   329
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
   330
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   331
.. #[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
   332
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
{{{ 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
   334
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
   335
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
   336
318
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   337
.. 
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   338
   Local Variables:
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   339
   mode: rst
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   340
   indent-tabs-mode: nil
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   341
   sentence-end-double-space: nil
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   342
   fill-column: 75
a45256cc5404 Reviewed getting started with arrays, script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 304
diff changeset
   343
   End: