... | ... |
@@ -280,6 +280,100 @@ |
280 | 280 |
"adaptive.live_plot(runner)" |
281 | 281 |
] |
282 | 282 |
}, |
283 |
+ { |
|
284 |
+ "cell_type": "markdown", |
|
285 |
+ "metadata": {}, |
|
286 |
+ "source": [ |
|
287 |
+ "# 1D integration learner with `cquad`" |
|
288 |
+ ] |
|
289 |
+ }, |
|
290 |
+ { |
|
291 |
+ "cell_type": "markdown", |
|
292 |
+ "metadata": {}, |
|
293 |
+ "source": [ |
|
294 |
+ "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](https://www.academia.edu/1976055/Adaptive_quadrature_re-revisited).\n", |
|
295 |
+ "\n", |
|
296 |
+ "Let's try the following function with cusps (that is difficult to integrate):" |
|
297 |
+ ] |
|
298 |
+ }, |
|
299 |
+ { |
|
300 |
+ "cell_type": "code", |
|
301 |
+ "execution_count": null, |
|
302 |
+ "metadata": {}, |
|
303 |
+ "outputs": [], |
|
304 |
+ "source": [ |
|
305 |
+ "import numpy as np\n", |
|
306 |
+ "import holoviews as hv\n", |
|
307 |
+ "\n", |
|
308 |
+ "def f24(x):\n", |
|
309 |
+ " return np.floor(np.exp(x))\n", |
|
310 |
+ "\n", |
|
311 |
+ "xs = np.linspace(0, 3, 200)\n", |
|
312 |
+ "hv.Scatter((xs, [f24(x) for x in xs]))" |
|
313 |
+ ] |
|
314 |
+ }, |
|
315 |
+ { |
|
316 |
+ "cell_type": "markdown", |
|
317 |
+ "metadata": {}, |
|
318 |
+ "source": [ |
|
319 |
+ "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." |
|
320 |
+ ] |
|
321 |
+ }, |
|
322 |
+ { |
|
323 |
+ "cell_type": "code", |
|
324 |
+ "execution_count": null, |
|
325 |
+ "metadata": {}, |
|
326 |
+ "outputs": [], |
|
327 |
+ "source": [ |
|
328 |
+ "import scipy.integrate\n", |
|
329 |
+ "scipy.integrate.quad(f24, 0, 3)" |
|
330 |
+ ] |
|
331 |
+ }, |
|
332 |
+ { |
|
333 |
+ "cell_type": "markdown", |
|
334 |
+ "metadata": {}, |
|
335 |
+ "source": [ |
|
336 |
+ "We initialize a learner again and pass the bounds and relative tolerance we want to reach. Then in the `Runner` we pass `goal=lambda l: l.done()` where `learner.done()` is `True` when the relative tolerance has been reached." |
|
337 |
+ ] |
|
338 |
+ }, |
|
339 |
+ { |
|
340 |
+ "cell_type": "code", |
|
341 |
+ "execution_count": null, |
|
342 |
+ "metadata": {}, |
|
343 |
+ "outputs": [], |
|
344 |
+ "source": [ |
|
345 |
+ "from cquad import Learner\n", |
|
346 |
+ "learner = Learner(f24, bounds=(0, 3), tol=1e-3)\n", |
|
347 |
+ "runner = adaptive.Runner(learner, goal=lambda l: l.done())" |
|
348 |
+ ] |
|
349 |
+ }, |
|
350 |
+ { |
|
351 |
+ "cell_type": "markdown", |
|
352 |
+ "metadata": {}, |
|
353 |
+ "source": [ |
|
354 |
+ "Now we could do the live plotting again, but lets just wait untill the runner is done." |
|
355 |
+ ] |
|
356 |
+ }, |
|
357 |
+ { |
|
358 |
+ "cell_type": "code", |
|
359 |
+ "execution_count": null, |
|
360 |
+ "metadata": {}, |
|
361 |
+ "outputs": [], |
|
362 |
+ "source": [ |
|
363 |
+ "if not runner.task.done():\n", |
|
364 |
+ " raise RuntimeError('Wait for the runner to finish before executing the cells below!')" |
|
365 |
+ ] |
|
366 |
+ }, |
|
367 |
+ { |
|
368 |
+ "cell_type": "code", |
|
369 |
+ "execution_count": null, |
|
370 |
+ "metadata": {}, |
|
371 |
+ "outputs": [], |
|
372 |
+ "source": [ |
|
373 |
+ "print('The integral value is {} with the corresponding error of {}'.format(learner.igral, learner.err))\n", |
|
374 |
+ "learner.plot()" |
|
375 |
+ ] |
|
376 |
+ }, |
|
283 | 377 |
{ |
284 | 378 |
"cell_type": "markdown", |
285 | 379 |
"metadata": {}, |