Web-Books
im Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Seite - 18 -
  • Benutzer
  • Version
    • Vollversion
    • Textversion
  • Sprache
    • Deutsch
    • English - Englisch

Seite - 18 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Bild der Seite - 18 -

Bild der Seite - 18 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Text der Seite - 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
zurück zum  Buch Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python
Titel
Programming for Computations – Python
Untertitel
A Gentle Introduction to Numerical Simulations with Python
Autoren
Svein Linge
Hans Petter Langtangen
Verlag
Springer Open
Datum
2016
Sprache
englisch
Lizenz
CC BY-NC 4.0
ISBN
978-3-319-32428-9
Abmessungen
17.8 x 25.4 cm
Seiten
248
Schlagwörter
Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
Kategorie
Informatik
Web-Books
Bibliothek
Datenschutz
Impressum
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python