Unverified Commit 17fad103 authored by Hongzhi (Steve), Chen's avatar Hongzhi (Steve), Chen Committed by GitHub
Browse files
parent a844d16e
......@@ -11,55 +11,17 @@ Sparse matrix class
.. class:: SparseMatrix
Class for creating a sparse matrix representation
A SparseMatrix can be created from Coordinate format indices using the
:func:`spmatrix` constructor:
There are a few ways to create a sparse matrix:
* In COO format using row and col indices, use :func:`from_coo`.
* In CSR format using row pointers and col indices, use :func:`from_csr`.
* In CSC format using col pointers and row indices, use :func:`from_csc`.
For example, one can create COO matrices as follows:
Case1: Sparse matrix with row and column indices without values
>>> row = torch.tensor([1, 1, 2])
>>> col = torch.tensor([2, 4, 3])
>>> A = from_coo(row, col)
>>> A
>>> indices = torch.tensor([[1, 1, 2],
>>> [2, 4, 3]])
>>> A = dglsp.spmatrix(indices)
SparseMatrix(indices=tensor([[1, 1, 2],
[2, 4, 3]]),
values=tensor([1., 1., 1.]),
shape=(3, 5), nnz=3)
Case2: Sparse matrix with scalar/vector values
>>> # vector values
>>> val = torch.tensor([[1, 1], [2, 2], [3, 3]])
>>> A = from_coo(row, col, val)
SparseMatrix(indices=tensor([[1, 1, 2],
[2, 4, 3]]),
values=tensor([[1, 1],
[2, 2],
[3, 3]]),
shape=(3, 5), nnz=3)
Similarly, one can create a CSR matrix as follows:
>>> indptr = torch.tensor([0, 1, 2, 5])
>>> indices = torch.tensor([1, 2, 0, 1, 2])
>>> val = torch.tensor([[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]])
>>> A = from_csr(indptr, indices, val)
>>> A
SparseMatrix(indices=tensor([[0, 1, 2, 2, 2],
[1, 2, 0, 1, 2]]),
values=tensor([[1, 1],
[2, 2],
[3, 3],
[4, 4],
[5, 5]]),
shape=(3, 3), nnz=5)
Creation Ops
````````
......
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