Page - 18 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Image of the Page - 18 -
Text of the Page - 18 -
18 1 TheFirstFewSteps
“aof zero” (i.e.,a[0]), “aofone” (i.e.,a[1]), andsoon. Theveryfirst line in the
exampleabove, i.e.
h = zeros(4)
instructs Python to reserve, orallocate, space inmemory for an arrayhwith four
elementsand initial valuesset to0. Thenext four linesoverwrite thezeroswith the
desired numbers (measured heights), onenumber for each element. Elements are,
byrule, indexed (numberswithinbrackets) from0to the lastelement, in thiscase3.
Wesay thatPythonhas zero based indexing. This differs fromone based indexing
(e.g., found inMatlab)where thearray indexstartswith1.
As illustrated in the code, youmay refer to the arrayas awholeby thenameh,
but also to each individual element by use of the index. The array elementsmay
enter in computations as individual variables, e.g., writing z = h[0] + h[1] +
h[2] + h[3]will compute the sum of all the elements in h, while the result is
assigned to thevariablez. Note that thiswayof creatingan array is a bit different
from the onewith linspace, where the filling in of numbers occurred automati-
cally “behind the scene”.
By the use of a colon, you may pick out a slice of an array. For example,
to create a new array from the two elements h[1] and h[2], we could write
slice_h = h[1:3]. Note that the indexspecification1:3means indices1and2,
i.e., the last index is not included. For thegeneratedslice_harray, indices are as
usual, i.e.,0and1 in thiscase. Thevery last entry inanarraymaybeaddressedas,
e.g.,h[-1].
Copyingarrays requires somecare since simplywritingnew_h = hwill,when
youafterwardschangeelementsofnew_h, alsochange thecorrespondingelements
inh! That is,h[1] is alsochangedwhenwriting
new_h = h
new_h[1] = 5.0
print h[1]
In this casewedonotget 1.85outon the screen, but 5.0. To reallyget a copy that
is decoupled fromtheoriginal array,youmaywritenew_h = copy(h). However,
copying a slice works straightforwardly (as shown above), i.e. an explicit use of
copy isnot required.
1.5.7 Plotting
Sometimesyouwould like to have twoormore curvesorgraphs in the sameplot.
Assumewehaveh as above, and also an arrayHwith the heights 0.50m, 0.70m,
1.90m, and 1.75m froma family next door. Thismay be donewith the program
plot_heights.pygivenas
back to the
book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python"
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python
- Title
- Programming for Computations – Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2016
- Language
- English
- License
- CC BY-NC 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 248
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik