@iyulab/u-numflow 0.6.4 → 0.8.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/README.md CHANGED
@@ -34,7 +34,7 @@ u-numflow provides foundational mathematical, statistical, and probabilistic bui
34
34
 
35
35
  ```toml
36
36
  [dependencies]
37
- u-numflow = "0.2"
37
+ u-numflow = "0.8"
38
38
  ```
39
39
 
40
40
  ```rust
@@ -103,12 +103,23 @@ The package resolves per environment via a conditional `exports` map:
103
103
  | Bundlers (webpack, Vite, …) | ESM + WebAssembly ESM-integration (`default` condition) |
104
104
  | Node.js — `require()`, ESM `import`, CJS TS runners (`tsx`, `ts-node`) | CJS glue loading the wasm from the filesystem (`node` condition) — no loader hooks or flags |
105
105
 
106
+ A browser **without** a bundler is not supported: the package loads its `.wasm`
107
+ file with an ES module import, which browsers refuse (`application/wasm` is not a
108
+ module script type), so `<script type="module">` from a CDN fails, and CDN
109
+ re-bundling services fail on the same import. Use a bundler or Node.
110
+
106
111
  Exported functions: `mean`, `std_dev`, `variance`, `normal_cdf`, `normal_sf` (upper tail `P(Z > x)`, computed directly so tail probabilities keep ~15 significant digits), `box_cox`, `estimate_lambda` (returns `{ lambda, at_bound }` — `at_bound` is `true` when the likelihood was still rising at an end of the search range, so `lambda` is that limit), and
107
112
  `rfft(data) -> Float64Array` — the DFT of a real sequence of any length, interleaved as
108
113
  `[re0, im0, re1, im1, …]` (bins `k` and `n − k` are conjugates, so `0..=n/2` describes the spectrum).
109
114
 
115
+ Every `data` argument is a `number[]` or a `Float64Array`, read exactly as sent: an
116
+ element that is not a number (`null`, `undefined`, a string) throws `malformed_input`
117
+ and a NaN or ±Infinity throws `value_not_finite`, each with the element's `index` —
118
+ `mean([1, null, 3])` throws rather than averaging the `null` as 0. `mean` of no values
119
+ throws `empty_input`; `std_dev` and `variance` of fewer than 2 throw `insufficient_data`.
120
+
110
121
  Distribution functions for critical values and p-values — each returns a `number` and
111
- **throws** (a message naming the argument) when an argument is outside the domain,
122
+ **throws** (an `Error` naming the argument — see *Errors* below) when an argument is outside the domain,
112
123
  rather than returning `NaN`:
113
124
 
114
125
  | Function | Returns |
@@ -126,6 +137,29 @@ const { t_distribution_quantile } = require("@iyulab/u-numflow");
126
137
  t_distribution_quantile(0.975, 10); // 2.2281…
127
138
  ```
128
139
 
140
+ **Errors.** A refusal throws an `Error` whose `message` is readable text and which
141
+ carries a `code` naming the reason, next to the values behind it:
142
+
143
+ ```js
144
+ const { t_distribution_quantile } = require("@iyulab/u-numflow");
145
+ try {
146
+ t_distribution_quantile(1.5, 10);
147
+ } catch (err) {
148
+ console.log(err.code, err.parameter, err.min, err.max, err.got); // parameter_out_of_range p 0 1 1.5
149
+ }
150
+ ```
151
+
152
+ | `code` | Fields | Meaning |
153
+ |---|---|---|
154
+ | `parameter_out_of_range` | `parameter`, `min`, `max` (or `null`), `got` | `p` not strictly inside (0, 1), or a degrees of freedom that is not a finite number `> 0` (both bounds excluded) |
155
+ | `malformed_input` | `parameter`, `index` (or absent) | A `data` argument that is not an array or `Float64Array`, or an element that is not a number |
156
+ | `value_not_finite` | `parameter`, `index` for an array element | A NaN argument, or a NaN or infinity in any `data` array |
157
+ | `empty_input` | `parameter` | `mean` of no values |
158
+ | `non_positive_data` | — | Box-Cox data with a value `≤ 0` |
159
+ | `insufficient_data` | `parameter`, `min`, `got` for `std_dev`/`variance`; — for Box-Cox | Fewer values than the function needs (`std_dev`/`variance` 2, Box-Cox 2) |
160
+ | `invalid_transform` | — | A Box-Cox result that is not finite |
161
+ | `invalid_lambda_range` | — | `estimate_lambda` bounds that are not finite with `lambda_min < lambda_max` |
162
+
129
163
  ### TypeScript
130
164
 
131
165
  Every exported function declares its parameter and return types, and the
@@ -133,17 +167,18 @@ declarations are generated from the same structs the binding reads and
133
167
  serialises, so they cannot drift from what it actually accepts and returns:
134
168
 
135
169
  ```ts
136
- export function estimate_lambda(data: Float64Array, lambda_min: number, lambda_max: number): LambdaEstimateDto;
170
+ export function mean(data: number[] | Float64Array): number;
171
+ export function estimate_lambda(data: number[] | Float64Array, lambda_min: number, lambda_max: number): LambdaEstimateDto;
137
172
  ```
138
173
 
139
174
  An absent optional value is declared `T | undefined`, which is what the binding
140
175
  sends. Nothing needs an `as` cast -- and a wrong assumption about a result's
141
176
  shape is a compile error rather than something that fails at run time.
142
177
 
143
- Every parameter is a number or a `Float64Array`, so there is nothing to
144
- declare beyond what wasm-bindgen already does; the publishing workflow keeps it
145
- that way. The binding still validates every input at the boundary, and a
146
- rejected one says what was wrong.
178
+ A `data` parameter is declared `number[] | Float64Array` and read by the
179
+ binding itself rather than copied into a typed array by the generated glue, so
180
+ a value the declaration does not allow is refused where it sits instead of
181
+ being converted. The publishing workflow keeps every declaration free of `any`.
147
182
 
148
183
  ## Related
149
184
 
@@ -6,22 +6,21 @@
6
6
  * All values in `data` must be strictly positive.
7
7
  *
8
8
  * # Errors
9
- * Returns a `JsValue` error string if data contains non-positive values or
10
- * has fewer than 2 elements.
11
- * @param {Float64Array} data
9
+ * Throws `non_positive_data`, `value_not_finite` or `insufficient_data` when
10
+ * data contains a value ≤ 0, a NaN or infinity, or fewer than 2 elements, and
11
+ * `invalid_transform` when a result is not finite.
12
+ * @param {number[] | Float64Array} data
12
13
  * @param {number} lambda
13
14
  * @returns {Float64Array}
14
15
  */
15
16
  function box_cox(data, lambda) {
16
- const ptr0 = passArrayF64ToWasm0(data, wasm.__wbindgen_malloc);
17
- const len0 = WASM_VECTOR_LEN;
18
- const ret = wasm.box_cox(ptr0, len0, lambda);
17
+ const ret = wasm.box_cox(data, lambda);
19
18
  if (ret[3]) {
20
19
  throw takeFromExternrefTable0(ret[2]);
21
20
  }
22
- var v2 = getArrayF64FromWasm0(ret[0], ret[1]).slice();
21
+ var v1 = getArrayF64FromWasm0(ret[0], ret[1]).slice();
23
22
  wasm.__wbindgen_free(ret[0], ret[1] * 8, 8);
24
- return v2;
23
+ return v1;
25
24
  }
26
25
  exports.box_cox = box_cox;
27
26
 
@@ -72,18 +71,15 @@ exports.chi_squared_quantile = chi_squared_quantile;
72
71
  * interior estimate; widen the range to find the unconstrained optimum.
73
72
  *
74
73
  * # Errors
75
- * Returns a `JsValue` error string if data contains non-positive values,
76
- * has fewer than 2 elements, or the range is not finite with
77
- * `lambda_min < lambda_max`.
78
- * @param {Float64Array} data
74
+ * Throws as [`box_cox`] does for the data, and `invalid_lambda_range` when the
75
+ * range is not finite with `lambda_min < lambda_max`.
76
+ * @param {number[] | Float64Array} data
79
77
  * @param {number} lambda_min
80
78
  * @param {number} lambda_max
81
79
  * @returns {LambdaEstimateDto}
82
80
  */
83
81
  function estimate_lambda(data, lambda_min, lambda_max) {
84
- const ptr0 = passArrayF64ToWasm0(data, wasm.__wbindgen_malloc);
85
- const len0 = WASM_VECTOR_LEN;
86
- const ret = wasm.estimate_lambda(ptr0, len0, lambda_min, lambda_max);
82
+ const ret = wasm.estimate_lambda(data, lambda_min, lambda_max);
87
83
  if (ret[2]) {
88
84
  throw takeFromExternrefTable0(ret[1]);
89
85
  }
@@ -150,15 +146,18 @@ exports.inverse_normal_cdf = inverse_normal_cdf;
150
146
  /**
151
147
  * Arithmetic mean of `data` using Kahan compensated summation.
152
148
  *
153
- * Returns `NaN` if `data` is empty or contains non-finite values.
154
- * @param {Float64Array} data
149
+ * # Errors
150
+ * Throws `empty_input` when `data` is empty, and as every `data` argument
151
+ * does for an element that is not a finite number.
152
+ * @param {number[] | Float64Array} data
155
153
  * @returns {number}
156
154
  */
157
155
  function mean(data) {
158
- const ptr0 = passArrayF64ToWasm0(data, wasm.__wbindgen_malloc);
159
- const len0 = WASM_VECTOR_LEN;
160
- const ret = wasm.mean(ptr0, len0);
161
- return ret;
156
+ const ret = wasm.mean(data);
157
+ if (ret[2]) {
158
+ throw takeFromExternrefTable0(ret[1]);
159
+ }
160
+ return ret[0];
162
161
  }
163
162
  exports.mean = mean;
164
163
 
@@ -199,31 +198,38 @@ exports.normal_sf = normal_sf;
199
198
  * Returns the `n` complex bins interleaved as `[re0, im0, re1, im1, …]`
200
199
  * (length `2n`). Bins `k` and `n − k` are conjugates, so the spectrum of a
201
200
  * real signal is fully described by bins `0..=n/2`.
202
- * @param {Float64Array} data
201
+ *
202
+ * # Errors
203
+ * Throws as every `data` argument does for an element that is not a finite
204
+ * number.
205
+ * @param {number[] | Float64Array} data
203
206
  * @returns {Float64Array}
204
207
  */
205
208
  function rfft(data) {
206
- const ptr0 = passArrayF64ToWasm0(data, wasm.__wbindgen_malloc);
207
- const len0 = WASM_VECTOR_LEN;
208
- const ret = wasm.rfft(ptr0, len0);
209
- var v2 = getArrayF64FromWasm0(ret[0], ret[1]).slice();
209
+ const ret = wasm.rfft(data);
210
+ if (ret[3]) {
211
+ throw takeFromExternrefTable0(ret[2]);
212
+ }
213
+ var v1 = getArrayF64FromWasm0(ret[0], ret[1]).slice();
210
214
  wasm.__wbindgen_free(ret[0], ret[1] * 8, 8);
211
- return v2;
215
+ return v1;
212
216
  }
213
217
  exports.rfft = rfft;
214
218
 
215
219
  /**
216
220
  * Sample standard deviation of `data` (Bessel-corrected, denominator n−1).
217
221
  *
218
- * Returns `NaN` if `data` has fewer than 2 elements or contains non-finite values.
219
- * @param {Float64Array} data
222
+ * # Errors
223
+ * Throws `insufficient_data` below 2 values.
224
+ * @param {number[] | Float64Array} data
220
225
  * @returns {number}
221
226
  */
222
227
  function std_dev(data) {
223
- const ptr0 = passArrayF64ToWasm0(data, wasm.__wbindgen_malloc);
224
- const len0 = WASM_VECTOR_LEN;
225
- const ret = wasm.std_dev(ptr0, len0);
226
- return ret;
228
+ const ret = wasm.std_dev(data);
229
+ if (ret[2]) {
230
+ throw takeFromExternrefTable0(ret[1]);
231
+ }
232
+ return ret[0];
227
233
  }
228
234
  exports.std_dev = std_dev;
229
235
 
@@ -268,15 +274,17 @@ exports.t_distribution_quantile = t_distribution_quantile;
268
274
  /**
269
275
  * Sample variance of `data` (Bessel-corrected, denominator n−1).
270
276
  *
271
- * Returns `NaN` if `data` has fewer than 2 elements or contains non-finite values.
272
- * @param {Float64Array} data
277
+ * # Errors
278
+ * Throws `insufficient_data` below 2 values.
279
+ * @param {number[] | Float64Array} data
273
280
  * @returns {number}
274
281
  */
275
282
  function variance(data) {
276
- const ptr0 = passArrayF64ToWasm0(data, wasm.__wbindgen_malloc);
277
- const len0 = WASM_VECTOR_LEN;
278
- const ret = wasm.variance(ptr0, len0);
279
- return ret;
283
+ const ret = wasm.variance(data);
284
+ if (ret[2]) {
285
+ throw takeFromExternrefTable0(ret[1]);
286
+ }
287
+ return ret[0];
280
288
  }
281
289
  exports.variance = variance;
282
290
  function __wbg_get_imports() {
@@ -289,13 +297,72 @@ function __wbg_get_imports() {
289
297
  getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
290
298
  getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
291
299
  },
300
+ __wbg___wbindgen_is_null_e343b7d08827ba72: function(arg0) {
301
+ const ret = arg0 === null;
302
+ return ret;
303
+ },
304
+ __wbg___wbindgen_number_get_2e0e7dee9f701a71: function(arg0, arg1) {
305
+ const obj = arg1;
306
+ const ret = typeof(obj) === 'number' ? obj : undefined;
307
+ getDataViewMemory0().setFloat64(arg0 + 8 * 1, isLikeNone(ret) ? 0 : ret, true);
308
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, !isLikeNone(ret), true);
309
+ },
310
+ __wbg___wbindgen_string_get_0380ccaa2f57f0d9: function(arg0, arg1) {
311
+ const obj = arg1;
312
+ const ret = typeof(obj) === 'string' ? obj : undefined;
313
+ var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
314
+ var len1 = WASM_VECTOR_LEN;
315
+ getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
316
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
317
+ },
292
318
  __wbg___wbindgen_throw_41e9ee4f547fc59a: function(arg0, arg1) {
293
319
  throw new Error(getStringFromWasm0(arg0, arg1));
294
320
  },
321
+ __wbg___wbindgen_typeof_e777a26e115d416b: function(arg0) {
322
+ const ret = typeof arg0;
323
+ return ret;
324
+ },
325
+ __wbg_get_unchecked_288889d017702237: function(arg0, arg1) {
326
+ const ret = arg0[arg1 >>> 0];
327
+ return ret;
328
+ },
329
+ __wbg_instanceof_Float64Array_8a59377f9eec1b8b: function(arg0) {
330
+ let result;
331
+ try {
332
+ result = arg0 instanceof Float64Array;
333
+ } catch (_) {
334
+ result = false;
335
+ }
336
+ const ret = result;
337
+ return ret;
338
+ },
339
+ __wbg_isArray_e15a2ff68ffdbef2: function(arg0) {
340
+ const ret = Array.isArray(arg0);
341
+ return ret;
342
+ },
343
+ __wbg_length_b5f0008bbf60cf59: function(arg0) {
344
+ const ret = arg0.length;
345
+ return ret;
346
+ },
347
+ __wbg_length_d4bdea10311bd9cf: function(arg0) {
348
+ const ret = arg0.length;
349
+ return ret;
350
+ },
351
+ __wbg_new_343a093a3c2ffb4e: function(arg0, arg1) {
352
+ const ret = new Error(getStringFromWasm0(arg0, arg1));
353
+ return ret;
354
+ },
295
355
  __wbg_new_617a8cdb8bb1130e: function() {
296
356
  const ret = new Object();
297
357
  return ret;
298
358
  },
359
+ __wbg_prototypesetcall_d49a4fab5ca427bc: function(arg0, arg1, arg2) {
360
+ Float64Array.prototype.set.call(getArrayF64FromWasm0(arg0, arg1), arg2);
361
+ },
362
+ __wbg_set_145a351398b48c65: function() { return handleError(function (arg0, arg1, arg2) {
363
+ const ret = Reflect.set(arg0, arg1, arg2);
364
+ return ret;
365
+ }, arguments); },
299
366
  __wbg_set_6be42768c690e380: function(arg0, arg1, arg2) {
300
367
  arg0[arg1] = arg2;
301
368
  },
@@ -325,6 +392,12 @@ function __wbg_get_imports() {
325
392
  };
326
393
  }
327
394
 
395
+ function addToExternrefTable0(obj) {
396
+ const idx = wasm.__externref_table_alloc();
397
+ wasm.__wbindgen_externrefs.set(idx, obj);
398
+ return idx;
399
+ }
400
+
328
401
  function getArrayF64FromWasm0(ptr, len) {
329
402
  ptr = ptr >>> 0;
330
403
  return getFloat64ArrayMemory0().subarray(ptr / 8, ptr / 8 + len);
@@ -358,11 +431,17 @@ function getUint8ArrayMemory0() {
358
431
  return cachedUint8ArrayMemory0;
359
432
  }
360
433
 
361
- function passArrayF64ToWasm0(arg, malloc) {
362
- const ptr = malloc(arg.length * 8, 8) >>> 0;
363
- getFloat64ArrayMemory0().set(arg, ptr / 8);
364
- WASM_VECTOR_LEN = arg.length;
365
- return ptr;
434
+ function handleError(f, args) {
435
+ try {
436
+ return f.apply(this, args);
437
+ } catch (e) {
438
+ const idx = addToExternrefTable0(e);
439
+ wasm.__wbindgen_exn_store(idx);
440
+ }
441
+ }
442
+
443
+ function isLikeNone(x) {
444
+ return x === undefined || x === null;
366
445
  }
367
446
 
368
447
  function passStringToWasm0(arg, malloc, realloc) {
@@ -15,10 +15,11 @@ export interface LambdaEstimateDto {
15
15
  * All values in `data` must be strictly positive.
16
16
  *
17
17
  * # Errors
18
- * Returns a `JsValue` error string if data contains non-positive values or
19
- * has fewer than 2 elements.
18
+ * Throws `non_positive_data`, `value_not_finite` or `insufficient_data` when
19
+ * data contains a value ≤ 0, a NaN or infinity, or fewer than 2 elements, and
20
+ * `invalid_transform` when a result is not finite.
20
21
  */
21
- export function box_cox(data: Float64Array, lambda: number): Float64Array;
22
+ export function box_cox(data: number[] | Float64Array, lambda: number): Float64Array;
22
23
 
23
24
  /**
24
25
  * Chi-squared CDF: `P(X ≤ x)` for `X ~ χ²(k)` (`0` for `x ≤ 0`).
@@ -47,11 +48,10 @@ export function chi_squared_quantile(p: number, k: number): number;
47
48
  * interior estimate; widen the range to find the unconstrained optimum.
48
49
  *
49
50
  * # Errors
50
- * Returns a `JsValue` error string if data contains non-positive values,
51
- * has fewer than 2 elements, or the range is not finite with
52
- * `lambda_min < lambda_max`.
51
+ * Throws as [`box_cox`] does for the data, and `invalid_lambda_range` when the
52
+ * range is not finite with `lambda_min < lambda_max`.
53
53
  */
54
- export function estimate_lambda(data: Float64Array, lambda_min: number, lambda_max: number): LambdaEstimateDto;
54
+ export function estimate_lambda(data: number[] | Float64Array, lambda_min: number, lambda_max: number): LambdaEstimateDto;
55
55
 
56
56
  /**
57
57
  * F CDF: `P(X ≤ x)` for `X ~ F(df1, df2)` (`0` for `x ≤ 0`).
@@ -81,9 +81,11 @@ export function inverse_normal_cdf(p: number): number;
81
81
  /**
82
82
  * Arithmetic mean of `data` using Kahan compensated summation.
83
83
  *
84
- * Returns `NaN` if `data` is empty or contains non-finite values.
84
+ * # Errors
85
+ * Throws `empty_input` when `data` is empty, and as every `data` argument
86
+ * does for an element that is not a finite number.
85
87
  */
86
- export function mean(data: Float64Array): number;
88
+ export function mean(data: number[] | Float64Array): number;
87
89
 
88
90
  /**
89
91
  * Standard normal CDF Φ(x) = P(Z ≤ x) for Z ~ N(0, 1).
@@ -110,15 +112,20 @@ export function normal_sf(x: number): number;
110
112
  * Returns the `n` complex bins interleaved as `[re0, im0, re1, im1, …]`
111
113
  * (length `2n`). Bins `k` and `n − k` are conjugates, so the spectrum of a
112
114
  * real signal is fully described by bins `0..=n/2`.
115
+ *
116
+ * # Errors
117
+ * Throws as every `data` argument does for an element that is not a finite
118
+ * number.
113
119
  */
114
- export function rfft(data: Float64Array): Float64Array;
120
+ export function rfft(data: number[] | Float64Array): Float64Array;
115
121
 
116
122
  /**
117
123
  * Sample standard deviation of `data` (Bessel-corrected, denominator n−1).
118
124
  *
119
- * Returns `NaN` if `data` has fewer than 2 elements or contains non-finite values.
125
+ * # Errors
126
+ * Throws `insufficient_data` below 2 values.
120
127
  */
121
- export function std_dev(data: Float64Array): number;
128
+ export function std_dev(data: number[] | Float64Array): number;
122
129
 
123
130
  /**
124
131
  * Student's t CDF: `P(T ≤ t)` for `T ~ t(df)`. `df` may be fractional.
@@ -141,6 +148,7 @@ export function t_distribution_quantile(p: number, df: number): number;
141
148
  /**
142
149
  * Sample variance of `data` (Bessel-corrected, denominator n−1).
143
150
  *
144
- * Returns `NaN` if `data` has fewer than 2 elements or contains non-finite values.
151
+ * # Errors
152
+ * Throws `insufficient_data` below 2 values.
145
153
  */
146
- export function variance(data: Float64Array): number;
154
+ export function variance(data: number[] | Float64Array): number;
Binary file
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "iyulab"
6
6
  ],
7
7
  "description": "Foundational mathematical primitives: statistics, probability distributions, random sampling, and collections.",
8
- "version": "0.6.4",
8
+ "version": "0.8.0",
9
9
  "license": "MIT",
10
10
  "repository": {
11
11
  "type": "git",
package/u_numflow.d.ts CHANGED
@@ -15,10 +15,11 @@ export interface LambdaEstimateDto {
15
15
  * All values in `data` must be strictly positive.
16
16
  *
17
17
  * # Errors
18
- * Returns a `JsValue` error string if data contains non-positive values or
19
- * has fewer than 2 elements.
18
+ * Throws `non_positive_data`, `value_not_finite` or `insufficient_data` when
19
+ * data contains a value ≤ 0, a NaN or infinity, or fewer than 2 elements, and
20
+ * `invalid_transform` when a result is not finite.
20
21
  */
21
- export function box_cox(data: Float64Array, lambda: number): Float64Array;
22
+ export function box_cox(data: number[] | Float64Array, lambda: number): Float64Array;
22
23
 
23
24
  /**
24
25
  * Chi-squared CDF: `P(X ≤ x)` for `X ~ χ²(k)` (`0` for `x ≤ 0`).
@@ -47,11 +48,10 @@ export function chi_squared_quantile(p: number, k: number): number;
47
48
  * interior estimate; widen the range to find the unconstrained optimum.
48
49
  *
49
50
  * # Errors
50
- * Returns a `JsValue` error string if data contains non-positive values,
51
- * has fewer than 2 elements, or the range is not finite with
52
- * `lambda_min < lambda_max`.
51
+ * Throws as [`box_cox`] does for the data, and `invalid_lambda_range` when the
52
+ * range is not finite with `lambda_min < lambda_max`.
53
53
  */
54
- export function estimate_lambda(data: Float64Array, lambda_min: number, lambda_max: number): LambdaEstimateDto;
54
+ export function estimate_lambda(data: number[] | Float64Array, lambda_min: number, lambda_max: number): LambdaEstimateDto;
55
55
 
56
56
  /**
57
57
  * F CDF: `P(X ≤ x)` for `X ~ F(df1, df2)` (`0` for `x ≤ 0`).
@@ -81,9 +81,11 @@ export function inverse_normal_cdf(p: number): number;
81
81
  /**
82
82
  * Arithmetic mean of `data` using Kahan compensated summation.
83
83
  *
84
- * Returns `NaN` if `data` is empty or contains non-finite values.
84
+ * # Errors
85
+ * Throws `empty_input` when `data` is empty, and as every `data` argument
86
+ * does for an element that is not a finite number.
85
87
  */
86
- export function mean(data: Float64Array): number;
88
+ export function mean(data: number[] | Float64Array): number;
87
89
 
88
90
  /**
89
91
  * Standard normal CDF Φ(x) = P(Z ≤ x) for Z ~ N(0, 1).
@@ -110,15 +112,20 @@ export function normal_sf(x: number): number;
110
112
  * Returns the `n` complex bins interleaved as `[re0, im0, re1, im1, …]`
111
113
  * (length `2n`). Bins `k` and `n − k` are conjugates, so the spectrum of a
112
114
  * real signal is fully described by bins `0..=n/2`.
115
+ *
116
+ * # Errors
117
+ * Throws as every `data` argument does for an element that is not a finite
118
+ * number.
113
119
  */
114
- export function rfft(data: Float64Array): Float64Array;
120
+ export function rfft(data: number[] | Float64Array): Float64Array;
115
121
 
116
122
  /**
117
123
  * Sample standard deviation of `data` (Bessel-corrected, denominator n−1).
118
124
  *
119
- * Returns `NaN` if `data` has fewer than 2 elements or contains non-finite values.
125
+ * # Errors
126
+ * Throws `insufficient_data` below 2 values.
120
127
  */
121
- export function std_dev(data: Float64Array): number;
128
+ export function std_dev(data: number[] | Float64Array): number;
122
129
 
123
130
  /**
124
131
  * Student's t CDF: `P(T ≤ t)` for `T ~ t(df)`. `df` may be fractional.
@@ -141,6 +148,7 @@ export function t_distribution_quantile(p: number, df: number): number;
141
148
  /**
142
149
  * Sample variance of `data` (Bessel-corrected, denominator n−1).
143
150
  *
144
- * Returns `NaN` if `data` has fewer than 2 elements or contains non-finite values.
151
+ * # Errors
152
+ * Throws `insufficient_data` below 2 values.
145
153
  */
146
- export function variance(data: Float64Array): number;
154
+ export function variance(data: number[] | Float64Array): number;
package/u_numflow_bg.js CHANGED
@@ -4,22 +4,21 @@
4
4
  * All values in `data` must be strictly positive.
5
5
  *
6
6
  * # Errors
7
- * Returns a `JsValue` error string if data contains non-positive values or
8
- * has fewer than 2 elements.
9
- * @param {Float64Array} data
7
+ * Throws `non_positive_data`, `value_not_finite` or `insufficient_data` when
8
+ * data contains a value ≤ 0, a NaN or infinity, or fewer than 2 elements, and
9
+ * `invalid_transform` when a result is not finite.
10
+ * @param {number[] | Float64Array} data
10
11
  * @param {number} lambda
11
12
  * @returns {Float64Array}
12
13
  */
13
14
  export function box_cox(data, lambda) {
14
- const ptr0 = passArrayF64ToWasm0(data, wasm.__wbindgen_malloc);
15
- const len0 = WASM_VECTOR_LEN;
16
- const ret = wasm.box_cox(ptr0, len0, lambda);
15
+ const ret = wasm.box_cox(data, lambda);
17
16
  if (ret[3]) {
18
17
  throw takeFromExternrefTable0(ret[2]);
19
18
  }
20
- var v2 = getArrayF64FromWasm0(ret[0], ret[1]).slice();
19
+ var v1 = getArrayF64FromWasm0(ret[0], ret[1]).slice();
21
20
  wasm.__wbindgen_free(ret[0], ret[1] * 8, 8);
22
- return v2;
21
+ return v1;
23
22
  }
24
23
 
25
24
  /**
@@ -67,18 +66,15 @@ export function chi_squared_quantile(p, k) {
67
66
  * interior estimate; widen the range to find the unconstrained optimum.
68
67
  *
69
68
  * # Errors
70
- * Returns a `JsValue` error string if data contains non-positive values,
71
- * has fewer than 2 elements, or the range is not finite with
72
- * `lambda_min < lambda_max`.
73
- * @param {Float64Array} data
69
+ * Throws as [`box_cox`] does for the data, and `invalid_lambda_range` when the
70
+ * range is not finite with `lambda_min < lambda_max`.
71
+ * @param {number[] | Float64Array} data
74
72
  * @param {number} lambda_min
75
73
  * @param {number} lambda_max
76
74
  * @returns {LambdaEstimateDto}
77
75
  */
78
76
  export function estimate_lambda(data, lambda_min, lambda_max) {
79
- const ptr0 = passArrayF64ToWasm0(data, wasm.__wbindgen_malloc);
80
- const len0 = WASM_VECTOR_LEN;
81
- const ret = wasm.estimate_lambda(ptr0, len0, lambda_min, lambda_max);
77
+ const ret = wasm.estimate_lambda(data, lambda_min, lambda_max);
82
78
  if (ret[2]) {
83
79
  throw takeFromExternrefTable0(ret[1]);
84
80
  }
@@ -141,15 +137,18 @@ export function inverse_normal_cdf(p) {
141
137
  /**
142
138
  * Arithmetic mean of `data` using Kahan compensated summation.
143
139
  *
144
- * Returns `NaN` if `data` is empty or contains non-finite values.
145
- * @param {Float64Array} data
140
+ * # Errors
141
+ * Throws `empty_input` when `data` is empty, and as every `data` argument
142
+ * does for an element that is not a finite number.
143
+ * @param {number[] | Float64Array} data
146
144
  * @returns {number}
147
145
  */
148
146
  export function mean(data) {
149
- const ptr0 = passArrayF64ToWasm0(data, wasm.__wbindgen_malloc);
150
- const len0 = WASM_VECTOR_LEN;
151
- const ret = wasm.mean(ptr0, len0);
152
- return ret;
147
+ const ret = wasm.mean(data);
148
+ if (ret[2]) {
149
+ throw takeFromExternrefTable0(ret[1]);
150
+ }
151
+ return ret[0];
153
152
  }
154
153
 
155
154
  /**
@@ -187,30 +186,37 @@ export function normal_sf(x) {
187
186
  * Returns the `n` complex bins interleaved as `[re0, im0, re1, im1, …]`
188
187
  * (length `2n`). Bins `k` and `n − k` are conjugates, so the spectrum of a
189
188
  * real signal is fully described by bins `0..=n/2`.
190
- * @param {Float64Array} data
189
+ *
190
+ * # Errors
191
+ * Throws as every `data` argument does for an element that is not a finite
192
+ * number.
193
+ * @param {number[] | Float64Array} data
191
194
  * @returns {Float64Array}
192
195
  */
193
196
  export function rfft(data) {
194
- const ptr0 = passArrayF64ToWasm0(data, wasm.__wbindgen_malloc);
195
- const len0 = WASM_VECTOR_LEN;
196
- const ret = wasm.rfft(ptr0, len0);
197
- var v2 = getArrayF64FromWasm0(ret[0], ret[1]).slice();
197
+ const ret = wasm.rfft(data);
198
+ if (ret[3]) {
199
+ throw takeFromExternrefTable0(ret[2]);
200
+ }
201
+ var v1 = getArrayF64FromWasm0(ret[0], ret[1]).slice();
198
202
  wasm.__wbindgen_free(ret[0], ret[1] * 8, 8);
199
- return v2;
203
+ return v1;
200
204
  }
201
205
 
202
206
  /**
203
207
  * Sample standard deviation of `data` (Bessel-corrected, denominator n−1).
204
208
  *
205
- * Returns `NaN` if `data` has fewer than 2 elements or contains non-finite values.
206
- * @param {Float64Array} data
209
+ * # Errors
210
+ * Throws `insufficient_data` below 2 values.
211
+ * @param {number[] | Float64Array} data
207
212
  * @returns {number}
208
213
  */
209
214
  export function std_dev(data) {
210
- const ptr0 = passArrayF64ToWasm0(data, wasm.__wbindgen_malloc);
211
- const len0 = WASM_VECTOR_LEN;
212
- const ret = wasm.std_dev(ptr0, len0);
213
- return ret;
215
+ const ret = wasm.std_dev(data);
216
+ if (ret[2]) {
217
+ throw takeFromExternrefTable0(ret[1]);
218
+ }
219
+ return ret[0];
214
220
  }
215
221
 
216
222
  /**
@@ -252,15 +258,17 @@ export function t_distribution_quantile(p, df) {
252
258
  /**
253
259
  * Sample variance of `data` (Bessel-corrected, denominator n−1).
254
260
  *
255
- * Returns `NaN` if `data` has fewer than 2 elements or contains non-finite values.
256
- * @param {Float64Array} data
261
+ * # Errors
262
+ * Throws `insufficient_data` below 2 values.
263
+ * @param {number[] | Float64Array} data
257
264
  * @returns {number}
258
265
  */
259
266
  export function variance(data) {
260
- const ptr0 = passArrayF64ToWasm0(data, wasm.__wbindgen_malloc);
261
- const len0 = WASM_VECTOR_LEN;
262
- const ret = wasm.variance(ptr0, len0);
263
- return ret;
267
+ const ret = wasm.variance(data);
268
+ if (ret[2]) {
269
+ throw takeFromExternrefTable0(ret[1]);
270
+ }
271
+ return ret[0];
264
272
  }
265
273
  export function __wbg_String_8564e559799eccda(arg0, arg1) {
266
274
  const ret = String(arg1);
@@ -269,13 +277,72 @@ export function __wbg_String_8564e559799eccda(arg0, arg1) {
269
277
  getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
270
278
  getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
271
279
  }
280
+ export function __wbg___wbindgen_is_null_e343b7d08827ba72(arg0) {
281
+ const ret = arg0 === null;
282
+ return ret;
283
+ }
284
+ export function __wbg___wbindgen_number_get_2e0e7dee9f701a71(arg0, arg1) {
285
+ const obj = arg1;
286
+ const ret = typeof(obj) === 'number' ? obj : undefined;
287
+ getDataViewMemory0().setFloat64(arg0 + 8 * 1, isLikeNone(ret) ? 0 : ret, true);
288
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, !isLikeNone(ret), true);
289
+ }
290
+ export function __wbg___wbindgen_string_get_0380ccaa2f57f0d9(arg0, arg1) {
291
+ const obj = arg1;
292
+ const ret = typeof(obj) === 'string' ? obj : undefined;
293
+ var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
294
+ var len1 = WASM_VECTOR_LEN;
295
+ getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
296
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
297
+ }
272
298
  export function __wbg___wbindgen_throw_41e9ee4f547fc59a(arg0, arg1) {
273
299
  throw new Error(getStringFromWasm0(arg0, arg1));
274
300
  }
301
+ export function __wbg___wbindgen_typeof_e777a26e115d416b(arg0) {
302
+ const ret = typeof arg0;
303
+ return ret;
304
+ }
305
+ export function __wbg_get_unchecked_288889d017702237(arg0, arg1) {
306
+ const ret = arg0[arg1 >>> 0];
307
+ return ret;
308
+ }
309
+ export function __wbg_instanceof_Float64Array_8a59377f9eec1b8b(arg0) {
310
+ let result;
311
+ try {
312
+ result = arg0 instanceof Float64Array;
313
+ } catch (_) {
314
+ result = false;
315
+ }
316
+ const ret = result;
317
+ return ret;
318
+ }
319
+ export function __wbg_isArray_e15a2ff68ffdbef2(arg0) {
320
+ const ret = Array.isArray(arg0);
321
+ return ret;
322
+ }
323
+ export function __wbg_length_b5f0008bbf60cf59(arg0) {
324
+ const ret = arg0.length;
325
+ return ret;
326
+ }
327
+ export function __wbg_length_d4bdea10311bd9cf(arg0) {
328
+ const ret = arg0.length;
329
+ return ret;
330
+ }
331
+ export function __wbg_new_343a093a3c2ffb4e(arg0, arg1) {
332
+ const ret = new Error(getStringFromWasm0(arg0, arg1));
333
+ return ret;
334
+ }
275
335
  export function __wbg_new_617a8cdb8bb1130e() {
276
336
  const ret = new Object();
277
337
  return ret;
278
338
  }
339
+ export function __wbg_prototypesetcall_d49a4fab5ca427bc(arg0, arg1, arg2) {
340
+ Float64Array.prototype.set.call(getArrayF64FromWasm0(arg0, arg1), arg2);
341
+ }
342
+ export function __wbg_set_145a351398b48c65() { return handleError(function (arg0, arg1, arg2) {
343
+ const ret = Reflect.set(arg0, arg1, arg2);
344
+ return ret;
345
+ }, arguments); }
279
346
  export function __wbg_set_6be42768c690e380(arg0, arg1, arg2) {
280
347
  arg0[arg1] = arg2;
281
348
  }
@@ -298,6 +365,12 @@ export function __wbindgen_init_externref_table() {
298
365
  table.set(offset + 2, true);
299
366
  table.set(offset + 3, false);
300
367
  }
368
+ function addToExternrefTable0(obj) {
369
+ const idx = wasm.__externref_table_alloc();
370
+ wasm.__wbindgen_externrefs.set(idx, obj);
371
+ return idx;
372
+ }
373
+
301
374
  function getArrayF64FromWasm0(ptr, len) {
302
375
  ptr = ptr >>> 0;
303
376
  return getFloat64ArrayMemory0().subarray(ptr / 8, ptr / 8 + len);
@@ -331,11 +404,17 @@ function getUint8ArrayMemory0() {
331
404
  return cachedUint8ArrayMemory0;
332
405
  }
333
406
 
334
- function passArrayF64ToWasm0(arg, malloc) {
335
- const ptr = malloc(arg.length * 8, 8) >>> 0;
336
- getFloat64ArrayMemory0().set(arg, ptr / 8);
337
- WASM_VECTOR_LEN = arg.length;
338
- return ptr;
407
+ function handleError(f, args) {
408
+ try {
409
+ return f.apply(this, args);
410
+ } catch (e) {
411
+ const idx = addToExternrefTable0(e);
412
+ wasm.__wbindgen_exn_store(idx);
413
+ }
414
+ }
415
+
416
+ function isLikeNone(x) {
417
+ return x === undefined || x === null;
339
418
  }
340
419
 
341
420
  function passStringToWasm0(arg, malloc, realloc) {
package/u_numflow_bg.wasm CHANGED
Binary file