@iyulab/u-numflow 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 iyulab
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,88 @@
1
+ # u-numflow
2
+
3
+ **Domain-agnostic mathematical primitives in Rust**
4
+
5
+ [![Crates.io](https://img.shields.io/crates/v/u-numflow.svg)](https://crates.io/crates/u-numflow)
6
+ [![docs.rs](https://docs.rs/u-numflow/badge.svg)](https://docs.rs/u-numflow)
7
+ [![CI](https://github.com/iyulab/u-numflow/actions/workflows/ci.yml/badge.svg)](https://github.com/iyulab/u-numflow/actions/workflows/ci.yml)
8
+ [![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
9
+
10
+ ## Overview
11
+
12
+ u-numflow provides foundational mathematical, statistical, and probabilistic building blocks. Entirely domain-agnostic with no external dependencies beyond `rand`.
13
+
14
+ ## Modules
15
+
16
+ | Module | Description |
17
+ |--------|-------------|
18
+ | `stats` | Descriptive statistics (mean, variance, skewness, kurtosis) with Welford's online algorithm and Neumaier summation |
19
+ | `distributions` | Probability distributions: Uniform, Triangular, PERT, Normal, LogNormal |
20
+ | `special` | Special functions: normal/t/F/chi² CDF, inverse normal CDF, regularized incomplete beta/gamma, erf |
21
+ | `transforms` | Data transformations: Box-Cox (λ via MLE golden-section search), inverse Box-Cox |
22
+ | `matrix` | Dense matrix operations: determinant, inverse, Cholesky decomposition, Jacobi eigenvalue decomposition |
23
+ | `random` | Seeded RNG, Fisher-Yates shuffle, weighted sampling, random subset selection |
24
+ | `collections` | Specialized data structures: Union-Find with path compression and union-by-rank |
25
+
26
+ ## Design Philosophy
27
+
28
+ - **Numerical stability first** — Welford's algorithm for variance, Neumaier summation for accumulation
29
+ - **Reproducibility** — Seeded RNG support for deterministic experiments
30
+ - **Property-based testing** — Mathematical invariants verified via `proptest`
31
+
32
+ ## Quick Start
33
+
34
+ ```toml
35
+ [dependencies]
36
+ u-numflow = "0.2"
37
+ ```
38
+
39
+ ```rust
40
+ use u_numflow::stats::OnlineStats;
41
+ use u_numflow::distributions::{PertDistribution, Distribution};
42
+ use u_numflow::random::Rng;
43
+
44
+ // Online statistics with numerical stability
45
+ let mut stats = OnlineStats::new();
46
+ for x in [1.0, 2.0, 3.0, 4.0, 5.0] {
47
+ stats.push(x);
48
+ }
49
+ assert_eq!(stats.mean(), 3.0);
50
+
51
+ // PERT distribution sampling
52
+ let pert = PertDistribution::new(1.0, 4.0, 7.0);
53
+ let mut rng = Rng::seed_from_u64(42);
54
+ let sample = pert.sample(&mut rng);
55
+
56
+ // Seeded shuffling for reproducibility
57
+ let mut items = vec![1, 2, 3, 4, 5];
58
+ u_numflow::random::shuffle(&mut items, &mut rng);
59
+
60
+ // Box-Cox transformation (non-normal data normalization)
61
+ use u_numflow::transforms::{estimate_lambda, box_cox};
62
+ let data = [1.0, 2.0, 4.0, 8.0, 16.0];
63
+ let lambda = estimate_lambda(&data, -2.0, 2.0).unwrap(); // MLE via golden-section
64
+ let transformed = box_cox(&data, lambda).unwrap();
65
+ ```
66
+
67
+ ## Build & Test
68
+
69
+ ```bash
70
+ cargo build
71
+ cargo test
72
+ ```
73
+
74
+ ## Dependencies
75
+
76
+ - `rand` 0.9 — Random number generation
77
+ - `proptest` 1.4 — Property-based testing (dev only)
78
+
79
+ ## License
80
+
81
+ MIT License — see [LICENSE](LICENSE).
82
+
83
+ ## Related
84
+
85
+ - [u-metaheur](https://github.com/iyulab/u-metaheur) — Metaheuristic optimization (GA, SA, ALNS, CP)
86
+ - [u-geometry](https://github.com/iyulab/u-geometry) — Computational geometry
87
+ - [u-schedule](https://github.com/iyulab/u-schedule) — Scheduling framework
88
+ - [u-nesting](https://github.com/iyulab/U-Nesting) — 2D/3D nesting and bin packing
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@iyulab/u-numflow",
3
+ "type": "module",
4
+ "collaborators": [
5
+ "iyulab"
6
+ ],
7
+ "description": "Mathematical primitives for the U-Engine ecosystem: statistics, probability, random sampling, and collections.",
8
+ "version": "0.2.1",
9
+ "license": "MIT",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/iyulab/u-numflow"
13
+ },
14
+ "files": [
15
+ "u_numflow_bg.wasm",
16
+ "u_numflow.js",
17
+ "u_numflow_bg.js",
18
+ "u_numflow.d.ts"
19
+ ],
20
+ "main": "u_numflow.js",
21
+ "types": "u_numflow.d.ts",
22
+ "sideEffects": [
23
+ "./u_numflow.js",
24
+ "./snippets/*"
25
+ ],
26
+ "keywords": [
27
+ "statistics",
28
+ "probability",
29
+ "optimization",
30
+ "mathematics"
31
+ ]
32
+ }
package/u_numflow.d.ts ADDED
@@ -0,0 +1,54 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ /**
5
+ * Apply the Box-Cox power transformation to positive data.
6
+ *
7
+ * All values in `data` must be strictly positive.
8
+ *
9
+ * # Errors
10
+ * Returns a `JsValue` error string if data contains non-positive values or
11
+ * has fewer than 2 elements.
12
+ */
13
+ export function box_cox(data: Float64Array, lambda: number): Float64Array;
14
+
15
+ /**
16
+ * Estimate the optimal Box-Cox lambda via profile maximum likelihood.
17
+ *
18
+ * Searches in the range `[lambda_min, lambda_max]` using golden-section search.
19
+ *
20
+ * # Errors
21
+ * Returns a `JsValue` error string if data contains non-positive values,
22
+ * has fewer than 2 elements, or `lambda_min >= lambda_max`.
23
+ */
24
+ export function estimate_lambda(data: Float64Array, lambda_min: number, lambda_max: number): number;
25
+
26
+ /**
27
+ * Arithmetic mean of `data` using Kahan compensated summation.
28
+ *
29
+ * Returns `NaN` if `data` is empty or contains non-finite values.
30
+ */
31
+ export function mean(data: Float64Array): number;
32
+
33
+ /**
34
+ * Standard normal CDF Φ(x) = P(Z ≤ x) for Z ~ N(0, 1).
35
+ *
36
+ * To evaluate a general normal N(μ, σ), pass `(x - μ) / σ`.
37
+ *
38
+ * Uses Abramowitz & Stegun formula 26.2.17 (max abs error < 7.5 × 10⁻⁸).
39
+ */
40
+ export function normal_cdf(x: number): number;
41
+
42
+ /**
43
+ * Sample standard deviation of `data` (Bessel-corrected, denominator n−1).
44
+ *
45
+ * Returns `NaN` if `data` has fewer than 2 elements or contains non-finite values.
46
+ */
47
+ export function std_dev(data: Float64Array): number;
48
+
49
+ /**
50
+ * Sample variance of `data` (Bessel-corrected, denominator n−1).
51
+ *
52
+ * Returns `NaN` if `data` has fewer than 2 elements or contains non-finite values.
53
+ */
54
+ export function variance(data: Float64Array): number;
package/u_numflow.js ADDED
@@ -0,0 +1,9 @@
1
+ /* @ts-self-types="./u_numflow.d.ts" */
2
+
3
+ import * as wasm from "./u_numflow_bg.wasm";
4
+ import { __wbg_set_wasm } from "./u_numflow_bg.js";
5
+ __wbg_set_wasm(wasm);
6
+ wasm.__wbindgen_start();
7
+ export {
8
+ box_cox, estimate_lambda, mean, normal_cdf, std_dev, variance
9
+ } from "./u_numflow_bg.js";
@@ -0,0 +1,176 @@
1
+ /**
2
+ * Apply the Box-Cox power transformation to positive data.
3
+ *
4
+ * All values in `data` must be strictly positive.
5
+ *
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
10
+ * @param {number} lambda
11
+ * @returns {Float64Array}
12
+ */
13
+ 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);
17
+ if (ret[3]) {
18
+ throw takeFromExternrefTable0(ret[2]);
19
+ }
20
+ var v2 = getArrayF64FromWasm0(ret[0], ret[1]).slice();
21
+ wasm.__wbindgen_free(ret[0], ret[1] * 8, 8);
22
+ return v2;
23
+ }
24
+
25
+ /**
26
+ * Estimate the optimal Box-Cox lambda via profile maximum likelihood.
27
+ *
28
+ * Searches in the range `[lambda_min, lambda_max]` using golden-section search.
29
+ *
30
+ * # Errors
31
+ * Returns a `JsValue` error string if data contains non-positive values,
32
+ * has fewer than 2 elements, or `lambda_min >= lambda_max`.
33
+ * @param {Float64Array} data
34
+ * @param {number} lambda_min
35
+ * @param {number} lambda_max
36
+ * @returns {number}
37
+ */
38
+ export function estimate_lambda(data, lambda_min, lambda_max) {
39
+ const ptr0 = passArrayF64ToWasm0(data, wasm.__wbindgen_malloc);
40
+ const len0 = WASM_VECTOR_LEN;
41
+ const ret = wasm.estimate_lambda(ptr0, len0, lambda_min, lambda_max);
42
+ if (ret[2]) {
43
+ throw takeFromExternrefTable0(ret[1]);
44
+ }
45
+ return ret[0];
46
+ }
47
+
48
+ /**
49
+ * Arithmetic mean of `data` using Kahan compensated summation.
50
+ *
51
+ * Returns `NaN` if `data` is empty or contains non-finite values.
52
+ * @param {Float64Array} data
53
+ * @returns {number}
54
+ */
55
+ export function mean(data) {
56
+ const ptr0 = passArrayF64ToWasm0(data, wasm.__wbindgen_malloc);
57
+ const len0 = WASM_VECTOR_LEN;
58
+ const ret = wasm.mean(ptr0, len0);
59
+ return ret;
60
+ }
61
+
62
+ /**
63
+ * Standard normal CDF Φ(x) = P(Z ≤ x) for Z ~ N(0, 1).
64
+ *
65
+ * To evaluate a general normal N(μ, σ), pass `(x - μ) / σ`.
66
+ *
67
+ * Uses Abramowitz & Stegun formula 26.2.17 (max abs error < 7.5 × 10⁻⁸).
68
+ * @param {number} x
69
+ * @returns {number}
70
+ */
71
+ export function normal_cdf(x) {
72
+ const ret = wasm.normal_cdf(x);
73
+ return ret;
74
+ }
75
+
76
+ /**
77
+ * Sample standard deviation of `data` (Bessel-corrected, denominator n−1).
78
+ *
79
+ * Returns `NaN` if `data` has fewer than 2 elements or contains non-finite values.
80
+ * @param {Float64Array} data
81
+ * @returns {number}
82
+ */
83
+ export function std_dev(data) {
84
+ const ptr0 = passArrayF64ToWasm0(data, wasm.__wbindgen_malloc);
85
+ const len0 = WASM_VECTOR_LEN;
86
+ const ret = wasm.std_dev(ptr0, len0);
87
+ return ret;
88
+ }
89
+
90
+ /**
91
+ * Sample variance of `data` (Bessel-corrected, denominator n−1).
92
+ *
93
+ * Returns `NaN` if `data` has fewer than 2 elements or contains non-finite values.
94
+ * @param {Float64Array} data
95
+ * @returns {number}
96
+ */
97
+ export function variance(data) {
98
+ const ptr0 = passArrayF64ToWasm0(data, wasm.__wbindgen_malloc);
99
+ const len0 = WASM_VECTOR_LEN;
100
+ const ret = wasm.variance(ptr0, len0);
101
+ return ret;
102
+ }
103
+ export function __wbindgen_cast_0000000000000001(arg0, arg1) {
104
+ // Cast intrinsic for `Ref(String) -> Externref`.
105
+ const ret = getStringFromWasm0(arg0, arg1);
106
+ return ret;
107
+ }
108
+ export function __wbindgen_init_externref_table() {
109
+ const table = wasm.__wbindgen_externrefs;
110
+ const offset = table.grow(4);
111
+ table.set(0, undefined);
112
+ table.set(offset + 0, undefined);
113
+ table.set(offset + 1, null);
114
+ table.set(offset + 2, true);
115
+ table.set(offset + 3, false);
116
+ }
117
+ function getArrayF64FromWasm0(ptr, len) {
118
+ ptr = ptr >>> 0;
119
+ return getFloat64ArrayMemory0().subarray(ptr / 8, ptr / 8 + len);
120
+ }
121
+
122
+ let cachedFloat64ArrayMemory0 = null;
123
+ function getFloat64ArrayMemory0() {
124
+ if (cachedFloat64ArrayMemory0 === null || cachedFloat64ArrayMemory0.byteLength === 0) {
125
+ cachedFloat64ArrayMemory0 = new Float64Array(wasm.memory.buffer);
126
+ }
127
+ return cachedFloat64ArrayMemory0;
128
+ }
129
+
130
+ function getStringFromWasm0(ptr, len) {
131
+ ptr = ptr >>> 0;
132
+ return decodeText(ptr, len);
133
+ }
134
+
135
+ let cachedUint8ArrayMemory0 = null;
136
+ function getUint8ArrayMemory0() {
137
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
138
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
139
+ }
140
+ return cachedUint8ArrayMemory0;
141
+ }
142
+
143
+ function passArrayF64ToWasm0(arg, malloc) {
144
+ const ptr = malloc(arg.length * 8, 8) >>> 0;
145
+ getFloat64ArrayMemory0().set(arg, ptr / 8);
146
+ WASM_VECTOR_LEN = arg.length;
147
+ return ptr;
148
+ }
149
+
150
+ function takeFromExternrefTable0(idx) {
151
+ const value = wasm.__wbindgen_externrefs.get(idx);
152
+ wasm.__externref_table_dealloc(idx);
153
+ return value;
154
+ }
155
+
156
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
157
+ cachedTextDecoder.decode();
158
+ const MAX_SAFARI_DECODE_BYTES = 2146435072;
159
+ let numBytesDecoded = 0;
160
+ function decodeText(ptr, len) {
161
+ numBytesDecoded += len;
162
+ if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
163
+ cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
164
+ cachedTextDecoder.decode();
165
+ numBytesDecoded = len;
166
+ }
167
+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
168
+ }
169
+
170
+ let WASM_VECTOR_LEN = 0;
171
+
172
+
173
+ let wasm;
174
+ export function __wbg_set_wasm(val) {
175
+ wasm = val;
176
+ }
Binary file