compute.ndarray.varMean¶
Summary¶
compute.ndarray.varMean returns the variance and mean of the same slices in one operation. Use compute.ndarray.stdMean when you need the standard deviation instead, for instance to pass the resulting { std, mean } directly to normalize.
Syntax¶
varMean<T extends CPUndarray | GPUndarray>(a: T, options?: NdarrayVarMeanOptions): NdarrayVarMeanResult<T>
stdMean<T extends CPUndarray | GPUndarray>(a: T, options?: NdarrayStdMeanOptions): NdarrayStdMeanResult<T>
const { std, mean } = nd.stdMean(a, { axes: -1 });
Parameters¶
| Name | Type | Required | Description |
|---|---|---|---|
a |
CPUndarray | GPUndarray |
Yes | Live real or complex input. |
options |
NdarrayVarMeanOptions | NdarrayStdMeanOptions |
No | Defaults to all axes, keepDims: false, nan: "propagate" and ddof: 0. GPU calls can supply one or both output arrays. |
Returns¶
NdarrayVarMeanResult<T> | NdarrayStdMeanResult<T> - Two packed results with the same shape and residency. Variance or standard deviation is real and defaults to f64 for f64 or c128 input and f32 otherwise. The mean uses that precision and is complex for complex input. GPU calls use f32 or c64 and reject wide input or output dtypes.
Type Details¶
type NdarrayVarMeanOptions = Omit<NdarrayVarianceOptions, "out" | "overflow"> & {
out?: { var?: GPUndarray; mean?: GPUndarray };
};
type NdarrayStdMeanOptions = Omit<NdarrayVarianceOptions, "out" | "overflow"> & {
out?: { std?: GPUndarray; mean?: GPUndarray };
};
type NdarrayVarMeanResult<T> = { var: T; mean: T };
type NdarrayStdMeanResult<T> = { std: T; mean: T };
Use dtype to select f32 or f64 precision. The GPU output ranges must be disjoint and match their expected dtypes and shapes. The GPU encoder and allocation rules follow compute.ndarray.contiguous.
Example¶
const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const nd = wgpu.compute.ndarray;
const a = nd.fromArray("f32", [3], [2, 4, 6]);
const { var: variance, mean } = nd.varMean(a, { ddof: 1 });
console.log(variance.get(), mean.get()); // 4, 4
variance.destroy();
mean.destroy();
a.destroy();
Notes¶
ddof changes the variance or standard deviation divisor and leaves the mean independent of that divisor. With no included values, both results are NaN. Destroy any newly allocated outputs after use.