| 15 | f.flush() |
| 16 | |
| 17 | def save_matrix_to_bin(filename, matrix, byte = 4): |
| 18 | try: |
| 19 | with open(filename, "wb") as file: |
| 20 | rows, cols = matrix.shape |
| 21 | # default is 4-bit int |
| 22 | if byte == 4: |
| 23 | file.write(rows.to_bytes(4, byteorder='little')) |
| 24 | file.write(cols.to_bytes(4, byteorder='little')) |
| 25 | elif byte == 8: |
| 26 | file.write(rows.to_bytes(8, byteorder='little', signed=False)) |
| 27 | file.write(cols.to_bytes(8, byteorder='little', signed=False)) |
| 28 | |
| 29 | matrix.T.tofile(file) |
| 30 | |
| 31 | print(f"Matrix saved to {filename}. Rows: {rows}, Cols: {cols}") |
| 32 | except IOError: |
| 33 | print("Cannot write to file") |
| 34 | |
| 35 | def load_matrix_from_bin(filename, byte = 4): |
| 36 | try: |