Unverified Commit d1e57006 authored by czkkkkkk's avatar czkkkkkk Committed by GitHub
Browse files

[Sparse] Polish the docstring of sparse_matrix.py (#5202)

* [Sparse] Polish the docstring of sparse_matrix.py

* Update

* Update

* Update

* Update
parent f145097f
...@@ -91,43 +91,92 @@ class SparseMatrix: ...@@ -91,43 +91,92 @@ class SparseMatrix:
""" """
return self.coo()[1] return self.coo()[1]
def coo(self) -> Tuple[torch.Tensor, ...]: def coo(self) -> Tuple[torch.Tensor, torch.Tensor]:
"""Returns the coordinate (COO) representation of the sparse matrix. r"""Returns the coordinate list (COO) representation of the sparse
matrix.
See `COO in Wikipedia <https://en.wikipedia.org/wiki/
Sparse_matrix#Coordinate_list_(COO)>`_.
Returns Returns
------- -------
Tuple[torch.Tensor, torch.Tensor] torch.Tensor
A tuple of tensors containing row and column coordinates Row coordinate
torch.Tensor
Column coordinate
Example
-------
>>> dst = torch.tensor([1, 2, 1])
>>> src = torch.tensor([2, 4, 3])
>>> A = from_coo(dst, src)
>>> A.coo()
(tensor([1, 2, 1]), tensor([2, 4, 3]))
""" """
return self.c_sparse_matrix.coo() return self.c_sparse_matrix.coo()
def csr(self) -> Tuple[torch.Tensor, ...]: def csr(self) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
r"""Returns the compressed sparse row (CSR) representation of the sparse r"""Returns the compressed sparse row (CSR) representation of the sparse
matrix. matrix.
See `CSR in Wikipedia <https://en.wikipedia.org/wiki/
Sparse_matrix#Compressed_sparse_row_(CSR, _CRS_or_Yale_format)>`_.
This function also returns value indices as an index tensor, indicating
the order of the values of non-zero elements in the CSR representation.
A ``None`` value indices array indicates the order of the values stays
the same as the values of the SparseMatrix.
Returns Returns
------- -------
Tuple[torch.Tensor, torch.Tensor, torch.Tensor] torch.Tensor
A tuple of tensors containing row indptr, column indices and value Row indptr
indices. Value indices is an index tensor, indicating the order of torch.Tensor
the values of non-zero elements in the CSR representation. A null Column indices
value indices indicates the order of the values stays the same as torch.Tensor
the values of the SparseMatrix. Value indices
Example
-------
>>> dst = torch.tensor([1, 2, 1])
>>> src = torch.tensor([2, 4, 3])
>>> A = from_coo(dst, src)
>>> A.csr()
(tensor([0, 0, 2, 3]), tensor([2, 3, 4]), tensor([0, 2, 1]))
""" """
return self.c_sparse_matrix.csr() return self.c_sparse_matrix.csr()
def csc(self) -> Tuple[torch.Tensor, ...]: def csc(self) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
r"""Returns the compressed sparse column (CSC) representation of the r"""Returns the compressed sparse column (CSC) representation of the
sparse matrix. sparse matrix.
See `CSC in Wikipedia <https://en.wikipedia.org/wiki/
Sparse_matrix#Compressed_sparse_column_(CSC_or_CCS)>`_.
This function also returns value indices as an index tensor, indicating
the order of the values of non-zero elements in the CSC representation.
A ``None`` value indices array indicates the order of the values stays
the same as the values of the SparseMatrix.
Returns Returns
------- -------
Tuple[torch.Tensor, torch.Tensor, torch.Tensor] torch.Tensor
A tuple of tensors containing column indptr, row indices and value Column indptr
indices. Value indices is an index tensor, indicating the order of torch.Tensor
the values of non-zero elements in the CSC representation. A null Row indices
value indices indicates the order of the values stays the same as torch.Tensor
the values of the SparseMatrix. Value indices
Example
-------
>>> dst = torch.tensor([1, 2, 1])
>>> src = torch.tensor([2, 4, 3])
>>> A = from_coo(dst, src)
>>> A.csc()
(tensor([0, 0, 0, 1, 2, 3]), tensor([1, 1, 2]), tensor([0, 2, 1]))
""" """
return self.c_sparse_matrix.csc() return self.c_sparse_matrix.csc()
...@@ -422,7 +471,11 @@ def from_coo( ...@@ -422,7 +471,11 @@ def from_coo(
val: Optional[torch.Tensor] = None, val: Optional[torch.Tensor] = None,
shape: Optional[Tuple[int, int]] = None, shape: Optional[Tuple[int, int]] = None,
) -> SparseMatrix: ) -> SparseMatrix:
r"""Creates a sparse matrix from row and column coordinates. r"""Creates a sparse matrix from a coordinate list (COO), which stores a list
of (row, column, value) tuples.
See `COO in Wikipedia
<https://en.wikipedia.org/wiki/Sparse_matrix#Coordinate_list_(COO)>`_.
Parameters Parameters
---------- ----------
...@@ -462,8 +515,18 @@ def from_coo( ...@@ -462,8 +515,18 @@ def from_coo(
values=tensor([1., 1., 1.]), values=tensor([1., 1., 1.]),
shape=(5, 5), nnz=3) shape=(5, 5), nnz=3)
Case2: Sparse matrix with scalar/vector values. Following example is with Case2: Sparse matrix with scalar values.
vector data.
>>> val = torch.tensor([[1.], [2.], [3.]])
>>> A = dglsp.from_coo(dst, src, val)
SparseMatrix(indices=tensor([[1, 1, 2],
[2, 4, 3]]),
values=tensor([[1.],
[2.],
[3.]]),
size=(3, 5), nnz=3, val_size=(1,))
Case3: Sparse matrix with vector values.
>>> dst = torch.tensor([1, 1, 2]) >>> dst = torch.tensor([1, 1, 2])
>>> src = torch.tensor([2, 4, 3]) >>> src = torch.tensor([2, 4, 3])
...@@ -494,7 +557,10 @@ def from_csr( ...@@ -494,7 +557,10 @@ def from_csr(
val: Optional[torch.Tensor] = None, val: Optional[torch.Tensor] = None,
shape: Optional[Tuple[int, int]] = None, shape: Optional[Tuple[int, int]] = None,
) -> SparseMatrix: ) -> SparseMatrix:
r"""Creates a sparse matrix from CSR indices. r"""Creates a sparse matrix from compress sparse row (CSR) format.
See `CSR in Wikipedia <https://en.wikipedia.org/wiki/
Sparse_matrix#Compressed_sparse_row_(CSR,_CRS_or_Yale_format)>`_.
For row i of the sparse matrix For row i of the sparse matrix
...@@ -581,7 +647,10 @@ def from_csc( ...@@ -581,7 +647,10 @@ def from_csc(
val: Optional[torch.Tensor] = None, val: Optional[torch.Tensor] = None,
shape: Optional[Tuple[int, int]] = None, shape: Optional[Tuple[int, int]] = None,
) -> SparseMatrix: ) -> SparseMatrix:
r"""Creates a sparse matrix from CSC indices. r"""Creates a sparse matrix from compress sparse column (CSC) format.
See `CSC in Wikipedia <https://en.wikipedia.org/wiki/
Sparse_matrix#Compressed_sparse_column_(CSC_or_CCS)>`_.
For column i of the sparse matrix For column i of the sparse matrix
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment