Skip to content

Latest commit

 

History

History
96 lines (71 loc) · 2.61 KB

tutorial.IntegratorLearner.md

File metadata and controls

96 lines (71 loc) · 2.61 KB
jupytext kernelspec
text_representation
extension format_name format_version jupytext_version
.md
myst
0.13
1.14.5
display_name name
python3
python3

Tutorial {class}~adaptive.IntegratorLearner

Because this documentation consists of static html, the `live_plot` and `live_info` widget is not live.
Download the notebook in order to see the real behaviour. [^download]
:tags: [hide-cell]

import adaptive

adaptive.notebook_extension()

import holoviews as hv
import numpy as np

This learner learns a 1D function and calculates the integral and error of the integral with it. It is based on Pedro Gonnet’s implementation.

Let’s try the following function with cusps (that is difficult to integrate):

def f24(x):
    return np.floor(np.exp(x))


xs = np.linspace(0, 3, 200)
hv.Scatter((xs, [f24(x) for x in xs]))

Just to prove that this really is a difficult to integrate function, let’s try a familiar function integrator scipy.integrate.quad, which will give us warnings that it encounters difficulties (if we run it in a notebook.)

import scipy.integrate

scipy.integrate.quad(f24, 0, 3)

We initialize a learner again and pass the bounds and relative tolerance we want to reach. Then in the {class}~adaptive.Runner we pass goal=lambda l: l.done() where learner.done() is True when the relative tolerance has been reached.

from adaptive.runner import SequentialExecutor

learner = adaptive.IntegratorLearner(f24, bounds=(0, 3), tol=1e-8)

# We use a SequentialExecutor, which runs the function to be learned in
# *this* process only. This means we don't pay
# the overhead of evaluating the function in another process.
runner = adaptive.Runner(learner, executor=SequentialExecutor())
:tags: [hide-cell]

await runner.task  # This is not needed in a notebook environment!
runner.live_info()

Now we could do the live plotting again, but let's just wait until the runner is done.

if not runner.task.done():
    raise RuntimeError(
        "Wait for the runner to finish before executing the cells below!"
    )
print(
    "The integral value is {} with the corresponding error of {}".format(
        learner.igral, learner.err
    )
)
learner.plot()