| 16 | } |
| 17 | |
| 18 | export function toFloat32Array(input, dimension) { |
| 19 | if (input === null || input === undefined) { |
| 20 | throw new Error('Vector is required.'); |
| 21 | } |
| 22 | |
| 23 | let data; |
| 24 | if (input instanceof Float32Array) { |
| 25 | data = input; |
| 26 | } else if (ArrayBuffer.isView(input)) { |
| 27 | data = Float32Array.from(input); |
| 28 | } else if (input instanceof ArrayBuffer) { |
| 29 | data = new Float32Array(input); |
| 30 | } else if (Array.isArray(input)) { |
| 31 | data = Float32Array.from(input); |
| 32 | } else { |
| 33 | throw new Error('Unsupported vector type.'); |
| 34 | } |
| 35 | |
| 36 | if (data.length !== dimension) { |
| 37 | throw new Error(`Vector dimension mismatch (expected ${dimension}, got ${data.length}).`); |
| 38 | } |
| 39 | |
| 40 | return data; |
| 41 | } |
| 42 | |
| 43 | export function prepareVector(input, dimension, normalize) { |
| 44 | const source = toFloat32Array(input, dimension); |