Web-Books
in the Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Page - 77 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

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

Image of the Page - 77 -

Image of the Page - 77 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Text of the Page - 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)
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
Web-Books
Library
Privacy
Imprint
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python