r"""Creates a sparse matrix with ones on the diagonal and zeros elsewhere. Parameters ---------- shape : tuple[int, int] Shape of the matrix. d : int, optional If None, the diagonal entries will be scaler 1. Otherwise, the diagonal entries will be a 1-valued
(
shape: Tuple[int, int],
d: Optional[int] = None,
dtype: Optional[torch.dtype] = None,
device: Optional[torch.device] = None,
)
| 1207 | |
| 1208 | |
| 1209 | def identity( |
| 1210 | shape: Tuple[int, int], |
| 1211 | d: Optional[int] = None, |
| 1212 | dtype: Optional[torch.dtype] = None, |
| 1213 | device: Optional[torch.device] = None, |
| 1214 | ) -> SparseMatrix: |
| 1215 | r"""Creates a sparse matrix with ones on the diagonal and zeros elsewhere. |
| 1216 | |
| 1217 | Parameters |
| 1218 | ---------- |
| 1219 | shape : tuple[int, int] |
| 1220 | Shape of the matrix. |
| 1221 | d : int, optional |
| 1222 | If None, the diagonal entries will be scaler 1. Otherwise, the diagonal |
| 1223 | entries will be a 1-valued tensor of shape ``(d)``. |
| 1224 | dtype : torch.dtype, optional |
| 1225 | The data type of the matrix |
| 1226 | device : torch.device, optional |
| 1227 | The device of the matrix |
| 1228 | |
| 1229 | Returns |
| 1230 | ------- |
| 1231 | SparseMatrix |
| 1232 | Sparse matrix |
| 1233 | |
| 1234 | Examples |
| 1235 | -------- |
| 1236 | |
| 1237 | Case1: 3-by-3 matrix with scaler diagonal values |
| 1238 | |
| 1239 | .. code:: |
| 1240 | |
| 1241 | [[1, 0, 0], |
| 1242 | [0, 1, 0], |
| 1243 | [0, 0, 1]] |
| 1244 | |
| 1245 | >>> dglsp.identity(shape=(3, 3)) |
| 1246 | SparseMatrix(indices=tensor([[0, 1, 2], |
| 1247 | [0, 1, 2]]), |
| 1248 | values=tensor([1., 1., 1.]), |
| 1249 | shape=(3, 3), nnz=3) |
| 1250 | |
| 1251 | Case2: 3-by-5 matrix with scaler diagonal values |
| 1252 | |
| 1253 | .. code:: |
| 1254 | |
| 1255 | [[1, 0, 0, 0, 0], |
| 1256 | [0, 1, 0, 0, 0], |
| 1257 | [0, 0, 1, 0, 0]] |
| 1258 | |
| 1259 | >>> dglsp.identity(shape=(3, 5)) |
| 1260 | SparseMatrix(indices=tensor([[0, 1, 2], |
| 1261 | [0, 1, 2]]), |
| 1262 | values=tensor([1., 1., 1.]), |
| 1263 | shape=(3, 5), nnz=3) |
| 1264 | |
| 1265 | Case3: 3-by-3 matrix with vector diagonal values |
| 1266 |