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
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
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,))