toString method

  1. @override
String toString()
override

A string representation of this object.

Some classes have a default textual representation, often paired with a static parse function (like int.parse). These classes will provide the textual representation as their string representation.

Other classes have no meaningful textual representation that a program will care about. Such classes will typically override toString to provide useful information when inspecting the object, mainly for debugging or logging.

Implementation

@override
String toString() {
  if (height == 1 && width <= 10) {
    return 'Float32Matrix.fromRowVector([${elements.join(', ')}])';
  }
  if (width == 1 && height <= 10) {
    return 'Float32Matrix.fromColumnVector([${elements.join(', ')}])';
  }
  if (width <= 4 && height <= 4) {
    final sb = StringBuffer();
    sb.write('Float32Matrix.fromRows([\n');
    var columnWidth = elements.map((e) => e.toString().length).max() + 3;
    for (var y = 0; y < height; y++) {
      sb.write('  [ ');
      final numbersStartIndex = sb.length;
      for (var x = 0; x < width; x++) {
        // Write comma
        if (x != 0) {
          sb.write(',  ');
        }

        // Write enough whitespace
        while (sb.length - numbersStartIndex < x * columnWidth) {
          sb.write(' ');
        }

        // Write number
        sb.write(getXY(x, y));
      }
      sb.write(' ],\n');
    }
    sb.write('])');
    return sb.toString();
  }
  return 'Float32Matrix(...; width=$width, height=$height)';
}