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