compute.ndarray.resize¶
Summary¶
compute.ndarray.resize changes the number of samples along selected axes and infers the sampling grid from the input and output sizes. Use resample when the grid is defined by physical spacing or origin.
Syntax¶
resize<T extends CPUndarray | GPUndarray>(a: T, options: NdarrayResizeOptions): T
const half = nd.resize(a, { axes: [0, 1], size: [64, 64], antialias: true });
Parameters¶
| Name | Type | Required | Description |
|---|---|---|---|
a |
CPUndarray | GPUndarray |
Yes | Live array, including a strided view. |
options |
NdarrayResizeOptions |
Yes | Axes, output sizes and interpolation settings. |
Returns¶
T - C-contiguous array in the source residency, with unselected axes unchanged. Destroy an allocated result when finished. GPU out and encoder behavior follow compute.ndarray.contiguous.
Type Details¶
type NdarrayResizeMethod = "nearest" | "linear" | "cubic";
type NdarrayResizeBoundary = NdarrayPadMode | "renormalize";
type NdarrayResizeOptions = NdarrayOperationOptions & {
axes: number | readonly number[];
size: number | readonly number[];
method?: NdarrayResizeMethod;
alignCorners?: boolean;
antialias?: boolean;
cubicCoefficient?: number;
boundary?: NdarrayResizeBoundary | readonly NdarrayResizeBoundary[];
value?: number | ComplexScalar;
dtype?: DType;
};
Pass at least one distinct axis, using negative indices to count from the end. size and boundary take one value for all selected axes or one entry per axis, in the supplied order. Sizes are non-negative integers that fit in u32. Resizing an empty axis to a nonzero size throws.
methoddefaults to"linear"."nearest"selectsfloor((d + 0.5) * nIn / nOut)for output indexd. Linear and cubic sample at(d + 0.5) * nIn / nOut - 0.5. Cubic uses a finitecubicCoefficient, default-0.75.alignCornersdefaults tofalse. For linear or cubic, setting it totruesamples atd * (nIn - 1) / (nOut - 1), with source coordinate zero for a length-one output.antialiasdefaults tofalse. Set it totrueonly with linear or cubic sampling andalignCorners: false. It widens the filter when downsampling.boundarydefaults to"edge"and accepts the padding modes."renormalize"discards outside taps and rescales the remaining weights.valuedefaults to zero and applies only to a constant boundary.dtypedefaults to the input type. Complex input requires a complex destination. Linear and cubic interpolate the two components independently. Integer conversion rounds ties to even and saturates.
Example¶
const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const nd = wgpu.compute.ndarray;
const a = nd.fromArray("f32", [2], [0, 4]);
const b = nd.resize(a, { axes: 0, size: 4 });
console.log(b.data()); // [0, 1, 3, 4]
b.destroy();
a.destroy();
Notes¶
Linear and cubic use f64 arithmetic on the CPU and f32 arithmetic on the GPU. GPU linear or cubic calls with f64 or c128 inputs or outputs throw. GPU nearest uses the cast dtype restrictions. A resize can fail if an intermediate or the output exceeds the available address space, GPU buffer size or storage binding limit.