-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.0.0.alpha.ts
382 lines (380 loc) · 11.1 KB
/
1.0.0.alpha.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
* open-dice: Probability calculator for dice!
* Copyright (C) 2023 Coarse Rosinflower
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
"use strict";
class _d {
/**
* Returns the first argument, sorted in ascending order. If the second argument is true, also returns the list of indexes. Otherwise, if the second argument is an array, also returns the second argument sorted in accordance to the first.
*/
static sort(x: number[]): number[];
static sort(x: number[], z: boolean): any;
static sort(x: number[], z: any[]): [number[], any[]];
static sort(x: number[], z?: any): any {
if (x.length == 0) {
return (z ? [[], []] : []);
}
let a = x.slice(), b = Array(x.length), s: number, m: number, e: number, w: number, li: number, ri: number, ti: number, z2 = z ? (typeof z === "object" ? z.slice() : [...Array(x.length).keys()]) : undefined, c = Array(x.length);
if (z) {
for (w = 1; w < x.length; w *= 2) {
for (s = 0; s < x.length; s += 2*w) {
m = Math.min(s + w, x.length);
e = Math.min(s + 2 * w, x.length);
li = s, ri = m, ti = s;
while (li < m && ri < e) {
if (a[li] <= a[ri]) {
b[ti] = a[li]; c[ti++] = z2[li++];
} else {
b[ti] = a[ri]; c[ti++] = z2[ri++];
}
}
while (li < m) {
b[ti] = a[li]; c[ti++] = z2[li++];
}
while (ri < e) {
b[ti] = a[ri]; c[ti++] = z2[ri++];
}
}
a = b.slice();
z2 = c.slice();
}
} else {
for (w = 1; w < x.length; w *= 2) {
for (s = 0; s < x.length; s += 2*w) {
m = Math.min(s + w, x.length);
e = Math.min(s + 2*w, x.length);
li = s, ri = m, ti = s;
while (li < m && ri < e) {
if (a[li] <= a[ri]) {
b[ti++] = a[li++];
} else {
b[ti++] = a[ri++];
}
}
while (li < m) {
b[ti++] = a[li++];
}
while (ri < e) {
b[ti++] = a[ri++];
}
}
a = b.slice();
}
}
return z ? [a, z2] : a;
}
/**
* Returns the convolution of two numerical arrays.
*/
static convolve(x: number[], y: number[]): number[] {
// TODO: fftconvolve
let a = [...Array(x.length + y.length - 1)].map(() => 0);
for (let i = 0; i < x.length; i++) {
for (let j = 0; j < y.length; j++) {
a[i + j] += x[i] * y[j];
}
}
return a;
}
/**
* Converts an array of values into an [n, p] array, where n is the list of numbers and p is the list of probabilities.
*/
static arrayToNP(x: number[]): [number[], number[]] {
// This complicated nightmare is to avoid the 0.1 + 0.2 problem, as well as an inefficiency in linear search.
let a: number[] = [], n: number[] = [], p: number[] = [];
for (let f of x) {
if (!a[f]) {
n.push(f);
a[f]++;
}
}
n = _d.sort(n);
for (let i in n) {
p[i] = a[n[i]] / x.length;
}
return [n, p];
}
/**
* Takes a list of values, an array of values, an [n, p] array, or a die, and returns the median. If a die or [n, p] is passed, it is expected that its probabilities sum to 1.
*/
static median(x: number[]): number;
static median(x: [number[], number[]]): number;
static median(x: _d): number;
static median(...x: number[]): number
static median(...argv: any[]): any {
switch (typeof argv[0]) {
case "number":
let a = _d.sort(argv);
let b = (argv.length - 1) / 2;
return (a[Math.floor(b)] + a[Math.ceil(b)]) / 2;
case "object":
if (argv[0] instanceof _d) {
let [n, p] = _d.sort(argv[0].totN, argv[0].totP);
let x = 0.5, i = 0;
for (; x > 0; i++) {
x -= p[i];
}
return n[i - 1];
}
else if (Array.isArray(argv[0])) {
if (Array.isArray(argv[0][0])) {
let [n, p] = _d.sort(argv[0][0], argv[0][1]);
let x = 0.5, i = 0;
for (; x > 0; i++) {
x -= p[i];
}
return n[i - 1];
}
else {
let a = _d.sort(argv[0]);
let b = (argv[0].length - 1) / 2;
return (a[Math.floor(b)] + a[Math.ceil(b)]) / 2;
}
}
throw new Error("Invalid argument vector to median; must be a list of numbers, Array<number>, [n, p] array, or _d.");
default:
throw new Error("Invalid argument vector to median; must be a list of numbers, Array<number>, [n, p] array, or _d.");
}
}
/**
* Takes a list of values, an array of values, an [n, p] array, or a die, and returns the mean. If a die or [n, p] is passed, it is expected that its probabilities sum to 1.
*/
static mean(x: number[]): number;
static mean(x: [number[], number[]]): number;
static mean(x: _d): number;
static mean(...x: number[]): number;
static mean(...argv: any): any {
switch (typeof argv[0]) {
case "number":
let sum = argv.reduce((x: number, y: number) => x + y);
return sum / argv.length;
case "object":
if (argv[0] instanceof _d) {
let sum = 0;
for (let i in argv[0].totN) {
sum += argv[0].totN[i] / argv[0].totP[i];
}
return sum;
}
else if (Array.isArray(argv[0])) {
if (Array.isArray(argv[0][0])) {
let sum = 0;
for (let i in argv[0][0]) {
sum += argv[0][0][i] / argv[0][1][i];
}
return sum;
}
else {
let sum = argv[0].reduce((x, y) => x + y);
return sum / argv[0].length;
}
}
throw new Error("Invalid argument vector to mean; must be a list of numbers, Array<number>, [n, p] array, or _d.");
default:
throw new Error("Invalid argument vector to mean; must be a list of numbers, Array<number>, [n, p] array, or _d.");
}
}
/**
* The array of number arrays.
*/
nums: number[][] = [[]];
/**
* The array of probability arrays.
*/
prob: number[][] = [[]];
/**
* The total amout of dice in the object. Remember to update this when adding a die.
*/
length = 0;
/**
* The entire object's number array.
*/
totN: number[] = [];
/**
* The entire object's probability array.
*/
totP: number[] = [];
[Symbol.iterator]: any;
constructor(die: number, amt?: number);
constructor(die: number[], amt?: number);
constructor(die: _d, amt?: number);
constructor();
constructor(die?: any, amt: number = 1) {
if (typeof die !== "undefined") {
if (amt < 1 || !Number.isSafeInteger(amt)) {
throw new Error("Second argument to constructor must be a positive integer or undefined.");
} else {
if (typeof die === "number") {
let n = [...Array(die).keys()].map(n1 => n1 + 1), p = [...Array(die)].map(() => 1/die);
for (let i = 0; i < amt; i++) {
this.nums[i] = n;
this.prob[i] = p;
}
this.length = amt;
this.totN = [...Array(amt * (die - 1) + 1).keys()].map(x => x + amt);
this.totP = this.prob.reduce((x, y) => _d.convolve(x, y));
}
else if (Array.isArray(die)) {
let [n, p] = _d.arrayToNP(die);
for (let i = 0; i < amt; i++) {
this.nums[i] = n;
this.prob[i] = p;
}
length = amt;
this.totN = this.nums.reduce((x, y) => _d.convolve(x, y));
this.totP = this.prob.reduce((x, y) => _d.convolve(x, y));
}
else if (die instanceof _d) {
let n = die.totN, p = die.totP;
this.length = amt;
while (amt--) {
this.nums[amt] = n;
this.prob[amt] = p;
}
this.totN = this.nums.reduce((x, y) => _d.convolve(x, y));
this.totP = this.prob.reduce((x, y) => _d.convolve(x, y));
}
else {
throw new Error("First argument to constructor must be a number, Array<number>, _d, or undefined.");
}
}
}
this[Symbol.iterator] = function* () {
let a: _d;
for (let i = 0; i < this.length; i++) {
a = new _d;
a.nums = [this.nums[i]];
a.prob = [this.prob[i]];
a.length = 1;
a.totN = this.nums[i];
a.totP = this.prob[i];
yield a;
}
}
}
[Symbol.toPrimitive]() {
return this.roll();
}
/**
* Rolls the nth die in the object, where n is the first argument. If no first argument is specified, rolls the entire object.
*/
roll(n?: number): number {
// TODO: Reduce from Θ(n) to Θ(log n).
let rand = Math.random(), i = 0;
if (typeof n === "undefined") {
for (; i < this.totP.length; i++) {
rand -= this.totP[i];
if (rand < 0) {
return this.totN[i];
}
}
}
else {
for (; i < this.prob[n].length; i++) {
rand -= this.prob[n][i];
if (rand < 0) {
return this.nums[n][i];
}
}
}
throw new Error("Tried to roll a value outside the bounds of the die. This means that the probabilities add up to less than 1. Use the _d.prototype.fixProbs() method in this case.");
}
/**
* If totP does not sum to 1, fixes totP.
*/
fixProbs(): void {
let sum = this.totP.reduce((x, y) => x + y);
if (sum === 1) {
return;
}
// Divides each element by the sum, so that they will now (hopefully) sum to 1.
this.totP.map((x) => x / sum);
sum = this.totP.reduce((x, y) => x + y);
if (sum === 1) {
return;
}
// If they *still* don't sum to 1, force them to.
if (sum > 1) {
let epsilon = 2.220446049250313e-16;
do {
for (let i = 0; sum !== 1 && i < this.totP.length; i--) {
if (this.totP[i] < epsilon) {
continue;
}
this.totP[i] -= epsilon;
sum -= epsilon;
}
} while (sum !== 1);
sum = this.totP.reduce((x, y) => x + y);
}
if (sum < 1) {
let epsilon = 1.1102230246251565e-16;
do {
for (let i = 0; sum !== 1 && i < this.totP.length; i--) {
// So that we don't give the graph a tail
if (this.totP[i] < 2.220446049250313e-16) {
continue;
}
this.totP[i] += epsilon;
sum += epsilon;
}
} while (sum !== 1);
}
}
/**
* Returns a clone of the die.
*/
clone(): _d {
let a = new _d;
a.nums = this.nums;
a.prob = this.prob;
a.length = this.length;
a.totN = this.totN;
a.totP = this.totP;
return a;
}
/**
* Pushes a list of _ds or [n, p] arrays onto the die.
*/
push(...argv: [number[], number[]][]): void;
push(...argv: _d[]): void;
push(...argv: any[]): void {
for (let i = 0; i < argv.length; i++) {
if (Array.isArray(argv[i])) {
if (Array.isArray(argv[i][0])) {
this.nums.push(argv[i][0]);
this.prob.push(argv[i][1]);
this.length++;
this.totN = _d.convolve(this.totN, argv[i][0]);
this.totP = _d.convolve(this.totP, argv[i][1]);
continue;
}
throw new Error("Every argument to push must either be an [n, p] array or _d.");
}
else if (argv[i] instanceof _d) {
this.nums.push(...argv[i].nums);
this.prob.push(...argv[i].prob);
this.length += argv[i].length;
this.totN = _d.convolve(this.totN, argv[i].totN);
this.totP = _d.convolve(this.totP, argv[i].totP);
continue;
}
throw new Error("Every argument to push must either be an [n, p] array or _d.");
}
}
}
var Die = _d;