derivkit.calculus_kit module

Provides the CalculusKit class.

A wrapper around the calculus helpers that exposes the gradient, Jacobian, and Hessian functions.

Typical usage examples:

>>> import numpy as np
>>> from derivkit.calculus_kit import CalculusKit
>>>
>>> def sin_function(x):
...     # scalar-valued function: f(x) = sin(x[0])
...     return np.sin(x[0])
>>>
>>> def identity_function(x):
...     # vector-valued function: f(x) = x
...     return np.asarray(x, dtype=float)
>>>
>>> calc = CalculusKit(sin_function, x0=np.array([0.5]))
>>> grad = calc.gradient()
>>> hess = calc.hessian()
>>>
>>> jac = CalculusKit(identity_function, x0=np.array([1.0, 2.0])).jacobian()
class derivkit.calculus_kit.CalculusKit(function: Callable[[Sequence[float] | ndarray], float | ndarray[tuple[Any, ...], dtype[floating]]], x0: Sequence[float] | ndarray)

Bases: object

Provides access to gradient, Jacobian, and Hessian tensors.

Initialises class with function and expansion point.

Parameters:
  • function – The function to be differentiated. Accepts a 1D array-like. Must return either a scalar (for gradient/Hessian) or a 1D array (for Jacobian).

  • x0 – Point at which to evaluate derivatives (shape (P,)).

gradient(*args, **kwargs) ndarray[tuple[Any, ...], dtype[floating]]

Returns the gradient of a scalar-valued function.

hessian(*args, **kwargs) ndarray[tuple[Any, ...], dtype[floating]]

Returns the Hessian of a scalar-valued function.

hessian_diag(*args, **kwargs) ndarray[tuple[Any, ...], dtype[floating]]

Returns the diagonal of the Hessian of a scalar-valued function.

jacobian(*args, **kwargs) ndarray[tuple[Any, ...], dtype[floating]]

Returns the Jacobian of a vector-valued function.