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,)) for p input parameters.

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

Returns the gradient of a scalar-valued function.

This is a wrapper around derivkit.calculus.build_gradient(), with the function and theta0 arguments fixed to the values provided at initialization of CalculusKit. Any additional positional or keyword arguments are forwarded to derivkit.calculus.build_gradient().

Refer to the documentation of derivkit.calculus.build_gradient() for available options.

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

Returns the Hessian of a scalar-valued function.

This is a wrapper around derivkit.calculus.build_hessian(), with the function and theta0 arguments fixed to the values provided at initialization of CalculusKit. Any additional positional or keyword arguments are forwarded to derivkit.calculus.build_hessian().

Refer to the documentation of derivkit.calculus.build_hessian() for available options.

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

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

This is a wrapper around derivkit.calculus.build_hessian_diag(), with the function and theta0 arguments fixed to the values provided at initialization of CalculusKit. Any additional positional or keyword arguments are forwarded to derivkit.calculus.build_hessian_diag().

Refer to the documentation of derivkit.calculus.build_hessian_diag() for available options.

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

Returns the third-derivative tensor of a function.

This is a wrapper around derivkit.calculus.build_hyper_hessian(), with the function and theta0 arguments fixed to the values provided at initialization of CalculusKit. Any additional positional or keyword arguments are forwarded to derivkit.calculus.build_hyper_hessian().

Refer to the documentation of derivkit.calculus.build_hyper_hessian() for available options.

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

Returns the Jacobian of a vector-valued function.

This is a wrapper around derivkit.calculus.build_jacobian(), with the function and theta0 arguments fixed to the values provided at initialization of CalculusKit. Any additional positional or keyword arguments are forwarded to derivkit.calculus.build_jacobian().

Refer to the documentation of derivkit.calculus.build_jacobian() for available options.