Creates a sparse matrix from an existing sparse matrix using new values. The new sparse matrix will have the same non-zero indices as the given sparse matrix and use the given values as the new non-zero values. Parameters ---------- mat : SparseMatrix An existing sparse
(mat: SparseMatrix, val: torch.Tensor)
| 1106 | |
| 1107 | |
| 1108 | def val_like(mat: SparseMatrix, val: torch.Tensor) -> SparseMatrix: |
| 1109 | """Creates a sparse matrix from an existing sparse matrix using new values. |
| 1110 | |
| 1111 | The new sparse matrix will have the same non-zero indices as the given |
| 1112 | sparse matrix and use the given values as the new non-zero values. |
| 1113 | |
| 1114 | Parameters |
| 1115 | ---------- |
| 1116 | mat : SparseMatrix |
| 1117 | An existing sparse matrix with non-zero values |
| 1118 | val : torch.Tensor |
| 1119 | The new values of the non-zero elements, a tensor of shape ``(nnz)`` or |
| 1120 | ``(nnz, D)`` |
| 1121 | |
| 1122 | Returns |
| 1123 | ------- |
| 1124 | SparseMatrix |
| 1125 | New sparse matrix |
| 1126 | |
| 1127 | Examples |
| 1128 | -------- |
| 1129 | |
| 1130 | >>> indices = torch.tensor([[1, 1, 2], [2, 4, 3]]) |
| 1131 | >>> val = torch.ones(3) |
| 1132 | >>> A = dglsp.spmatrix(indices, val) |
| 1133 | >>> A = dglsp.val_like(A, torch.tensor([2, 2, 2])) |
| 1134 | SparseMatrix(indices=tensor([[1, 1, 2], |
| 1135 | [2, 4, 3]]), |
| 1136 | values=tensor([2, 2, 2]), |
| 1137 | shape=(3, 5), nnz=3) |
| 1138 | """ |
| 1139 | assert ( |
| 1140 | val.dim() <= 2 |
| 1141 | ), "The values of a SparseMatrix can only be scalars or vectors." |
| 1142 | |
| 1143 | return SparseMatrix(torch.ops.dgl_sparse.val_like(mat.c_sparse_matrix, val)) |
| 1144 | |
| 1145 | |
| 1146 | def diag( |