@iyulab/u-numflow 0.13.1 → 0.14.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
@@ -24,6 +24,7 @@ u-numflow provides foundational mathematical, statistical, and probabilistic bui
24
24
  | `random` | Seeded RNG, Fisher-Yates shuffle, weighted sampling, random subset selection |
25
25
  | `distributions::Sample` | Random variates from every continuous distribution — `sample(&mut rng)` and reproducible `sample_n(n, seed)`; inversion where the quantile is direct, Marsaglia–Tsang for the gamma family (Gamma, χ², Beta, PERT); uniforms from the open interval (0, 1), so no variate is infinite |
26
26
  | `collections` | Specialized data structures: Union-Find with path compression and union-by-rank; `IntervalSet` — unions of half-open intervals with union, intersection, difference, clip and a measure that counts overlapping input once |
27
+ | `units` | Units of measure: dimensional analysis over the seven SI base quantities, UCUM-syntax unit expressions (`kW.h`, `m/s2`, `J/(kg.K)`), conversion of readings (with a scale's offset — 20 °C is 293.15 K) and of differences (without — a rise of 20 °C is a rise of 20 K); the SI built in, every other unit defined by the caller |
27
28
 
28
29
  ## Design Philosophy
29
30
 
@@ -35,7 +36,7 @@ u-numflow provides foundational mathematical, statistical, and probabilistic bui
35
36
 
36
37
  ```toml
37
38
  [dependencies]
38
- u-numflow = "0.13"
39
+ u-numflow = "0.14"
39
40
  ```
40
41
 
41
42
  ```rust
@@ -86,6 +87,31 @@ let up = week.difference(&planned.union(&unplanned));
86
87
  assert_eq!(up.measure(), 138.0);
87
88
  ```
88
89
 
90
+ ```rust
91
+ // Units: the SI is built in; a unit whose size varies by convention is yours to define
92
+ use u_numflow::units::Units;
93
+ let mut units = Units::si();
94
+ units.define("cal_IT", 4.1868, "J", 0.0).unwrap(); // the international calorie
95
+ units.define("toe", 1e7, "kcal_IT", 0.0).unwrap(); // tonne of oil equivalent
96
+ assert!((units.convert(1.0, "toe", "GJ").unwrap() - 41.868).abs() < 1e-9);
97
+ assert!((units.convert(20.0, "Cel", "K").unwrap() - 293.15).abs() < 1e-9); // a reading
98
+ assert_eq!(units.convert_difference(20.0, "Cel", "K").unwrap(), 20.0); // a change
99
+ assert!(units.convert(1.0, "m3", "kg").is_err()); // volume is not mass
100
+ ```
101
+
102
+ Unit expressions follow UCUM's case-sensitive syntax: an atom with an optional
103
+ decimal prefix (`km`, `kW`, `uA` — `u` is micro), an integer exponent after it
104
+ (`m3`, `s-2`), `.` for a product and `/` for a quotient read left to right
105
+ (`kg/m.s` is `(kg/m)·s`), parentheses, integer factors (`1000.m`), and `{…}`
106
+ annotations, which have no effect (`m3{gas}`). Built in: the SI base units
107
+ (`m`, `g`, `s`, `A`, `K`, `mol`, `cd`), the 22 derived units with special names
108
+ (`N`, `Pa`, `J`, `W`, `Cel`, …), `min`, `h`, `d`, `L`/`l`, `t`, and the prefixes
109
+ `Q` … `q`. No unit table beyond the SI is embedded: the calorie (three
110
+ definitions, up to 0.07 % apart), the tonne of oil equivalent, imperial units
111
+ and the normal cubic metre are the caller's `define` calls, so the crate never
112
+ picks one convention silently. A scale with an offset (`Cel`) stands alone — a
113
+ prefix, exponent or product on it is refused.
114
+
89
115
  `IntervalSet<T>` works over `f32`, `f64` and every primitive integer type. Its
90
116
  measure has the type of a distance — the unsigned type of the same width for
91
117
  integers (as `i64::abs_diff` returns `u64`), so it cannot overflow. A reversed
@@ -210,6 +236,35 @@ const up = interval_difference([[0, 168]], down); // [[30, 168]]
210
236
  console.log(interval_measure(up)); // 138
211
237
  ```
212
238
 
239
+ Units of measure — `from`, `to` and `unit` are unit expressions (UCUM syntax, see the
240
+ Rust section above) over the SI and the optional `definitions`, an array of
241
+ `UnitDefinitionSpec` `{ symbol, unit, factor?, offset? }` (`symbol` = `factor` × `unit`,
242
+ plus `offset` SI units at its zero; `factor` defaults to 1, `offset` to 0; a definition
243
+ may use the ones before it):
244
+
245
+ | Function | Returns |
246
+ |---|---|
247
+ | `unit_convert(value, from, to, definitions?)` | A reading converted — 20 `Cel` is 293.15 `K` |
248
+ | `unit_convert_difference(value, from, to, definitions?)` | An amount of change converted — the offset cancels: a rise of 20 `Cel` is a rise of 20 `K` |
249
+ | `unit_info(unit, definitions?)` | `UnitInfo` `{ dimension, exponents, scale, offset }` — the dimension in SI base units (`"kg.m2.s-2"`; exponents of m, kg, s, A, K, mol, cd) and `value_in_si = value · scale + offset`. Two units convert into each other exactly when their dimensions are equal |
250
+
251
+ ```js
252
+ const { unit_convert, unit_convert_difference, unit_info } = require("@iyulab/u-numflow");
253
+ const energy = [
254
+ { symbol: "cal_IT", unit: "J", factor: 4.1868 },
255
+ { symbol: "toe", unit: "kcal_IT", factor: 1e7 },
256
+ ];
257
+ console.log(unit_convert(1, "Gcal_IT", "GJ", energy)); // 4.1868
258
+ console.log(unit_convert(20, "Cel", "K")); // 293.15
259
+ console.log(unit_convert_difference(20, "Cel", "K")); // 20
260
+ console.log(unit_info("toe", energy).dimension); // "kg.m2.s-2"
261
+ try {
262
+ unit_convert(1, "m3", "kg");
263
+ } catch (err) {
264
+ console.log(err.code, err.parameter, err.expected, err.got); // dimension_mismatch to m3 kg
265
+ }
266
+ ```
267
+
213
268
  `codes()` returns every refusal `code` the functions throw, with the fields that
214
269
  can come with it — `{ refusals: [{ code, fields }], violations: [] }` — built from
215
270
  the table the refusals come from; the table below explains each. The C library
@@ -231,11 +286,15 @@ try {
231
286
 
232
287
  | `code` | Fields | Meaning |
233
288
  |---|---|---|
234
- | `parameter_out_of_range` | `parameter`, `min`, `max`, `got`; `exclusive_min` / `exclusive_max` on an open end | `p` not strictly inside (0, 1) (`exclusive_min` 0, `exclusive_max` 1), or a degrees of freedom that is not a finite number `> 0` (`exclusive_min` 0); a distribution parameter that must be `> 0` (`exclusive_min` 0; `parameter` is its path, e.g. `distribution.sigma`), or that falls outside the range the others leave it: `distribution.max` not above `min` (`exclusive_min` = `min`), `distribution.mode` outside `[min, max]` (`min`, `max`); `estimate_lambda`'s `lambda_min` not below `lambda_max` (`exclusive_max` = `lambda_max`); a whole `n` or `seed` outside its range, a negative one included (`min` 0, `max`) |
289
+ | `parameter_out_of_range` | `parameter`, `min`, `max`, `got`; `exclusive_min` / `exclusive_max` on an open end | `p` not strictly inside (0, 1) (`exclusive_min` 0, `exclusive_max` 1), or a degrees of freedom that is not a finite number `> 0` (`exclusive_min` 0); a distribution parameter that must be `> 0` (`exclusive_min` 0; `parameter` is its path, e.g. `distribution.sigma`), or that falls outside the range the others leave it: `distribution.max` not above `min` (`exclusive_min` = `min`), `distribution.mode` outside `[min, max]` (`min`, `max`); `estimate_lambda`'s `lambda_min` not below `lambda_max` (`exclusive_max` = `lambda_max`); a whole `n` or `seed` outside its range, a negative one included (`min` 0, `max`); a unit definition's `factor` not `> 0` (`parameter` `definitions[i].factor`, `exclusive_min` 0) |
235
290
  | `unknown_option` | `parameter`, `got`, `expected` | A `distribution.kind` the crate does not have |
236
- | `malformed_input` | `parameter`, `index` (or absent) | A `data` argument that is not an array or `Float64Array`, or an element that is not a number; a `distribution` that is not an object with a string `kind`, lacks a parameter or has one its kind does not take; an interval row that is not two numbers (`parameter` is its path, e.g. `a[2]`); an `n` or `seed` with a fraction ("must be a whole number") |
291
+ | `malformed_input` | `parameter`, `index` (or absent) | A `data` argument that is not an array or `Float64Array`, or an element that is not a number; a `distribution` that is not an object with a string `kind`, lacks a parameter or has one its kind does not take; an interval row that is not two numbers (`parameter` is its path, e.g. `a[2]`); an `n` or `seed` with a fraction ("must be a whole number"); a unit expression that does not parse (`from`, `to`, `unit`, `definitions[i].unit`), a definition symbol that is empty, starts with a digit or holds an operator character (`definitions[i].symbol`), or a definition of the wrong shape (`definitions[i]`) |
292
+ | `unknown_unit` | `parameter`, `got` | A unit symbol that is neither built in nor defined, nor a prefix on one — `got` is the symbol (`hr` in `kW.hr`) |
293
+ | `dimension_mismatch` | `parameter`, `expected`, `got` | `unit_convert` between units of different dimensions — `parameter` is `to`, `expected` the dimension of `from` and `got` that of `to`, both in SI base units (`m3`, `kg`) |
294
+ | `invalid_option` | `parameter` | A scale with an offset (`Cel`, or a defined one) given a prefix, an exponent or a product — it has a meaning only alone |
295
+ | `duplicate_id` | `parameter`, `id`, `index` | A unit definition whose `symbol` is already a unit (built in or defined before) — `index` is the definition's position |
237
296
  | `reversed_interval` | `parameter`, `index` | An interval row with start > end — refused, not swapped |
238
- | `value_not_finite` | `parameter`, `index` for an array element | A NaN argument, an infinite `n`, `seed`, `lambda_min` or `lambda_max`, or a NaN or infinity in any `data` array or interval row (`parameter` is the row's path) |
297
+ | `value_not_finite` | `parameter`, `index` for an array element | A NaN argument, an infinite `n`, `seed`, `lambda_min` or `lambda_max`, or a NaN or infinity in any `data` array or interval row (`parameter` is the row's path); a NaN or infinite `value` to convert or definition `factor` / `offset` |
239
298
  | `empty_input` | `parameter` | `mean` of no values |
240
299
  | `non_positive_data` | `parameter`, `index`, `got` | Box-Cox data with a value `≤ 0` — `index` is the first such value |
241
300
  | `insufficient_data` | `parameter`, `min`, `got` | Fewer values than the function needs (`std_dev`/`variance` 2, Box-Cox 2) |
@@ -472,6 +472,89 @@ function t_distribution_quantile(p, df) {
472
472
  }
473
473
  exports.t_distribution_quantile = t_distribution_quantile;
474
474
 
475
+ /**
476
+ * A reading `value` in the unit `from`, in the unit `to`: 20 `Cel` is
477
+ * 293.15 `K`. Units are UCUM expressions (`kW.h`, `m/s2`, `J/(kg.K)`) over
478
+ * the SI and the `definitions` given — e.g.
479
+ * `[{ symbol: "cal_IT", unit: "J", factor: 4.1868 }]`.
480
+ *
481
+ * # Errors
482
+ * `value_not_finite` (`parameter` `value`); `malformed_input` for an
483
+ * expression that does not parse or a definition of the wrong shape;
484
+ * `unknown_unit` (`parameter`, `got` = the symbol); `invalid_option` for a
485
+ * scale such as `Cel` with a prefix, exponent or product;
486
+ * `dimension_mismatch` (`parameter` `to`, `expected` = the dimension of
487
+ * `from`, `got` = that of `to`, both in SI base units); for a definition,
488
+ * `duplicate_id` (`id`, `index`) for a symbol already a unit and
489
+ * `parameter_out_of_range` for a `factor` not `> 0`, each at
490
+ * `definitions[i].…`.
491
+ * @param {number} value
492
+ * @param {string} from
493
+ * @param {string} to
494
+ * @param {UnitDefinitionSpec[] | null} [definitions]
495
+ * @returns {number}
496
+ */
497
+ function unit_convert(value, from, to, definitions) {
498
+ const ptr0 = passStringToWasm0(from, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
499
+ const len0 = WASM_VECTOR_LEN;
500
+ const ptr1 = passStringToWasm0(to, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
501
+ const len1 = WASM_VECTOR_LEN;
502
+ const ret = wasm.unit_convert(value, ptr0, len0, ptr1, len1, isLikeNone(definitions) ? 0 : addToExternrefTable0(definitions));
503
+ if (ret[2]) {
504
+ throw takeFromExternrefTable0(ret[1]);
505
+ }
506
+ return ret[0];
507
+ }
508
+ exports.unit_convert = unit_convert;
509
+
510
+ /**
511
+ * An amount of change `value` in `from`, in `to`: the offset of a scale
512
+ * cancels, so a rise of 20 `Cel` is a rise of 20 `K`.
513
+ *
514
+ * # Errors
515
+ * As [`unit_convert`].
516
+ * @param {number} value
517
+ * @param {string} from
518
+ * @param {string} to
519
+ * @param {UnitDefinitionSpec[] | null} [definitions]
520
+ * @returns {number}
521
+ */
522
+ function unit_convert_difference(value, from, to, definitions) {
523
+ const ptr0 = passStringToWasm0(from, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
524
+ const len0 = WASM_VECTOR_LEN;
525
+ const ptr1 = passStringToWasm0(to, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
526
+ const len1 = WASM_VECTOR_LEN;
527
+ const ret = wasm.unit_convert_difference(value, ptr0, len0, ptr1, len1, isLikeNone(definitions) ? 0 : addToExternrefTable0(definitions));
528
+ if (ret[2]) {
529
+ throw takeFromExternrefTable0(ret[1]);
530
+ }
531
+ return ret[0];
532
+ }
533
+ exports.unit_convert_difference = unit_convert_difference;
534
+
535
+ /**
536
+ * What the unit expression `unit` stands for: its dimension in SI base units
537
+ * (`"kg.m2.s-2"`, with the exponents of m, kg, s, A, K, mol, cd), and
538
+ * `scale` / `offset` with `value_in_si = value · scale + offset`. Two units
539
+ * convert into each other exactly when their dimensions are equal.
540
+ *
541
+ * # Errors
542
+ * As [`unit_convert`] for `unit` (at `parameter` `unit`) and `definitions`.
543
+ * @param {string} unit
544
+ * @param {UnitDefinitionSpec[] | null} [definitions]
545
+ * @returns {UnitInfo}
546
+ */
547
+ function unit_info(unit, definitions) {
548
+ const ptr0 = passStringToWasm0(unit, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
549
+ const len0 = WASM_VECTOR_LEN;
550
+ const ret = wasm.unit_info(ptr0, len0, isLikeNone(definitions) ? 0 : addToExternrefTable0(definitions));
551
+ if (ret[2]) {
552
+ throw takeFromExternrefTable0(ret[1]);
553
+ }
554
+ return takeFromExternrefTable0(ret[0]);
555
+ }
556
+ exports.unit_info = unit_info;
557
+
475
558
  /**
476
559
  * Sample variance of `data` (Bessel-corrected, denominator n−1).
477
560
  *
@@ -541,6 +624,10 @@ function __wbg_get_imports() {
541
624
  const ret = typeof(val) === 'object' && val !== null;
542
625
  return ret;
543
626
  },
627
+ __wbg___wbindgen_is_undefined_8865fb403f8fe9d8: function(arg0) {
628
+ const ret = arg0 === undefined;
629
+ return ret;
630
+ },
544
631
  __wbg___wbindgen_jsval_eq_02babf21faa37971: function(arg0, arg1) {
545
632
  const ret = arg0 === arg1;
546
633
  return ret;
@@ -598,6 +685,10 @@ function __wbg_get_imports() {
598
685
  const ret = arg0[arg1 >>> 0];
599
686
  return ret;
600
687
  },
688
+ __wbg_get_with_ref_key_6412cf3094599694: function(arg0, arg1) {
689
+ const ret = arg0[arg1];
690
+ return ret;
691
+ },
601
692
  __wbg_instanceof_ArrayBuffer_a99f175873e5d9b8: function(arg0) {
602
693
  let result;
603
694
  try {
@@ -14,6 +14,29 @@ export interface RefusalCode {
14
14
  fields: string[];
15
15
  }
16
16
 
17
+ /**
18
+ * A unit the caller defines: `symbol` = `factor` × `unit` (+ `offset` SI
19
+ * units at its zero, for a scale such as a Fahrenheit degree).
20
+ */
21
+ export interface UnitDefinitionSpec {
22
+ /**
23
+ * The new symbol, e.g. `cal_IT`.
24
+ */
25
+ symbol: string;
26
+ /**
27
+ * What it is a multiple of, as a unit expression, e.g. `J`.
28
+ */
29
+ unit: string;
30
+ /**
31
+ * How many `unit` one `symbol` is (default 1, `> 0`).
32
+ */
33
+ factor?: number;
34
+ /**
35
+ * SI value at the symbol's zero (default 0).
36
+ */
37
+ offset?: number;
38
+ }
39
+
17
40
  /**
18
41
  * A violation a checker reports. This package has no checker; the type is
19
42
  * here so every engine's [`Codes`] has one shape.
@@ -40,6 +63,28 @@ export interface LambdaEstimateDto {
40
63
  at_bound: boolean;
41
64
  }
42
65
 
66
+ /**
67
+ * What a unit expression stands for.
68
+ */
69
+ export interface UnitInfo {
70
+ /**
71
+ * The dimension in SI base units, e.g. `kg.m2.s-2` (`1` for a pure number).
72
+ */
73
+ dimension: string;
74
+ /**
75
+ * Exponents of m, kg, s, A, K, mol, cd.
76
+ */
77
+ exponents: [number, number, number, number, number, number, number];
78
+ /**
79
+ * Size of one unit in coherent SI units.
80
+ */
81
+ scale: number;
82
+ /**
83
+ * SI value of the unit's zero (0 except for scales such as `Cel`).
84
+ */
85
+ offset: number;
86
+ }
87
+
43
88
  /**
44
89
  * What this package can answer with besides a result: every refusal
45
90
  * `code`, and no violations -- it has no checker.
@@ -289,6 +334,45 @@ export function t_distribution_cdf(t: number, df: number): number;
289
334
  */
290
335
  export function t_distribution_quantile(p: number, df: number): number;
291
336
 
337
+ /**
338
+ * A reading `value` in the unit `from`, in the unit `to`: 20 `Cel` is
339
+ * 293.15 `K`. Units are UCUM expressions (`kW.h`, `m/s2`, `J/(kg.K)`) over
340
+ * the SI and the `definitions` given — e.g.
341
+ * `[{ symbol: "cal_IT", unit: "J", factor: 4.1868 }]`.
342
+ *
343
+ * # Errors
344
+ * `value_not_finite` (`parameter` `value`); `malformed_input` for an
345
+ * expression that does not parse or a definition of the wrong shape;
346
+ * `unknown_unit` (`parameter`, `got` = the symbol); `invalid_option` for a
347
+ * scale such as `Cel` with a prefix, exponent or product;
348
+ * `dimension_mismatch` (`parameter` `to`, `expected` = the dimension of
349
+ * `from`, `got` = that of `to`, both in SI base units); for a definition,
350
+ * `duplicate_id` (`id`, `index`) for a symbol already a unit and
351
+ * `parameter_out_of_range` for a `factor` not `> 0`, each at
352
+ * `definitions[i].…`.
353
+ */
354
+ export function unit_convert(value: number, from: string, to: string, definitions?: UnitDefinitionSpec[] | null): number;
355
+
356
+ /**
357
+ * An amount of change `value` in `from`, in `to`: the offset of a scale
358
+ * cancels, so a rise of 20 `Cel` is a rise of 20 `K`.
359
+ *
360
+ * # Errors
361
+ * As [`unit_convert`].
362
+ */
363
+ export function unit_convert_difference(value: number, from: string, to: string, definitions?: UnitDefinitionSpec[] | null): number;
364
+
365
+ /**
366
+ * What the unit expression `unit` stands for: its dimension in SI base units
367
+ * (`"kg.m2.s-2"`, with the exponents of m, kg, s, A, K, mol, cd), and
368
+ * `scale` / `offset` with `value_in_si = value · scale + offset`. Two units
369
+ * convert into each other exactly when their dimensions are equal.
370
+ *
371
+ * # Errors
372
+ * As [`unit_convert`] for `unit` (at `parameter` `unit`) and `definitions`.
373
+ */
374
+ export function unit_info(unit: string, definitions?: UnitDefinitionSpec[] | null): UnitInfo;
375
+
292
376
  /**
293
377
  * Sample variance of `data` (Bessel-corrected, denominator n−1).
294
378
  *
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.13.1",
8
+ "version": "0.14.0",
9
9
  "license": "MIT",
10
10
  "repository": {
11
11
  "type": "git",
package/u_numflow.d.ts CHANGED
@@ -14,6 +14,29 @@ export interface RefusalCode {
14
14
  fields: string[];
15
15
  }
16
16
 
17
+ /**
18
+ * A unit the caller defines: `symbol` = `factor` × `unit` (+ `offset` SI
19
+ * units at its zero, for a scale such as a Fahrenheit degree).
20
+ */
21
+ export interface UnitDefinitionSpec {
22
+ /**
23
+ * The new symbol, e.g. `cal_IT`.
24
+ */
25
+ symbol: string;
26
+ /**
27
+ * What it is a multiple of, as a unit expression, e.g. `J`.
28
+ */
29
+ unit: string;
30
+ /**
31
+ * How many `unit` one `symbol` is (default 1, `> 0`).
32
+ */
33
+ factor?: number;
34
+ /**
35
+ * SI value at the symbol's zero (default 0).
36
+ */
37
+ offset?: number;
38
+ }
39
+
17
40
  /**
18
41
  * A violation a checker reports. This package has no checker; the type is
19
42
  * here so every engine's [`Codes`] has one shape.
@@ -40,6 +63,28 @@ export interface LambdaEstimateDto {
40
63
  at_bound: boolean;
41
64
  }
42
65
 
66
+ /**
67
+ * What a unit expression stands for.
68
+ */
69
+ export interface UnitInfo {
70
+ /**
71
+ * The dimension in SI base units, e.g. `kg.m2.s-2` (`1` for a pure number).
72
+ */
73
+ dimension: string;
74
+ /**
75
+ * Exponents of m, kg, s, A, K, mol, cd.
76
+ */
77
+ exponents: [number, number, number, number, number, number, number];
78
+ /**
79
+ * Size of one unit in coherent SI units.
80
+ */
81
+ scale: number;
82
+ /**
83
+ * SI value of the unit's zero (0 except for scales such as `Cel`).
84
+ */
85
+ offset: number;
86
+ }
87
+
43
88
  /**
44
89
  * What this package can answer with besides a result: every refusal
45
90
  * `code`, and no violations -- it has no checker.
@@ -289,6 +334,45 @@ export function t_distribution_cdf(t: number, df: number): number;
289
334
  */
290
335
  export function t_distribution_quantile(p: number, df: number): number;
291
336
 
337
+ /**
338
+ * A reading `value` in the unit `from`, in the unit `to`: 20 `Cel` is
339
+ * 293.15 `K`. Units are UCUM expressions (`kW.h`, `m/s2`, `J/(kg.K)`) over
340
+ * the SI and the `definitions` given — e.g.
341
+ * `[{ symbol: "cal_IT", unit: "J", factor: 4.1868 }]`.
342
+ *
343
+ * # Errors
344
+ * `value_not_finite` (`parameter` `value`); `malformed_input` for an
345
+ * expression that does not parse or a definition of the wrong shape;
346
+ * `unknown_unit` (`parameter`, `got` = the symbol); `invalid_option` for a
347
+ * scale such as `Cel` with a prefix, exponent or product;
348
+ * `dimension_mismatch` (`parameter` `to`, `expected` = the dimension of
349
+ * `from`, `got` = that of `to`, both in SI base units); for a definition,
350
+ * `duplicate_id` (`id`, `index`) for a symbol already a unit and
351
+ * `parameter_out_of_range` for a `factor` not `> 0`, each at
352
+ * `definitions[i].…`.
353
+ */
354
+ export function unit_convert(value: number, from: string, to: string, definitions?: UnitDefinitionSpec[] | null): number;
355
+
356
+ /**
357
+ * An amount of change `value` in `from`, in `to`: the offset of a scale
358
+ * cancels, so a rise of 20 `Cel` is a rise of 20 `K`.
359
+ *
360
+ * # Errors
361
+ * As [`unit_convert`].
362
+ */
363
+ export function unit_convert_difference(value: number, from: string, to: string, definitions?: UnitDefinitionSpec[] | null): number;
364
+
365
+ /**
366
+ * What the unit expression `unit` stands for: its dimension in SI base units
367
+ * (`"kg.m2.s-2"`, with the exponents of m, kg, s, A, K, mol, cd), and
368
+ * `scale` / `offset` with `value_in_si = value · scale + offset`. Two units
369
+ * convert into each other exactly when their dimensions are equal.
370
+ *
371
+ * # Errors
372
+ * As [`unit_convert`] for `unit` (at `parameter` `unit`) and `definitions`.
373
+ */
374
+ export function unit_info(unit: string, definitions?: UnitDefinitionSpec[] | null): UnitInfo;
375
+
292
376
  /**
293
377
  * Sample variance of `data` (Bessel-corrected, denominator n−1).
294
378
  *
package/u_numflow.js CHANGED
@@ -5,5 +5,5 @@ import { __wbg_set_wasm } from "./u_numflow_bg.js";
5
5
  __wbg_set_wasm(wasm);
6
6
  wasm.__wbindgen_start();
7
7
  export {
8
- box_cox, chi_squared_cdf, chi_squared_quantile, codes, distribution_cdf, distribution_moments, distribution_quantile, distribution_sample, estimate_lambda, f_distribution_cdf, f_distribution_quantile, interval_difference, interval_intersection, interval_measure, interval_normalize, interval_union, inverse_normal_cdf, mean, normal_cdf, normal_sf, rfft, std_dev, t_distribution_cdf, t_distribution_quantile, variance
8
+ box_cox, chi_squared_cdf, chi_squared_quantile, codes, distribution_cdf, distribution_moments, distribution_quantile, distribution_sample, estimate_lambda, f_distribution_cdf, f_distribution_quantile, interval_difference, interval_intersection, interval_measure, interval_normalize, interval_union, inverse_normal_cdf, mean, normal_cdf, normal_sf, rfft, std_dev, t_distribution_cdf, t_distribution_quantile, unit_convert, unit_convert_difference, unit_info, variance
9
9
  } from "./u_numflow_bg.js";
package/u_numflow_bg.js CHANGED
@@ -446,6 +446,86 @@ export function t_distribution_quantile(p, df) {
446
446
  return ret[0];
447
447
  }
448
448
 
449
+ /**
450
+ * A reading `value` in the unit `from`, in the unit `to`: 20 `Cel` is
451
+ * 293.15 `K`. Units are UCUM expressions (`kW.h`, `m/s2`, `J/(kg.K)`) over
452
+ * the SI and the `definitions` given — e.g.
453
+ * `[{ symbol: "cal_IT", unit: "J", factor: 4.1868 }]`.
454
+ *
455
+ * # Errors
456
+ * `value_not_finite` (`parameter` `value`); `malformed_input` for an
457
+ * expression that does not parse or a definition of the wrong shape;
458
+ * `unknown_unit` (`parameter`, `got` = the symbol); `invalid_option` for a
459
+ * scale such as `Cel` with a prefix, exponent or product;
460
+ * `dimension_mismatch` (`parameter` `to`, `expected` = the dimension of
461
+ * `from`, `got` = that of `to`, both in SI base units); for a definition,
462
+ * `duplicate_id` (`id`, `index`) for a symbol already a unit and
463
+ * `parameter_out_of_range` for a `factor` not `> 0`, each at
464
+ * `definitions[i].…`.
465
+ * @param {number} value
466
+ * @param {string} from
467
+ * @param {string} to
468
+ * @param {UnitDefinitionSpec[] | null} [definitions]
469
+ * @returns {number}
470
+ */
471
+ export function unit_convert(value, from, to, definitions) {
472
+ const ptr0 = passStringToWasm0(from, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
473
+ const len0 = WASM_VECTOR_LEN;
474
+ const ptr1 = passStringToWasm0(to, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
475
+ const len1 = WASM_VECTOR_LEN;
476
+ const ret = wasm.unit_convert(value, ptr0, len0, ptr1, len1, isLikeNone(definitions) ? 0 : addToExternrefTable0(definitions));
477
+ if (ret[2]) {
478
+ throw takeFromExternrefTable0(ret[1]);
479
+ }
480
+ return ret[0];
481
+ }
482
+
483
+ /**
484
+ * An amount of change `value` in `from`, in `to`: the offset of a scale
485
+ * cancels, so a rise of 20 `Cel` is a rise of 20 `K`.
486
+ *
487
+ * # Errors
488
+ * As [`unit_convert`].
489
+ * @param {number} value
490
+ * @param {string} from
491
+ * @param {string} to
492
+ * @param {UnitDefinitionSpec[] | null} [definitions]
493
+ * @returns {number}
494
+ */
495
+ export function unit_convert_difference(value, from, to, definitions) {
496
+ const ptr0 = passStringToWasm0(from, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
497
+ const len0 = WASM_VECTOR_LEN;
498
+ const ptr1 = passStringToWasm0(to, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
499
+ const len1 = WASM_VECTOR_LEN;
500
+ const ret = wasm.unit_convert_difference(value, ptr0, len0, ptr1, len1, isLikeNone(definitions) ? 0 : addToExternrefTable0(definitions));
501
+ if (ret[2]) {
502
+ throw takeFromExternrefTable0(ret[1]);
503
+ }
504
+ return ret[0];
505
+ }
506
+
507
+ /**
508
+ * What the unit expression `unit` stands for: its dimension in SI base units
509
+ * (`"kg.m2.s-2"`, with the exponents of m, kg, s, A, K, mol, cd), and
510
+ * `scale` / `offset` with `value_in_si = value · scale + offset`. Two units
511
+ * convert into each other exactly when their dimensions are equal.
512
+ *
513
+ * # Errors
514
+ * As [`unit_convert`] for `unit` (at `parameter` `unit`) and `definitions`.
515
+ * @param {string} unit
516
+ * @param {UnitDefinitionSpec[] | null} [definitions]
517
+ * @returns {UnitInfo}
518
+ */
519
+ export function unit_info(unit, definitions) {
520
+ const ptr0 = passStringToWasm0(unit, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
521
+ const len0 = WASM_VECTOR_LEN;
522
+ const ret = wasm.unit_info(ptr0, len0, isLikeNone(definitions) ? 0 : addToExternrefTable0(definitions));
523
+ if (ret[2]) {
524
+ throw takeFromExternrefTable0(ret[1]);
525
+ }
526
+ return takeFromExternrefTable0(ret[0]);
527
+ }
528
+
449
529
  /**
450
530
  * Sample variance of `data` (Bessel-corrected, denominator n−1).
451
531
  *
@@ -511,6 +591,10 @@ export function __wbg___wbindgen_is_object_3c45d4f2dde4e749(arg0) {
511
591
  const ret = typeof(val) === 'object' && val !== null;
512
592
  return ret;
513
593
  }
594
+ export function __wbg___wbindgen_is_undefined_8865fb403f8fe9d8(arg0) {
595
+ const ret = arg0 === undefined;
596
+ return ret;
597
+ }
514
598
  export function __wbg___wbindgen_jsval_eq_02babf21faa37971(arg0, arg1) {
515
599
  const ret = arg0 === arg1;
516
600
  return ret;
@@ -568,6 +652,10 @@ export function __wbg_get_unchecked_288889d017702237(arg0, arg1) {
568
652
  const ret = arg0[arg1 >>> 0];
569
653
  return ret;
570
654
  }
655
+ export function __wbg_get_with_ref_key_6412cf3094599694(arg0, arg1) {
656
+ const ret = arg0[arg1];
657
+ return ret;
658
+ }
571
659
  export function __wbg_instanceof_ArrayBuffer_a99f175873e5d9b8(arg0) {
572
660
  let result;
573
661
  try {
package/u_numflow_bg.wasm CHANGED
Binary file