fn

diff

Tensor
diff(input: Tensor)
source

Compute the n-th order finite difference along an axis.

The first-order difference is yi=xi+1xiy_i = x_{i+1} - x_i; higher orders apply the operation recursively. Each application reduces the size of the chosen axis by one.

Parameters

inputTensor
Source tensor.

Returns

Tensor

Tensor with the differenced axis shorter by n elements.

Notes

Δnx[i]  =  Δn1x[i+1]    Δn1x[i],Δ0x=x.\Delta^n x[i] \;=\; \Delta^{n-1} x[i+1] \;-\; \Delta^{n-1} x[i] , \qquad \Delta^0 x = x .

First-order differences are widely used as a discrete approximation to the derivative on a unit-spaced grid.

Examples

>>> import lucid
>>> x = lucid.tensor([1.0, 3.0, 6.0, 10.0])
>>> lucid.diff(x)
Tensor([2., 3., 4.])