reshape method

Tensor reshape(
  1. List<int> newShape
)

Reshape the tensor to a new shape. -1 infers that dimension.

Implementation

Tensor reshape(List<int> newShape) {
  final ns = List<int>.from(newShape);
  int inferIdx = -1;
  int known = 1;
  for (int i = 0; i < ns.length; i++) {
    if (ns[i] == -1) {
      assert(inferIdx == -1, 'Only one dimension can be -1');
      inferIdx = i;
    } else {
      known *= ns[i];
    }
  }
  if (inferIdx >= 0) {
    ns[inferIdx] = size ~/ known;
  }
  assert(_productOfShape(ns) == size,
      'Cannot reshape ${shape} to $ns (size $size vs ${_productOfShape(ns)})');
  return Tensor(Float32List.fromList(data), ns);
}