Page - 48 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 48 -
Text of the Page - 48 -
48 2 AFewMoreSteps
In [4]: type(x) # check type of array as a whole
Out[4]: numpy.ndarray
In [5]: type(x[0]) # check type of array element
Out[5]: numpy.float64
The line x = linspace(0, 2, 3) makes Python reserve, or allocate, space in
memory for the array produced by linspace. With the assignment, x becomes
a reference to the array object from linspace, i.e. x becomes the “name of the
array”.Thearraywillhavethreeelementswithnamesx[0],x[1]andx[2],where
thebracketednumbersare referred toas indices (note thatwhenreading,wesay“x
ofzero” tox[0], “xofone” tox[1], andsoon).
Observethat the indexingstartswith0,so thatanarraywithnelementswillhave
n-1 as the last index. We say that Python has zero based indexing, which differs
fromone based indexingwherearray indexingstarts with 1 (as, e.g., in Matlab). In
x, the value ofx[0] is 0.0, the value ofx[1] is 1.0 and, finally, the value ofx[2]
is2.0.Thesevaluesaregivenby theprintoutarray([ 0., 1., 2.])above.
With thecommandtype(x),we confirmthat thearrayobjectnamedxhas type
numpy.ndarray.Note that, at thesame time, the individualarrayelements refer to
objects with another type. We see this from the very last command,type(x[0]),
which makes Python respond withnumpy.float64 (being just a certain float data
type in NumPy9).
If we continue the previous dialogue with a few lines, we can also demonstrate
thatuseof individualarrayelements is straight forward:
In [4]: sum_elements = x[0] + x[1] + x[2]
In [5]: sum_elements
Out[5]: 3.0
In [6]: product_2_elements = x[1]*x[2]
In [7]: product_2_elements
Out[7]: 2.0
In [8]: x[0] = 5.0 # overwrite previous value
In [9]: x
Out[9]: array([ 5., 1., 2.])
The Zeros Function There are other common ways to generate arrays too. One
way is to use anothernumpy function namedzeros, which (as the name suggests)
may be used to produce an array with zeros. These zeros can be either floating
point numbers or integers, depending on the arguments provided when zeros is
called.10 Often, the zerosare overwritten in a secondstep to arriveat an array with
thenumbersactuallywanted.
9 You may check out the many numerical data types in NumPy at https://docs.scipy.org/
doc/numpy-1.13.0/user/basics.types.html.
10 https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.zeros.html.
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
- Title
- Programming for Computations – Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Volume
- Second Edition
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2020
- Language
- English
- License
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 356
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik