fromBinary static method

Matrix fromBinary(
  1. ByteData byteData, {
  2. bool jsonFormat = false,
})

Importing from binary byteData: ByteData object containing the matrix data jsonFormat (optional): Set to true to use JSON string format, false to use binary format (default: false)

Expected output: m1 and m2 will be matrices with the same values as the original matrix stored in the ByteData object.

Example:

Matrix m1 = Matrix.fromBinary(byteData, jsonFormat: false); // Binary format
Matrix m2 = Matrix.fromBinary(byteData, jsonFormat: true); // JSON format

Implementation

static Matrix fromBinary(ByteData byteData, {bool jsonFormat = false}) {
  if (jsonFormat) {
    String jsonString = utf8.decode(byteData.buffer.asUint8List());
    return fromJSON(jsonString: jsonString);
  } else {
    int numRows = byteData.getInt32(0, Endian.little);
    int numCols = byteData.getInt32(4, Endian.little);
    int offset = 8;

    List<List<dynamic>> rows =
        List.generate(numRows, (_) => List.filled(numCols, Complex.zero()));
    for (int i = 0; i < numRows; i++) {
      for (int j = 0; j < numCols; j++) {
        rows[i][j] = Complex(byteData.getFloat64(offset, Endian.little));
        offset += 8;
      }
    }

    return Matrix.fromList(rows);
  }
}