MCPcopy Create free account
hub / github.com/OpenAnonymity/oa-chat / prepareVector

Function prepareVector

vector/utils.js:43–64  ·  view source on GitHub ↗
(input, dimension, normalize)

Source from the content-addressed store, hash-verified

41}
42
43export function prepareVector(input, dimension, normalize) {
44 const source = toFloat32Array(input, dimension);
45 const output = new Float32Array(dimension);
46 let sum = 0;
47
48 for (let i = 0; i < dimension; i += 1) {
49 const value = source[i];
50 output[i] = value;
51 sum += value * value;
52 }
53
54 if (normalize) {
55 if (sum > 0) {
56 const scale = 1 / Math.sqrt(sum);
57 for (let i = 0; i < dimension; i += 1) {
58 output[i] *= scale;
59 }
60 }
61 }
62
63 return output;
64}
65
66export function ensurePositiveInteger(value, fallback) {
67 if (!Number.isFinite(value)) return fallback;

Callers 4

searchMethod · 0.90
_prepareItemsMethod · 0.90
prepareItemsMethod · 0.90
searchMethod · 0.90

Calls 1

toFloat32ArrayFunction · 0.85

Tested by

no test coverage detected