168
6 Computing Integrals and Testing Code
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 the file MC_double.py.)
Remark About Version Control of Files
Having a suite of test functions for automatically checking that your software
works is considered as a fundamental requirement for reliable computing.
Equally important is a system that can keep track of different versions of 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
scientific or engineering work. We have a quick intro b to Git and GitHub that
gets you up and running within minutes.
The typical workflow with Git goes as follows.
1. Before you start working with files, make sure you have the latest
version of them by running git pull.
2. Edit files, remove or create files (new files must be registered by git
add).
3. When a natural piece of work is done, commit your changes by the git
commit command.
4. Implement your changes also in the cloud by doing git push.
A nice feature of Git is that people can edit the same file at the same time and
very often Git will be able to automatically merge 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 the entire file collection to a previous commit. This feature is, of course,
fundamental for reliable work.
a https://en.wikipedia.org/wiki/Git_(software).
b http://hplgit.github.io/teamods/bitgit/Langtangen_bitgit-bootstrap.html.
6 Computing Integrals and Testing Code
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 the file MC_double.py.)
Remark About Version Control of Files
Having a suite of test functions for automatically checking that your software
works is considered as a fundamental requirement for reliable computing.
Equally important is a system that can keep track of different versions of 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
scientific or engineering work. We have a quick intro b to Git and GitHub that
gets you up and running within minutes.
The typical workflow with Git goes as follows.
1. Before you start working with files, make sure you have the latest
version of them by running git pull.
2. Edit files, remove or create files (new files must be registered by git
add).
3. When a natural piece of work is done, commit your changes by the git
commit command.
4. Implement your changes also in the cloud by doing git push.
A nice feature of Git is that people can edit the same file at the same time and
very often Git will be able to automatically merge 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 the entire file collection to a previous commit. This feature is, of course,
fundamental for reliable work.
a https://en.wikipedia.org/wiki/Git_(software).
b http://hplgit.github.io/teamods/bitgit/Langtangen_bitgit-bootstrap.html.
