Skip to content

compute.ndarray.transpose

Summary

compute.ndarray.transpose reorders axes as a view. Use compute.ndarray.swapAxes to exchange two axes or compute.ndarray.moveAxis to insert one axis at a different position while preserving the relative order of the others.

Syntax

transpose<T extends CPUndarray | GPUndarray>(a: T, axes?: ReadonlyArray<number>): T
swapAxes<T extends CPUndarray | GPUndarray>(a: T, axis1: number, axis2: number): T
moveAxis<T extends CPUndarray | GPUndarray>(a: T, source: number, destination: number): T
const b = nd.transpose(a, [1, 0]);

Parameters

Name Type Required Description
a CPUndarray | GPUndarray Yes Live input array.
axes ReadonlyArray<number> No Each input axis once, in output order. Defaults to reversing axis order.
axis1, axis2 number For swapAxes Axes to exchange.
source, destination number For moveAxis Input axis and its output position.

Axis indices are integers. Negative indices count from the end. Invalid indices or an invalid permutation throw.

Returns

T - View with permuted shape and strides. It shares the root's storage and retains the input dtype and residency.

Example

const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const nd = wgpu.compute.ndarray;
const a = nd.fromArray("f32", [2, 3], [1, 2, 3, 4, 5, 6]);
const b = nd.transpose(a);
console.log(b.shape, b.get(2, 1)); // [3, 2], 6
b.destroy();
a.destroy();

See Also