1.2.3. Functions and Interpolators

class raysect.core.math.function.function1d.base.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.autowrap.PythonFunction1D

Bases: raysect.core.math.function.function1d.base.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.function1d.constant.Constant1D

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

Wraps a scalar constant with a Function1D object.

This class allows a numeric Python scalar, such as a float or an integer, to interact with cython code that requires a Function1D object. The scalar must be convertible to double. The value of the scalar constant will be returned independent of the arguments the function is called with.

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

value (float) – the constant value to return when called

class raysect.core.math.function.function1d.arg.Arg1D

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

Returns the argument the function is passed, unmodified

This is used to pass coordinates through to other functions in the function framework which expect a Function1D object.

>>> arg = Arg1D()
>>> arg(2)
2.0
>>> arg(-3.14)
-3.14
>>> squarer = Arg1D()**2
>>> squarer(2)
4.0
>>> squarer(-3.14)
9.8596
class raysect.core.math.function.function1d.cmath.Exp1D

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

A Function1D class that implements the exponential of the result of a Function1D object: exp(f())

Parameters

function (Function1D) – A Function1D object.

class raysect.core.math.function.function1d.cmath.Sin1D

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

A Function1D class that implements the sine of the result of a Function1D object: sin(f())

Parameters

function (Function1D) – A Function1D object.

class raysect.core.math.function.function1d.cmath.Cos1D

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

A Function1D class that implements the cosine of the result of a Function1D object: cos(f())

Parameters

function (Function1D) – A Function1D object.

class raysect.core.math.function.function1d.cmath.Tan1D

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

A Function1D class that implements the tangent of the result of a Function1D object: tan(f())

Parameters

function (Function1D) – A Function1D object.

class raysect.core.math.function.function1d.cmath.Asin1D

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

A Function1D class that implements the arcsine of the result of a Function1D object: asin(f())

Parameters

function (Function1D) – A Function1D object.

class raysect.core.math.function.function1d.cmath.Acos1D

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

A Function1D class that implements the arccosine of the result of a Function1D object: acos(f())

Parameters

function (Function1D) – A Function1D object.

class raysect.core.math.function.function1d.cmath.Atan1D

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

A Function1D class that implements the arctangent of the result of a Function1D object: atan(f())

Parameters

function (Function1D) – A Function1D object.

class raysect.core.math.function.function1d.cmath.Atan4Q1D

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

A Function1D class that implements the arctangent of the result of 2 Function1D objects: atan2(f1(), f2())

This differs from Atan1D in that it takes separate functions for the numerator and denominator, in order to get the quadrant correct.

Parameters
  • numerator (Function1D) – A Function1D object representing the numerator

  • denominator (Function1D) – A Function1D object representing the denominator

class raysect.core.math.function.function2d.base.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.autowrap.PythonFunction2D

Bases: raysect.core.math.function.function2d.base.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.function2d.constant.Constant2D

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

Wraps a scalar constant with a Function2D object.

This class allows a numeric Python scalar, such as a float or an integer, to interact with cython code that requires a Function2D object. The scalar must be convertible to double. The value of the scalar constant will be returned independent of the arguments the function is called with.

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

value (float) – the constant value to return when called

class raysect.core.math.function.function2d.arg.Arg2D

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

Returns one of the arguments the function is passed, unmodified

This is used to pass coordinates through to other functions in the function framework which expect a Function2D object.

Valid options for argument are “x” and “y”.

>>> argx = Arg2D("x")
>>> argx(2, 3)
2.0
>>> argy = Arg2D("y")
>>> argy(2, 3)
3.0
>>> squarerx = argx**2
>>> squarerx(2, 3)
4.0
>>> squarery = argy**2
>>> squarery(2, 3)
9.0
Parameters

argument (str) – either “x” or “y”, the argument to return

class raysect.core.math.function.function2d.cmath.Exp2D

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

A Function2D class that implements the exponential of the result of a Function2D object: exp(f())

Parameters

function (Function2D) – A Function2D object.

class raysect.core.math.function.function2d.cmath.Sin2D

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

A Function2D class that implements the sine of the result of a Function2D object: sin(f())

Parameters

function (Function2D) – A Function2D object.

class raysect.core.math.function.function2d.cmath.Cos2D

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

A Function2D class that implements the cosine of the result of a Function2D object: cos(f())

Parameters

function (Function2D) – A Function2D object.

class raysect.core.math.function.function2d.cmath.Tan2D

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

A Function2D class that implements the tangent of the result of a Function2D object: tan(f())

Parameters

function (Function2D) – A Function2D object.

class raysect.core.math.function.function2d.cmath.Asin2D

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

A Function2D class that implements the arcsine of the result of a Function2D object: asin(f())

Parameters

function (Function2D) – A Function2D object.

class raysect.core.math.function.function2d.cmath.Acos2D

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

A Function2D class that implements the arccosine of the result of a Function2D object: acos(f())

Parameters

function (Function2D) – A Function2D object.

class raysect.core.math.function.function2d.cmath.Atan2D

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

A Function2D class that implements the arctangent of the result of a Function2D object: atan(f())

Parameters

function (Function2D) – A Function2D object.

class raysect.core.math.function.function2d.cmath.Atan4Q2D

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

A Function2D class that implements the arctangent of the result of 2 Function2D objects: atan2(f1(), f2())

This differs from Atan2D in that it takes separate functions for the numerator and denominator, in order to get the quadrant correct.

Parameters
  • numerator (Function2D) – A Function2D object representing the numerator

  • denominator (Function2D) – A Function2D object representing the denominator

class raysect.core.math.function.function3d.base.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.autowrap.PythonFunction3D

Bases: raysect.core.math.function.function3d.base.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.function.function3d.constant.Constant3D

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

Wraps a scalar constant with a Function3D object.

This class allows a numeric Python scalar, such as a float or an integer, to interact with cython code that requires a Function3D object. The scalar must be convertible to double. The value of the scalar constant will be returned independent of the arguments the function is called with.

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()

Parameters

value (float) – the constant value to return when called

class raysect.core.math.function.function3d.arg.Arg3D

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

Returns one of the arguments the function is passed, unmodified

This is used to pass coordinates through to other functions in the function framework which expect a Function3D object.

Valid options for argument are “x”, “y” or “z”.

>>> argx = Arg3D("x")
>>> argx(2, 3, 5)
2.0
>>> argy = Arg3D("y")
>>> argy(2, 3, 5)
3.0
>>> argz = Arg3D("z")
>>> argz(2, 3, 5)
5.0
>>> squarerx = argx**2
>>> squarerx(2, 3, 5)
4.0
>>> squarery = argy**2
>>> squarery(2, 3, 5)
9.0
>>> squarerz = argz**2
>>> squarerz(2, 3, 5)
25.0
Parameters

argument (str) – either “x”, “y” or “z”, the argument to return

class raysect.core.math.function.function3d.cmath.Exp3D

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

A Function3D class that implements the exponential of the result of a Function3D object: exp(f())

Parameters

function (Function3D) – A Function3D object.

class raysect.core.math.function.function3d.cmath.Sin3D

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

A Function3D class that implements the sine of the result of a Function3D object: sin(f())

Parameters

function (Function3D) – A Function3D object.

class raysect.core.math.function.function3d.cmath.Cos3D

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

A Function3D class that implements the cosine of the result of a Function3D object: cos(f())

Parameters

function (Function3D) – A Function3D object.

class raysect.core.math.function.function3d.cmath.Tan3D

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

A Function3D class that implements the tangent of the result of a Function3D object: tan(f())

Parameters

function (Function3D) – A Function3D object.

class raysect.core.math.function.function3d.cmath.Asin3D

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

A Function3D class that implements the arcsine of the result of a Function3D object: asin(f())

Parameters

function (Function3D) – A Function3D object.

class raysect.core.math.function.function3d.cmath.Acos3D

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

A Function3D class that implements the arccosine of the result of a Function3D object: acos(f())

Parameters

function (Function3D) – A Function3D object.

class raysect.core.math.function.function3d.cmath.Atan3D

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

A Function3D class that implements the arctangent of the result of a Function3D object: atan(f())

Parameters

function (Function3D) – A Function3D object.

class raysect.core.math.function.function3d.cmath.Atan4Q3D

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

A Function3D class that implements the arctangent of the result of 2 Function3D objects: atan2(f1(), f2())

This differs from Atan3D in that it takes separate functions for the numerator and denominator, in order to get the quadrant correct.

Parameters
  • numerator (Function3D) – A Function3D object representing the numerator

  • denominator (Function3D) – A Function3D object representing the denominator

class raysect.core.math.function.function2d.interpolate.discrete2dmesh.Discrete2DMesh

Bases: raysect.core.math.function.function2d.base.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.function.function2d.interpolate.interpolator2dmesh.Interpolator2DMesh

Bases: raysect.core.math.function.function2d.base.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