Page - 168 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 168 -
Text of the Page - 168 -
168 6 ComputingIntegralsandTestingCode
r = sympy.symbols(βrβ)
I_exact = sympy.integrate(2*sympy.pi*r*r, (r, 0, 2))
print(βExact integral: {:g}β.format(I_exact.evalf()))
x0 = -2; x1 = 2; y0 = -2; y1 = 2
n = 1000
np.random.seed(6)
I_expected = 16.7970837117376384 # Computed with this seed
I_computed = MonteCarlo_double(
lambda x, y: np.sqrt(x**2 + y**2),
g, x0, x1, y0, y1, n)
print(βMC approximation, {:d} samples: {:.16f}β\
.format(n**2, I_computed))
assert abs(I_expected - I_computed) < 1E-15
(See thefileMC_double.py.)
RemarkAboutVersionControlofFiles
Havingasuiteof test functionsforautomaticallychecking thatyoursoftware
works is considered as a fundamental requirement for reliable computing.
Equally important is a system that can keep track of different versionsof the
files and the tests, known as a version control system. Todayβs most popular
version control system is Git,a which the authors strongly recommend the
reader to use for programming and writing reports. The combination of Git
and cloud storage such as GitHub is a very common way of organizing
scientificorengineeringwork.We havea quick introb to Git andGitHub that
getsyouupandrunningwithinminutes.
The typicalworkflowwithGit goesas follows.
1. Before you start working with files, make sure you have the latest
versionof thembyrunninggit pull.
2. Edit files, remove or create files (new files must be registered bygit
add).
3. Whenanaturalpieceofwork isdone,commit yourchangesby thegit
commitcommand.
4. Implementyourchangesalso in thecloudbydoinggit push.
Anice featureofGit is thatpeoplecanedit thesamefileat thesametimeand
veryoftenGit will beable toautomaticallymerge the changes(!).Therefore,
version control is crucial when you work with others or when you do your
work on different types of computers. Another key feature is that anyone
can at any time view the history of a file, see who did what when, and roll
back theentirefile collection toapreviouscommit.This feature is, of course,
fundamentalfor reliablework.
a https://en.wikipedia.org/wiki/Git_(software).
b http://hplgit.github.io/teamods/bitgit/Langtangen_bitgit-bootstrap.html.
Programming for Computations β Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
- Title
- Programming for Computations β Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Volume
- Second Edition
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2020
- Language
- English
- License
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 356
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik