<!--
This file is generated by a tool. Do not edit directly.
For open-source contributions the docs will be updated automatically.
-->

*Last updated: 2023-03-16.*

<div itemscope itemtype="http://developers.google.com/ReferenceObject">
<meta itemprop="name" content="tf_quant_finance.math.diff" />
<meta itemprop="path" content="Stable" />
</div>

# tf_quant_finance.math.diff

<!-- Insert buttons and diff -->

<table class="tfo-notebook-buttons tfo-api" align="left">
</table>

<a target="_blank" href="https://github.com/paolodelia99/tf-quant-finance/blob/main/tf_quant_finance/math/diff_ops.py">View source</a>



Computes the difference between elements of an array at a regular interval.

```python
tf_quant_finance.math.diff(
    x, order=1, exclusive=False, axis=-1, dtype=None, name=None
)
```



<!-- Placeholder for "Used in" -->

For a difference along the final axis, if exclusive is True, then computes:

```
  result[..., i] = x[..., i+order] - x[..., i] for i < size(x) - order

```

This is the same as doing `x[..., order:] - x[..., :-order]`. Note that in
this case the result `Tensor` is smaller in size than the input `Tensor`.

If exclusive is False, then computes:

```
  result[..., i] = x[..., i] - x[..., i-order] for i >= order
  result[..., i] = x[..., i]  for 0 <= i < order

```

#### Example

```python
  x = tf.constant([1, 2, 3, 4, 5])
  dx = diff(x, order=1, exclusive=False)  # Returns [1, 1, 1, 1, 1]
  dx1 = diff(x, order=1, exclusive=True)  # Returns [1, 1, 1, 1]
  dx2 = diff(x, order=2, exclusive=False)  # Returns [1, 2, 2, 2, 2]
```

#### Args:


* <b>`x`</b>: A `Tensor` of shape `batch_shape + [n]` and of any dtype for which
  arithmetic operations are permitted.
* <b>`order`</b>: Positive Python int. The order of the difference to compute. `order =
  1` corresponds to the difference between successive elements.
  Default value: 1
* <b>`exclusive`</b>: Python bool. See description above.
  Default value: False
* <b>`axis`</b>: Python int. The axis of `x` along which to difference.
  Default value: -1 (the final axis).
* <b>`dtype`</b>: Optional `tf.DType`. If supplied, the dtype for `x` to use when
  converting to `Tensor`.
  Default value: None which maps to the default dtype inferred by TF.
* <b>`name`</b>: Python `str` name prefixed to Ops created by this class.
  Default value: None which is mapped to the default name 'diff'.


#### Returns:


* <b>`diffs`</b>: A `Tensor` of the same dtype as `x`. If `exclusive` is True,
  then the shape is `batch_shape + [n-min(order, n)]`, otherwise it is
  `batch_shape + [n]`. The final dimension of which contains the differences
  of the requested order.