dgl.sparse.from_coo¶
-
dgl.sparse.
from_coo
(row: torch.Tensor, col: torch.Tensor, val: Optional[torch.Tensor] = None, shape: Optional[Tuple[int, int]] = None) → dgl.sparse.sparse_matrix.SparseMatrix[source]¶ Creates a sparse matrix from a coordinate list (COO), which stores a list of (row, column, value) tuples.
See COO in Wikipedia.
- Parameters
row (torch.Tensor) – The row indices of shape
(nnz)
col (torch.Tensor) – The column indices of shape
(nnz)
val (torch.Tensor, optional) – The values of shape
(nnz)
or(nnz, D)
. If None, it will be a tensor of shape(nnz)
filled by 1.shape (tuple[int, int], optional) – If not specified, it will be inferred from
row
andcol
, i.e.,(row.max() + 1, col.max() + 1)
. Otherwise,shape
should be no smaller than this.
- Returns
Sparse matrix
- Return type
Examples
Case1: Sparse matrix with row and column indices without values.
>>> dst = torch.tensor([1, 1, 2]) >>> src = torch.tensor([2, 4, 3]) >>> A = dglsp.from_coo(dst, src) SparseMatrix(indices=tensor([[1, 1, 2], [2, 4, 3]]), values=tensor([1., 1., 1.]), shape=(3, 5), nnz=3) >>> # Specify shape >>> A = dglsp.from_coo(dst, src, shape=(5, 5)) SparseMatrix(indices=tensor([[1, 1, 2], [2, 4, 3]]), values=tensor([1., 1., 1.]), shape=(5, 5), nnz=3)
Case2: Sparse matrix with scalar values.
>>> indices = torch.tensor([[1, 1, 2], [2, 4, 3]]) >>> val = torch.tensor([[1.], [2.], [3.]]) >>> A = dglsp.spmatrix(indices, val) SparseMatrix(indices=tensor([[1, 1, 2], [2, 4, 3]]), values=tensor([[1.], [2.], [3.]]), shape=(3, 5), nnz=3, val_size=(1,))
Case3: Sparse matrix with vector values.
>>> dst = torch.tensor([1, 1, 2]) >>> src = torch.tensor([2, 4, 3]) >>> val = torch.tensor([[1., 1.], [2., 2.], [3., 3.]]) >>> A = dglsp.from_coo(dst, src, val) SparseMatrix(indices=tensor([[1, 1, 2], [2, 4, 3]]), values=tensor([[1., 1.], [2., 2.], [3., 3.]]), shape=(3, 5), nnz=3, val_size=(2,))