compute.ndarray.viewAsReal¶
Summary¶
compute.ndarray.viewAsReal exposes both components of each complex element through a trailing real-valued axis. Use compute.ndarray.viewAsComplex to interpret aligned pairs of consecutive real components as complex elements.
Syntax¶
viewAsReal<T extends CPUndarray | GPUndarray>(a: T): T
viewAsComplex<T extends CPUndarray | GPUndarray>(a: T): T
const pairs = nd.viewAsReal(z);
Parameters¶
| Name | Type | Required | Description |
|---|---|---|---|
a |
CPUndarray | GPUndarray |
Yes | For viewAsReal, a c64 or c128 array. For viewAsComplex, an f32 or f64 array with a trailing axis of length two and a positive stride of one component on that axis. |
Returns¶
T - View in the input residency. viewAsReal appends a length-two axis with real then imaginary values and changes the dtype to f32 or f64. viewAsComplex removes that axis, returning c64 or c128, respectively. Both views alias the original storage.
Example¶
const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const nd = wgpu.compute.ndarray;
const pairs = nd.fromArray("f32", [2, 2], [1, -1, 2, -2]);
const z = nd.viewAsComplex(pairs);
console.log(z.get(1)); // [2, -2]
z.destroy();
pairs.destroy();
Notes¶
For viewAsComplex, the first address of a non-empty array must be aligned to the complex element size. Any outer axis with length greater than one must have a stride that is a multiple of that size. An incompatible layout throws. Use compute.ndarray.contiguous() to make an independent packed copy when the source layout prevents reinterpretation.