transpose method
Returns a view of this tensor with dimensions permuted according to axes.
This is a zero-copy operation that only changes the strides.
Throws ShapeMismatchException if axes length does not match rank.
Throws IndexOutOfBoundsException if axis is out of range.
Throws InvalidParameterException if axes contain duplicates.
Implementation
TensorBuffer transpose(List<int> axes) {
if (axes.length != rank) {
throw ShapeMismatchException.rank(rank, axes.length);
}
final seen = <int>{};
for (final axis in axes) {
if (axis < 0 || axis >= rank) {
throw IndexOutOfBoundsException(
index: axis,
min: 0,
max: rank - 1,
dimension: 'axis',
);
}
if (!seen.add(axis)) {
throw InvalidParameterException('axes', axes, 'Duplicate axis: $axis');
}
}
return TensorBuffer._view(
storage: storage,
shape: [for (final a in axes) shape[a]],
strides: [for (final a in axes) strides[a]],
storageOffset: storageOffset,
memoryFormat: memoryFormat,
);
}