# tensorflow::ops::MatrixDiagV2

`#include <array_ops.h>`

Returns a batched diagonal tensor with given batched diagonal values.

## Summary

Returns a tensor with the contents in `diagonal` as `k[0]`-th to `k[1]`-th diagonals of a matrix, with everything else padded with `padding`. `num_rows` and `num_cols` specify the dimension of the innermost matrix of the output. If both are not specified, the op assumes the innermost matrix is square and infers its size from `k` and the innermost dimension of `diagonal`. If only one of them is specified, the op assumes the unspecified value is the smallest possible based on other criteria.

Let `diagonal` have `r` dimensions `[I, J, ..., L, M, N]`. The output tensor has rank `r+1` with shape `[I, J, ..., L, M, num_rows, num_cols]` when only one diagonal is given (`k` is an integer or `k[0] == k[1]`). Otherwise, it has rank `r` with shape `[I, J, ..., L, num_rows, num_cols]`.

The second innermost dimension of `diagonal` has double meaning. When `k` is scalar or `k[0] == k[1]`, `M` is part of the batch size \[I, J, ..., M\], and the output tensor is:


```
output[i, j, ..., l, m, n]
  = diagonal[i, j, ..., l, n-max(d_upper, 0)] ; if n - m == d_upper
    output[i, j, ..., l, m, n]                ; otherwise
```

<br />

Otherwise, `M` is treated as the number of diagonals for the matrix in the same batch (`M = k[1]-k[0]+1`), and the output tensor is:

<br />

```
output[i, j, ..., l, m, n]
  = diagonal[i, j, ..., l, k[1]-d, n-max(d, 0)] ; if d_lower <= d <= d_upper
    input[i, j, ..., l, m, n]                   ; otherwise
```
where `d = n - m`

<br />

For example:


```
# The main diagonal.
diagonal = np.array([[1, 2, 3, 4],            # Input shape: (2, 4)
                     [5, 6, 7, 8]])
tf.matrix_diag(diagonal) ==> [[[1, 0, 0, 0],  # Output shape: (2, 4, 4)
                               [0, 2, 0, 0],
                               [0, 0, 3, 0],
                               [0, 0, 0, 4]],
                              [[5, 0, 0, 0],
                               [0, 6, 0, 0],
                               [0, 0, 7, 0],
                               [0, 0, 0, 8]]]
```

<br />


```
# A superdiagonal (per batch).
diagonal = np.array([[1, 2, 3],  # Input shape: (2, 3)
                     [4, 5, 6]])
tf.matrix_diag(diagonal, k = 1)
  ==> [[[0, 1, 0, 0],  # Output shape: (2, 4, 4)
        [0, 0, 2, 0],
        [0, 0, 0, 3],
        [0, 0, 0, 0]],
       [[0, 4, 0, 0],
        [0, 0, 5, 0],
        [0, 0, 0, 6],
        [0, 0, 0, 0]]]
```

<br />


```
# A band of diagonals.
diagonals = np.array([[[1, 2, 3],  # Input shape: (2, 2, 3)
                       [4, 5, 0]],
                      [[6, 7, 9],
                       [9, 1, 0]]])
tf.matrix_diag(diagonals, k = (-1, 0))
  ==> [[[1, 0, 0],  # Output shape: (2, 3, 3)
        [4, 2, 0],
        [0, 5, 3]],
       [[6, 0, 0],
        [9, 7, 0],
        [0, 1, 9]]]
```

<br />


```
# Rectangular matrix.
diagonal = np.array([1, 2])  # Input shape: (2)
tf.matrix_diag(diagonal, k = -1, num_rows = 3, num_cols = 4)
  ==> [[0, 0, 0, 0],  # Output shape: (3, 4)
       [1, 0, 0, 0],
       [0, 2, 0, 0]]
```

<br />


```
# Rectangular matrix with inferred num_cols and padding = 9.
tf.matrix_diag(diagonal, k = -1, num_rows = 3, padding = 9)
  ==> [[9, 9],  # Output shape: (3, 2)
       [1, 9],
       [9, 2]]
```

<br />

Arguments:

- scope: A [Scope](https://www.tensorflow.org/versions/r1.15/api_docs/cc/class/tensorflow/scope#classtensorflow_1_1_scope) object
- diagonal: Rank `r`, where `r >= 1`
- k: Diagonal offset(s). Positive value means superdiagonal, 0 refers to the main diagonal, and negative value means subdiagonals. `k` can be a single integer (for a single diagonal) or a pair of integers specifying the low and high ends of a matrix band. `k[0]` must not be larger than `k[1]`.
- num_rows: The number of rows of the output matrix. If it is not provided, the op assumes the output matrix is a square matrix and infers the matrix size from k and the innermost dimension of `diagonal`.
- num_cols: The number of columns of the output matrix. If it is not provided, the op assumes the output matrix is a square matrix and infers the matrix size from k and the innermost dimension of `diagonal`.
- padding_value: The number to fill the area outside the specified diagonal band with. Default is 0.

<br />

Returns:

- `https://www.tensorflow.org/versions/r1.15/api_docs/cc/class/tensorflow/output#classtensorflow_1_1_output`: Has rank `r+1` when `k` is an integer or `k[0] == k[1]`, rank `r` otherwise.

<br />

| ### Constructors and Destructors ||
|---|---|
| `https://www.tensorflow.org/versions/r1.15/api_docs/cc/class/tensorflow/ops/matrix-diag-v2#classtensorflow_1_1ops_1_1_matrix_diag_v2_1a0f4757e436efeaed1ba2bd9690367b7f(const ::https://www.tensorflow.org/versions/r1.15/api_docs/cc/class/tensorflow/scope#classtensorflow_1_1_scope & scope, ::https://www.tensorflow.org/versions/r1.15/api_docs/cc/class/tensorflow/input#classtensorflow_1_1_input diagonal, ::https://www.tensorflow.org/versions/r1.15/api_docs/cc/class/tensorflow/input#classtensorflow_1_1_input k, ::https://www.tensorflow.org/versions/r1.15/api_docs/cc/class/tensorflow/input#classtensorflow_1_1_input num_rows, ::https://www.tensorflow.org/versions/r1.15/api_docs/cc/class/tensorflow/input#classtensorflow_1_1_input num_cols, ::https://www.tensorflow.org/versions/r1.15/api_docs/cc/class/tensorflow/input#classtensorflow_1_1_input padding_value)` ||

| ### Public attributes ||
|---|---|
| `https://www.tensorflow.org/versions/r1.15/api_docs/cc/class/tensorflow/ops/matrix-diag-v2#classtensorflow_1_1ops_1_1_matrix_diag_v2_1a09bb44acf7362d90e9b70dfaa3ebd8fd` | `https://www.tensorflow.org/versions/r1.15/api_docs/cc/class/tensorflow/operation#classtensorflow_1_1_operation` |
| `https://www.tensorflow.org/versions/r1.15/api_docs/cc/class/tensorflow/ops/matrix-diag-v2#classtensorflow_1_1ops_1_1_matrix_diag_v2_1a180f0e2069ba614f9a8bf4593d6b322a` | `::https://www.tensorflow.org/versions/r1.15/api_docs/cc/class/tensorflow/output#classtensorflow_1_1_output` |

| ### Public functions ||
|---|---|
| `https://www.tensorflow.org/versions/r1.15/api_docs/cc/class/tensorflow/ops/matrix-diag-v2#classtensorflow_1_1ops_1_1_matrix_diag_v2_1a5faf264fc6c643dfc49d1d5b00828973() const ` | `::tensorflow::Node *` |
| `https://www.tensorflow.org/versions/r1.15/api_docs/cc/class/tensorflow/ops/matrix-diag-v2#classtensorflow_1_1ops_1_1_matrix_diag_v2_1a8301ec1ffa503e41034e06b9fc8dfd93() const ` | ` `` ` |
| `https://www.tensorflow.org/versions/r1.15/api_docs/cc/class/tensorflow/ops/matrix-diag-v2#classtensorflow_1_1ops_1_1_matrix_diag_v2_1a96f20bc6c061cadeeb30e3986311fc2c() const ` | ` `` ` |

## Public attributes

### operation

```
Operation operation
```

### output

```
::tensorflow::Output output
```

## Public functions

### MatrixDiagV2

```
 MatrixDiagV2(
  const ::tensorflow::Scope & scope,
  ::tensorflow::Input diagonal,
  ::tensorflow::Input k,
  ::tensorflow::Input num_rows,
  ::tensorflow::Input num_cols,
  ::tensorflow::Input padding_value
)
```

### node

```
::tensorflow::Node * node() const 
```

### operator::tensorflow::Input

```
 operator::tensorflow::Input() const 
```

### operator::tensorflow::Output

```
 operator::tensorflow::Output() const 
```