-
Notifications
You must be signed in to change notification settings - Fork 275
/
stepThrough.js
131 lines (115 loc) · 4.16 KB
/
stepThrough.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'use strict';
const checks = require('../checks');
const Node = require('../node');
const arithmeticSearch = require('./arithmeticSearch');
const basicsSearch = require('./basicsSearch');
const breakUpNumeratorSearch = require('./breakUpNumeratorSearch');
const distributeSearch = require('./distributeSearch');
const divisionSearch = require('./divisionSearch');
const collectAndCombineSearch = require('./collectAndCombineSearch');
const fractionsSearch = require('./fractionsSearch');
const functionsSearch = require('./functionsSearch');
const multiplyFractionsSearch = require('./multiplyFractionsSearch');
const clone = require('../util/clone');
const flattenOperands = require('../util/flattenOperands');
const removeUnnecessaryParens = require('../util/removeUnnecessaryParens');
const print = require('../util/print');
// Given a mathjs expression node, steps through simplifying the expression.
// Returns a list of details about each step.
function stepThrough(node, debug=false) {
if (debug) {
// eslint-disable-next-line
console.log('\n\nSimplifying: ' + print(node, false, true));
}
if(checks.hasUnsupportedNodes(node)) {
return [];
}
let nodeStatus;
let steps = [];
const originalExpressionStr = print(node);
const MAX_STEP_COUNT = 20;
let iters = 0;
// Now, step through the math expression until nothing changes
nodeStatus = step(node);
while (nodeStatus.hasChanged()) {
steps = addStep(steps, nodeStatus, debug);
nodeStatus.reset();
nodeStatus = step(nodeStatus.newNode);
if (iters++ === MAX_STEP_COUNT) {
// eslint-disable-next-line
console.error('Math error: Potential infinite loop for expression: ' +
originalExpressionStr + ', returning no steps');
return [];
}
}
return steps;
}
// Given a mathjs expression node, performs a single step to simplify the
// expression. Returns a Node.Status object.
function step(node) {
let nodeStatus;
node = flattenOperands(node);
node = removeUnnecessaryParens(node, true);
const simplificationTreeSearches = [
// Basic simplifications that we always try first e.g. (...)^0 => 1
basicsSearch,
// Simplify any division chains so there's at most one division operation.
// e.g. 2/x/6 -> 2/(x*6) e.g. 2/(x/6) => 2 * 6/x
divisionSearch,
// Adding fractions, cancelling out things in fractions
fractionsSearch,
// e.g. 2 + 2 => 4
arithmeticSearch,
// e.g. addition: 2x + 4x^2 + x => 4x^2 + 3x
// e.g. multiplication: 2x * x * x^2 => 2x^3
collectAndCombineSearch,
// e.g. (2 + x) / 4 => 2/4 + x/4
breakUpNumeratorSearch,
// e.g. 3/x * 2x/5 => (3 * 2x) / (x * 5)
multiplyFractionsSearch,
// e.g. (2x + 3)(x + 4) => 2x^2 + 11x + 12
distributeSearch,
// e.g. abs(-4) => 4
functionsSearch,
];
for (let i = 0; i < simplificationTreeSearches.length; i++) {
nodeStatus = simplificationTreeSearches[i](node);
// Always update node, since there might be changes that didn't count as
// a step. Remove unnecessary parens, in case one a step results in more
// parens than needed.
node = removeUnnecessaryParens(nodeStatus.newNode, true);
if (nodeStatus.hasChanged()) {
node = flattenOperands(node);
nodeStatus.newNode = clone(node);
return nodeStatus;
}
else {
node = flattenOperands(node);
}
}
return Node.Status.noChange(node);
}
// Adds a new step to the array, given details of a change that just happened.
// Returns the new steps array.
function addStep(steps, nodeStatus, debug) {
let substeps = [];
if (nodeStatus.substeps.length) {
nodeStatus.substeps.forEach(substepStatus =>
substeps = addStep(substeps, substepStatus, debug));
}
steps.push({
'changeType': nodeStatus.changeType,
'oldNode': removeUnnecessaryParens(nodeStatus.oldNode, true),
'newNode': removeUnnecessaryParens(nodeStatus.newNode, true),
'substeps': substeps,
'asciimath': print(nodeStatus.newNode),
});
if (debug) {
// eslint-disable-next-line
console.log(nodeStatus.changeType);
// eslint-disable-next-line
console.log(print(nodeStatus.newNode) + '\n');
}
return steps;
}
module.exports = stepThrough;