# 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.sparse_matrix.SparseMatrix[source]ΒΆ

Creates a sparse 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

Sparse matrix

Return type

SparseMatrix

Examples

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

```[[1, 0, 0],
[0, 1, 0],
[0, 0, 1]]
```
```>>> dglsp.identity(shape=(3, 3))
SparseMatrix(indices=tensor([[0, 1, 2],
[0, 1, 2]]),
values=tensor([1., 1., 1.]),
shape=(3, 3), nnz=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))
SparseMatrix(indices=tensor([[0, 1, 2],
[0, 1, 2]]),
values=tensor([1., 1., 1.]),
shape=(3, 5), nnz=3)
```

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

```>>> dglsp.identity(shape=(3, 3), d=2)
SparseMatrix(indices=tensor([[0, 1, 2],
[0, 1, 2]]),
values=tensor([[1., 1.],
[1., 1.],
[1., 1.]]),
shape=(3, 3), nnz=3, val_size=(2,))
```