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 pathdivideLikeTerms.js
More file actions
178 lines (154 loc) · 6.02 KB
/
divideLikeTerms.js
File metadata and controls
178 lines (154 loc) · 6.02 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
const arithmeticSearch = require('../arithmeticSearch');
const checks = require('../../checks');
const clone = require('../../util/clone');
const ConstantOrConstantPower = require('./ConstantOrConstantPower');
const ChangeTypes = require('../../ChangeTypes');
const Node = require('../../node');
// Divides a list of nodes that are polynomial like terms or constants with same base.
// Returns a node.
// The nodes should *not* have coefficients.
function divideLikeTerms(node, polynomialOnly = false) {
if (!Node.Type.isOperator(node)) {
return Node.Status.noChange(node);
}
let status;
if (!polynomialOnly && !checks.canDivideLikeTermConstantNodes(node)) {
status = arithmeticSearch(node);
if (status.hasChanged()) {
status.changeType = ChangeTypes.SIMPLIFY_FRACTION;
return status;
}
}
status = dividePolynomialTerms(node);
if (status.hasChanged()) {
status.changeType = ChangeTypes.SIMPLIFY_FRACTION;
return status;
}
return Node.Status.noChange(node);
}
function dividePolynomialTerms(node) {
if (!checks.canDivideLikeTermPolynomialNodes(node) &&
!checks.canDivideLikeTermConstantNodes(node)) {
return Node.Status.noChange(node);
}
const substeps = [];
let newNode = clone(node);
// STEP 1: If any term has no exponent, make it have exponent 1
// e.g. x -> x^1 (this is for pedagogy reasons)
// (this step only happens under certain conditions and later steps might
// happen even if step 1 does not)
let status = addOneExponent(newNode);
if (status.hasChanged()) {
substeps.push(status);
newNode = Node.Status.resetChangeGroups(status.newNode);
}
// STEP 2: collect exponents to a single exponent difference
// e.g. x^1 / x^3 -> x^(1 + -3)
if (checks.canDivideLikeTermConstantNodes(node)) {
status = collectConstantExponents(newNode);
}
else {
status = collectPolynomialExponents(newNode);
}
substeps.push(status);
newNode = Node.Status.resetChangeGroups(status.newNode);
// STEP 3: calculate difference of exponents.
// NOTE: This might not be a step if the exponents aren't all constants,
// but this case isn't that common and can be caught in other steps.
// e.g. x^(2-4-z)
// TODO: handle fractions, combining and collecting like terms, etc, here
const exponentDiff = newNode.args[1].content;
const diffStatus = arithmeticSearch(exponentDiff);
if (diffStatus.hasChanged()) {
status = Node.Status.childChanged(newNode, diffStatus, 1);
substeps.push(status);
newNode = Node.Status.resetChangeGroups(status.newNode);
}
if (substeps.length === 1) { // possible if only step 2 happens
return substeps[0];
}
else {
return Node.Status.nodeChanged(
ChangeTypes.DIVIDE_POLYNOMIAL_TERMS,
node, newNode, true, substeps);
}
}
// Given a product of polynomial terms, changes any term with no exponent
// into a term with an explicit exponent of 1. This is for pedagogy, and
// makes the adding coefficients step clearer.
// e.g. x^2 / x -> x^2 / x^1
// Returns a Node.Status object.
function addOneExponent(node) {
const newNode = clone(node);
let change = false;
let changeGroup = 1;
if (checks.canDivideLikeTermConstantNodes(node)) {
newNode.args.forEach((child, i) => {
if (Node.Type.isConstant(child)) {
const base = ConstantOrConstantPower.getBaseNode(child);
const exponent = Node.Creator.constant(1);
newNode.args[i] = Node.Creator.operator('^', [base, exponent]);
newNode.args[i].changeGroup = changeGroup;
node.args[i].changeGroup = changeGroup; // note that this is the "oldNode"
change = true;
changeGroup++;
}
});
}
else {
newNode.args.forEach((child, i) => {
const polyTerm = new Node.PolynomialTerm(child);
if (!polyTerm.getExponentNode()) {
newNode.args[i] = Node.Creator.polynomialTerm(
polyTerm.getSymbolNode(),
Node.Creator.constant(1),
polyTerm.getCoeffNode());
newNode.args[i].changeGroup = changeGroup;
node.args[i].changeGroup = changeGroup; // note that this is the "oldNode"
change = true;
changeGroup++;
}
});
}
if (change) {
return Node.Status.nodeChanged(
ChangeTypes.ADD_EXPONENT_OF_ONE, node, newNode, false);
}
else {
return Node.Status.noChange(node);
}
}
// Given a division of constant terms, groups the exponents into a difference
// e.g. 10^5 / 10^3 -> 10^(5 - 3)
// Returns a Node.Status object.
function collectConstantExponents(node) {
// If we're dividing constant nodes together, they all share the same
// base. Get that from the first node.
const baseNode = ConstantOrConstantPower.getBaseNode(node.args[0]);
// The new exponent will be a difference of exponents (an operation, wrapped in
// parens) e.g. 10^(5-3)
const exponentNodeList = node.args.map(ConstantOrConstantPower.getExponentNode);
const newExponent = Node.Creator.parenthesis(
Node.Creator.operator('-', exponentNodeList));
const newNode = Node.Creator.operator('^', [baseNode, newExponent]);
return Node.Status.nodeChanged(
ChangeTypes.COLLECT_CONSTANT_EXPONENTS, node, newNode);
}
// Given a division of polynomial terms, groups the exponents into a difference
// e.g. x^5 / x^3 -> x^(5 - 3)
// Returns a Node.Status object.
function collectPolynomialExponents(node) {
const polynomialTermList = node.args.map(n => new Node.PolynomialTerm(n));
// If we're dividing polynomial nodes together, they all share the same
// symbol. Get that from the first node.
const symbolNode = polynomialTermList[0].getSymbolNode();
// The new exponent will be a difference of exponents (an operation, wrapped in
// parens) e.g. x^(5-3)
const exponentNodeList = polynomialTermList.map(p => p.getExponentNode(true));
const newExponent = Node.Creator.parenthesis(
Node.Creator.operator('-', exponentNodeList));
const newNode = Node.Creator.polynomialTerm(symbolNode, newExponent, null);
return Node.Status.nodeChanged(
ChangeTypes.COLLECT_POLYNOMIAL_EXPONENTS, node, newNode);
}
module.exports = divideLikeTerms;