deepbox 0.1.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.
- package/LICENSE +21 -0
- package/README.md +344 -0
- package/dist/CSRMatrix-CwGwQRea.d.cts +219 -0
- package/dist/CSRMatrix-KzNt6QpS.d.ts +219 -0
- package/dist/Tensor-BQLk1ltW.d.cts +147 -0
- package/dist/Tensor-g8mUClel.d.ts +147 -0
- package/dist/chunk-4S73VUBD.js +677 -0
- package/dist/chunk-4S73VUBD.js.map +1 -0
- package/dist/chunk-5R4S63PF.js +2925 -0
- package/dist/chunk-5R4S63PF.js.map +1 -0
- package/dist/chunk-6AE5FKKQ.cjs +9264 -0
- package/dist/chunk-6AE5FKKQ.cjs.map +1 -0
- package/dist/chunk-AD436M45.js +3854 -0
- package/dist/chunk-AD436M45.js.map +1 -0
- package/dist/chunk-ALS7ETWZ.cjs +4263 -0
- package/dist/chunk-ALS7ETWZ.cjs.map +1 -0
- package/dist/chunk-AU7XHGKJ.js +2092 -0
- package/dist/chunk-AU7XHGKJ.js.map +1 -0
- package/dist/chunk-B5TNKUEY.js +1481 -0
- package/dist/chunk-B5TNKUEY.js.map +1 -0
- package/dist/chunk-BCR7G3A6.js +9136 -0
- package/dist/chunk-BCR7G3A6.js.map +1 -0
- package/dist/chunk-C4PKXY74.cjs +1917 -0
- package/dist/chunk-C4PKXY74.cjs.map +1 -0
- package/dist/chunk-DWZY6PIP.cjs +6400 -0
- package/dist/chunk-DWZY6PIP.cjs.map +1 -0
- package/dist/chunk-E3EU5FZO.cjs +2113 -0
- package/dist/chunk-E3EU5FZO.cjs.map +1 -0
- package/dist/chunk-F3JWBINJ.js +1054 -0
- package/dist/chunk-F3JWBINJ.js.map +1 -0
- package/dist/chunk-FJYLIGJX.js +1940 -0
- package/dist/chunk-FJYLIGJX.js.map +1 -0
- package/dist/chunk-JSCDE774.cjs +729 -0
- package/dist/chunk-JSCDE774.cjs.map +1 -0
- package/dist/chunk-LWECRCW2.cjs +2412 -0
- package/dist/chunk-LWECRCW2.cjs.map +1 -0
- package/dist/chunk-MLBMYKCG.js +6379 -0
- package/dist/chunk-MLBMYKCG.js.map +1 -0
- package/dist/chunk-OX6QXFMV.cjs +3874 -0
- package/dist/chunk-OX6QXFMV.cjs.map +1 -0
- package/dist/chunk-PHV2DKRS.cjs +1072 -0
- package/dist/chunk-PHV2DKRS.cjs.map +1 -0
- package/dist/chunk-PL7TAYKI.js +4056 -0
- package/dist/chunk-PL7TAYKI.js.map +1 -0
- package/dist/chunk-PR647I7R.js +1898 -0
- package/dist/chunk-PR647I7R.js.map +1 -0
- package/dist/chunk-QERHVCHC.cjs +2960 -0
- package/dist/chunk-QERHVCHC.cjs.map +1 -0
- package/dist/chunk-XEG44RF6.cjs +1514 -0
- package/dist/chunk-XEG44RF6.cjs.map +1 -0
- package/dist/chunk-XMWVME2W.js +2377 -0
- package/dist/chunk-XMWVME2W.js.map +1 -0
- package/dist/chunk-ZB75FESB.cjs +1979 -0
- package/dist/chunk-ZB75FESB.cjs.map +1 -0
- package/dist/chunk-ZLW62TJG.cjs +4061 -0
- package/dist/chunk-ZLW62TJG.cjs.map +1 -0
- package/dist/chunk-ZXKBDFP3.js +4235 -0
- package/dist/chunk-ZXKBDFP3.js.map +1 -0
- package/dist/core/index.cjs +204 -0
- package/dist/core/index.cjs.map +1 -0
- package/dist/core/index.d.cts +2 -0
- package/dist/core/index.d.ts +2 -0
- package/dist/core/index.js +3 -0
- package/dist/core/index.js.map +1 -0
- package/dist/dataframe/index.cjs +22 -0
- package/dist/dataframe/index.cjs.map +1 -0
- package/dist/dataframe/index.d.cts +3 -0
- package/dist/dataframe/index.d.ts +3 -0
- package/dist/dataframe/index.js +5 -0
- package/dist/dataframe/index.js.map +1 -0
- package/dist/datasets/index.cjs +134 -0
- package/dist/datasets/index.cjs.map +1 -0
- package/dist/datasets/index.d.cts +3 -0
- package/dist/datasets/index.d.ts +3 -0
- package/dist/datasets/index.js +5 -0
- package/dist/datasets/index.js.map +1 -0
- package/dist/index-74AB8Cyh.d.cts +1126 -0
- package/dist/index-9oQx1HgV.d.cts +1180 -0
- package/dist/index-BJY2SI4i.d.ts +483 -0
- package/dist/index-BWGhrDlr.d.ts +733 -0
- package/dist/index-B_DK4FKY.d.cts +242 -0
- package/dist/index-BbA2Gxfl.d.ts +456 -0
- package/dist/index-BgHYAoSS.d.cts +837 -0
- package/dist/index-BndMbqsM.d.ts +1439 -0
- package/dist/index-C1mfVYoo.d.ts +2517 -0
- package/dist/index-CCvlwAmL.d.cts +809 -0
- package/dist/index-CDw5CnOU.d.ts +785 -0
- package/dist/index-Cn3SdB0O.d.ts +1126 -0
- package/dist/index-CrqLlS-a.d.ts +776 -0
- package/dist/index-D61yaSMY.d.cts +483 -0
- package/dist/index-D9Loo1_A.d.cts +2517 -0
- package/dist/index-DIT_OO9C.d.cts +785 -0
- package/dist/index-DIp_RrRt.d.ts +242 -0
- package/dist/index-DbultU6X.d.cts +1427 -0
- package/dist/index-DmEg_LCm.d.cts +776 -0
- package/dist/index-DoPWVxPo.d.cts +1439 -0
- package/dist/index-DuCxd-8d.d.ts +837 -0
- package/dist/index-Dx42TZaY.d.ts +809 -0
- package/dist/index-DyZ4QQf5.d.cts +456 -0
- package/dist/index-GFAVyOWO.d.ts +1427 -0
- package/dist/index-WHQLn0e8.d.cts +733 -0
- package/dist/index-ZtI1Iy4L.d.ts +1180 -0
- package/dist/index-eJgeni9c.d.cts +1911 -0
- package/dist/index-tk4lSYod.d.ts +1911 -0
- package/dist/index.cjs +72 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +17 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/linalg/index.cjs +86 -0
- package/dist/linalg/index.cjs.map +1 -0
- package/dist/linalg/index.d.cts +3 -0
- package/dist/linalg/index.d.ts +3 -0
- package/dist/linalg/index.js +5 -0
- package/dist/linalg/index.js.map +1 -0
- package/dist/metrics/index.cjs +158 -0
- package/dist/metrics/index.cjs.map +1 -0
- package/dist/metrics/index.d.cts +3 -0
- package/dist/metrics/index.d.ts +3 -0
- package/dist/metrics/index.js +5 -0
- package/dist/metrics/index.js.map +1 -0
- package/dist/ml/index.cjs +87 -0
- package/dist/ml/index.cjs.map +1 -0
- package/dist/ml/index.d.cts +3 -0
- package/dist/ml/index.d.ts +3 -0
- package/dist/ml/index.js +6 -0
- package/dist/ml/index.js.map +1 -0
- package/dist/ndarray/index.cjs +501 -0
- package/dist/ndarray/index.cjs.map +1 -0
- package/dist/ndarray/index.d.cts +5 -0
- package/dist/ndarray/index.d.ts +5 -0
- package/dist/ndarray/index.js +4 -0
- package/dist/ndarray/index.js.map +1 -0
- package/dist/nn/index.cjs +142 -0
- package/dist/nn/index.cjs.map +1 -0
- package/dist/nn/index.d.cts +6 -0
- package/dist/nn/index.d.ts +6 -0
- package/dist/nn/index.js +5 -0
- package/dist/nn/index.js.map +1 -0
- package/dist/optim/index.cjs +77 -0
- package/dist/optim/index.cjs.map +1 -0
- package/dist/optim/index.d.cts +4 -0
- package/dist/optim/index.d.ts +4 -0
- package/dist/optim/index.js +4 -0
- package/dist/optim/index.js.map +1 -0
- package/dist/plot/index.cjs +114 -0
- package/dist/plot/index.cjs.map +1 -0
- package/dist/plot/index.d.cts +6 -0
- package/dist/plot/index.d.ts +6 -0
- package/dist/plot/index.js +5 -0
- package/dist/plot/index.js.map +1 -0
- package/dist/preprocess/index.cjs +82 -0
- package/dist/preprocess/index.cjs.map +1 -0
- package/dist/preprocess/index.d.cts +4 -0
- package/dist/preprocess/index.d.ts +4 -0
- package/dist/preprocess/index.js +5 -0
- package/dist/preprocess/index.js.map +1 -0
- package/dist/random/index.cjs +74 -0
- package/dist/random/index.cjs.map +1 -0
- package/dist/random/index.d.cts +3 -0
- package/dist/random/index.d.ts +3 -0
- package/dist/random/index.js +5 -0
- package/dist/random/index.js.map +1 -0
- package/dist/stats/index.cjs +142 -0
- package/dist/stats/index.cjs.map +1 -0
- package/dist/stats/index.d.cts +3 -0
- package/dist/stats/index.d.ts +3 -0
- package/dist/stats/index.js +5 -0
- package/dist/stats/index.js.map +1 -0
- package/dist/tensor-B96jjJLQ.d.cts +205 -0
- package/dist/tensor-B96jjJLQ.d.ts +205 -0
- package/package.json +226 -0
|
@@ -0,0 +1,1072 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunk6AE5FKKQ_cjs = require('./chunk-6AE5FKKQ.cjs');
|
|
4
|
+
var chunkJSCDE774_cjs = require('./chunk-JSCDE774.cjs');
|
|
5
|
+
|
|
6
|
+
// src/random/index.ts
|
|
7
|
+
var random_exports = {};
|
|
8
|
+
chunkJSCDE774_cjs.__export(random_exports, {
|
|
9
|
+
beta: () => beta,
|
|
10
|
+
binomial: () => binomial,
|
|
11
|
+
choice: () => choice,
|
|
12
|
+
clearSeed: () => clearSeed,
|
|
13
|
+
exponential: () => exponential,
|
|
14
|
+
gamma: () => gamma,
|
|
15
|
+
getSeed: () => getSeed,
|
|
16
|
+
normal: () => normal,
|
|
17
|
+
permutation: () => permutation,
|
|
18
|
+
poisson: () => poisson,
|
|
19
|
+
rand: () => rand,
|
|
20
|
+
randint: () => randint,
|
|
21
|
+
randn: () => randn,
|
|
22
|
+
setSeed: () => setSeed,
|
|
23
|
+
shuffle: () => shuffle,
|
|
24
|
+
uniform: () => uniform
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// src/random/random.ts
|
|
28
|
+
var __globalSeed;
|
|
29
|
+
var UINT64_MASK = (1n << 64n) - 1n;
|
|
30
|
+
var PCG_MULT = 6364136223846793005n;
|
|
31
|
+
var SPLITMIX_GAMMA = 0x9e3779b97f4a7c15n;
|
|
32
|
+
var SPLITMIX_MIX1 = 0xbf58476d1ce4e5b9n;
|
|
33
|
+
var SPLITMIX_MIX2 = 0x94d049bb133111ebn;
|
|
34
|
+
var __SplitMix64 = class {
|
|
35
|
+
state;
|
|
36
|
+
constructor(seed) {
|
|
37
|
+
this.state = seed & UINT64_MASK;
|
|
38
|
+
}
|
|
39
|
+
next() {
|
|
40
|
+
this.state = this.state + SPLITMIX_GAMMA & UINT64_MASK;
|
|
41
|
+
let z = this.state;
|
|
42
|
+
z = (z ^ z >> 30n) * SPLITMIX_MIX1;
|
|
43
|
+
z &= UINT64_MASK;
|
|
44
|
+
z = (z ^ z >> 27n) * SPLITMIX_MIX2;
|
|
45
|
+
z &= UINT64_MASK;
|
|
46
|
+
return z ^ z >> 31n;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var __SeededRandom = class {
|
|
50
|
+
/** Current uint64 state. */
|
|
51
|
+
state;
|
|
52
|
+
/** Stream/sequence selector (must be odd). */
|
|
53
|
+
inc;
|
|
54
|
+
/**
|
|
55
|
+
* Create a new PRNG from a seed.
|
|
56
|
+
*
|
|
57
|
+
* @param seedUint64 - Seed coerced to uint64.
|
|
58
|
+
*/
|
|
59
|
+
constructor(seedUint64) {
|
|
60
|
+
const sm = new __SplitMix64(seedUint64);
|
|
61
|
+
this.state = sm.next();
|
|
62
|
+
this.inc = sm.next() << 1n | 1n;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Generate the next uint32 sample.
|
|
66
|
+
*/
|
|
67
|
+
nextUint32() {
|
|
68
|
+
const oldstate = this.state;
|
|
69
|
+
this.state = oldstate * PCG_MULT + this.inc & UINT64_MASK;
|
|
70
|
+
const xorshifted = Number((oldstate >> 18n ^ oldstate) >> 27n) >>> 0;
|
|
71
|
+
const rot = Number(oldstate >> 59n) & 31;
|
|
72
|
+
return (xorshifted >>> rot | xorshifted << (-rot & 31)) >>> 0;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Generate the next uniform sample in [0, 1).
|
|
76
|
+
*/
|
|
77
|
+
next() {
|
|
78
|
+
return this.nextUint32() / 2 ** 32;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
var __rng = null;
|
|
82
|
+
function __setSeed(seed) {
|
|
83
|
+
if (!Number.isFinite(seed)) {
|
|
84
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError(`seed must be a finite number; received ${seed}`, "seed", seed);
|
|
85
|
+
}
|
|
86
|
+
__globalSeed = seed;
|
|
87
|
+
const seedUint64 = BigInt.asUintN(64, BigInt(Math.trunc(seed)));
|
|
88
|
+
__rng = new __SeededRandom(seedUint64);
|
|
89
|
+
}
|
|
90
|
+
function __getSeed() {
|
|
91
|
+
return __globalSeed;
|
|
92
|
+
}
|
|
93
|
+
function __clearSeed() {
|
|
94
|
+
__globalSeed = void 0;
|
|
95
|
+
__rng = null;
|
|
96
|
+
}
|
|
97
|
+
function getCrypto() {
|
|
98
|
+
if (typeof crypto === "undefined") {
|
|
99
|
+
return void 0;
|
|
100
|
+
}
|
|
101
|
+
if (typeof crypto.getRandomValues !== "function") {
|
|
102
|
+
return void 0;
|
|
103
|
+
}
|
|
104
|
+
return crypto;
|
|
105
|
+
}
|
|
106
|
+
function randomUint32FromCrypto() {
|
|
107
|
+
const crypto2 = getCrypto();
|
|
108
|
+
if (!crypto2) {
|
|
109
|
+
throw new chunkJSCDE774_cjs.NotImplementedError(
|
|
110
|
+
"Cryptographically secure randomness is unavailable in this environment. Provide a seed for deterministic randomness."
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
const buf = new Uint32Array(1);
|
|
114
|
+
crypto2.getRandomValues(buf);
|
|
115
|
+
const value = buf[0];
|
|
116
|
+
if (value === void 0) {
|
|
117
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("Failed to read cryptographic randomness", "crypto", value);
|
|
118
|
+
}
|
|
119
|
+
return value >>> 0;
|
|
120
|
+
}
|
|
121
|
+
function __random() {
|
|
122
|
+
if (__rng) {
|
|
123
|
+
return __rng.next();
|
|
124
|
+
}
|
|
125
|
+
return randomUint32FromCrypto() / 2 ** 32;
|
|
126
|
+
}
|
|
127
|
+
function __randomUint32() {
|
|
128
|
+
if (__rng) {
|
|
129
|
+
return __rng.nextUint32();
|
|
130
|
+
}
|
|
131
|
+
return randomUint32FromCrypto();
|
|
132
|
+
}
|
|
133
|
+
function __randomUint53() {
|
|
134
|
+
const hi = __randomUint32() >>> 5;
|
|
135
|
+
const lo = __randomUint32() >>> 6;
|
|
136
|
+
return hi * 2 ** 26 + lo;
|
|
137
|
+
}
|
|
138
|
+
function __normalRandom() {
|
|
139
|
+
let u1 = __random();
|
|
140
|
+
while (u1 === 0) {
|
|
141
|
+
u1 = __random();
|
|
142
|
+
}
|
|
143
|
+
const u2 = __random();
|
|
144
|
+
return Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);
|
|
145
|
+
}
|
|
146
|
+
function __gammaLarge(shape) {
|
|
147
|
+
if (!Number.isFinite(shape) || shape <= 1 / 3) {
|
|
148
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError(
|
|
149
|
+
"shape must be a finite number > 1/3 for Marsaglia-Tsang method",
|
|
150
|
+
"shape",
|
|
151
|
+
shape
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
const d = shape - 1 / 3;
|
|
155
|
+
const c = 1 / Math.sqrt(9 * d);
|
|
156
|
+
while (true) {
|
|
157
|
+
let x;
|
|
158
|
+
let v;
|
|
159
|
+
do {
|
|
160
|
+
x = __normalRandom();
|
|
161
|
+
v = 1 + c * x;
|
|
162
|
+
} while (v <= 0);
|
|
163
|
+
v = v * v * v;
|
|
164
|
+
const u = __random();
|
|
165
|
+
if (u < 1 - 0.0331 * x * x * x * x) {
|
|
166
|
+
return d * v;
|
|
167
|
+
}
|
|
168
|
+
if (Math.log(u) < 0.5 * x * x + d * (1 - v + Math.log(v))) {
|
|
169
|
+
return d * v;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// src/random/index.ts
|
|
175
|
+
var INT32_MIN = -2147483648;
|
|
176
|
+
var INT32_MAX = 2147483647;
|
|
177
|
+
var UINT32_RANGE = 2 ** 32;
|
|
178
|
+
var UINT53_RANGE = 2 ** 53;
|
|
179
|
+
function resolveDevice(device) {
|
|
180
|
+
const resolved = device ?? chunkJSCDE774_cjs.getConfig().defaultDevice;
|
|
181
|
+
return chunkJSCDE774_cjs.validateDevice(resolved, "device");
|
|
182
|
+
}
|
|
183
|
+
function resolveFloatDType(dtype, functionName) {
|
|
184
|
+
const resolved = dtype ?? "float32";
|
|
185
|
+
if (resolved !== "float32" && resolved !== "float64") {
|
|
186
|
+
throw new chunkJSCDE774_cjs.DTypeError(`${functionName} only supports float32 or float64 dtype`);
|
|
187
|
+
}
|
|
188
|
+
return resolved;
|
|
189
|
+
}
|
|
190
|
+
function resolveIntegerDType(dtype, functionName) {
|
|
191
|
+
const resolved = dtype ?? "int32";
|
|
192
|
+
if (resolved !== "int32" && resolved !== "int64") {
|
|
193
|
+
throw new chunkJSCDE774_cjs.DTypeError(`${functionName} only supports int32 or int64 dtype`);
|
|
194
|
+
}
|
|
195
|
+
return resolved;
|
|
196
|
+
}
|
|
197
|
+
function assertSafeInteger(value, name) {
|
|
198
|
+
if (!Number.isFinite(value) || !Number.isInteger(value) || !Number.isSafeInteger(value)) {
|
|
199
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError(`${name} must be a safe integer`, name, value);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
function assertInt32Bounds(value, name) {
|
|
203
|
+
if (value < INT32_MIN || value > INT32_MAX) {
|
|
204
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError(
|
|
205
|
+
`${name} must be within int32 range [${INT32_MIN}, ${INT32_MAX}]`,
|
|
206
|
+
name,
|
|
207
|
+
value
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
function randomIntBelow(maxExclusive) {
|
|
212
|
+
if (!Number.isSafeInteger(maxExclusive) || maxExclusive <= 0) {
|
|
213
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("range must be a positive safe integer", "range", maxExclusive);
|
|
214
|
+
}
|
|
215
|
+
if (maxExclusive <= UINT32_RANGE) {
|
|
216
|
+
const limit = Math.floor(UINT32_RANGE / maxExclusive) * maxExclusive;
|
|
217
|
+
let value = __randomUint32();
|
|
218
|
+
while (value >= limit) {
|
|
219
|
+
value = __randomUint32();
|
|
220
|
+
}
|
|
221
|
+
return value % maxExclusive;
|
|
222
|
+
}
|
|
223
|
+
if (maxExclusive <= UINT53_RANGE) {
|
|
224
|
+
const limit = Math.floor(UINT53_RANGE / maxExclusive) * maxExclusive;
|
|
225
|
+
let value = __randomUint53();
|
|
226
|
+
while (value >= limit) {
|
|
227
|
+
value = __randomUint53();
|
|
228
|
+
}
|
|
229
|
+
return value % maxExclusive;
|
|
230
|
+
}
|
|
231
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError(
|
|
232
|
+
"range must be <= 2^53 for unbiased sampling",
|
|
233
|
+
"range",
|
|
234
|
+
maxExclusive
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
function allocateFloatBuffer(dtype, size) {
|
|
238
|
+
return dtype === "float32" ? new Float32Array(size) : new Float64Array(size);
|
|
239
|
+
}
|
|
240
|
+
function allocateIntegerBuffer(dtype, size) {
|
|
241
|
+
return dtype === "int64" ? new BigInt64Array(size) : new Int32Array(size);
|
|
242
|
+
}
|
|
243
|
+
function writeInteger(buffer, index, value) {
|
|
244
|
+
if (buffer instanceof BigInt64Array) {
|
|
245
|
+
buffer[index] = BigInt(value);
|
|
246
|
+
} else {
|
|
247
|
+
buffer[index] = value;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
function randomOpenUnit() {
|
|
251
|
+
let u = __random();
|
|
252
|
+
while (u === 0) {
|
|
253
|
+
u = __random();
|
|
254
|
+
}
|
|
255
|
+
return u;
|
|
256
|
+
}
|
|
257
|
+
function validateContiguous(t, functionName) {
|
|
258
|
+
if (t.offset !== 0) {
|
|
259
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError(
|
|
260
|
+
`${functionName} currently requires offset === 0`,
|
|
261
|
+
"offset",
|
|
262
|
+
t.offset
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
for (let axis = 0; axis < t.ndim; axis++) {
|
|
266
|
+
const expected = t.strides[axis];
|
|
267
|
+
const tail = t.shape.slice(axis + 1).reduce((acc, v) => acc * v, 1);
|
|
268
|
+
if (expected !== tail) {
|
|
269
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError(
|
|
270
|
+
`${functionName} currently requires a contiguous tensor`,
|
|
271
|
+
"strides",
|
|
272
|
+
t.strides
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
var LANCZOS_COEFFS = [
|
|
278
|
+
676.5203681218851,
|
|
279
|
+
-1259.1392167224028,
|
|
280
|
+
771.3234287776531,
|
|
281
|
+
-176.6150291621406,
|
|
282
|
+
12.507343278686905,
|
|
283
|
+
-0.13857109526572012,
|
|
284
|
+
9984369578019572e-21,
|
|
285
|
+
15056327351493116e-23
|
|
286
|
+
];
|
|
287
|
+
function logGamma(z) {
|
|
288
|
+
if (!Number.isFinite(z) || z <= 0) {
|
|
289
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("logGamma requires a positive finite input", "z", z);
|
|
290
|
+
}
|
|
291
|
+
let x = 0.9999999999998099;
|
|
292
|
+
for (const [i, coeff] of LANCZOS_COEFFS.entries()) {
|
|
293
|
+
x += coeff / (z + i);
|
|
294
|
+
}
|
|
295
|
+
const g = 7;
|
|
296
|
+
const t = z + g - 0.5;
|
|
297
|
+
return 0.5 * Math.log(2 * Math.PI) + (z - 0.5) * Math.log(t) - t + Math.log(x);
|
|
298
|
+
}
|
|
299
|
+
function logFactorial(n) {
|
|
300
|
+
if (n <= 1) return 0;
|
|
301
|
+
if (n <= 20) {
|
|
302
|
+
let result = 0;
|
|
303
|
+
for (let i = 2; i <= n; i++) {
|
|
304
|
+
result += Math.log(i);
|
|
305
|
+
}
|
|
306
|
+
return result;
|
|
307
|
+
}
|
|
308
|
+
return logGamma(n + 1);
|
|
309
|
+
}
|
|
310
|
+
function sampleGammaUnit(shape) {
|
|
311
|
+
if (shape < 1) {
|
|
312
|
+
const u = randomOpenUnit();
|
|
313
|
+
return __gammaLarge(shape + 1) * u ** (1 / shape);
|
|
314
|
+
}
|
|
315
|
+
return __gammaLarge(shape);
|
|
316
|
+
}
|
|
317
|
+
function setSeed(seed) {
|
|
318
|
+
__setSeed(seed);
|
|
319
|
+
}
|
|
320
|
+
function getSeed() {
|
|
321
|
+
return __getSeed();
|
|
322
|
+
}
|
|
323
|
+
function clearSeed() {
|
|
324
|
+
__clearSeed();
|
|
325
|
+
}
|
|
326
|
+
function rand(shape, opts = {}) {
|
|
327
|
+
const size = chunkJSCDE774_cjs.shapeToSize(shape);
|
|
328
|
+
const dtype = resolveFloatDType(opts.dtype, "rand");
|
|
329
|
+
const data = allocateFloatBuffer(dtype, size);
|
|
330
|
+
for (let i = 0; i < size; i++) {
|
|
331
|
+
data[i] = __random();
|
|
332
|
+
}
|
|
333
|
+
return chunk6AE5FKKQ_cjs.Tensor.fromTypedArray({
|
|
334
|
+
data,
|
|
335
|
+
shape,
|
|
336
|
+
dtype,
|
|
337
|
+
device: resolveDevice(opts.device)
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
function randn(shape, opts = {}) {
|
|
341
|
+
const size = chunkJSCDE774_cjs.shapeToSize(shape);
|
|
342
|
+
const dtype = resolveFloatDType(opts.dtype, "randn");
|
|
343
|
+
const data = allocateFloatBuffer(dtype, size);
|
|
344
|
+
for (let i = 0; i < size; i++) {
|
|
345
|
+
data[i] = __normalRandom();
|
|
346
|
+
}
|
|
347
|
+
return chunk6AE5FKKQ_cjs.Tensor.fromTypedArray({
|
|
348
|
+
data,
|
|
349
|
+
shape,
|
|
350
|
+
dtype,
|
|
351
|
+
device: resolveDevice(opts.device)
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
function randint(low, high, shape, opts = {}) {
|
|
355
|
+
assertSafeInteger(low, "low");
|
|
356
|
+
assertSafeInteger(high, "high");
|
|
357
|
+
if (high <= low) {
|
|
358
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("high must be > low", "high", high);
|
|
359
|
+
}
|
|
360
|
+
const dtype = resolveIntegerDType(opts.dtype, "randint");
|
|
361
|
+
if (dtype === "int32") {
|
|
362
|
+
assertInt32Bounds(low, "low");
|
|
363
|
+
if (high > INT32_MAX + 1) {
|
|
364
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError(
|
|
365
|
+
`high must be <= ${INT32_MAX + 1} for int32 output`,
|
|
366
|
+
"high",
|
|
367
|
+
high
|
|
368
|
+
);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
const size = chunkJSCDE774_cjs.shapeToSize(shape);
|
|
372
|
+
const data = allocateIntegerBuffer(dtype, size);
|
|
373
|
+
const range = high - low;
|
|
374
|
+
if (!Number.isSafeInteger(range) || range <= 0) {
|
|
375
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("range must be a positive safe integer", "high", high);
|
|
376
|
+
}
|
|
377
|
+
for (let i = 0; i < size; i++) {
|
|
378
|
+
const sample = randomIntBelow(range) + low;
|
|
379
|
+
writeInteger(data, i, sample);
|
|
380
|
+
}
|
|
381
|
+
return chunk6AE5FKKQ_cjs.Tensor.fromTypedArray({
|
|
382
|
+
data,
|
|
383
|
+
shape,
|
|
384
|
+
dtype,
|
|
385
|
+
device: resolveDevice(opts.device)
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
function uniform(low = 0, high = 1, shape = [], opts = {}) {
|
|
389
|
+
if (!Number.isFinite(low) || !Number.isFinite(high)) {
|
|
390
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("low and high must be finite", "low/high", {
|
|
391
|
+
low,
|
|
392
|
+
high
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
if (high < low) {
|
|
396
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("high must be >= low", "high", high);
|
|
397
|
+
}
|
|
398
|
+
const size = chunkJSCDE774_cjs.shapeToSize(shape);
|
|
399
|
+
const dtype = resolveFloatDType(opts.dtype, "uniform");
|
|
400
|
+
const data = allocateFloatBuffer(dtype, size);
|
|
401
|
+
const range = high - low;
|
|
402
|
+
for (let i = 0; i < size; i++) {
|
|
403
|
+
data[i] = __random() * range + low;
|
|
404
|
+
}
|
|
405
|
+
return chunk6AE5FKKQ_cjs.Tensor.fromTypedArray({
|
|
406
|
+
data,
|
|
407
|
+
shape,
|
|
408
|
+
dtype,
|
|
409
|
+
device: resolveDevice(opts.device)
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
function normal(mean = 0, std = 1, shape = [], opts = {}) {
|
|
413
|
+
if (!Number.isFinite(mean) || !Number.isFinite(std)) {
|
|
414
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("mean and std must be finite", "mean/std", {
|
|
415
|
+
mean,
|
|
416
|
+
std
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
if (std < 0) {
|
|
420
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("std must be >= 0", "std", std);
|
|
421
|
+
}
|
|
422
|
+
const size = chunkJSCDE774_cjs.shapeToSize(shape);
|
|
423
|
+
const dtype = resolveFloatDType(opts.dtype, "normal");
|
|
424
|
+
const data = allocateFloatBuffer(dtype, size);
|
|
425
|
+
for (let i = 0; i < size; i++) {
|
|
426
|
+
data[i] = __normalRandom() * std + mean;
|
|
427
|
+
}
|
|
428
|
+
return chunk6AE5FKKQ_cjs.Tensor.fromTypedArray({
|
|
429
|
+
data,
|
|
430
|
+
shape,
|
|
431
|
+
dtype,
|
|
432
|
+
device: resolveDevice(opts.device)
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
function binomialSmallMean(n, logQ) {
|
|
436
|
+
let successes = 0;
|
|
437
|
+
let trials = 0;
|
|
438
|
+
while (true) {
|
|
439
|
+
const u = randomOpenUnit();
|
|
440
|
+
const gap = Math.floor(Math.log(u) / logQ) + 1;
|
|
441
|
+
if (gap > n - trials) {
|
|
442
|
+
return successes;
|
|
443
|
+
}
|
|
444
|
+
trials += gap;
|
|
445
|
+
successes++;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
function binomialChopDown(n, p, q, mode, pmfMode) {
|
|
449
|
+
const u = __random();
|
|
450
|
+
let cumulative = pmfMode;
|
|
451
|
+
if (u <= cumulative) {
|
|
452
|
+
return mode;
|
|
453
|
+
}
|
|
454
|
+
let left = mode;
|
|
455
|
+
let right = mode;
|
|
456
|
+
let pmfLeft = pmfMode;
|
|
457
|
+
let pmfRight = pmfMode;
|
|
458
|
+
const ratioLeft = q / p;
|
|
459
|
+
const ratioRight = p / q;
|
|
460
|
+
while (left > 0 || right < n) {
|
|
461
|
+
if (left > 0) {
|
|
462
|
+
pmfLeft *= left / (n - left + 1) * ratioLeft;
|
|
463
|
+
left -= 1;
|
|
464
|
+
cumulative += pmfLeft;
|
|
465
|
+
if (u <= cumulative) {
|
|
466
|
+
return left;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
if (right < n) {
|
|
470
|
+
pmfRight *= (n - right) / (right + 1) * ratioRight;
|
|
471
|
+
right += 1;
|
|
472
|
+
cumulative += pmfRight;
|
|
473
|
+
if (u <= cumulative) {
|
|
474
|
+
return right;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
return u <= cumulative ? left : right;
|
|
479
|
+
}
|
|
480
|
+
function binomial(n, p, shape = [], opts = {}) {
|
|
481
|
+
assertSafeInteger(n, "n");
|
|
482
|
+
if (n < 0) {
|
|
483
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("n must be >= 0", "n", n);
|
|
484
|
+
}
|
|
485
|
+
if (!Number.isFinite(p) || p < 0 || p > 1) {
|
|
486
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("p must be in [0, 1]", "p", p);
|
|
487
|
+
}
|
|
488
|
+
const size = chunkJSCDE774_cjs.shapeToSize(shape);
|
|
489
|
+
const dtype = resolveIntegerDType(opts.dtype, "binomial");
|
|
490
|
+
if (dtype === "int32") {
|
|
491
|
+
if (n > INT32_MAX) {
|
|
492
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError(`n must be <= ${INT32_MAX} for int32 output`, "n", n);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
const data = allocateIntegerBuffer(dtype, size);
|
|
496
|
+
if (n === 0) {
|
|
497
|
+
return chunk6AE5FKKQ_cjs.Tensor.fromTypedArray({
|
|
498
|
+
data,
|
|
499
|
+
shape,
|
|
500
|
+
dtype,
|
|
501
|
+
device: resolveDevice(opts.device)
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
if (p === 0) {
|
|
505
|
+
return chunk6AE5FKKQ_cjs.Tensor.fromTypedArray({
|
|
506
|
+
data,
|
|
507
|
+
shape,
|
|
508
|
+
dtype,
|
|
509
|
+
device: resolveDevice(opts.device)
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
if (p === 1) {
|
|
513
|
+
for (let i = 0; i < size; i++) {
|
|
514
|
+
writeInteger(data, i, n);
|
|
515
|
+
}
|
|
516
|
+
return chunk6AE5FKKQ_cjs.Tensor.fromTypedArray({
|
|
517
|
+
data,
|
|
518
|
+
shape,
|
|
519
|
+
dtype,
|
|
520
|
+
device: resolveDevice(opts.device)
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
const flip = p > 0.5;
|
|
524
|
+
const prob = flip ? 1 - p : p;
|
|
525
|
+
const q = 1 - prob;
|
|
526
|
+
if (q === 1) {
|
|
527
|
+
const value = flip ? n : 0;
|
|
528
|
+
for (let i = 0; i < size; i++) {
|
|
529
|
+
writeInteger(data, i, value);
|
|
530
|
+
}
|
|
531
|
+
return chunk6AE5FKKQ_cjs.Tensor.fromTypedArray({
|
|
532
|
+
data,
|
|
533
|
+
shape,
|
|
534
|
+
dtype,
|
|
535
|
+
device: resolveDevice(opts.device)
|
|
536
|
+
});
|
|
537
|
+
}
|
|
538
|
+
const mean = n * prob;
|
|
539
|
+
const logQ = Math.log(q);
|
|
540
|
+
if (mean < 10) {
|
|
541
|
+
for (let i = 0; i < size; i++) {
|
|
542
|
+
const sample = binomialSmallMean(n, logQ);
|
|
543
|
+
writeInteger(data, i, flip ? n - sample : sample);
|
|
544
|
+
}
|
|
545
|
+
} else {
|
|
546
|
+
const mode = Math.floor((n + 1) * prob);
|
|
547
|
+
const logP = Math.log(prob);
|
|
548
|
+
const logPmfMode = logFactorial(n) - logFactorial(mode) - logFactorial(n - mode) + mode * logP + (n - mode) * logQ;
|
|
549
|
+
const pmfMode = Math.exp(logPmfMode);
|
|
550
|
+
if (!Number.isFinite(pmfMode) || pmfMode <= 0) {
|
|
551
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("Failed to initialize binomial sampler", "p", p);
|
|
552
|
+
}
|
|
553
|
+
for (let i = 0; i < size; i++) {
|
|
554
|
+
const sample = binomialChopDown(n, prob, q, mode, pmfMode);
|
|
555
|
+
writeInteger(data, i, flip ? n - sample : sample);
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
return chunk6AE5FKKQ_cjs.Tensor.fromTypedArray({
|
|
559
|
+
data,
|
|
560
|
+
shape,
|
|
561
|
+
dtype,
|
|
562
|
+
device: resolveDevice(opts.device)
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
function poisson(lambda, shape = [], opts = {}) {
|
|
566
|
+
if (!Number.isFinite(lambda) || lambda < 0) {
|
|
567
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("lambda must be a finite number >= 0", "lambda", lambda);
|
|
568
|
+
}
|
|
569
|
+
const size = chunkJSCDE774_cjs.shapeToSize(shape);
|
|
570
|
+
const dtype = resolveIntegerDType(opts.dtype, "poisson");
|
|
571
|
+
if (dtype === "int32") {
|
|
572
|
+
if (lambda > INT32_MAX) {
|
|
573
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError(
|
|
574
|
+
`lambda must be <= ${INT32_MAX} for int32 output`,
|
|
575
|
+
"lambda",
|
|
576
|
+
lambda
|
|
577
|
+
);
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
const data = allocateIntegerBuffer(dtype, size);
|
|
581
|
+
if (lambda < 30) {
|
|
582
|
+
const L = Math.exp(-lambda);
|
|
583
|
+
for (let i = 0; i < size; i++) {
|
|
584
|
+
let k = 0;
|
|
585
|
+
let p = 1;
|
|
586
|
+
do {
|
|
587
|
+
k++;
|
|
588
|
+
p *= __random();
|
|
589
|
+
} while (p > L);
|
|
590
|
+
const sample = k - 1;
|
|
591
|
+
if (!Number.isSafeInteger(sample)) {
|
|
592
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError(
|
|
593
|
+
"poisson sample exceeds safe integer range",
|
|
594
|
+
"lambda",
|
|
595
|
+
lambda
|
|
596
|
+
);
|
|
597
|
+
}
|
|
598
|
+
if (dtype === "int32" && sample > INT32_MAX) {
|
|
599
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("poisson sample exceeds int32 range", "lambda", lambda);
|
|
600
|
+
}
|
|
601
|
+
writeInteger(data, i, sample);
|
|
602
|
+
}
|
|
603
|
+
} else {
|
|
604
|
+
const c = 0.767 - 3.36 / lambda;
|
|
605
|
+
const beta2 = Math.PI / Math.sqrt(3 * lambda);
|
|
606
|
+
const alpha = beta2 * lambda;
|
|
607
|
+
const k = Math.log(c) - lambda - Math.log(beta2);
|
|
608
|
+
for (let i = 0; i < size; i++) {
|
|
609
|
+
while (true) {
|
|
610
|
+
const u = __random();
|
|
611
|
+
if (u === 0 || u === 1) continue;
|
|
612
|
+
const x = (alpha - Math.log((1 - u) / u)) / beta2;
|
|
613
|
+
const n = Math.floor(x + 0.5);
|
|
614
|
+
if (n < 0 || !Number.isFinite(n)) continue;
|
|
615
|
+
const v = __random();
|
|
616
|
+
const y = alpha - beta2 * x;
|
|
617
|
+
const lhs = y + Math.log(v / (1 + Math.exp(y)) ** 2);
|
|
618
|
+
const rhs = k + n * Math.log(lambda) - logFactorial(n);
|
|
619
|
+
if (lhs <= rhs) {
|
|
620
|
+
if (!Number.isSafeInteger(n)) {
|
|
621
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError(
|
|
622
|
+
"poisson sample exceeds safe integer range",
|
|
623
|
+
"lambda",
|
|
624
|
+
lambda
|
|
625
|
+
);
|
|
626
|
+
}
|
|
627
|
+
if (dtype === "int32" && n > INT32_MAX) {
|
|
628
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("poisson sample exceeds int32 range", "lambda", lambda);
|
|
629
|
+
}
|
|
630
|
+
writeInteger(data, i, n);
|
|
631
|
+
break;
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
return chunk6AE5FKKQ_cjs.Tensor.fromTypedArray({
|
|
637
|
+
data,
|
|
638
|
+
shape,
|
|
639
|
+
dtype,
|
|
640
|
+
device: resolveDevice(opts.device)
|
|
641
|
+
});
|
|
642
|
+
}
|
|
643
|
+
function exponential(scale = 1, shape = [], opts = {}) {
|
|
644
|
+
if (!Number.isFinite(scale) || scale <= 0) {
|
|
645
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("scale must be a finite number > 0", "scale", scale);
|
|
646
|
+
}
|
|
647
|
+
const size = chunkJSCDE774_cjs.shapeToSize(shape);
|
|
648
|
+
const dtype = resolveFloatDType(opts.dtype, "exponential");
|
|
649
|
+
const data = allocateFloatBuffer(dtype, size);
|
|
650
|
+
for (let i = 0; i < size; i++) {
|
|
651
|
+
const u = randomOpenUnit();
|
|
652
|
+
data[i] = -scale * Math.log(u);
|
|
653
|
+
}
|
|
654
|
+
return chunk6AE5FKKQ_cjs.Tensor.fromTypedArray({
|
|
655
|
+
data,
|
|
656
|
+
shape,
|
|
657
|
+
dtype,
|
|
658
|
+
device: resolveDevice(opts.device)
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
function gamma(shape_param, scale = 1, shape = [], opts = {}) {
|
|
662
|
+
if (!Number.isFinite(shape_param) || shape_param <= 0) {
|
|
663
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError(
|
|
664
|
+
"shape_param must be a finite number > 0",
|
|
665
|
+
"shape_param",
|
|
666
|
+
shape_param
|
|
667
|
+
);
|
|
668
|
+
}
|
|
669
|
+
if (!Number.isFinite(scale) || scale <= 0) {
|
|
670
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("scale must be a finite number > 0", "scale", scale);
|
|
671
|
+
}
|
|
672
|
+
const size = chunkJSCDE774_cjs.shapeToSize(shape);
|
|
673
|
+
const dtype = resolveFloatDType(opts.dtype, "gamma");
|
|
674
|
+
const data = allocateFloatBuffer(dtype, size);
|
|
675
|
+
for (let i = 0; i < size; i++) {
|
|
676
|
+
data[i] = sampleGammaUnit(shape_param) * scale;
|
|
677
|
+
}
|
|
678
|
+
return chunk6AE5FKKQ_cjs.Tensor.fromTypedArray({
|
|
679
|
+
data,
|
|
680
|
+
shape,
|
|
681
|
+
dtype,
|
|
682
|
+
device: resolveDevice(opts.device)
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
function beta(alpha, beta_param, shape = [], opts = {}) {
|
|
686
|
+
if (!Number.isFinite(alpha) || alpha <= 0) {
|
|
687
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("alpha must be a finite number > 0", "alpha", alpha);
|
|
688
|
+
}
|
|
689
|
+
if (!Number.isFinite(beta_param) || beta_param <= 0) {
|
|
690
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("beta must be a finite number > 0", "beta_param", beta_param);
|
|
691
|
+
}
|
|
692
|
+
const size = chunkJSCDE774_cjs.shapeToSize(shape);
|
|
693
|
+
const dtype = resolveFloatDType(opts.dtype, "beta");
|
|
694
|
+
const data = allocateFloatBuffer(dtype, size);
|
|
695
|
+
for (let i = 0; i < size; i++) {
|
|
696
|
+
let sampled = false;
|
|
697
|
+
for (let attempt = 0; attempt < 1024; attempt++) {
|
|
698
|
+
const x = sampleGammaUnit(alpha);
|
|
699
|
+
const y = sampleGammaUnit(beta_param);
|
|
700
|
+
const sum = x + y;
|
|
701
|
+
if (!Number.isFinite(sum) || sum <= 0) {
|
|
702
|
+
continue;
|
|
703
|
+
}
|
|
704
|
+
const value = x / sum;
|
|
705
|
+
if (Number.isFinite(value) && value >= 0 && value <= 1) {
|
|
706
|
+
data[i] = value;
|
|
707
|
+
sampled = true;
|
|
708
|
+
break;
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
if (!sampled) {
|
|
712
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError(
|
|
713
|
+
"beta sampling failed to produce a finite sample",
|
|
714
|
+
"alpha/beta_param",
|
|
715
|
+
{ alpha, beta_param }
|
|
716
|
+
);
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
return chunk6AE5FKKQ_cjs.Tensor.fromTypedArray({
|
|
720
|
+
data,
|
|
721
|
+
shape,
|
|
722
|
+
dtype,
|
|
723
|
+
device: resolveDevice(opts.device)
|
|
724
|
+
});
|
|
725
|
+
}
|
|
726
|
+
function readNumericTensorValue(t, index) {
|
|
727
|
+
if (t.dtype === "string") {
|
|
728
|
+
throw new chunkJSCDE774_cjs.DTypeError("Expected numeric tensor");
|
|
729
|
+
}
|
|
730
|
+
const value = t.data[index];
|
|
731
|
+
if (typeof value === "number" || typeof value === "bigint") {
|
|
732
|
+
return value;
|
|
733
|
+
}
|
|
734
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("Internal error: tensor index out of bounds", "index", index);
|
|
735
|
+
}
|
|
736
|
+
function allocateNumericBuffer(dtype, size) {
|
|
737
|
+
switch (dtype) {
|
|
738
|
+
case "float32":
|
|
739
|
+
return new Float32Array(size);
|
|
740
|
+
case "float64":
|
|
741
|
+
return new Float64Array(size);
|
|
742
|
+
case "int32":
|
|
743
|
+
return new Int32Array(size);
|
|
744
|
+
case "int64":
|
|
745
|
+
return new BigInt64Array(size);
|
|
746
|
+
case "uint8":
|
|
747
|
+
case "bool":
|
|
748
|
+
return new Uint8Array(size);
|
|
749
|
+
case "string":
|
|
750
|
+
throw new chunkJSCDE774_cjs.DTypeError("choice() does not support string tensors");
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
function writeNumericValue(out, index, value) {
|
|
754
|
+
if (out instanceof BigInt64Array) {
|
|
755
|
+
out[index] = typeof value === "bigint" ? value : BigInt(Math.trunc(value));
|
|
756
|
+
return;
|
|
757
|
+
}
|
|
758
|
+
if (typeof value === "bigint") {
|
|
759
|
+
out[index] = Number(value);
|
|
760
|
+
return;
|
|
761
|
+
}
|
|
762
|
+
out[index] = value;
|
|
763
|
+
}
|
|
764
|
+
function buildNormalizedProbabilities(probabilities, n) {
|
|
765
|
+
if (probabilities.dtype === "string") {
|
|
766
|
+
throw new chunkJSCDE774_cjs.DTypeError("choice() probabilities must be numeric");
|
|
767
|
+
}
|
|
768
|
+
if (probabilities.ndim !== 1) {
|
|
769
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("p must be a 1D tensor", "p", probabilities.shape);
|
|
770
|
+
}
|
|
771
|
+
if (probabilities.size !== n) {
|
|
772
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError(
|
|
773
|
+
"p must have the same length as the population",
|
|
774
|
+
"p",
|
|
775
|
+
probabilities.size
|
|
776
|
+
);
|
|
777
|
+
}
|
|
778
|
+
validateContiguous(probabilities, "choice(p)");
|
|
779
|
+
const normalized = new Float64Array(n);
|
|
780
|
+
let sum = 0;
|
|
781
|
+
for (let i = 0; i < n; i++) {
|
|
782
|
+
const value = Number(readNumericTensorValue(probabilities, i));
|
|
783
|
+
if (!Number.isFinite(value) || value < 0) {
|
|
784
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError(
|
|
785
|
+
"p must contain finite non-negative probabilities",
|
|
786
|
+
"p",
|
|
787
|
+
value
|
|
788
|
+
);
|
|
789
|
+
}
|
|
790
|
+
normalized[i] = value;
|
|
791
|
+
sum += value;
|
|
792
|
+
}
|
|
793
|
+
if (!Number.isFinite(sum) || sum <= 0) {
|
|
794
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("sum(p) must be > 0 and finite", "p", sum);
|
|
795
|
+
}
|
|
796
|
+
for (let i = 0; i < n; i++) {
|
|
797
|
+
normalized[i] = (normalized[i] ?? 0) / sum;
|
|
798
|
+
}
|
|
799
|
+
return normalized;
|
|
800
|
+
}
|
|
801
|
+
function sampleFromCdf(cdf) {
|
|
802
|
+
const u = randomOpenUnit();
|
|
803
|
+
let left = 0;
|
|
804
|
+
let right = cdf.length - 1;
|
|
805
|
+
while (left < right) {
|
|
806
|
+
const mid = Math.floor((left + right) / 2);
|
|
807
|
+
const value = cdf[mid];
|
|
808
|
+
if (value === void 0) {
|
|
809
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("Internal error: invalid CDF index", "mid", mid);
|
|
810
|
+
}
|
|
811
|
+
if (u <= value) {
|
|
812
|
+
right = mid;
|
|
813
|
+
} else {
|
|
814
|
+
left = mid + 1;
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
return left;
|
|
818
|
+
}
|
|
819
|
+
function choice(a, size, replace = true, p) {
|
|
820
|
+
if (typeof a === "number") {
|
|
821
|
+
assertSafeInteger(a, "a");
|
|
822
|
+
if (a < 0) {
|
|
823
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("Invalid population size", "a", a);
|
|
824
|
+
}
|
|
825
|
+
if (a > INT32_MAX + 1) {
|
|
826
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError(
|
|
827
|
+
`Population size must be <= ${INT32_MAX + 1} for choice()`,
|
|
828
|
+
"a",
|
|
829
|
+
a
|
|
830
|
+
);
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
const aa = typeof a === "number" ? chunk6AE5FKKQ_cjs.arange(0, a, 1, { dtype: "int32" }) : a;
|
|
834
|
+
if (aa.dtype === "string") {
|
|
835
|
+
throw new chunkJSCDE774_cjs.DTypeError("choice() does not support string tensors");
|
|
836
|
+
}
|
|
837
|
+
const n = aa.size;
|
|
838
|
+
if (!Number.isInteger(n) || n < 0) {
|
|
839
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("Invalid tensor size", "n", n);
|
|
840
|
+
}
|
|
841
|
+
if (n > INT32_MAX + 1) {
|
|
842
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError(`Population size must be <= ${INT32_MAX + 1}`, "n", n);
|
|
843
|
+
}
|
|
844
|
+
let outputSize;
|
|
845
|
+
if (typeof size === "number") {
|
|
846
|
+
outputSize = size;
|
|
847
|
+
} else if (size) {
|
|
848
|
+
chunkJSCDE774_cjs.validateShape(size);
|
|
849
|
+
outputSize = chunkJSCDE774_cjs.shapeToSize(size);
|
|
850
|
+
} else {
|
|
851
|
+
outputSize = 1;
|
|
852
|
+
}
|
|
853
|
+
if (!Number.isSafeInteger(outputSize) || outputSize < 0) {
|
|
854
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("size must be an integer >= 0", "size", size);
|
|
855
|
+
}
|
|
856
|
+
if (outputSize > INT32_MAX) {
|
|
857
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError(`size must be <= ${INT32_MAX}`, "size", outputSize);
|
|
858
|
+
}
|
|
859
|
+
if (n === 0 && outputSize > 0) {
|
|
860
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("Cannot sample from an empty population", "a", a);
|
|
861
|
+
}
|
|
862
|
+
const indices = new Int32Array(outputSize);
|
|
863
|
+
const weights = p ? buildNormalizedProbabilities(p, n) : void 0;
|
|
864
|
+
if (weights) {
|
|
865
|
+
if (replace) {
|
|
866
|
+
const cdf = new Float64Array(weights.length);
|
|
867
|
+
let cumulative = 0;
|
|
868
|
+
for (let i = 0; i < weights.length; i++) {
|
|
869
|
+
cumulative += weights[i] ?? 0;
|
|
870
|
+
cdf[i] = cumulative;
|
|
871
|
+
}
|
|
872
|
+
cdf[cdf.length - 1] = 1;
|
|
873
|
+
for (let i = 0; i < outputSize; i++) {
|
|
874
|
+
indices[i] = sampleFromCdf(cdf);
|
|
875
|
+
}
|
|
876
|
+
} else {
|
|
877
|
+
let nonZeroCount = 0;
|
|
878
|
+
for (let i = 0; i < weights.length; i++) {
|
|
879
|
+
if ((weights[i] ?? 0) > 0) nonZeroCount++;
|
|
880
|
+
}
|
|
881
|
+
if (outputSize > nonZeroCount) {
|
|
882
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError(
|
|
883
|
+
"Cannot sample without replacement with zero-probability mass for requested size",
|
|
884
|
+
"size",
|
|
885
|
+
outputSize
|
|
886
|
+
);
|
|
887
|
+
}
|
|
888
|
+
const remaining = new Float64Array(weights);
|
|
889
|
+
let remainingMass = 1;
|
|
890
|
+
for (let i = 0; i < outputSize; i++) {
|
|
891
|
+
if (remainingMass <= 0) {
|
|
892
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError(
|
|
893
|
+
"Insufficient probability mass to sample",
|
|
894
|
+
"p",
|
|
895
|
+
remainingMass
|
|
896
|
+
);
|
|
897
|
+
}
|
|
898
|
+
const u = __random() * remainingMass;
|
|
899
|
+
let cumulative = 0;
|
|
900
|
+
let chosen = -1;
|
|
901
|
+
for (let j = 0; j < remaining.length; j++) {
|
|
902
|
+
const w = remaining[j] ?? 0;
|
|
903
|
+
if (w <= 0) {
|
|
904
|
+
continue;
|
|
905
|
+
}
|
|
906
|
+
cumulative += w;
|
|
907
|
+
if (u <= cumulative) {
|
|
908
|
+
chosen = j;
|
|
909
|
+
break;
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
if (chosen < 0) {
|
|
913
|
+
for (let j = remaining.length - 1; j >= 0; j--) {
|
|
914
|
+
if ((remaining[j] ?? 0) > 0) {
|
|
915
|
+
chosen = j;
|
|
916
|
+
break;
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
if (chosen < 0) {
|
|
921
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("Failed to select weighted sample", "p", weights);
|
|
922
|
+
}
|
|
923
|
+
indices[i] = chosen;
|
|
924
|
+
remainingMass -= remaining[chosen] ?? 0;
|
|
925
|
+
remaining[chosen] = 0;
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
} else if (replace) {
|
|
929
|
+
for (let i = 0; i < outputSize; i++) {
|
|
930
|
+
indices[i] = randomIntBelow(n);
|
|
931
|
+
}
|
|
932
|
+
} else {
|
|
933
|
+
if (outputSize > n) {
|
|
934
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError(
|
|
935
|
+
"Cannot sample without replacement when size > population",
|
|
936
|
+
"size",
|
|
937
|
+
outputSize
|
|
938
|
+
);
|
|
939
|
+
}
|
|
940
|
+
const pool = Array.from({ length: n }, (_, i) => i);
|
|
941
|
+
for (let i = 0; i < outputSize; i++) {
|
|
942
|
+
const j = randomIntBelow(n - i) + i;
|
|
943
|
+
const poolI = pool[i];
|
|
944
|
+
const poolJ = pool[j];
|
|
945
|
+
if (poolI === void 0 || poolJ === void 0) {
|
|
946
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("Internal error: pool index out of bounds", "pool", {
|
|
947
|
+
i,
|
|
948
|
+
j,
|
|
949
|
+
n
|
|
950
|
+
});
|
|
951
|
+
}
|
|
952
|
+
pool[i] = poolJ;
|
|
953
|
+
pool[j] = poolI;
|
|
954
|
+
indices[i] = poolJ;
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
const outputShape = typeof size === "number" ? [size] : size ?? [1];
|
|
958
|
+
validateContiguous(aa, "choice()");
|
|
959
|
+
const out = allocateNumericBuffer(aa.dtype, outputSize);
|
|
960
|
+
for (let i = 0; i < outputSize; i++) {
|
|
961
|
+
const idx = indices[i];
|
|
962
|
+
if (idx === void 0) {
|
|
963
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("Internal error: undefined index", "indices", i);
|
|
964
|
+
}
|
|
965
|
+
const value = readNumericTensorValue(aa, idx);
|
|
966
|
+
writeNumericValue(out, i, value);
|
|
967
|
+
}
|
|
968
|
+
return chunk6AE5FKKQ_cjs.Tensor.fromTypedArray({
|
|
969
|
+
data: out,
|
|
970
|
+
shape: outputShape,
|
|
971
|
+
dtype: aa.dtype,
|
|
972
|
+
device: aa.device
|
|
973
|
+
});
|
|
974
|
+
}
|
|
975
|
+
function shuffle(x) {
|
|
976
|
+
if (x.dtype === "string") {
|
|
977
|
+
throw new chunkJSCDE774_cjs.DTypeError("shuffle() does not support string tensors");
|
|
978
|
+
}
|
|
979
|
+
validateContiguous(x, "shuffle()");
|
|
980
|
+
const data = x.data;
|
|
981
|
+
if (!chunkJSCDE774_cjs.isTypedArray(data)) {
|
|
982
|
+
throw new chunkJSCDE774_cjs.DTypeError("shuffle() does not support string tensors");
|
|
983
|
+
}
|
|
984
|
+
const n = data.length;
|
|
985
|
+
if (data instanceof BigInt64Array) {
|
|
986
|
+
for (let i = n - 1; i > 0; i--) {
|
|
987
|
+
const j = randomIntBelow(i + 1);
|
|
988
|
+
const temp = data[i];
|
|
989
|
+
const swap = data[j];
|
|
990
|
+
if (temp === void 0 || swap === void 0) {
|
|
991
|
+
throw new chunkJSCDE774_cjs.DeepboxError("Internal error: shuffle index out of bounds");
|
|
992
|
+
}
|
|
993
|
+
data[i] = swap;
|
|
994
|
+
data[j] = temp;
|
|
995
|
+
}
|
|
996
|
+
} else {
|
|
997
|
+
for (let i = n - 1; i > 0; i--) {
|
|
998
|
+
const j = randomIntBelow(i + 1);
|
|
999
|
+
const temp = data[i];
|
|
1000
|
+
const swap = data[j];
|
|
1001
|
+
if (temp === void 0 || swap === void 0) {
|
|
1002
|
+
throw new chunkJSCDE774_cjs.DeepboxError("Internal error: shuffle index out of bounds");
|
|
1003
|
+
}
|
|
1004
|
+
data[i] = swap;
|
|
1005
|
+
data[j] = temp;
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
function permutation(x) {
|
|
1010
|
+
if (typeof x === "number") {
|
|
1011
|
+
assertSafeInteger(x, "x");
|
|
1012
|
+
const n = x;
|
|
1013
|
+
if (n < 0) {
|
|
1014
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("x must be a non-negative integer", "x", x);
|
|
1015
|
+
}
|
|
1016
|
+
if (n > INT32_MAX + 1) {
|
|
1017
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError(`x must be <= ${INT32_MAX + 1} for int32 output`, "x", x);
|
|
1018
|
+
}
|
|
1019
|
+
const indices = Array.from({ length: n }, (_, i) => i);
|
|
1020
|
+
for (let i = n - 1; i > 0; i--) {
|
|
1021
|
+
const j = randomIntBelow(i + 1);
|
|
1022
|
+
const indicesI = indices[i];
|
|
1023
|
+
const indicesJ = indices[j];
|
|
1024
|
+
if (indicesI === void 0 || indicesJ === void 0) {
|
|
1025
|
+
throw new chunkJSCDE774_cjs.InvalidParameterError("Internal error: indices out of bounds", "indices", {
|
|
1026
|
+
i,
|
|
1027
|
+
j,
|
|
1028
|
+
n
|
|
1029
|
+
});
|
|
1030
|
+
}
|
|
1031
|
+
indices[i] = indicesJ;
|
|
1032
|
+
indices[j] = indicesI;
|
|
1033
|
+
}
|
|
1034
|
+
return chunk6AE5FKKQ_cjs.tensor(indices, { dtype: "int32" });
|
|
1035
|
+
}
|
|
1036
|
+
if (x.dtype === "string") {
|
|
1037
|
+
throw new chunkJSCDE774_cjs.DTypeError("permutation() does not support string tensors");
|
|
1038
|
+
}
|
|
1039
|
+
validateContiguous(x, "permutation()");
|
|
1040
|
+
const data = x.data;
|
|
1041
|
+
if (!chunkJSCDE774_cjs.isTypedArray(data)) {
|
|
1042
|
+
throw new chunkJSCDE774_cjs.DTypeError("permutation() does not support string tensors");
|
|
1043
|
+
}
|
|
1044
|
+
const copy = chunk6AE5FKKQ_cjs.Tensor.fromTypedArray({
|
|
1045
|
+
data: data.slice(),
|
|
1046
|
+
shape: x.shape,
|
|
1047
|
+
dtype: x.dtype,
|
|
1048
|
+
device: x.device
|
|
1049
|
+
});
|
|
1050
|
+
shuffle(copy);
|
|
1051
|
+
return copy;
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
exports.beta = beta;
|
|
1055
|
+
exports.binomial = binomial;
|
|
1056
|
+
exports.choice = choice;
|
|
1057
|
+
exports.clearSeed = clearSeed;
|
|
1058
|
+
exports.exponential = exponential;
|
|
1059
|
+
exports.gamma = gamma;
|
|
1060
|
+
exports.getSeed = getSeed;
|
|
1061
|
+
exports.normal = normal;
|
|
1062
|
+
exports.permutation = permutation;
|
|
1063
|
+
exports.poisson = poisson;
|
|
1064
|
+
exports.rand = rand;
|
|
1065
|
+
exports.randint = randint;
|
|
1066
|
+
exports.randn = randn;
|
|
1067
|
+
exports.random_exports = random_exports;
|
|
1068
|
+
exports.setSeed = setSeed;
|
|
1069
|
+
exports.shuffle = shuffle;
|
|
1070
|
+
exports.uniform = uniform;
|
|
1071
|
+
//# sourceMappingURL=chunk-PHV2DKRS.cjs.map
|
|
1072
|
+
//# sourceMappingURL=chunk-PHV2DKRS.cjs.map
|