dgl.sparse.SparseMatrix.coalesce

SparseMatrix.coalesce()[source]

Returns a coalesced sparse matrix.

A coalesced sparse matrix satisfies the following properties:

  • the indices of the non-zero elements are unique,

  • the indices are sorted in lexicographical order.

The coalescing process will accumulate the non-zero elements of the same indices by summation.

The function does not support autograd.

Returns

The coalesced sparse matrix

Return type

SparseMatrix

Examples

>>> indices = torch.tensor([[1, 0, 0, 0, 1], [1, 1, 1, 2, 2]])
>>> val = torch.tensor([0, 1, 2, 3, 4])
>>> A = dglsp.spmatrix(indices, val)
>>> A.coalesce()
SparseMatrix(indices=tensor([[0, 0, 1, 1],
                             [1, 2, 1, 2]]),
             values=tensor([3, 3, 0, 4]),
             shape=(2, 3), nnz=4)