-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmathTools.js
More file actions
48 lines (48 loc) · 1.47 KB
/
mathTools.js
File metadata and controls
48 lines (48 loc) · 1.47 KB
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
"use strict";
function randInt(lower, upper) {
return lower + Math.floor(Math.random() * (upper - lower));
}
function shuffledBits(n, k) {
const arr = [...Array(n)].map((_, i) => {
if (i < k)
return 1;
else
return 0;
});
[...Array(k)].forEach((_, i) => {
const rand = randInt(i, n);
const reservoir = arr[i];
arr[i] = arr[rand];
arr[rand] = reservoir;
});
return arr;
}
//[...Array(1000)].forEach(()=>{shuffledBits(5,2).forEach((x,i)=>{test[i]+=x})})
function constrain(x, lower, upper) {
return Math.max(lower, Math.min(upper, x));
}
function constrainVec(v, absBound) {
const absV = v.map(x => x ^ 2).reduce((a, b) => a + b);
if (absV > absBound)
return v.map(x => x * absBound / absV);
else
return v;
}
function argMin(array) {
return array.map((x, i) => [x, i]).reduce((r, a) => (a[0] < r[0] ? a : r))[1];
}
function roulette(probs) {
const probSum = probs.reduce((a, b) => a + b, 0);
const normalizedProbs = probs.map(prob => prob / probSum);
const normalizedProbAccum = [0];
normalizedProbs.forEach(normalizedProb => {
normalizedProbAccum.push(normalizedProbAccum[normalizedProbAccum.length - 1] + normalizedProb);
});
let result = -1;
const rand = Math.random();
probs.forEach((_, i) => {
if (normalizedProbAccum[i] <= rand && rand < normalizedProbAccum[i + 1])
result = i;
});
return result;
}