# mot.library_functions package¶

## mot.library_functions.base module¶

class mot.library_functions.base.CLLibrary[source]
class mot.library_functions.base.SimpleCLLibrary(cl_code, **kwargs)[source]
class mot.library_functions.base.SimpleCLLibraryFromFile(return_type, cl_function_name, parameter_list, cl_code_file, var_replace_dict=None, **kwargs)[source]

Create a CL function for a library function.

These functions are not meant to be optimized, but can be used a helper functions in models.

Parameters: cl_function_name (str) – The name of the CL function cl_code_file (str) – The location of the code file var_replace_dict (dict) – In the cl_code file these replacements will be made (using the % format function of Python)
get_cl_code()[source]

Get the function code for this function and all its dependencies, with include guards.

Returns: The CL code for inclusion in a kernel. str

## mot.library_functions.error_functions module¶

class mot.library_functions.error_functions.CerfImWOfX[source]

Calculate the cerf.

class mot.library_functions.error_functions.dawson[source]
class mot.library_functions.error_functions.erfi[source]

Calculate the imaginary error function for a real argument (special case).

Compute erfi(x) = -i erf(ix), the imaginary error function.

## mot.library_functions.lanczos module¶

class mot.library_functions.lanczos.lanczos_sum_expg_scaled[source]

Copied from Scipy (https://github.com/scipy/scipy/blob/master/scipy/special/cephes/lanczos.c), 2018-05-07.

## mot.library_functions.unity module¶

class mot.library_functions.unity.lgam1p[source]

Compute lgam(x + 1).

This is a simplification of the corresponding function in scipy https://github.com/scipy/scipy/blob/master/scipy/special/cephes/unity.c 2018-05-14

class mot.library_functions.unity.log1pmx[source]

log(1 + x) - x

## Module contents¶

class mot.library_functions.Besseli0[source]

Return the zeroth-order modified Bessel function of the first kind

Original author of C code: M.G.R. Vogelaar

class mot.library_functions.EuclidianNormFunction(memspace='private', memtype='mot_float_type')[source]

A CL functions for calculating the Euclidian distance between n values.

Parameters: memspace (str) – The memory space of the memtyped array (private, constant, global). memtype (str) – the memory type to use, double, float, mot_float_type, …
class mot.library_functions.LevenbergMarquardt(eval_func, nmr_parameters, nmr_observations, jacobian_func, patience=250, step_bound=100.0, scale_diag=1, usertol_mult=30, **kwargs)[source]

The Powell CL implementation.

Parameters: eval_func (mot.lib.cl_function.CLFunction) – the function we want to optimize, Should be of signature: void evaluate(local mot_float_type* x, void* data_void, local mot_float_type* result); nmr_parameters (int) – the number of parameters in the model, this will be hardcoded in the method nmr_observations (int) – the number of observations in the model jacobian_func (mot.lib.cl_function.CLFunction) – the function used to compute the Jacobian. patience (int) – the patience of the Powell algorithm patience_line_search (int) – the patience of the line search algorithm reset_method (str) – one of RESET_TO_IDENTITY or EXTRAPOLATED_POINT. The method used to reset the search directions every iteration.
get_kernel_data()[source]

Get the kernel data needed for this optimization routine to work.

class mot.library_functions.LibNMSimplex(function_name)[source]

The NMSimplex algorithm as a reusable library component.

Parameters: function_name (str) – the name of the evaluation function to call, defaults to ‘evaluate’. This should point to a function with signature: double evaluate(local mot_float_type* x, void* data_void);
class mot.library_functions.LogBesseli0[source]

Return the log of the zeroth-order modified Bessel function of the first kind.

class mot.library_functions.LogCosh[source]

Computes $$log(cosh(x))$$

For large x this will try to estimate it without overflow. For small x we use the opencl functions log and cos. The estimation for large numbers has been taken from: https://github.com/JaneliaSciComp/tmt/blob/master/basics/logcosh.m

class mot.library_functions.NMSimplex(function_name, nmr_parameters, patience=200, alpha=1.0, beta=0.5, gamma=2.0, delta=0.5, scale=0.1, adaptive_scales=True, **kwargs)[source]
get_kernel_data()[source]

Get the kernel data needed for this optimization routine to work.

class mot.library_functions.Powell(eval_func, nmr_parameters, patience=2, patience_line_search=None, reset_method='EXTRAPOLATED_POINT', **kwargs)[source]

The Powell CL implementation.

Parameters: eval_func (mot.lib.cl_function.CLFunction) – the function we want to optimize, Should be of signature: double evaluate(local mot_float_type* x, void* data_void); nmr_parameters (int) – the number of parameters in the model, this will be hardcoded in the method patience (int) – the patience of the Powell algorithm patience_line_search (int) – the patience of the line search algorithm. If None, we set it equal to the patience. reset_method (str) – one of RESET_TO_IDENTITY or EXTRAPOLATED_POINT. The method used to reset the search directions every iteration.
get_kernel_data()[source]

Get the kernel data needed for this optimization routine to work.

class mot.library_functions.Rand123[source]
class mot.library_functions.Subplex(eval_func, nmr_parameters, patience=10, patience_nmsimplex=100, alpha=1.0, beta=0.5, gamma=2.0, delta=0.5, scale=1.0, psi=0.001, omega=0.01, adaptive_scales=True, min_subspace_length='auto', max_subspace_length='auto', **kwargs)[source]

The Subplex optimization routines.

get_kernel_data()[source]

Get the kernel data needed for this optimization routine to work.

class mot.library_functions.eigen_decompose_real_symmetric_matrix[source]

Computes eigenvalues and eigenvectors of real symmetric matrix.

This uses the RS routine from the EISPACK code, to be found at: https://people.sc.fsu.edu/~jburkardt/c_src/eispack/eispack.html

It first tri-diagonalizes the matrix using Householder transformations and then computes the diagonal using QR transformations.

This routine only works with real symmetric matrices as input.

Parameters: n – input, the order of the matrix. A[n*n] – input, the real symmetric matrix to invert W[n] – output, the eigenvalues in ascending order. Z[n*n] – output, the eigenvectors, can coincide with A[n*n]. scratch[n] – input, scratch data
Output:
error code: the error for the the TQL2 routine (see EISPACK). The no-error, normal completion code is zero.
class mot.library_functions.eigenvalues_3x3_symmetric[source]

Calculate the eigenvalues of a symmetric 3x3 matrix.

This simple algorithm only works in case of a real and symmetric matrix.

The input to this function is an array with the upper triangular elements of the matrix. The output are the eigenvalues such that eig1 >= eig2 >= eig3, i.e. from large to small.

Parameters: A – the upper triangle of an 3x3 matrix v – the output eigenvalues as a vector of three elements.

References

[1]: https://en.wikipedia.org/wiki/Eigenvalue_algorithm#3.C3.973_matrices [2]: Smith, Oliver K. (April 1961), “Eigenvalues of a symmetric 3 × 3 matrix.”, Communications of the ACM,

4 (4): 168, doi:10.1145/355578.366316
class mot.library_functions.linear_cubic_interpolation[source]

Cubic interpolation for a one-dimensional grid.

This uses the theory of Cubic Hermite splines for interpolating a one-dimensional grid of values.

At the borders, it will clip the values to the nearest border.

Example usage:
constant float data[] = {1.0, 2.0, 5.0, 6.0}; linear_cubic_interpolation(1.5, 4, data);
class mot.library_functions.multiply_square_matrices[source]

Matrix multiplication of two square matrices.

Having this as a special function is slightly faster than a more generic matrix multiplication algorithm.

All matrices are expected to be in c/row-major order.

Parameters: n – the rectangular size of the matrix (the number of rows / the number of columns). A[n*n] – the left matrix B[n*n] – the right matrix C[n*n] – the output matrix
class mot.library_functions.pseudo_inverse_real_symmetric_matrix_upper_triangular[source]

Compute the pseudo-inverse of a real symmetric matrix stored as an upper triangular vector.

Results are placed in the upper triangular input vector.

Parameters: n – the size of the symmetric matrix A[n* (n+1) – on input, the matrix to inverse. On output, the inverse of the matrix. + 2*n*n] (scratch[2*n) – scratch data
class mot.library_functions.simpsons_rule(function_name)[source]

Create a CL function for integrating a function using Simpson’s rule.

This creates a CL function specifically meant for integrating the function of the given name. The name of the generated CL function will be ‘simpsons_rule_<function_name>’.

Parameters: function_name (str) – the name of the function to integrate, accepting the arguments: - a: the lower bound of the integral - b: the upper bound of the integral - n: the number of steps, i.e. the number of approximations to make - data: a pointer to some data, this is passed on to the function we are integrating.