-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathdistances.ts
49 lines (46 loc) · 1.17 KB
/
distances.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
*Returns the Inner Product similarity between vectors a and b
* @link [Inner Product Similarity algorithm](https://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf)
* @param a - first vector
* @param b - second vector
*
*/
export function innerProduct(a: number[], b: number[]): number {
let ans = 0;
for (let i = 0; i < a.length; i++) {
ans += a[i] * b[i];
}
return ans;
}
/**
*Returns the Chebyshev distance between vectors a and b
* @link [Chebyshev algorithm](https://en.wikipedia.org/wiki/Chebyshev_distance)
* @param a - first vector
* @param b - second vector
*
*/
export function chebyshev(a: number[], b: number[]): number {
let max = 0;
let aux = 0;
for (let i = 0; i < a.length; i++) {
aux = Math.abs(a[i] - b[i]);
if (max < aux) {
max = aux;
}
}
return max;
}
/**
*Returns the Manhattan distance between vectors a and b
* @link [Manhattan algorithm](https://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf)
* @param a - first vector
* @param b - second vector
*
*/
export function manhattan(a: number[], b: number[]): number {
let d = 0;
for (let i = 0; i < a.length; i++) {
d += Math.abs(a[i] - b[i]);
}
return d;
}