mathjs 10.1.1 → 10.2.0
Sign up to get free protection for your applications and to get access to all the features.
- package/HISTORY.md +17 -0
- package/docs/expressions/algebra.md +27 -0
- package/docs/reference/functions/abs.md +6 -0
- package/docs/reference/functions/acos.md +6 -0
- package/docs/reference/functions/acosh.md +6 -0
- package/docs/reference/functions/acot.md +6 -0
- package/docs/reference/functions/acoth.md +6 -0
- package/docs/reference/functions/acsc.md +6 -0
- package/docs/reference/functions/acsch.md +6 -0
- package/docs/reference/functions/add.md +6 -0
- package/docs/reference/functions/and.md +6 -0
- package/docs/reference/functions/apply.md +6 -0
- package/docs/reference/functions/arg.md +6 -0
- package/docs/reference/functions/asec.md +6 -0
- package/docs/reference/functions/asech.md +6 -0
- package/docs/reference/functions/asin.md +6 -0
- package/docs/reference/functions/asinh.md +6 -0
- package/docs/reference/functions/atan.md +6 -0
- package/docs/reference/functions/atan2.md +6 -0
- package/docs/reference/functions/atanh.md +6 -0
- package/docs/reference/functions/bellNumbers.md +6 -0
- package/docs/reference/functions/bin.md +6 -0
- package/docs/reference/functions/bitAnd.md +6 -0
- package/docs/reference/functions/bitNot.md +6 -0
- package/docs/reference/functions/bitOr.md +6 -0
- package/docs/reference/functions/bitXor.md +6 -0
- package/docs/reference/functions/catalan.md +6 -0
- package/docs/reference/functions/cbrt.md +6 -0
- package/docs/reference/functions/ceil.md +6 -0
- package/docs/reference/functions/clone.md +6 -0
- package/docs/reference/functions/column.md +6 -0
- package/docs/reference/functions/combinations.md +6 -0
- package/docs/reference/functions/combinationsWithRep.md +6 -0
- package/docs/reference/functions/compare.md +6 -0
- package/docs/reference/functions/compareNatural.md +6 -0
- package/docs/reference/functions/compareText.md +6 -0
- package/docs/reference/functions/compile.md +6 -0
- package/docs/reference/functions/composition.md +6 -0
- package/docs/reference/functions/concat.md +6 -0
- package/docs/reference/functions/conj.md +6 -0
- package/docs/reference/functions/cos.md +6 -0
- package/docs/reference/functions/cosh.md +6 -0
- package/docs/reference/functions/cot.md +6 -0
- package/docs/reference/functions/coth.md +6 -0
- package/docs/reference/functions/count.md +6 -0
- package/docs/reference/functions/cross.md +6 -0
- package/docs/reference/functions/csc.md +6 -0
- package/docs/reference/functions/csch.md +6 -0
- package/docs/reference/functions/ctranspose.md +6 -0
- package/docs/reference/functions/cube.md +6 -0
- package/docs/reference/functions/deepEqual.md +6 -0
- package/docs/reference/functions/derivative.md +6 -0
- package/docs/reference/functions/det.md +6 -0
- package/docs/reference/functions/diag.md +6 -0
- package/docs/reference/functions/diff.md +6 -0
- package/docs/reference/functions/distance.md +6 -0
- package/docs/reference/functions/divide.md +6 -0
- package/docs/reference/functions/dot.md +6 -0
- package/docs/reference/functions/dotDivide.md +6 -0
- package/docs/reference/functions/dotMultiply.md +6 -0
- package/docs/reference/functions/dotPow.md +6 -0
- package/docs/reference/functions/eigs.md +6 -0
- package/docs/reference/functions/equal.md +6 -0
- package/docs/reference/functions/equalText.md +6 -0
- package/docs/reference/functions/erf.md +6 -0
- package/docs/reference/functions/evaluate.md +6 -0
- package/docs/reference/functions/exp.md +6 -0
- package/docs/reference/functions/expm.md +6 -0
- package/docs/reference/functions/expm1.md +6 -0
- package/docs/reference/functions/factorial.md +6 -0
- package/docs/reference/functions/filter.md +6 -0
- package/docs/reference/functions/fix.md +6 -0
- package/docs/reference/functions/flatten.md +6 -0
- package/docs/reference/functions/floor.md +6 -0
- package/docs/reference/functions/forEach.md +6 -0
- package/docs/reference/functions/format.md +6 -0
- package/docs/reference/functions/gamma.md +6 -0
- package/docs/reference/functions/gcd.md +6 -0
- package/docs/reference/functions/getMatrixDataType.md +6 -0
- package/docs/reference/functions/hasNumericValue.md +6 -0
- package/docs/reference/functions/help.md +6 -0
- package/docs/reference/functions/hex.md +6 -0
- package/docs/reference/functions/hypot.md +6 -0
- package/docs/reference/functions/identity.md +6 -0
- package/docs/reference/functions/im.md +6 -0
- package/docs/reference/functions/intersect.md +6 -0
- package/docs/reference/functions/inv.md +6 -0
- package/docs/reference/functions/invmod.md +6 -0
- package/docs/reference/functions/isInteger.md +6 -0
- package/docs/reference/functions/isNaN.md +6 -0
- package/docs/reference/functions/isNegative.md +6 -0
- package/docs/reference/functions/isNumeric.md +6 -0
- package/docs/reference/functions/isPositive.md +6 -0
- package/docs/reference/functions/isPrime.md +6 -0
- package/docs/reference/functions/isZero.md +6 -0
- package/docs/reference/functions/kldivergence.md +6 -0
- package/docs/reference/functions/kron.md +6 -0
- package/docs/reference/functions/larger.md +6 -0
- package/docs/reference/functions/largerEq.md +6 -0
- package/docs/reference/functions/lcm.md +6 -0
- package/docs/reference/functions/leafCount.md +52 -0
- package/docs/reference/functions/leftShift.md +6 -0
- package/docs/reference/functions/log.md +6 -0
- package/docs/reference/functions/log10.md +6 -0
- package/docs/reference/functions/log1p.md +6 -0
- package/docs/reference/functions/log2.md +6 -0
- package/docs/reference/functions/lsolve.md +6 -0
- package/docs/reference/functions/lsolveAll.md +6 -0
- package/docs/reference/functions/lup.md +6 -0
- package/docs/reference/functions/lusolve.md +6 -0
- package/docs/reference/functions/mad.md +6 -0
- package/docs/reference/functions/map.md +6 -0
- package/docs/reference/functions/matrixFromColumns.md +6 -0
- package/docs/reference/functions/matrixFromFunction.md +6 -0
- package/docs/reference/functions/matrixFromRows.md +6 -0
- package/docs/reference/functions/max.md +6 -0
- package/docs/reference/functions/mean.md +6 -0
- package/docs/reference/functions/median.md +6 -0
- package/docs/reference/functions/min.md +6 -0
- package/docs/reference/functions/mod.md +6 -0
- package/docs/reference/functions/mode.md +6 -0
- package/docs/reference/functions/multinomial.md +6 -0
- package/docs/reference/functions/multiply.md +6 -0
- package/docs/reference/functions/norm.md +6 -0
- package/docs/reference/functions/not.md +6 -0
- package/docs/reference/functions/nthRoot.md +6 -0
- package/docs/reference/functions/nthRoots.md +6 -0
- package/docs/reference/functions/numeric.md +6 -0
- package/docs/reference/functions/oct.md +6 -0
- package/docs/reference/functions/ones.md +6 -0
- package/docs/reference/functions/or.md +6 -0
- package/docs/reference/functions/parser.md +6 -0
- package/docs/reference/functions/partitionSelect.md +6 -0
- package/docs/reference/functions/permutations.md +6 -0
- package/docs/reference/functions/pickRandom.md +6 -0
- package/docs/reference/functions/pow.md +6 -0
- package/docs/reference/functions/print.md +6 -0
- package/docs/reference/functions/prod.md +6 -0
- package/docs/reference/functions/qr.md +6 -0
- package/docs/reference/functions/quantileSeq.md +6 -0
- package/docs/reference/functions/random.md +6 -0
- package/docs/reference/functions/randomInt.md +6 -0
- package/docs/reference/functions/range.md +6 -0
- package/docs/reference/functions/rationalize.md +7 -1
- package/docs/reference/functions/re.md +6 -0
- package/docs/reference/functions/reshape.md +7 -0
- package/docs/reference/functions/resize.md +6 -0
- package/docs/reference/functions/resolve.md +46 -0
- package/docs/reference/functions/rightArithShift.md +6 -0
- package/docs/reference/functions/rightLogShift.md +6 -0
- package/docs/reference/functions/rotate.md +6 -0
- package/docs/reference/functions/rotationMatrix.md +6 -0
- package/docs/reference/functions/round.md +6 -0
- package/docs/reference/functions/row.md +6 -0
- package/docs/reference/functions/sec.md +6 -0
- package/docs/reference/functions/sech.md +6 -0
- package/docs/reference/functions/setCartesian.md +6 -0
- package/docs/reference/functions/setDifference.md +6 -0
- package/docs/reference/functions/setDistinct.md +6 -0
- package/docs/reference/functions/setIntersect.md +6 -0
- package/docs/reference/functions/setIsSubset.md +6 -0
- package/docs/reference/functions/setMultiplicity.md +6 -0
- package/docs/reference/functions/setPowerset.md +6 -0
- package/docs/reference/functions/setSize.md +6 -0
- package/docs/reference/functions/setSymDifference.md +6 -0
- package/docs/reference/functions/setUnion.md +6 -0
- package/docs/reference/functions/sign.md +6 -0
- package/docs/reference/functions/simplify.md +43 -6
- package/docs/reference/functions/simplifyCore.md +50 -0
- package/docs/reference/functions/sin.md +6 -0
- package/docs/reference/functions/sinh.md +6 -0
- package/docs/reference/functions/size.md +6 -0
- package/docs/reference/functions/slu.md +6 -0
- package/docs/reference/functions/smaller.md +6 -0
- package/docs/reference/functions/smallerEq.md +6 -0
- package/docs/reference/functions/sort.md +6 -0
- package/docs/reference/functions/sqrt.md +6 -0
- package/docs/reference/functions/sqrtm.md +6 -0
- package/docs/reference/functions/square.md +6 -0
- package/docs/reference/functions/squeeze.md +6 -0
- package/docs/reference/functions/std.md +6 -0
- package/docs/reference/functions/stirlingS2.md +6 -0
- package/docs/reference/functions/subset.md +6 -0
- package/docs/reference/functions/subtract.md +6 -0
- package/docs/reference/functions/sum.md +6 -0
- package/docs/reference/functions/tan.md +6 -0
- package/docs/reference/functions/tanh.md +6 -0
- package/docs/reference/functions/to.md +6 -0
- package/docs/reference/functions/trace.md +6 -0
- package/docs/reference/functions/transpose.md +6 -0
- package/docs/reference/functions/typeOf.md +6 -0
- package/docs/reference/functions/unaryMinus.md +6 -0
- package/docs/reference/functions/unaryPlus.md +6 -0
- package/docs/reference/functions/unequal.md +6 -0
- package/docs/reference/functions/usolve.md +6 -0
- package/docs/reference/functions/usolveAll.md +6 -0
- package/docs/reference/functions/variance.md +6 -0
- package/docs/reference/functions/xgcd.md +6 -0
- package/docs/reference/functions/xor.md +6 -0
- package/docs/reference/functions/zeros.md +6 -0
- package/docs/reference/functions.md +3 -0
- package/lib/browser/math.js +6 -6
- package/lib/browser/math.js.map +1 -1
- package/lib/cjs/core/create.js +4 -4
- package/lib/cjs/core/function/import.js +3 -3
- package/lib/cjs/core/function/typed.js +2 -2
- package/lib/cjs/defaultInstance.js +3 -3
- package/lib/cjs/entry/allFactoriesAny.js +1 -1
- package/lib/cjs/entry/allFactoriesNumber.js +1 -1
- package/lib/cjs/entry/configReadonly.js +1 -1
- package/lib/cjs/entry/dependenciesAny/dependenciesLeafCount.generated.js +23 -0
- package/lib/cjs/entry/dependenciesAny/dependenciesRationalize.generated.js +3 -0
- package/lib/cjs/entry/dependenciesAny/dependenciesResolve.generated.js +32 -0
- package/lib/cjs/entry/dependenciesAny/dependenciesSimplify.generated.js +6 -0
- package/lib/cjs/entry/dependenciesAny/dependenciesSimplifyCore.generated.js +65 -0
- package/lib/cjs/entry/dependenciesAny.generated.js +24 -0
- package/lib/cjs/entry/dependenciesNumber/dependenciesRationalize.generated.js +3 -0
- package/lib/cjs/entry/dependenciesNumber/dependenciesResolve.generated.js +32 -0
- package/lib/cjs/entry/dependenciesNumber/dependenciesSimplify.generated.js +6 -0
- package/lib/cjs/entry/dependenciesNumber/dependenciesSimplifyCore.generated.js +65 -0
- package/lib/cjs/entry/dependenciesNumber.generated.js +16 -0
- package/lib/cjs/entry/impureFunctionsAny.generated.js +67 -29
- package/lib/cjs/entry/impureFunctionsNumber.generated.js +96 -64
- package/lib/cjs/entry/pureFunctionsAny.generated.js +53 -55
- package/lib/cjs/entry/pureFunctionsNumber.generated.js +46 -48
- package/lib/cjs/expression/Help.js +4 -0
- package/lib/cjs/expression/Parser.js +1 -1
- package/lib/cjs/expression/embeddedDocs/core/typed.js +1 -1
- package/lib/cjs/expression/embeddedDocs/embeddedDocs.js +14 -5
- package/lib/cjs/expression/embeddedDocs/function/algebra/leafCount.js +15 -0
- package/lib/cjs/expression/embeddedDocs/function/algebra/resolve.js +16 -0
- package/lib/cjs/expression/embeddedDocs/function/algebra/simplify.js +1 -1
- package/lib/cjs/expression/embeddedDocs/function/algebra/simplifyCore.js +15 -0
- package/lib/cjs/expression/node/FunctionNode.js +7 -7
- package/lib/cjs/expression/node/IndexNode.js +1 -1
- package/lib/cjs/expression/node/Node.js +3 -3
- package/lib/cjs/expression/node/ObjectNode.js +1 -1
- package/lib/cjs/expression/node/utils/access.js +1 -1
- package/lib/cjs/expression/node/utils/assign.js +1 -1
- package/lib/cjs/expression/parse.js +13 -13
- package/lib/cjs/factoriesAny.js +24 -0
- package/lib/cjs/factoriesNumber.js +16 -0
- package/lib/cjs/function/algebra/decomposition/qr.js +1 -1
- package/lib/cjs/function/algebra/leafCount.js +66 -0
- package/lib/cjs/function/algebra/rationalize.js +24 -41
- package/lib/cjs/function/algebra/resolve.js +106 -0
- package/lib/cjs/function/algebra/simplify/simplifyConstant.js +5 -5
- package/lib/cjs/function/algebra/simplify/util.js +171 -33
- package/lib/cjs/function/algebra/simplify.js +554 -207
- package/lib/cjs/function/algebra/{simplify/simplifyCore.js → simplifyCore.js} +67 -43
- package/lib/cjs/function/algebra/solver/lsolveAll.js +2 -2
- package/lib/cjs/function/algebra/solver/usolveAll.js +2 -2
- package/lib/cjs/function/arithmetic/ceil.js +3 -3
- package/lib/cjs/function/arithmetic/floor.js +3 -3
- package/lib/cjs/function/arithmetic/invmod.js +1 -1
- package/lib/cjs/function/arithmetic/norm.js +1 -1
- package/lib/cjs/function/arithmetic/round.js +1 -1
- package/lib/cjs/function/matrix/eigs/complexEigs.js +5 -5
- package/lib/cjs/function/matrix/matrixFromColumns.js +1 -1
- package/lib/cjs/function/matrix/matrixFromRows.js +1 -1
- package/lib/cjs/function/probability/util/seededRNG.js +2 -2
- package/lib/cjs/function/relational/compareNatural.js +6 -6
- package/lib/cjs/header.js +2 -2
- package/lib/cjs/plain/bignumber/index.js +1 -1
- package/lib/cjs/plain/number/combinations.js +18 -6
- package/lib/cjs/type/bignumber/BigNumber.js +2 -2
- package/lib/cjs/type/bignumber/function/bignumber.js +1 -1
- package/lib/cjs/type/boolean.js +2 -2
- package/lib/cjs/type/complex/Complex.js +14 -14
- package/lib/cjs/type/complex/function/complex.js +1 -1
- package/lib/cjs/type/fraction/Fraction.js +6 -6
- package/lib/cjs/type/fraction/function/fraction.js +1 -1
- package/lib/cjs/type/matrix/DenseMatrix.js +5 -5
- package/lib/cjs/type/matrix/SparseMatrix.js +2 -2
- package/lib/cjs/type/number.js +1 -1
- package/lib/cjs/type/string.js +2 -2
- package/lib/cjs/type/unit/Unit.js +8 -8
- package/lib/cjs/utils/customs.js +2 -2
- package/lib/cjs/utils/emitter.js +1 -1
- package/lib/cjs/utils/function.js +2 -2
- package/lib/cjs/utils/is.js +6 -6
- package/lib/cjs/utils/latex.js +3 -3
- package/lib/cjs/utils/lruQueue.js +1 -1
- package/lib/cjs/utils/map.js +3 -3
- package/lib/cjs/utils/object.js +2 -2
- package/lib/cjs/utils/snapshot.js +7 -7
- package/lib/cjs/utils/string.js +2 -2
- package/lib/cjs/version.js +1 -1
- package/lib/esm/entry/dependenciesAny/dependenciesLeafCount.generated.js +12 -0
- package/lib/esm/entry/dependenciesAny/dependenciesRationalize.generated.js +2 -0
- package/lib/esm/entry/dependenciesAny/dependenciesResolve.generated.js +18 -0
- package/lib/esm/entry/dependenciesAny/dependenciesSimplify.generated.js +4 -0
- package/lib/esm/entry/dependenciesAny/dependenciesSimplifyCore.generated.js +40 -0
- package/lib/esm/entry/dependenciesAny.generated.js +3 -0
- package/lib/esm/entry/dependenciesNumber/dependenciesRationalize.generated.js +2 -0
- package/lib/esm/entry/dependenciesNumber/dependenciesResolve.generated.js +18 -0
- package/lib/esm/entry/dependenciesNumber/dependenciesSimplify.generated.js +4 -0
- package/lib/esm/entry/dependenciesNumber/dependenciesSimplifyCore.generated.js +40 -0
- package/lib/esm/entry/dependenciesNumber.generated.js +2 -0
- package/lib/esm/entry/impureFunctionsAny.generated.js +57 -22
- package/lib/esm/entry/impureFunctionsNumber.generated.js +83 -53
- package/lib/esm/entry/pureFunctionsAny.generated.js +42 -42
- package/lib/esm/entry/pureFunctionsNumber.generated.js +32 -32
- package/lib/esm/expression/Help.js +4 -0
- package/lib/esm/expression/embeddedDocs/core/typed.js +1 -1
- package/lib/esm/expression/embeddedDocs/embeddedDocs.js +6 -0
- package/lib/esm/expression/embeddedDocs/function/algebra/leafCount.js +8 -0
- package/lib/esm/expression/embeddedDocs/function/algebra/resolve.js +9 -0
- package/lib/esm/expression/embeddedDocs/function/algebra/simplify.js +1 -1
- package/lib/esm/expression/embeddedDocs/function/algebra/simplifyCore.js +8 -0
- package/lib/esm/factoriesAny.js +3 -0
- package/lib/esm/factoriesNumber.js +2 -0
- package/lib/esm/function/algebra/leafCount.js +59 -0
- package/lib/esm/function/algebra/rationalize.js +24 -40
- package/lib/esm/function/algebra/resolve.js +95 -0
- package/lib/esm/function/algebra/simplify/simplifyConstant.js +3 -3
- package/lib/esm/function/algebra/simplify/util.js +170 -34
- package/lib/esm/function/algebra/simplify.js +549 -202
- package/lib/esm/function/algebra/{simplify/simplifyCore.js → simplifyCore.js} +60 -44
- package/lib/esm/plain/number/combinations.js +18 -6
- package/lib/esm/version.js +1 -1
- package/package.json +15 -10
- package/types/index.d.ts +44 -2
- package/lib/cjs/function/algebra/simplify/resolve.js +0 -76
- package/lib/esm/function/algebra/simplify/resolve.js +0 -67
@@ -0,0 +1,95 @@
|
|
1
|
+
import { createMap, isMap } from '../../utils/map.js';
|
2
|
+
import { isFunctionNode, isNode, isOperatorNode, isParenthesisNode, isSymbolNode } from '../../utils/is.js';
|
3
|
+
import { factory } from '../../utils/factory.js';
|
4
|
+
var name = 'resolve';
|
5
|
+
var dependencies = ['parse', 'ConstantNode', 'FunctionNode', 'OperatorNode', 'ParenthesisNode'];
|
6
|
+
export var createResolve = /* #__PURE__ */factory(name, dependencies, _ref => {
|
7
|
+
var {
|
8
|
+
parse,
|
9
|
+
ConstantNode,
|
10
|
+
FunctionNode,
|
11
|
+
OperatorNode,
|
12
|
+
ParenthesisNode
|
13
|
+
} = _ref;
|
14
|
+
|
15
|
+
/**
|
16
|
+
* resolve(expr, scope) replaces variable nodes with their scoped values
|
17
|
+
*
|
18
|
+
* Syntax:
|
19
|
+
*
|
20
|
+
* resolve(expr, scope)
|
21
|
+
*
|
22
|
+
* Examples:
|
23
|
+
*
|
24
|
+
* math.resolve('x + y', {x:1, y:2}) // Node {1 + 2}
|
25
|
+
* math.resolve(math.parse('x+y'), {x:1, y:2}) // Node {1 + 2}
|
26
|
+
* math.simplify('x+y', {x:2, y:'x+x'}).toString() // "6"
|
27
|
+
*
|
28
|
+
* See also:
|
29
|
+
*
|
30
|
+
* simplify, evaluate
|
31
|
+
*
|
32
|
+
* @param {Node} node
|
33
|
+
* The expression tree to be simplified
|
34
|
+
* @param {Object} scope
|
35
|
+
* Scope specifying variables to be resolved
|
36
|
+
* @return {Node} Returns `node` with variables recursively substituted.
|
37
|
+
* @throws {ReferenceError}
|
38
|
+
* If there is a cyclic dependency among the variables in `scope`,
|
39
|
+
* resolution is impossible and a ReferenceError is thrown.
|
40
|
+
*/
|
41
|
+
function resolve(node, scope) {
|
42
|
+
var within = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : new Set();
|
43
|
+
|
44
|
+
// note `within`:
|
45
|
+
// `within` is not documented, since it is for internal cycle
|
46
|
+
// detection only
|
47
|
+
if (!scope) {
|
48
|
+
return node;
|
49
|
+
}
|
50
|
+
|
51
|
+
if (!isMap(scope)) {
|
52
|
+
scope = createMap(scope);
|
53
|
+
}
|
54
|
+
|
55
|
+
if (isSymbolNode(node)) {
|
56
|
+
if (within.has(node.name)) {
|
57
|
+
var variables = Array.from(within).join(', ');
|
58
|
+
throw new ReferenceError("recursive loop of variable definitions among {".concat(variables, "}"));
|
59
|
+
}
|
60
|
+
|
61
|
+
var value = scope.get(node.name);
|
62
|
+
|
63
|
+
if (isNode(value)) {
|
64
|
+
var nextWithin = new Set(within);
|
65
|
+
nextWithin.add(node.name);
|
66
|
+
return resolve(value, scope, nextWithin);
|
67
|
+
} else if (typeof value === 'number') {
|
68
|
+
return parse(String(value));
|
69
|
+
} else if (value !== undefined) {
|
70
|
+
return new ConstantNode(value);
|
71
|
+
} else {
|
72
|
+
return node;
|
73
|
+
}
|
74
|
+
} else if (isOperatorNode(node)) {
|
75
|
+
var args = node.args.map(function (arg) {
|
76
|
+
return resolve(arg, scope, within);
|
77
|
+
});
|
78
|
+
return new OperatorNode(node.op, node.fn, args, node.implicit);
|
79
|
+
} else if (isParenthesisNode(node)) {
|
80
|
+
return new ParenthesisNode(resolve(node.content, scope, within));
|
81
|
+
} else if (isFunctionNode(node)) {
|
82
|
+
var _args = node.args.map(function (arg) {
|
83
|
+
return resolve(arg, scope, within);
|
84
|
+
});
|
85
|
+
|
86
|
+
return new FunctionNode(node.name, _args);
|
87
|
+
} // Otherwise just recursively resolve any children (might also work
|
88
|
+
// for some of the above special cases)
|
89
|
+
|
90
|
+
|
91
|
+
return node.map(child => resolve(child, scope, within));
|
92
|
+
}
|
93
|
+
|
94
|
+
return resolve;
|
95
|
+
});
|
@@ -386,11 +386,11 @@ export var createSimplifyConstant = /* #__PURE__ */factory(name, dependencies, _
|
|
386
386
|
} else {
|
387
387
|
res = makeNode(_args);
|
388
388
|
}
|
389
|
-
} else if (isAssociative(node)) {
|
390
|
-
_args = allChildren(node);
|
389
|
+
} else if (isAssociative(node, options.context)) {
|
390
|
+
_args = allChildren(node, options.context);
|
391
391
|
_args = _args.map(arg => foldFraction(arg, options));
|
392
392
|
|
393
|
-
if (isCommutative(fn)) {
|
393
|
+
if (isCommutative(fn, options.context)) {
|
394
394
|
// commutative binary operator
|
395
395
|
var consts = [];
|
396
396
|
var vars = [];
|
@@ -1,4 +1,10 @@
|
|
1
|
-
import
|
1
|
+
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
2
|
+
|
3
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
4
|
+
|
5
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
6
|
+
|
7
|
+
import { isFunctionNode, isOperatorNode, isParenthesisNode } from '../../../utils/is.js';
|
2
8
|
import { factory } from '../../../utils/factory.js';
|
3
9
|
import { hasOwnProperty } from '../../../utils/object.js';
|
4
10
|
var name = 'simplifyUtil';
|
@@ -13,41 +19,166 @@ export var createUtil = /* #__PURE__ */factory(name, dependencies, _ref => {
|
|
13
19
|
// e.g. multiply is not commutative for matrices
|
14
20
|
// The properties should be calculated from an argument to simplify, or possibly something in math.config
|
15
21
|
// the other option is for typed() to specify a return type so that we can evaluate the type of arguments
|
16
|
-
|
17
|
-
|
18
|
-
|
22
|
+
|
23
|
+
/* So that properties of an operator fit on one line: */
|
24
|
+
var T = true;
|
25
|
+
var F = false;
|
26
|
+
var defaultName = 'defaultF';
|
27
|
+
var defaultContext = {
|
28
|
+
/* */
|
29
|
+
add: {
|
30
|
+
trivial: T,
|
31
|
+
total: T,
|
32
|
+
commutative: T,
|
33
|
+
associative: T
|
34
|
+
},
|
35
|
+
|
36
|
+
/**/
|
37
|
+
unaryPlus: {
|
38
|
+
trivial: T,
|
39
|
+
total: T,
|
40
|
+
commutative: T,
|
41
|
+
associative: T
|
42
|
+
},
|
43
|
+
|
44
|
+
/* */
|
45
|
+
subtract: {
|
46
|
+
trivial: F,
|
47
|
+
total: T,
|
48
|
+
commutative: F,
|
49
|
+
associative: F
|
50
|
+
},
|
51
|
+
|
52
|
+
/* */
|
53
|
+
multiply: {
|
54
|
+
trivial: T,
|
55
|
+
total: T,
|
56
|
+
commutative: T,
|
57
|
+
associative: T
|
58
|
+
},
|
59
|
+
|
60
|
+
/* */
|
61
|
+
divide: {
|
62
|
+
trivial: F,
|
63
|
+
total: T,
|
64
|
+
commutative: F,
|
65
|
+
associative: F
|
66
|
+
},
|
67
|
+
|
68
|
+
/* */
|
69
|
+
paren: {
|
70
|
+
trivial: T,
|
71
|
+
total: T,
|
72
|
+
commutative: T,
|
73
|
+
associative: F
|
74
|
+
},
|
75
|
+
|
76
|
+
/* */
|
77
|
+
defaultF: {
|
78
|
+
trivial: F,
|
79
|
+
total: T,
|
80
|
+
commutative: F,
|
81
|
+
associative: F
|
82
|
+
}
|
83
|
+
};
|
84
|
+
var realContext = {
|
85
|
+
divide: {
|
86
|
+
total: F
|
87
|
+
},
|
88
|
+
log: {
|
89
|
+
total: F
|
90
|
+
}
|
19
91
|
};
|
20
|
-
var
|
21
|
-
|
22
|
-
|
92
|
+
var positiveContext = {
|
93
|
+
subtract: {
|
94
|
+
total: F
|
95
|
+
},
|
96
|
+
abs: {
|
97
|
+
trivial: T
|
98
|
+
},
|
99
|
+
log: {
|
100
|
+
total: T
|
101
|
+
}
|
23
102
|
};
|
24
103
|
|
25
|
-
function
|
26
|
-
|
27
|
-
|
104
|
+
function hasProperty(nodeOrName, property) {
|
105
|
+
var context = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultContext;
|
106
|
+
var name = defaultName;
|
107
|
+
|
108
|
+
if (typeof nodeOrName === 'string') {
|
109
|
+
name = nodeOrName;
|
110
|
+
} else if (isOperatorNode(nodeOrName)) {
|
111
|
+
name = nodeOrName.fn.toString();
|
112
|
+
} else if (isFunctionNode(nodeOrName)) {
|
113
|
+
name = nodeOrName.name;
|
114
|
+
} else if (isParenthesisNode(nodeOrName)) {
|
115
|
+
name = 'paren';
|
28
116
|
}
|
29
117
|
|
30
|
-
|
118
|
+
if (hasOwnProperty(context, name)) {
|
119
|
+
var properties = context[name];
|
31
120
|
|
32
|
-
|
33
|
-
|
121
|
+
if (hasOwnProperty(properties, property)) {
|
122
|
+
return properties[property];
|
123
|
+
}
|
124
|
+
|
125
|
+
if (hasOwnProperty(defaultContext, name)) {
|
126
|
+
return defaultContext[name][property];
|
127
|
+
}
|
34
128
|
}
|
35
129
|
|
36
|
-
|
37
|
-
|
130
|
+
if (hasOwnProperty(context, defaultName)) {
|
131
|
+
var _properties = context[defaultName];
|
132
|
+
|
133
|
+
if (hasOwnProperty(_properties, property)) {
|
134
|
+
return _properties[property];
|
135
|
+
}
|
136
|
+
|
137
|
+
return defaultContext[defaultName][property];
|
138
|
+
}
|
139
|
+
/* name not found in context and context has no global default */
|
140
|
+
|
141
|
+
/* So use default context. */
|
142
|
+
|
143
|
+
|
144
|
+
if (hasOwnProperty(defaultContext, name)) {
|
145
|
+
var _properties2 = defaultContext[name];
|
38
146
|
|
39
|
-
|
40
|
-
|
41
|
-
|
147
|
+
if (hasOwnProperty(_properties2, property)) {
|
148
|
+
return _properties2[property];
|
149
|
+
}
|
42
150
|
}
|
43
151
|
|
44
|
-
|
152
|
+
return defaultContext[defaultName][property];
|
153
|
+
}
|
154
|
+
|
155
|
+
function isCommutative(node) {
|
156
|
+
var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultContext;
|
157
|
+
return hasProperty(node, 'commutative', context);
|
158
|
+
}
|
45
159
|
|
46
|
-
|
47
|
-
|
160
|
+
function isAssociative(node) {
|
161
|
+
var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultContext;
|
162
|
+
return hasProperty(node, 'associative', context);
|
163
|
+
}
|
164
|
+
/**
|
165
|
+
* Merge the given contexts, with primary overriding secondary
|
166
|
+
* wherever they might conflict
|
167
|
+
*/
|
168
|
+
|
169
|
+
|
170
|
+
function mergeContext(primary, secondary) {
|
171
|
+
var merged = _objectSpread({}, primary);
|
172
|
+
|
173
|
+
for (var prop in secondary) {
|
174
|
+
if (hasOwnProperty(primary, prop)) {
|
175
|
+
merged[prop] = _objectSpread(_objectSpread({}, secondary[prop]), primary[prop]);
|
176
|
+
} else {
|
177
|
+
merged[prop] = secondary[prop];
|
178
|
+
}
|
48
179
|
}
|
49
180
|
|
50
|
-
return
|
181
|
+
return merged;
|
51
182
|
}
|
52
183
|
/**
|
53
184
|
* Flatten all associative operators in an expression tree.
|
@@ -55,15 +186,15 @@ export var createUtil = /* #__PURE__ */factory(name, dependencies, _ref => {
|
|
55
186
|
*/
|
56
187
|
|
57
188
|
|
58
|
-
function flatten(node) {
|
189
|
+
function flatten(node, context) {
|
59
190
|
if (!node.args || node.args.length === 0) {
|
60
191
|
return node;
|
61
192
|
}
|
62
193
|
|
63
|
-
node.args = allChildren(node);
|
194
|
+
node.args = allChildren(node, context);
|
64
195
|
|
65
196
|
for (var i = 0; i < node.args.length; i++) {
|
66
|
-
flatten(node.args[i]);
|
197
|
+
flatten(node.args[i], context);
|
67
198
|
}
|
68
199
|
}
|
69
200
|
/**
|
@@ -72,7 +203,7 @@ export var createUtil = /* #__PURE__ */factory(name, dependencies, _ref => {
|
|
72
203
|
*/
|
73
204
|
|
74
205
|
|
75
|
-
function allChildren(node) {
|
206
|
+
function allChildren(node, context) {
|
76
207
|
var op;
|
77
208
|
var children = [];
|
78
209
|
|
@@ -88,7 +219,7 @@ export var createUtil = /* #__PURE__ */factory(name, dependencies, _ref => {
|
|
88
219
|
}
|
89
220
|
};
|
90
221
|
|
91
|
-
if (isAssociative(node)) {
|
222
|
+
if (isAssociative(node, context)) {
|
92
223
|
op = node.op;
|
93
224
|
findChildren(node);
|
94
225
|
return children;
|
@@ -101,7 +232,7 @@ export var createUtil = /* #__PURE__ */factory(name, dependencies, _ref => {
|
|
101
232
|
*/
|
102
233
|
|
103
234
|
|
104
|
-
function unflattenr(node) {
|
235
|
+
function unflattenr(node, context) {
|
105
236
|
if (!node.args || node.args.length === 0) {
|
106
237
|
return;
|
107
238
|
}
|
@@ -110,10 +241,10 @@ export var createUtil = /* #__PURE__ */factory(name, dependencies, _ref => {
|
|
110
241
|
var l = node.args.length;
|
111
242
|
|
112
243
|
for (var i = 0; i < l; i++) {
|
113
|
-
unflattenr(node.args[i]);
|
244
|
+
unflattenr(node.args[i], context);
|
114
245
|
}
|
115
246
|
|
116
|
-
if (l > 2 && isAssociative(node)) {
|
247
|
+
if (l > 2 && isAssociative(node, context)) {
|
117
248
|
var curnode = node.args.pop();
|
118
249
|
|
119
250
|
while (node.args.length > 0) {
|
@@ -128,7 +259,7 @@ export var createUtil = /* #__PURE__ */factory(name, dependencies, _ref => {
|
|
128
259
|
*/
|
129
260
|
|
130
261
|
|
131
|
-
function unflattenl(node) {
|
262
|
+
function unflattenl(node, context) {
|
132
263
|
if (!node.args || node.args.length === 0) {
|
133
264
|
return;
|
134
265
|
}
|
@@ -137,10 +268,10 @@ export var createUtil = /* #__PURE__ */factory(name, dependencies, _ref => {
|
|
137
268
|
var l = node.args.length;
|
138
269
|
|
139
270
|
for (var i = 0; i < l; i++) {
|
140
|
-
unflattenl(node.args[i]);
|
271
|
+
unflattenl(node.args[i], context);
|
141
272
|
}
|
142
273
|
|
143
|
-
if (l > 2 && isAssociative(node)) {
|
274
|
+
if (l > 2 && isAssociative(node, context)) {
|
144
275
|
var curnode = node.args.shift();
|
145
276
|
|
146
277
|
while (node.args.length > 0) {
|
@@ -170,11 +301,16 @@ export var createUtil = /* #__PURE__ */factory(name, dependencies, _ref => {
|
|
170
301
|
|
171
302
|
return {
|
172
303
|
createMakeNodeFunction,
|
304
|
+
hasProperty,
|
173
305
|
isCommutative,
|
174
306
|
isAssociative,
|
307
|
+
mergeContext,
|
175
308
|
flatten,
|
176
309
|
allChildren,
|
177
310
|
unflattenr,
|
178
|
-
unflattenl
|
311
|
+
unflattenl,
|
312
|
+
defaultContext,
|
313
|
+
realContext,
|
314
|
+
positiveContext
|
179
315
|
};
|
180
316
|
});
|