qp.math.diag

diag(values, k=0, like=None)[source]

Construct a diagonal tensor from a list of scalars.

Parameters:
  • values (tensor_like or Sequence[scalar]) – sequence of numeric values that make up the diagonal

  • k (int) – The diagonal in question. k=0 corresponds to the main diagonal. Use k>0 for diagonals above the main diagonal, and k<0 for diagonals below the main diagonal.

Returns:

the 2D diagonal tensor

Return type:

tensor_like

Example

>>> x = [1., 2., torch.tensor(3.)]
>>> qp.math.diag(x)
tensor([[1., 0., 0.],
        [0., 2., 0.],
        [0., 0., 3.]])
>>> y = torch.tensor([0.65, 0.2, 0.1])
>>> qp.math.diag(y, k=-1)
tensor([[0.0000, 0.0000, 0.0000, 0.0000],
        [0.6500, 0.0000, 0.0000, 0.0000],
        [0.0000, 0.2000, 0.0000, 0.0000],
        [0.0000, 0.0000, 0.1000, 0.0000]])
>>> z = torch.tensor([0.1, 0.2])
>>> qp.math.diag(z, k=1)
tensor([[0.0000, 0.1000, 0.0000],
        [0.0000, 0.0000, 0.2000],
        [0.0000, 0.0000, 0.0000]])