Shape manipulation
Overview
Shape manipulation changes how the same data is interpreted across dimensions. Some operations return views over the same storage, while others create new arrays. Use arr.shape and arr.dim to verify the resulting layout.
Reshape
reshape reinterprets the data with a new shape without changing element order. The new shape must contain the same number of elements as the original array.
There is no -1 dimension inference, so all dimensions must be specified explicitly. When possible, reshape returns a view; otherwise it may copy data to keep storage consistent.
Flatten
flatten always returns a one-dimensional copy of the array.
Transpose
transpose reorders axes. With no arguments, it reverses the axes order.
For higher dimensions, provide the new axes order:
transpose returns a view with new shape and strides.
Squeeze and unsqueeze
squeeze removes axes of size 1. Use it to drop redundant dimensions.
unsqueeze inserts axes of size 1 at the specified positions:
You can also use expandDims/expandNDims as helpers for adding size-1 axes.
Concatenate and stack
Use cat to concatenate arrays along an axis. The shapes must match on all other axes.
Use mk.stack to add a new axis and combine arrays of the same shape:
cat and stack create new arrays and copy data.