@johnhenry/math 0.0.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 +165 -0
- package/dist/Bivector4.d.ts +52 -0
- package/dist/Bivector4.d.ts.map +1 -0
- package/dist/Bivector4.js +105 -0
- package/dist/Bivector4.js.map +1 -0
- package/dist/CellGraph.d.ts +259 -0
- package/dist/CellGraph.d.ts.map +1 -0
- package/dist/CellGraph.js +637 -0
- package/dist/CellGraph.js.map +1 -0
- package/dist/Clifford4Internal.d.ts +35 -0
- package/dist/Clifford4Internal.d.ts.map +1 -0
- package/dist/Clifford4Internal.js +86 -0
- package/dist/Clifford4Internal.js.map +1 -0
- package/dist/Combinatorics.d.ts +61 -0
- package/dist/Combinatorics.d.ts.map +1 -0
- package/dist/Combinatorics.js +186 -0
- package/dist/Combinatorics.js.map +1 -0
- package/dist/CombinatoricsGenerators.d.ts +44 -0
- package/dist/CombinatoricsGenerators.d.ts.map +1 -0
- package/dist/CombinatoricsGenerators.js +148 -0
- package/dist/CombinatoricsGenerators.js.map +1 -0
- package/dist/ComplexMath.d.ts +46 -0
- package/dist/ComplexMath.d.ts.map +1 -0
- package/dist/ComplexMath.js +98 -0
- package/dist/ComplexMath.js.map +1 -0
- package/dist/ComplexNumber.d.ts +174 -0
- package/dist/ComplexNumber.d.ts.map +1 -0
- package/dist/ComplexNumber.js +443 -0
- package/dist/ComplexNumber.js.map +1 -0
- package/dist/Cycle.d.ts +42 -0
- package/dist/Cycle.d.ts.map +1 -0
- package/dist/Cycle.js +120 -0
- package/dist/Cycle.js.map +1 -0
- package/dist/Decimal.d.ts +45 -0
- package/dist/Decimal.d.ts.map +1 -0
- package/dist/Decimal.js +135 -0
- package/dist/Decimal.js.map +1 -0
- package/dist/Distributions.d.ts +55 -0
- package/dist/Distributions.d.ts.map +1 -0
- package/dist/Distributions.js +222 -0
- package/dist/Distributions.js.map +1 -0
- package/dist/DualNumber.d.ts +42 -0
- package/dist/DualNumber.d.ts.map +1 -0
- package/dist/DualNumber.js +91 -0
- package/dist/DualNumber.js.map +1 -0
- package/dist/Environment.d.ts +44 -0
- package/dist/Environment.d.ts.map +1 -0
- package/dist/Environment.js +129 -0
- package/dist/Environment.js.map +1 -0
- package/dist/Expression.d.ts +19 -0
- package/dist/Expression.d.ts.map +1 -0
- package/dist/Expression.js +43 -0
- package/dist/Expression.js.map +1 -0
- package/dist/FFT.d.ts +21 -0
- package/dist/FFT.d.ts.map +1 -0
- package/dist/FFT.js +136 -0
- package/dist/FFT.js.map +1 -0
- package/dist/GeometricAlgebra4.d.ts +73 -0
- package/dist/GeometricAlgebra4.d.ts.map +1 -0
- package/dist/GeometricAlgebra4.js +115 -0
- package/dist/GeometricAlgebra4.js.map +1 -0
- package/dist/Geometry.d.ts +41 -0
- package/dist/Geometry.d.ts.map +1 -0
- package/dist/Geometry.js +167 -0
- package/dist/Geometry.js.map +1 -0
- package/dist/Graph.d.ts +72 -0
- package/dist/Graph.d.ts.map +1 -0
- package/dist/Graph.js +337 -0
- package/dist/Graph.js.map +1 -0
- package/dist/Graph3DUtils.d.ts +80 -0
- package/dist/Graph3DUtils.d.ts.map +1 -0
- package/dist/Graph3DUtils.js +156 -0
- package/dist/Graph3DUtils.js.map +1 -0
- package/dist/GraphUtils.d.ts +67 -0
- package/dist/GraphUtils.d.ts.map +1 -0
- package/dist/GraphUtils.js +72 -0
- package/dist/GraphUtils.js.map +1 -0
- package/dist/GroupTheory.d.ts +73 -0
- package/dist/GroupTheory.d.ts.map +1 -0
- package/dist/GroupTheory.js +194 -0
- package/dist/GroupTheory.js.map +1 -0
- package/dist/IntUtils.d.ts +33 -0
- package/dist/IntUtils.d.ts.map +1 -0
- package/dist/IntUtils.js +185 -0
- package/dist/IntUtils.js.map +1 -0
- package/dist/Interval.d.ts +44 -0
- package/dist/Interval.d.ts.map +1 -0
- package/dist/Interval.js +165 -0
- package/dist/Interval.js.map +1 -0
- package/dist/Lattice.d.ts +48 -0
- package/dist/Lattice.d.ts.map +1 -0
- package/dist/Lattice.js +65 -0
- package/dist/Lattice.js.map +1 -0
- package/dist/Logic.d.ts +22 -0
- package/dist/Logic.d.ts.map +1 -0
- package/dist/Logic.js +34 -0
- package/dist/Logic.js.map +1 -0
- package/dist/MatrixMath.d.ts +71 -0
- package/dist/MatrixMath.d.ts.map +1 -0
- package/dist/MatrixMath.js +302 -0
- package/dist/MatrixMath.js.map +1 -0
- package/dist/NumberTheory.d.ts +49 -0
- package/dist/NumberTheory.d.ts.map +1 -0
- package/dist/NumberTheory.js +230 -0
- package/dist/NumberTheory.js.map +1 -0
- package/dist/Numerical.d.ts +85 -0
- package/dist/Numerical.d.ts.map +1 -0
- package/dist/Numerical.js +343 -0
- package/dist/Numerical.js.map +1 -0
- package/dist/Permutation.d.ts +43 -0
- package/dist/Permutation.d.ts.map +1 -0
- package/dist/Permutation.js +142 -0
- package/dist/Permutation.js.map +1 -0
- package/dist/Polygon.d.ts +64 -0
- package/dist/Polygon.d.ts.map +1 -0
- package/dist/Polygon.js +221 -0
- package/dist/Polygon.js.map +1 -0
- package/dist/PolynomialRing.d.ts +74 -0
- package/dist/PolynomialRing.d.ts.map +1 -0
- package/dist/PolynomialRing.js +231 -0
- package/dist/PolynomialRing.js.map +1 -0
- package/dist/Quaternion.d.ts +37 -0
- package/dist/Quaternion.d.ts.map +1 -0
- package/dist/Quaternion.js +106 -0
- package/dist/Quaternion.js.map +1 -0
- package/dist/Rational.d.ts +34 -0
- package/dist/Rational.d.ts.map +1 -0
- package/dist/Rational.js +136 -0
- package/dist/Rational.js.map +1 -0
- package/dist/RealMath.d.ts +112 -0
- package/dist/RealMath.d.ts.map +1 -0
- package/dist/RealMath.js +262 -0
- package/dist/RealMath.js.map +1 -0
- package/dist/Rotor4.d.ts +180 -0
- package/dist/Rotor4.d.ts.map +1 -0
- package/dist/Rotor4.js +388 -0
- package/dist/Rotor4.js.map +1 -0
- package/dist/SpecialFunctions.d.ts +32 -0
- package/dist/SpecialFunctions.d.ts.map +1 -0
- package/dist/SpecialFunctions.js +161 -0
- package/dist/SpecialFunctions.js.map +1 -0
- package/dist/SpecialOperator.d.ts +21 -0
- package/dist/SpecialOperator.d.ts.map +1 -0
- package/dist/SpecialOperator.js +26 -0
- package/dist/SpecialOperator.js.map +1 -0
- package/dist/Statistics.d.ts +50 -0
- package/dist/Statistics.d.ts.map +1 -0
- package/dist/Statistics.js +131 -0
- package/dist/Statistics.js.map +1 -0
- package/dist/StringEvaluator.d.ts +48 -0
- package/dist/StringEvaluator.d.ts.map +1 -0
- package/dist/StringEvaluator.js +190 -0
- package/dist/StringEvaluator.js.map +1 -0
- package/dist/Structure.d.ts +130 -0
- package/dist/Structure.d.ts.map +1 -0
- package/dist/Structure.js +394 -0
- package/dist/Structure.js.map +1 -0
- package/dist/Symbolic.d.ts +501 -0
- package/dist/Symbolic.d.ts.map +1 -0
- package/dist/Symbolic.js +4077 -0
- package/dist/Symbolic.js.map +1 -0
- package/dist/Type.d.ts +31 -0
- package/dist/Type.d.ts.map +1 -0
- package/dist/Type.js +47 -0
- package/dist/Type.js.map +1 -0
- package/dist/Utilities.d.ts +27 -0
- package/dist/Utilities.d.ts.map +1 -0
- package/dist/Utilities.js +47 -0
- package/dist/Utilities.js.map +1 -0
- package/dist/Vec4.d.ts +39 -0
- package/dist/Vec4.d.ts.map +1 -0
- package/dist/Vec4.js +82 -0
- package/dist/Vec4.js.map +1 -0
- package/dist/Vector.d.ts +83 -0
- package/dist/Vector.d.ts.map +1 -0
- package/dist/Vector.js +173 -0
- package/dist/Vector.js.map +1 -0
- package/dist/VectorCalculus.d.ts +42 -0
- package/dist/VectorCalculus.d.ts.map +1 -0
- package/dist/VectorCalculus.js +82 -0
- package/dist/VectorCalculus.js.map +1 -0
- package/dist/VectorUtils.d.ts +172 -0
- package/dist/VectorUtils.d.ts.map +1 -0
- package/dist/VectorUtils.js +706 -0
- package/dist/VectorUtils.js.map +1 -0
- package/dist/differentiation-rules.d.ts +21 -0
- package/dist/differentiation-rules.d.ts.map +1 -0
- package/dist/differentiation-rules.js +52 -0
- package/dist/differentiation-rules.js.map +1 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +66 -0
- package/dist/index.js.map +1 -0
- package/package.json +68 -0
- package/src/Bivector4.ts +136 -0
- package/src/CellGraph.ts +669 -0
- package/src/Clifford4Internal.ts +90 -0
- package/src/Combinatorics.ts +183 -0
- package/src/CombinatoricsGenerators.ts +144 -0
- package/src/ComplexMath.ts +162 -0
- package/src/ComplexNumber.ts +491 -0
- package/src/Cycle.ts +126 -0
- package/src/Decimal.ts +145 -0
- package/src/Distributions.ts +269 -0
- package/src/DualNumber.ts +105 -0
- package/src/Environment.ts +127 -0
- package/src/Expression.ts +48 -0
- package/src/FFT.ts +146 -0
- package/src/GeometricAlgebra4.ts +157 -0
- package/src/Geometry.ts +173 -0
- package/src/Graph.ts +337 -0
- package/src/Graph3DUtils.ts +269 -0
- package/src/GraphUtils.ts +159 -0
- package/src/GroupTheory.ts +214 -0
- package/src/IntUtils.ts +200 -0
- package/src/Interval.ts +171 -0
- package/src/Lattice.ts +87 -0
- package/src/Logic.ts +38 -0
- package/src/MatrixMath.ts +326 -0
- package/src/NumberTheory.ts +228 -0
- package/src/Numerical.ts +390 -0
- package/src/Permutation.ts +148 -0
- package/src/Polygon.ts +229 -0
- package/src/PolynomialRing.ts +242 -0
- package/src/Quaternion.ts +125 -0
- package/src/Rational.ts +148 -0
- package/src/RealMath.ts +346 -0
- package/src/Rotor4.ts +440 -0
- package/src/SpecialFunctions.ts +152 -0
- package/src/SpecialOperator.ts +28 -0
- package/src/Statistics.ts +170 -0
- package/src/StringEvaluator.ts +208 -0
- package/src/Structure.ts +481 -0
- package/src/Symbolic.ts +4313 -0
- package/src/Type.ts +48 -0
- package/src/Utilities.ts +49 -0
- package/src/Vec4.ts +98 -0
- package/src/Vector.ts +189 -0
- package/src/VectorCalculus.ts +87 -0
- package/src/VectorUtils.ts +797 -0
- package/src/differentiation-rules.ts +73 -0
- package/src/index.ts +140 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal Cl(4,0) geometric-algebra engine, used by {@link Vec4}, {@link Bivector4}
|
|
3
|
+
* and {@link Rotor4} to implement the geometric product, sandwich product, and
|
|
4
|
+
* exponential/logarithm maps correctly.
|
|
5
|
+
*
|
|
6
|
+
* Not exported from the package index — it is an implementation detail. Basis
|
|
7
|
+
* vectors e1..e4 are represented as bits 0..3 of a "blade" bitmask (0..15),
|
|
8
|
+
* e.g. blade 0b0011 = e1^e2, blade 0b1111 = e1^e2^e3^e4 (the pseudoscalar).
|
|
9
|
+
* Because the metric is Euclidean (e_i * e_i = +1 for every i — no negative or
|
|
10
|
+
* null basis vectors), the geometric product of two basis blades only needs a
|
|
11
|
+
* *reordering sign* (how many transpositions sort the concatenated index list
|
|
12
|
+
* into canonical order); there is no metric-signature contribution. This is
|
|
13
|
+
* the standard bitmask/blade technique used by geometric-algebra libraries
|
|
14
|
+
* (e.g. ganja.js), and is far less error-prone than hand-deriving each
|
|
15
|
+
* product formula term by term.
|
|
16
|
+
*/
|
|
17
|
+
export declare const BLADE_COUNT = 16;
|
|
18
|
+
export type Multivector4 = Float64Array;
|
|
19
|
+
export declare function mvZero(): Multivector4;
|
|
20
|
+
/** Full geometric product of two multivectors: a * b. */
|
|
21
|
+
export declare function geometricProductMV(a: Multivector4, b: Multivector4): Multivector4;
|
|
22
|
+
/** Outer (wedge) product: nonzero only for blade pairs that share no basis vector. */
|
|
23
|
+
export declare function wedgeProductMV(a: Multivector4, b: Multivector4): Multivector4;
|
|
24
|
+
export declare const E1: number;
|
|
25
|
+
export declare const E2: number;
|
|
26
|
+
export declare const E3: number;
|
|
27
|
+
export declare const E4: number;
|
|
28
|
+
export declare const E12: number;
|
|
29
|
+
export declare const E13: number;
|
|
30
|
+
export declare const E14: number;
|
|
31
|
+
export declare const E23: number;
|
|
32
|
+
export declare const E24: number;
|
|
33
|
+
export declare const E34: number;
|
|
34
|
+
export declare const PSEUDOSCALAR4: number;
|
|
35
|
+
//# sourceMappingURL=Clifford4Internal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Clifford4Internal.d.ts","sourceRoot":"","sources":["../src/Clifford4Internal.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,eAAO,MAAM,WAAW,KAAK,CAAC;AAuB9B,MAAM,MAAM,YAAY,GAAG,YAAY,CAAC;AAExC,wBAAgB,MAAM,IAAI,YAAY,CAErC;AAED,yDAAyD;AACzD,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,EAAE,YAAY,GAAG,YAAY,CAajF;AAED,sFAAsF;AACtF,wBAAgB,cAAc,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,EAAE,YAAY,GAAG,YAAY,CAa7E;AAGD,eAAO,MAAM,EAAE,QAAS,CAAC;AACzB,eAAO,MAAM,EAAE,QAAS,CAAC;AACzB,eAAO,MAAM,EAAE,QAAS,CAAC;AACzB,eAAO,MAAM,EAAE,QAAS,CAAC;AACzB,eAAO,MAAM,GAAG,QAAU,CAAC;AAC3B,eAAO,MAAM,GAAG,QAAU,CAAC;AAC3B,eAAO,MAAM,GAAG,QAAU,CAAC;AAC3B,eAAO,MAAM,GAAG,QAAU,CAAC;AAC3B,eAAO,MAAM,GAAG,QAAU,CAAC;AAC3B,eAAO,MAAM,GAAG,QAAU,CAAC;AAC3B,eAAO,MAAM,aAAa,QAAoB,CAAC"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal Cl(4,0) geometric-algebra engine, used by {@link Vec4}, {@link Bivector4}
|
|
3
|
+
* and {@link Rotor4} to implement the geometric product, sandwich product, and
|
|
4
|
+
* exponential/logarithm maps correctly.
|
|
5
|
+
*
|
|
6
|
+
* Not exported from the package index — it is an implementation detail. Basis
|
|
7
|
+
* vectors e1..e4 are represented as bits 0..3 of a "blade" bitmask (0..15),
|
|
8
|
+
* e.g. blade 0b0011 = e1^e2, blade 0b1111 = e1^e2^e3^e4 (the pseudoscalar).
|
|
9
|
+
* Because the metric is Euclidean (e_i * e_i = +1 for every i — no negative or
|
|
10
|
+
* null basis vectors), the geometric product of two basis blades only needs a
|
|
11
|
+
* *reordering sign* (how many transpositions sort the concatenated index list
|
|
12
|
+
* into canonical order); there is no metric-signature contribution. This is
|
|
13
|
+
* the standard bitmask/blade technique used by geometric-algebra libraries
|
|
14
|
+
* (e.g. ganja.js), and is far less error-prone than hand-deriving each
|
|
15
|
+
* product formula term by term.
|
|
16
|
+
*/
|
|
17
|
+
export const BLADE_COUNT = 16; // 2^4 basis blades in Cl(4,0)
|
|
18
|
+
function popcount(n) {
|
|
19
|
+
let count = 0;
|
|
20
|
+
let x = n;
|
|
21
|
+
while (x !== 0) {
|
|
22
|
+
count += x & 1;
|
|
23
|
+
x >>= 1;
|
|
24
|
+
}
|
|
25
|
+
return count;
|
|
26
|
+
}
|
|
27
|
+
/** Sign picked up reordering the concatenation of blade `a` then blade `b` into canonical order. */
|
|
28
|
+
function reorderingSign(a, b) {
|
|
29
|
+
let aShifted = a >> 1;
|
|
30
|
+
let swaps = 0;
|
|
31
|
+
while (aShifted !== 0) {
|
|
32
|
+
swaps += popcount(aShifted & b);
|
|
33
|
+
aShifted >>= 1;
|
|
34
|
+
}
|
|
35
|
+
return swaps % 2 === 0 ? 1 : -1;
|
|
36
|
+
}
|
|
37
|
+
export function mvZero() {
|
|
38
|
+
return new Float64Array(BLADE_COUNT);
|
|
39
|
+
}
|
|
40
|
+
/** Full geometric product of two multivectors: a * b. */
|
|
41
|
+
export function geometricProductMV(a, b) {
|
|
42
|
+
const out = mvZero();
|
|
43
|
+
for (let i = 0; i < BLADE_COUNT; i++) {
|
|
44
|
+
const ai = a[i];
|
|
45
|
+
if (!ai)
|
|
46
|
+
continue;
|
|
47
|
+
for (let j = 0; j < BLADE_COUNT; j++) {
|
|
48
|
+
const bj = b[j];
|
|
49
|
+
if (!bj)
|
|
50
|
+
continue;
|
|
51
|
+
const blade = i ^ j;
|
|
52
|
+
out[blade] += reorderingSign(i, j) * ai * bj;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
/** Outer (wedge) product: nonzero only for blade pairs that share no basis vector. */
|
|
58
|
+
export function wedgeProductMV(a, b) {
|
|
59
|
+
const out = mvZero();
|
|
60
|
+
for (let i = 0; i < BLADE_COUNT; i++) {
|
|
61
|
+
const ai = a[i];
|
|
62
|
+
if (!ai)
|
|
63
|
+
continue;
|
|
64
|
+
for (let j = 0; j < BLADE_COUNT; j++) {
|
|
65
|
+
const bj = b[j];
|
|
66
|
+
if (!bj || (i & j) !== 0)
|
|
67
|
+
continue;
|
|
68
|
+
const blade = i ^ j;
|
|
69
|
+
out[blade] += reorderingSign(i, j) * ai * bj;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return out;
|
|
73
|
+
}
|
|
74
|
+
// Basis-vector and basis-bivector blade indices.
|
|
75
|
+
export const E1 = 1 << 0;
|
|
76
|
+
export const E2 = 1 << 1;
|
|
77
|
+
export const E3 = 1 << 2;
|
|
78
|
+
export const E4 = 1 << 3;
|
|
79
|
+
export const E12 = E1 | E2;
|
|
80
|
+
export const E13 = E1 | E3;
|
|
81
|
+
export const E14 = E1 | E4;
|
|
82
|
+
export const E23 = E2 | E3;
|
|
83
|
+
export const E24 = E2 | E4;
|
|
84
|
+
export const E34 = E3 | E4;
|
|
85
|
+
export const PSEUDOSCALAR4 = E1 | E2 | E3 | E4;
|
|
86
|
+
//# sourceMappingURL=Clifford4Internal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Clifford4Internal.js","sourceRoot":"","sources":["../src/Clifford4Internal.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,MAAM,CAAC,MAAM,WAAW,GAAG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AE,GAAG,EAAE,CAAC;AAC3B,MAAM,CAAC,MAAM,GAAG,GAAG,EAAE,GAAG,EAAE,CAAC;AAC3B,MAAM,CAAC,MAAM,GAAG,GAAG,EAAE,GAAG,EAAE,CAAC;AAC3B,MAAM,CAAC,MAAM,GAAG,GAAG,EAAE,GAAG,EAAE,CAAC;AAC3B,MAAM,CAAC,MAAM,GAAG,GAAG,EAAE,GAAG,EAAE,CAAC;AAC3B,MAAM,CAAC,MAAM,GAAG,GAAG,EAAE,GAAG,EAAE,CAAC;AAC3B,MAAM,CAAC,MAAM,aAAa,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Combinatorics — exact counting mathematics: factorials, binomial/multinomial
|
|
3
|
+
* coefficients, Catalan/Stirling/Bell numbers, integer partitions, and
|
|
4
|
+
* derangements. The natural counting-math sibling to {@link Permutation},
|
|
5
|
+
* {@link Cycle}, and {@link GroupTheory} — none of those previously had a way
|
|
6
|
+
* to just compute `nCr`.
|
|
7
|
+
*
|
|
8
|
+
* Everything here is `bigint`-based (matching {@link NumberTheory}'s
|
|
9
|
+
* convention) since factorial-scale values exceed `Number.MAX_SAFE_INTEGER`
|
|
10
|
+
* well before n reaches 20.
|
|
11
|
+
*/
|
|
12
|
+
import { combinations, combinationsWithReplacement, permutations, product } from "./CombinatoricsGenerators.ts";
|
|
13
|
+
export declare class Combinatorics {
|
|
14
|
+
/** `n!` */
|
|
15
|
+
static factorial(n: bigint | number): bigint;
|
|
16
|
+
/** `nCk` — the number of ways to choose `k` items from `n` (order doesn't matter). */
|
|
17
|
+
static binomial(n: bigint | number, k: bigint | number): bigint;
|
|
18
|
+
/** `nPk` — the number of ordered arrangements of `k` items chosen from `n`. */
|
|
19
|
+
static permutationsCount(n: bigint | number, k: bigint | number): bigint;
|
|
20
|
+
/** `n! / (k1! · k2! · … · km!)`, the number of ways to partition `n` labeled items into groups of the given sizes. */
|
|
21
|
+
static multinomial(n: bigint | number, groupSizes: Array<bigint | number>): bigint;
|
|
22
|
+
/** The `n`th Catalan number: `C(2n, n) / (n + 1)` — counts balanced parenthesizations, binary trees, etc. */
|
|
23
|
+
static catalan(n: bigint | number): bigint;
|
|
24
|
+
/** Stirling numbers of the second kind `S(n, k)` — ways to partition an `n`-set into `k` nonempty unlabeled subsets. */
|
|
25
|
+
static stirlingSecond(n: bigint | number, k: bigint | number): bigint;
|
|
26
|
+
/** Unsigned Stirling numbers of the first kind `c(n, k)` — permutations of `n` elements with exactly `k` cycles. */
|
|
27
|
+
static stirlingFirstUnsigned(n: bigint | number, k: bigint | number): bigint;
|
|
28
|
+
/**
|
|
29
|
+
* Full row of Stirling numbers of the second kind: `S(n, j)` for every `j` from `0` to `n`,
|
|
30
|
+
* built in a single `O(n²)` DP pass (same recurrence as {@link stirlingSecond}, but computed
|
|
31
|
+
* once for all columns instead of re-running the whole table per column).
|
|
32
|
+
*/
|
|
33
|
+
private static stirlingSecondRow;
|
|
34
|
+
/** The `n`th Bell number — the total number of partitions of an `n`-set (`Σₖ S(n, k)`). */
|
|
35
|
+
static bell(n: bigint | number): bigint;
|
|
36
|
+
/** The number of integer partitions of `n` (ways to write `n` as a sum of positive integers, order irrelevant). */
|
|
37
|
+
static partitionCount(n: bigint | number): bigint;
|
|
38
|
+
/** The number of derangements of `n` items (permutations with no fixed point), aka the subfactorial `!n`. */
|
|
39
|
+
static derangements(n: bigint | number): bigint;
|
|
40
|
+
/**
|
|
41
|
+
* Cartesian product of the given iterables, last varying fastest.
|
|
42
|
+
* Counting counterpart: multiply the pool sizes.
|
|
43
|
+
*/
|
|
44
|
+
static product: typeof product;
|
|
45
|
+
/**
|
|
46
|
+
* `r`-length permutations of `iterable` (default `r` = pool size).
|
|
47
|
+
* Counting counterpart: {@link Combinatorics.permutationsCount}.
|
|
48
|
+
*/
|
|
49
|
+
static permutations: typeof permutations;
|
|
50
|
+
/**
|
|
51
|
+
* `r`-length combinations of `iterable`, in input order.
|
|
52
|
+
* Counting counterpart: {@link Combinatorics.binomial}.
|
|
53
|
+
*/
|
|
54
|
+
static combinations: typeof combinations;
|
|
55
|
+
/**
|
|
56
|
+
* `r`-length combinations of `iterable` allowing repeats.
|
|
57
|
+
* Counting counterpart: `binomial(n + r - 1, r)`.
|
|
58
|
+
*/
|
|
59
|
+
static combinationsWithReplacement: typeof combinationsWithReplacement;
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=Combinatorics.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Combinatorics.d.ts","sourceRoot":"","sources":["../src/Combinatorics.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,YAAY,EAAE,2BAA2B,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,8BAA8B,CAAC;AAIhH,qBAAa,aAAa;IACxB,WAAW;IACX,MAAM,CAAC,SAAS,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM;IAQ5C,sFAAsF;IACtF,MAAM,CAAC,QAAQ,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM;IAU/D,+EAA+E;IAC/E,MAAM,CAAC,iBAAiB,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM;IASxE,sHAAsH;IACtH,MAAM,CAAC,WAAW,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,UAAU,EAAE,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,GAAG,MAAM;IAUlF,6GAA6G;IAC7G,MAAM,CAAC,OAAO,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM;IAM1C,wHAAwH;IACxH,MAAM,CAAC,cAAc,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM;IAiBrE,oHAAoH;IACpH,MAAM,CAAC,qBAAqB,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM;IAgB5E;;;;OAIG;IACH,OAAO,CAAC,MAAM,CAAC,iBAAiB;IAYhC,2FAA2F;IAC3F,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM;IASvC,mHAAmH;IACnH,MAAM,CAAC,cAAc,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM;IAWjD,6GAA6G;IAC7G,MAAM,CAAC,YAAY,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM;IAmB/C;;;OAGG;IACH,MAAM,CAAC,OAAO,iBAAW;IAEzB;;;OAGG;IACH,MAAM,CAAC,YAAY,sBAAgB;IAEnC;;;OAGG;IACH,MAAM,CAAC,YAAY,sBAAgB;IAEnC;;;OAGG;IACH,MAAM,CAAC,2BAA2B,qCAA+B;CAClE"}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Combinatorics — exact counting mathematics: factorials, binomial/multinomial
|
|
3
|
+
* coefficients, Catalan/Stirling/Bell numbers, integer partitions, and
|
|
4
|
+
* derangements. The natural counting-math sibling to {@link Permutation},
|
|
5
|
+
* {@link Cycle}, and {@link GroupTheory} — none of those previously had a way
|
|
6
|
+
* to just compute `nCr`.
|
|
7
|
+
*
|
|
8
|
+
* Everything here is `bigint`-based (matching {@link NumberTheory}'s
|
|
9
|
+
* convention) since factorial-scale values exceed `Number.MAX_SAFE_INTEGER`
|
|
10
|
+
* well before n reaches 20.
|
|
11
|
+
*/
|
|
12
|
+
import { combinations, combinationsWithReplacement, permutations, product } from "./CombinatoricsGenerators.js";
|
|
13
|
+
const big = (x) => (typeof x === "bigint" ? x : BigInt(x));
|
|
14
|
+
export class Combinatorics {
|
|
15
|
+
/** `n!` */
|
|
16
|
+
static factorial(n) {
|
|
17
|
+
const num = big(n);
|
|
18
|
+
if (num < 0n)
|
|
19
|
+
throw new RangeError("factorial is undefined for negative numbers");
|
|
20
|
+
let result = 1n;
|
|
21
|
+
for (let i = 2n; i <= num; i++)
|
|
22
|
+
result *= i;
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
/** `nCk` — the number of ways to choose `k` items from `n` (order doesn't matter). */
|
|
26
|
+
static binomial(n, k) {
|
|
27
|
+
const N = big(n);
|
|
28
|
+
const K = big(k);
|
|
29
|
+
if (K < 0n || K > N)
|
|
30
|
+
return 0n;
|
|
31
|
+
const kk = K > N - K ? N - K : K; // symmetry: nCk = nC(n-k)
|
|
32
|
+
let result = 1n;
|
|
33
|
+
for (let i = 0n; i < kk; i++)
|
|
34
|
+
result = (result * (N - i)) / (i + 1n);
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
/** `nPk` — the number of ordered arrangements of `k` items chosen from `n`. */
|
|
38
|
+
static permutationsCount(n, k) {
|
|
39
|
+
const N = big(n);
|
|
40
|
+
const K = big(k);
|
|
41
|
+
if (K < 0n || K > N)
|
|
42
|
+
return 0n;
|
|
43
|
+
let result = 1n;
|
|
44
|
+
for (let i = 0n; i < K; i++)
|
|
45
|
+
result *= N - i;
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
/** `n! / (k1! · k2! · … · km!)`, the number of ways to partition `n` labeled items into groups of the given sizes. */
|
|
49
|
+
static multinomial(n, groupSizes) {
|
|
50
|
+
const N = big(n);
|
|
51
|
+
const sizes = groupSizes.map(big);
|
|
52
|
+
const sum = sizes.reduce((s, k) => s + k, 0n);
|
|
53
|
+
if (sum !== N)
|
|
54
|
+
throw new RangeError("multinomial: group sizes must sum to n");
|
|
55
|
+
let result = Combinatorics.factorial(N);
|
|
56
|
+
for (const k of sizes)
|
|
57
|
+
result /= Combinatorics.factorial(k);
|
|
58
|
+
return result;
|
|
59
|
+
}
|
|
60
|
+
/** The `n`th Catalan number: `C(2n, n) / (n + 1)` — counts balanced parenthesizations, binary trees, etc. */
|
|
61
|
+
static catalan(n) {
|
|
62
|
+
const N = big(n);
|
|
63
|
+
if (N < 0n)
|
|
64
|
+
throw new RangeError("catalan is undefined for negative numbers");
|
|
65
|
+
return Combinatorics.binomial(2n * N, N) / (N + 1n);
|
|
66
|
+
}
|
|
67
|
+
/** Stirling numbers of the second kind `S(n, k)` — ways to partition an `n`-set into `k` nonempty unlabeled subsets. */
|
|
68
|
+
static stirlingSecond(n, k) {
|
|
69
|
+
const N = Number(big(n));
|
|
70
|
+
const K = Number(big(k));
|
|
71
|
+
if (N < 0 || K < 0)
|
|
72
|
+
throw new RangeError("stirlingSecond is undefined for negative arguments");
|
|
73
|
+
if (K > N)
|
|
74
|
+
return 0n;
|
|
75
|
+
// dp[j] holds S(i, j) for the current row i, built bottom-up.
|
|
76
|
+
const dp = new Array(N + 1).fill(0n);
|
|
77
|
+
dp[0] = 1n; // S(0, 0) = 1
|
|
78
|
+
for (let i = 1; i <= N; i++) {
|
|
79
|
+
for (let j = Math.min(i, K); j >= 1; j--) {
|
|
80
|
+
dp[j] = BigInt(j) * dp[j] + dp[j - 1];
|
|
81
|
+
}
|
|
82
|
+
dp[0] = 0n; // S(i, 0) = 0 for i > 0
|
|
83
|
+
}
|
|
84
|
+
return dp[K];
|
|
85
|
+
}
|
|
86
|
+
/** Unsigned Stirling numbers of the first kind `c(n, k)` — permutations of `n` elements with exactly `k` cycles. */
|
|
87
|
+
static stirlingFirstUnsigned(n, k) {
|
|
88
|
+
const N = Number(big(n));
|
|
89
|
+
const K = Number(big(k));
|
|
90
|
+
if (N < 0 || K < 0)
|
|
91
|
+
throw new RangeError("stirlingFirstUnsigned is undefined for negative arguments");
|
|
92
|
+
if (K > N)
|
|
93
|
+
return 0n;
|
|
94
|
+
const dp = new Array(N + 1).fill(0n);
|
|
95
|
+
dp[0] = 1n; // c(0, 0) = 1
|
|
96
|
+
for (let i = 1; i <= N; i++) {
|
|
97
|
+
for (let j = Math.min(i, K); j >= 1; j--) {
|
|
98
|
+
dp[j] = BigInt(i - 1) * dp[j] + dp[j - 1];
|
|
99
|
+
}
|
|
100
|
+
dp[0] = 0n; // c(i, 0) = 0 for i > 0
|
|
101
|
+
}
|
|
102
|
+
return dp[K];
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Full row of Stirling numbers of the second kind: `S(n, j)` for every `j` from `0` to `n`,
|
|
106
|
+
* built in a single `O(n²)` DP pass (same recurrence as {@link stirlingSecond}, but computed
|
|
107
|
+
* once for all columns instead of re-running the whole table per column).
|
|
108
|
+
*/
|
|
109
|
+
static stirlingSecondRow(n) {
|
|
110
|
+
const dp = new Array(n + 1).fill(0n);
|
|
111
|
+
dp[0] = 1n; // S(0, 0) = 1
|
|
112
|
+
for (let i = 1; i <= n; i++) {
|
|
113
|
+
for (let j = i; j >= 1; j--) {
|
|
114
|
+
dp[j] = BigInt(j) * dp[j] + dp[j - 1];
|
|
115
|
+
}
|
|
116
|
+
dp[0] = 0n; // S(i, 0) = 0 for i > 0
|
|
117
|
+
}
|
|
118
|
+
return dp;
|
|
119
|
+
}
|
|
120
|
+
/** The `n`th Bell number — the total number of partitions of an `n`-set (`Σₖ S(n, k)`). */
|
|
121
|
+
static bell(n) {
|
|
122
|
+
const N = Number(big(n));
|
|
123
|
+
if (N < 0)
|
|
124
|
+
throw new RangeError("bell is undefined for negative numbers");
|
|
125
|
+
const row = Combinatorics.stirlingSecondRow(N);
|
|
126
|
+
let total = 0n;
|
|
127
|
+
for (let k = 0; k <= N; k++)
|
|
128
|
+
total += row[k];
|
|
129
|
+
return total;
|
|
130
|
+
}
|
|
131
|
+
/** The number of integer partitions of `n` (ways to write `n` as a sum of positive integers, order irrelevant). */
|
|
132
|
+
static partitionCount(n) {
|
|
133
|
+
const N = Number(big(n));
|
|
134
|
+
if (N < 0)
|
|
135
|
+
throw new RangeError("partitionCount is undefined for negative numbers");
|
|
136
|
+
const dp = new Array(N + 1).fill(0n);
|
|
137
|
+
dp[0] = 1n;
|
|
138
|
+
for (let part = 1; part <= N; part++) {
|
|
139
|
+
for (let total = part; total <= N; total++)
|
|
140
|
+
dp[total] += dp[total - part];
|
|
141
|
+
}
|
|
142
|
+
return dp[N];
|
|
143
|
+
}
|
|
144
|
+
/** The number of derangements of `n` items (permutations with no fixed point), aka the subfactorial `!n`. */
|
|
145
|
+
static derangements(n) {
|
|
146
|
+
const N = Number(big(n));
|
|
147
|
+
if (N < 0)
|
|
148
|
+
throw new RangeError("derangements is undefined for negative numbers");
|
|
149
|
+
if (N === 0)
|
|
150
|
+
return 1n;
|
|
151
|
+
if (N === 1)
|
|
152
|
+
return 0n;
|
|
153
|
+
let prev2 = 1n; // !0
|
|
154
|
+
let prev1 = 0n; // !1
|
|
155
|
+
for (let i = 2; i <= N; i++) {
|
|
156
|
+
const cur = BigInt(i - 1) * (prev1 + prev2);
|
|
157
|
+
prev2 = prev1;
|
|
158
|
+
prev1 = cur;
|
|
159
|
+
}
|
|
160
|
+
return prev1;
|
|
161
|
+
}
|
|
162
|
+
// ---- enumeration ---------------------------------------------------------
|
|
163
|
+
// The counting functions above answer "how many?"; these yield the objects
|
|
164
|
+
// themselves. See CombinatoricsGenerators.ts for the algorithms.
|
|
165
|
+
/**
|
|
166
|
+
* Cartesian product of the given iterables, last varying fastest.
|
|
167
|
+
* Counting counterpart: multiply the pool sizes.
|
|
168
|
+
*/
|
|
169
|
+
static product = product;
|
|
170
|
+
/**
|
|
171
|
+
* `r`-length permutations of `iterable` (default `r` = pool size).
|
|
172
|
+
* Counting counterpart: {@link Combinatorics.permutationsCount}.
|
|
173
|
+
*/
|
|
174
|
+
static permutations = permutations;
|
|
175
|
+
/**
|
|
176
|
+
* `r`-length combinations of `iterable`, in input order.
|
|
177
|
+
* Counting counterpart: {@link Combinatorics.binomial}.
|
|
178
|
+
*/
|
|
179
|
+
static combinations = combinations;
|
|
180
|
+
/**
|
|
181
|
+
* `r`-length combinations of `iterable` allowing repeats.
|
|
182
|
+
* Counting counterpart: `binomial(n + r - 1, r)`.
|
|
183
|
+
*/
|
|
184
|
+
static combinationsWithReplacement = combinationsWithReplacement;
|
|
185
|
+
}
|
|
186
|
+
//# sourceMappingURL=Combinatorics.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Combinatorics.js","sourceRoot":"","sources":["../src/Combinatorics.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,YAAY,EAAE,2BAA2B,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,8BAA8B,CAAC;AAEhH,MAAM,GAAG,GAAG,CAAC,CAAkB,EAAU,EAAE,CAAC,CAAC,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;AAEpF,MAAM,OAAO,aAAa;IACxB,WAAW;IACX,MAAM,CAAC,SAAS,CAAC,CAAkB;QACjC,MAAM,GAAG,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC;QACnB,IAAI,GAAG,GAAG,EAAE;YAAE,MAAM,IAAI,UAAU,CAAC,6CAA6C,CAAC,CAAC;QAClF,IAAI,MAAM,GAAG,EAAE,CAAC;QAChB,KAAK,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,GAAG,EAAE,CAAC,EAAE;YAAE,MAAM,IAAI,CAAC,CAAC;QAC5C,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,sFAAsF;IACtF,MAAM,CAAC,QAAQ,CAAC,CAAkB,EAAE,CAAkB;QACpD,MAAM,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC;QACjB,MAAM,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC;QACjB,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC;YAAE,OAAO,EAAE,CAAC;QAC/B,MAAM,EAAE,GAAG,CAAC,GAAG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AAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,KAAK,GAAG,KAAK,CAAC,CAAC;YAC5C,KAAK,GAAG,KAAK,CAAC;YACd,KAAK,GAAG,GAAG,CAAC;QACd,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,6EAA6E;IAC7E,2EAA2E;IAC3E,iEAAiE;IAEjE;;;OAGG;IACH,MAAM,CAAC,OAAO,GAAG,OAAO,CAAC;IAEzB;;;OAGG;IACH,MAAM,CAAC,YAAY,GAAG,YAAY,CAAC;IAEnC;;;OAGG;IACH,MAAM,CAAC,YAAY,GAAG,YAAY,CAAC;IAEnC;;;OAGG;IACH,MAAM,CAAC,2BAA2B,GAAG,2BAA2B,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Combinatorial generators: `product`, `permutations`, `combinations`,
|
|
3
|
+
* `combinationsWithReplacement` — ports of the iterative equivalent-code
|
|
4
|
+
* algorithms documented in CPython's itertools docs (chosen over a recursive
|
|
5
|
+
* approach to avoid recursion-depth limits on large inputs).
|
|
6
|
+
*
|
|
7
|
+
* These are the **enumerating** counterpart to {@link Combinatorics}'s
|
|
8
|
+
* **counting** functions: `Combinatorics.permutationsCount(n, r)` tells you how
|
|
9
|
+
* many there are, `Combinatorics.permutations(iterable, r)` yields them. They
|
|
10
|
+
* are surfaced as static methods on {@link Combinatorics} so both faces of each
|
|
11
|
+
* concept sit together.
|
|
12
|
+
*
|
|
13
|
+
* These are inherently "materialize fully, then compute" algorithms — CPython's
|
|
14
|
+
* own docs note that `product()` completely consumes its inputs before running,
|
|
15
|
+
* so a fully lazy formulation isn't available. Pools are therefore buffered in
|
|
16
|
+
* memory; enumerating a large product is bounded by that, not by laziness.
|
|
17
|
+
*
|
|
18
|
+
* Originally written for the library now published as `@johnhenry/iteration`;
|
|
19
|
+
* moved here because enumeration is mathematics rather than stream plumbing.
|
|
20
|
+
* The async duals stayed behind — they only added a "collect the source, then
|
|
21
|
+
* run this same sync generator" wrapper, and @johnhenry/math is deliberately
|
|
22
|
+
* synchronous throughout.
|
|
23
|
+
*/
|
|
24
|
+
/**
|
|
25
|
+
* Cartesian product of the given iterables. Mirrors Python's
|
|
26
|
+
* itertools.product(*iterables) -- the last iterable varies fastest.
|
|
27
|
+
*/
|
|
28
|
+
export declare function product<T>(...iterables: Array<Iterable<T>>): Generator<T[]>;
|
|
29
|
+
/**
|
|
30
|
+
* r-length permutations of `iterable` (default r = pool length). Mirrors
|
|
31
|
+
* Python's itertools.permutations(iterable, r).
|
|
32
|
+
*/
|
|
33
|
+
export declare function permutations<T>(iterable: Iterable<T>, r?: number): Generator<T[]>;
|
|
34
|
+
/**
|
|
35
|
+
* r-length combinations of `iterable`, in sorted (input) order, without
|
|
36
|
+
* replacement. Mirrors Python's itertools.combinations(iterable, r).
|
|
37
|
+
*/
|
|
38
|
+
export declare function combinations<T>(iterable: Iterable<T>, r: number): Generator<T[]>;
|
|
39
|
+
/**
|
|
40
|
+
* r-length combinations of `iterable`, with replacement. Mirrors Python's
|
|
41
|
+
* itertools.combinations_with_replacement(iterable, r).
|
|
42
|
+
*/
|
|
43
|
+
export declare function combinationsWithReplacement<T>(iterable: Iterable<T>, r: number): Generator<T[]>;
|
|
44
|
+
//# sourceMappingURL=CombinatoricsGenerators.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CombinatoricsGenerators.d.ts","sourceRoot":"","sources":["../src/CombinatoricsGenerators.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAkBH;;;GAGG;AACH,wBAAiB,OAAO,CAAC,CAAC,EAAE,GAAG,SAAS,EAAE,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,EAAE,CAAC,CAE5E;AA+BD;;;GAGG;AACH,wBAAiB,YAAY,CAAC,CAAC,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,EAAE,CAAC,CAElF;AAwBD;;;GAGG;AACH,wBAAiB,YAAY,CAAC,CAAC,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,EAAE,CAAC,CAEjF;AAwBD;;;GAGG;AACH,wBAAiB,2BAA2B,CAAC,CAAC,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,EAAE,CAAC,CAEhG"}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Combinatorial generators: `product`, `permutations`, `combinations`,
|
|
3
|
+
* `combinationsWithReplacement` — ports of the iterative equivalent-code
|
|
4
|
+
* algorithms documented in CPython's itertools docs (chosen over a recursive
|
|
5
|
+
* approach to avoid recursion-depth limits on large inputs).
|
|
6
|
+
*
|
|
7
|
+
* These are the **enumerating** counterpart to {@link Combinatorics}'s
|
|
8
|
+
* **counting** functions: `Combinatorics.permutationsCount(n, r)` tells you how
|
|
9
|
+
* many there are, `Combinatorics.permutations(iterable, r)` yields them. They
|
|
10
|
+
* are surfaced as static methods on {@link Combinatorics} so both faces of each
|
|
11
|
+
* concept sit together.
|
|
12
|
+
*
|
|
13
|
+
* These are inherently "materialize fully, then compute" algorithms — CPython's
|
|
14
|
+
* own docs note that `product()` completely consumes its inputs before running,
|
|
15
|
+
* so a fully lazy formulation isn't available. Pools are therefore buffered in
|
|
16
|
+
* memory; enumerating a large product is bounded by that, not by laziness.
|
|
17
|
+
*
|
|
18
|
+
* Originally written for the library now published as `@johnhenry/iteration`;
|
|
19
|
+
* moved here because enumeration is mathematics rather than stream plumbing.
|
|
20
|
+
* The async duals stayed behind — they only added a "collect the source, then
|
|
21
|
+
* run this same sync generator" wrapper, and @johnhenry/math is deliberately
|
|
22
|
+
* synchronous throughout.
|
|
23
|
+
*/
|
|
24
|
+
function* productFromPools(pools) {
|
|
25
|
+
if (pools.some((pool) => pool.length === 0))
|
|
26
|
+
return;
|
|
27
|
+
const indices = pools.map(() => 0);
|
|
28
|
+
while (true) {
|
|
29
|
+
yield pools.map((pool, i) => pool[indices[i]]);
|
|
30
|
+
let i = indices.length - 1;
|
|
31
|
+
while (i >= 0) {
|
|
32
|
+
indices[i]++;
|
|
33
|
+
if (indices[i] < pools[i].length)
|
|
34
|
+
break;
|
|
35
|
+
indices[i] = 0;
|
|
36
|
+
i--;
|
|
37
|
+
}
|
|
38
|
+
if (i < 0)
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Cartesian product of the given iterables. Mirrors Python's
|
|
44
|
+
* itertools.product(*iterables) -- the last iterable varies fastest.
|
|
45
|
+
*/
|
|
46
|
+
export function* product(...iterables) {
|
|
47
|
+
yield* productFromPools(iterables.map((it) => [...it]));
|
|
48
|
+
}
|
|
49
|
+
function* permutationsFromPool(pool, r) {
|
|
50
|
+
const n = pool.length;
|
|
51
|
+
if (r === undefined || r === null)
|
|
52
|
+
r = n;
|
|
53
|
+
if (r > n || r < 0)
|
|
54
|
+
return;
|
|
55
|
+
const indices = Array.from({ length: n }, (_, i) => i);
|
|
56
|
+
const cycles = Array.from({ length: r }, (_, i) => n - i);
|
|
57
|
+
yield indices.slice(0, r).map((i) => pool[i]);
|
|
58
|
+
if (n === 0)
|
|
59
|
+
return;
|
|
60
|
+
outer: while (true) {
|
|
61
|
+
for (let i = r - 1; i >= 0; i--) {
|
|
62
|
+
cycles[i] -= 1;
|
|
63
|
+
if (cycles[i] === 0) {
|
|
64
|
+
const tail = indices.slice(i + 1).concat(indices.slice(i, i + 1));
|
|
65
|
+
indices.splice(i, indices.length - i, ...tail);
|
|
66
|
+
cycles[i] = n - i;
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
const j = cycles[i];
|
|
70
|
+
const a = indices[i];
|
|
71
|
+
const b = indices[n - j];
|
|
72
|
+
indices[i] = b;
|
|
73
|
+
indices[n - j] = a;
|
|
74
|
+
yield indices.slice(0, r).map((idx) => pool[idx]);
|
|
75
|
+
continue outer;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* r-length permutations of `iterable` (default r = pool length). Mirrors
|
|
83
|
+
* Python's itertools.permutations(iterable, r).
|
|
84
|
+
*/
|
|
85
|
+
export function* permutations(iterable, r) {
|
|
86
|
+
yield* permutationsFromPool([...iterable], r);
|
|
87
|
+
}
|
|
88
|
+
function* combinationsFromPool(pool, r) {
|
|
89
|
+
const n = pool.length;
|
|
90
|
+
if (r > n || r < 0)
|
|
91
|
+
return;
|
|
92
|
+
const indices = Array.from({ length: r }, (_, i) => i);
|
|
93
|
+
yield indices.map((i) => pool[i]);
|
|
94
|
+
while (true) {
|
|
95
|
+
let i = -1;
|
|
96
|
+
for (let k = r - 1; k >= 0; k--) {
|
|
97
|
+
if (indices[k] !== k + n - r) {
|
|
98
|
+
i = k;
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (i === -1)
|
|
103
|
+
return;
|
|
104
|
+
indices[i] += 1;
|
|
105
|
+
for (let j = i + 1; j < r; j++) {
|
|
106
|
+
indices[j] = indices[j - 1] + 1;
|
|
107
|
+
}
|
|
108
|
+
yield indices.map((idx) => pool[idx]);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* r-length combinations of `iterable`, in sorted (input) order, without
|
|
113
|
+
* replacement. Mirrors Python's itertools.combinations(iterable, r).
|
|
114
|
+
*/
|
|
115
|
+
export function* combinations(iterable, r) {
|
|
116
|
+
yield* combinationsFromPool([...iterable], r);
|
|
117
|
+
}
|
|
118
|
+
function* combinationsWithReplacementFromPool(pool, r) {
|
|
119
|
+
const n = pool.length;
|
|
120
|
+
if (n === 0 && r > 0)
|
|
121
|
+
return;
|
|
122
|
+
const indices = new Array(r).fill(0);
|
|
123
|
+
yield indices.map((i) => pool[i]);
|
|
124
|
+
while (true) {
|
|
125
|
+
let i = -1;
|
|
126
|
+
for (let k = r - 1; k >= 0; k--) {
|
|
127
|
+
if (indices[k] !== n - 1) {
|
|
128
|
+
i = k;
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (i === -1)
|
|
133
|
+
return;
|
|
134
|
+
const value = indices[i] + 1;
|
|
135
|
+
for (let j = i; j < r; j++) {
|
|
136
|
+
indices[j] = value;
|
|
137
|
+
}
|
|
138
|
+
yield indices.map((idx) => pool[idx]);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* r-length combinations of `iterable`, with replacement. Mirrors Python's
|
|
143
|
+
* itertools.combinations_with_replacement(iterable, r).
|
|
144
|
+
*/
|
|
145
|
+
export function* combinationsWithReplacement(iterable, r) {
|
|
146
|
+
yield* combinationsWithReplacementFromPool([...iterable], r);
|
|
147
|
+
}
|
|
148
|
+
//# sourceMappingURL=CombinatoricsGenerators.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CombinatoricsGenerators.js","sourceRoot":"","sources":["../src/CombinatoricsGenerators.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,QAAQ,CAAC,CAAC,gBAAgB,CAAI,KAAY;IACxC,IAAI,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,CAAC;QAAE,OAAO;IACpD,MAAM,OAAO,GAAG,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC;IACnC,OAAO,IAAI,EAAE,CAAC;QACZ,MAAM,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC,IAAI,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CAAC,CAAC,GAAG,KAAK,CAAC;QACrB,CAAC;QACD,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;IACxC,CAAC;AACH,CAAC;AAED;;;GAGG;AACH,MAAM,SAAS,CAAC,CAAC,2BAA2B,CAAI,QAAqB,EAAE,CAAS;IAC9E,KAAK,CAAC,CAAC,mCAAmC,CAAC,CAAC,GAAG,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC;AAC/D,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { ComplexNumber } from "./ComplexNumber.ts";
|
|
2
|
+
import { Vector } from "./Vector.ts";
|
|
3
|
+
/**
|
|
4
|
+
* ComplexMath — the remaining complex-valued operations that don't belong on a
|
|
5
|
+
* single {@link ComplexNumber}: vector algebra over `Vector<ComplexNumber>`
|
|
6
|
+
* and probability distributions. Plain exported functions rather than a
|
|
7
|
+
* `static` method bag (same "namespace class" fix as {@link RealMath}).
|
|
8
|
+
*
|
|
9
|
+
* Everything per-value (arithmetic, exponentials/logs, trig, comparisons) is
|
|
10
|
+
* now a fluent {@link ComplexNumber} instance method. Matrix operations were
|
|
11
|
+
* dropped — `Structure.complexField()` already provides generic linear
|
|
12
|
+
* algebra. Order-dependent statistics (median/percentile/variance/etc.) were
|
|
13
|
+
* dropped with them: they had no test coverage here and don't generalize to
|
|
14
|
+
* complex numbers (there's no total order on the complex plane to found
|
|
15
|
+
* percentiles on) — see {@link Statistics} for the real-only equivalents.
|
|
16
|
+
*
|
|
17
|
+
* Ported from Mallory's ActionScript `ComplexMath`; see `ComplexNumber`'s doc
|
|
18
|
+
* comment for the bug fixes inherited from that port (directed-infinity
|
|
19
|
+
* division, zero-base `power`, etc.). `normalDistribution`'s AS3 bug (the
|
|
20
|
+
* reciprocal was taken of the whole PDF expression, flipping the exponential's
|
|
21
|
+
* sign) is fixed here too.
|
|
22
|
+
*/
|
|
23
|
+
type CNInput = ComplexNumber | number;
|
|
24
|
+
type CVec = Vector<ComplexNumber>;
|
|
25
|
+
type CUnary = (x: ComplexNumber) => ComplexNumber;
|
|
26
|
+
export declare function addVector(alpha: CVec, beta: CVec): CVec;
|
|
27
|
+
export declare function scaleVector(alpha: CVec, scalar: CNInput): CVec;
|
|
28
|
+
export declare function negativeVector(alpha: CVec): CVec;
|
|
29
|
+
export declare function subtractVector(alpha: CVec, beta: CVec): CVec;
|
|
30
|
+
export declare function dotProduct(alpha: CVec, beta: CVec): ComplexNumber;
|
|
31
|
+
/** 3D cross product (bug fix: index 2 for z, presence not truthiness — as in RealMath). */
|
|
32
|
+
export declare function crossProduct(alpha: CVec, beta: CVec): CVec;
|
|
33
|
+
export declare function kroneckerProduct(A: CVec, B: CVec): CVec;
|
|
34
|
+
export declare function pNorm(alpha: CVec, norm?: CNInput): ComplexNumber;
|
|
35
|
+
export declare function distanceVector(alpha: CVec, beta: CVec, norm?: CNInput): ComplexNumber;
|
|
36
|
+
export declare function magnitudeVector(alpha: CVec): ComplexNumber;
|
|
37
|
+
export declare function angleBetween(alpha: CVec, beta: CVec): ComplexNumber;
|
|
38
|
+
export declare function zScore(x: CNInput, mean?: CNInput, sDev?: CNInput): ComplexNumber;
|
|
39
|
+
export declare function invertZScore(z: CNInput, mean?: CNInput, sDev?: CNInput): ComplexNumber;
|
|
40
|
+
/** Normal PDF closure over complex arguments. */
|
|
41
|
+
export declare function normalDistribution(mean: CNInput, sDev: CNInput): CUnary;
|
|
42
|
+
export declare function standardNormalDistribution(x: CNInput): ComplexNumber;
|
|
43
|
+
export declare function normalProbability(x: CNInput, mean: CNInput, sDev: CNInput, negativeInfinityApproximation?: number, integrationInterval?: number): ComplexNumber;
|
|
44
|
+
export declare function standardNormalProbability(z: CNInput, negativeInfinityApproximation?: number, interval?: number): ComplexNumber;
|
|
45
|
+
export {};
|
|
46
|
+
//# sourceMappingURL=ComplexMath.d.ts.map
|