# tensorflow::ops::MatrixSetDiagV2

`#include <array_ops.h>`

Returns a batched matrix tensor with new batched diagonal values.

## Summary

Given `input` and `diagonal`, this operation returns a tensor with the same shape and values as `input`, except for the specified diagonals of the innermost matrices. These will be overwritten by the values in `diagonal`.

`input` has `r+1` dimensions `[I, J, ..., L, M, N]`. When `k` is scalar or `k[0] == k[1]`, `diagonal` has `r` dimensions `[I, J, ..., L, max_diag_len]`. Otherwise, it has `r+1` dimensions `[I, J, ..., L, num_diags, max_diag_len]`. `num_diags` is the number of diagonals, `num_diags = k[1] - k[0] + 1`. `max_diag_len` is the longest diagonal in the range `[k[0], k[1]]`, `max_diag_len = min(M + min(k[1], 0), N + min(-k[0], 0))`

The output is a tensor of rank `k+1` with dimensions `[I, J, ..., L, M, N]`. If `k` is scalar or `k[0] == k[1]`:


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

<br />

Otherwise,

<br />

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

<br />

For example:


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

<br />


```
# A superdiagonal (per batch).
tf.matrix_set_diag(diagonal, k = 1)
  ==> [[[7, 1, 7, 7],  # Output shape: (2, 3, 4)
        [7, 7, 2, 7],
        [7, 7, 7, 3]],
       [[7, 4, 7, 7],
        [7, 7, 5, 7],
        [7, 7, 7, 6]]]
```

<br />


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

<br />


```

```

<br />

Args:

- scope: A [Scope](https://www.tensorflow.org/api_docs/cc/class/tensorflow/scope#classtensorflow_1_1_scope) object
- input: Rank `r+1`, where `r >= 1`.
- diagonal: Rank `r` when `k` is an integer or `k[0] == k[1]`. Otherwise, it has rank `r+1`. `k >= 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]`.

<br />

Returns:

- `https://www.tensorflow.org/api_docs/cc/class/tensorflow/output#classtensorflow_1_1_output`: Rank `r+1`, with `output.shape = input.shape`.

<br />

| ### Constructors and Destructors ||
|---|---|
| `https://www.tensorflow.org/api_docs/cc/class/tensorflow/ops/matrix-set-diag-v2#classtensorflow_1_1ops_1_1_matrix_set_diag_v2_1a438f858712bda6df180ee19d8f278bf4(const ::https://www.tensorflow.org/api_docs/cc/class/tensorflow/scope#classtensorflow_1_1_scope & scope, ::https://www.tensorflow.org/api_docs/cc/class/tensorflow/input#classtensorflow_1_1_input input, ::https://www.tensorflow.org/api_docs/cc/class/tensorflow/input#classtensorflow_1_1_input diagonal, ::https://www.tensorflow.org/api_docs/cc/class/tensorflow/input#classtensorflow_1_1_input k)` ||

| ### Public attributes ||
|---|---|
| `https://www.tensorflow.org/api_docs/cc/class/tensorflow/ops/matrix-set-diag-v2#classtensorflow_1_1ops_1_1_matrix_set_diag_v2_1a433c91a80772823c3acd4729a873900f` | `https://www.tensorflow.org/api_docs/cc/class/tensorflow/operation#classtensorflow_1_1_operation` |
| `https://www.tensorflow.org/api_docs/cc/class/tensorflow/ops/matrix-set-diag-v2#classtensorflow_1_1ops_1_1_matrix_set_diag_v2_1a390fc69019f7170f80f7c4c3acb12cee` | `::https://www.tensorflow.org/api_docs/cc/class/tensorflow/output#classtensorflow_1_1_output` |

| ### Public functions ||
|---|---|
| `https://www.tensorflow.org/api_docs/cc/class/tensorflow/ops/matrix-set-diag-v2#classtensorflow_1_1ops_1_1_matrix_set_diag_v2_1ab0f95dc9ddcb2221701f55c8caddcdb1() const ` | `::tensorflow::Node *` |
| `https://www.tensorflow.org/api_docs/cc/class/tensorflow/ops/matrix-set-diag-v2#classtensorflow_1_1ops_1_1_matrix_set_diag_v2_1a333c742af8203776572da9009d8c0930() const ` |   |
| `https://www.tensorflow.org/api_docs/cc/class/tensorflow/ops/matrix-set-diag-v2#classtensorflow_1_1ops_1_1_matrix_set_diag_v2_1a0574bd8260d99f8d93fa3a0cb880f0fa() const ` |   |

## Public attributes

### operation

```
Operation operation
```

### output

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

## Public functions

### MatrixSetDiagV2

```
 MatrixSetDiagV2(
  const ::tensorflow::Scope & scope,
  ::tensorflow::Input input,
  ::tensorflow::Input diagonal,
  ::tensorflow::Input k
)
```

### node

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

### operator::tensorflow::Input

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

### operator::tensorflow::Output

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