---
title: "ClikaRT::ops::diag"
sidebar_label: "diag"
description: "Rank-1 <-> rank-2 diagonal converter. A vector of length L becomes an (L + |diagonal|) x (L + |diagonal|) matrix carrying it on the chosen diagonal (zeros elsewhere); a matrix input reads the chosen d…"
---

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

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

## diag() {#diag}

<div class="api-signature">

[`Tensor`](../Tensor.md) **`diag`**([`Tensor`](../Tensor.md) `input`, `int64_t` `diagonal` `=` `0`)

</div>

Rank-1 \<-\> rank-2 diagonal converter.

A vector of length `L` becomes an `(L + |diagonal|) x (L + |diagonal|)` matrix carrying it on the chosen diagonal (zeros elsewhere); a matrix input reads the chosen diagonal back out as a vector. [`diagonal`](./diagonal.md#diagonal): 0 = main, +k above, -k below. Copies.

**Throws**

- `ClikaRT::Error`: (`INVALID_ARGUMENT`) when the input is not rank-1 or rank-2 (`code_name()` carries the reason).

**Returns:** a new matrix (rank-1 input) or vector (rank-2 input).

<div class="api-declared">

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

</div>
