compute.ndarray.mean¶
Summary¶
compute.ndarray.mean computes one arithmetic mean per slice of the selected axes. Use keepDims: true when the means should broadcast back over the input, for example when centering data. For complex input it averages the real and imaginary components separately.
Syntax¶
mean<T extends CPUndarray | GPUndarray>(a: T, options?: NdarrayReduceOptions): T
const perRow = nd.mean(a, { axes: 1, keepDims: true });
Parameters¶
| Name | Type | Required | Description |
|---|---|---|---|
a |
CPUndarray | GPUndarray |
Yes | Live real or complex input. |
options |
NdarrayReduceOptions |
No | axes defaults to all axes and keepDims to false. nan defaults to "propagate". See the shared reduction options for axis, limit and GPU settings. |
Returns¶
T - Packed result in the input residency. The default dtype is f32 for integer input and the input dtype for float or complex input. Override it with f32 or f64 for real input or c64 or c128 for complex input. GPU mean rejects f64 and c128 inputs and outputs.
Example¶
const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const nd = wgpu.compute.ndarray;
const a = nd.fromArray("f32", [2, 2], [0, 2, 2, 4]);
const m = nd.mean(a, { axes: 1, keepDims: true });
console.log(m.shape, m.data()); // [2, 1], [1, 3]
m.destroy();
a.destroy();
Notes¶
A slice with no included values gives NaN. With nan: "omit", either NaN component excludes the entire complex element. GPU arithmetic can differ from CPU arithmetic in the low bits. Destroy allocated results when finished.