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,498 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.prodSubst = prodSubst;
|
|
4
|
+
exports.sumSubst = sumSubst;
|
|
5
|
+
exports.subst = subst;
|
|
6
|
+
exports.getU = getU;
|
|
7
|
+
exports.uSub = uSub;
|
|
8
|
+
exports.rewriteRadicals = rewriteRadicals;
|
|
9
|
+
exports.subRadicals = subRadicals;
|
|
10
|
+
exports.uUnSub = uUnSub;
|
|
11
|
+
exports.uSubConstants = uSubConstants;
|
|
12
|
+
exports.uSubFN = uSubFN;
|
|
13
|
+
exports.uSubEXP = uSubEXP;
|
|
14
|
+
const Expression_1 = require("../classes/expression/Expression");
|
|
15
|
+
const shortcuts_1 = require("../classes/expression/shortcuts");
|
|
16
|
+
const utils_1 = require("../classes/expression/utils");
|
|
17
|
+
const helpers_1 = require("../classes/parser/helpers");
|
|
18
|
+
const Settings_1 = require("../Settings");
|
|
19
|
+
function prodSubst(expression, value, withValue, includeNumeric) {
|
|
20
|
+
let retval;
|
|
21
|
+
/**
|
|
22
|
+
* The overview:
|
|
23
|
+
* We loop through the elements of each. The multiplier of the the power (meaning the power being substituted divided by the existing),
|
|
24
|
+
* has to match for each of the elements. Additionally, that ratio has to be an integer. If neither criteria is satisfied, then
|
|
25
|
+
* we exit and return the expression.
|
|
26
|
+
*/
|
|
27
|
+
if (expression.isProduct()) {
|
|
28
|
+
if (value.isProduct()) {
|
|
29
|
+
// First check the multipliers. This has to be an integer
|
|
30
|
+
const m = expression.getMultiplier().div(value.getMultiplier());
|
|
31
|
+
if (!m.isInteger() && !Settings_1.Settings.ALLOW_RAT_SUBS) {
|
|
32
|
+
retval = new Expression_1.Expression(expression);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
let power;
|
|
36
|
+
// Get the expression elements
|
|
37
|
+
let expressionElements = expression.elementsArray();
|
|
38
|
+
// Get the elements of the value being subbed.
|
|
39
|
+
const valueElements = value.elementsArray();
|
|
40
|
+
// We'll loop through the elements of the value to be subbed
|
|
41
|
+
// Create an array of the items not substituted
|
|
42
|
+
// A flag to keep track if a substitution occurred
|
|
43
|
+
let success = false;
|
|
44
|
+
// The array of the elements encountered
|
|
45
|
+
let filtered = [];
|
|
46
|
+
// Keep track of the number of element substituted
|
|
47
|
+
let remaining = valueElements.length;
|
|
48
|
+
while (valueElements.length > 0) {
|
|
49
|
+
// Reset the filtered array
|
|
50
|
+
filtered = [];
|
|
51
|
+
// Start with the assumption that no substitution occurred
|
|
52
|
+
success = false;
|
|
53
|
+
const vc = valueElements[valueElements.length - 1];
|
|
54
|
+
valueElements.pop();
|
|
55
|
+
for (const ec of expressionElements) {
|
|
56
|
+
// First find the value
|
|
57
|
+
if (ec.value === vc.value) {
|
|
58
|
+
// Then get the ratio of their powers
|
|
59
|
+
const pr = ec.getPower().div(vc.getPower());
|
|
60
|
+
// If the power ratio is undefined then we're at the first value. So just set the value to this value
|
|
61
|
+
if (power === undefined) {
|
|
62
|
+
power = pr;
|
|
63
|
+
}
|
|
64
|
+
// If the ratio of the powers is not an integer then we're done and exit or
|
|
65
|
+
// If the power ratios don't match then we're done. For instance if we're substituting x*y in x^2*y,
|
|
66
|
+
// the ratio will be (2, 1) and it's therefore not a valid substitution
|
|
67
|
+
if ((!pr.isInteger() || !pr.eq(power)) && !ec.isEXP()) {
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
success = true;
|
|
71
|
+
remaining--;
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
filtered.push(subst(ec, value, withValue, includeNumeric));
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
// If any substitution failed then we're done and exit
|
|
78
|
+
if (!success) {
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
// Update the list with the new one which doesn't have the substituted element
|
|
82
|
+
expressionElements = filtered;
|
|
83
|
+
}
|
|
84
|
+
// If all the elements weren't substituted then return the original expression
|
|
85
|
+
if (remaining !== 0 || power === undefined) {
|
|
86
|
+
retval = (0, utils_1.product)(...expression.elementsArray(true).map(x => {
|
|
87
|
+
return subst(x, value, withValue, includeNumeric);
|
|
88
|
+
})).pow(expression.getPower());
|
|
89
|
+
}
|
|
90
|
+
// Otherwise add the substituted value to the filtered set and return the sum product
|
|
91
|
+
else {
|
|
92
|
+
filtered.push(withValue.pow(power));
|
|
93
|
+
retval = (0, utils_1.product)(...filtered);
|
|
94
|
+
// Transfer the power and multiplier
|
|
95
|
+
retval.multiplier = retval.getMultiplier().times(m);
|
|
96
|
+
}
|
|
97
|
+
// Make substitutions in the power
|
|
98
|
+
retval = retval.pow(subst(expression.getPower(), value, withValue, includeNumeric));
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
// Convert it to an array, substitute out each element, convert it back to a product
|
|
103
|
+
// but make sure to put back the expression multiplier.
|
|
104
|
+
retval = (0, utils_1.product)(...expression.elementsArray(true).map(x => {
|
|
105
|
+
return subst(x, value, withValue, includeNumeric);
|
|
106
|
+
})).pow(expression.getPower());
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return retval || expression;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Performs a substitution on a sum
|
|
113
|
+
*
|
|
114
|
+
* @param expression
|
|
115
|
+
* @param value
|
|
116
|
+
* @param withValue
|
|
117
|
+
* @returns
|
|
118
|
+
*/
|
|
119
|
+
function sumSubst(expression, value, withValue, includeNumeric) {
|
|
120
|
+
let retval;
|
|
121
|
+
if (!expression.isSum()) {
|
|
122
|
+
return expression;
|
|
123
|
+
}
|
|
124
|
+
// Put back later (do NOT mutate fields directly)
|
|
125
|
+
const exprPow = expression.getPower();
|
|
126
|
+
const exprMul = expression.getMultiplier();
|
|
127
|
+
if (value.isSum()) {
|
|
128
|
+
// Work on raw elements first (do not subst yet)
|
|
129
|
+
const remaining = expression.elementsArray().slice();
|
|
130
|
+
// Decide how to treat the value being substituted
|
|
131
|
+
const valueElements = value.isSum() && value.isLinear() ? value.elementsArray() : [value.toUnitMultiplier()];
|
|
132
|
+
const matched = [];
|
|
133
|
+
let m;
|
|
134
|
+
for (const vc of valueElements) {
|
|
135
|
+
// Find exactly one matching term in the remaining pool
|
|
136
|
+
const idx = remaining.findIndex(ec => ec.value === vc.value && ec.getPower().eq(vc.getPower()));
|
|
137
|
+
if (idx === -1) {
|
|
138
|
+
// Fail: cannot substitute, so just recurse normally on the whole sum
|
|
139
|
+
const out = (0, utils_1.sum)(...expression
|
|
140
|
+
.elementsArray()
|
|
141
|
+
.map(x => subst(x, value, withValue, includeNumeric)));
|
|
142
|
+
return out.pow(exprPow).times(exprMul);
|
|
143
|
+
}
|
|
144
|
+
const ec = remaining[idx];
|
|
145
|
+
remaining.splice(idx, 1);
|
|
146
|
+
matched.push(ec);
|
|
147
|
+
const tm = ec.div(vc);
|
|
148
|
+
if (!m) {
|
|
149
|
+
m = tm;
|
|
150
|
+
}
|
|
151
|
+
else if (!tm.eq(m)) {
|
|
152
|
+
// Ratio mismatch: fail -> normal recursion
|
|
153
|
+
const out = (0, utils_1.sum)(...expression
|
|
154
|
+
.elementsArray()
|
|
155
|
+
.map(x => subst(x, value, withValue, includeNumeric)));
|
|
156
|
+
return out.pow(exprPow).times(exprMul);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
// Success: substitute the whole group by withValue * m,
|
|
160
|
+
// and recurse into the remaining terms (important!)
|
|
161
|
+
const substituted = withValue.times(m || (0, shortcuts_1.one)());
|
|
162
|
+
const rewrittenTerms = remaining.map(t => subst(t, value, withValue, includeNumeric));
|
|
163
|
+
rewrittenTerms.push(substituted);
|
|
164
|
+
retval = (0, utils_1.sum)(...rewrittenTerms)
|
|
165
|
+
.pow(exprPow)
|
|
166
|
+
.times(exprMul);
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
// Original behavior for non-sum value: recurse per term
|
|
170
|
+
retval = (0, utils_1.sum)(...expression.elementsArray().map(x => subst(x, value, withValue, includeNumeric)))
|
|
171
|
+
.pow(exprPow)
|
|
172
|
+
.times(exprMul);
|
|
173
|
+
}
|
|
174
|
+
return retval || expression;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Replaces a value with another using the existing Expression tree.
|
|
178
|
+
*
|
|
179
|
+
* @param expression The expression being used to perform the substitution
|
|
180
|
+
* @param value The value currently in the expression
|
|
181
|
+
* @param withValue The value that the substitution is being replaced with
|
|
182
|
+
*/
|
|
183
|
+
function subst(expression, value, withValue, includeNumeric = false) {
|
|
184
|
+
expression = Expression_1.Expression.create(expression);
|
|
185
|
+
value = Expression_1.Expression.create(value);
|
|
186
|
+
withValue = Expression_1.Expression.create(withValue);
|
|
187
|
+
let retval;
|
|
188
|
+
// If the value to be substituted is a sum or product but the value does not contain
|
|
189
|
+
// any then we're done. Also, we don't consider a number a proper LHS value.
|
|
190
|
+
if (expression.isNUM() && !includeNumeric) {
|
|
191
|
+
retval = expression;
|
|
192
|
+
}
|
|
193
|
+
// We next move to the most obvious substitution which is the expression is equal to the value
|
|
194
|
+
else if (expression.eq(value)) {
|
|
195
|
+
retval = withValue;
|
|
196
|
+
}
|
|
197
|
+
// A variable can carry its coefficient and numeric power on the node itself. Replace
|
|
198
|
+
// that base directly so a simple substitution stays within the existing expression tree.
|
|
199
|
+
else if (value.isVAR() &&
|
|
200
|
+
value.getPower().isOne() &&
|
|
201
|
+
value.getMultiplier().isOne() &&
|
|
202
|
+
expression.isVAR() &&
|
|
203
|
+
expression.value === value.value) {
|
|
204
|
+
const exponent = subst(expression.getPower(), value, withValue, includeNumeric);
|
|
205
|
+
const multiplier = expression.getMultiplier();
|
|
206
|
+
if (exponent.isNUM() && exponent.getMultiplier().isNegative()) {
|
|
207
|
+
retval = (0, shortcuts_1.one)().div(withValue.copy().pow(exponent.abs()));
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
retval = withValue.copy().pow(exponent);
|
|
211
|
+
}
|
|
212
|
+
if (!multiplier.isOne()) {
|
|
213
|
+
retval = retval.times(multiplier);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
else if (expression.isProduct()) {
|
|
217
|
+
retval = prodSubst(expression, value, withValue, includeNumeric);
|
|
218
|
+
}
|
|
219
|
+
// The power has to be one because we cannot swap terms otherwise
|
|
220
|
+
// TODO: SUM with nested GRP
|
|
221
|
+
else if (expression.isSum()) {
|
|
222
|
+
// Perform a simple sum factor on the expression so we can detect the t+1 in expression like a*t+a.
|
|
223
|
+
// This should not affect a substitution for the a*t since products are checked first.
|
|
224
|
+
// expression = sumFactor(expression);
|
|
225
|
+
// If it was factored then it belongs to the product substitution
|
|
226
|
+
retval = sumSubst(expression, value, withValue, includeNumeric);
|
|
227
|
+
}
|
|
228
|
+
else if (expression.isFunction()) {
|
|
229
|
+
if (expression.value === value.value) {
|
|
230
|
+
retval = new Expression_1.Expression(withValue);
|
|
231
|
+
}
|
|
232
|
+
else {
|
|
233
|
+
retval = Expression_1.Expression.Function(expression.name);
|
|
234
|
+
retval.args = expression.getArguments().map((x) => {
|
|
235
|
+
return subst(x, value, withValue, includeNumeric);
|
|
236
|
+
});
|
|
237
|
+
// Update the value to reflect the new argument(s)
|
|
238
|
+
retval.updateValue();
|
|
239
|
+
}
|
|
240
|
+
retval = retval.pow(expression.getPower()).times(expression.getMultiplier());
|
|
241
|
+
}
|
|
242
|
+
else if (expression.isEXP()) {
|
|
243
|
+
const base = subst(expression.getBase(), value, withValue, true);
|
|
244
|
+
const exp = subst(expression.getPower(), value, withValue, includeNumeric);
|
|
245
|
+
retval = base.pow(exp).times(expression.getMultiplier());
|
|
246
|
+
}
|
|
247
|
+
return retval || expression;
|
|
248
|
+
}
|
|
249
|
+
function getU(x, map) {
|
|
250
|
+
x = Expression_1.Expression.create(x);
|
|
251
|
+
const variables = x.variables();
|
|
252
|
+
map ??= {};
|
|
253
|
+
let count = 0;
|
|
254
|
+
let u;
|
|
255
|
+
do {
|
|
256
|
+
u = `u${count}`;
|
|
257
|
+
count++;
|
|
258
|
+
} while (variables.includes(u) || u in map);
|
|
259
|
+
return u;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Replaces one repeated sub-expression with a generated `uN` symbol.
|
|
263
|
+
*
|
|
264
|
+
* The generated symbol is chosen so it does not collide with variables already present
|
|
265
|
+
* in the expression or substitution map. Pass the returned map to later calls when
|
|
266
|
+
* several substitutions should share one namespace, then use {@link uUnSub} to restore
|
|
267
|
+
* the original expressions.
|
|
268
|
+
*
|
|
269
|
+
* @param x - Expression in which the substitution will occur.
|
|
270
|
+
* @param value - Sub-expression to replace.
|
|
271
|
+
* @param map - Existing substitution map to extend.
|
|
272
|
+
* @returns The substituted expression and the updated substitution map.
|
|
273
|
+
*
|
|
274
|
+
* @example
|
|
275
|
+
* ```ts
|
|
276
|
+
* const [substituted, map] = uSub('cos(x)^2+cos(x)+1', 'cos(x)');
|
|
277
|
+
* substituted.text({ sort: true }); // "u0^2+u0+1"
|
|
278
|
+
* uUnSub(substituted, map).eq('cos(x)^2+cos(x)+1'); // true
|
|
279
|
+
* ```
|
|
280
|
+
*/
|
|
281
|
+
function uSub(x, value, map) {
|
|
282
|
+
// Create the map object is one wasn't provided
|
|
283
|
+
map ??= {};
|
|
284
|
+
x = Expression_1.Expression.create(x);
|
|
285
|
+
value = Expression_1.Expression.create(value);
|
|
286
|
+
// Get a suitable u
|
|
287
|
+
const u = getU(x, map);
|
|
288
|
+
// Perform the substitution
|
|
289
|
+
const subbed = subst(x, value, (0, helpers_1._)(u));
|
|
290
|
+
// Mark it as updated
|
|
291
|
+
if (!subbed.eq(x)) {
|
|
292
|
+
map[u] = value;
|
|
293
|
+
}
|
|
294
|
+
return [subbed, map];
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Rewrites powers of one variable through a radical substitution without
|
|
298
|
+
* sending the replacement back through generic nested-power simplification.
|
|
299
|
+
* The caller supplies the substitution degree, so fractional powers become
|
|
300
|
+
* exact integer powers of the replacement variable when possible.
|
|
301
|
+
*/
|
|
302
|
+
function rewriteRadicals(e, variable, newVar, n) {
|
|
303
|
+
let retval = e;
|
|
304
|
+
if ((e.isVAR() || e.isEXP()) && e.value === variable) {
|
|
305
|
+
const m = e.getMultiplier();
|
|
306
|
+
const p = e.getPower();
|
|
307
|
+
if (p.isNUM()) {
|
|
308
|
+
const r = p.getMultiplier();
|
|
309
|
+
const newNum = n * r.numerator;
|
|
310
|
+
const newDen = r.denominator < 0n ? -r.denominator : r.denominator;
|
|
311
|
+
if (newNum % newDen !== 0n) {
|
|
312
|
+
retval = Expression_1.Expression.fromRational(m).times(newVar.pow(`${newNum}/${newDen}`));
|
|
313
|
+
}
|
|
314
|
+
else {
|
|
315
|
+
const intPower = newNum / newDen;
|
|
316
|
+
retval = Expression_1.Expression.fromRational(m).times(newVar.pow(intPower.toString()));
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
else {
|
|
320
|
+
retval = Expression_1.Expression.fromRational(m).times(newVar.pow(p.times(n.toString())));
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
else if (e.isEXP()) {
|
|
324
|
+
const base = rewriteRadicals(e.getBase(), variable, newVar, n);
|
|
325
|
+
const power = rewriteRadicals(e.getPower(), variable, newVar, n);
|
|
326
|
+
retval = Expression_1.Expression.toEXP(base, power);
|
|
327
|
+
if (!e.getMultiplier().isOne()) {
|
|
328
|
+
retval = retval.times(Expression_1.Expression.fromRational(e.getMultiplier()));
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
else if (e.isSum()) {
|
|
332
|
+
const m = e.getMultiplier();
|
|
333
|
+
const p = e.getPower();
|
|
334
|
+
retval = (0, shortcuts_1.zero)();
|
|
335
|
+
for (const term of e.elementsArray()) {
|
|
336
|
+
retval = retval.plus(rewriteRadicals(term, variable, newVar, n));
|
|
337
|
+
}
|
|
338
|
+
if (!p.isOne()) {
|
|
339
|
+
retval = retval.pow(p);
|
|
340
|
+
}
|
|
341
|
+
if (!m.isOne()) {
|
|
342
|
+
retval = retval.times(Expression_1.Expression.fromRational(m));
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
else if (e.isProduct()) {
|
|
346
|
+
const m = e.getMultiplier();
|
|
347
|
+
const p = e.getPower();
|
|
348
|
+
retval = (0, shortcuts_1.one)();
|
|
349
|
+
for (const element of e.elementsArray()) {
|
|
350
|
+
retval = retval.times(rewriteRadicals(element, variable, newVar, n));
|
|
351
|
+
}
|
|
352
|
+
if (!p.isOne()) {
|
|
353
|
+
retval = retval.pow(p);
|
|
354
|
+
}
|
|
355
|
+
if (!m.isOne()) {
|
|
356
|
+
retval = retval.times(Expression_1.Expression.fromRational(m));
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
else if (e.isFunction()) {
|
|
360
|
+
const args = e.getArguments();
|
|
361
|
+
if (args.length > 0) {
|
|
362
|
+
const newArgs = args.map(a => rewriteRadicals(a, variable, newVar, n));
|
|
363
|
+
retval = Expression_1.Expression.toFunction(e.name, newArgs);
|
|
364
|
+
retval.multiplier = e.getMultiplier().copy();
|
|
365
|
+
retval = Expression_1.Expression.setPower(retval, e.getPower());
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
return retval;
|
|
369
|
+
}
|
|
370
|
+
function subRadicals(x) {
|
|
371
|
+
let num = x.getNumerator();
|
|
372
|
+
let den = x.getDenominator();
|
|
373
|
+
const subs = {};
|
|
374
|
+
// The map of uSubstitutions
|
|
375
|
+
const map = {};
|
|
376
|
+
// A record to track which variables already map to which u
|
|
377
|
+
const references = {};
|
|
378
|
+
function collectPowers(exp) {
|
|
379
|
+
const elements = exp.elementsArray();
|
|
380
|
+
// We're going to loop through the elements and look for radicals
|
|
381
|
+
// Like variables are just going to get their combined power so
|
|
382
|
+
// a^(1/n)+a^(1/m) will just be mapped to a: n*m
|
|
383
|
+
for (const e of elements) {
|
|
384
|
+
if (e.hasRadical()) {
|
|
385
|
+
// Don't touch numbers.
|
|
386
|
+
if (e.isNUM()) {
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
const n = e.getPower().getDenominator();
|
|
390
|
+
const v = e.value;
|
|
391
|
+
const existing = subs[v];
|
|
392
|
+
// Multiply the power on the existing if one already exists
|
|
393
|
+
subs[v] = existing ? subs[v].times(n) : n;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
function makeSubstitutions(exp) {
|
|
398
|
+
// Make the substitutions
|
|
399
|
+
for (const y in subs) {
|
|
400
|
+
const u = references[y] ? references[y] : getU(exp, map);
|
|
401
|
+
// Map the u to the variable
|
|
402
|
+
references[y] = u;
|
|
403
|
+
const pow = subs[y];
|
|
404
|
+
// Make the substitution
|
|
405
|
+
const n = pow.getMultiplier().numerator;
|
|
406
|
+
map[u] = Expression_1.Expression.create(y).pow(pow.invert());
|
|
407
|
+
exp = rewriteRadicals(exp, y, Expression_1.Expression.create(u), n);
|
|
408
|
+
}
|
|
409
|
+
return exp;
|
|
410
|
+
}
|
|
411
|
+
collectPowers(num);
|
|
412
|
+
collectPowers(den);
|
|
413
|
+
num = makeSubstitutions(num);
|
|
414
|
+
den = makeSubstitutions(den);
|
|
415
|
+
return [num.div(den), map];
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Restores substitutions produced by {@link uSub}.
|
|
419
|
+
*
|
|
420
|
+
* @param expression - Expression containing generated substitution symbols.
|
|
421
|
+
* @param map - Map returned by one or more u-substitution calls.
|
|
422
|
+
* @returns The expression after restoring every mapped value.
|
|
423
|
+
*/
|
|
424
|
+
function uUnSub(expression, map) {
|
|
425
|
+
// Substitute back in all the elements. Remember that the order is not preserved.
|
|
426
|
+
for (const u in map) {
|
|
427
|
+
const value = map[u];
|
|
428
|
+
expression = subst(expression, (0, helpers_1._)(u), value);
|
|
429
|
+
}
|
|
430
|
+
return expression;
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* Performs a u-substitution of all the constants in the expression.
|
|
434
|
+
*
|
|
435
|
+
* @param expression
|
|
436
|
+
* @param map
|
|
437
|
+
* @returns
|
|
438
|
+
*/
|
|
439
|
+
function uSubConstants(expression, map) {
|
|
440
|
+
map ??= {};
|
|
441
|
+
const constants = new Set();
|
|
442
|
+
// forEveryElement returns immediately for a VAR root. Check the root first so standalone
|
|
443
|
+
// mathematical constants and their powers are promoted just like the same constants nested
|
|
444
|
+
// inside sums and products.
|
|
445
|
+
if (expression.isVAR() && (expression.isConstant() || expression.isI())) {
|
|
446
|
+
constants.add(expression.value);
|
|
447
|
+
}
|
|
448
|
+
expression.forEveryElement(e => {
|
|
449
|
+
// The imaginary unit is a mathematical constant, but isConstant() only recognizes parser
|
|
450
|
+
// constants such as pi and e. Promote i here as well so polynomial factorization does not
|
|
451
|
+
// treat it as an ordinary polynomial variable.
|
|
452
|
+
if (e.isVAR() && (e.isConstant() || e.isI())) {
|
|
453
|
+
constants.add(e.value);
|
|
454
|
+
}
|
|
455
|
+
return e;
|
|
456
|
+
});
|
|
457
|
+
for (const c of constants) {
|
|
458
|
+
[expression, map] = uSub(expression, (0, helpers_1._)(c), map);
|
|
459
|
+
}
|
|
460
|
+
return [expression, map];
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Performs a u-substitution on all the functions in the expression
|
|
464
|
+
*
|
|
465
|
+
* @param expression
|
|
466
|
+
* @param map
|
|
467
|
+
* @returns
|
|
468
|
+
*/
|
|
469
|
+
function uSubFN(expression, map) {
|
|
470
|
+
// Create the map object if none was provided
|
|
471
|
+
map ??= {};
|
|
472
|
+
// We get all the functions in the expression. Next we'll u-substitute each one.
|
|
473
|
+
expression.functions(undefined, true).forEach(fn => {
|
|
474
|
+
[expression, map] = uSub(expression, (0, helpers_1._)(fn), map);
|
|
475
|
+
});
|
|
476
|
+
return [expression, map];
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
* Performs a u-substitution on all the exponential functions in the expression
|
|
480
|
+
* @param expression
|
|
481
|
+
* @param map
|
|
482
|
+
* @returns
|
|
483
|
+
*/
|
|
484
|
+
function uSubEXP(expression, map) {
|
|
485
|
+
map ??= {};
|
|
486
|
+
const elementSet = new Set();
|
|
487
|
+
// Create a list of all the EXP function
|
|
488
|
+
expression.forEveryElement(e => {
|
|
489
|
+
if (e.isEXP()) {
|
|
490
|
+
elementSet.add(e.toUnitMultiplier().text());
|
|
491
|
+
}
|
|
492
|
+
return e;
|
|
493
|
+
});
|
|
494
|
+
for (const x of elementSet) {
|
|
495
|
+
[expression, map] = uSub(expression, (0, helpers_1._)(x), map);
|
|
496
|
+
}
|
|
497
|
+
return [expression, map];
|
|
498
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Removes duplicates from an array
|
|
3
|
+
*
|
|
4
|
+
* @param arr
|
|
5
|
+
* @returns
|
|
6
|
+
*/
|
|
7
|
+
export declare function arrayUnique<T>(arr: T[]): T[];
|
|
8
|
+
/**
|
|
9
|
+
* Similar to concat but only adds elements if they're unique
|
|
10
|
+
*
|
|
11
|
+
* @param fromArray The array being added from
|
|
12
|
+
* @param toArray The array being added to
|
|
13
|
+
*/
|
|
14
|
+
export declare function arrayAddUnique<T>(fromArray: T[], toArray: T[]): T[];
|
|
15
|
+
/**
|
|
16
|
+
* Checks to see if the inverse of decimal is within a max bound.
|
|
17
|
+
* For example: given the number 0.01 it will return true for 0 - 100
|
|
18
|
+
*
|
|
19
|
+
* @param n
|
|
20
|
+
* @param max
|
|
21
|
+
* @returns
|
|
22
|
+
*/
|
|
23
|
+
export declare function checkDecimal(n: number, max: number): boolean;
|
|
24
|
+
export declare function isSorted<T>(arr: T[]): boolean;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.arrayUnique = arrayUnique;
|
|
4
|
+
exports.arrayAddUnique = arrayAddUnique;
|
|
5
|
+
exports.checkDecimal = checkDecimal;
|
|
6
|
+
exports.isSorted = isSorted;
|
|
7
|
+
/**
|
|
8
|
+
* Removes duplicates from an array
|
|
9
|
+
*
|
|
10
|
+
* @param arr
|
|
11
|
+
* @returns
|
|
12
|
+
*/
|
|
13
|
+
function arrayUnique(arr) {
|
|
14
|
+
return [...new Set(arr)];
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Similar to concat but only adds elements if they're unique
|
|
18
|
+
*
|
|
19
|
+
* @param fromArray The array being added from
|
|
20
|
+
* @param toArray The array being added to
|
|
21
|
+
*/
|
|
22
|
+
function arrayAddUnique(fromArray, toArray) {
|
|
23
|
+
for (const x of fromArray) {
|
|
24
|
+
if (!toArray.includes(x)) {
|
|
25
|
+
toArray.push(x);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return toArray;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Checks to see if the inverse of decimal is within a max bound.
|
|
32
|
+
* For example: given the number 0.01 it will return true for 0 - 100
|
|
33
|
+
*
|
|
34
|
+
* @param n
|
|
35
|
+
* @param max
|
|
36
|
+
* @returns
|
|
37
|
+
*/
|
|
38
|
+
function checkDecimal(n, max) {
|
|
39
|
+
return 1 / (n % 1) <= max;
|
|
40
|
+
}
|
|
41
|
+
function isSorted(arr) {
|
|
42
|
+
if (arr.length <= 1) {
|
|
43
|
+
return true; // An empty array or an array with one element is considered sorted
|
|
44
|
+
}
|
|
45
|
+
for (let i = 1; i < arr.length; i++) {
|
|
46
|
+
if (arr[i - 1] > arr[i]) {
|
|
47
|
+
return false; // If any element is smaller than the previous one, the array is not sorted
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return true; // If the loop completes without returning false, the array is sorted
|
|
51
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { Collection } from './classes/collection/Collection';
|
|
2
|
+
import type { Dictionary } from './classes/dictionary/Dictionary';
|
|
3
|
+
import type { Equation } from './classes/equation/Equation';
|
|
4
|
+
import type { Expression } from './classes/expression/Expression';
|
|
5
|
+
import type { Matrix } from './classes/matrix/Matrix';
|
|
6
|
+
import type { Rational } from './classes/rational/Rational';
|
|
7
|
+
import type { ValuesSet } from './classes/valuesSet/ValuesSet';
|
|
8
|
+
import type { Vector } from './classes/vector/Vector';
|
|
9
|
+
import type Decimal from 'decimal.js';
|
|
10
|
+
/**
|
|
11
|
+
* Values accepted by Nerdamer's expression-oriented parsing APIs.
|
|
12
|
+
*
|
|
13
|
+
* @remarks
|
|
14
|
+
* Strings are interpreted as parser notation. Primitive numbers, `bigint`, and
|
|
15
|
+
* `Decimal` instances are converted through their textual form; {@link Rational}
|
|
16
|
+
* values are converted directly, and existing {@link Expression} values retain their
|
|
17
|
+
* symbolic structure.
|
|
18
|
+
* An existing expression may be reused by identity by APIs such as
|
|
19
|
+
* {@link Expression.create}; request a copy at the receiving API when independent
|
|
20
|
+
* mutation is required.
|
|
21
|
+
*/
|
|
22
|
+
export type ExpressionInput = number | string | Rational | Expression | bigint | Decimal;
|
|
23
|
+
/**
|
|
24
|
+
* Values accepted across Nerdamer's root API and direct object model.
|
|
25
|
+
*
|
|
26
|
+
* This extends {@link ExpressionInput} with the structured parser entities and
|
|
27
|
+
* equations that can be passed back into supported operations. Individual functions
|
|
28
|
+
* may accept a narrower subset and should document that restriction locally.
|
|
29
|
+
*/
|
|
30
|
+
export type NerdamerInput = ExpressionInput | Vector | ValuesSet | Collection | Matrix | Dictionary | Equation;
|
|
31
|
+
/**
|
|
32
|
+
* Values that can be produced by Nerdamer's parser.
|
|
33
|
+
*
|
|
34
|
+
* @remarks
|
|
35
|
+
* Equality syntax produces an {@link Equation}; bracket, set, matrix, and dictionary
|
|
36
|
+
* syntax can produce the corresponding structured entity instead of an
|
|
37
|
+
* {@link Expression}. Callers of the general parser should therefore narrow this union
|
|
38
|
+
* before using expression-specific methods.
|
|
39
|
+
*/
|
|
40
|
+
export type ParserEntity = Expression | Vector | ValuesSet | Collection | Matrix | Equation | Dictionary;
|
|
41
|
+
/**
|
|
42
|
+
* Structured parser entities that participate in aggregate operations.
|
|
43
|
+
*
|
|
44
|
+
* The union describes Nerdamer's supported matrix, vector, ordered collection, and
|
|
45
|
+
* keyed dictionary containers. It excludes value sets and equations, whose semantics
|
|
46
|
+
* are not ordinary elementwise structured operations.
|
|
47
|
+
*/
|
|
48
|
+
export type StructuredEntityType = Matrix | Vector | Collection | Dictionary;
|