This repository was archived by the owner on Aug 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 282
Expand file tree
/
Copy pathaddConstantFractions.js
More file actions
220 lines (186 loc) · 7.03 KB
/
addConstantFractions.js
File metadata and controls
220 lines (186 loc) · 7.03 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
const divideByGCD = require('./divideByGCD');
const math = require('mathjs');
const ChangeTypes = require('../../ChangeTypes');
const evaluate = require('../../util/evaluate');
const Node = require('../../node');
// Adds constant fractions -- can start from either step 1 or 2
// 1A. Find the LCD if denominators are different and multiplies to make
// denominators equal, e.g. 2/3 + 4/6 --> (2*2)/(3*2) + 4/6
// 1B. Multiplies out to make constant fractions again
// e.g. (2*2)/(3*2) + 4/6 -> 4/6 + 4/6
// 2A. Combines numerators, e.g. 4/6 + 4/6 -> e.g. 2/5 + 4/5 --> (2+4)/5
// 2B. Adds numerators together, e.g. (2+4)/5 -> 6/5
// Returns a Node.Status object with substeps
function addConstantFractions(node) {
let newNode = node.cloneDeep();
if (!Node.Type.isOperator(node) || node.op !== '+') {
return Node.Status.noChange(node);
}
if (!node.args.every(n => Node.Type.isIntegerFraction(n, true) ||
hasIntegerMultiplicationDenominator(n))) {
return Node.Status.noChange(node);
}
const denominators = node.args.map(fraction => {
return parseFloat(evaluate(fraction.args[1]));
});
const substeps = [];
let status;
// 1A. First create the common denominator if needed
// e.g. 2/6 + 1/4 -> (2*2)/(6*2) + (1*3)/(4*3)
if (!denominators.every(denominator => denominator === denominators[0])) {
status = makeCommonDenominator(newNode);
substeps.push(status);
newNode = Node.Status.resetChangeGroups(status.newNode);
// 1B. Multiply out the denominators
status = evaluateDenominators(newNode);
substeps.push(status);
newNode = Node.Status.resetChangeGroups(status.newNode);
// 1B. Multiply out the numerators
status = evaluateNumerators(newNode);
substeps.push(status);
newNode = Node.Status.resetChangeGroups(status.newNode);
}
const newDenominators = newNode.args.map(fraction => {
return fraction.args[1];
});
if (!newDenominators.every(denominator => hasEqualNumberOfArgs(denominator, newDenominators[0]))){
// Multiply out the denominators
status = evaluateDenominators(newNode);
substeps.push(status);
newNode = Node.Status.resetChangeGroups(status.newNode);
}
// 2A. Now that they all have the same denominator, combine the numerators
// e.g. 2/3 + 5/3 -> (2+5)/3
status = combineNumeratorsAboveCommonDenominator(newNode);
substeps.push(status);
newNode = Node.Status.resetChangeGroups(status.newNode);
// 2B. Finally, add the numerators together
status = addNumeratorsTogether(newNode);
substeps.push(status);
newNode = Node.Status.resetChangeGroups(status.newNode);
// 2C. If the numerator is 0, simplify to just 0
status = reduceNumerator(newNode);
if (status.hasChanged()) {
substeps.push(status);
newNode = Node.Status.resetChangeGroups(status.newNode);
}
// 2D. If we can simplify the fraction, do so
status = divideByGCD(newNode);
if (status.hasChanged()) {
substeps.push(status);
newNode = Node.Status.resetChangeGroups(status.newNode);
}
return Node.Status.nodeChanged(
ChangeTypes.ADD_FRACTIONS, node, newNode, true, substeps);
}
// Given a + operation node with a list of fraction nodes as args that all have
// the same denominator, add them together. e.g. 2/3 + 5/3 -> (2+5)/3
// Returns the new node.
function combineNumeratorsAboveCommonDenominator(node) {
let newNode = node.cloneDeep();
const commonDenominator = newNode.args[0].args[1];
const numeratorArgs = [];
newNode.args.forEach(fraction => {
numeratorArgs.push(fraction.args[0]);
});
const newNumerator = Node.Creator.parenthesis(
Node.Creator.operator('+', numeratorArgs));
newNode = Node.Creator.operator('/', [newNumerator, commonDenominator]);
return Node.Status.nodeChanged(
ChangeTypes.COMBINE_NUMERATORS, node, newNode);
}
// Given a node with a numerator that is an addition node, will add
// all the numerators and return the result
function addNumeratorsTogether(node) {
const newNode = node.cloneDeep();
newNode.args[0] = Node.Creator.constant(evaluate(newNode.args[0]));
return Node.Status.nodeChanged(
ChangeTypes.ADD_NUMERATORS, node, newNode);
}
function reduceNumerator(node) {
let newNode = node.cloneDeep();
if (newNode.args[0].value === '0') {
newNode = Node.Creator.constant(0);
return Node.Status.nodeChanged(
ChangeTypes.REDUCE_ZERO_NUMERATOR, node, newNode);
}
return Node.Status.noChange(node);
}
// Takes `node`, a sum of fractions, and returns a node that's a sum of
// fractions with denominators that evaluate to the same common denominator
// e.g. 2/6 + 1/4 -> (2*2)/(6*2) + (1*3)/(4*3)
// Returns the new node.
function makeCommonDenominator(node) {
const newNode = node.cloneDeep();
const denominators = newNode.args.map(fraction => {
return parseFloat(fraction.args[1].value);
});
const commonDenominator = math.lcm(...denominators);
newNode.args.forEach((child, i) => {
// missingFactor is what we need to multiply the top and bottom by
// so that the denominator is the LCD
const missingFactor = commonDenominator / denominators[i];
if (missingFactor !== 1) {
const missingFactorNode = Node.Creator.constant(missingFactor);
const newNumerator = Node.Creator.parenthesis(
Node.Creator.operator('*', [child.args[0], missingFactorNode]));
const newDeominator = Node.Creator.parenthesis(
Node.Creator.operator('*', [child.args[1], missingFactorNode]));
newNode.args[i] = Node.Creator.operator('/', [newNumerator, newDeominator]);
}
});
return Node.Status.nodeChanged(
ChangeTypes.COMMON_DENOMINATOR, node, newNode);
}
function evaluateDenominators(node) {
const newNode = node.cloneDeep();
newNode.args.map(fraction => {
fraction.args[1] = Node.Creator.constant(evaluate(fraction.args[1]));
});
return Node.Status.nodeChanged(
ChangeTypes.MULTIPLY_DENOMINATORS, node, newNode);
}
function evaluateNumerators(node) {
const newNode = node.cloneDeep();
newNode.args.map(fraction => {
fraction.args[0] = Node.Creator.constant(evaluate(fraction.args[0]));
});
return Node.Status.nodeChanged(
ChangeTypes.MULTIPLY_NUMERATORS, node, newNode);
}
function hasIntegerMultiplicationDenominator(node) {
if (!Node.Type.isOperator(node, '/')) {
return false;
}
var denominator;
if (Node.Type.isParenthesis(node.args[1])) {
denominator = node.args[1].content;
}
else {
denominator = node.args[1];
}
if (!Node.Type.isOperator(denominator, '*')) {
return false;
}
if (!denominator.args.every(n => (Node.Type.isConstant(n, true) ||
Number.isInteger(parseFloat(n.value))))){
return false;
}
return true;
}
function hasEqualNumberOfArgs(n1, n2){
let node1 = n1.cloneDeep();
let node2 = n2.cloneDeep();
node1 = Node.Type.isParenthesis(node1) ? node1.content : node1;
node2 = Node.Type.isParenthesis(node2) ? node2.content : node2;
let length1 = 1;
let length2 = 1;
if (node1.args) {
length1 = node1.args.length;
}
if (node2.args) {
length2 = node2.args.length;
}
return length1 === length2;
}
module.exports = addConstantFractions;