compute.ndarray.slice¶
Summary¶
compute.ndarray.slice selects an ndarray region as a view. Use a NumPy-style string or explicit entries to crop, reverse, drop or insert axes without copying elements.
Syntax¶
slice<T extends CPUndarray | GPUndarray>(a: T, spec: NdarraySliceSpec): T
const crop = nd.slice(a, "1:4, ::2");
Parameters¶
| Name | Type | Required | Description |
|---|---|---|---|
a |
CPUndarray | GPUndarray |
Yes | Live source array. |
spec |
NdarraySliceSpec |
Yes | Comma-separated string or array of per-axis instructions. |
Returns¶
T - View with the same dtype and residency. The shape, signed byte strides and offset describe the selection. The backing range can be rebased. Destroy the view when finished and keep its root live.
Type Details¶
type NdarraySliceRange = readonly [start?: number | null, stop?: number | null, step?: number | null];
type NdarraySliceEntry = null | number | NdarraySliceRange | "..." | "newaxis";
type NdarraySliceSpec = string | ReadonlyArray<NdarraySliceEntry>;
| String item | Array entry | Effect |
|---|---|---|
: |
null |
Keep a full axis. Omitted trailing axes are also kept. |
| Integer | Number | Select an element and remove that axis. Negative indices count from the end. Out-of-range indices throw. |
start:stop:step |
[start, stop, step] |
Select a half-open range. Bounds are clipped to the axis. The step defaults to one and can be negative. Omitted bounds follow the step direction. A zero step throws. |
... |
"..." |
Expand to the unindexed axes. At most one ellipsis is allowed. |
None or newaxis |
"newaxis" |
Insert a length-one axis without consuming an input axis. |
The string form accepts surrounding whitespace and one trailing comma. An empty spec selects the full array. The number of consumed axes cannot exceed the input rank.
Example¶
const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const nd = wgpu.compute.ndarray;
const a = nd.fromArray("i32", [2, 3], [0, 1, 2, 3, 4, 5]);
const row = nd.slice(a, "-1, ::-1");
console.log(row.get(0), row.get(2)); // 5, 3
row.destroy();
a.destroy();