reshape method

Tensor<T> reshape(
  1. List<int> shape
)

Returns a reshaped view, if possible. If a dimension is set to -1 it is inferred automatically to keep the length constant.

Implementation

Tensor<T> reshape(List<int> shape) {
  // Test if there is an undefined value.
  final inferredIndex = shape.indexOf(-1);
  if (inferredIndex >= 0) {
    shape = shape.toList(growable: false);
    shape[inferredIndex] = length ~/ -shape.product();
  }
  // Create new layout and copy data if necessary.
  final (layout_, data_) = layout.isContiguous
      ? (Layout(shape: shape, offset: layout.offset), data)
      : (Layout(shape: shape), type.copyList(values));
  // Check if the new layout is compatible at all.
  LayoutError.checkEqualLength(layout, layout_, 'shape');
  return Tensor<T>.internal(type: type, layout: layout_, data: data_);
}