-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmathTools.ts
More file actions
46 lines (42 loc) · 1.6 KB
/
mathTools.ts
File metadata and controls
46 lines (42 loc) · 1.6 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
function randInt(lower: number, upper: number): number{// upperbound exclusive
return lower + Math.floor(Math.random() * (upper - lower));
}
function shuffledBits(n: number, k: number): (1 | 0)[]{
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: number, lower: number, upper: number): number{// upperbound inclusive
return Math.max(lower, Math.min(upper, x));
}
function constrainVec(v: number[], absBound: number): number[]{// bound inclusive
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: number[]): number{
return array.map((x, i) => [x, i]).reduce((r, a) => (a[0] < r[0] ? a : r))[1];
}
function roulette(probs: number[]): number{
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;
}