dgl.sparse.identity

dgl.sparse.identity(shape: Tuple[int, int], d: Optional[int] = None, dtype: Optional[torch.dtype] = None, device: Optional[torch.device] = None)dgl.sparse.diag_matrix.DiagMatrix[source]

Creates a diagonal matrix with ones on the diagonal and zeros elsewhere.

Parameters
  • shape (tuple[int, int]) – Shape of the matrix.

  • d (int, optional) – If None, the diagonal entries will be scaler 1. Otherwise, the diagonal entries will be a 1-valued tensor of shape (d).

  • dtype (torch.dtype, optional) – The data type of the matrix

  • device (torch.device, optional) – The device of the matrix

Returns

Diagonal matrix

Return type

DiagMatrix

Examples

Case1: 3-by-3 matrix with scaler diagonal values

[[1, 0, 0],
 [0, 1, 0],
 [0, 0, 1]]
>>> dglsp.identity(shape=(3, 3))
DiagMatrix(val=tensor([1., 1., 1.]),
           shape=(3, 3))

Case2: 3-by-5 matrix with scaler diagonal values

[[1, 0, 0, 0, 0],
 [0, 1, 0, 0, 0],
 [0, 0, 1, 0, 0]]
>>> dglsp.identity(shape=(3, 5))
DiagMatrix(val=tensor([1., 1., 1.]),
           shape=(3, 5))

Case3: 3-by-3 matrix with vector diagonal values

>>> dglsp.identity(shape=(3, 3), d=2)
DiagMatrix(values=tensor([[1., 1.],
                          [1., 1.],
                          [1., 1.]]),
           shape=(3, 3), val_size=(2,))