derivkit.utils module

Provides assorted utility functions.

derivkit.utils.central_difference_error_estimate(step_size, order=1)

Provides a rough truncation error estimate for central differences.

Parameters:
  • step_size (float) – Finite difference step size.

  • order (int) – Order of the derivative (1 to 4).

Returns:

Estimated error magnitude.

Return type:

float

derivkit.utils.generate_test_function(name='sin')

Returns a known test function and its first/second derivatives.

Parameters:

name (str) – One of ‘sin’, ‘exp’, ‘polynomial’, ‘gaussian’.

Returns:

(f(x), df/dx, d2f/dx2)

Return type:

tuple

derivkit.utils.is_finite_and_differentiable(function, x, delta=1e-05, tol=0.01)

Checks if a function is finite and numerically differentiable at a point.

Parameters:
  • function (callable) – The function to test.

  • x (float or np.ndarray) – The input value(s).

  • delta (float) – Step size for finite difference.

  • tol (float) – Tolerance for differentiability check.

Returns:

True if function is finite and differentiable at x, False

otherwise.

Return type:

bool

derivkit.utils.is_symmetric_grid(x_vals)

Checks if evaluation grid is symmetric around 0.

Parameters:

x_vals (np.ndarray) – Evaluation points (1D).

Returns:

True if grid is symmetric, False otherwise.

Return type:

bool

derivkit.utils.log_debug_message(message, debug=False, log_file=None, log_to_file=None)

Logs a debug message to stdout and optionally to a file.

Parameters:
  • message (str) – The debug message to print/log.

  • debug (bool) – Whether to print the message.

  • log_file (str or None) – Path to the log file.

  • log_to_file (bool or None) – Whether to write the message to the file.

derivkit.utils.normalize_derivative(derivative, reference)

Computes the relative error between estimated and reference derivative.

Parameters:
  • derivative (float or np.ndarray) – Estimated derivative.

  • reference (float or np.ndarray) – True/reference derivative.

Returns:

Normalized relative error.

Return type:

float or np.ndarray