nerdamer 1.1.13 → 2.0.0-rc.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.md +184 -0
- package/README.md +293 -312
- package/dist/bundle.js +2 -0
- package/dist/bundle.js.LICENSE.txt +7 -0
- package/dist/parser.js +2 -0
- package/dist/parser.js.LICENSE.txt +7 -0
- package/docs-data/parser-functions.json +1960 -0
- package/index.d.ts +10 -426
- package/output/algebra/adapters.d.ts +33 -0
- package/output/algebra/adapters.js +109 -0
- package/output/algebra/algorithms/arith.d.ts +84 -0
- package/output/algebra/algorithms/arith.js +323 -0
- package/output/algebra/algorithms/factor.d.ts +3 -0
- package/output/algebra/algorithms/factor.js +5 -0
- package/output/algebra/algorithms/factorMultivariate.d.ts +23 -0
- package/output/algebra/algorithms/factorMultivariate.js +2393 -0
- package/output/algebra/algorithms/factorUnivariate.d.ts +72 -0
- package/output/algebra/algorithms/factorUnivariate.js +1072 -0
- package/output/algebra/algorithms/gcd.d.ts +22 -0
- package/output/algebra/algorithms/gcd.js +690 -0
- package/output/algebra/algorithms/groebnerBase.d.ts +157 -0
- package/output/algebra/algorithms/groebnerBase.js +1166 -0
- package/output/algebra/algorithms/multiPoly/MultiPoly.d.ts +137 -0
- package/output/algebra/algorithms/multiPoly/MultiPoly.js +346 -0
- package/output/algebra/algorithms/poly.d.ts +228 -0
- package/output/algebra/algorithms/poly.js +1299 -0
- package/output/algebra/algorithms/rational.d.ts +17 -0
- package/output/algebra/algorithms/rational.js +115 -0
- package/output/algebra/factor/factor.d.ts +62 -0
- package/output/algebra/factor/factor.js +158 -0
- package/output/algebra/gcd/gcd.d.ts +29 -0
- package/output/algebra/gcd/gcd.js +141 -0
- package/output/algebra/groebner.d.ts +21 -0
- package/output/algebra/groebner.js +42 -0
- package/output/algebra/partfrac.d.ts +19 -0
- package/output/algebra/partfrac.js +398 -0
- package/output/algebra/polynomialize.d.ts +20 -0
- package/output/algebra/polynomialize.js +24 -0
- package/output/algebra/simplify/complexsimp.d.ts +7 -0
- package/output/algebra/simplify/complexsimp.js +24 -0
- package/output/algebra/simplify/factorCommon.d.ts +13 -0
- package/output/algebra/simplify/factorCommon.js +136 -0
- package/output/algebra/simplify/funcsimp.d.ts +26 -0
- package/output/algebra/simplify/funcsimp.js +633 -0
- package/output/algebra/simplify/invtrigrewrite.d.ts +10 -0
- package/output/algebra/simplify/invtrigrewrite.js +96 -0
- package/output/algebra/simplify/ratsimp.d.ts +21 -0
- package/output/algebra/simplify/ratsimp.js +140 -0
- package/output/algebra/simplify/simplify.d.ts +25 -0
- package/output/algebra/simplify/simplify.js +228 -0
- package/output/algebra/simplify/trigreduce.d.ts +38 -0
- package/output/algebra/simplify/trigreduce.js +424 -0
- package/output/algebra/simplify/trigrewrite.d.ts +17 -0
- package/output/algebra/simplify/trigrewrite.js +156 -0
- package/output/algebra/simplify/trigsimp.d.ts +11 -0
- package/output/algebra/simplify/trigsimp.js +369 -0
- package/output/algebra/simplify/utils.d.ts +32 -0
- package/output/algebra/simplify/utils.js +56 -0
- package/output/algebra/utils.d.ts +32 -0
- package/output/algebra/utils.js +48 -0
- package/output/api/advanced.d.ts +7 -0
- package/output/api/advanced.js +18 -0
- package/output/api/algebra.d.ts +17 -0
- package/output/api/algebra.js +32 -0
- package/output/api/assumptions.d.ts +4 -0
- package/output/api/assumptions.js +11 -0
- package/output/api/calculus.d.ts +9 -0
- package/output/api/calculus.js +21 -0
- package/output/api/core.d.ts +17 -0
- package/output/api/core.js +41 -0
- package/output/api/debug.d.ts +16 -0
- package/output/api/debug.js +23 -0
- package/output/api/parser.d.ts +85 -0
- package/output/api/parser.js +16 -0
- package/output/api/solve.d.ts +10 -0
- package/output/api/solve.js +16 -0
- package/output/api/structures.d.ts +10 -0
- package/output/api/structures.js +22 -0
- package/output/calculus/derivative/diff.d.ts +30 -0
- package/output/calculus/derivative/diff.js +238 -0
- package/output/calculus/fresnel.d.ts +16 -0
- package/output/calculus/fresnel.js +39 -0
- package/output/calculus/integrate/byParts.d.ts +18 -0
- package/output/calculus/integrate/byParts.js +339 -0
- package/output/calculus/integrate/bySubstitution.d.ts +135 -0
- package/output/calculus/integrate/bySubstitution.js +411 -0
- package/output/calculus/integrate/integrate.d.ts +38 -0
- package/output/calculus/integrate/integrate.js +652 -0
- package/output/calculus/integrate/integrationTable.d.ts +2 -0
- package/output/calculus/integrate/integrationTable.js +914 -0
- package/output/calculus/integrate/utils.d.ts +28 -0
- package/output/calculus/integrate/utils.js +96 -0
- package/output/calculus/laplace/ilaplace.d.ts +31 -0
- package/output/calculus/laplace/ilaplace.js +192 -0
- package/output/calculus/laplace/ilaplaceTable.d.ts +2 -0
- package/output/calculus/laplace/ilaplaceTable.js +200 -0
- package/output/calculus/laplace/laplace.d.ts +22 -0
- package/output/calculus/laplace/laplace.js +109 -0
- package/output/calculus/laplace/laplaceTable.d.ts +2 -0
- package/output/calculus/laplace/laplaceTable.js +187 -0
- package/output/calculus/limit/limit.d.ts +35 -0
- package/output/calculus/limit/limit.js +1183 -0
- package/output/calculus/limit/limitsTable.d.ts +2 -0
- package/output/calculus/limit/limitsTable.js +7 -0
- package/output/core/Settings.d.ts +32 -0
- package/output/core/Settings.js +68 -0
- package/output/core/adapters.d.ts +18 -0
- package/output/core/adapters.js +40 -0
- package/output/core/classes/assumption/Assumption.d.ts +218 -0
- package/output/core/classes/assumption/Assumption.js +605 -0
- package/output/core/classes/assumption/assertiveFunctions.d.ts +6 -0
- package/output/core/classes/assumption/assertiveFunctions.js +64 -0
- package/output/core/classes/assumption/assume.d.ts +90 -0
- package/output/core/classes/assumption/assume.js +145 -0
- package/output/core/classes/collection/Collection.d.ts +107 -0
- package/output/core/classes/collection/Collection.js +231 -0
- package/output/core/classes/complex/Complex.d.ts +99 -0
- package/output/core/classes/complex/Complex.js +135 -0
- package/output/core/classes/decimalSet/DecimalSet.d.ts +226 -0
- package/output/core/classes/decimalSet/DecimalSet.js +453 -0
- package/output/core/classes/dictionary/Dictionary.d.ts +102 -0
- package/output/core/classes/dictionary/Dictionary.js +213 -0
- package/output/core/classes/equation/Equation.d.ts +256 -0
- package/output/core/classes/equation/Equation.js +350 -0
- package/output/core/classes/expression/CoeffObject.d.ts +100 -0
- package/output/core/classes/expression/CoeffObject.js +198 -0
- package/output/core/classes/expression/Expression.d.ts +1674 -0
- package/output/core/classes/expression/Expression.js +2332 -0
- package/output/core/classes/expression/analysis.d.ts +25 -0
- package/output/core/classes/expression/analysis.js +218 -0
- package/output/core/classes/expression/collect.d.ts +9 -0
- package/output/core/classes/expression/collect.js +49 -0
- package/output/core/classes/expression/format.d.ts +45 -0
- package/output/core/classes/expression/format.js +217 -0
- package/output/core/classes/expression/products.d.ts +23 -0
- package/output/core/classes/expression/products.js +115 -0
- package/output/core/classes/expression/shortcuts.d.ts +35 -0
- package/output/core/classes/expression/shortcuts.js +129 -0
- package/output/core/classes/expression/traversal.d.ts +31 -0
- package/output/core/classes/expression/traversal.js +216 -0
- package/output/core/classes/expression/trig.d.ts +25 -0
- package/output/core/classes/expression/trig.js +40 -0
- package/output/core/classes/expression/utils.d.ts +52 -0
- package/output/core/classes/expression/utils.js +189 -0
- package/output/core/classes/lookupTable/LookupTable.d.ts +12 -0
- package/output/core/classes/lookupTable/LookupTable.js +55 -0
- package/output/core/classes/matrix/Matrix.d.ts +227 -0
- package/output/core/classes/matrix/Matrix.js +889 -0
- package/output/core/classes/matrix/Sylvester.d.ts +11 -0
- package/output/core/classes/matrix/Sylvester.js +54 -0
- package/output/core/classes/matrix/functions.d.ts +26 -0
- package/output/core/classes/matrix/functions.js +52 -0
- package/output/core/classes/matrix/utils.d.ts +9 -0
- package/output/core/classes/matrix/utils.js +22 -0
- package/output/core/classes/parser/Parser.d.ts +480 -0
- package/output/core/classes/parser/Parser.js +1812 -0
- package/output/core/classes/parser/Token.d.ts +58 -0
- package/output/core/classes/parser/Token.js +137 -0
- package/output/core/classes/parser/constants.d.ts +160 -0
- package/output/core/classes/parser/constants.js +227 -0
- package/output/core/classes/parser/helpers.d.ts +21 -0
- package/output/core/classes/parser/helpers.js +42 -0
- package/output/core/classes/parser/operations/add.d.ts +15 -0
- package/output/core/classes/parser/operations/add.js +238 -0
- package/output/core/classes/parser/operations/comma.d.ts +7 -0
- package/output/core/classes/parser/operations/comma.js +11 -0
- package/output/core/classes/parser/operations/compare.d.ts +21 -0
- package/output/core/classes/parser/operations/compare.js +461 -0
- package/output/core/classes/parser/operations/divide.d.ts +2 -0
- package/output/core/classes/parser/operations/divide.js +72 -0
- package/output/core/classes/parser/operations/functions.d.ts +46 -0
- package/output/core/classes/parser/operations/functions.js +430 -0
- package/output/core/classes/parser/operations/multiply.d.ts +11 -0
- package/output/core/classes/parser/operations/multiply.js +321 -0
- package/output/core/classes/parser/operations/power.d.ts +48 -0
- package/output/core/classes/parser/operations/power.js +673 -0
- package/output/core/classes/parser/operations/subtract.d.ts +3 -0
- package/output/core/classes/parser/operations/subtract.js +35 -0
- package/output/core/classes/parser/preprocess.d.ts +14 -0
- package/output/core/classes/parser/preprocess.js +259 -0
- package/output/core/classes/parser/scripting/controlFlow.d.ts +121 -0
- package/output/core/classes/parser/scripting/controlFlow.js +355 -0
- package/output/core/classes/parser/scripting/evaluate.d.ts +10 -0
- package/output/core/classes/parser/scripting/evaluate.js +112 -0
- package/output/core/classes/parser/scripting/functions.d.ts +14 -0
- package/output/core/classes/parser/scripting/functions.js +59 -0
- package/output/core/classes/parser/scripting/scope.d.ts +35 -0
- package/output/core/classes/parser/scripting/scope.js +155 -0
- package/output/core/classes/parser/types.d.ts +125 -0
- package/output/core/classes/parser/types.js +2 -0
- package/output/core/classes/parser/wrappers/IndexedReference.d.ts +31 -0
- package/output/core/classes/parser/wrappers/IndexedReference.js +68 -0
- package/output/core/classes/parser/wrappers/KeyValuePair.d.ts +24 -0
- package/output/core/classes/parser/wrappers/KeyValuePair.js +51 -0
- package/output/core/classes/polynomial/Polynomial.d.ts +500 -0
- package/output/core/classes/polynomial/Polynomial.js +1149 -0
- package/output/core/classes/polynomial/Term.d.ts +222 -0
- package/output/core/classes/polynomial/Term.js +430 -0
- package/output/core/classes/polynomial/functions.d.ts +69 -0
- package/output/core/classes/polynomial/functions.js +126 -0
- package/output/core/classes/polynomial/utils.d.ts +62 -0
- package/output/core/classes/polynomial/utils.js +206 -0
- package/output/core/classes/rational/Rational.d.ts +504 -0
- package/output/core/classes/rational/Rational.js +779 -0
- package/output/core/classes/seq/SEQ.d.ts +13 -0
- package/output/core/classes/seq/SEQ.js +137 -0
- package/output/core/classes/valuesSet/ValuesSet.d.ts +179 -0
- package/output/core/classes/valuesSet/ValuesSet.js +381 -0
- package/output/core/classes/vector/Vector.d.ts +222 -0
- package/output/core/classes/vector/Vector.js +497 -0
- package/output/core/classes/vector/functions.d.ts +28 -0
- package/output/core/classes/vector/functions.js +47 -0
- package/output/core/common/classes/MathematicalAggregate.d.ts +53 -0
- package/output/core/common/classes/MathematicalAggregate.js +140 -0
- package/output/core/common/classes/Scope.d.ts +62 -0
- package/output/core/common/classes/Scope.js +122 -0
- package/output/core/common/classes/StructuredEntity.d.ts +55 -0
- package/output/core/common/classes/StructuredEntity.js +151 -0
- package/output/core/common/common.d.ts +63 -0
- package/output/core/common/common.js +279 -0
- package/output/core/common/functions/functions.d.ts +14 -0
- package/output/core/common/functions/functions.js +33 -0
- package/output/core/common/functions/structuredEntityUtils.d.ts +27 -0
- package/output/core/common/functions/structuredEntityUtils.js +40 -0
- package/output/core/converters/BaseConverter.d.ts +97 -0
- package/output/core/converters/BaseConverter.js +405 -0
- package/output/core/converters/Converter.d.ts +111 -0
- package/output/core/converters/Converter.js +802 -0
- package/output/core/converters/Pattern.d.ts +71 -0
- package/output/core/converters/Pattern.js +302 -0
- package/output/core/dispatch.d.ts +34 -0
- package/output/core/dispatch.js +304 -0
- package/output/core/errors.d.ts +531 -0
- package/output/core/errors.js +621 -0
- package/output/core/functions/bigint/bigint.d.ts +121 -0
- package/output/core/functions/bigint/bigint.js +392 -0
- package/output/core/functions/bigint/primeFactor.d.ts +49 -0
- package/output/core/functions/bigint/primeFactor.js +267 -0
- package/output/core/functions/bigint/primes.d.ts +1 -0
- package/output/core/functions/bigint/primes.js +11 -0
- package/output/core/functions/build/definitions.d.ts +12 -0
- package/output/core/functions/build/definitions.js +133 -0
- package/output/core/functions/build/index.d.ts +32 -0
- package/output/core/functions/build/index.js +136 -0
- package/output/core/functions/complex.d.ts +90 -0
- package/output/core/functions/complex.js +482 -0
- package/output/core/functions/decimal.d.ts +24 -0
- package/output/core/functions/decimal.js +664 -0
- package/output/core/functions/expand/expand.d.ts +42 -0
- package/output/core/functions/expand/expand.js +376 -0
- package/output/core/functions/fresnelNumeric.d.ts +12 -0
- package/output/core/functions/fresnelNumeric.js +123 -0
- package/output/core/functions/numeric.d.ts +140 -0
- package/output/core/functions/numeric.js +790 -0
- package/output/core/functions/rationalNormalization.d.ts +11 -0
- package/output/core/functions/rationalNormalization.js +117 -0
- package/output/core/functions/setFunction.d.ts +19 -0
- package/output/core/functions/setFunction.js +75 -0
- package/output/core/functions/string.d.ts +26 -0
- package/output/core/functions/string.js +101 -0
- package/output/core/functions/subst.d.ts +90 -0
- package/output/core/functions/subst.js +498 -0
- package/output/core/functions/utils.d.ts +24 -0
- package/output/core/functions/utils.js +51 -0
- package/output/core/types.d.ts +48 -0
- package/output/core/types.js +2 -0
- package/output/index.d.ts +339 -0
- package/output/index.js +715 -0
- package/output/math/defint/defintDecimal.d.ts +37 -0
- package/output/math/defint/defintDecimal.js +320 -0
- package/output/math/defint/defintNative.d.ts +57 -0
- package/output/math/defint/defintNative.js +292 -0
- package/output/math/geometry.d.ts +8 -0
- package/output/math/geometry.js +32 -0
- package/output/math/math.d.ts +655 -0
- package/output/math/math.js +1811 -0
- package/output/math/trig.d.ts +518 -0
- package/output/math/trig.js +1444 -0
- package/output/math/trunc.d.ts +19 -0
- package/output/math/trunc.js +36 -0
- package/output/math/utils.d.ts +60 -0
- package/output/math/utils.js +180 -0
- package/output/solve/classes/DecimalMatrix.d.ts +17 -0
- package/output/solve/classes/DecimalMatrix.js +86 -0
- package/output/solve/classes/FunctionSolver.d.ts +135 -0
- package/output/solve/classes/FunctionSolver.js +433 -0
- package/output/solve/classes/MultivariateSolver.d.ts +84 -0
- package/output/solve/classes/MultivariateSolver.js +240 -0
- package/output/solve/classes/PolynomialSolver.d.ts +85 -0
- package/output/solve/classes/PolynomialSolver.js +292 -0
- package/output/solve/classes/SolutionSet.d.ts +153 -0
- package/output/solve/classes/SolutionSet.js +357 -0
- package/output/solve/classes/Solver.d.ts +17 -0
- package/output/solve/classes/Solver.js +132 -0
- package/output/solve/classes/SymbolicSolver.d.ts +74 -0
- package/output/solve/classes/SymbolicSolver.js +601 -0
- package/output/solve/linsolve.d.ts +51 -0
- package/output/solve/linsolve.js +225 -0
- package/output/solve/solve.d.ts +34 -0
- package/output/solve/solve.js +260 -0
- package/output/solve/solveSystem.d.ts +31 -0
- package/output/solve/solveSystem.js +290 -0
- package/output/solve/utils.d.ts +9 -0
- package/output/solve/utils.js +62 -0
- package/output/utils/array.d.ts +42 -0
- package/output/utils/array.js +98 -0
- package/output/utils/debug.d.ts +131 -0
- package/output/utils/debug.js +295 -0
- package/output/utils/decimal.d.ts +3 -0
- package/output/utils/decimal.js +16 -0
- package/output/utils/numeric.d.ts +14 -0
- package/output/utils/numeric.js +51 -0
- package/output/utils/object.d.ts +26 -0
- package/output/utils/object.js +56 -0
- package/package.json +213 -57
- package/.travis.yml +0 -3
- package/Algebra.js +0 -4568
- package/BREAKING_CHANGES.md +0 -5
- package/CODE_OF_CONDUCT.md +0 -46
- package/CONTRIBUTING.md +0 -14
- package/Calculus.js +0 -2675
- package/Extra.js +0 -622
- package/Solve.js +0 -1783
- package/all.js +0 -16
- package/all.min.js +0 -1
- package/gulpfile.js +0 -16
- package/index.html +0 -158
- package/license.txt +0 -19
- package/nerdamer.core.js +0 -12510
- package/spec/LaTeX.spec.js +0 -302
- package/spec/TeXConvert.spec.js +0 -8
- package/spec/algebra.spec.js +0 -287
- package/spec/basic_parser.spec.js +0 -134
- package/spec/build.spec.js +0 -131
- package/spec/calculus.spec.js +0 -183
- package/spec/core.spec.js +0 -2970
- package/spec/extra.spec.js +0 -54
- package/spec/solve.spec.js +0 -126
- package/spec/support/jasmine.json +0 -11
- package/spec/support/utils.js +0 -42
- package/spec/text.spec.js +0 -132
|
@@ -0,0 +1,790 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable no-loss-of-precision */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.EULER_GAMMA = void 0;
|
|
5
|
+
exports.factorial = factorial;
|
|
6
|
+
exports.gammaLN = gammaLN;
|
|
7
|
+
exports.gamma = gamma;
|
|
8
|
+
exports.erfc = erfc;
|
|
9
|
+
exports.erf = erf;
|
|
10
|
+
exports.GCD = GCD;
|
|
11
|
+
exports.isInt = isInt;
|
|
12
|
+
exports.doubleFactorial = doubleFactorial;
|
|
13
|
+
exports.LCM = LCM;
|
|
14
|
+
exports.modInv = modInv;
|
|
15
|
+
exports.isPrime = isPrime;
|
|
16
|
+
exports.heaviside = heaviside;
|
|
17
|
+
exports.round = round;
|
|
18
|
+
exports.product = product;
|
|
19
|
+
exports.productInRange = productInRange;
|
|
20
|
+
exports.avg = avg;
|
|
21
|
+
exports.sum = sum;
|
|
22
|
+
exports.getRandomInt = getRandomInt;
|
|
23
|
+
exports.mod = mod;
|
|
24
|
+
exports.diff = diff;
|
|
25
|
+
exports.sinc = sinc;
|
|
26
|
+
exports.Si = Si;
|
|
27
|
+
exports.Shi = Shi;
|
|
28
|
+
exports.Ci = Ci;
|
|
29
|
+
exports.Chi = Chi;
|
|
30
|
+
exports.Ei = Ei;
|
|
31
|
+
exports.Li = Li;
|
|
32
|
+
const EULER_GAMMA = 0.577215664901532860606512090082402431;
|
|
33
|
+
exports.EULER_GAMMA = EULER_GAMMA;
|
|
34
|
+
const DEFAULT_NUMERICAL_DERIVATIVE_STEP = 1e-4;
|
|
35
|
+
/**
|
|
36
|
+
* Computes factorial values with JavaScript-number arithmetic for compiled expressions.
|
|
37
|
+
*
|
|
38
|
+
* Non-integers use the gamma extension. Negative or unsafe integers return `NaN`, and
|
|
39
|
+
* sufficiently large positive integers naturally overflow to `Infinity`.
|
|
40
|
+
*/
|
|
41
|
+
function factorial(x) {
|
|
42
|
+
const MAX_FINITE_FACTORIAL_INPUT = 170;
|
|
43
|
+
if (!isInt(x)) {
|
|
44
|
+
return gamma(x + 1);
|
|
45
|
+
}
|
|
46
|
+
if (!Number.isSafeInteger(x) || x < 0) {
|
|
47
|
+
return NaN;
|
|
48
|
+
}
|
|
49
|
+
if (x > MAX_FINITE_FACTORIAL_INPUT) {
|
|
50
|
+
return Infinity;
|
|
51
|
+
}
|
|
52
|
+
const cache = [
|
|
53
|
+
1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600, 6227020800,
|
|
54
|
+
87178291200, 1307674368000, 20922789888000, 355687428096000, 6402373705728000,
|
|
55
|
+
121645100408832000, 2432902008176640000, 51090942171709440000, 1.1240007277776077e21,
|
|
56
|
+
2.585201673888498e22, 6.204484017332394e23, 1.5511210043330986e25, 4.0329146112660565e26,
|
|
57
|
+
1.0888869450418352e28, 3.0488834461171384e29, 8.841761993739701e30, 2.6525285981219103e32,
|
|
58
|
+
];
|
|
59
|
+
if (cache[x] !== undefined) {
|
|
60
|
+
return cache[x];
|
|
61
|
+
}
|
|
62
|
+
const start = cache.length - 1;
|
|
63
|
+
let result = cache[start];
|
|
64
|
+
for (let i = start + 1; i <= x; i++) {
|
|
65
|
+
result *= i;
|
|
66
|
+
}
|
|
67
|
+
return result;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The gammaLN function which is used to compute the gamma function
|
|
71
|
+
* Ported from: https://www.mrob.com/pub/ries/lanczos-gamma.html
|
|
72
|
+
*
|
|
73
|
+
* @param x
|
|
74
|
+
*/
|
|
75
|
+
function gammaLN(z) {
|
|
76
|
+
const g = 607 / 128;
|
|
77
|
+
const N = 14;
|
|
78
|
+
const lct = [
|
|
79
|
+
0.9999999999999953, 57.15623566586292, -59.59796035547549, 14.136097974741746,
|
|
80
|
+
-0.4919138160976202, 0.00003399464998481189, 0.00004652362892704858,
|
|
81
|
+
-0.00009837447530487956, 0.0001580887032249125, -0.00021026444172410488,
|
|
82
|
+
0.00021743961811521265, -0.0001643181065367639, 0.00008441822398385275,
|
|
83
|
+
-0.000026190838401581408, 0.0000036899182659531625,
|
|
84
|
+
];
|
|
85
|
+
let sum = 0;
|
|
86
|
+
if (z < 0.5) {
|
|
87
|
+
return Math.log(Math.PI / Math.sin(z * Math.PI)) - gammaLN(1 - z);
|
|
88
|
+
}
|
|
89
|
+
z = z - 1;
|
|
90
|
+
const base = z + g + 0.5;
|
|
91
|
+
for (let i = N; i >= 1; i--) {
|
|
92
|
+
sum += lct[i] / (z + i);
|
|
93
|
+
}
|
|
94
|
+
sum += lct[0];
|
|
95
|
+
return Math.log(Math.sqrt(Math.PI * 2)) + Math.log(sum) - base + Math.log(base) * (z + 0.5);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* The gamma function
|
|
99
|
+
*
|
|
100
|
+
* @param z
|
|
101
|
+
* @returns
|
|
102
|
+
*/
|
|
103
|
+
function gamma(z) {
|
|
104
|
+
if (isInt(z) && z > 1) {
|
|
105
|
+
return factorial(z - 1);
|
|
106
|
+
}
|
|
107
|
+
return Math.exp(gammaLN(z));
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* The complementary error function
|
|
111
|
+
* https://en.wikipedia.org/wiki/Error_function
|
|
112
|
+
*
|
|
113
|
+
* @param x
|
|
114
|
+
*/
|
|
115
|
+
function erfc(x) {
|
|
116
|
+
if (x < 0) {
|
|
117
|
+
return 2 - erfc(-x);
|
|
118
|
+
}
|
|
119
|
+
const xsq = x ** 2;
|
|
120
|
+
const a1 = 0.56418958354775629 / (x + 2.06955023132914151);
|
|
121
|
+
const a2 = (xsq + 2.71078540045147805 * x + 5.80755613130301624) /
|
|
122
|
+
(xsq + 3.4795405709951896 * x + 12.06166887286239555);
|
|
123
|
+
const a3 = (xsq + 3.47469513777439592 * x + 12.07402036406381411) /
|
|
124
|
+
(xsq + 3.72068443960225092 * x + 8.44319781003968454);
|
|
125
|
+
const a4 = (xsq + 4.00561509202259545 * x + 9.30596659485887898) /
|
|
126
|
+
(xsq + 3.90225704029924078 * x + 6.36161630953880464);
|
|
127
|
+
const a5 = (xsq + 5.16722705817812584 * x + 9.12661617673673262) /
|
|
128
|
+
(xsq + 4.03296893109262491 * x + 5.13578530585681539);
|
|
129
|
+
const a6 = (xsq + 5.95908795446633271 * x + 9.19435612886969243) /
|
|
130
|
+
(xsq + 4.11240942957450885 * x + 4.48640329523408675);
|
|
131
|
+
return a1 * a2 * a3 * a4 * a5 * a6 * Math.exp(-xsq);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* The numeric error function
|
|
135
|
+
*
|
|
136
|
+
* @param x
|
|
137
|
+
* @returns
|
|
138
|
+
*/
|
|
139
|
+
function erf(x) {
|
|
140
|
+
if (x === 0) {
|
|
141
|
+
return 0;
|
|
142
|
+
}
|
|
143
|
+
return 1 - erfc(x);
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Computes the Euclidean GCD of JavaScript safe integers for numerical compilation.
|
|
147
|
+
*
|
|
148
|
+
* This is the small native-number helper used by `buildFunction`; it is separate from
|
|
149
|
+
* Nerdamer's symbolic polynomial GCD implementation. Non-safe-integer inputs return `NaN`.
|
|
150
|
+
*/
|
|
151
|
+
function GCD(...args) {
|
|
152
|
+
let retval = 0;
|
|
153
|
+
const values = [...new Set(args)].map(x => Math.abs(x));
|
|
154
|
+
for (const value of values) {
|
|
155
|
+
if (!Number.isSafeInteger(value)) {
|
|
156
|
+
retval = NaN;
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
let b = value;
|
|
160
|
+
while (b !== 0) {
|
|
161
|
+
const remainder = retval % b;
|
|
162
|
+
retval = b;
|
|
163
|
+
b = remainder;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return retval;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* A utility function to check if a number is an integer
|
|
170
|
+
*
|
|
171
|
+
* @param x
|
|
172
|
+
* @returns
|
|
173
|
+
*/
|
|
174
|
+
function isInt(x) {
|
|
175
|
+
return x % 1 === 0;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Computes the double factorial using JavaScript-number arithmetic.
|
|
179
|
+
*
|
|
180
|
+
* Non-negative integer inputs use the finite product directly. Non-integers follow
|
|
181
|
+
* the same gamma-based extension used by the symbolic implementation. Negative or
|
|
182
|
+
* unsafe integer inputs return `NaN`.
|
|
183
|
+
*/
|
|
184
|
+
function doubleFactorial(x) {
|
|
185
|
+
if (isInt(x)) {
|
|
186
|
+
if (!Number.isSafeInteger(x) || x < 0) {
|
|
187
|
+
return NaN;
|
|
188
|
+
}
|
|
189
|
+
let retval = 1;
|
|
190
|
+
for (let n = x; n > 1; n -= 2) {
|
|
191
|
+
retval *= n;
|
|
192
|
+
if (!Number.isFinite(retval)) {
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return retval;
|
|
197
|
+
}
|
|
198
|
+
const c = Math.cos(Math.PI * x);
|
|
199
|
+
return (Math.pow(2, (2 * x + 1 - c) / 4) *
|
|
200
|
+
Math.pow(Math.PI, (c - 1) / 4) *
|
|
201
|
+
gamma(x / 2 + 1));
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Computes an LCM while the exact integer result remains representable as a safe number.
|
|
205
|
+
* Inputs or results outside that range return `NaN`.
|
|
206
|
+
*/
|
|
207
|
+
function LCM(...args) {
|
|
208
|
+
let retval = 1;
|
|
209
|
+
for (const value of args) {
|
|
210
|
+
if (!Number.isSafeInteger(value)) {
|
|
211
|
+
retval = NaN;
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
if (value === 0) {
|
|
215
|
+
retval = 0;
|
|
216
|
+
break;
|
|
217
|
+
}
|
|
218
|
+
const gcd = GCD(retval, value);
|
|
219
|
+
const next = Math.abs((retval / gcd) * value);
|
|
220
|
+
if (!Number.isSafeInteger(next)) {
|
|
221
|
+
retval = NaN;
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
retval = next;
|
|
225
|
+
}
|
|
226
|
+
return retval;
|
|
227
|
+
}
|
|
228
|
+
/** Computes the modular multiplicative inverse for integer JavaScript numbers. */
|
|
229
|
+
function modInv(a, p) {
|
|
230
|
+
if (!Number.isSafeInteger(a) || !Number.isSafeInteger(p) || p === 0) {
|
|
231
|
+
return NaN;
|
|
232
|
+
}
|
|
233
|
+
let t = 0;
|
|
234
|
+
let nextT = 1;
|
|
235
|
+
let r = Math.abs(p);
|
|
236
|
+
let nextR = mod(a, r);
|
|
237
|
+
while (nextR !== 0) {
|
|
238
|
+
const q = Math.floor(r / nextR);
|
|
239
|
+
[t, nextT] = [nextT, t - q * nextT];
|
|
240
|
+
[r, nextR] = [nextR, r - q * nextR];
|
|
241
|
+
}
|
|
242
|
+
return r === 1 ? mod(t, Math.abs(p)) : NaN;
|
|
243
|
+
}
|
|
244
|
+
/** Returns `1` for a prime safe integer and `0` otherwise. */
|
|
245
|
+
function isPrime(x) {
|
|
246
|
+
if (!Number.isSafeInteger(x) || x < 2) {
|
|
247
|
+
return 0;
|
|
248
|
+
}
|
|
249
|
+
if (x === 2) {
|
|
250
|
+
return 1;
|
|
251
|
+
}
|
|
252
|
+
if (x % 2 === 0) {
|
|
253
|
+
return 0;
|
|
254
|
+
}
|
|
255
|
+
for (let divisor = 3; divisor * divisor <= x; divisor += 2) {
|
|
256
|
+
if (x % divisor === 0) {
|
|
257
|
+
return 0;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
return 1;
|
|
261
|
+
}
|
|
262
|
+
/** Numeric half-maximum convention for the Heaviside step function. */
|
|
263
|
+
function heaviside(x) {
|
|
264
|
+
return Number.isNaN(x) ? NaN : x < 0 ? 0 : x > 0 ? 1 : 0.5;
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Rounds a number to n digits
|
|
268
|
+
* @param x The number to be rounded
|
|
269
|
+
* @param s The number of digits to round to
|
|
270
|
+
* @returns
|
|
271
|
+
*/
|
|
272
|
+
function round(x, n = 0) {
|
|
273
|
+
let retval = x;
|
|
274
|
+
if (!isInt(x)) {
|
|
275
|
+
const scale = Math.pow(10, n);
|
|
276
|
+
const scaled = x * scale;
|
|
277
|
+
const rounded = scaled < 0 ? -Math.round(-scaled) : Math.round(scaled);
|
|
278
|
+
retval = rounded / scale;
|
|
279
|
+
}
|
|
280
|
+
return retval;
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Multiplies an array of numbers
|
|
284
|
+
*
|
|
285
|
+
* @param args
|
|
286
|
+
* @returns
|
|
287
|
+
*/
|
|
288
|
+
function product(...args) {
|
|
289
|
+
let retval = 1;
|
|
290
|
+
for (let i = 0; i < args.length; i++) {
|
|
291
|
+
retval *= args[i];
|
|
292
|
+
}
|
|
293
|
+
return retval;
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Gets the product given a number range start and finish
|
|
297
|
+
*
|
|
298
|
+
* @param start The start of the range
|
|
299
|
+
* @param end The end of the range
|
|
300
|
+
* @param step The increment step
|
|
301
|
+
*/
|
|
302
|
+
function productInRange(start, end, step = 1) {
|
|
303
|
+
let retval = start;
|
|
304
|
+
let n = start;
|
|
305
|
+
while (n < end) {
|
|
306
|
+
n += step;
|
|
307
|
+
retval *= n;
|
|
308
|
+
}
|
|
309
|
+
return retval;
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Gets the average of a set of numbers
|
|
313
|
+
*
|
|
314
|
+
* @param args
|
|
315
|
+
* @returns
|
|
316
|
+
*/
|
|
317
|
+
function avg(...args) {
|
|
318
|
+
return sum(...args) / args.length;
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Sums an array of numbers
|
|
322
|
+
*
|
|
323
|
+
* @param args
|
|
324
|
+
* @returns
|
|
325
|
+
*/
|
|
326
|
+
function sum(...args) {
|
|
327
|
+
let retval = 0;
|
|
328
|
+
for (let i = 0; i < args.length; i++) {
|
|
329
|
+
retval += args[i];
|
|
330
|
+
}
|
|
331
|
+
return retval;
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Generates a random integer between a min and max
|
|
335
|
+
*
|
|
336
|
+
* @param min
|
|
337
|
+
* @param max
|
|
338
|
+
* @returns
|
|
339
|
+
*/
|
|
340
|
+
function getRandomInt(min, max) {
|
|
341
|
+
min = Math.ceil(min);
|
|
342
|
+
max = Math.floor(max);
|
|
343
|
+
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Returns the modulus of a number. Allows for negative numbers
|
|
347
|
+
*
|
|
348
|
+
* @param n
|
|
349
|
+
* @param m
|
|
350
|
+
* @returns
|
|
351
|
+
*/
|
|
352
|
+
function mod(n, m) {
|
|
353
|
+
return ((n % m) + m) % m;
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Return a function which returns a numeric approximation of the derivative of the provided function
|
|
357
|
+
* @example
|
|
358
|
+
* const d = diff(Math.cos);
|
|
359
|
+
* d(Math.PI) // 0
|
|
360
|
+
*
|
|
361
|
+
* @param f The function to be differentiated
|
|
362
|
+
* @param h The delta to be used
|
|
363
|
+
* @returns
|
|
364
|
+
*/
|
|
365
|
+
function diff(f, h = DEFAULT_NUMERICAL_DERIVATIVE_STEP) {
|
|
366
|
+
return (x) => {
|
|
367
|
+
return (f(x + h) - f(x - h)) / (2 * h);
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
function sinc(x) {
|
|
371
|
+
const SINC_SERIES_CUTOFF = 1e-4;
|
|
372
|
+
if (x === 0) {
|
|
373
|
+
return 1;
|
|
374
|
+
}
|
|
375
|
+
const ax = Math.abs(x);
|
|
376
|
+
if (ax < SINC_SERIES_CUTOFF) {
|
|
377
|
+
const x2 = x * x;
|
|
378
|
+
return 1 - x2 / 6 + (x2 * x2) / 120 - (x2 * x2 * x2) / 5040;
|
|
379
|
+
}
|
|
380
|
+
return Math.sin(x) / x;
|
|
381
|
+
}
|
|
382
|
+
function Si(x) {
|
|
383
|
+
const OSCILLATORY_ASYMPTOTIC_CUTOFF = 1e9;
|
|
384
|
+
const RATIONAL_APPROXIMATION_CUTOFF = 4;
|
|
385
|
+
const ASYMPTOTIC_APPROXIMATION_CUTOFF = 8;
|
|
386
|
+
if (x === 0) {
|
|
387
|
+
return 0;
|
|
388
|
+
}
|
|
389
|
+
let sign = 1;
|
|
390
|
+
if (x < 0) {
|
|
391
|
+
sign = -1;
|
|
392
|
+
x = -x;
|
|
393
|
+
}
|
|
394
|
+
function polevl(x, coef) {
|
|
395
|
+
let ans = coef[0];
|
|
396
|
+
for (let i = 1; i < coef.length; i++) {
|
|
397
|
+
ans = ans * x + coef[i];
|
|
398
|
+
}
|
|
399
|
+
return ans;
|
|
400
|
+
}
|
|
401
|
+
function p1evl(x, coef) {
|
|
402
|
+
let ans = x + coef[0];
|
|
403
|
+
for (let i = 1; i < coef.length; i++) {
|
|
404
|
+
ans = ans * x + coef[i];
|
|
405
|
+
}
|
|
406
|
+
return ans;
|
|
407
|
+
}
|
|
408
|
+
if (x > OSCILLATORY_ASYMPTOTIC_CUTOFF) {
|
|
409
|
+
return sign * (Math.PI / 2 - Math.cos(x) / x);
|
|
410
|
+
}
|
|
411
|
+
// Small-x rational approximation
|
|
412
|
+
if (x <= RATIONAL_APPROXIMATION_CUTOFF) {
|
|
413
|
+
const SN = [
|
|
414
|
+
-8.39167827910303881427e-11, 4.62591714427012837309e-8, -9.75759303843632795789e-6,
|
|
415
|
+
9.76945438170435310816e-4, -4.13470316229406538752e-2, 1.00000000000000000302,
|
|
416
|
+
];
|
|
417
|
+
const SD = [
|
|
418
|
+
2.03269266195951942049e-12, 1.27997891179943299903e-9, 4.41827842801218905784e-7,
|
|
419
|
+
9.96412122043875552487e-5, 1.4208523932614989393e-2, 9.99999999999999996984e-1,
|
|
420
|
+
];
|
|
421
|
+
const z = x * x;
|
|
422
|
+
const s = (x * polevl(z, SN)) / polevl(z, SD);
|
|
423
|
+
return sign * s;
|
|
424
|
+
}
|
|
425
|
+
// Asymptotic region
|
|
426
|
+
const s = Math.sin(x);
|
|
427
|
+
const c = Math.cos(x);
|
|
428
|
+
const z = 1.0 / (x * x);
|
|
429
|
+
let f, g;
|
|
430
|
+
if (x < ASYMPTOTIC_APPROXIMATION_CUTOFF) {
|
|
431
|
+
const FN4 = [
|
|
432
|
+
4.23612862892216586994, 5.45937717161812843388, 1.62083287701538329132,
|
|
433
|
+
1.67006611831323023771e-1, 6.81020132472518137426e-3, 1.08936580650328664411e-4,
|
|
434
|
+
5.48900223421373614008e-7,
|
|
435
|
+
];
|
|
436
|
+
const FD4 = [
|
|
437
|
+
8.16496634205391016773, 7.30828822505564552187, 1.86792257950184183883,
|
|
438
|
+
1.78792052963149907262e-1, 7.0171066832278975361e-3, 1.10034357153915731354e-4,
|
|
439
|
+
5.48900252756255700982e-7,
|
|
440
|
+
];
|
|
441
|
+
const GN4 = [
|
|
442
|
+
8.71001698973114191777e-2, 6.11379109952219284151e-1, 3.97180296392337498885e-1,
|
|
443
|
+
7.48527737628469092119e-2, 5.38868681462177273157e-3, 1.61999794598934024525e-4,
|
|
444
|
+
1.97963874140963632189e-6, 7.82579040744090311069e-9,
|
|
445
|
+
];
|
|
446
|
+
const GD4 = [
|
|
447
|
+
1.64402202413355338886, 6.66296701268987968381e-1, 9.88771761277688796203e-2,
|
|
448
|
+
6.2239634544176842076e-3, 1.73221081474177119497e-4, 2.02659182086343991969e-6,
|
|
449
|
+
7.82579218933534490868e-9,
|
|
450
|
+
];
|
|
451
|
+
f = polevl(z, FN4) / (x * p1evl(z, FD4));
|
|
452
|
+
g = (z * polevl(z, GN4)) / p1evl(z, GD4);
|
|
453
|
+
}
|
|
454
|
+
else {
|
|
455
|
+
const FN8 = [
|
|
456
|
+
4.55880873470465315206e-1, 7.13715274100146711374e-1, 1.6030015822231945632e-1,
|
|
457
|
+
1.16064229408124407915e-2, 3.49556442447859055605e-4, 4.86215430826454749482e-6,
|
|
458
|
+
3.20092790091004902806e-8, 9.41779576128512936592e-11, 9.70507110881952024631e-14,
|
|
459
|
+
];
|
|
460
|
+
const FD8 = [
|
|
461
|
+
9.17463611873684053703e-1, 1.78685545332074536321e-1, 1.22253594771971293032e-2,
|
|
462
|
+
3.58696481881851580297e-4, 4.92435064317881464393e-6, 3.21956939101046018377e-8,
|
|
463
|
+
9.43720590350276732376e-11, 9.70507110881952025725e-14,
|
|
464
|
+
];
|
|
465
|
+
const GN8 = [
|
|
466
|
+
6.97359953443276214934e-1, 3.30410979305632063225e-1, 3.8487876764997429592e-2,
|
|
467
|
+
1.71718239052347903558e-3, 3.48941165502279436777e-5, 3.471311670841166738e-7,
|
|
468
|
+
1.70404452782044526189e-9, 3.85945925430276600453e-12, 3.1404009894636333464e-15,
|
|
469
|
+
];
|
|
470
|
+
const GD8 = [
|
|
471
|
+
1.68548898811011640017, 4.87852258695304967486e-1, 4.6791319425962580632e-2,
|
|
472
|
+
1.90284426674399523638e-3, 3.68475504442561108162e-5, 3.57043223443740838771e-7,
|
|
473
|
+
1.72693748966316146736e-9, 3.87830166023954706752e-12, 3.14040098946363335242e-15,
|
|
474
|
+
];
|
|
475
|
+
f = polevl(z, FN8) / (x * p1evl(z, FD8));
|
|
476
|
+
g = (z * polevl(z, GN8)) / p1evl(z, GD8);
|
|
477
|
+
}
|
|
478
|
+
const si = Math.PI / 2 - f * c - g * s;
|
|
479
|
+
return sign * si;
|
|
480
|
+
}
|
|
481
|
+
function Shi(x) {
|
|
482
|
+
const ASYMPTOTIC_APPROXIMATION_CUTOFF = 8;
|
|
483
|
+
const EXPONENTIAL_ASYMPTOTIC_CUTOFF = 40;
|
|
484
|
+
const SERIES_MAX_ITERATIONS = 200;
|
|
485
|
+
const ASYMPTOTIC_MAX_TERMS = 100;
|
|
486
|
+
const POWER_SERIES_MAX_TERMS = 500;
|
|
487
|
+
const SERIES_RELATIVE_TOLERANCE = 1e-16;
|
|
488
|
+
if (x === 0) {
|
|
489
|
+
return 0;
|
|
490
|
+
}
|
|
491
|
+
const sign = x < 0 ? -1 : 1;
|
|
492
|
+
const ax = Math.abs(x);
|
|
493
|
+
// Power series for small |x|
|
|
494
|
+
if (ax < ASYMPTOTIC_APPROXIMATION_CUTOFF) {
|
|
495
|
+
let term = ax;
|
|
496
|
+
let sum = term;
|
|
497
|
+
let n = 0;
|
|
498
|
+
for (let iter = 0; iter < SERIES_MAX_ITERATIONS; iter++) {
|
|
499
|
+
const a = 2 * n + 1;
|
|
500
|
+
term *= (ax * ax * a) / ((a + 2) * (a + 2) * (a + 1));
|
|
501
|
+
sum += term;
|
|
502
|
+
n++;
|
|
503
|
+
if (Math.abs(term) < Math.abs(sum) * SERIES_RELATIVE_TOLERANCE) {
|
|
504
|
+
break;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
return sign * sum;
|
|
508
|
+
}
|
|
509
|
+
// Inline real Ei(y)
|
|
510
|
+
function Ei(y) {
|
|
511
|
+
if (y === 0) {
|
|
512
|
+
return -Infinity;
|
|
513
|
+
}
|
|
514
|
+
// Asymptotic expansion for large |y|
|
|
515
|
+
if (y > EXPONENTIAL_ASYMPTOTIC_CUTOFF || y < -EXPONENTIAL_ASYMPTOTIC_CUTOFF) {
|
|
516
|
+
let term = 1;
|
|
517
|
+
let sum = 1;
|
|
518
|
+
let best = Math.abs(term);
|
|
519
|
+
for (let n = 1; n < ASYMPTOTIC_MAX_TERMS; n++) {
|
|
520
|
+
term *= n / y;
|
|
521
|
+
const at = Math.abs(term);
|
|
522
|
+
if (at > best) {
|
|
523
|
+
break;
|
|
524
|
+
}
|
|
525
|
+
best = at;
|
|
526
|
+
sum += term;
|
|
527
|
+
if (at < Math.abs(sum) * SERIES_RELATIVE_TOLERANCE) {
|
|
528
|
+
break;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
return (Math.exp(y) * sum) / y;
|
|
532
|
+
}
|
|
533
|
+
// Power series
|
|
534
|
+
let term = y;
|
|
535
|
+
let sum = term;
|
|
536
|
+
for (let k = 1; k < POWER_SERIES_MAX_TERMS; k++) {
|
|
537
|
+
term *= (y * k) / ((k + 1) * (k + 1));
|
|
538
|
+
sum += term;
|
|
539
|
+
if (Math.abs(term) < Math.abs(sum) * SERIES_RELATIVE_TOLERANCE) {
|
|
540
|
+
break;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
return EULER_GAMMA + Math.log(Math.abs(y)) + sum;
|
|
544
|
+
}
|
|
545
|
+
return 0.5 * (Ei(x) - Ei(-x));
|
|
546
|
+
}
|
|
547
|
+
function Ci(x) {
|
|
548
|
+
const OSCILLATORY_ASYMPTOTIC_CUTOFF = 1e9;
|
|
549
|
+
const RATIONAL_APPROXIMATION_CUTOFF = 4;
|
|
550
|
+
const ASYMPTOTIC_APPROXIMATION_CUTOFF = 8;
|
|
551
|
+
if (x === 0) {
|
|
552
|
+
return -Infinity;
|
|
553
|
+
}
|
|
554
|
+
if (x < 0) {
|
|
555
|
+
x = -x;
|
|
556
|
+
}
|
|
557
|
+
function polevl(x, coef) {
|
|
558
|
+
let ans = coef[0];
|
|
559
|
+
for (let i = 1; i < coef.length; i++) {
|
|
560
|
+
ans = ans * x + coef[i];
|
|
561
|
+
}
|
|
562
|
+
return ans;
|
|
563
|
+
}
|
|
564
|
+
function p1evl(x, coef) {
|
|
565
|
+
let ans = x + coef[0];
|
|
566
|
+
for (let i = 1; i < coef.length; i++) {
|
|
567
|
+
ans = ans * x + coef[i];
|
|
568
|
+
}
|
|
569
|
+
return ans;
|
|
570
|
+
}
|
|
571
|
+
if (x > OSCILLATORY_ASYMPTOTIC_CUTOFF) {
|
|
572
|
+
return Math.sin(x) / x;
|
|
573
|
+
}
|
|
574
|
+
// Small-x rational approximation
|
|
575
|
+
if (x <= RATIONAL_APPROXIMATION_CUTOFF) {
|
|
576
|
+
const CN = [
|
|
577
|
+
2.02524002389102268789e-11, -1.35249504915790756375e-8, 3.59325051419993077021e-6,
|
|
578
|
+
-4.74007206873407909465e-4, 2.89159652607555242092e-2, -1.0000000000000000008,
|
|
579
|
+
];
|
|
580
|
+
const CD = [
|
|
581
|
+
4.07746040061880559506e-12, 3.06780997581887812692e-9, 1.23210355685883423679e-6,
|
|
582
|
+
3.17442024775032769882e-4, 5.10028056236446052392e-2, 4.0000000000000000008,
|
|
583
|
+
];
|
|
584
|
+
const z = x * x;
|
|
585
|
+
const c0 = (z * polevl(z, CN)) / polevl(z, CD);
|
|
586
|
+
return EULER_GAMMA + Math.log(x) + c0;
|
|
587
|
+
}
|
|
588
|
+
// Asymptotic region
|
|
589
|
+
const s = Math.sin(x);
|
|
590
|
+
const c = Math.cos(x);
|
|
591
|
+
const z = 1.0 / (x * x);
|
|
592
|
+
let f, g;
|
|
593
|
+
if (x < ASYMPTOTIC_APPROXIMATION_CUTOFF) {
|
|
594
|
+
const FN4 = [
|
|
595
|
+
4.23612862892216586994, 5.45937717161812843388, 1.62083287701538329132,
|
|
596
|
+
1.67006611831323023771e-1, 6.81020132472518137426e-3, 1.08936580650328664411e-4,
|
|
597
|
+
5.48900223421373614008e-7,
|
|
598
|
+
];
|
|
599
|
+
const FD4 = [
|
|
600
|
+
8.16496634205391016773, 7.30828822505564552187, 1.86792257950184183883,
|
|
601
|
+
1.78792052963149907262e-1, 7.0171066832278975361e-3, 1.10034357153915731354e-4,
|
|
602
|
+
5.48900252756255700982e-7,
|
|
603
|
+
];
|
|
604
|
+
const GN4 = [
|
|
605
|
+
8.71001698973114191777e-2, 6.11379109952219284151e-1, 3.97180296392337498885e-1,
|
|
606
|
+
7.48527737628469092119e-2, 5.38868681462177273157e-3, 1.61999794598934024525e-4,
|
|
607
|
+
1.97963874140963632189e-6, 7.82579040744090311069e-9,
|
|
608
|
+
];
|
|
609
|
+
const GD4 = [
|
|
610
|
+
1.64402202413355338886, 6.66296701268987968381e-1, 9.88771761277688796203e-2,
|
|
611
|
+
6.2239634544176842076e-3, 1.73221081474177119497e-4, 2.02659182086343991969e-6,
|
|
612
|
+
7.82579218933534490868e-9,
|
|
613
|
+
];
|
|
614
|
+
f = polevl(z, FN4) / (x * p1evl(z, FD4));
|
|
615
|
+
g = (z * polevl(z, GN4)) / p1evl(z, GD4);
|
|
616
|
+
}
|
|
617
|
+
else {
|
|
618
|
+
const FN8 = [
|
|
619
|
+
4.55880873470465315206e-1, 7.13715274100146711374e-1, 1.6030015822231945632e-1,
|
|
620
|
+
1.16064229408124407915e-2, 3.49556442447859055605e-4, 4.86215430826454749482e-6,
|
|
621
|
+
3.20092790091004902806e-8, 9.41779576128512936592e-11, 9.70507110881952024631e-14,
|
|
622
|
+
];
|
|
623
|
+
const FD8 = [
|
|
624
|
+
9.17463611873684053703e-1, 1.78685545332074536321e-1, 1.22253594771971293032e-2,
|
|
625
|
+
3.58696481881851580297e-4, 4.92435064317881464393e-6, 3.21956939101046018377e-8,
|
|
626
|
+
9.43720590350276732376e-11, 9.70507110881952025725e-14,
|
|
627
|
+
];
|
|
628
|
+
const GN8 = [
|
|
629
|
+
6.97359953443276214934e-1, 3.30410979305632063225e-1, 3.8487876764997429592e-2,
|
|
630
|
+
1.71718239052347903558e-3, 3.48941165502279436777e-5, 3.471311670841166738e-7,
|
|
631
|
+
1.70404452782044526189e-9, 3.85945925430276600453e-12, 3.1404009894636333464e-15,
|
|
632
|
+
];
|
|
633
|
+
const GD8 = [
|
|
634
|
+
1.68548898811011640017, 4.87852258695304967486e-1, 4.6791319425962580632e-2,
|
|
635
|
+
1.90284426674399523638e-3, 3.68475504442561108162e-5, 3.57043223443740838771e-7,
|
|
636
|
+
1.72693748966316146736e-9, 3.87830166023954706752e-12, 3.14040098946363335242e-15,
|
|
637
|
+
];
|
|
638
|
+
f = polevl(z, FN8) / (x * p1evl(z, FD8));
|
|
639
|
+
g = (z * polevl(z, GN8)) / p1evl(z, GD8);
|
|
640
|
+
}
|
|
641
|
+
return f * s - g * c;
|
|
642
|
+
}
|
|
643
|
+
function Chi(x) {
|
|
644
|
+
const ASYMPTOTIC_APPROXIMATION_CUTOFF = 8;
|
|
645
|
+
const EXPONENTIAL_ASYMPTOTIC_CUTOFF = 40;
|
|
646
|
+
const SERIES_MAX_ITERATIONS = 200;
|
|
647
|
+
const ASYMPTOTIC_MAX_TERMS = 100;
|
|
648
|
+
const POWER_SERIES_MAX_TERMS = 500;
|
|
649
|
+
const SERIES_RELATIVE_TOLERANCE = 1e-16;
|
|
650
|
+
const ax = Math.abs(x);
|
|
651
|
+
if (ax === 0) {
|
|
652
|
+
return -Infinity;
|
|
653
|
+
}
|
|
654
|
+
// Power series for small |x|
|
|
655
|
+
if (ax < ASYMPTOTIC_APPROXIMATION_CUTOFF) {
|
|
656
|
+
let n = 1;
|
|
657
|
+
let term = (ax * ax) / 4;
|
|
658
|
+
let sum = term;
|
|
659
|
+
for (let iter = 0; iter < SERIES_MAX_ITERATIONS; iter++) {
|
|
660
|
+
term *= (ax * ax * n) / ((n + 1) * (2 * n + 1) * (2 * n + 2));
|
|
661
|
+
sum += term;
|
|
662
|
+
n++;
|
|
663
|
+
if (Math.abs(term) < Math.abs(sum) * SERIES_RELATIVE_TOLERANCE) {
|
|
664
|
+
break;
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
return EULER_GAMMA + Math.log(ax) + sum;
|
|
668
|
+
}
|
|
669
|
+
// Inline real Ei(y)
|
|
670
|
+
function Ei(y) {
|
|
671
|
+
if (y === 0) {
|
|
672
|
+
return -Infinity;
|
|
673
|
+
}
|
|
674
|
+
// Asymptotic expansion for large |y|
|
|
675
|
+
if (y > EXPONENTIAL_ASYMPTOTIC_CUTOFF || y < -EXPONENTIAL_ASYMPTOTIC_CUTOFF) {
|
|
676
|
+
let term = 1;
|
|
677
|
+
let sum = 1;
|
|
678
|
+
let best = Math.abs(term);
|
|
679
|
+
for (let n = 1; n < ASYMPTOTIC_MAX_TERMS; n++) {
|
|
680
|
+
term *= n / y;
|
|
681
|
+
const at = Math.abs(term);
|
|
682
|
+
if (at > best) {
|
|
683
|
+
break;
|
|
684
|
+
}
|
|
685
|
+
best = at;
|
|
686
|
+
sum += term;
|
|
687
|
+
if (at < Math.abs(sum) * SERIES_RELATIVE_TOLERANCE) {
|
|
688
|
+
break;
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
return (Math.exp(y) * sum) / y;
|
|
692
|
+
}
|
|
693
|
+
// Power series
|
|
694
|
+
let term = y;
|
|
695
|
+
let sum = term;
|
|
696
|
+
for (let k = 1; k < POWER_SERIES_MAX_TERMS; k++) {
|
|
697
|
+
term *= (y * k) / ((k + 1) * (k + 1));
|
|
698
|
+
sum += term;
|
|
699
|
+
if (Math.abs(term) < Math.abs(sum) * SERIES_RELATIVE_TOLERANCE) {
|
|
700
|
+
break;
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
return EULER_GAMMA + Math.log(Math.abs(y)) + sum;
|
|
704
|
+
}
|
|
705
|
+
return 0.5 * (Ei(x) + Ei(-x));
|
|
706
|
+
}
|
|
707
|
+
function Ei(x) {
|
|
708
|
+
const EXPONENTIAL_ASYMPTOTIC_CUTOFF = 40;
|
|
709
|
+
const ASYMPTOTIC_MAX_TERMS = 100;
|
|
710
|
+
const POWER_SERIES_MAX_TERMS = 500;
|
|
711
|
+
const SERIES_RELATIVE_TOLERANCE = 1e-16;
|
|
712
|
+
if (x === 0) {
|
|
713
|
+
return -Infinity;
|
|
714
|
+
}
|
|
715
|
+
// Asymptotic expansion for large |x|
|
|
716
|
+
if (x > EXPONENTIAL_ASYMPTOTIC_CUTOFF || x < -EXPONENTIAL_ASYMPTOTIC_CUTOFF) {
|
|
717
|
+
let term = 1;
|
|
718
|
+
let sum = 1;
|
|
719
|
+
let best = Math.abs(term);
|
|
720
|
+
for (let n = 1; n < ASYMPTOTIC_MAX_TERMS; n++) {
|
|
721
|
+
term *= n / x;
|
|
722
|
+
const at = Math.abs(term);
|
|
723
|
+
if (at > best) {
|
|
724
|
+
break;
|
|
725
|
+
}
|
|
726
|
+
best = at;
|
|
727
|
+
sum += term;
|
|
728
|
+
if (at < Math.abs(sum) * SERIES_RELATIVE_TOLERANCE) {
|
|
729
|
+
break;
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
return (Math.exp(x) * sum) / x;
|
|
733
|
+
}
|
|
734
|
+
// Power series
|
|
735
|
+
let term = x;
|
|
736
|
+
let sum = term;
|
|
737
|
+
for (let k = 1; k < POWER_SERIES_MAX_TERMS; k++) {
|
|
738
|
+
term *= (x * k) / ((k + 1) * (k + 1));
|
|
739
|
+
sum += term;
|
|
740
|
+
if (Math.abs(term) < Math.abs(sum) * SERIES_RELATIVE_TOLERANCE) {
|
|
741
|
+
break;
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
return EULER_GAMMA + Math.log(Math.abs(x)) + sum;
|
|
745
|
+
}
|
|
746
|
+
function Li(x) {
|
|
747
|
+
const EXPONENTIAL_ASYMPTOTIC_CUTOFF = 40;
|
|
748
|
+
const ASYMPTOTIC_MAX_TERMS = 100;
|
|
749
|
+
const POWER_SERIES_MAX_TERMS = 500;
|
|
750
|
+
const SERIES_RELATIVE_TOLERANCE = 1e-16;
|
|
751
|
+
if (x <= 0) {
|
|
752
|
+
return NaN;
|
|
753
|
+
}
|
|
754
|
+
if (x === 1) {
|
|
755
|
+
return -Infinity;
|
|
756
|
+
}
|
|
757
|
+
const y = Math.log(x);
|
|
758
|
+
if (y === 0) {
|
|
759
|
+
return -Infinity;
|
|
760
|
+
}
|
|
761
|
+
// Inline Ei(y)
|
|
762
|
+
if (y > EXPONENTIAL_ASYMPTOTIC_CUTOFF || y < -EXPONENTIAL_ASYMPTOTIC_CUTOFF) {
|
|
763
|
+
let term = 1;
|
|
764
|
+
let sum = 1;
|
|
765
|
+
let best = Math.abs(term);
|
|
766
|
+
for (let n = 1; n < ASYMPTOTIC_MAX_TERMS; n++) {
|
|
767
|
+
term *= n / y;
|
|
768
|
+
const at = Math.abs(term);
|
|
769
|
+
if (at > best) {
|
|
770
|
+
break;
|
|
771
|
+
}
|
|
772
|
+
best = at;
|
|
773
|
+
sum += term;
|
|
774
|
+
if (at < Math.abs(sum) * SERIES_RELATIVE_TOLERANCE) {
|
|
775
|
+
break;
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
return (Math.exp(y) * sum) / y;
|
|
779
|
+
}
|
|
780
|
+
let term = y;
|
|
781
|
+
let sum = term;
|
|
782
|
+
for (let k = 1; k < POWER_SERIES_MAX_TERMS; k++) {
|
|
783
|
+
term *= (y * k) / ((k + 1) * (k + 1));
|
|
784
|
+
sum += term;
|
|
785
|
+
if (Math.abs(term) < Math.abs(sum) * SERIES_RELATIVE_TOLERANCE) {
|
|
786
|
+
break;
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
return EULER_GAMMA + Math.log(Math.abs(y)) + sum;
|
|
790
|
+
}
|