MCPcopy
hub / github.com/dmlc/dgl / from_coo

Function from_coo

python/dgl/sparse/sparse_matrix.py:845–921  ·  view source on GitHub ↗

r"""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 sha

(
    row: torch.Tensor,
    col: torch.Tensor,
    val: Optional[torch.Tensor] = None,
    shape: Optional[Tuple[int, int]] = None,
)

Source from the content-addressed store, hash-verified

843
844
845def from_coo(
846 row: torch.Tensor,
847 col: torch.Tensor,
848 val: Optional[torch.Tensor] = None,
849 shape: Optional[Tuple[int, int]] = None,
850) -> SparseMatrix:
851 r"""Creates a sparse matrix from a coordinate list (COO), which stores a list
852 of (row, column, value) tuples.
853
854 See `COO in Wikipedia
855 <https://en.wikipedia.org/wiki/Sparse_matrix#Coordinate_list_(COO)>`_.
856
857 Parameters
858 ----------
859 row : torch.Tensor
860 The row indices of shape ``(nnz)``
861 col : torch.Tensor
862 The column indices of shape ``(nnz)``
863 val : torch.Tensor, optional
864 The values of shape ``(nnz)`` or ``(nnz, D)``. If None, it will be a
865 tensor of shape ``(nnz)`` filled by 1.
866 shape : tuple[int, int], optional
867 If not specified, it will be inferred from :attr:`row` and :attr:`col`,
868 i.e., ``(row.max() + 1, col.max() + 1)``. Otherwise, :attr:`shape`
869 should be no smaller than this.
870
871 Returns
872 -------
873 SparseMatrix
874 Sparse matrix
875
876 Examples
877 --------
878
879 Case1: Sparse matrix with row and column indices without values.
880
881 >>> dst = torch.tensor([1, 1, 2])
882 >>> src = torch.tensor([2, 4, 3])
883 >>> A = dglsp.from_coo(dst, src)
884 SparseMatrix(indices=tensor([[1, 1, 2],
885 [2, 4, 3]]),
886 values=tensor([1., 1., 1.]),
887 shape=(3, 5), nnz=3)
888 >>> # Specify shape
889 >>> A = dglsp.from_coo(dst, src, shape=(5, 5))
890 SparseMatrix(indices=tensor([[1, 1, 2],
891 [2, 4, 3]]),
892 values=tensor([1., 1., 1.]),
893 shape=(5, 5), nnz=3)
894
895 Case2: Sparse matrix with scalar values.
896
897 >>> indices = torch.tensor([[1, 1, 2], [2, 4, 3]])
898 >>> val = torch.tensor([[1.], [2.], [3.]])
899 >>> A = dglsp.spmatrix(indices, val)
900 SparseMatrix(indices=tensor([[1, 1, 2],
901 [2, 4, 3]]),
902 values=tensor([[1.],

Callers 15

test_softmaxFunction · 0.90
test_muldiv_scalarFunction · 0.90
test_powFunction · 0.90
test_error_op_scalarFunction · 0.90
test_from_cooFunction · 0.90
test_denseFunction · 0.90
test_coo_to_csrFunction · 0.90
test_coo_to_cscFunction · 0.90
test_val_likeFunction · 0.90
test_coalesceFunction · 0.90
test_has_duplicateFunction · 0.90
test_printFunction · 0.90

Calls 1

spmatrixFunction · 0.85

Tested by 15

test_softmaxFunction · 0.72
test_muldiv_scalarFunction · 0.72
test_powFunction · 0.72
test_error_op_scalarFunction · 0.72
test_from_cooFunction · 0.72
test_denseFunction · 0.72
test_coo_to_csrFunction · 0.72
test_coo_to_cscFunction · 0.72
test_val_likeFunction · 0.72
test_coalesceFunction · 0.72
test_has_duplicateFunction · 0.72
test_printFunction · 0.72