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,292 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Adaptive Simpson definite integration using native `number` arithmetic.
|
|
4
|
+
*
|
|
5
|
+
* Same algorithm as the Decimal.js version (adaptive Simpson with Richardson
|
|
6
|
+
* extrapolation, iterative stack) but 50-100x faster for typical integrands.
|
|
7
|
+
*
|
|
8
|
+
* Features:
|
|
9
|
+
* - Adaptive error control with combined absolute + relative tolerance
|
|
10
|
+
* - Breakpoint segmentation for piecewise/abs/kink functions
|
|
11
|
+
* - Infinite bounds via variable substitution
|
|
12
|
+
* - Best-effort singularity handling
|
|
13
|
+
*/
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.definiteIntegrateNative = definiteIntegrateNative;
|
|
16
|
+
const DEFAULT_TOL = 1e-12;
|
|
17
|
+
const DEFAULT_MAX_DEPTH = 50;
|
|
18
|
+
const DEFAULT_MAX_EVALUATIONS = 100_000;
|
|
19
|
+
const MAX_SINGULAR_TRIES = 8;
|
|
20
|
+
const INFINITE_BOUND_ENDPOINT_EPSILON = 1e-15;
|
|
21
|
+
// ── Helpers ──────────────────────────────────────────────────────────
|
|
22
|
+
function simpson(a, b, fa, fm, fb) {
|
|
23
|
+
return ((b - a) / 6) * (fa + 4 * fm + fb);
|
|
24
|
+
}
|
|
25
|
+
// ── Core: finite-bounds adaptive Simpson ─────────────────────────────
|
|
26
|
+
function withFiniteBounds(f, aIn, bIn, tolAbs, tolRel, maxDepth, maxEvals, singular) {
|
|
27
|
+
let a = aIn;
|
|
28
|
+
let b = bIn;
|
|
29
|
+
if (a === b) {
|
|
30
|
+
return 0;
|
|
31
|
+
}
|
|
32
|
+
let sign = 1;
|
|
33
|
+
if (b < a) {
|
|
34
|
+
sign = -1;
|
|
35
|
+
const t = a;
|
|
36
|
+
a = b;
|
|
37
|
+
b = t;
|
|
38
|
+
}
|
|
39
|
+
let evals = 0;
|
|
40
|
+
const evalF = (x) => {
|
|
41
|
+
if (++evals > maxEvals) {
|
|
42
|
+
throw new Error(`defint: maxEvals exceeded (${maxEvals}).`);
|
|
43
|
+
}
|
|
44
|
+
const y = f(x);
|
|
45
|
+
if (singular === 'strict' && !isFinite(y)) {
|
|
46
|
+
throw new Error(`defint: non-finite f(x) at x=${x}`);
|
|
47
|
+
}
|
|
48
|
+
return y;
|
|
49
|
+
};
|
|
50
|
+
const m0 = (a + b) / 2;
|
|
51
|
+
const fa = evalF(a);
|
|
52
|
+
const fm = evalF(m0);
|
|
53
|
+
const fb = evalF(b);
|
|
54
|
+
if (singular === 'ignore' && (isNaN(fa) || isNaN(fm) || isNaN(fb))) {
|
|
55
|
+
return NaN;
|
|
56
|
+
}
|
|
57
|
+
// Auto-split if initial evaluations hit non-finite values
|
|
58
|
+
if (singular === 'auto' && (!isFinite(fa) || !isFinite(fm) || !isFinite(fb))) {
|
|
59
|
+
const leftEndpointSingular = !isFinite(fa) && isFinite(fm) && isFinite(fb);
|
|
60
|
+
const rightEndpointSingular = isFinite(fa) && isFinite(fm) && !isFinite(fb);
|
|
61
|
+
if (leftEndpointSingular || rightEndpointSingular) {
|
|
62
|
+
const span = b - a;
|
|
63
|
+
const endpointProbe = Math.sqrt(Number.EPSILON);
|
|
64
|
+
const singularAtLeft = leftEndpointSingular;
|
|
65
|
+
// A quadratic substitution removes the common integrable endpoint singularities
|
|
66
|
+
// without evaluating the original function at the singular endpoint. Clamping the
|
|
67
|
+
// transformed endpoint inward also avoids roundoff mapping tiny t values back to it.
|
|
68
|
+
const transformed = t => {
|
|
69
|
+
const u = Math.max(t, endpointProbe);
|
|
70
|
+
const x = singularAtLeft ? a + span * u * u : b - span * u * u;
|
|
71
|
+
const y = f(x) * 2 * span * u;
|
|
72
|
+
if (!isFinite(y)) {
|
|
73
|
+
throw new Error(`defint: endpoint singularity could not be regularized near x=${x}`);
|
|
74
|
+
}
|
|
75
|
+
return y;
|
|
76
|
+
};
|
|
77
|
+
return (sign *
|
|
78
|
+
withFiniteBounds(transformed, 0, 1, tolAbs, tolRel, maxDepth, maxEvals, singular));
|
|
79
|
+
}
|
|
80
|
+
return sign * autoSplitFinite(f, a, b, tolAbs, tolRel, maxDepth, maxEvals, singular);
|
|
81
|
+
}
|
|
82
|
+
const S0 = simpson(a, b, fa, fm, fb);
|
|
83
|
+
const stack = [
|
|
84
|
+
{
|
|
85
|
+
a,
|
|
86
|
+
b,
|
|
87
|
+
m: m0,
|
|
88
|
+
fa,
|
|
89
|
+
fm,
|
|
90
|
+
fb,
|
|
91
|
+
S: S0,
|
|
92
|
+
depth: 0,
|
|
93
|
+
tolAbs,
|
|
94
|
+
tolRel,
|
|
95
|
+
singularTries: 0,
|
|
96
|
+
},
|
|
97
|
+
];
|
|
98
|
+
let acc = 0;
|
|
99
|
+
while (stack.length > 0) {
|
|
100
|
+
const fr = stack[stack.length - 1];
|
|
101
|
+
stack.pop();
|
|
102
|
+
const { a: a0, b: b0, m } = fr;
|
|
103
|
+
// Absolute tolerance is budgeted across child intervals. Relative tolerance stays
|
|
104
|
+
// relative to each child estimate rather than tightening again at every depth.
|
|
105
|
+
const childTolRel = fr.tolRel;
|
|
106
|
+
const lm = (a0 + m) / 2;
|
|
107
|
+
const rm = (m + b0) / 2;
|
|
108
|
+
const fLM = evalF(lm);
|
|
109
|
+
const fRM = evalF(rm);
|
|
110
|
+
if (singular === 'ignore' && (isNaN(fLM) || isNaN(fRM))) {
|
|
111
|
+
return NaN;
|
|
112
|
+
}
|
|
113
|
+
// Auto: bisect around non-finite values
|
|
114
|
+
if (singular === 'auto' &&
|
|
115
|
+
(!isFinite(fLM) ||
|
|
116
|
+
!isFinite(fRM) ||
|
|
117
|
+
!isFinite(fr.fa) ||
|
|
118
|
+
!isFinite(fr.fm) ||
|
|
119
|
+
!isFinite(fr.fb))) {
|
|
120
|
+
if (fr.depth >= maxDepth || fr.singularTries >= MAX_SINGULAR_TRIES) {
|
|
121
|
+
throw new Error(`defint: singular/non-finite encountered near [${a0}, ${b0}]`);
|
|
122
|
+
}
|
|
123
|
+
const nextDepth = fr.depth + 1;
|
|
124
|
+
const childTolAbs = fr.tolAbs / 2;
|
|
125
|
+
stack.push({
|
|
126
|
+
a: m,
|
|
127
|
+
b: b0,
|
|
128
|
+
m: rm,
|
|
129
|
+
fa: fr.fm,
|
|
130
|
+
fm: fRM,
|
|
131
|
+
fb: fr.fb,
|
|
132
|
+
S: simpson(m, b0, fr.fm, fRM, fr.fb),
|
|
133
|
+
depth: nextDepth,
|
|
134
|
+
tolAbs: childTolAbs,
|
|
135
|
+
tolRel: childTolRel,
|
|
136
|
+
singularTries: fr.singularTries + 1,
|
|
137
|
+
});
|
|
138
|
+
stack.push({
|
|
139
|
+
a: a0,
|
|
140
|
+
b: m,
|
|
141
|
+
m: lm,
|
|
142
|
+
fa: fr.fa,
|
|
143
|
+
fm: fLM,
|
|
144
|
+
fb: fr.fm,
|
|
145
|
+
S: simpson(a0, m, fr.fa, fLM, fr.fm),
|
|
146
|
+
depth: nextDepth,
|
|
147
|
+
tolAbs: childTolAbs,
|
|
148
|
+
tolRel: childTolRel,
|
|
149
|
+
singularTries: fr.singularTries + 1,
|
|
150
|
+
});
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
const Sleft = simpson(a0, m, fr.fa, fLM, fr.fm);
|
|
154
|
+
const Sright = simpson(m, b0, fr.fm, fRM, fr.fb);
|
|
155
|
+
const S2 = Sleft + Sright;
|
|
156
|
+
const err = Math.abs(S2 - fr.S) / 15;
|
|
157
|
+
const scale = fr.tolAbs + fr.tolRel * Math.abs(S2);
|
|
158
|
+
if (err <= scale || fr.depth >= maxDepth) {
|
|
159
|
+
// Richardson extrapolation
|
|
160
|
+
acc += S2 + (S2 - fr.S) / 15;
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
const nextDepth = fr.depth + 1;
|
|
164
|
+
const childTolAbs = fr.tolAbs / 2;
|
|
165
|
+
// Push right then left so left is processed first (LIFO)
|
|
166
|
+
stack.push({
|
|
167
|
+
a: m,
|
|
168
|
+
b: b0,
|
|
169
|
+
m: rm,
|
|
170
|
+
fa: fr.fm,
|
|
171
|
+
fm: fRM,
|
|
172
|
+
fb: fr.fb,
|
|
173
|
+
S: Sright,
|
|
174
|
+
depth: nextDepth,
|
|
175
|
+
tolAbs: childTolAbs,
|
|
176
|
+
tolRel: childTolRel,
|
|
177
|
+
singularTries: fr.singularTries,
|
|
178
|
+
});
|
|
179
|
+
stack.push({
|
|
180
|
+
a: a0,
|
|
181
|
+
b: m,
|
|
182
|
+
m: lm,
|
|
183
|
+
fa: fr.fa,
|
|
184
|
+
fm: fLM,
|
|
185
|
+
fb: fr.fm,
|
|
186
|
+
S: Sleft,
|
|
187
|
+
depth: nextDepth,
|
|
188
|
+
tolAbs: childTolAbs,
|
|
189
|
+
tolRel: childTolRel,
|
|
190
|
+
singularTries: fr.singularTries,
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
return acc * sign;
|
|
194
|
+
}
|
|
195
|
+
// ── Auto-split helper for singular endpoints ─────────────────────────
|
|
196
|
+
function autoSplitFinite(f, a, b, tolAbs, tolRel, maxDepth, maxEvals, singular) {
|
|
197
|
+
const m = (a + b) / 2;
|
|
198
|
+
const q1 = (a + m) / 2;
|
|
199
|
+
const q3 = (m + b) / 2;
|
|
200
|
+
const segTol = tolAbs / 4;
|
|
201
|
+
return (withFiniteBounds(f, a, q1, segTol, tolRel, maxDepth, maxEvals, singular) +
|
|
202
|
+
withFiniteBounds(f, q1, m, segTol, tolRel, maxDepth, maxEvals, singular) +
|
|
203
|
+
withFiniteBounds(f, m, q3, segTol, tolRel, maxDepth, maxEvals, singular) +
|
|
204
|
+
withFiniteBounds(f, q3, b, segTol, tolRel, maxDepth, maxEvals, singular));
|
|
205
|
+
}
|
|
206
|
+
// ── Public API ───────────────────────────────────────────────────────
|
|
207
|
+
/**
|
|
208
|
+
* Definite integral of `f` from `a` to `b` using adaptive Simpson's rule
|
|
209
|
+
* with Richardson extrapolation, native `number` arithmetic.
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* ```ts
|
|
213
|
+
* // ∫₀^π sin(x) dx = 2
|
|
214
|
+
* definiteIntegrate(Math.sin, 0, Math.PI);
|
|
215
|
+
*
|
|
216
|
+
* // With breakpoints for |x|
|
|
217
|
+
* definiteIntegrate(x => Math.abs(x), -1, 1, { breakpoints: [0] });
|
|
218
|
+
*
|
|
219
|
+
* // Improper integral ∫₁^∞ 1/x² dx = 1
|
|
220
|
+
* definiteIntegrate(x => 1/(x*x), 1, Infinity, { allowInfinite: true });
|
|
221
|
+
* ```
|
|
222
|
+
*/
|
|
223
|
+
function definiteIntegrateNative(f, a, b, options = {}) {
|
|
224
|
+
if (a === b) {
|
|
225
|
+
return 0;
|
|
226
|
+
}
|
|
227
|
+
const tolAbs = options.tolAbs ?? DEFAULT_TOL;
|
|
228
|
+
const tolRel = options.tolRel ?? DEFAULT_TOL;
|
|
229
|
+
const maxDepth = options.maxDepth ?? DEFAULT_MAX_DEPTH;
|
|
230
|
+
const maxEvals = options.maxEvals ?? DEFAULT_MAX_EVALUATIONS;
|
|
231
|
+
const singular = options.singular ?? 'auto';
|
|
232
|
+
const allowInfinite = options.allowInfinite ?? false;
|
|
233
|
+
const isInfA = !isFinite(a) && !isNaN(a);
|
|
234
|
+
const isInfB = !isFinite(b) && !isNaN(b);
|
|
235
|
+
if ((isInfA || isInfB) && !allowInfinite) {
|
|
236
|
+
throw new Error('defint: infinite bounds require allowInfinite: true');
|
|
237
|
+
}
|
|
238
|
+
// ── Infinite-bound substitutions ─────────────────────────────
|
|
239
|
+
if (allowInfinite && (isInfA || isInfB)) {
|
|
240
|
+
// Guard against very small eps clamping to 0 in float64
|
|
241
|
+
const eps = INFINITE_BOUND_ENDPOINT_EPSILON;
|
|
242
|
+
if (!isInfA && isInfB && b > 0) {
|
|
243
|
+
// ∫_a^∞ → t ∈ [0, 1-eps], x = a + t/(1-t), dx = 1/(1-t)²
|
|
244
|
+
const a0 = a;
|
|
245
|
+
const g = t => {
|
|
246
|
+
const u = 1 - t;
|
|
247
|
+
const x = a0 + t / u;
|
|
248
|
+
return f(x) / (u * u);
|
|
249
|
+
};
|
|
250
|
+
return withFiniteBounds(g, 0, 1 - eps, tolAbs, tolRel, maxDepth, maxEvals, singular);
|
|
251
|
+
}
|
|
252
|
+
if (isInfA && a < 0 && !isInfB) {
|
|
253
|
+
// ∫_{-∞}^b → t ∈ [0, 1-eps], x = b - t/(1-t)
|
|
254
|
+
const b0 = b;
|
|
255
|
+
const g = t => {
|
|
256
|
+
const u = 1 - t;
|
|
257
|
+
const x = b0 - t / u;
|
|
258
|
+
return f(x) / (u * u);
|
|
259
|
+
};
|
|
260
|
+
return withFiniteBounds(g, 0, 1 - eps, tolAbs, tolRel, maxDepth, maxEvals, singular);
|
|
261
|
+
}
|
|
262
|
+
if (isInfA && a < 0 && isInfB && b > 0) {
|
|
263
|
+
// ∫_{-∞}^{∞} → t ∈ [eps, 1-eps], x = tan(π(t - ½))
|
|
264
|
+
const g = t => {
|
|
265
|
+
const u = Math.PI * (t - 0.5);
|
|
266
|
+
const cosU = Math.cos(u);
|
|
267
|
+
// dx/dt = π / cos²(u)
|
|
268
|
+
return (f(Math.tan(u)) * Math.PI) / (cosU * cosU);
|
|
269
|
+
};
|
|
270
|
+
return withFiniteBounds(g, eps, 1 - eps, tolAbs, tolRel, maxDepth, maxEvals, singular);
|
|
271
|
+
}
|
|
272
|
+
throw new Error('defint: unsupported infinite bound configuration');
|
|
273
|
+
}
|
|
274
|
+
// ── Finite bounds with optional breakpoints ──────────────────
|
|
275
|
+
const lo = Math.min(a, b);
|
|
276
|
+
const hi = Math.max(a, b);
|
|
277
|
+
const bps = (options.breakpoints ?? []).filter(x => x > lo && x < hi).sort((x, y) => x - y);
|
|
278
|
+
if (bps.length === 0) {
|
|
279
|
+
return withFiniteBounds(f, a, b, tolAbs, tolRel, maxDepth, maxEvals, singular);
|
|
280
|
+
}
|
|
281
|
+
// Segment integration with tolerance budget split across segments
|
|
282
|
+
const nSeg = bps.length + 1;
|
|
283
|
+
const segTolAbs = tolAbs / nSeg;
|
|
284
|
+
// Determine integration direction
|
|
285
|
+
const forward = a <= b;
|
|
286
|
+
const points = forward ? [a, ...bps, b] : [a, ...bps.reverse(), b];
|
|
287
|
+
let total = 0;
|
|
288
|
+
for (let i = 0; i < points.length - 1; i++) {
|
|
289
|
+
total += withFiniteBounds(f, points[i], points[i + 1], segTolAbs, tolRel, maxDepth, maxEvals, singular);
|
|
290
|
+
}
|
|
291
|
+
return total;
|
|
292
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Expression } from '../core/classes/expression/Expression';
|
|
2
|
+
/**
|
|
3
|
+
* Returns the symbolic hypotenuse of two expressions.
|
|
4
|
+
*
|
|
5
|
+
* Matching linear sine and cosine terms collapse through the Pythagorean identity;
|
|
6
|
+
* otherwise the result is the principal square root of the sum of squares.
|
|
7
|
+
*/
|
|
8
|
+
export declare function hypot(a: Expression, b: Expression): Expression;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.hypot = hypot;
|
|
4
|
+
const Expression_1 = require("../core/classes/expression/Expression");
|
|
5
|
+
const shortcuts_1 = require("../core/classes/expression/shortcuts");
|
|
6
|
+
const constants_1 = require("../core/classes/parser/constants");
|
|
7
|
+
const add_1 = require("../core/classes/parser/operations/add");
|
|
8
|
+
const power_1 = require("../core/classes/parser/operations/power");
|
|
9
|
+
/**
|
|
10
|
+
* Returns the symbolic hypotenuse of two expressions.
|
|
11
|
+
*
|
|
12
|
+
* Matching linear sine and cosine terms collapse through the Pythagorean identity;
|
|
13
|
+
* otherwise the result is the principal square root of the sum of squares.
|
|
14
|
+
*/
|
|
15
|
+
function hypot(a, b) {
|
|
16
|
+
const fns = [constants_1.SIN, constants_1.COS];
|
|
17
|
+
let retval;
|
|
18
|
+
if (a.isFunction(fns) &&
|
|
19
|
+
b.isFunction(fns) &&
|
|
20
|
+
a.name !== b.name &&
|
|
21
|
+
a.isLinear() &&
|
|
22
|
+
b.isLinear() &&
|
|
23
|
+
a.getMultiplier().eq(b.getMultiplier())) {
|
|
24
|
+
retval = Expression_1.Expression.fromRational(a.getMultiplier());
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
const aSquared = (0, power_1.power)(a, (0, shortcuts_1.two)());
|
|
28
|
+
const bSquared = (0, power_1.power)(b, (0, shortcuts_1.two)());
|
|
29
|
+
retval = (0, power_1.power)((0, add_1.add)(aSquared, bSquared), (0, shortcuts_1.half)());
|
|
30
|
+
}
|
|
31
|
+
return retval;
|
|
32
|
+
}
|