r"""Creates a sparse matrix from compress sparse column (CSC) format. See `CSC in Wikipedia `_. For column i of the sparse matrix - the row indices of the non-zero elements are stored in ``in
(
indptr: torch.Tensor,
indices: torch.Tensor,
val: Optional[torch.Tensor] = None,
shape: Optional[Tuple[int, int]] = None,
)
| 1014 | |
| 1015 | |
| 1016 | def from_csc( |
| 1017 | indptr: torch.Tensor, |
| 1018 | indices: torch.Tensor, |
| 1019 | val: Optional[torch.Tensor] = None, |
| 1020 | shape: Optional[Tuple[int, int]] = None, |
| 1021 | ) -> SparseMatrix: |
| 1022 | r"""Creates a sparse matrix from compress sparse column (CSC) format. |
| 1023 | |
| 1024 | See `CSC in Wikipedia <https://en.wikipedia.org/wiki/ |
| 1025 | Sparse_matrix#Compressed_sparse_column_(CSC_or_CCS)>`_. |
| 1026 | |
| 1027 | For column i of the sparse matrix |
| 1028 | |
| 1029 | - the row indices of the non-zero elements are stored in |
| 1030 | ``indices[indptr[i]: indptr[i+1]]`` |
| 1031 | - the corresponding values are stored in ``val[indptr[i]: indptr[i+1]]`` |
| 1032 | |
| 1033 | Parameters |
| 1034 | ---------- |
| 1035 | indptr : torch.Tensor |
| 1036 | Pointer to the row indices of shape N + 1, where N is the |
| 1037 | number of columns |
| 1038 | indices : torch.Tensor |
| 1039 | The row indices of shape nnz |
| 1040 | val : torch.Tensor, optional |
| 1041 | The values of shape ``(nnz)`` or ``(nnz, D)``. If None, it will be a |
| 1042 | tensor of shape ``(nnz)`` filled by 1. |
| 1043 | shape : tuple[int, int], optional |
| 1044 | If not specified, it will be inferred from :attr:`indptr` and |
| 1045 | :attr:`indices`, i.e., ``(indices.max() + 1, len(indptr) - 1)``. |
| 1046 | Otherwise, :attr:`shape` should be no smaller than this. |
| 1047 | |
| 1048 | Returns |
| 1049 | ------- |
| 1050 | SparseMatrix |
| 1051 | Sparse matrix |
| 1052 | |
| 1053 | Examples |
| 1054 | -------- |
| 1055 | |
| 1056 | Case1: Sparse matrix without values |
| 1057 | |
| 1058 | .. code:: |
| 1059 | |
| 1060 | [[0, 1, 0], |
| 1061 | [0, 0, 1], |
| 1062 | [1, 1, 1]] |
| 1063 | |
| 1064 | >>> indptr = torch.tensor([0, 1, 3, 5]) |
| 1065 | >>> indices = torch.tensor([2, 0, 2, 1, 2]) |
| 1066 | >>> A = dglsp.from_csc(indptr, indices) |
| 1067 | SparseMatrix(indices=tensor([[2, 0, 2, 1, 2], |
| 1068 | [0, 1, 1, 2, 2]]), |
| 1069 | values=tensor([1., 1., 1., 1., 1.]), |
| 1070 | shape=(3, 3), nnz=5) |
| 1071 | >>> # Specify shape |
| 1072 | >>> A = dglsp.from_csc(indptr, indices, shape=(5, 3)) |
| 1073 | SparseMatrix(indices=tensor([[2, 0, 2, 1, 2], |