@tktb-tess/util-fns 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,4 +18,3 @@ export declare const getRandPrimeByRange: (min: bigint, max: bigint) => bigint;
18
18
  * @returns
19
19
  */
20
20
  export declare const getRandPrimeByBitLength: (bitLength: number, fixed?: boolean) => bigint;
21
- //# sourceMappingURL=baillie-psw.d.ts.map
@@ -0,0 +1,45 @@
1
+ import { isEqArray, sleep, lazify, parseCSV, getHash } from './util';
2
+ import { getRndInt, residue, toBigInt, getRandBIByBitLength, getRandBIByRange, modPow, exEuclidean, factorial, rot32, rot32BI, jacobiSymbol, isSquare } from './math';
3
+ import { default as PCGMinimal } from './pcg-minimal';
4
+ import { bailliePSW, getRandPrimeByBitLength, getRandPrimeByRange } from './baillie-psw';
5
+ import { default as Fraction } from './fraction';
6
+ import { base64ToU8Arr, base64UrlToU8Arr, u8ArrToBase64, u8ArrToBase64Url, BItoU8Arr, u8ArrToBI } from './u8arr-ext';
7
+ declare const Util: {
8
+ isEqArray: <T>(arr1: T[], arr2: T[]) => boolean;
9
+ sleep: (delay: number) => Promise<void>;
10
+ lazify: <ArgT extends unknown[], RetT>(func: (...args: ArgT) => RetT) => (...args: ArgT) => () => RetT;
11
+ parseCSV: (csv: string) => string[][];
12
+ getHash: (str: string, algorithm: AlgorithmIdentifier) => Promise<Uint8Array<ArrayBuffer>>;
13
+ };
14
+ declare const AdMath: {
15
+ getRndInt: (min: number, max: number) => number;
16
+ residue: (n: bigint, mod: bigint) => bigint;
17
+ toBigInt: (...nums: number[]) => bigint[];
18
+ getRandBIByBitLength: (length: number, fixed?: boolean) => bigint;
19
+ getRandBIByRange: (min: bigint, max: bigint) => bigint;
20
+ modPow: (base: bigint, power: bigint, mod: bigint) => bigint;
21
+ exEuclidean: (a: bigint, b: bigint) => {
22
+ x: bigint;
23
+ y: bigint;
24
+ gcd: bigint;
25
+ };
26
+ factorial: (n: bigint) => bigint;
27
+ rot32: (value: number, rot: number) => number;
28
+ rot32BI: (value: bigint, rot: bigint) => bigint;
29
+ jacobiSymbol: (a: bigint, n: bigint) => bigint;
30
+ isSquare: (n: bigint) => boolean;
31
+ };
32
+ declare const Prime: {
33
+ bailliePSW: (n: bigint) => boolean;
34
+ getRandPrimeByBitLength: (bitLength: number, fixed?: boolean) => bigint;
35
+ getRandPrimeByRange: (min: bigint, max: bigint) => bigint;
36
+ };
37
+ declare const U8Ext: {
38
+ base64ToU8Arr: (base64: string) => Uint8Array<ArrayBuffer>;
39
+ base64UrlToU8Arr: (base64Url: string) => Uint8Array<ArrayBuffer>;
40
+ u8ArrToBase64: (u8Arr: Uint8Array) => string;
41
+ u8ArrToBase64Url: (u8Arr: Uint8Array) => string;
42
+ BItoU8Arr: (n: bigint) => Uint8Array<ArrayBuffer>;
43
+ u8ArrToBI: (buf: Uint8Array) => bigint;
44
+ };
45
+ export { isEqArray, sleep, lazify, parseCSV, getHash, getRndInt, residue, toBigInt, getRandBIByBitLength, getRandBIByRange, modPow, exEuclidean, factorial, rot32, rot32BI, jacobiSymbol, isSquare, bailliePSW, getRandPrimeByBitLength, getRandPrimeByRange, PCGMinimal, Fraction, Util, AdMath, Prime, base64ToU8Arr, base64UrlToU8Arr, u8ArrToBase64, u8ArrToBase64Url, BItoU8Arr, u8ArrToBI, U8Ext, };
package/dist/bundle.js ADDED
@@ -0,0 +1,494 @@
1
+ const R = new TextEncoder(), x = (n, t) => {
2
+ if (n.length !== t.length) return !1;
3
+ for (let r = 0; r < n.length; r++)
4
+ if (n[r] !== t[r]) return !1;
5
+ return !0;
6
+ }, _ = (n) => new Promise((t) => {
7
+ setTimeout(() => {
8
+ t();
9
+ }, n);
10
+ }), M = (n) => (...t) => () => n(...t), T = (n) => {
11
+ const t = [];
12
+ let r = [], e = "", s = !1;
13
+ for (let i = 0; i < n.length; i++) {
14
+ const o = n[i];
15
+ o === '"' && (i === 0 || n[i - 1] !== "\\") ? s = !s : o === "," && !s ? (r.push(e.trim()), e = "") : o === `
16
+ ` && !s ? (r.push(e.trim()), t.push(r), r = [], e = "") : e += o;
17
+ }
18
+ return r.push(e.trim()), t.push(r), t;
19
+ }, L = async (n, t) => {
20
+ const r = R.encode(n), e = await crypto.subtle.digest(t, r);
21
+ return new Uint8Array(e);
22
+ }, I = typeof process < "u" && typeof process.versions.node < "u", V = (n, t) => Math.floor(Math.random() * (t - n) + n), h = (n, t) => {
23
+ const r = n % t;
24
+ return r < 0n ? r + t : r;
25
+ }, j = (...n) => n.map((t) => BigInt(t)), w = (n, t = !1) => {
26
+ if (!Number.isFinite(n)) throw Error("`length` is not a valid number");
27
+ if (n <= 0) throw Error("`length` must be positive");
28
+ const r = Math.ceil(n / 8), e = crypto.getRandomValues(new Uint8Array(r));
29
+ let s = Array.from(e, (i) => i.toString(2).padStart(8, "0")).join("").slice(0, n);
30
+ return t && (s = s.replace(/^./, "1")), BigInt("0b" + s);
31
+ }, S = (n, t) => {
32
+ if (n >= t) throw Error("rangeError");
33
+ const r = t - n, e = r.toString(2).length, s = (() => {
34
+ for (let o = 0; o < 1e5; o++) {
35
+ const c = w(e);
36
+ if (c >= a(2n, BigInt(e), r))
37
+ return c % r;
38
+ }
39
+ throw Error("Failed to generate a random bigint");
40
+ })();
41
+ return n + s;
42
+ }, a = (n, t, r) => {
43
+ if (r < 1n) throw Error("`mod` must be positive");
44
+ if (t < 0n) throw Error("`power` must not be negative");
45
+ for (; n < 0n; ) n += r;
46
+ if (r === 1n) return 0n;
47
+ if (n % r === 1n || n % r === 0n) return n;
48
+ if (n === r - 1n) return t & 1n ? r - 1n : 1n;
49
+ let e = 1n;
50
+ for (; t > 0n; )
51
+ t & 1n && (e = e * n % r), n = n * n % r, t >>= 1n;
52
+ return e;
53
+ }, A = (n, t) => {
54
+ if (n === 0n && t === 0n) return { x: 0n, y: 0n, gcd: 0n };
55
+ if (n === 0n)
56
+ return t > 0n ? { x: 0n, y: -1n, gcd: t } : { x: 0n, y: 1n, gcd: -t };
57
+ if (t === 0n)
58
+ return n > 0n ? { x: 1n, y: 0n, gcd: n } : { x: -1n, y: 0n, gcd: -n };
59
+ let [r, e, s] = [1n, 0n, n], [i, o, c] = [0n, -1n, t];
60
+ for (; ; ) {
61
+ const u = s / c, f = s - u * c;
62
+ if (f === 0n) break;
63
+ [r, i] = [i, r - u * i], [e, o] = [o, e - u * o], [s, c] = [c, f];
64
+ }
65
+ return c < 0n && (i *= -1n, o *= -1n, c *= -1n), { x: i, y: o, gcd: c };
66
+ }, m = (n, t) => {
67
+ if (n >= t) return 1n;
68
+ const r = BigInt((t - 2n).toString(2).length), e = (t - n) / 2n;
69
+ if (r * e < 63n) {
70
+ let c = n;
71
+ for (let u = n + 2n; u < t; u += 2n)
72
+ c *= u;
73
+ return c;
74
+ }
75
+ const s = n + e | 1n, i = m(n, s), o = m(s, t);
76
+ return i * o;
77
+ }, C = (n) => {
78
+ let t = 3n, r = 1n, e = 1n;
79
+ const s = BigInt(n.toString(2).length) - 1n;
80
+ for (let i = s - 1n; i > -1n; --i) {
81
+ const o = (n >> i) + 1n | 1n;
82
+ e *= m(t, o), t = o, r *= e;
83
+ }
84
+ return r;
85
+ }, q = (n) => {
86
+ if (n < 0n) throw Error("'n' must be non-negative");
87
+ if (n === 0n) return 1n;
88
+ const t = n - BigInt(n.toString(2).match(/1/g)?.length ?? 0);
89
+ return C(n) << t;
90
+ }, D = (n, t) => BigInt.asUintN(32, n >> (t & 31n) | n << (-t & 31n)), E = (n, t) => (n >>> (t & 31) | n << (-t & 31)) >>> 0, p = (n, t) => {
91
+ if (t < 1n || t % 2n === 0n)
92
+ throw Error("`n` is invalid");
93
+ for (; n < 0n; )
94
+ n += t;
95
+ n %= t;
96
+ let r = 1n;
97
+ for (; n !== 0n; ) {
98
+ for (; n % 2n === 0n; ) {
99
+ n /= 2n;
100
+ const e = t % 8n;
101
+ (e === 3n || e === 5n) && (r *= -1n);
102
+ }
103
+ [n, t] = [t, n], n % 4n === 3n && t % 4n === 3n && (r *= -1n), n %= t;
104
+ }
105
+ return t === 1n ? r : 0n;
106
+ }, N = (n) => {
107
+ if (n < 0n) return !1;
108
+ if (n === 0n) return !0;
109
+ let t = 1n, r = n;
110
+ for (; t + 1n < r; ) {
111
+ const e = (t + r) / 2n;
112
+ e ** 2n < n ? t = e : r = e;
113
+ }
114
+ return n === t ** 2n || n === (t + 1n) ** 2n;
115
+ }, O = Symbol(), b = [0x853c49e6748fea9bn, 0xda3e39cb94b95bdbn], $ = 0x5851f42d4c957f2dn;
116
+ class F {
117
+ /**
118
+ * length = 2, `[state, increment]`
119
+ */
120
+ #t = new BigUint64Array(2);
121
+ [O];
122
+ /** シード値の配列を返す */
123
+ static getSeed() {
124
+ return crypto.getRandomValues(new BigUint64Array(2));
125
+ }
126
+ /**
127
+ * @param seeds 64bit整数の配列 (長さ2以上), 省略した場合, 常に同じ値によって初期化される
128
+ */
129
+ constructor(t) {
130
+ t ? (this.#t[1] = t[1] << 1n | 1n, this.step(), this.#t[0] = t[0], this.step()) : (this.#t[0] = b[0], this.#t[1] = b[1]);
131
+ }
132
+ /** 内部状態を1サイクル進める */
133
+ step() {
134
+ this.#t[0] = this.#t[0] * $ + this.#t[1];
135
+ }
136
+ /** 32bit 乱数を返す (内部状態は変わらない) */
137
+ get value() {
138
+ const t = this.#t[0];
139
+ if (!t) throw Error("empty state");
140
+ const r = t >> 59n, e = BigInt.asUintN(32, (t ^ t >> 18n) >> 27n);
141
+ return E(Number(e), Number(r));
142
+ }
143
+ /** 内部状態を1進め、乱数を返す \
144
+ * 普通はこれを使う
145
+ */
146
+ getRand() {
147
+ return this.step(), this.value;
148
+ }
149
+ /** `bound` 未満の乱数を返す */
150
+ getBoundedRand(t) {
151
+ if (t > 4294967296) throw Error("`bound` exceeded limit (2^32)");
152
+ const e = 4294967296 % t;
153
+ for (; ; ) {
154
+ const s = this.getRand();
155
+ if (s >= e) return s % t;
156
+ }
157
+ }
158
+ /**
159
+ * step の値だけ乱数を生成する
160
+ * @param step
161
+ * @param bound
162
+ */
163
+ *genRands(t, r) {
164
+ for (let e = 0; e < t; e++)
165
+ yield typeof r == "number" ? this.getBoundedRand(r) : this.getRand();
166
+ }
167
+ }
168
+ const v = (n) => {
169
+ if (n <= 1n) return !1;
170
+ if (n % 2n === 0n) return n === 2n;
171
+ let t = n - 1n, r = 0n;
172
+ for (; t % 2n === 0n; )
173
+ t >>= 1n, r += 1n;
174
+ const [e, s] = [t, r];
175
+ let o = a(2n, e, n);
176
+ if (o === 1n) return !0;
177
+ for (let c = 0n; c < s; c++) {
178
+ if (o === n - 1n) return !0;
179
+ o = o * o % n;
180
+ }
181
+ return !1;
182
+ }, J = (n) => {
183
+ let t = 5n, r = p(t, n);
184
+ for (; r > 0n; ) {
185
+ if (t = t > 0n ? t + 2n : t - 2n, t *= -1n, t === -15n && N(n))
186
+ return [0n, 0n];
187
+ r = p(t, n);
188
+ }
189
+ return [t, r];
190
+ }, g = (n, t) => (n & 1n) === 1n ? h(n + t >> 1n, t) : h(n >> 1n, t), k = (n, t, r, e) => {
191
+ let s = 1n, i = r;
192
+ const o = n.toString(2).slice(1);
193
+ for (const c of o)
194
+ [s, i] = [h(s * i, t), g(i * i + e * s * s, t)], c === "1" && ([s, i] = [g(r * s + i, t), g(e * s + r * i, t)]);
195
+ return [s, i];
196
+ }, z = (n, t, r, e) => {
197
+ if (n % 2n !== 1n) throw Error("`n` must be odd");
198
+ let s = n + 1n, i = 0n;
199
+ for (; s % 2n === 0n; )
200
+ s >>= 1n, i += 1n;
201
+ const [o, c] = k(s, n, r, t);
202
+ let u = c;
203
+ if (o === 0n) return !0;
204
+ e = a(e, s, n);
205
+ for (let f = 0n; f < i; f++) {
206
+ if (u === 0n) return !0;
207
+ u = h(u * u - 2n * e, n), e = a(e, 2n, n);
208
+ }
209
+ return !1;
210
+ }, y = (n) => {
211
+ if (n <= 1n) return !1;
212
+ if (n % 2n === 0n) return n === 2n;
213
+ const t = [
214
+ 2n,
215
+ 3n,
216
+ 5n,
217
+ 7n,
218
+ 11n,
219
+ 13n,
220
+ 17n,
221
+ 19n,
222
+ 23n,
223
+ 29n,
224
+ 31n,
225
+ 37n,
226
+ 41n,
227
+ 43n,
228
+ 47n,
229
+ 53n,
230
+ 59n,
231
+ 61n,
232
+ 67n,
233
+ 71n,
234
+ 73n,
235
+ 79n,
236
+ 83n,
237
+ 89n,
238
+ 97n,
239
+ 101n
240
+ ];
241
+ for (const i of t)
242
+ if (n % i === 0n)
243
+ return n === i;
244
+ if (!v(n))
245
+ return !1;
246
+ const [r, e] = J(n);
247
+ if (e === 0n) return !1;
248
+ const s = (1n - r) / 4n;
249
+ return z(n, r, 1n, s);
250
+ }, G = (n, t) => {
251
+ if (t < 2n)
252
+ throw Error("noPrimesFound");
253
+ for (let e = 0; e < 1e5; e++) {
254
+ const s = S(n, t);
255
+ if (y(s)) return s;
256
+ }
257
+ throw Error("noPrimesFound");
258
+ }, H = (n, t = !1) => {
259
+ if (n < 2)
260
+ throw Error("noPrimesFound");
261
+ for (let e = 0; e < 1e5; e++) {
262
+ const s = w(n, t);
263
+ if (y(s)) return s;
264
+ }
265
+ throw Error("noPrimesFound");
266
+ }, W = Symbol();
267
+ class l {
268
+ #t;
269
+ #n;
270
+ [W];
271
+ /**
272
+ *
273
+ * @param numerator
274
+ * @param denominator
275
+ */
276
+ constructor(t, r) {
277
+ r === 0n ? (this.#n = 0n, this.#t = t === 0n ? 0n : t > 0n ? 1n : -1n) : r > 0n ? (this.#t = t, this.#n = r) : (this.#t = -t, this.#n = -r), this.#r();
278
+ }
279
+ /**
280
+ * generates fraction from `number` decimal using continued fraction
281
+ * @param value 値
282
+ * @param denominatorDigits 分母の桁数 (十進)
283
+ */
284
+ static fromDecimal(t, r = 20) {
285
+ if (Number.isNaN(t))
286
+ return new l(0n, 0n);
287
+ if (Math.abs(t) === 1 / 0)
288
+ return new l(t > 0 ? 1n : -1n, 0n);
289
+ const e = t < 0;
290
+ e && (t *= -1);
291
+ const s = BigInt(Math.floor(t)), i = t - Number(s);
292
+ if (i === 0)
293
+ return new l(e ? -s : s, 1n);
294
+ t = 1 / i;
295
+ let [o, c] = [1n, s], [u, f] = [0n, 1n];
296
+ for (; `${f}`.length < r + 1; ) {
297
+ const d = BigInt(Math.floor(t)), B = t - Number(d);
298
+ if ([o, c] = [c, d * c + o], [u, f] = [f, d * f + u], B === 0)
299
+ return new l(e ? -c : c, f);
300
+ t = 1 / B;
301
+ }
302
+ return new l(e ? -o : o, u);
303
+ }
304
+ /**
305
+ * reduction
306
+ */
307
+ #r() {
308
+ const { gcd: t } = A(this.#t, this.#n);
309
+ t !== 0n && (this.#t /= t, this.#n /= t);
310
+ }
311
+ /**
312
+ * returns a fraction multiplied by -1
313
+ * @returns
314
+ */
315
+ minus() {
316
+ return new l(-this.#t, this.#n);
317
+ }
318
+ /**
319
+ * returns inverse of this fraction
320
+ * @returns
321
+ */
322
+ inverse() {
323
+ return new l(this.#n, this.#t);
324
+ }
325
+ /**
326
+ * returns `this + right`
327
+ * @param right
328
+ * @returns
329
+ */
330
+ add(t) {
331
+ const r = this.#n * t.#n, e = this.#t * t.#n + t.#t * this.#n;
332
+ return new l(e, r);
333
+ }
334
+ /**
335
+ * returns `this - right`
336
+ * @param right
337
+ * @returns
338
+ */
339
+ substr(t) {
340
+ return new l(this.#t, this.#n).add(t.minus());
341
+ }
342
+ /**
343
+ * returns `this * right`
344
+ * @param right
345
+ * @returns
346
+ */
347
+ multiply(t) {
348
+ const r = this.#n * t.#n, e = this.#t * t.#t;
349
+ return new l(e, r);
350
+ }
351
+ /**
352
+ * returns `this / right`
353
+ * @param right
354
+ * @returns
355
+ */
356
+ divide(t) {
357
+ return new l(this.#t, this.#n).multiply(
358
+ t.inverse()
359
+ );
360
+ }
361
+ /**
362
+ * mediant
363
+ * @param right
364
+ * @returns
365
+ */
366
+ mediant(t) {
367
+ const r = this.#n + t.#n, e = this.#t + t.#t;
368
+ return new l(e, r);
369
+ }
370
+ /**
371
+ * `number` 型の小数に変換
372
+ * @returns
373
+ */
374
+ toDecimal() {
375
+ return Number(this.#t) / Number(this.#n);
376
+ }
377
+ /**
378
+ * 分数の文字列に変換
379
+ * @returns
380
+ */
381
+ toString() {
382
+ return this.#t === 0n && this.#n === 0n ? "NaN" : this.#t === 0n ? "0" : this.#n === 0n ? this.#t < 0n ? "-Infinity" : "Infinity" : this.#n === 1n ? `${this.#t}` : `${this.#t}/${this.#n}`;
383
+ }
384
+ valueOf() {
385
+ return this.toDecimal();
386
+ }
387
+ toJSON() {
388
+ return {
389
+ type: "Fraction",
390
+ numerator: "0x" + this.#t.toString(16),
391
+ denominator: "0x" + this.#n.toString(16)
392
+ };
393
+ }
394
+ static parse(t) {
395
+ const r = JSON.parse(t), { type: e, numerator: s, denominator: i } = r;
396
+ if (e !== "Fraction") throw Error("cannot parse");
397
+ if (typeof s == "string" && typeof i == "string") {
398
+ const o = BigInt(s), c = BigInt(i);
399
+ return new l(o, c);
400
+ } else
401
+ throw Error("cannot parse");
402
+ }
403
+ }
404
+ const K = (n) => {
405
+ let t = n.toString(16);
406
+ t.length & 1 && (t = "0" + t);
407
+ const r = [...t.match(/.{2}/g) ?? []];
408
+ return Uint8Array.from(r, (e) => Number.parseInt(e, 16));
409
+ }, X = (n) => {
410
+ const t = Array.from(n, (r) => r.toString(16).padStart(2, "0")).join("") || "00";
411
+ return BigInt("0x" + t);
412
+ }, U = (n) => {
413
+ if (I)
414
+ return Buffer.copyBytesFrom(n).toString("base64");
415
+ {
416
+ const t = Array.from(n, (r) => String.fromCharCode(r)).join("");
417
+ return btoa(t);
418
+ }
419
+ }, P = (n) => {
420
+ if (I) {
421
+ const t = Buffer.from(n, "base64");
422
+ return new Uint8Array(t.buffer, t.byteOffset, t.byteLength);
423
+ } else
424
+ return Uint8Array.from(atob(n), (t) => t.charCodeAt(0));
425
+ }, Y = (n) => U(n).replaceAll(/\+/g, "-").replaceAll(/\//g, "_").replaceAll(/=*$/g, ""), Z = (n) => {
426
+ const t = n.length & 3;
427
+ t > 0 && (n = n + "=".repeat(4 - t));
428
+ const r = n.replaceAll(/-/g, "+").replaceAll(/_/g, "/");
429
+ return P(r);
430
+ }, Q = {
431
+ isEqArray: x,
432
+ sleep: _,
433
+ lazify: M,
434
+ parseCSV: T,
435
+ getHash: L
436
+ }, tt = {
437
+ getRndInt: V,
438
+ residue: h,
439
+ toBigInt: j,
440
+ getRandBIByBitLength: w,
441
+ getRandBIByRange: S,
442
+ modPow: a,
443
+ exEuclidean: A,
444
+ factorial: q,
445
+ rot32: E,
446
+ rot32BI: D,
447
+ jacobiSymbol: p,
448
+ isSquare: N
449
+ }, nt = {
450
+ bailliePSW: y,
451
+ getRandPrimeByBitLength: H,
452
+ getRandPrimeByRange: G
453
+ }, rt = {
454
+ base64ToU8Arr: P,
455
+ base64UrlToU8Arr: Z,
456
+ u8ArrToBase64: U,
457
+ u8ArrToBase64Url: Y,
458
+ BItoU8Arr: K,
459
+ u8ArrToBI: X
460
+ };
461
+ export {
462
+ tt as AdMath,
463
+ K as BItoU8Arr,
464
+ l as Fraction,
465
+ F as PCGMinimal,
466
+ nt as Prime,
467
+ rt as U8Ext,
468
+ Q as Util,
469
+ y as bailliePSW,
470
+ P as base64ToU8Arr,
471
+ Z as base64UrlToU8Arr,
472
+ A as exEuclidean,
473
+ q as factorial,
474
+ L as getHash,
475
+ w as getRandBIByBitLength,
476
+ S as getRandBIByRange,
477
+ H as getRandPrimeByBitLength,
478
+ G as getRandPrimeByRange,
479
+ V as getRndInt,
480
+ x as isEqArray,
481
+ N as isSquare,
482
+ p as jacobiSymbol,
483
+ M as lazify,
484
+ a as modPow,
485
+ T as parseCSV,
486
+ h as residue,
487
+ E as rot32,
488
+ D as rot32BI,
489
+ _ as sleep,
490
+ j as toBigInt,
491
+ X as u8ArrToBI,
492
+ U as u8ArrToBase64,
493
+ Y as u8ArrToBase64Url
494
+ };
@@ -0,0 +1,2 @@
1
+ (function(o,g){typeof exports=="object"&&typeof module<"u"?g(exports):typeof define=="function"&&define.amd?define(["exports"],g):(o=typeof globalThis<"u"?globalThis:o||self,g(o["util-fns"]={}))})(this,(function(o){"use strict";const g=new TextEncoder,R=(t,n)=>{if(t.length!==n.length)return!1;for(let e=0;e<t.length;e++)if(t[e]!==n[e])return!1;return!0},N=t=>new Promise(n=>{setTimeout(()=>{n()},t)}),T=t=>(...n)=>()=>t(...n),M=t=>{const n=[];let e=[],r="",i=!1;for(let s=0;s<t.length;s++){const c=t[s];c==='"'&&(s===0||t[s-1]!=="\\")?i=!i:c===","&&!i?(e.push(r.trim()),r=""):c===`
2
+ `&&!i?(e.push(r.trim()),n.push(e),e=[],r=""):r+=c}return e.push(r.trim()),n.push(e),n},_=async(t,n)=>{const e=g.encode(t),r=await crypto.subtle.digest(n,e);return new Uint8Array(r)},L=typeof process<"u"&&typeof process.versions.node<"u",j=(t,n)=>Math.floor(Math.random()*(n-t)+t),a=(t,n)=>{const e=t%n;return e<0n?e+n:e},C=(...t)=>t.map(n=>BigInt(n)),m=(t,n=!1)=>{if(!Number.isFinite(t))throw Error("`length` is not a valid number");if(t<=0)throw Error("`length` must be positive");const e=Math.ceil(t/8),r=crypto.getRandomValues(new Uint8Array(e));let i=Array.from(r,s=>s.toString(2).padStart(8,"0")).join("").slice(0,t);return n&&(i=i.replace(/^./,"1")),BigInt("0b"+i)},B=(t,n)=>{if(t>=n)throw Error("rangeError");const e=n-t,r=e.toString(2).length,i=(()=>{for(let c=0;c<1e5;c++){const u=m(r);if(u>=d(2n,BigInt(r),e))return u%e}throw Error("Failed to generate a random bigint")})();return t+i},d=(t,n,e)=>{if(e<1n)throw Error("`mod` must be positive");if(n<0n)throw Error("`power` must not be negative");for(;t<0n;)t+=e;if(e===1n)return 0n;if(t%e===1n||t%e===0n)return t;if(t===e-1n)return n&1n?e-1n:1n;let r=1n;for(;n>0n;)n&1n&&(r=r*t%e),t=t*t%e,n>>=1n;return r},b=(t,n)=>{if(t===0n&&n===0n)return{x:0n,y:0n,gcd:0n};if(t===0n)return n>0n?{x:0n,y:-1n,gcd:n}:{x:0n,y:1n,gcd:-n};if(n===0n)return t>0n?{x:1n,y:0n,gcd:t}:{x:-1n,y:0n,gcd:-t};let[e,r,i]=[1n,0n,t],[s,c,u]=[0n,-1n,n];for(;;){const l=i/u,h=i-l*u;if(h===0n)break;[e,s]=[s,e-l*s],[r,c]=[c,r-l*c],[i,u]=[u,h]}return u<0n&&(s*=-1n,c*=-1n,u*=-1n),{x:s,y:c,gcd:u}},I=(t,n)=>{if(t>=n)return 1n;const e=BigInt((n-2n).toString(2).length),r=(n-t)/2n;if(e*r<63n){let u=t;for(let l=t+2n;l<n;l+=2n)u*=l;return u}const i=t+r|1n,s=I(t,i),c=I(i,n);return s*c},W=t=>{let n=3n,e=1n,r=1n;const i=BigInt(t.toString(2).length)-1n;for(let s=i-1n;s>-1n;--s){const c=(t>>s)+1n|1n;r*=I(n,c),n=c,e*=r}return e},V=t=>{if(t<0n)throw Error("'n' must be non-negative");if(t===0n)return 1n;const n=t-BigInt(t.toString(2).match(/1/g)?.length??0);return W(t)<<n},q=(t,n)=>BigInt.asUintN(32,t>>(n&31n)|t<<(-n&31n)),S=(t,n)=>(t>>>(n&31)|t<<(-n&31))>>>0,y=(t,n)=>{if(n<1n||n%2n===0n)throw Error("`n` is invalid");for(;t<0n;)t+=n;t%=n;let e=1n;for(;t!==0n;){for(;t%2n===0n;){t/=2n;const r=n%8n;(r===3n||r===5n)&&(e*=-1n)}[t,n]=[n,t],t%4n===3n&&n%4n===3n&&(e*=-1n),t%=n}return n===1n?e:0n},p=t=>{if(t<0n)return!1;if(t===0n)return!0;let n=1n,e=t;for(;n+1n<e;){const r=(n+e)/2n;r**2n<t?n=r:e=r}return t===n**2n||t===(n+1n)**2n},k=Symbol(),O=[0x853c49e6748fea9bn,0xda3e39cb94b95bdbn],K=0x5851f42d4c957f2dn;class X{#n=new BigUint64Array(2);[k];static getSeed(){return crypto.getRandomValues(new BigUint64Array(2))}constructor(n){n?(this.#n[1]=n[1]<<1n|1n,this.step(),this.#n[0]=n[0],this.step()):(this.#n[0]=O[0],this.#n[1]=O[1])}step(){this.#n[0]=this.#n[0]*K+this.#n[1]}get value(){const n=this.#n[0];if(!n)throw Error("empty state");const e=n>>59n,r=BigInt.asUintN(32,(n^n>>18n)>>27n);return S(Number(r),Number(e))}getRand(){return this.step(),this.value}getBoundedRand(n){if(n>4294967296)throw Error("`bound` exceeded limit (2^32)");const r=4294967296%n;for(;;){const i=this.getRand();if(i>=r)return i%n}}*genRands(n,e){for(let r=0;r<n;r++)yield typeof e=="number"?this.getBoundedRand(e):this.getRand()}}const Y=t=>{if(t<=1n)return!1;if(t%2n===0n)return t===2n;let n=t-1n,e=0n;for(;n%2n===0n;)n>>=1n,e+=1n;const[r,i]=[n,e];let c=d(2n,r,t);if(c===1n)return!0;for(let u=0n;u<i;u++){if(c===t-1n)return!0;c=c*c%t}return!1},Z=t=>{let n=5n,e=y(n,t);for(;e>0n;){if(n=n>0n?n+2n:n-2n,n*=-1n,n===-15n&&p(t))return[0n,0n];e=y(n,t)}return[n,e]},A=(t,n)=>(t&1n)===1n?a(t+n>>1n,n):a(t>>1n,n),F=(t,n,e,r)=>{let i=1n,s=e;const c=t.toString(2).slice(1);for(const u of c)[i,s]=[a(i*s,n),A(s*s+r*i*i,n)],u==="1"&&([i,s]=[A(e*i+s,n),A(r*i+e*s,n)]);return[i,s]},Q=(t,n,e,r)=>{if(t%2n!==1n)throw Error("`n` must be odd");let i=t+1n,s=0n;for(;i%2n===0n;)i>>=1n,s+=1n;const[c,u]=F(i,t,e,n);let l=u;if(c===0n)return!0;r=d(r,i,t);for(let h=0n;h<s;h++){if(l===0n)return!0;l=a(l*l-2n*r,t),r=d(r,2n,t)}return!1},w=t=>{if(t<=1n)return!1;if(t%2n===0n)return t===2n;const n=[2n,3n,5n,7n,11n,13n,17n,19n,23n,29n,31n,37n,41n,43n,47n,53n,59n,61n,67n,71n,73n,79n,83n,89n,97n,101n];for(const s of n)if(t%s===0n)return t===s;if(!Y(t))return!1;const[e,r]=Z(t);if(r===0n)return!1;const i=(1n-e)/4n;return Q(t,e,1n,i)},D=(t,n)=>{if(n<2n)throw Error("noPrimesFound");for(let r=0;r<1e5;r++){const i=B(t,n);if(w(i))return i}throw Error("noPrimesFound")},$=(t,n=!1)=>{if(t<2)throw Error("noPrimesFound");for(let r=0;r<1e5;r++){const i=m(t,n);if(w(i))return i}throw Error("noPrimesFound")},x=Symbol();class f{#n;#t;[x];constructor(n,e){e===0n?(this.#t=0n,this.#n=n===0n?0n:n>0n?1n:-1n):e>0n?(this.#n=n,this.#t=e):(this.#n=-n,this.#t=-e),this.#e()}static fromDecimal(n,e=20){if(Number.isNaN(n))return new f(0n,0n);if(Math.abs(n)===1/0)return new f(n>0?1n:-1n,0n);const r=n<0;r&&(n*=-1);const i=BigInt(Math.floor(n)),s=n-Number(i);if(s===0)return new f(r?-i:i,1n);n=1/s;let[c,u]=[1n,i],[l,h]=[0n,1n];for(;`${h}`.length<e+1;){const P=BigInt(Math.floor(n)),J=n-Number(P);if([c,u]=[u,P*u+c],[l,h]=[h,P*h+l],J===0)return new f(r?-u:u,h);n=1/J}return new f(r?-c:c,l)}#e(){const{gcd:n}=b(this.#n,this.#t);n!==0n&&(this.#n/=n,this.#t/=n)}minus(){return new f(-this.#n,this.#t)}inverse(){return new f(this.#t,this.#n)}add(n){const e=this.#t*n.#t,r=this.#n*n.#t+n.#n*this.#t;return new f(r,e)}substr(n){return new f(this.#n,this.#t).add(n.minus())}multiply(n){const e=this.#t*n.#t,r=this.#n*n.#n;return new f(r,e)}divide(n){return new f(this.#n,this.#t).multiply(n.inverse())}mediant(n){const e=this.#t+n.#t,r=this.#n+n.#n;return new f(r,e)}toDecimal(){return Number(this.#n)/Number(this.#t)}toString(){return this.#n===0n&&this.#t===0n?"NaN":this.#n===0n?"0":this.#t===0n?this.#n<0n?"-Infinity":"Infinity":this.#t===1n?`${this.#n}`:`${this.#n}/${this.#t}`}valueOf(){return this.toDecimal()}toJSON(){return{type:"Fraction",numerator:"0x"+this.#n.toString(16),denominator:"0x"+this.#t.toString(16)}}static parse(n){const e=JSON.parse(n),{type:r,numerator:i,denominator:s}=e;if(r!=="Fraction")throw Error("cannot parse");if(typeof i=="string"&&typeof s=="string"){const c=BigInt(i),u=BigInt(s);return new f(c,u)}else throw Error("cannot parse")}}const v=t=>{let n=t.toString(16);n.length&1&&(n="0"+n);const e=[...n.match(/.{2}/g)??[]];return Uint8Array.from(e,r=>Number.parseInt(r,16))},z=t=>{const n=Array.from(t,e=>e.toString(16).padStart(2,"0")).join("")||"00";return BigInt("0x"+n)},E=t=>{if(L)return Buffer.copyBytesFrom(t).toString("base64");{const n=Array.from(t,e=>String.fromCharCode(e)).join("");return btoa(n)}},U=t=>{if(L){const n=Buffer.from(t,"base64");return new Uint8Array(n.buffer,n.byteOffset,n.byteLength)}else return Uint8Array.from(atob(t),n=>n.charCodeAt(0))},G=t=>E(t).replaceAll(/\+/g,"-").replaceAll(/\//g,"_").replaceAll(/=*$/g,""),H=t=>{const n=t.length&3;n>0&&(t=t+"=".repeat(4-n));const e=t.replaceAll(/-/g,"+").replaceAll(/_/g,"/");return U(e)},nn={isEqArray:R,sleep:N,lazify:T,parseCSV:M,getHash:_},tn={getRndInt:j,residue:a,toBigInt:C,getRandBIByBitLength:m,getRandBIByRange:B,modPow:d,exEuclidean:b,factorial:V,rot32:S,rot32BI:q,jacobiSymbol:y,isSquare:p},en={bailliePSW:w,getRandPrimeByBitLength:$,getRandPrimeByRange:D},rn={base64ToU8Arr:U,base64UrlToU8Arr:H,u8ArrToBase64:E,u8ArrToBase64Url:G,BItoU8Arr:v,u8ArrToBI:z};o.AdMath=tn,o.BItoU8Arr=v,o.Fraction=f,o.PCGMinimal=X,o.Prime=en,o.U8Ext=rn,o.Util=nn,o.bailliePSW=w,o.base64ToU8Arr=U,o.base64UrlToU8Arr=H,o.exEuclidean=b,o.factorial=V,o.getHash=_,o.getRandBIByBitLength=m,o.getRandBIByRange=B,o.getRandPrimeByBitLength=$,o.getRandPrimeByRange=D,o.getRndInt=j,o.isEqArray=R,o.isSquare=p,o.jacobiSymbol=y,o.lazify=T,o.modPow=d,o.parseCSV=M,o.residue=a,o.rot32=S,o.rot32BI=q,o.sleep=N,o.toBigInt=C,o.u8ArrToBI=z,o.u8ArrToBase64=E,o.u8ArrToBase64Url=G,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})}));
@@ -1,4 +1,4 @@
1
- declare const __fraction_brand: unique symbol;
1
+ declare const __frac_brand: unique symbol;
2
2
  export type FractionData = {
3
3
  type: 'Fraction';
4
4
  numerator: string;
@@ -6,48 +6,59 @@ export type FractionData = {
6
6
  };
7
7
  export default class Fraction {
8
8
  #private;
9
- readonly [__fraction_brand]: unknown;
9
+ readonly [__frac_brand]: unknown;
10
+ /**
11
+ *
12
+ * @param numerator
13
+ * @param denominator
14
+ */
10
15
  constructor(numerator: bigint, denominator: bigint);
11
16
  /**
12
- * `number` 型から構成
17
+ * generates fraction from `number` decimal using continued fraction
13
18
  * @param value 値
14
19
  * @param denominatorDigits 分母の桁数 (十進)
15
20
  */
16
21
  static fromDecimal(value: number, denominatorDigits?: number): Fraction;
17
22
  /**
18
- * -1
23
+ * returns a fraction multiplied by -1
19
24
  * @returns
20
25
  */
21
26
  minus(): Fraction;
22
27
  /**
23
- * 逆数
28
+ * returns inverse of this fraction
24
29
  * @returns
25
30
  */
26
31
  inverse(): Fraction;
27
32
  /**
28
- * 加法 (非破壊的)
33
+ * returns `this + right`
29
34
  * @param right
30
35
  * @returns
31
36
  */
32
37
  add(right: Fraction): Fraction;
33
38
  /**
34
- * 減法
39
+ * returns `this - right`
35
40
  * @param right
36
41
  * @returns
37
42
  */
38
43
  substr(right: Fraction): Fraction;
39
44
  /**
40
- * 乗法
45
+ * returns `this * right`
41
46
  * @param right
42
47
  * @returns
43
48
  */
44
49
  multiply(right: Fraction): Fraction;
45
50
  /**
46
- * 除法
51
+ * returns `this / right`
47
52
  * @param right
48
53
  * @returns
49
54
  */
50
55
  divide(right: Fraction): Fraction;
56
+ /**
57
+ * mediant
58
+ * @param right
59
+ * @returns
60
+ */
61
+ mediant(right: Fraction): Fraction;
51
62
  /**
52
63
  * `number` 型の小数に変換
53
64
  * @returns
@@ -63,4 +74,3 @@ export default class Fraction {
63
74
  static parse(text: string): Fraction;
64
75
  }
65
76
  export {};
66
- //# sourceMappingURL=fraction.d.ts.map
package/dist/math.d.ts CHANGED
@@ -87,4 +87,3 @@ export declare const jacobiSymbol: (a: bigint, n: bigint) => bigint;
87
87
  * @returns
88
88
  */
89
89
  export declare const isSquare: (n: bigint) => boolean;
90
- //# sourceMappingURL=math.d.ts.map
@@ -29,4 +29,3 @@ export default class PCGMinimal {
29
29
  genRands(step: number, bound?: number): Generator<number, void, unknown>;
30
30
  }
31
31
  export {};
32
- //# sourceMappingURL=pcg-minimal.d.ts.map
@@ -0,0 +1,7 @@
1
+ declare const BItoU8Arr: (n: bigint) => Uint8Array<ArrayBuffer>;
2
+ declare const u8ArrToBI: (buf: Uint8Array) => bigint;
3
+ declare const u8ArrToBase64: (u8Arr: Uint8Array) => string;
4
+ declare const base64ToU8Arr: (base64: string) => Uint8Array<ArrayBuffer>;
5
+ declare const u8ArrToBase64Url: (u8Arr: Uint8Array) => string;
6
+ declare const base64UrlToU8Arr: (base64Url: string) => Uint8Array<ArrayBuffer>;
7
+ export { BItoU8Arr, u8ArrToBI, u8ArrToBase64, base64ToU8Arr, u8ArrToBase64Url, base64UrlToU8Arr, };