... | ... |
@@ -10,8 +10,6 @@ Tutorial `~adaptive.IntegratorLearner` |
10 | 10 |
The complete source code of this tutorial can be found in |
11 | 11 |
:jupyter-download:notebook:`tutorial.IntegratorLearner` |
12 | 12 |
|
13 |
-.. thebe-button:: Run the code live inside the documentation! |
|
14 |
- |
|
15 | 13 |
.. jupyter-execute:: |
16 | 14 |
:hide-code: |
17 | 15 |
|
... | ... |
@@ -10,6 +10,8 @@ Tutorial `~adaptive.IntegratorLearner` |
10 | 10 |
The complete source code of this tutorial can be found in |
11 | 11 |
:jupyter-download:notebook:`tutorial.IntegratorLearner` |
12 | 12 |
|
13 |
+.. thebe-button:: Run the code live inside the documentation! |
|
14 |
+ |
|
13 | 15 |
.. jupyter-execute:: |
14 | 16 |
:hide-code: |
15 | 17 |
|
... | ... |
@@ -8,11 +8,10 @@ Tutorial `~adaptive.IntegratorLearner` |
8 | 8 |
|
9 | 9 |
.. seealso:: |
10 | 10 |
The complete source code of this tutorial can be found in |
11 |
- :jupyter-download:notebook:`IntegratorLearner` |
|
11 |
+ :jupyter-download:notebook:`tutorial.IntegratorLearner` |
|
12 | 12 |
|
13 |
-.. execute:: |
|
13 |
+.. jupyter-execute:: |
|
14 | 14 |
:hide-code: |
15 |
- :new-notebook: IntegratorLearner |
|
16 | 15 |
|
17 | 16 |
import adaptive |
18 | 17 |
adaptive.notebook_extension() |
... | ... |
@@ -27,7 +26,7 @@ of the integral with it. It is based on Pedro Gonnet’s |
27 | 26 |
Let’s try the following function with cusps (that is difficult to |
28 | 27 |
integrate): |
29 | 28 |
|
30 |
-.. execute:: |
|
29 |
+.. jupyter-execute:: |
|
31 | 30 |
|
32 | 31 |
def f24(x): |
33 | 32 |
return np.floor(np.exp(x)) |
... | ... |
@@ -40,7 +39,7 @@ let’s try a familiar function integrator `scipy.integrate.quad`, which |
40 | 39 |
will give us warnings that it encounters difficulties (if we run it |
41 | 40 |
in a notebook.) |
42 | 41 |
|
43 |
-.. execute:: |
|
42 |
+.. jupyter-execute:: |
|
44 | 43 |
|
45 | 44 |
import scipy.integrate |
46 | 45 |
scipy.integrate.quad(f24, 0, 3) |
... | ... |
@@ -50,7 +49,7 @@ we want to reach. Then in the `~adaptive.Runner` we pass |
50 | 49 |
``goal=lambda l: l.done()`` where ``learner.done()`` is ``True`` when |
51 | 50 |
the relative tolerance has been reached. |
52 | 51 |
|
53 |
-.. execute:: |
|
52 |
+.. jupyter-execute:: |
|
54 | 53 |
|
55 | 54 |
from adaptive.runner import SequentialExecutor |
56 | 55 |
|
... | ... |
@@ -61,24 +60,24 @@ the relative tolerance has been reached. |
61 | 60 |
# the overhead of evaluating the function in another process. |
62 | 61 |
runner = adaptive.Runner(learner, executor=SequentialExecutor(), goal=lambda l: l.done()) |
63 | 62 |
|
64 |
-.. execute:: |
|
63 |
+.. jupyter-execute:: |
|
65 | 64 |
:hide-code: |
66 | 65 |
|
67 | 66 |
await runner.task # This is not needed in a notebook environment! |
68 | 67 |
|
69 |
-.. execute:: |
|
68 |
+.. jupyter-execute:: |
|
70 | 69 |
|
71 | 70 |
runner.live_info() |
72 | 71 |
|
73 | 72 |
Now we could do the live plotting again, but lets just wait untill the |
74 | 73 |
runner is done. |
75 | 74 |
|
76 |
-.. execute:: |
|
75 |
+.. jupyter-execute:: |
|
77 | 76 |
|
78 | 77 |
if not runner.task.done(): |
79 | 78 |
raise RuntimeError('Wait for the runner to finish before executing the cells below!') |
80 | 79 |
|
81 |
-.. execute:: |
|
80 |
+.. jupyter-execute:: |
|
82 | 81 |
|
83 | 82 |
print('The integral value is {} with the corresponding error of {}'.format(learner.igral, learner.err)) |
84 | 83 |
learner.plot() |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,84 @@ |
1 |
+Tutorial `~adaptive.IntegratorLearner` |
|
2 |
+-------------------------------------- |
|
3 |
+ |
|
4 |
+.. note:: |
|
5 |
+ Because this documentation consists of static html, the ``live_plot`` |
|
6 |
+ and ``live_info`` widget is not live. Download the notebook |
|
7 |
+ in order to see the real behaviour. |
|
8 |
+ |
|
9 |
+.. seealso:: |
|
10 |
+ The complete source code of this tutorial can be found in |
|
11 |
+ :jupyter-download:notebook:`IntegratorLearner` |
|
12 |
+ |
|
13 |
+.. execute:: |
|
14 |
+ :hide-code: |
|
15 |
+ :new-notebook: IntegratorLearner |
|
16 |
+ |
|
17 |
+ import adaptive |
|
18 |
+ adaptive.notebook_extension() |
|
19 |
+ |
|
20 |
+ import holoviews as hv |
|
21 |
+ import numpy as np |
|
22 |
+ |
|
23 |
+This learner learns a 1D function and calculates the integral and error |
|
24 |
+of the integral with it. It is based on Pedro Gonnet’s |
|
25 |
+`implementation <https://www.academia.edu/1976055/Adaptive_quadrature_re-revisited>`__. |
|
26 |
+ |
|
27 |
+Let’s try the following function with cusps (that is difficult to |
|
28 |
+integrate): |
|
29 |
+ |
|
30 |
+.. execute:: |
|
31 |
+ |
|
32 |
+ def f24(x): |
|
33 |
+ return np.floor(np.exp(x)) |
|
34 |
+ |
|
35 |
+ xs = np.linspace(0, 3, 200) |
|
36 |
+ hv.Scatter((xs, [f24(x) for x in xs])) |
|
37 |
+ |
|
38 |
+Just to prove that this really is a difficult to integrate function, |
|
39 |
+let’s try a familiar function integrator `scipy.integrate.quad`, which |
|
40 |
+will give us warnings that it encounters difficulties (if we run it |
|
41 |
+in a notebook.) |
|
42 |
+ |
|
43 |
+.. execute:: |
|
44 |
+ |
|
45 |
+ import scipy.integrate |
|
46 |
+ scipy.integrate.quad(f24, 0, 3) |
|
47 |
+ |
|
48 |
+We initialize a learner again and pass the bounds and relative tolerance |
|
49 |
+we want to reach. Then in the `~adaptive.Runner` we pass |
|
50 |
+``goal=lambda l: l.done()`` where ``learner.done()`` is ``True`` when |
|
51 |
+the relative tolerance has been reached. |
|
52 |
+ |
|
53 |
+.. execute:: |
|
54 |
+ |
|
55 |
+ from adaptive.runner import SequentialExecutor |
|
56 |
+ |
|
57 |
+ learner = adaptive.IntegratorLearner(f24, bounds=(0, 3), tol=1e-8) |
|
58 |
+ |
|
59 |
+ # We use a SequentialExecutor, which runs the function to be learned in |
|
60 |
+ # *this* process only. This means we don't pay |
|
61 |
+ # the overhead of evaluating the function in another process. |
|
62 |
+ runner = adaptive.Runner(learner, executor=SequentialExecutor(), goal=lambda l: l.done()) |
|
63 |
+ |
|
64 |
+.. execute:: |
|
65 |
+ :hide-code: |
|
66 |
+ |
|
67 |
+ await runner.task # This is not needed in a notebook environment! |
|
68 |
+ |
|
69 |
+.. execute:: |
|
70 |
+ |
|
71 |
+ runner.live_info() |
|
72 |
+ |
|
73 |
+Now we could do the live plotting again, but lets just wait untill the |
|
74 |
+runner is done. |
|
75 |
+ |
|
76 |
+.. execute:: |
|
77 |
+ |
|
78 |
+ if not runner.task.done(): |
|
79 |
+ raise RuntimeError('Wait for the runner to finish before executing the cells below!') |
|
80 |
+ |
|
81 |
+.. execute:: |
|
82 |
+ |
|
83 |
+ print('The integral value is {} with the corresponding error of {}'.format(learner.igral, learner.err)) |
|
84 |
+ learner.plot() |