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,11 @@
|
|
|
1
|
+
import { Expression } from '../classes/expression/Expression';
|
|
2
|
+
/**
|
|
3
|
+
* Rewrites exact ratios to a common denominator without invoking algebra-level
|
|
4
|
+
* factoring or simplification.
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* Core comparison uses this normalization to determine whether an exact symbolic
|
|
8
|
+
* difference is zero. Algebra also reuses the same implementation for rational
|
|
9
|
+
* simplification, keeping the dependency direction from algebra to core.
|
|
10
|
+
*/
|
|
11
|
+
export declare function toCommonDenominator(x: Expression): Expression;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toCommonDenominator = toCommonDenominator;
|
|
4
|
+
const Expression_1 = require("../classes/expression/Expression");
|
|
5
|
+
const shortcuts_1 = require("../classes/expression/shortcuts");
|
|
6
|
+
/**
|
|
7
|
+
* Groups additive terms that already share the same symbolic denominator.
|
|
8
|
+
*
|
|
9
|
+
* This is part of core exact normalization rather than the higher-level algebra
|
|
10
|
+
* simplifier because symbolic comparison relies on it for zero/equality checks.
|
|
11
|
+
*/
|
|
12
|
+
function groupByDenominator(x) {
|
|
13
|
+
let retval = x;
|
|
14
|
+
if (x.isSum()) {
|
|
15
|
+
let nonRationals = (0, shortcuts_1.zero)();
|
|
16
|
+
const rationals = {};
|
|
17
|
+
x.each(e => {
|
|
18
|
+
const num = e.getNumerator();
|
|
19
|
+
const den = e.getDenominator();
|
|
20
|
+
// Numerator extraction is exact, so restore the term's decimal marker
|
|
21
|
+
// before regrouping it. Otherwise a decimal coefficient silently becomes
|
|
22
|
+
// an exact rational during common-denominator normalization.
|
|
23
|
+
if (e.getMultiplier().asDecimal) {
|
|
24
|
+
num.getMultiplier().asDecimal = true;
|
|
25
|
+
}
|
|
26
|
+
if (den.isOne()) {
|
|
27
|
+
nonRationals = nonRationals.plus(num);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
const key = den.text();
|
|
31
|
+
rationals[key] = rationals[key] ? rationals[key].plus(num) : num;
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
retval = nonRationals;
|
|
35
|
+
for (const denominator in rationals) {
|
|
36
|
+
retval = retval.plus(rationals[denominator].div(Expression_1.Expression.create(denominator)));
|
|
37
|
+
}
|
|
38
|
+
retval = retval.pow(x.getPower()).times(x.getMultiplier());
|
|
39
|
+
}
|
|
40
|
+
return retval;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Rewrites exact ratios to a common denominator without invoking algebra-level
|
|
44
|
+
* factoring or simplification.
|
|
45
|
+
*
|
|
46
|
+
* @remarks
|
|
47
|
+
* Core comparison uses this normalization to determine whether an exact symbolic
|
|
48
|
+
* difference is zero. Algebra also reuses the same implementation for rational
|
|
49
|
+
* simplification, keeping the dependency direction from algebra to core.
|
|
50
|
+
*/
|
|
51
|
+
function toCommonDenominator(x) {
|
|
52
|
+
let retval = x;
|
|
53
|
+
if (x.isProduct()) {
|
|
54
|
+
retval = Expression_1.Expression.fromRational(x.getMultiplier());
|
|
55
|
+
x.each(e => {
|
|
56
|
+
e = e.pow(x.getPower());
|
|
57
|
+
if (e.isSum()) {
|
|
58
|
+
retval = retval.times(toCommonDenominator(e));
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
retval = retval.times(e);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
else if (x.isSum()) {
|
|
66
|
+
x = groupByDenominator(x);
|
|
67
|
+
if (!x.isSum()) {
|
|
68
|
+
return x;
|
|
69
|
+
}
|
|
70
|
+
const p = x.getPower().abs();
|
|
71
|
+
const m = x.getMultiplier();
|
|
72
|
+
const terms = x.elementsArray();
|
|
73
|
+
let hasNonTrivialDenominator = false;
|
|
74
|
+
let hasSymbolicDenominator = false;
|
|
75
|
+
for (const e of terms) {
|
|
76
|
+
const den = e.getDenominator();
|
|
77
|
+
if (!den.isOne()) {
|
|
78
|
+
hasNonTrivialDenominator = true;
|
|
79
|
+
if (!den.isNUM()) {
|
|
80
|
+
hasSymbolicDenominator = true;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (!hasNonTrivialDenominator) {
|
|
85
|
+
return x;
|
|
86
|
+
}
|
|
87
|
+
if (!hasSymbolicDenominator && !x.isPolynomialLike()) {
|
|
88
|
+
return x;
|
|
89
|
+
}
|
|
90
|
+
let numerator = (0, shortcuts_1.zero)();
|
|
91
|
+
let denominator = (0, shortcuts_1.one)();
|
|
92
|
+
for (const e of terms) {
|
|
93
|
+
const normalizedTerm = toCommonDenominator(e);
|
|
94
|
+
const num = normalizedTerm.getNumerator();
|
|
95
|
+
const den = normalizedTerm.getDenominator();
|
|
96
|
+
// getNumerator() returns the exact rational numerator, so carry the
|
|
97
|
+
// original term's decimal intent onto that numerator before combining
|
|
98
|
+
// fractions. This keeps decimal contagion intact through the rewrite.
|
|
99
|
+
if (e.getMultiplier().asDecimal) {
|
|
100
|
+
num.getMultiplier().asDecimal = true;
|
|
101
|
+
}
|
|
102
|
+
numerator = numerator.times(den).plus(denominator.times(num));
|
|
103
|
+
denominator = denominator.times(den);
|
|
104
|
+
}
|
|
105
|
+
if (x.getPower().sign() === -1) {
|
|
106
|
+
[denominator, numerator] = [numerator, denominator];
|
|
107
|
+
}
|
|
108
|
+
retval = numerator.div(denominator);
|
|
109
|
+
if (!p.isOne()) {
|
|
110
|
+
retval = retval.pow(p);
|
|
111
|
+
}
|
|
112
|
+
if (!m.isOne()) {
|
|
113
|
+
retval = retval.times(Expression_1.Expression.fromRational(m));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return retval;
|
|
117
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ExpressionFunction, JsFunction } from '../classes/parser/types';
|
|
2
|
+
import type { RegisteredMathFunction } from '../dispatch';
|
|
3
|
+
interface SetFunctionBaseParams {
|
|
4
|
+
name: string;
|
|
5
|
+
minArgs?: number;
|
|
6
|
+
maxArgs?: number;
|
|
7
|
+
}
|
|
8
|
+
export interface SetJSFunctionParams extends SetFunctionBaseParams {
|
|
9
|
+
type: 'js';
|
|
10
|
+
fn: JsFunction;
|
|
11
|
+
}
|
|
12
|
+
export interface SetSymbolicFunctionParams extends SetFunctionBaseParams {
|
|
13
|
+
type: 'symbolic';
|
|
14
|
+
fn: string | RegisteredMathFunction | ExpressionFunction;
|
|
15
|
+
argsOrder?: string[];
|
|
16
|
+
}
|
|
17
|
+
export type SetFunctionParams = SetJSFunctionParams | SetSymbolicFunctionParams;
|
|
18
|
+
export declare function setFunction(params: SetFunctionParams): void;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.setFunction = setFunction;
|
|
4
|
+
const Expression_1 = require("../classes/expression/Expression");
|
|
5
|
+
const functions_1 = require("../classes/parser/scripting/functions");
|
|
6
|
+
const dispatch_1 = require("../dispatch");
|
|
7
|
+
const errors_1 = require("../errors");
|
|
8
|
+
/**
|
|
9
|
+
* This is essentially a wrapper around the user provided JS function. It checks each input
|
|
10
|
+
* to ensure that it can be converted to a number. If all inputs are numbers, then the
|
|
11
|
+
* function is called, otherwise a symbolic function is returned.
|
|
12
|
+
*
|
|
13
|
+
* @param name
|
|
14
|
+
* @param fn
|
|
15
|
+
* @returns
|
|
16
|
+
*/
|
|
17
|
+
function jsWrapper(name, fn) {
|
|
18
|
+
return (...expressions) => {
|
|
19
|
+
const args = [];
|
|
20
|
+
for (const e of expressions) {
|
|
21
|
+
if (e.isNUM()) {
|
|
22
|
+
args.push(Number(e.getMultiplier().toDecimal()));
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
return Expression_1.Expression.toFunction(name, expressions);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return Expression_1.Expression.create(Number(fn(...args)));
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function entry(minArgs, maxArgs, fn, maxPrecision) {
|
|
32
|
+
return {
|
|
33
|
+
minArgs,
|
|
34
|
+
maxArgs,
|
|
35
|
+
fn,
|
|
36
|
+
maxPrecision,
|
|
37
|
+
usage: 'notation',
|
|
38
|
+
level: 'user',
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function setFunction(params) {
|
|
42
|
+
const existing = dispatch_1.mathFunctionRegistry[params.name];
|
|
43
|
+
if (existing?.level === 'system') {
|
|
44
|
+
throw new errors_1.AssignmentError((0, errors_1.message)('restrictedVariableName', { name: params.name }));
|
|
45
|
+
}
|
|
46
|
+
if (params.type === 'js') {
|
|
47
|
+
const name = params.name;
|
|
48
|
+
if (typeof params.fn !== 'function') {
|
|
49
|
+
throw new errors_1.UnexpectedInputError((0, errors_1.message)('setJSFunctionExpectsFunction'));
|
|
50
|
+
}
|
|
51
|
+
if (!params.minArgs || !params.maxArgs) {
|
|
52
|
+
throw new errors_1.UnexpectedInputError((0, errors_1.message)('functionRequiresMinAndMaxArgs'));
|
|
53
|
+
}
|
|
54
|
+
dispatch_1.mathFunctionRegistry[name] = entry(params.minArgs, params.maxArgs, jsWrapper(name, params.fn), 17);
|
|
55
|
+
}
|
|
56
|
+
else if (params.type === 'symbolic') {
|
|
57
|
+
// Check if the user provided a string
|
|
58
|
+
if (typeof params.fn === 'string') {
|
|
59
|
+
// Only parse the body when argument order must be inferred. Parsing a body with
|
|
60
|
+
// control flow should not execute that control flow during registration.
|
|
61
|
+
const args = params.argsOrder ?? Expression_1.Expression.create(params.fn).variables().sort();
|
|
62
|
+
dispatch_1.mathFunctionRegistry[params.name] = entry(
|
|
63
|
+
// If the user didn't provide a min and max arg number then default to the args length
|
|
64
|
+
params.minArgs ?? args.length, params.maxArgs ?? args.length, (0, functions_1.wrappedFunction)(params.fn, args));
|
|
65
|
+
}
|
|
66
|
+
// This one is the simplest one of them all. Just set the function and be done
|
|
67
|
+
else {
|
|
68
|
+
const name = params.name;
|
|
69
|
+
if (params.minArgs === undefined || params.maxArgs === undefined) {
|
|
70
|
+
throw new errors_1.UnexpectedInputError((0, errors_1.message)('functionRequiresMinAndMaxArgs'));
|
|
71
|
+
}
|
|
72
|
+
dispatch_1.mathFunctionRegistry[name] = entry(params.minArgs, params.maxArgs, params.fn);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a string from scientific notation form to decimal form
|
|
3
|
+
*
|
|
4
|
+
* @param str
|
|
5
|
+
* @returns
|
|
6
|
+
*/
|
|
7
|
+
export declare function scientificToDecimal(str: string): string;
|
|
8
|
+
/**
|
|
9
|
+
* Splits at the decimal but ensures that array length is always 2
|
|
10
|
+
*
|
|
11
|
+
* @param str
|
|
12
|
+
* @returns
|
|
13
|
+
*/
|
|
14
|
+
export declare function decimalSplit(str: string): string[];
|
|
15
|
+
/**
|
|
16
|
+
* A helper function to replace multiple occurrences in a string. Tag the replacement
|
|
17
|
+
* with a double curly.
|
|
18
|
+
* @example format('Hi. My name is {{name}}.', {name: 'John Doe'})
|
|
19
|
+
*
|
|
20
|
+
* @param str
|
|
21
|
+
* @param replacements
|
|
22
|
+
* @returns
|
|
23
|
+
*/
|
|
24
|
+
export declare function format(str: string, replacements: {
|
|
25
|
+
[replacement: string]: string;
|
|
26
|
+
} | string[]): string;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.scientificToDecimal = scientificToDecimal;
|
|
4
|
+
exports.decimalSplit = decimalSplit;
|
|
5
|
+
exports.format = format;
|
|
6
|
+
/**
|
|
7
|
+
* Removes the minus sign from the beginning of the string
|
|
8
|
+
*
|
|
9
|
+
* @param str
|
|
10
|
+
* @returns an array with the first item as true if a minus
|
|
11
|
+
* was found and the string minus the minus sign.
|
|
12
|
+
*/
|
|
13
|
+
function stripSign(str) {
|
|
14
|
+
// Check if it has a minus sign
|
|
15
|
+
const hasMinus = str.charAt(0) === '-';
|
|
16
|
+
// Remove it if it does
|
|
17
|
+
if (hasMinus || str.charAt(0) === '+') {
|
|
18
|
+
str = str.substring(1);
|
|
19
|
+
}
|
|
20
|
+
return [hasMinus, str];
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Converts a string from scientific notation form to decimal form
|
|
24
|
+
*
|
|
25
|
+
* @param str
|
|
26
|
+
* @returns
|
|
27
|
+
*/
|
|
28
|
+
function scientificToDecimal(str) {
|
|
29
|
+
let isNegative;
|
|
30
|
+
// Remove the sign by slicing the string
|
|
31
|
+
[isNegative, str] = stripSign(str);
|
|
32
|
+
// Split it into coefficient and exponent
|
|
33
|
+
const [c, e] = str.toLowerCase().split('e');
|
|
34
|
+
// Split the coefficient into the whole and decimal portion
|
|
35
|
+
// eslint-disable-next-line prefer-const
|
|
36
|
+
let [w, d] = c.split('.');
|
|
37
|
+
// Provide and empty sting for safety if in the form n(e)n
|
|
38
|
+
d = d || '';
|
|
39
|
+
// The total length of the string
|
|
40
|
+
const length = w.length + (d ? d.length : 0);
|
|
41
|
+
// The total string minus the dot
|
|
42
|
+
const numString = w + d;
|
|
43
|
+
// If it's small then we need to calculate the leading zeros
|
|
44
|
+
// The shift of the decimal place to the left
|
|
45
|
+
const dotLocation = w.length + Number(e);
|
|
46
|
+
// Is the dot needed or not
|
|
47
|
+
const dot = dotLocation === length ? '' : '.';
|
|
48
|
+
let value;
|
|
49
|
+
if (dotLocation <= 0) {
|
|
50
|
+
// Join the value but pad after the dot with zeroes
|
|
51
|
+
value = `0${dot}${'0'.repeat(Math.abs(dotLocation))}${numString}`;
|
|
52
|
+
}
|
|
53
|
+
else if (dotLocation > length) {
|
|
54
|
+
value = `${numString}${'0'.repeat(Math.abs(dotLocation - length))}`;
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
value = `${numString.substring(0, dotLocation)}${dot}${numString.substring(dotLocation)}`;
|
|
58
|
+
}
|
|
59
|
+
return isNegative ? '-' + value : value;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Splits at the decimal but ensures that array length is always 2
|
|
63
|
+
*
|
|
64
|
+
* @param str
|
|
65
|
+
* @returns
|
|
66
|
+
*/
|
|
67
|
+
function decimalSplit(str) {
|
|
68
|
+
const parts = str.split('.');
|
|
69
|
+
if (parts.length > 2) {
|
|
70
|
+
throw new SyntaxError(`Invalid decimal value: ${str}`);
|
|
71
|
+
}
|
|
72
|
+
if (parts.length === 1) {
|
|
73
|
+
parts.push('');
|
|
74
|
+
}
|
|
75
|
+
return parts;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* A helper function to replace multiple occurrences in a string. Tag the replacement
|
|
79
|
+
* with a double curly.
|
|
80
|
+
* @example format('Hi. My name is {{name}}.', {name: 'John Doe'})
|
|
81
|
+
*
|
|
82
|
+
* @param str
|
|
83
|
+
* @param replacements
|
|
84
|
+
* @returns
|
|
85
|
+
*/
|
|
86
|
+
function format(str, replacements) {
|
|
87
|
+
const replace = function (str, replacement, newValue) {
|
|
88
|
+
return str.replace(new RegExp(`{{${replacement}}}`, 'g'), newValue);
|
|
89
|
+
};
|
|
90
|
+
if (Array.isArray(replacements)) {
|
|
91
|
+
for (let i = 0; i < replacements.length; i++) {
|
|
92
|
+
str = replace(str, String(i), replacements[i]);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
for (const x in replacements) {
|
|
97
|
+
str = replace(str, x, replacements[x]);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return str;
|
|
101
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { Expression } from '../classes/expression/Expression';
|
|
2
|
+
import type { ExpressionInput } from '../types';
|
|
3
|
+
/** Maps generated substitution symbols such as `u0` back to their original expressions. */
|
|
4
|
+
export type SubstitutionMap = Record<string, Expression>;
|
|
5
|
+
/** Result returned by {@link uSub}. */
|
|
6
|
+
export type USubstitutionResult = [
|
|
7
|
+
expression: Expression,
|
|
8
|
+
substitutions: SubstitutionMap
|
|
9
|
+
];
|
|
10
|
+
/** @deprecated Use {@link SubstitutionMap}. */
|
|
11
|
+
export type MapObjectType = SubstitutionMap;
|
|
12
|
+
export declare function prodSubst(expression: Expression, value: Expression, withValue: Expression, includeNumeric?: boolean): Expression;
|
|
13
|
+
/**
|
|
14
|
+
* Performs a substitution on a sum
|
|
15
|
+
*
|
|
16
|
+
* @param expression
|
|
17
|
+
* @param value
|
|
18
|
+
* @param withValue
|
|
19
|
+
* @returns
|
|
20
|
+
*/
|
|
21
|
+
export declare function sumSubst(expression: Expression, value: Expression, withValue: Expression, includeNumeric?: boolean): Expression;
|
|
22
|
+
/**
|
|
23
|
+
* Replaces a value with another using the existing Expression tree.
|
|
24
|
+
*
|
|
25
|
+
* @param expression The expression being used to perform the substitution
|
|
26
|
+
* @param value The value currently in the expression
|
|
27
|
+
* @param withValue The value that the substitution is being replaced with
|
|
28
|
+
*/
|
|
29
|
+
export declare function subst(expression: ExpressionInput, value: ExpressionInput, withValue: ExpressionInput, includeNumeric?: boolean): Expression;
|
|
30
|
+
export declare function getU(x: ExpressionInput, map?: MapObjectType): string;
|
|
31
|
+
/**
|
|
32
|
+
* Replaces one repeated sub-expression with a generated `uN` symbol.
|
|
33
|
+
*
|
|
34
|
+
* The generated symbol is chosen so it does not collide with variables already present
|
|
35
|
+
* in the expression or substitution map. Pass the returned map to later calls when
|
|
36
|
+
* several substitutions should share one namespace, then use {@link uUnSub} to restore
|
|
37
|
+
* the original expressions.
|
|
38
|
+
*
|
|
39
|
+
* @param x - Expression in which the substitution will occur.
|
|
40
|
+
* @param value - Sub-expression to replace.
|
|
41
|
+
* @param map - Existing substitution map to extend.
|
|
42
|
+
* @returns The substituted expression and the updated substitution map.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* const [substituted, map] = uSub('cos(x)^2+cos(x)+1', 'cos(x)');
|
|
47
|
+
* substituted.text({ sort: true }); // "u0^2+u0+1"
|
|
48
|
+
* uUnSub(substituted, map).eq('cos(x)^2+cos(x)+1'); // true
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export declare function uSub(x: ExpressionInput, value: ExpressionInput, map?: SubstitutionMap): USubstitutionResult;
|
|
52
|
+
/**
|
|
53
|
+
* Rewrites powers of one variable through a radical substitution without
|
|
54
|
+
* sending the replacement back through generic nested-power simplification.
|
|
55
|
+
* The caller supplies the substitution degree, so fractional powers become
|
|
56
|
+
* exact integer powers of the replacement variable when possible.
|
|
57
|
+
*/
|
|
58
|
+
export declare function rewriteRadicals(e: Expression, variable: string, newVar: Expression, n: bigint): Expression;
|
|
59
|
+
export declare function subRadicals(x: Expression): [Expression, MapObjectType];
|
|
60
|
+
/**
|
|
61
|
+
* Restores substitutions produced by {@link uSub}.
|
|
62
|
+
*
|
|
63
|
+
* @param expression - Expression containing generated substitution symbols.
|
|
64
|
+
* @param map - Map returned by one or more u-substitution calls.
|
|
65
|
+
* @returns The expression after restoring every mapped value.
|
|
66
|
+
*/
|
|
67
|
+
export declare function uUnSub(expression: Expression, map: SubstitutionMap): Expression;
|
|
68
|
+
/**
|
|
69
|
+
* Performs a u-substitution of all the constants in the expression.
|
|
70
|
+
*
|
|
71
|
+
* @param expression
|
|
72
|
+
* @param map
|
|
73
|
+
* @returns
|
|
74
|
+
*/
|
|
75
|
+
export declare function uSubConstants(expression: Expression, map?: MapObjectType): [Expression, MapObjectType];
|
|
76
|
+
/**
|
|
77
|
+
* Performs a u-substitution on all the functions in the expression
|
|
78
|
+
*
|
|
79
|
+
* @param expression
|
|
80
|
+
* @param map
|
|
81
|
+
* @returns
|
|
82
|
+
*/
|
|
83
|
+
export declare function uSubFN(expression: Expression, map?: MapObjectType): [Expression, MapObjectType];
|
|
84
|
+
/**
|
|
85
|
+
* Performs a u-substitution on all the exponential functions in the expression
|
|
86
|
+
* @param expression
|
|
87
|
+
* @param map
|
|
88
|
+
* @returns
|
|
89
|
+
*/
|
|
90
|
+
export declare function uSubEXP(expression: Expression, map?: MapObjectType): [Expression, MapObjectType];
|