---
title: "ClikaRT::ops::diagonal"
sidebar_label: "diagonal"
description: "Extract the offset-th diagonal between dim1 and dim2: the two source dims are removed and a trailing dim of the diagonal's length is appended (rank - 1 total)."
---

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

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

## diagonal() {#diagonal}

<div class="api-signature">

[`Tensor`](../Tensor.md) **`diagonal`**(<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`Tensor`](../Tensor.md) `input`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;`int64_t` `offset` `=` `0`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;`int64_t` `dim1` `=` `0`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;`int64_t` `dim2` `=` `1`<br />
)

</div>

Extract the `offset`-th diagonal between `dim1` and `dim2`: the two source dims are removed and a trailing dim of the diagonal's length is appended (rank - 1 total). Copies into fresh storage (deliberately not a view).

**Throws**

- `ClikaRT::Error`: (`INVALID_ARGUMENT`) when `dim1`/`dim2` collide or are out of range (`code_name()` carries the reason).

**Returns:** a new tensor of rank - 1 with the diagonal as its trailing dim.

<div class="api-declared">

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

</div>
