-
Notifications
You must be signed in to change notification settings - Fork 22
/
norms.js
38 lines (33 loc) · 821 Bytes
/
norms.js
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
var epsilon = 0.00001;
module.exports = {
L1: function(vector) {
var norm = 0;
for (var i = 0; i < vector.length; i++) {
norm += Math.abs(vector[i]);
}
var denom = norm + epsilon;
for (var i = 0; i < vector.length; i++) {
vector[i] /= denom;
}
},
'L1-sqrt': function(vector) {
var norm = 0;
for (var i = 0; i < vector.length; i++) {
norm += Math.abs(vector[i]);
}
var denom = norm + epsilon;
for (var i = 0; i < vector.length; i++) {
vector[i] = Math.sqrt(vector[i] / denom);
}
},
L2: function(vector) {
var sum = 0;
for (var i = 0; i < vector.length; i++) {
sum += Math.pow(vector[i], 2);
}
var denom = Math.sqrt(sum + epsilon);
for (var i = 0; i < vector.length; i++) {
vector[i] /= denom;
}
}
}