---
title: "ClikaRT::ops::diff"
sidebar_label: "diff"
description: "n-th order finite difference along dim. \\[ \\mathrm{diff}(x)_i = x_{i+1} - x_i \\quad \\text{(applied } n \\text{ times along } \\mathit{dim}\\text{)} \\] Each application shortens the axis by one, so the ou…"
---

<!-- Generated by tools/doxygen2mdx from Doxygen XML. Do not edit. -->

<span class="api-kind api-kind--function">function</span>

## diff() {#diff}

<div class="api-signature">

[`Tensor`](../Tensor.md) **`diff`**(<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`Tensor`](../Tensor.md) `input`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;`int64_t` `n` `=` `1`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;`int64_t` `dim` `=` `-1`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`OptionalTensor`](../../index.md#OptionalTensor) `prepend` `=` `{}`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`OptionalTensor`](../../index.md#OptionalTensor) `append` `=` `{}`<br />
)

</div>

n-th order finite difference along `dim`.

$$
\mathrm{diff}(x)_i = x_{i+1} - x_i \quad \text{(applied } n \text{ times along } \mathit{dim}\text{)}
$$

Each application shortens the axis by one, so the output's `dim` extent is `size(dim) - n`. `prepend` / `append` are concatenated onto the axis before differencing (they must match `input`'s shape outside `dim`).

**Parameters**

- `input`: the input tensor.
- `n`: how many times to difference. Default `1`.
- `dim`: the axis. Negative counts from the end. Default `-1`.
- `prepend`: optional tensor concatenated before `input` along `dim`.
- `append`: optional tensor concatenated after `input` along `dim`.

**Throws**

- `ClikaRT::Error`: when `dim` is out of range, `n` exceeds the (extended) axis length, or a prepend/append shape does not match (`code_name()` carries the reason).

**Returns:** dtype of `input`; `dim` shortened by `n` (after any prepend/append extension).

```cpp
auto d1 = ClikaRT::ops::diff(series);        // first difference
auto d2 = ClikaRT::ops::diff(series, 2);     // second difference
```

<div class="api-declared">

Declared in `ClikaRT/compute/ops.h`, line 5000

</div>
