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

Function spmatrix

python/dgl/sparse/sparse_matrix.py:763–842  ·  view source on GitHub ↗

r"""Creates a sparse matrix from Coordinate format indices. Parameters ---------- indices : tensor.Tensor The indices are the coordinates of the non-zero elements in the matrix, which should have shape of ``(2, N)`` where the first row is the row indices and the

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

Source from the content-addressed store, hash-verified

761
762
763def spmatrix(
764 indices: torch.Tensor,
765 val: Optional[torch.Tensor] = None,
766 shape: Optional[Tuple[int, int]] = None,
767) -> SparseMatrix:
768 r"""Creates a sparse matrix from Coordinate format indices.
769
770 Parameters
771 ----------
772 indices : tensor.Tensor
773 The indices are the coordinates of the non-zero elements in the matrix,
774 which should have shape of ``(2, N)`` where the first row is the row
775 indices and the second row is the column indices of non-zero elements.
776 val : tensor.Tensor, optional
777 The values of shape ``(nnz)`` or ``(nnz, D)``. If None, it will be a
778 tensor of shape ``(nnz)`` filled by 1.
779 shape : tuple[int, int], optional
780 If not specified, it will be inferred from :attr:`row` and :attr:`col`,
781 i.e., ``(row.max() + 1, col.max() + 1)``. Otherwise, :attr:`shape`
782 should be no smaller than this.
783
784 Returns
785 -------
786 SparseMatrix
787 Sparse matrix
788
789 Examples
790 --------
791
792 Case1: Sparse matrix with row and column indices without values.
793
794 >>> indices = torch.tensor([[1, 1, 2], [2, 4, 3]])
795 >>> A = dglsp.spmatrix(indices)
796 SparseMatrix(indices=tensor([[1, 1, 2],
797 [2, 4, 3]]),
798 values=tensor([1., 1., 1.]),
799 shape=(3, 5), nnz=3)
800 >>> # Specify shape
801 >>> A = dglsp.spmatrix(indices, shape=(5, 5))
802 SparseMatrix(indices=tensor([[1, 1, 2],
803 [2, 4, 3]]),
804 values=tensor([1., 1., 1.]),
805 shape=(5, 5), nnz=3)
806
807 Case2: Sparse matrix with scalar values.
808
809 >>> indices = torch.tensor([[1, 1, 2], [2, 4, 3]])
810 >>> val = torch.tensor([[1.], [2.], [3.]])
811 >>> A = dglsp.spmatrix(indices, val)
812 SparseMatrix(indices=tensor([[1, 1, 2],
813 [2, 4, 3]]),
814 values=tensor([[1.],
815 [2.],
816 [3.]]),
817 shape=(3, 5), nnz=3, val_size=(1,))
818
819 Case3: Sparse matrix with vector values.
820

Callers 8

test_negFunction · 0.90
rand_cooFunction · 0.90
rand_coo_uncoalescedFunction · 0.90
test_spspdivFunction · 0.90
adjMethod · 0.85
from_cooFunction · 0.85
from_torch_sparseFunction · 0.85

Calls 2

SparseMatrixClass · 0.70
toMethod · 0.45

Tested by 2

test_negFunction · 0.72
test_spspdivFunction · 0.72