fn

swapaxes

Tensor
swapaxes(x: Tensor, axis0: int, axis1: int)
source

Return x with axes axis0 and axis1 exchanged.

NumPy-style spelling of pairwise axis transposition. Verbose alias of swapdims; both names refer to the same composite.

Parameters

xTensor
Input tensor.
axis0int
First axis to swap. Negative values count from the end.
axis1int
Second axis to swap. Negative values count from the end.

Returns

Tensor

Tensor with the same data as x but with axis0 and axis1 exchanged. Where possible Lucid returns a view; if the underlying storage is non-contiguous, a freshly materialised tensor is returned (still satisfying value semantics — Lucid never aliases in a way that would surprise in-place writers).

Notes

For a tensor with shape (s0,,sn1)(s_0, \dots, s_{n-1}), the output shape is

si={sa1,i=a0,sa0,i=a1,si,otherwise.s'_i = \begin{cases} s_{a_1}, & i = a_0, \\ s_{a_0}, & i = a_1, \\ s_i, & \text{otherwise.} \end{cases}

Some reference frameworks document swapaxes as having in-place view semantics — in Lucid it is always a (possibly fresh) value-view.

Examples

>>> import lucid
>>> x = lucid.zeros((2, 3, 4))
>>> lucid.swapaxes(x, 0, 2).shape
(4, 3, 2)