@johnhenry/math 0.1.0 → 0.3.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 +5 -4
- package/dist/Distributions.d.ts +86 -16
- package/dist/Distributions.d.ts.map +1 -1
- package/dist/Distributions.js +340 -89
- package/dist/Distributions.js.map +1 -1
- package/dist/EigenGeneral.d.ts +22 -0
- package/dist/EigenGeneral.d.ts.map +1 -0
- package/dist/EigenGeneral.js +327 -0
- package/dist/EigenGeneral.js.map +1 -0
- package/dist/Frobenius.d.ts +60 -0
- package/dist/Frobenius.d.ts.map +1 -0
- package/dist/Frobenius.js +162 -0
- package/dist/Frobenius.js.map +1 -0
- package/dist/Graph.d.ts +26 -0
- package/dist/Graph.d.ts.map +1 -1
- package/dist/Graph.js +75 -0
- package/dist/Graph.js.map +1 -1
- package/dist/MatrixMath.d.ts +10 -0
- package/dist/MatrixMath.d.ts.map +1 -1
- package/dist/MatrixMath.js +11 -0
- package/dist/MatrixMath.js.map +1 -1
- package/dist/MonteCarlo.d.ts +106 -0
- package/dist/MonteCarlo.d.ts.map +1 -0
- package/dist/MonteCarlo.js +183 -0
- package/dist/MonteCarlo.js.map +1 -0
- package/dist/Random.d.ts +31 -0
- package/dist/Random.d.ts.map +1 -0
- package/dist/Random.js +95 -0
- package/dist/Random.js.map +1 -0
- package/dist/Regression.d.ts +114 -0
- package/dist/Regression.d.ts.map +1 -0
- package/dist/Regression.js +338 -0
- package/dist/Regression.js.map +1 -0
- package/dist/Shader.d.ts +41 -0
- package/dist/Shader.d.ts.map +1 -0
- package/dist/Shader.js +758 -0
- package/dist/Shader.js.map +1 -0
- package/dist/SpecialFunctions.d.ts +3 -3
- package/dist/SpecialFunctions.d.ts.map +1 -1
- package/dist/SpecialFunctions.js +61 -51
- package/dist/SpecialFunctions.js.map +1 -1
- package/dist/Symbolic.d.ts +12 -0
- package/dist/Symbolic.d.ts.map +1 -1
- package/dist/Symbolic.js +16 -0
- package/dist/Symbolic.js.map +1 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/Distributions.ts +391 -94
- package/src/EigenGeneral.ts +342 -0
- package/src/Frobenius.ts +203 -0
- package/src/Graph.ts +68 -0
- package/src/MatrixMath.ts +13 -0
- package/src/MonteCarlo.ts +257 -0
- package/src/Random.ts +104 -0
- package/src/Regression.ts +423 -0
- package/src/Shader.ts +837 -0
- package/src/SpecialFunctions.ts +52 -48
- package/src/Symbolic.ts +18 -0
- package/src/index.ts +34 -0
package/src/SpecialFunctions.ts
CHANGED
|
@@ -38,22 +38,18 @@ export class SpecialFunctions {
|
|
|
38
38
|
return Math.exp(SpecialFunctions.lnGamma(a) + SpecialFunctions.lnGamma(b) - SpecialFunctions.lnGamma(a + b));
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
/** The error function erf(x). */
|
|
41
|
+
/** The error function erf(x) (to ~1e-15, via the regularised incomplete gamma P(1/2, x²)). */
|
|
42
42
|
static erf(x: number): number {
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
const y =
|
|
47
|
-
1 -
|
|
48
|
-
((((1.061405429 * t - 1.453152027) * t + 1.421413741) * t - 0.284496736) * t + 0.254829592) *
|
|
49
|
-
t *
|
|
50
|
-
Math.exp(-ax * ax);
|
|
51
|
-
return sign * y;
|
|
43
|
+
if (x === 0 || Number.isNaN(x)) return x;
|
|
44
|
+
const v = SpecialFunctions.regularizedGammaP(0.5, x * x);
|
|
45
|
+
return x < 0 ? -v : v;
|
|
52
46
|
}
|
|
53
47
|
|
|
54
|
-
/** The complementary error function erfc(x) = 1 − erf(x). */
|
|
48
|
+
/** The complementary error function erfc(x) = 1 − erf(x), accurate in the upper tail (no cancellation). */
|
|
55
49
|
static erfc(x: number): number {
|
|
56
|
-
|
|
50
|
+
if (Number.isNaN(x)) return x;
|
|
51
|
+
if (x < 0) return 2 - SpecialFunctions.regularizedGammaQ(0.5, x * x);
|
|
52
|
+
return SpecialFunctions.regularizedGammaQ(0.5, x * x);
|
|
57
53
|
}
|
|
58
54
|
|
|
59
55
|
/**
|
|
@@ -63,44 +59,16 @@ export class SpecialFunctions {
|
|
|
63
59
|
static regularizedGammaP(a: number, x: number): number {
|
|
64
60
|
if (x < 0 || a <= 0) return Number.NaN;
|
|
65
61
|
if (x === 0) return 0;
|
|
66
|
-
if (x
|
|
67
|
-
|
|
68
|
-
let ap = a;
|
|
69
|
-
let sum = 1 / a;
|
|
70
|
-
let del = sum;
|
|
71
|
-
for (let n = 0; n < 200; n++) {
|
|
72
|
-
ap++;
|
|
73
|
-
del *= x / ap;
|
|
74
|
-
sum += del;
|
|
75
|
-
if (Math.abs(del) < Math.abs(sum) * 1e-15) break;
|
|
76
|
-
}
|
|
77
|
-
return sum * Math.exp(-x + a * Math.log(x) - SpecialFunctions.lnGamma(a));
|
|
78
|
-
}
|
|
79
|
-
// continued fraction for the upper part Q, then P = 1 - Q
|
|
80
|
-
const tiny = 1e-300;
|
|
81
|
-
let b = x + 1 - a;
|
|
82
|
-
let c = 1 / tiny;
|
|
83
|
-
let d = 1 / b;
|
|
84
|
-
let h = d;
|
|
85
|
-
for (let i = 1; i < 200; i++) {
|
|
86
|
-
const an = -i * (i - a);
|
|
87
|
-
b += 2;
|
|
88
|
-
d = an * d + b;
|
|
89
|
-
if (Math.abs(d) < tiny) d = tiny;
|
|
90
|
-
c = b + an / c;
|
|
91
|
-
if (Math.abs(c) < tiny) c = tiny;
|
|
92
|
-
d = 1 / d;
|
|
93
|
-
const del = d * c;
|
|
94
|
-
h *= del;
|
|
95
|
-
if (Math.abs(del - 1) < 1e-15) break;
|
|
96
|
-
}
|
|
97
|
-
const q = Math.exp(-x + a * Math.log(x) - SpecialFunctions.lnGamma(a)) * h;
|
|
98
|
-
return 1 - q;
|
|
62
|
+
if (x === Number.POSITIVE_INFINITY) return 1;
|
|
63
|
+
return x < a + 1 ? gammaSeries(a, x) : 1 - gammaContinuedFraction(a, x);
|
|
99
64
|
}
|
|
100
65
|
|
|
101
|
-
/** The regularised upper incomplete gamma function Q(a, x) = 1 − P(a, x). */
|
|
66
|
+
/** The regularised upper incomplete gamma function Q(a, x) = 1 − P(a, x), computed directly in the upper tail. */
|
|
102
67
|
static regularizedGammaQ(a: number, x: number): number {
|
|
103
|
-
|
|
68
|
+
if (x < 0 || a <= 0) return Number.NaN;
|
|
69
|
+
if (x === 0) return 1;
|
|
70
|
+
if (x === Number.POSITIVE_INFINITY) return 0;
|
|
71
|
+
return x < a + 1 ? 1 - gammaSeries(a, x) : gammaContinuedFraction(a, x);
|
|
104
72
|
}
|
|
105
73
|
|
|
106
74
|
/**
|
|
@@ -129,7 +97,7 @@ function betaContinuedFraction(a: number, b: number, x: number): number {
|
|
|
129
97
|
if (Math.abs(d) < tiny) d = tiny;
|
|
130
98
|
d = 1 / d;
|
|
131
99
|
let h = d;
|
|
132
|
-
for (let m = 1; m <
|
|
100
|
+
for (let m = 1; m < 1000; m++) {
|
|
133
101
|
const m2 = 2 * m;
|
|
134
102
|
let aa = (m * (b - m) * x) / ((qam + m2) * (a + m2));
|
|
135
103
|
d = 1 + aa * d;
|
|
@@ -150,3 +118,39 @@ function betaContinuedFraction(a: number, b: number, x: number): number {
|
|
|
150
118
|
}
|
|
151
119
|
return h;
|
|
152
120
|
}
|
|
121
|
+
|
|
122
|
+
/** Series for P(a, x), valid and fast for x < a + 1. */
|
|
123
|
+
function gammaSeries(a: number, x: number): number {
|
|
124
|
+
let ap = a;
|
|
125
|
+
let sum = 1 / a;
|
|
126
|
+
let del = sum;
|
|
127
|
+
for (let n = 0; n < 1000; n++) {
|
|
128
|
+
ap++;
|
|
129
|
+
del *= x / ap;
|
|
130
|
+
sum += del;
|
|
131
|
+
if (Math.abs(del) < Math.abs(sum) * 1e-16) break;
|
|
132
|
+
}
|
|
133
|
+
return sum * Math.exp(-x + a * Math.log(x) - SpecialFunctions.lnGamma(a));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Lentz continued fraction for Q(a, x), valid and fast for x >= a + 1. */
|
|
137
|
+
function gammaContinuedFraction(a: number, x: number): number {
|
|
138
|
+
const tiny = 1e-300;
|
|
139
|
+
let b = x + 1 - a;
|
|
140
|
+
let c = 1 / tiny;
|
|
141
|
+
let d = 1 / b;
|
|
142
|
+
let h = d;
|
|
143
|
+
for (let i = 1; i < 1000; i++) {
|
|
144
|
+
const an = -i * (i - a);
|
|
145
|
+
b += 2;
|
|
146
|
+
d = an * d + b;
|
|
147
|
+
if (Math.abs(d) < tiny) d = tiny;
|
|
148
|
+
c = b + an / c;
|
|
149
|
+
if (Math.abs(c) < tiny) c = tiny;
|
|
150
|
+
d = 1 / d;
|
|
151
|
+
const del = d * c;
|
|
152
|
+
h *= del;
|
|
153
|
+
if (Math.abs(del - 1) < 1e-16) break;
|
|
154
|
+
}
|
|
155
|
+
return Math.exp(-x + a * Math.log(x) - SpecialFunctions.lnGamma(a)) * h;
|
|
156
|
+
}
|
package/src/Symbolic.ts
CHANGED
|
@@ -8,6 +8,7 @@ import { FUNC_DERIVATIVE_RULES } from "./differentiation-rules.ts";
|
|
|
8
8
|
import { MatrixMath } from "./MatrixMath.ts";
|
|
9
9
|
import { Numerical } from "./Numerical.ts";
|
|
10
10
|
import { Rational } from "./Rational.ts";
|
|
11
|
+
import { compileShader, type ShaderOptions, type ShaderResult } from "./Shader.ts";
|
|
11
12
|
import { SpecialFunctions } from "./SpecialFunctions.ts";
|
|
12
13
|
import type { Structure } from "./Structure.ts";
|
|
13
14
|
|
|
@@ -864,6 +865,23 @@ export class Symbolic {
|
|
|
864
865
|
return render(typeof expr === "string" ? Symbolic.parse(expr) : expr, 0);
|
|
865
866
|
}
|
|
866
867
|
|
|
868
|
+
/**
|
|
869
|
+
* Compile `expr` to a GLSL ES 3.0 expression over `float` (default) or, with
|
|
870
|
+
* `{ complex: true }`, over `vec2` complex numbers (the helpers it needs, such as
|
|
871
|
+
* `mx_cmul`, are returned in `helpers` and emitted once). Output is deterministic.
|
|
872
|
+
* Refuses nodes with no shader form (`sum`, `product`, `erf`, `gcd`, `lcm`, and anything
|
|
873
|
+
* not closed under the complex numbers in complex mode) with a `ShaderUnsupportedError`.
|
|
874
|
+
* Shader `pow` of a negative base with a non-integer exponent is undefined, unlike JS `NaN`.
|
|
875
|
+
*/
|
|
876
|
+
static toGLSL(expr: Expr | string, options: ShaderOptions = {}): ShaderResult {
|
|
877
|
+
return compileShader(typeof expr === "string" ? Symbolic.parse(expr) : expr, "glsl", options);
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
/** As {@link toGLSL}, for WGSL (`f32` / `vec2<f32>`; `select` replaces the ternary). */
|
|
881
|
+
static toWGSL(expr: Expr | string, options: ShaderOptions = {}): ShaderResult {
|
|
882
|
+
return compileShader(typeof expr === "string" ? Symbolic.parse(expr) : expr, "wgsl", options);
|
|
883
|
+
}
|
|
884
|
+
|
|
867
885
|
/** Render `expr` as a LaTeX string, e.g. `"x/2"` -> `"\\frac{x}{2}"`. */
|
|
868
886
|
static toLatex(expr: Expr | string): string {
|
|
869
887
|
return toLatexRec(typeof expr === "string" ? Symbolic.parse(expr) : expr, 0);
|
package/src/index.ts
CHANGED
|
@@ -24,18 +24,31 @@ export { ComplexNumber } from "./ComplexNumber.ts";
|
|
|
24
24
|
export { Cycle } from "./Cycle.ts";
|
|
25
25
|
export { Decimal } from "./Decimal.ts";
|
|
26
26
|
export {
|
|
27
|
+
type Alternative,
|
|
28
|
+
type ChiSquareIndependenceResult,
|
|
27
29
|
type ContinuousDistribution,
|
|
28
30
|
type DiscreteDistribution,
|
|
29
31
|
Distributions,
|
|
30
32
|
HypothesisTests,
|
|
31
33
|
type TestResult,
|
|
34
|
+
type TTestIndependentOptions,
|
|
35
|
+
type TTestOptions,
|
|
32
36
|
} from "./Distributions.ts";
|
|
33
37
|
export { DualNumber } from "./DualNumber.ts";
|
|
34
38
|
export { FUNC_DERIVATIVE_RULES, type FuncDerivativeRule } from "./differentiation-rules.ts";
|
|
39
|
+
export type { GeneralEigenResult } from "./EigenGeneral.ts";
|
|
35
40
|
// Expression evaluation
|
|
36
41
|
export { Environment } from "./Environment.ts";
|
|
37
42
|
export { Expression } from "./Expression.ts";
|
|
38
43
|
export { FFT } from "./FFT.ts";
|
|
44
|
+
export {
|
|
45
|
+
type FrobeniusBlock,
|
|
46
|
+
type FrobeniusResult,
|
|
47
|
+
frobeniusNormalForm,
|
|
48
|
+
type NumericMatrix,
|
|
49
|
+
type RationalCanonicalResult,
|
|
50
|
+
rationalCanonicalForm,
|
|
51
|
+
} from "./Frobenius.ts";
|
|
39
52
|
export {
|
|
40
53
|
type BivectorGeometricProduct,
|
|
41
54
|
commutatorProduct,
|
|
@@ -90,16 +103,37 @@ export {
|
|
|
90
103
|
type QRResult,
|
|
91
104
|
type SVDResult,
|
|
92
105
|
} from "./MatrixMath.ts";
|
|
106
|
+
export {
|
|
107
|
+
type BootstrapResult,
|
|
108
|
+
type ConvergencePoint,
|
|
109
|
+
type DartPiResult,
|
|
110
|
+
type DartPoint,
|
|
111
|
+
type HistogramBin,
|
|
112
|
+
type IntegrationConvergencePoint,
|
|
113
|
+
type IntegrationResult,
|
|
114
|
+
MonteCarlo,
|
|
115
|
+
} from "./MonteCarlo.ts";
|
|
93
116
|
export { NumberTheory } from "./NumberTheory.ts";
|
|
94
117
|
export { type LevenbergMarquardtResult, Numerical, type ODEStep } from "./Numerical.ts";
|
|
95
118
|
export { Permutation } from "./Permutation.ts";
|
|
96
119
|
export { Polygon } from "./Polygon.ts";
|
|
97
120
|
export { PolynomialRing, parsePolynomial, polynomialToString } from "./PolynomialRing.ts";
|
|
98
121
|
export { Quaternion } from "./Quaternion.ts";
|
|
122
|
+
export { Rng, seed } from "./Random.ts";
|
|
99
123
|
export { Rational } from "./Rational.ts";
|
|
100
124
|
// Numeric cores
|
|
101
125
|
export * as RealMath from "./RealMath.ts";
|
|
126
|
+
export {
|
|
127
|
+
type CurveFitOptions,
|
|
128
|
+
type CurveFitResult,
|
|
129
|
+
type HuberLinearResult,
|
|
130
|
+
type LeastSquaresOptions,
|
|
131
|
+
type LeastSquaresResult,
|
|
132
|
+
Regression,
|
|
133
|
+
type RobustLoss,
|
|
134
|
+
} from "./Regression.ts";
|
|
102
135
|
export { Rotor4 } from "./Rotor4.ts";
|
|
136
|
+
export { type ShaderDialect, type ShaderOptions, type ShaderResult, ShaderUnsupportedError } from "./Shader.ts";
|
|
103
137
|
export { SpecialFunctions } from "./SpecialFunctions.ts";
|
|
104
138
|
export { SpecialOperator } from "./SpecialOperator.ts";
|
|
105
139
|
export * as Statistics from "./Statistics.ts";
|