Skip to content

compute.ndarray.reshape

Summary

compute.ndarray.reshape changes shape while preserving logical C-order element traversal. It creates a metadata view when the existing strides can represent the requested shape.

Syntax

reshape<T extends CPUndarray | GPUndarray>(a: T, shape: ReadonlyArray<number>): T
const b = nd.reshape(a, [2, -1]);

Parameters

Name Type Required Description
a CPUndarray | GPUndarray Yes Live input array.
shape ReadonlyArray<number> Yes Non-negative integer dimensions with the same element count. Use one -1 to infer a dimension from the others if their product is positive and divides the original count.

Returns

T - View with the requested shape and the original dtype and residency. A layout that requires a copy throws. Materialize it with compute.ndarray.contiguous() first.

Example

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

See Also