Skip to content

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.

  • method defaults to "linear". "nearest" selects floor((d + 0.5) * nIn / nOut) for output index d. Linear and cubic sample at (d + 0.5) * nIn / nOut - 0.5. Cubic uses a finite cubicCoefficient, default -0.75.
  • alignCorners defaults to false. For linear or cubic, setting it to true samples at d * (nIn - 1) / (nOut - 1), with source coordinate zero for a length-one output.
  • antialias defaults to false. Set it to true only with linear or cubic sampling and alignCorners: false. It widens the filter when downsampling.
  • boundary defaults to "edge" and accepts the padding modes. "renormalize" discards outside taps and rescales the remaining weights. value defaults to zero and applies only to a constant boundary.
  • dtype defaults 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.

See Also