face-api/src/euclideanDistance.ts

13 lines
398 B
TypeScript
Raw Normal View History

2020-08-18 13:54:53 +02:00
export function euclideanDistance(arr1: number[] | Float32Array, arr2: number[] | Float32Array) {
2020-12-27 00:35:17 +01:00
if (arr1.length !== arr2.length) throw new Error('euclideanDistance: arr1.length !== arr2.length');
2020-08-26 00:24:48 +02:00
2020-12-27 00:35:17 +01:00
const desc1 = Array.from(arr1);
const desc2 = Array.from(arr2);
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
return Math.sqrt(
desc1
.map((val, i) => val - desc2[i])
2020-12-27 00:35:17 +01:00
.reduce((res, diff) => res + (diff ** 2), 0),
);
}