derivkit.calculus.gradient module#

Contains functions used to construct the gradient of scalar-valued functions.

derivkit.calculus.gradient.build_gradient(function: Callable, theta0: ndarray, method: str | None = None, n_workers: int = 1, dk_init_kwargs: dict | None = None, **dk_diff_kwargs: dict) ndarray#

Returns the gradient of a scalar-valued function.

Parameters:
  • function – The function to be differentiated.

  • theta0 – The parameter vector at which the gradient is evaluated.

  • method – Method name or alias (e.g., "adaptive", "finite"). If None, the derivkit.derivative_kit.DerivativeKit default ("adaptive") is used.

  • n_workers – Number of workers used by derivkit.derivative_kit.DerivativeKit.differentiate(). This setting does not parallelize across parameters. Default is 1.

  • dk_init_kwargs – Optional keyword arguments passed to derivkit.derivative_kit.DerivativeKit when constructing the helper derivative objects. These control initialization-time behavior of DerivativeKit.

  • **dk_diff_kwargs – Additional keyword arguments passed to derivkit.derivative_kit.DerivativeKit.differentiate(). These control the numerical differentiation step itself.

Returns:

A 1D array representing the gradient.

Raises:

TypeError – If function does not return a scalar value.