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

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

Bild der Seite - 77 -

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

Text der Seite - 77 -

3.6 MeasuringComputationalSpeed 77 Letus test thecode interactively inaPythonshell tocompute R1 0 3t2dt. Thefile with the codeabovehas thenameintegration_methods_vec.pyand is a valid module fromwhichwecan import thevectorized function: >>> from integration_methods_vec import midpoint >>> from numpy import exp >>> v = lambda t: 3*t**2*exp(t**3) >>> midpoint(v, 0, 1, 10) 1.7014827690091872 Note thenecessity to useexp fromnumpy: ourv functionwill be calledwithx as anarray,and theexp functionmustbecapableofworkingwithanarray. Thevectorizedcodeperformsall loopsveryefficiently incompiledcode, result- ing inmuchfaster execution.Moreover,manyreadersof thecodewill alsosay that thealgorithmlooksclearer than in the loop-based implementation. Vectorizing the trapezoidal rule Wecanuse the same approach to vectorize the trapezoid function. However, the trapezoidal ruleperformsa sumwhere theend pointshavedifferentweight. Ifwedosum(f(x)),weget theendpointsf(a)and f(b)withweightunity insteadofonehalf. A remedy is to subtract the error from sum(f(x)): sum(f(x)) - 0.5*f(a) - 0.5*f(b). The vectorized version of the trapezoidalmethod thenbecomes def trapezoidal(f, a, b, n): h = float(b-a)/n x = linspace(a, b, n+1) s = sum(f(x)) - 0.5*f(a) - 0.5*f(b) return h*s 3.6 MeasuringComputationalSpeed Now thatwe have created faster, vectorized versions of functions in the previous section, it is interesting tomeasure howmuch faster they are. The purpose of the present section is therefore to explain howwecan record theCPUtime consumed bya function sowecananswer thisquestion. Therearemany techniques formea- suringtheCPUtimeinPython,andhereweshall justexplain thesimplestandmost convenientone: the%timeitcommand in IPython. The following interactive ses- sion should illustrate a competitionwhere the vectorizedversions of the functions are supposed towin: In [1]: from integration_methods_vec import midpoint as midpoint_vec In [3]: from midpoint import midpoint In [4]: from numpy import exp In [5]: v = lambda t: 3*t**2*exp(t**3)
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