@descix/egpt-math-sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +224 -0
- package/dist/egpt-math-sdk.browser.js +5 -0
- package/dist/index.d.ts +100 -0
- package/dist/index.js +985 -0
- package/dist/notebooks/arithmetic-basics.html +166 -0
- package/dist/notebooks/complex-twiddle.html +148 -0
- package/dist/notebooks/double-slit-rosetta-qft.html +154 -0
- package/dist/notebooks/egpt-fft-test.html +202 -0
- package/dist/notebooks/egpt-matrix-test.html +241 -0
- package/dist/notebooks/egpt-polynomial-test.html +703 -0
- package/dist/notebooks/egpt-prime-composite-test.html +561 -0
- package/dist/notebooks/egpt-test-suite.html +1387 -0
- package/dist/notebooks/egpt-topology-test.html +410 -0
- package/dist/notebooks/factorization-basics.html +131 -0
- package/dist/notebooks/fft.html +155 -0
- package/dist/notebooks/frqtl-qft-benchmark.html +207 -0
- package/dist/notebooks/gallery-atomic-model.html +51 -0
- package/dist/notebooks/gallery-big-bang.html +62 -0
- package/dist/notebooks/gallery-blackbody.html +52 -0
- package/dist/notebooks/gallery-circuit-sat.html +104 -0
- package/dist/notebooks/gallery-collision-matrix-3d.html +51 -0
- package/dist/notebooks/gallery-double-slit.html +53 -0
- package/dist/notebooks/gallery-gun-chamber.html +62 -0
- package/dist/notebooks/gallery-particle-walk-3d.html +53 -0
- package/dist/notebooks/gallery-wave-interference.html +52 -0
- package/dist/notebooks/getting-started-hello-frqtl.html +53 -0
- package/dist/notebooks/iso-canonical-chain.html +203 -0
- package/dist/notebooks/iso-entropy-log.html +127 -0
- package/dist/notebooks/iso-fft-polynomial.html +136 -0
- package/dist/notebooks/iso-matrix-four-views.html +131 -0
- package/dist/notebooks/iso-number-theory-four-views.html +99 -0
- package/dist/notebooks/iso-pi-log-fraction-loop.html +140 -0
- package/dist/notebooks/matrix.html +202 -0
- package/dist/notebooks/notebooks.json +240 -0
- package/dist/notebooks/polynomials.html +201 -0
- package/dist/notebooks/prime-composite.html +268 -0
- package/dist/notebooks/rational-core.html +179 -0
- package/dist/notebooks/reference-egpt-fft-test.html +187 -0
- package/dist/notebooks/reference-order-finder-test.html +196 -0
- package/dist/notebooks/reference-prime-atom-polynomial-test.html +542 -0
- package/dist/notebooks/reference-solve-polynomial-system-test.html +471 -0
- package/dist/notebooks/scaled-vectors.html +112 -0
- package/dist/notebooks/stats.html +206 -0
- package/dist/notebooks/transcendentals.html +195 -0
- package/dist/notebooks/view-vendored-d3.html +78 -0
- package/dist/vendor/d3-array/src/array.js +4 -0
- package/dist/vendor/d3-array/src/ascending.js +3 -0
- package/dist/vendor/d3-array/src/bin.js +125 -0
- package/dist/vendor/d3-array/src/bisect.js +9 -0
- package/dist/vendor/d3-array/src/bisector.js +56 -0
- package/dist/vendor/d3-array/src/blur.js +115 -0
- package/dist/vendor/d3-array/src/constant.js +3 -0
- package/dist/vendor/d3-array/src/count.js +18 -0
- package/dist/vendor/d3-array/src/cross.js +33 -0
- package/dist/vendor/d3-array/src/cumsum.js +6 -0
- package/dist/vendor/d3-array/src/descending.js +7 -0
- package/dist/vendor/d3-array/src/deviation.js +6 -0
- package/dist/vendor/d3-array/src/difference.js +11 -0
- package/dist/vendor/d3-array/src/disjoint.js +15 -0
- package/dist/vendor/d3-array/src/every.js +10 -0
- package/dist/vendor/d3-array/src/extent.js +29 -0
- package/dist/vendor/d3-array/src/filter.js +11 -0
- package/dist/vendor/d3-array/src/fsum.js +69 -0
- package/dist/vendor/d3-array/src/greatest.js +29 -0
- package/dist/vendor/d3-array/src/greatestIndex.js +19 -0
- package/dist/vendor/d3-array/src/group.js +65 -0
- package/dist/vendor/d3-array/src/groupSort.js +10 -0
- package/dist/vendor/d3-array/src/identity.js +3 -0
- package/dist/vendor/d3-array/src/index.js +57 -0
- package/dist/vendor/d3-array/src/intersection.js +19 -0
- package/dist/vendor/d3-array/src/least.js +29 -0
- package/dist/vendor/d3-array/src/leastIndex.js +19 -0
- package/dist/vendor/d3-array/src/map.js +5 -0
- package/dist/vendor/d3-array/src/max.js +20 -0
- package/dist/vendor/d3-array/src/maxIndex.js +22 -0
- package/dist/vendor/d3-array/src/mean.js +19 -0
- package/dist/vendor/d3-array/src/median.js +9 -0
- package/dist/vendor/d3-array/src/merge.js +9 -0
- package/dist/vendor/d3-array/src/min.js +20 -0
- package/dist/vendor/d3-array/src/minIndex.js +22 -0
- package/dist/vendor/d3-array/src/mode.js +28 -0
- package/dist/vendor/d3-array/src/nice.js +18 -0
- package/dist/vendor/d3-array/src/number.js +20 -0
- package/dist/vendor/d3-array/src/pairs.js +15 -0
- package/dist/vendor/d3-array/src/permute.js +3 -0
- package/dist/vendor/d3-array/src/quantile.js +47 -0
- package/dist/vendor/d3-array/src/quickselect.js +53 -0
- package/dist/vendor/d3-array/src/range.js +13 -0
- package/dist/vendor/d3-array/src/rank.js +24 -0
- package/dist/vendor/d3-array/src/reduce.js +14 -0
- package/dist/vendor/d3-array/src/reverse.js +4 -0
- package/dist/vendor/d3-array/src/scan.js +6 -0
- package/dist/vendor/d3-array/src/shuffle.js +13 -0
- package/dist/vendor/d3-array/src/some.js +10 -0
- package/dist/vendor/d3-array/src/sort.js +39 -0
- package/dist/vendor/d3-array/src/subset.js +5 -0
- package/dist/vendor/d3-array/src/sum.js +18 -0
- package/dist/vendor/d3-array/src/superset.js +19 -0
- package/dist/vendor/d3-array/src/threshold/freedmanDiaconis.js +7 -0
- package/dist/vendor/d3-array/src/threshold/scott.js +7 -0
- package/dist/vendor/d3-array/src/threshold/sturges.js +5 -0
- package/dist/vendor/d3-array/src/ticks.js +55 -0
- package/dist/vendor/d3-array/src/transpose.js +15 -0
- package/dist/vendor/d3-array/src/union.js +11 -0
- package/dist/vendor/d3-array/src/variance.js +25 -0
- package/dist/vendor/d3-array/src/zip.js +5 -0
- package/dist/vendor/internmap/src/index.js +61 -0
- package/dist/vendor/manifest.json +11 -0
- package/dist/wasm/egpt_math_sdk_wasm.d.ts +214 -0
- package/dist/wasm/egpt_math_sdk_wasm.js +1120 -0
- package/dist/wasm/egpt_math_sdk_wasm_bg.wasm +0 -0
- package/dist/wasm/egpt_math_sdk_wasm_bg.wasm.d.ts +66 -0
- package/dist/wasm/manifest.json +5 -0
- package/package.json +53 -0
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
export function blur(values, r) {
|
|
2
|
+
if (!((r = +r) >= 0)) throw new RangeError("invalid r");
|
|
3
|
+
let length = values.length;
|
|
4
|
+
if (!((length = Math.floor(length)) >= 0)) throw new RangeError("invalid length");
|
|
5
|
+
if (!length || !r) return values;
|
|
6
|
+
const blur = blurf(r);
|
|
7
|
+
const temp = values.slice();
|
|
8
|
+
blur(values, temp, 0, length, 1);
|
|
9
|
+
blur(temp, values, 0, length, 1);
|
|
10
|
+
blur(values, temp, 0, length, 1);
|
|
11
|
+
return values;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const blur2 = Blur2(blurf);
|
|
15
|
+
|
|
16
|
+
export const blurImage = Blur2(blurfImage);
|
|
17
|
+
|
|
18
|
+
function Blur2(blur) {
|
|
19
|
+
return function(data, rx, ry = rx) {
|
|
20
|
+
if (!((rx = +rx) >= 0)) throw new RangeError("invalid rx");
|
|
21
|
+
if (!((ry = +ry) >= 0)) throw new RangeError("invalid ry");
|
|
22
|
+
let {data: values, width, height} = data;
|
|
23
|
+
if (!((width = Math.floor(width)) >= 0)) throw new RangeError("invalid width");
|
|
24
|
+
if (!((height = Math.floor(height !== undefined ? height : values.length / width)) >= 0)) throw new RangeError("invalid height");
|
|
25
|
+
if (!width || !height || (!rx && !ry)) return data;
|
|
26
|
+
const blurx = rx && blur(rx);
|
|
27
|
+
const blury = ry && blur(ry);
|
|
28
|
+
const temp = values.slice();
|
|
29
|
+
if (blurx && blury) {
|
|
30
|
+
blurh(blurx, temp, values, width, height);
|
|
31
|
+
blurh(blurx, values, temp, width, height);
|
|
32
|
+
blurh(blurx, temp, values, width, height);
|
|
33
|
+
blurv(blury, values, temp, width, height);
|
|
34
|
+
blurv(blury, temp, values, width, height);
|
|
35
|
+
blurv(blury, values, temp, width, height);
|
|
36
|
+
} else if (blurx) {
|
|
37
|
+
blurh(blurx, values, temp, width, height);
|
|
38
|
+
blurh(blurx, temp, values, width, height);
|
|
39
|
+
blurh(blurx, values, temp, width, height);
|
|
40
|
+
} else if (blury) {
|
|
41
|
+
blurv(blury, values, temp, width, height);
|
|
42
|
+
blurv(blury, temp, values, width, height);
|
|
43
|
+
blurv(blury, values, temp, width, height);
|
|
44
|
+
}
|
|
45
|
+
return data;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function blurh(blur, T, S, w, h) {
|
|
50
|
+
for (let y = 0, n = w * h; y < n;) {
|
|
51
|
+
blur(T, S, y, y += w, 1);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function blurv(blur, T, S, w, h) {
|
|
56
|
+
for (let x = 0, n = w * h; x < w; ++x) {
|
|
57
|
+
blur(T, S, x, x + n, w);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function blurfImage(radius) {
|
|
62
|
+
const blur = blurf(radius);
|
|
63
|
+
return (T, S, start, stop, step) => {
|
|
64
|
+
start <<= 2, stop <<= 2, step <<= 2;
|
|
65
|
+
blur(T, S, start + 0, stop + 0, step);
|
|
66
|
+
blur(T, S, start + 1, stop + 1, step);
|
|
67
|
+
blur(T, S, start + 2, stop + 2, step);
|
|
68
|
+
blur(T, S, start + 3, stop + 3, step);
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Given a target array T, a source array S, sets each value T[i] to the average
|
|
73
|
+
// of {S[i - r], …, S[i], …, S[i + r]}, where r = ⌊radius⌋, start <= i < stop,
|
|
74
|
+
// for each i, i + step, i + 2 * step, etc., and where S[j] is clamped between
|
|
75
|
+
// S[start] (inclusive) and S[stop] (exclusive). If the given radius is not an
|
|
76
|
+
// integer, S[i - r - 1] and S[i + r + 1] are added to the sum, each weighted
|
|
77
|
+
// according to r - ⌊radius⌋.
|
|
78
|
+
function blurf(radius) {
|
|
79
|
+
const radius0 = Math.floor(radius);
|
|
80
|
+
if (radius0 === radius) return bluri(radius);
|
|
81
|
+
const t = radius - radius0;
|
|
82
|
+
const w = 2 * radius + 1;
|
|
83
|
+
return (T, S, start, stop, step) => { // stop must be aligned!
|
|
84
|
+
if (!((stop -= step) >= start)) return; // inclusive stop
|
|
85
|
+
let sum = radius0 * S[start];
|
|
86
|
+
const s0 = step * radius0;
|
|
87
|
+
const s1 = s0 + step;
|
|
88
|
+
for (let i = start, j = start + s0; i < j; i += step) {
|
|
89
|
+
sum += S[Math.min(stop, i)];
|
|
90
|
+
}
|
|
91
|
+
for (let i = start, j = stop; i <= j; i += step) {
|
|
92
|
+
sum += S[Math.min(stop, i + s0)];
|
|
93
|
+
T[i] = (sum + t * (S[Math.max(start, i - s1)] + S[Math.min(stop, i + s1)])) / w;
|
|
94
|
+
sum -= S[Math.max(start, i - s0)];
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Like blurf, but optimized for integer radius.
|
|
100
|
+
function bluri(radius) {
|
|
101
|
+
const w = 2 * radius + 1;
|
|
102
|
+
return (T, S, start, stop, step) => { // stop must be aligned!
|
|
103
|
+
if (!((stop -= step) >= start)) return; // inclusive stop
|
|
104
|
+
let sum = radius * S[start];
|
|
105
|
+
const s = step * radius;
|
|
106
|
+
for (let i = start, j = start + s; i < j; i += step) {
|
|
107
|
+
sum += S[Math.min(stop, i)];
|
|
108
|
+
}
|
|
109
|
+
for (let i = start, j = stop; i <= j; i += step) {
|
|
110
|
+
sum += S[Math.min(stop, i + s)];
|
|
111
|
+
T[i] = sum / w;
|
|
112
|
+
sum -= S[Math.max(start, i - s)];
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export default function count(values, valueof) {
|
|
2
|
+
let count = 0;
|
|
3
|
+
if (valueof === undefined) {
|
|
4
|
+
for (let value of values) {
|
|
5
|
+
if (value != null && (value = +value) >= value) {
|
|
6
|
+
++count;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
} else {
|
|
10
|
+
let index = -1;
|
|
11
|
+
for (let value of values) {
|
|
12
|
+
if ((value = valueof(value, ++index, values)) != null && (value = +value) >= value) {
|
|
13
|
+
++count;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return count;
|
|
18
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
function length(array) {
|
|
2
|
+
return array.length | 0;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
function empty(length) {
|
|
6
|
+
return !(length > 0);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function arrayify(values) {
|
|
10
|
+
return typeof values !== "object" || "length" in values ? values : Array.from(values);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function reducer(reduce) {
|
|
14
|
+
return values => reduce(...values);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export default function cross(...values) {
|
|
18
|
+
const reduce = typeof values[values.length - 1] === "function" && reducer(values.pop());
|
|
19
|
+
values = values.map(arrayify);
|
|
20
|
+
const lengths = values.map(length);
|
|
21
|
+
const j = values.length - 1;
|
|
22
|
+
const index = new Array(j + 1).fill(0);
|
|
23
|
+
const product = [];
|
|
24
|
+
if (j < 0 || lengths.some(empty)) return product;
|
|
25
|
+
while (true) {
|
|
26
|
+
product.push(index.map((j, i) => values[i][j]));
|
|
27
|
+
let i = j;
|
|
28
|
+
while (++index[i] === lengths[i]) {
|
|
29
|
+
if (i === 0) return reduce ? product.map(reduce) : product;
|
|
30
|
+
index[i--] = 0;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {InternSet} from "internmap";
|
|
2
|
+
|
|
3
|
+
export default function disjoint(values, other) {
|
|
4
|
+
const iterator = other[Symbol.iterator](), set = new InternSet();
|
|
5
|
+
for (const v of values) {
|
|
6
|
+
if (set.has(v)) return false;
|
|
7
|
+
let value, done;
|
|
8
|
+
while (({value, done} = iterator.next())) {
|
|
9
|
+
if (done) break;
|
|
10
|
+
if (Object.is(v, value)) return false;
|
|
11
|
+
set.add(value);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export default function extent(values, valueof) {
|
|
2
|
+
let min;
|
|
3
|
+
let max;
|
|
4
|
+
if (valueof === undefined) {
|
|
5
|
+
for (const value of values) {
|
|
6
|
+
if (value != null) {
|
|
7
|
+
if (min === undefined) {
|
|
8
|
+
if (value >= value) min = max = value;
|
|
9
|
+
} else {
|
|
10
|
+
if (min > value) min = value;
|
|
11
|
+
if (max < value) max = value;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
} else {
|
|
16
|
+
let index = -1;
|
|
17
|
+
for (let value of values) {
|
|
18
|
+
if ((value = valueof(value, ++index, values)) != null) {
|
|
19
|
+
if (min === undefined) {
|
|
20
|
+
if (value >= value) min = max = value;
|
|
21
|
+
} else {
|
|
22
|
+
if (min > value) min = value;
|
|
23
|
+
if (max < value) max = value;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return [min, max];
|
|
29
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export default function filter(values, test) {
|
|
2
|
+
if (typeof test !== "function") throw new TypeError("test is not a function");
|
|
3
|
+
const array = [];
|
|
4
|
+
let index = -1;
|
|
5
|
+
for (const value of values) {
|
|
6
|
+
if (test(value, ++index, values)) {
|
|
7
|
+
array.push(value);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
return array;
|
|
11
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// https://github.com/python/cpython/blob/a74eea238f5baba15797e2e8b570d153bc8690a7/Modules/mathmodule.c#L1423
|
|
2
|
+
export class Adder {
|
|
3
|
+
constructor() {
|
|
4
|
+
this._partials = new Float64Array(32);
|
|
5
|
+
this._n = 0;
|
|
6
|
+
}
|
|
7
|
+
add(x) {
|
|
8
|
+
const p = this._partials;
|
|
9
|
+
let i = 0;
|
|
10
|
+
for (let j = 0; j < this._n && j < 32; j++) {
|
|
11
|
+
const y = p[j],
|
|
12
|
+
hi = x + y,
|
|
13
|
+
lo = Math.abs(x) < Math.abs(y) ? x - (hi - y) : y - (hi - x);
|
|
14
|
+
if (lo) p[i++] = lo;
|
|
15
|
+
x = hi;
|
|
16
|
+
}
|
|
17
|
+
p[i] = x;
|
|
18
|
+
this._n = i + 1;
|
|
19
|
+
return this;
|
|
20
|
+
}
|
|
21
|
+
valueOf() {
|
|
22
|
+
const p = this._partials;
|
|
23
|
+
let n = this._n, x, y, lo, hi = 0;
|
|
24
|
+
if (n > 0) {
|
|
25
|
+
hi = p[--n];
|
|
26
|
+
while (n > 0) {
|
|
27
|
+
x = hi;
|
|
28
|
+
y = p[--n];
|
|
29
|
+
hi = x + y;
|
|
30
|
+
lo = y - (hi - x);
|
|
31
|
+
if (lo) break;
|
|
32
|
+
}
|
|
33
|
+
if (n > 0 && ((lo < 0 && p[n - 1] < 0) || (lo > 0 && p[n - 1] > 0))) {
|
|
34
|
+
y = lo * 2;
|
|
35
|
+
x = hi + y;
|
|
36
|
+
if (y == x - hi) hi = x;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return hi;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function fsum(values, valueof) {
|
|
44
|
+
const adder = new Adder();
|
|
45
|
+
if (valueof === undefined) {
|
|
46
|
+
for (let value of values) {
|
|
47
|
+
if (value = +value) {
|
|
48
|
+
adder.add(value);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
} else {
|
|
52
|
+
let index = -1;
|
|
53
|
+
for (let value of values) {
|
|
54
|
+
if (value = +valueof(value, ++index, values)) {
|
|
55
|
+
adder.add(value);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return +adder;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function fcumsum(values, valueof) {
|
|
63
|
+
const adder = new Adder();
|
|
64
|
+
let index = -1;
|
|
65
|
+
return Float64Array.from(values, valueof === undefined
|
|
66
|
+
? v => adder.add(+v || 0)
|
|
67
|
+
: v => adder.add(+valueof(v, ++index, values) || 0)
|
|
68
|
+
);
|
|
69
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import ascending from "./ascending.js";
|
|
2
|
+
|
|
3
|
+
export default function greatest(values, compare = ascending) {
|
|
4
|
+
let max;
|
|
5
|
+
let defined = false;
|
|
6
|
+
if (compare.length === 1) {
|
|
7
|
+
let maxValue;
|
|
8
|
+
for (const element of values) {
|
|
9
|
+
const value = compare(element);
|
|
10
|
+
if (defined
|
|
11
|
+
? ascending(value, maxValue) > 0
|
|
12
|
+
: ascending(value, value) === 0) {
|
|
13
|
+
max = element;
|
|
14
|
+
maxValue = value;
|
|
15
|
+
defined = true;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
} else {
|
|
19
|
+
for (const value of values) {
|
|
20
|
+
if (defined
|
|
21
|
+
? compare(value, max) > 0
|
|
22
|
+
: compare(value, value) === 0) {
|
|
23
|
+
max = value;
|
|
24
|
+
defined = true;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return max;
|
|
29
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import ascending from "./ascending.js";
|
|
2
|
+
import maxIndex from "./maxIndex.js";
|
|
3
|
+
|
|
4
|
+
export default function greatestIndex(values, compare = ascending) {
|
|
5
|
+
if (compare.length === 1) return maxIndex(values, compare);
|
|
6
|
+
let maxValue;
|
|
7
|
+
let max = -1;
|
|
8
|
+
let index = -1;
|
|
9
|
+
for (const value of values) {
|
|
10
|
+
++index;
|
|
11
|
+
if (max < 0
|
|
12
|
+
? compare(value, value) === 0
|
|
13
|
+
: compare(value, maxValue) > 0) {
|
|
14
|
+
maxValue = value;
|
|
15
|
+
max = index;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return max;
|
|
19
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import {InternMap} from "internmap";
|
|
2
|
+
import identity from "./identity.js";
|
|
3
|
+
|
|
4
|
+
export default function group(values, ...keys) {
|
|
5
|
+
return nest(values, identity, identity, keys);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function groups(values, ...keys) {
|
|
9
|
+
return nest(values, Array.from, identity, keys);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function flatten(groups, keys) {
|
|
13
|
+
for (let i = 1, n = keys.length; i < n; ++i) {
|
|
14
|
+
groups = groups.flatMap(g => g.pop().map(([key, value]) => [...g, key, value]));
|
|
15
|
+
}
|
|
16
|
+
return groups;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function flatGroup(values, ...keys) {
|
|
20
|
+
return flatten(groups(values, ...keys), keys);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function flatRollup(values, reduce, ...keys) {
|
|
24
|
+
return flatten(rollups(values, reduce, ...keys), keys);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function rollup(values, reduce, ...keys) {
|
|
28
|
+
return nest(values, identity, reduce, keys);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function rollups(values, reduce, ...keys) {
|
|
32
|
+
return nest(values, Array.from, reduce, keys);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function index(values, ...keys) {
|
|
36
|
+
return nest(values, identity, unique, keys);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function indexes(values, ...keys) {
|
|
40
|
+
return nest(values, Array.from, unique, keys);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function unique(values) {
|
|
44
|
+
if (values.length !== 1) throw new Error("duplicate key");
|
|
45
|
+
return values[0];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function nest(values, map, reduce, keys) {
|
|
49
|
+
return (function regroup(values, i) {
|
|
50
|
+
if (i >= keys.length) return reduce(values);
|
|
51
|
+
const groups = new InternMap();
|
|
52
|
+
const keyof = keys[i++];
|
|
53
|
+
let index = -1;
|
|
54
|
+
for (const value of values) {
|
|
55
|
+
const key = keyof(value, ++index, values);
|
|
56
|
+
const group = groups.get(key);
|
|
57
|
+
if (group) group.push(value);
|
|
58
|
+
else groups.set(key, [value]);
|
|
59
|
+
}
|
|
60
|
+
for (const [key, values] of groups) {
|
|
61
|
+
groups.set(key, regroup(values, i));
|
|
62
|
+
}
|
|
63
|
+
return map(groups);
|
|
64
|
+
})(values, 0);
|
|
65
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import ascending from "./ascending.js";
|
|
2
|
+
import group, {rollup} from "./group.js";
|
|
3
|
+
import sort from "./sort.js";
|
|
4
|
+
|
|
5
|
+
export default function groupSort(values, reduce, key) {
|
|
6
|
+
return (reduce.length !== 2
|
|
7
|
+
? sort(rollup(values, reduce, key), (([ak, av], [bk, bv]) => ascending(av, bv) || ascending(ak, bk)))
|
|
8
|
+
: sort(group(values, key), (([ak, av], [bk, bv]) => reduce(av, bv) || ascending(ak, bk))))
|
|
9
|
+
.map(([key]) => key);
|
|
10
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export {default as bisect, bisectRight, bisectLeft, bisectCenter} from "./bisect.js";
|
|
2
|
+
export {default as ascending} from "./ascending.js";
|
|
3
|
+
export {default as bisector} from "./bisector.js";
|
|
4
|
+
export {blur, blur2, blurImage} from "./blur.js";
|
|
5
|
+
export {default as count} from "./count.js";
|
|
6
|
+
export {default as cross} from "./cross.js";
|
|
7
|
+
export {default as cumsum} from "./cumsum.js";
|
|
8
|
+
export {default as descending} from "./descending.js";
|
|
9
|
+
export {default as deviation} from "./deviation.js";
|
|
10
|
+
export {default as extent} from "./extent.js";
|
|
11
|
+
export {Adder, fsum, fcumsum} from "./fsum.js";
|
|
12
|
+
export {default as group, flatGroup, flatRollup, groups, index, indexes, rollup, rollups} from "./group.js";
|
|
13
|
+
export {default as groupSort} from "./groupSort.js";
|
|
14
|
+
export {default as bin, default as histogram} from "./bin.js"; // Deprecated; use bin.
|
|
15
|
+
export {default as thresholdFreedmanDiaconis} from "./threshold/freedmanDiaconis.js";
|
|
16
|
+
export {default as thresholdScott} from "./threshold/scott.js";
|
|
17
|
+
export {default as thresholdSturges} from "./threshold/sturges.js";
|
|
18
|
+
export {default as max} from "./max.js";
|
|
19
|
+
export {default as maxIndex} from "./maxIndex.js";
|
|
20
|
+
export {default as mean} from "./mean.js";
|
|
21
|
+
export {default as median, medianIndex} from "./median.js";
|
|
22
|
+
export {default as merge} from "./merge.js";
|
|
23
|
+
export {default as min} from "./min.js";
|
|
24
|
+
export {default as minIndex} from "./minIndex.js";
|
|
25
|
+
export {default as mode} from "./mode.js";
|
|
26
|
+
export {default as nice} from "./nice.js";
|
|
27
|
+
export {default as pairs} from "./pairs.js";
|
|
28
|
+
export {default as permute} from "./permute.js";
|
|
29
|
+
export {default as quantile, quantileIndex, quantileSorted} from "./quantile.js";
|
|
30
|
+
export {default as quickselect} from "./quickselect.js";
|
|
31
|
+
export {default as range} from "./range.js";
|
|
32
|
+
export {default as rank} from "./rank.js";
|
|
33
|
+
export {default as least} from "./least.js";
|
|
34
|
+
export {default as leastIndex} from "./leastIndex.js";
|
|
35
|
+
export {default as greatest} from "./greatest.js";
|
|
36
|
+
export {default as greatestIndex} from "./greatestIndex.js";
|
|
37
|
+
export {default as scan} from "./scan.js"; // Deprecated; use leastIndex.
|
|
38
|
+
export {default as shuffle, shuffler} from "./shuffle.js";
|
|
39
|
+
export {default as sum} from "./sum.js";
|
|
40
|
+
export {default as ticks, tickIncrement, tickStep} from "./ticks.js";
|
|
41
|
+
export {default as transpose} from "./transpose.js";
|
|
42
|
+
export {default as variance} from "./variance.js";
|
|
43
|
+
export {default as zip} from "./zip.js";
|
|
44
|
+
export {default as every} from "./every.js";
|
|
45
|
+
export {default as some} from "./some.js";
|
|
46
|
+
export {default as filter} from "./filter.js";
|
|
47
|
+
export {default as map} from "./map.js";
|
|
48
|
+
export {default as reduce} from "./reduce.js";
|
|
49
|
+
export {default as reverse} from "./reverse.js";
|
|
50
|
+
export {default as sort} from "./sort.js";
|
|
51
|
+
export {default as difference} from "./difference.js";
|
|
52
|
+
export {default as disjoint} from "./disjoint.js";
|
|
53
|
+
export {default as intersection} from "./intersection.js";
|
|
54
|
+
export {default as subset} from "./subset.js";
|
|
55
|
+
export {default as superset} from "./superset.js";
|
|
56
|
+
export {default as union} from "./union.js";
|
|
57
|
+
export {InternMap, InternSet} from "internmap";
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {InternSet} from "internmap";
|
|
2
|
+
|
|
3
|
+
export default function intersection(values, ...others) {
|
|
4
|
+
values = new InternSet(values);
|
|
5
|
+
others = others.map(set);
|
|
6
|
+
out: for (const value of values) {
|
|
7
|
+
for (const other of others) {
|
|
8
|
+
if (!other.has(value)) {
|
|
9
|
+
values.delete(value);
|
|
10
|
+
continue out;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return values;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function set(values) {
|
|
18
|
+
return values instanceof InternSet ? values : new InternSet(values);
|
|
19
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import ascending from "./ascending.js";
|
|
2
|
+
|
|
3
|
+
export default function least(values, compare = ascending) {
|
|
4
|
+
let min;
|
|
5
|
+
let defined = false;
|
|
6
|
+
if (compare.length === 1) {
|
|
7
|
+
let minValue;
|
|
8
|
+
for (const element of values) {
|
|
9
|
+
const value = compare(element);
|
|
10
|
+
if (defined
|
|
11
|
+
? ascending(value, minValue) < 0
|
|
12
|
+
: ascending(value, value) === 0) {
|
|
13
|
+
min = element;
|
|
14
|
+
minValue = value;
|
|
15
|
+
defined = true;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
} else {
|
|
19
|
+
for (const value of values) {
|
|
20
|
+
if (defined
|
|
21
|
+
? compare(value, min) < 0
|
|
22
|
+
: compare(value, value) === 0) {
|
|
23
|
+
min = value;
|
|
24
|
+
defined = true;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return min;
|
|
29
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import ascending from "./ascending.js";
|
|
2
|
+
import minIndex from "./minIndex.js";
|
|
3
|
+
|
|
4
|
+
export default function leastIndex(values, compare = ascending) {
|
|
5
|
+
if (compare.length === 1) return minIndex(values, compare);
|
|
6
|
+
let minValue;
|
|
7
|
+
let min = -1;
|
|
8
|
+
let index = -1;
|
|
9
|
+
for (const value of values) {
|
|
10
|
+
++index;
|
|
11
|
+
if (min < 0
|
|
12
|
+
? compare(value, value) === 0
|
|
13
|
+
: compare(value, minValue) < 0) {
|
|
14
|
+
minValue = value;
|
|
15
|
+
min = index;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return min;
|
|
19
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export default function map(values, mapper) {
|
|
2
|
+
if (typeof values[Symbol.iterator] !== "function") throw new TypeError("values is not iterable");
|
|
3
|
+
if (typeof mapper !== "function") throw new TypeError("mapper is not a function");
|
|
4
|
+
return Array.from(values, (value, index) => mapper(value, index, values));
|
|
5
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export default function max(values, valueof) {
|
|
2
|
+
let max;
|
|
3
|
+
if (valueof === undefined) {
|
|
4
|
+
for (const value of values) {
|
|
5
|
+
if (value != null
|
|
6
|
+
&& (max < value || (max === undefined && value >= value))) {
|
|
7
|
+
max = value;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
} else {
|
|
11
|
+
let index = -1;
|
|
12
|
+
for (let value of values) {
|
|
13
|
+
if ((value = valueof(value, ++index, values)) != null
|
|
14
|
+
&& (max < value || (max === undefined && value >= value))) {
|
|
15
|
+
max = value;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return max;
|
|
20
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export default function maxIndex(values, valueof) {
|
|
2
|
+
let max;
|
|
3
|
+
let maxIndex = -1;
|
|
4
|
+
let index = -1;
|
|
5
|
+
if (valueof === undefined) {
|
|
6
|
+
for (const value of values) {
|
|
7
|
+
++index;
|
|
8
|
+
if (value != null
|
|
9
|
+
&& (max < value || (max === undefined && value >= value))) {
|
|
10
|
+
max = value, maxIndex = index;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
} else {
|
|
14
|
+
for (let value of values) {
|
|
15
|
+
if ((value = valueof(value, ++index, values)) != null
|
|
16
|
+
&& (max < value || (max === undefined && value >= value))) {
|
|
17
|
+
max = value, maxIndex = index;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return maxIndex;
|
|
22
|
+
}
|