value property

T get value

Implementation

T get value {
  if (shape.isEmpty) return data[0] as T;

  if (shape.length == 1) {
    List<double> vec = [];
    for (int i = 0; i < shape[0]; i = i + 1) {
      vec.add(data[i]);
    }
    return vec as T;
  }

  if (shape.length == 2) {
    int rows = shape[0];
    int cols = shape[1];
    List<List<double>> mat = [];
    for (int i = 0; i < rows; i = i + 1) {
      List<double> row = [];
      for (int j = 0; j < cols; j = j + 1) {
        row.add(data[(i * cols) + j]);
      }
      mat.add(row);
    }
    return mat as T;
  }

  if (shape.length == 3) {
    int depth  = shape[0];
    int height = shape[1];
    int width  = shape[2];
    List<List<List<double>>> tensor3d = [];
    for (int d = 0; d < depth; d = d + 1) {
      List<List<double>> matrix = [];
      for (int h = 0; h < height; h = h + 1) {
        List<double> row = [];
        for (int w = 0; w < width; w = w + 1) {
          row.add(data[(d * height * width) + (h * width) + w]);
        }
        matrix.add(row);
      }
      tensor3d.add(matrix);
    }
    return tensor3d as T;
  }

  throw Exception('Unflattening beyond 3D is not supported.');
}