turboflow.pysolver_view.numerical_differentiation module

turboflow.pysolver_view.numerical_differentiation.approx_gradient(function_handle, x, f0=None, abs_step=None, method='central_finite_differences')[source]

Approximate the derivatives of a given function at a point using various differentiation methods.

Parameters:
function_handlecallable

The function for which derivatives are to be approximated.

xarray_like

The point at which derivatives are to be approximated.

abs_stepfloat or np.ndarray with the same size as x, optional

Step size for finite difference methods. By default uses a suitable value for each method.

methodstr, optional

The method to use for differentiation. Available options are: - ‘forward_finite_differences’ or ‘2-point’: Forward finite differences. - ‘central_finite_differences’ or ‘3-point’: Central finite differences. - ‘complex_step’ or ‘cs’: Complex step method.

Defaults to ‘central_finite_differences’.

Returns:
numpy.ndarray

The approximated derivatives of the function at the given point x.

Raises:
ValueError

If an invalid method is provided.

turboflow.pysolver_view.numerical_differentiation.approx_jacobian_hessians(f, x, abs_step=1e-05, lower_triangular=True)[source]

Calculate the Hessian matrices for each component of a vector-valued function using finite differences.

Parameters:
fcallable

The function for which to find the Hessian matrices. It must take a single argument which is a numpy array and can return a scalar or a numpy array.

xnumpy array

The point at which the Hessian matrices are calculated.

abs_stepfloat, optional

The step size for the finite differences, default is 1e-5.

lower_triangularbool, optional

If True, the Hessians are returned in a lower triangular form suitable for Pygmo, default is True.

Returns:
Hessiansnumpy array

A tensor where each slice along the first dimension corresponds to the Hessian matrix of each component of the function f at x. If f returns a scalar, the first dimension size is 1. If lower_triangular is True, the Hessians are returned in a lower triangular form.

turboflow.pysolver_view.numerical_differentiation.central_finite_differences(function_handle, x, abs_step=None)[source]

Gradient approximation by central finite differences.

Parameters:
function_handlecallable

The function for which the derivative is calculated.

xnp.ndarray

The point at which the derivative is calculated.

abs_stepfloat or np.ndarray with the same size as x, optional

The step size for finite differences. Default is cobic root of machine epsilon.

Returns:
np.ndarray

The gradient of the function at point x.

turboflow.pysolver_view.numerical_differentiation.complex_step_derivative(function_handle, x, abs_step=None)[source]

Gradient approximation using the complex step method.

Parameters:
function_handlecallable

The function for which the derivative is calculated.

xnp.ndarray

The point at which the derivative is calculated.

abs_stepfloat or np.ndarray with the same size as x, optional

The step size for the complex step. Default is machine epsilon.

Returns:
np.ndarray

The gradient of the function at point x.

turboflow.pysolver_view.numerical_differentiation.forward_finite_differences(function_handle, x, abs_step=None, f0=None)[source]

Gradient approximation by forward finite differences.

Parameters:
function_handlecallable

The function for which the derivative is calculated.

xnp.ndarray

The point at which the derivative is calculated.

abs_stepfloat or np.ndarray with the same size as x, optional

The step size for finite differences. Default is square root of machine epsilon.

Returns:
np.ndarray

The gradient of the function at point x.