Derivative

Computation of higher order derivatives (functional, composable)

ndmap.derivative.derivative(order: tuple[int, ...], function: Callable, state: torch.Tensor, knobs: list[torch.Tensor], *pars: tuple, **kwargs: dict) list | torch.Tensor

Compute function derivatives for state and knobs upto given orders wrt state and knobs

Examples

>>> import torch
>>> from ndmap.derivative import derivative
>>> def fn(x, y):
...    x1, x2 = x
...    y1, y2 = y
...    return (x1 + x2 + x1**2 + x1*x2 + x2**2)*(1 + y1 + y2)
>>> x = torch.tensor([0.0, 0.0])
>>> y = torch.zeros_like(x)
>>> [[f, dfdy], [dfdx, dfdxdy]] = derivative((1, 1), fn, x, [y])
>>> f
tensor(0.)
>>> dfdy
tensor([0., 0.])
>>> dfdx
tensor([1., 1.])
>>> dfdxdy
tensor([[1., 1.],
        [1., 1.]])