9.1. Caching

class cherab.core.math.caching.caching1d.Caching1D

Bases: raysect.core.math.function.float.function1d.base.Function1D

Precalculate and cache a 1D function on a finite space area.

The function is sampled and a cubic interpolation is then used to calculate a cubic spline approximation of the function. As the spline has a constant cost of evaluation, this decreases the evaluation time of functions which are very often used.

The sampling and interpolation are done locally and on demand, so that the caching is done progressively when the function is evaluated. Coordinates are normalised to the range [0, 1] to avoid float accuracy troubles. The values of the function are normalised if their boundaries are given.

Parameters
  • function1d (object) – 1D function to be cached.

  • space_area (tuple) – space area where the function has to be cached: (minx, maxx).

  • resolution (double) – resolution of the sampling

  • no_boundary_error – Behaviour when evaluated outside the caching area. When False a ValueError is raised. When True the function is directly evaluated (without caching). Default is False.

  • function_boundaries – Boundaries of the function values for normalisation: (min, max). If None, function values are not normalised. Default is None.

>>> from numpy import sqrt
>>> from time import sleep
>>> from cherab.core.math import Caching1D
>>>
>>> def expensive_sqrt(x):
>>>     sleep(5)
>>>     return sqrt(x)
>>>
>>> f1 = Caching1D(expensive_sqrt, (-5, 5), 0.1)
>>>
>>> # if you try this, first two executions will be slow, third will be fast
>>> f1(2.5)
1.5811388
>>> f1(2.6)
1.6124515
>>> f1(2.55)
1.5968720
class cherab.core.math.caching.caching2d.Caching2D

Bases: raysect.core.math.function.float.function2d.base.Function2D

Precalculate and cache a 2D function on a finite space area.

The function is sampled and a cubic interpolation is then used to calculate a cubic spline approximation of the function. As the spline has a constant cost of evaluation, this decreases the evaluation time of functions which are very often used.

The sampling and interpolation are done locally and on demand, so that the caching is done progressively when the function is evaluated. Coordinates are normalised to the range [0, 1] to avoid float accuracy troubles. The values of the function are normalised if their boundaries are given.

Parameters
  • function2d (object) – 2D function to be cached.

  • space_area (tuple) – space area where the function has to be cached: (minx, maxx, miny, maxy).

  • resolution (tuple) – resolution of the sampling: (resolutionx, resolutiony).

  • no_boundary_error – Behaviour when evaluated outside the caching area. When False a ValueError is raised. When True the function is directly evaluated (without caching). Default is False.

  • function_boundaries – Boundaries of the function values for normalisation: (min, max). If None, function values are not normalised. Default is None.

>>> from numpy import sqrt
>>> from time import sleep
>>> from cherab.core.math import Caching2D
>>>
>>> def expensive_radius(x, y):
>>>     sleep(5)
>>>     return sqrt(x**2 + y**2)
>>>
>>> f1 = Caching2D(expensive_radius, (-5, 5, -5, 5), (0.1, 0.1))
>>>
>>> # if you try this, first two executions will be slow, third will be fast
>>> f1(1.5, 1.5)
2.121320343595476
>>> f1(1.6, 1.5)
2.19317121996626
>>> f1(1.55, 1.5)
2.156964925578382
class cherab.core.math.caching.caching3d.Caching3D

Bases: raysect.core.math.function.float.function3d.base.Function3D

Precalculate and cache a 3D function on a finite space area.

The function is sampled and a cubic interpolation is then used to calculate a cubic spline approximation of the function. As the spline has a constant cost of evaluation, this decreases the evaluation time of functions which are very often used.

The sampling and interpolation are done locally and on demand, so that the caching is done progressively when the function is evaluated. Coordinates are normalised to the range [0, 1] to avoid float accuracy troubles. The values of the function are normalised if their boundaries are given.

Parameters
  • function3d (object) – 3D function to be cached.

  • space_area (tuple) – space area where the function has to be cached: (minx, maxx, miny, maxy, minz, maxz).

  • resolution (tuple) – resolution of the sampling: (resolutionx, resolutiony, resolutionz).

  • no_boundary_error – Behaviour when evaluated outside the caching area. When False a ValueError is raised. When True the function is directly evaluated (without caching). Default is False.

  • function_boundaries – Boundaries of the function values for normalisation: (min, max). If None, function values are not normalised. Default is None.

>>> from numpy import sqrt
>>> from time import sleep
>>> from cherab.core.math import Caching3D
>>>
>>> def expensive_radius(x, y, z):
>>>     sleep(5)
>>>     return sqrt(x**2 + y**2 + z**2)
>>>
>>> f1 = Caching3D(expensive_radius, (-5, 5, -5, 5, -5, 5), (0.1, 0.1, 0.1))
>>>
>>> # if you try this, first two executions will be slow, third will be fast
>>> # Note: the first execution might be particularly slow, this is because it
>>> # sets up the caching structures on first execution.
>>> f1(1.5, 1.5, 1.5)
2.598076
>>> f1(1.6, 1.5, 1.5)
2.657066
>>> f1(1.55, 1.5, 1.5)
2.627260