Internal function for converting a sparse matrix to string representation.
(spmat: SparseMatrix)
| 1447 | |
| 1448 | |
| 1449 | def _sparse_matrix_str(spmat: SparseMatrix) -> str: |
| 1450 | """Internal function for converting a sparse matrix to string |
| 1451 | representation. |
| 1452 | """ |
| 1453 | indices_str = str(torch.stack(spmat.coo())) |
| 1454 | values_str = str(spmat.val) |
| 1455 | meta_str = f"shape={spmat.shape}, nnz={spmat.nnz}" |
| 1456 | if spmat.val.dim() > 1: |
| 1457 | val_size = tuple(spmat.val.shape[1:]) |
| 1458 | meta_str += f", val_size={val_size}" |
| 1459 | prefix = f"{type(spmat).__name__}(" |
| 1460 | |
| 1461 | def _add_indent(_str, indent): |
| 1462 | lines = _str.split("\n") |
| 1463 | lines = [lines[0]] + [" " * indent + line for line in lines[1:]] |
| 1464 | return "\n".join(lines) |
| 1465 | |
| 1466 | final_str = ( |
| 1467 | "indices=" |
| 1468 | + _add_indent(indices_str, len("indices=")) |
| 1469 | + ",\n" |
| 1470 | + "values=" |
| 1471 | + _add_indent(values_str, len("values=")) |
| 1472 | + ",\n" |
| 1473 | + meta_str |
| 1474 | + ")" |
| 1475 | ) |
| 1476 | final_str = prefix + _add_indent(final_str, len(prefix)) |
| 1477 | return final_str |
no test coverage detected