1.2.3. Functions and Interpolators

class raysect.core.math.function.function1d.Function1D

Cython optimised class for representing an arbitrary 1D function.

Using __call__() in cython is slow. This class provides an overloadable cython cdef evaluate() method which has much less overhead than a python function call.

For use in cython code only, this class cannot be extended via python.

To create a new function object, inherit this class and implement the evaluate() method. The new function object can then be used with any code that accepts a function object.

__call__

Evaluate the function f(x)

Parameters:x (float) – function parameter x
Return type:float
class raysect.core.math.function.function1d.PythonFunction1D

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

Wraps a python callable object with a Function1D object.

This class allows a python object to interact with cython code that requires a Function1D object. The python object must implement __call__() expecting one argument.

This class is intended to be used to transparently wrap python objects that are passed via constructors or methods into cython optimised code. It is not intended that the users should need to directly interact with these wrapping objects. Constructors and methods expecting a Function1D object should be designed to accept a generic python object and then test that object to determine if it is an instance of Function1D. If the object is not a Function1D object it should be wrapped using this class for internal use.

See also: autowrap_function1d()

Parameters:function (object) – the python function to wrap, __call__() function must be

implemented on the object.

class raysect.core.math.function.function2d.Function2D

Cython optimised class for representing an arbitrary 2D function.

Using __call__() in cython is slow. This class provides an overloadable cython cdef evaluate() method which has much less overhead than a python function call.

For use in cython code only, this class cannot be extended via python.

To create a new function object, inherit this class and implement the evaluate() method. The new function object can then be used with any code that accepts a function object.

__call__

Evaluate the function f(x, y)

Parameters:
  • x (float) – function parameter x
  • y (float) – function parameter y
Return type:

float

class raysect.core.math.function.function2d.PythonFunction2D

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

Wraps a python callable object with a Function2D object.

This class allows a python object to interact with cython code that requires a Function2D object. The python object must implement __call__() expecting two arguments.

This class is intended to be used to transparently wrap python objects that are passed via constructors or methods into cython optimised code. It is not intended that the users should need to directly interact with these wrapping objects. Constructors and methods expecting a Function2D object should be designed to accept a generic python object and then test that object to determine if it is an instance of Function2D. If the object is not a Function2D object it should be wrapped using this class for internal use.

See also: autowrap_function2d()

Parameters:function (object) – the python function to wrap, __call__() function must

be implemented on the object.

class raysect.core.math.function.function3d.Function3D

Cython optimised class for representing an arbitrary 3D function.

Using __call__() in cython is slow. This class provides an overloadable cython cdef evaluate() method which has much less overhead than a python function call.

For use in cython code only, this class cannot be extended via python.

To create a new function object, inherit this class and implement the evaluate() method. The new function object can then be used with any code that accepts a function object.

__call__

Evaluate the function f(x, y, z)

Parameters:
  • x (float) – function parameter x
  • y (float) – function parameter y
  • y – function parameter z
Return type:

float

class raysect.core.math.function.function3d.PythonFunction3D

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

Wraps a python callable object with a Function3D object.

This class allows a python object to interact with cython code that requires a Function3D object. The python object must implement __call__() expecting three arguments.

This class is intended to be used to transparently wrap python objects that are passed via constructors or methods into cython optimised code. It is not intended that the users should need to directly interact with these wrapping objects. Constructors and methods expecting a Function3D object should be designed to accept a generic python object and then test that object to determine if it is an instance of Function3D. If the object is not a Function3D object it should be wrapped using this class for internal use.

See also: autowrap_function3d()

class raysect.core.math.interpolators.discrete2dmesh.Discrete2DMesh

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

Discrete interpolator for data on a 2d ungridded tri-poly mesh.

The mesh is specified as a set of 2D vertices supplied as an Nx2 numpy array or a suitably sized sequence that can be converted to a numpy array.

The mesh triangles are defined with a Mx3 array where the three values are indices into the vertex array that specify the triangle vertices. The mesh must not contain overlapping triangles. Supplying a mesh with overlapping triangles will result in undefined behaviour.

A data array of length M, containing a value for each triangle, holds the data to be interpolated across the mesh.

By default, requesting a point outside the bounds of the mesh will cause a ValueError exception to be raised. If this is not desired the limit attribute (default True) can be set to False. When set to False, a default value will be returned for any point lying outside the mesh. The value return can be specified by setting the default_value attribute (default is 0.0).

To optimise the lookup of triangles, the interpolator builds an acceleration structure (a KD-Tree) from the specified mesh data. Depending on the size of the mesh, this can be quite slow to construct. If the user wishes to interpolate a number of different data sets across the same mesh - for example: temperature and density data that are both defined on the same mesh - then the user can use the instance() method on an existing interpolator to create a new interpolator. The new interpolator will shares a copy of the internal acceleration data. The triangle_data, limit and default_value can be customised for the new instance. See instance(). This will avoid the cost in memory and time of rebuilding an identical acceleration structure.

Parameters:
  • vertex_coords (ndarray) – An array of vertex coordinates (x, y) with shape Nx2.
  • triangles (ndarray) – An array of vertex indices defining the mesh triangles, with shape Mx3.
  • triangle_data (ndarray) – An array containing data for each triangle of shape Mx1.
  • limit (bool) – Raise an exception outside mesh limits - True (default) or False.
  • default_value (float) – The value to return outside the mesh limits if limit is set to False.
instance()

Creates a new interpolator instance from an existing interpolator instance.

The new interpolator instance will share the same internal acceleration data as the original interpolator. The triangle_data, limit and default_value settings of the new instance can be redefined by setting the appropriate attributes. If any of the attributes are set to None (default) then the value from the original interpolator will be copied.

This method should be used if the user has multiple sets of triangle_data that lie on the same mesh geometry. Using this methods avoids the repeated rebuilding of the mesh acceleration structures by sharing the geometry data between multiple interpolator objects.

Parameters:
  • instance (Discrete2DMesh) – Discrete2DMesh object.
  • triangle_data (ndarray) – An array containing data for each triangle of shape Mx1 (default None).
  • limit (bool) – Raise an exception outside mesh limits - True (default) or False (default None).
  • default_value (float) – The value to return outside the mesh limits if limit is set to False (default None).
Returns:

An Discrete2DMesh object.

Return type:

Discrete2DMesh

class raysect.core.math.interpolators.interpolator2dmesh.Interpolator2DMesh

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

Linear interpolator for data on a 2d ungridded tri-poly mesh.

The mesh is specified as a set of 2D vertices supplied as an Nx2 numpy array or a suitably sized sequence that can be converted to a numpy array.

The mesh triangles are defined with a Mx3 array where the three values are indices into the vertex array that specify the triangle vertices. The mesh must not contain overlapping triangles. Supplying a mesh with overlapping triangles will result in undefined behaviour.

A data array of length N, containing a value for each vertex, holds the data to be interpolated across the mesh.

By default, requesting a point outside the bounds of the mesh will cause a ValueError exception to be raised. If this is not desired the limit attribute (default True) can be set to False. When set to False, a default value will be returned for any point lying outside the mesh. The value return can be specified by setting the default_value attribute (default is 0.0).

To optimise the lookup of triangles, the interpolator builds an acceleration structure (a KD-Tree) from the specified mesh data. Depending on the size of the mesh, this can be quite slow to construct. If the user wishes to interpolate a number of different data sets across the same mesh - for example: temperature and density data that are both defined on the same mesh - then the user can use the instance() method on an existing interpolator to create a new interpolator. The new interpolator will shares a copy of the internal acceleration data. The vertex_data, limit and default_value can be customised for the new instance. See instance(). This will avoid the cost in memory and time of rebuilding an identical acceleration structure.

Parameters:
  • vertex_coords (ndarray) – An array of vertex coordinates (x, y) with shape Nx2.
  • vertex_data (ndarray) – An array containing data for each vertex of shape Nx1.
  • triangles (ndarray) – An array of vertex indices defining the mesh triangles, with shape Mx3.
  • limit (bool) – Raise an exception outside mesh limits - True (default) or False.
  • default_value (float) – The value to return outside the mesh limits if limit is set to False.
instance()

Creates a new interpolator instance from an existing interpolator instance.

The new interpolator instance will share the same internal acceleration data as the original interpolator. The vertex_data, limit and default_value settings of the new instance can be redefined by setting the appropriate attributes. If any of the attributes are set to None (default) then the value from the original interpolator will be copied.

This method should be used if the user has multiple sets of vertex_data that lie on the same mesh geometry. Using this methods avoids the repeated rebuilding of the mesh acceleration structures by sharing the geometry data between multiple interpolator objects.

Parameters:
  • instance (Interpolator2DMesh) – Interpolator2DMesh object.
  • vertex_data (ndarray) – An array containing data for each vertex of shape Nx1 (default None).
  • limit (bool) – Raise an exception outside mesh limits - True (default) or False (default None).
  • default_value (float) – The value to return outside the mesh limits if limit is set to False (default None).
Returns:

An Interpolator2DMesh object.

Return type:

Interpolator2DMesh