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

Seite - 48 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition

Bild der Seite - 48 -

Bild der Seite - 48 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition

Text der Seite - 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.
zurück zum  Buch Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Titel
Programming for Computations – Python
Untertitel
A Gentle Introduction to Numerical Simulations with Python 3.6
Band
Second Edition
Autoren
Svein Linge
Hans Petter Langtangen
Verlag
Springer Open
Datum
2020
Sprache
englisch
Lizenz
CC BY 4.0
ISBN
978-3-319-32428-9
Abmessungen
17.8 x 25.4 cm
Seiten
356
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