mindspore.ops.diag_embed

mindspore.ops.diag_embed(input, offset=0, dim1=- 2, dim2=- 1)[source]

Creates a tensor with diagonals filled by input. The remaining elements are filled by 0. If the shape of input is \([x_{0}, x_{1}, ..., x_{n-1}, x_{n}]\), the output shape is: the vector obtained by inserting \(x_{n}+|offset|\) into the vector \([x_{0}, x_{1}, ..., x_{n-1}]\) at position dim1 and dim2.

Parameters
  • input (Tensor) – Values to fill diagonal.

  • offset (int, optional) –

    Offset of the diagonal. \(offset=0\) refers to the main diagonal. Default: 0.

    • If \(offset>0\), fill the diagonals that are offset units upward from the main diagonal.

    • If \(offset<0\), fill the diagonals that are |offset| units downward from the main diagonal.

  • dim1 (int, optional) – The first dimension in input with respect to which to fill diagonal. Default: -2.

  • dim2 (int, optional) – The second dimension in input with respect to which to fill diagonal. Default: -1.

Returns

Tensor, has the same dtype as input, but the shape of output is one dimension higher than the input.

Raises
  • TypeError – If input is not a Tensor.

  • TypeError – If dtype of input is not supported.

  • TypeError – If offset is not an int.

  • TypeError – If dim1 or dim2 is not an int.

  • ValueError – If the dimension of input is not 1D-6D.

  • ValueError – If dim1 is not in range of [-len(input.shape) - 1, len(input.shape)].

  • ValueError – If dim2 is not in range of [-len(input.shape) - 1, len(input.shape)].

  • ValueError – If dim1 and dim2 are identical.

Supported Platforms:

Ascend GPU CPU

Examples

>>> x = Tensor(np.array([2,3,4]), mindspore.float32)
>>> output = ops.diag_embed(x)
>>> print(output)
[[2. 0. 0.]
 [0. 3. 0.]
 [0. 0. 4.]]