Skip to content

compute.ndarray.minMax

Summary

compute.ndarray.minMax computes minima and maxima over the same axes in one operation. Use it when both bounds are needed, such as when choosing a display or normalization range.

Syntax

minMax<T extends CPUndarray | GPUndarray>(a: T, options?: NdarrayMinMaxOptions): NdarrayMinMaxResult<T>
const { min, max } = nd.minMax(a, { axes: -1 });

Parameters

Name Type Required Description
a CPUndarray | GPUndarray Yes Live array of real values.
options NdarrayMinMaxOptions No Defaults to reducing and removing all axes and propagating NaNs. Select axes and NaN rules as for min and max.

Returns

NdarrayMinMaxResult<T> - Two packed arrays in the input dtype and residency with the same output shape, including scalar results for a full reduction. Destroy each newly allocated array when finished.

Type Details

type NdarrayMinMaxOptions = Omit<NdarrayReduceOptions, "out" | "dtype" | "overflow"> & {
    out?: { min?: GPUndarray; max?: GPUndarray };
};
type NdarrayMinMaxResult<T> = { min: T; max: T };

For GPU calls, supply either or both outputs as live, matching C-contiguous arrays. Output ranges must be disjoint. Omitted members are allocated. The GPU submission and lifetime rules are the same as for compute.ndarray.contiguous.

Example

const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const nd = wgpu.compute.ndarray;
const a = nd.fromArray("f32", [3], [4, 2, 8]);
const { min, max } = nd.minMax(a);
console.log(min.get(), max.get()); // 2, 8
min.destroy();
max.destroy();
a.destroy();

Notes

The results follow the NaN, signed-zero and empty-slice rules of compute.ndarray.min and max.

See Also