@teachinglab/omd 0.6.0 → 0.6.2
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/README.md +257 -251
- package/README.old.md +137 -137
- package/canvas/core/canvasConfig.js +202 -202
- package/canvas/drawing/segment.js +167 -167
- package/canvas/drawing/stroke.js +385 -385
- package/canvas/events/eventManager.js +444 -444
- package/canvas/events/pointerEventHandler.js +262 -262
- package/canvas/index.js +48 -48
- package/canvas/tools/PointerTool.js +71 -71
- package/canvas/tools/tool.js +222 -222
- package/canvas/utils/boundingBox.js +377 -377
- package/canvas/utils/mathUtils.js +258 -258
- package/docs/api/configuration-options.md +198 -198
- package/docs/api/eventManager.md +82 -82
- package/docs/api/focusFrameManager.md +144 -144
- package/docs/api/index.md +105 -105
- package/docs/api/main.md +62 -62
- package/docs/api/omdBinaryExpressionNode.md +86 -86
- package/docs/api/omdCanvas.md +83 -83
- package/docs/api/omdConfigManager.md +112 -112
- package/docs/api/omdConstantNode.md +52 -52
- package/docs/api/omdDisplay.md +87 -87
- package/docs/api/omdEquationNode.md +174 -174
- package/docs/api/omdEquationSequenceNode.md +258 -258
- package/docs/api/omdEquationStack.md +192 -192
- package/docs/api/omdFunctionNode.md +82 -82
- package/docs/api/omdGroupNode.md +78 -78
- package/docs/api/omdHelpers.md +87 -87
- package/docs/api/omdLeafNode.md +85 -85
- package/docs/api/omdNode.md +201 -201
- package/docs/api/omdOperationDisplayNode.md +117 -117
- package/docs/api/omdOperatorNode.md +91 -91
- package/docs/api/omdParenthesisNode.md +133 -133
- package/docs/api/omdPopup.md +191 -191
- package/docs/api/omdPowerNode.md +131 -131
- package/docs/api/omdRationalNode.md +144 -144
- package/docs/api/omdSequenceNode.md +128 -128
- package/docs/api/omdSimplification.md +78 -78
- package/docs/api/omdSqrtNode.md +144 -144
- package/docs/api/omdStepVisualizer.md +146 -146
- package/docs/api/omdStepVisualizerHighlighting.md +65 -65
- package/docs/api/omdStepVisualizerInteractiveSteps.md +108 -108
- package/docs/api/omdStepVisualizerLayout.md +70 -70
- package/docs/api/omdStepVisualizerNodeUtils.md +140 -140
- package/docs/api/omdStepVisualizerTextBoxes.md +76 -76
- package/docs/api/omdToolbar.md +130 -130
- package/docs/api/omdTranscriptionService.md +95 -95
- package/docs/api/omdTreeDiff.md +169 -169
- package/docs/api/omdUnaryExpressionNode.md +137 -137
- package/docs/api/omdUtilities.md +82 -82
- package/docs/api/omdVariableNode.md +123 -123
- package/docs/api/selectTool.md +74 -74
- package/docs/api/simplificationEngine.md +97 -97
- package/docs/api/simplificationRules.md +76 -76
- package/docs/api/simplificationUtils.md +64 -64
- package/docs/api/transcribe.md +43 -43
- package/docs/api-reference.md +85 -85
- package/docs/index.html +453 -453
- package/docs/index.md +38 -38
- package/docs/omd-objects.md +258 -258
- package/index.js +79 -79
- package/jsvg/index.js +3 -0
- package/jsvg/jsvg.js +898 -898
- package/jsvg/jsvgComponents.js +357 -358
- package/npm-docs/DOCUMENTATION_SUMMARY.md +220 -220
- package/npm-docs/README.md +251 -251
- package/npm-docs/api/api-reference.md +85 -85
- package/npm-docs/api/configuration-options.md +198 -198
- package/npm-docs/api/eventManager.md +82 -82
- package/npm-docs/api/expression-nodes.md +561 -561
- package/npm-docs/api/focusFrameManager.md +144 -144
- package/npm-docs/api/index.md +105 -105
- package/npm-docs/api/main.md +62 -62
- package/npm-docs/api/omdBinaryExpressionNode.md +86 -86
- package/npm-docs/api/omdCanvas.md +83 -83
- package/npm-docs/api/omdConfigManager.md +112 -112
- package/npm-docs/api/omdConstantNode.md +52 -52
- package/npm-docs/api/omdDisplay.md +87 -87
- package/npm-docs/api/omdEquationNode.md +174 -174
- package/npm-docs/api/omdEquationSequenceNode.md +258 -258
- package/npm-docs/api/omdEquationStack.md +192 -192
- package/npm-docs/api/omdFunctionNode.md +82 -82
- package/npm-docs/api/omdGroupNode.md +78 -78
- package/npm-docs/api/omdHelpers.md +87 -87
- package/npm-docs/api/omdLeafNode.md +85 -85
- package/npm-docs/api/omdNode.md +201 -201
- package/npm-docs/api/omdOperationDisplayNode.md +117 -117
- package/npm-docs/api/omdOperatorNode.md +91 -91
- package/npm-docs/api/omdParenthesisNode.md +133 -133
- package/npm-docs/api/omdPopup.md +191 -191
- package/npm-docs/api/omdPowerNode.md +131 -131
- package/npm-docs/api/omdRationalNode.md +144 -144
- package/npm-docs/api/omdSequenceNode.md +128 -128
- package/npm-docs/api/omdSimplification.md +78 -78
- package/npm-docs/api/omdSqrtNode.md +144 -144
- package/npm-docs/api/omdStepVisualizer.md +146 -146
- package/npm-docs/api/omdStepVisualizerHighlighting.md +65 -65
- package/npm-docs/api/omdStepVisualizerInteractiveSteps.md +108 -108
- package/npm-docs/api/omdStepVisualizerLayout.md +70 -70
- package/npm-docs/api/omdStepVisualizerNodeUtils.md +140 -140
- package/npm-docs/api/omdStepVisualizerTextBoxes.md +76 -76
- package/npm-docs/api/omdToolbar.md +130 -130
- package/npm-docs/api/omdTranscriptionService.md +95 -95
- package/npm-docs/api/omdTreeDiff.md +169 -169
- package/npm-docs/api/omdUnaryExpressionNode.md +137 -137
- package/npm-docs/api/omdUtilities.md +82 -82
- package/npm-docs/api/omdVariableNode.md +123 -123
- package/npm-docs/api/selectTool.md +74 -74
- package/npm-docs/api/simplificationEngine.md +97 -97
- package/npm-docs/api/simplificationRules.md +76 -76
- package/npm-docs/api/simplificationUtils.md +64 -64
- package/npm-docs/api/transcribe.md +43 -43
- package/npm-docs/guides/equations.md +854 -854
- package/npm-docs/guides/factory-functions.md +354 -354
- package/npm-docs/guides/getting-started.md +318 -318
- package/npm-docs/guides/quick-examples.md +525 -525
- package/npm-docs/guides/visualizations.md +682 -682
- package/npm-docs/index.html +12 -0
- package/npm-docs/json-schemas.md +826 -826
- package/omd/config/omdConfigManager.js +279 -267
- package/omd/core/index.js +158 -158
- package/omd/core/omdEquationStack.js +546 -546
- package/omd/core/omdUtilities.js +113 -113
- package/omd/display/omdDisplay.js +969 -962
- package/omd/display/omdToolbar.js +501 -501
- package/omd/nodes/omdBinaryExpressionNode.js +459 -459
- package/omd/nodes/omdConstantNode.js +141 -141
- package/omd/nodes/omdEquationNode.js +1327 -1327
- package/omd/nodes/omdFunctionNode.js +351 -351
- package/omd/nodes/omdGroupNode.js +67 -67
- package/omd/nodes/omdLeafNode.js +76 -76
- package/omd/nodes/omdNode.js +556 -556
- package/omd/nodes/omdOperationDisplayNode.js +321 -321
- package/omd/nodes/omdOperatorNode.js +108 -108
- package/omd/nodes/omdParenthesisNode.js +292 -292
- package/omd/nodes/omdPowerNode.js +235 -235
- package/omd/nodes/omdRationalNode.js +295 -295
- package/omd/nodes/omdSqrtNode.js +307 -307
- package/omd/nodes/omdUnaryExpressionNode.js +227 -227
- package/omd/nodes/omdVariableNode.js +122 -122
- package/omd/simplification/omdSimplification.js +140 -140
- package/omd/simplification/omdSimplificationEngine.js +887 -887
- package/omd/simplification/package.json +5 -5
- package/omd/simplification/rules/binaryRules.js +1037 -1037
- package/omd/simplification/rules/functionRules.js +111 -111
- package/omd/simplification/rules/index.js +48 -48
- package/omd/simplification/rules/parenthesisRules.js +19 -19
- package/omd/simplification/rules/powerRules.js +143 -143
- package/omd/simplification/rules/rationalRules.js +725 -725
- package/omd/simplification/rules/sqrtRules.js +48 -48
- package/omd/simplification/rules/unaryRules.js +37 -37
- package/omd/simplification/simplificationRules.js +31 -31
- package/omd/simplification/simplificationUtils.js +1055 -1055
- package/omd/step-visualizer/omdStepVisualizer.js +947 -947
- package/omd/step-visualizer/omdStepVisualizerHighlighting.js +246 -246
- package/omd/step-visualizer/omdStepVisualizerLayout.js +892 -892
- package/omd/step-visualizer/omdStepVisualizerTextBoxes.js +200 -200
- package/omd/utils/aiNextEquationStep.js +106 -106
- package/omd/utils/omdNodeOverlay.js +638 -638
- package/omd/utils/omdPopup.js +1203 -1203
- package/omd/utils/omdStepVisualizerInteractiveSteps.js +684 -684
- package/omd/utils/omdStepVisualizerNodeUtils.js +267 -267
- package/omd/utils/omdTranscriptionService.js +123 -123
- package/omd/utils/omdTreeDiff.js +733 -733
- package/package.json +59 -56
- package/readme.html +184 -120
- package/src/index.js +74 -74
- package/src/json-schemas.md +576 -576
- package/src/omd-json-samples.js +147 -147
- package/src/omdApp.js +391 -391
- package/src/omdAppCanvas.js +335 -335
- package/src/omdBalanceHanger.js +199 -199
- package/src/omdColor.js +13 -13
- package/src/omdCoordinatePlane.js +541 -541
- package/src/omdExpression.js +115 -115
- package/src/omdFactory.js +150 -150
- package/src/omdFunction.js +114 -114
- package/src/omdMetaExpression.js +290 -290
- package/src/omdNaturalExpression.js +563 -563
- package/src/omdNode.js +383 -383
- package/src/omdNumber.js +52 -52
- package/src/omdNumberLine.js +114 -112
- package/src/omdNumberTile.js +118 -118
- package/src/omdOperator.js +72 -72
- package/src/omdPowerExpression.js +91 -91
- package/src/omdProblem.js +259 -259
- package/src/omdRatioChart.js +251 -251
- package/src/omdRationalExpression.js +114 -114
- package/src/omdSampleData.js +215 -215
- package/src/omdShapes.js +512 -512
- package/src/omdSpinner.js +151 -151
- package/src/omdString.js +49 -49
- package/src/omdTable.js +498 -498
- package/src/omdTapeDiagram.js +244 -244
- package/src/omdTerm.js +91 -91
- package/src/omdTileEquation.js +349 -349
- package/src/omdUtils.js +84 -84
- package/src/omdVariable.js +51 -51
|
@@ -1,725 +1,725 @@
|
|
|
1
|
-
import { SimplificationEngine } from '../omdSimplificationEngine.js';
|
|
2
|
-
import * as utils from '../simplificationUtils.js';
|
|
3
|
-
import { omdRationalNode } from '../../nodes/omdRationalNode.js';
|
|
4
|
-
|
|
5
|
-
// ===== RATIONAL NODE RULES =====
|
|
6
|
-
export const rationalRules = [
|
|
7
|
-
// Simplify when both numerator and denominator are unary negatives: (-a)/(-b) -> a/b
|
|
8
|
-
SimplificationEngine.createRule("Unary Minus Cancellation",
|
|
9
|
-
(node) => {
|
|
10
|
-
if (node.type !== 'omdRationalNode') return false;
|
|
11
|
-
|
|
12
|
-
const num = SimplificationEngine.unwrapParentheses(node.numerator);
|
|
13
|
-
const den = SimplificationEngine.unwrapParentheses(node.denominator);
|
|
14
|
-
|
|
15
|
-
if (SimplificationEngine.isType(num, 'omdUnaryExpressionNode') &&
|
|
16
|
-
SimplificationEngine.isType(den, 'omdUnaryExpressionNode')) {
|
|
17
|
-
// Both sides are unary; cancel the unary minus operators
|
|
18
|
-
return {
|
|
19
|
-
numeratorArg: num.argument || num.operand,
|
|
20
|
-
denominatorArg: den.argument || den.operand,
|
|
21
|
-
numeratorNode: num,
|
|
22
|
-
denominatorNode: den
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
return false;
|
|
27
|
-
},
|
|
28
|
-
(node, data) => {
|
|
29
|
-
// Create a new rational node with arguments unwrapped
|
|
30
|
-
const fontSize = node.getFontSize();
|
|
31
|
-
const numArg = data.numeratorArg;
|
|
32
|
-
const denArg = data.denominatorArg;
|
|
33
|
-
|
|
34
|
-
// If the denominator argument is a constant 1, return the numerator directly (e.g., x/1 -> x)
|
|
35
|
-
if (denArg && typeof denArg.isConstant === 'function' && denArg.isConstant() && denArg.getValue() === 1) {
|
|
36
|
-
const result = numArg.clone();
|
|
37
|
-
// Preserve provenance from original nodes and the rational node
|
|
38
|
-
try { utils.applyProvenance(result, node.numerator, node.denominator, node); } catch (e) {
|
|
39
|
-
// Fallback to manual pushes if applyProvenance fails
|
|
40
|
-
result.provenance = result.provenance || [];
|
|
41
|
-
if (data.numeratorNode) result.provenance.push(data.numeratorNode.id);
|
|
42
|
-
if (data.denominatorNode) result.provenance.push(data.denominatorNode.id);
|
|
43
|
-
result.provenance.push(node.id);
|
|
44
|
-
}
|
|
45
|
-
return result;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const newNum = numArg.clone();
|
|
49
|
-
const newDen = denArg.clone();
|
|
50
|
-
|
|
51
|
-
const ast = {
|
|
52
|
-
type: 'OperatorNode', op: '/', fn: 'divide',
|
|
53
|
-
args: [newNum.toMathJSNode(), newDen.toMathJSNode()],
|
|
54
|
-
clone: function() { return { ...this, args: this.args.map(a => a.clone()) }; }
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
const rational = new omdRationalNode(ast);
|
|
58
|
-
rational.setFontSize(fontSize);
|
|
59
|
-
rational.initialize();
|
|
60
|
-
|
|
61
|
-
// Give granular provenance to numerator and denominator children
|
|
62
|
-
try {
|
|
63
|
-
// Attach provenance to the child numerator and denominator specifically
|
|
64
|
-
if (rational.numerator) utils.applyProvenance(rational.numerator, node.numerator);
|
|
65
|
-
if (rational.denominator) utils.applyProvenance(rational.denominator, node.denominator);
|
|
66
|
-
// Also attach provenance to the rational node itself
|
|
67
|
-
utils.applyProvenance(rational, node.numerator, node.denominator, node);
|
|
68
|
-
} catch (e) {
|
|
69
|
-
_preserveComponentProvenance(rational, data.numeratorNode.argument || data.numeratorNode.operand, data.denominatorNode.argument || data.denominatorNode.operand);
|
|
70
|
-
if (!rational.provenance.includes(node.id)) rational.provenance.push(node.id);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return rational;
|
|
74
|
-
},
|
|
75
|
-
(originalNode, ruleData, newNode) => {
|
|
76
|
-
return `Canceled unary negatives in fraction`;
|
|
77
|
-
},
|
|
78
|
-
'rational'
|
|
79
|
-
),
|
|
80
|
-
|
|
81
|
-
// Simplify when numerator is unary minus and denominator is constant -1: (-x)/-1 -> x
|
|
82
|
-
SimplificationEngine.createRule("Unary Numerator Divide By -1",
|
|
83
|
-
(node) => {
|
|
84
|
-
if (node.type !== 'omdRationalNode') return false;
|
|
85
|
-
const num = SimplificationEngine.unwrapParentheses(node.numerator);
|
|
86
|
-
const den = SimplificationEngine.unwrapParentheses(node.denominator);
|
|
87
|
-
|
|
88
|
-
if (SimplificationEngine.isType(num, 'omdUnaryExpressionNode') && den.isConstant && den.isConstant() && den.getValue() === -1) {
|
|
89
|
-
return { numeratorArg: num.argument || num.operand, numeratorNode: num, denominatorNode: den };
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
return false;
|
|
93
|
-
},
|
|
94
|
-
(node, data) => {
|
|
95
|
-
const newNode = data.numeratorArg.clone();
|
|
96
|
-
// Preserve granular provenance: numerator argument's provenance and the denominator and whole node
|
|
97
|
-
try {
|
|
98
|
-
// If the result is a leaf (variable/constant), ensure it gets provenance from numerator/denominator
|
|
99
|
-
utils.applyProvenance(newNode, node.numerator, node.denominator, node);
|
|
100
|
-
} catch (e) {
|
|
101
|
-
newNode.provenance = newNode.provenance || [];
|
|
102
|
-
if (data.numeratorNode) newNode.provenance.push(data.numeratorNode.id);
|
|
103
|
-
if (data.denominatorNode) newNode.provenance.push(data.denominatorNode.id);
|
|
104
|
-
newNode.provenance.push(node.id);
|
|
105
|
-
}
|
|
106
|
-
return newNode;
|
|
107
|
-
},
|
|
108
|
-
(originalNode, ruleData, newNode) => {
|
|
109
|
-
return `Divided by -1 cancels unary minus in numerator`;
|
|
110
|
-
},
|
|
111
|
-
'rational'
|
|
112
|
-
),
|
|
113
|
-
// Simplify x/x = 1 (variable divided by itself)
|
|
114
|
-
SimplificationEngine.createRule("Variable Self Division",
|
|
115
|
-
(node) => {
|
|
116
|
-
if (node.type !== 'omdRationalNode') {
|
|
117
|
-
return false;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const numerator = SimplificationEngine.unwrapParentheses(node.numerator);
|
|
121
|
-
const denominator = SimplificationEngine.unwrapParentheses(node.denominator);
|
|
122
|
-
|
|
123
|
-
// Check if both numerator and denominator are the same variable
|
|
124
|
-
if (SimplificationEngine.isType(numerator, 'omdVariableNode') &&
|
|
125
|
-
SimplificationEngine.isType(denominator, 'omdVariableNode') &&
|
|
126
|
-
numerator.name === denominator.name) {
|
|
127
|
-
return {
|
|
128
|
-
variable: numerator.name,
|
|
129
|
-
numeratorNode: numerator,
|
|
130
|
-
denominatorNode: denominator
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
return false;
|
|
135
|
-
},
|
|
136
|
-
(node, data) => {
|
|
137
|
-
const newNode = SimplificationEngine.createConstant(1, node.getFontSize());
|
|
138
|
-
newNode.provenance.push(data.numeratorNode.id);
|
|
139
|
-
newNode.provenance.push(data.denominatorNode.id);
|
|
140
|
-
newNode.provenance.push(node.id);
|
|
141
|
-
return newNode;
|
|
142
|
-
},
|
|
143
|
-
(originalNode, ruleData, newNode) => {
|
|
144
|
-
const { variable } = ruleData;
|
|
145
|
-
return `Simplified variable self-division: ${variable}/${variable} = 1`;
|
|
146
|
-
}
|
|
147
|
-
),
|
|
148
|
-
|
|
149
|
-
// Simplify x^n/x = x^(n-1) (power divided by base)
|
|
150
|
-
SimplificationEngine.createRule("Power Base Division",
|
|
151
|
-
(node) => {
|
|
152
|
-
if (node.type !== 'omdRationalNode') {
|
|
153
|
-
return false;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const numerator = SimplificationEngine.unwrapParentheses(node.numerator);
|
|
157
|
-
const denominator = SimplificationEngine.unwrapParentheses(node.denominator);
|
|
158
|
-
|
|
159
|
-
// Check if numerator is a power and denominator is the same variable
|
|
160
|
-
if (SimplificationEngine.isType(numerator, 'omdPowerNode') &&
|
|
161
|
-
SimplificationEngine.isType(denominator, 'omdVariableNode') &&
|
|
162
|
-
SimplificationEngine.isType(numerator.base, 'omdVariableNode') &&
|
|
163
|
-
numerator.base.name === denominator.name &&
|
|
164
|
-
numerator.exponent.isConstant()) {
|
|
165
|
-
|
|
166
|
-
const exponent = numerator.exponent.getValue();
|
|
167
|
-
const newExponent = exponent - 1;
|
|
168
|
-
|
|
169
|
-
return {
|
|
170
|
-
variable: numerator.base.name,
|
|
171
|
-
originalExponent: exponent,
|
|
172
|
-
newExponent: newExponent,
|
|
173
|
-
baseNode: numerator.base,
|
|
174
|
-
powerNode: numerator,
|
|
175
|
-
denominatorNode: denominator
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
return false;
|
|
180
|
-
},
|
|
181
|
-
(node, data) => {
|
|
182
|
-
const { variable, newExponent, baseNode, powerNode, denominatorNode } = data;
|
|
183
|
-
const fontSize = node.getFontSize();
|
|
184
|
-
|
|
185
|
-
let newNode;
|
|
186
|
-
if (newExponent === 0) {
|
|
187
|
-
// x^1/x = x^0 = 1
|
|
188
|
-
newNode = SimplificationEngine.createConstant(1, fontSize);
|
|
189
|
-
} else if (newExponent === 1) {
|
|
190
|
-
// x^2/x = x^1 = x
|
|
191
|
-
newNode = baseNode.clone();
|
|
192
|
-
} else {
|
|
193
|
-
// x^n/x = x^(n-1)
|
|
194
|
-
newNode = utils.createPowerTerm(baseNode.clone(), newExponent, fontSize);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
// Preserve provenance
|
|
198
|
-
newNode.provenance.push(powerNode.id);
|
|
199
|
-
newNode.provenance.push(denominatorNode.id);
|
|
200
|
-
newNode.provenance.push(node.id);
|
|
201
|
-
|
|
202
|
-
return newNode;
|
|
203
|
-
},
|
|
204
|
-
(originalNode, ruleData, newNode) => {
|
|
205
|
-
const { variable, originalExponent, newExponent } = ruleData;
|
|
206
|
-
|
|
207
|
-
if (newExponent === 0) {
|
|
208
|
-
return `Simplified power division: ${variable}^${originalExponent}/${variable} = 1`;
|
|
209
|
-
} else if (newExponent === 1) {
|
|
210
|
-
return `Simplified power division: ${variable}^${originalExponent}/${variable} = ${variable}`;
|
|
211
|
-
} else {
|
|
212
|
-
return `Simplified power division: ${variable}^${originalExponent}/${variable} = ${variable}^${newExponent}`;
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
),
|
|
216
|
-
|
|
217
|
-
// Simplify cx/x = c (monomial divided by its variable)
|
|
218
|
-
SimplificationEngine.createRule("Monomial Variable Division",
|
|
219
|
-
(node) => {
|
|
220
|
-
if (node.type !== 'omdRationalNode') {
|
|
221
|
-
return false;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
const numerator = SimplificationEngine.unwrapParentheses(node.numerator);
|
|
225
|
-
const denominator = SimplificationEngine.unwrapParentheses(node.denominator);
|
|
226
|
-
|
|
227
|
-
// Check if numerator is a monomial and denominator is the same variable
|
|
228
|
-
const monomialInfo = SimplificationEngine.isMonomial(numerator);
|
|
229
|
-
if (monomialInfo &&
|
|
230
|
-
SimplificationEngine.isType(denominator, 'omdVariableNode') &&
|
|
231
|
-
monomialInfo.variable === denominator.name &&
|
|
232
|
-
monomialInfo.power === 1) {
|
|
233
|
-
|
|
234
|
-
return {
|
|
235
|
-
coefficient: monomialInfo.coefficient,
|
|
236
|
-
variable: monomialInfo.variable,
|
|
237
|
-
numeratorNode: numerator,
|
|
238
|
-
denominatorNode: denominator
|
|
239
|
-
};
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
return false;
|
|
243
|
-
},
|
|
244
|
-
(node, data) => {
|
|
245
|
-
const { coefficient } = data;
|
|
246
|
-
const fontSize = node.getFontSize();
|
|
247
|
-
|
|
248
|
-
const newNode = SimplificationEngine.createConstant(coefficient, fontSize);
|
|
249
|
-
newNode.provenance.push(data.numeratorNode.id);
|
|
250
|
-
newNode.provenance.push(data.denominatorNode.id);
|
|
251
|
-
newNode.provenance.push(node.id);
|
|
252
|
-
|
|
253
|
-
return newNode;
|
|
254
|
-
},
|
|
255
|
-
(originalNode, ruleData, newNode) => {
|
|
256
|
-
const { coefficient, variable } = ruleData;
|
|
257
|
-
return `Simplified monomial division: ${coefficient}${variable}/${variable} = ${coefficient}`;
|
|
258
|
-
}
|
|
259
|
-
),
|
|
260
|
-
|
|
261
|
-
// Simplify fractions (e.g., 6/8 → 3/4, 4/2 → 2)
|
|
262
|
-
SimplificationEngine.createRule("Simplify Fraction",
|
|
263
|
-
(node) => {
|
|
264
|
-
if (node.type !== 'omdRationalNode') {
|
|
265
|
-
return false;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
const numerator = node.numerator;
|
|
269
|
-
const denominator = node.denominator;
|
|
270
|
-
|
|
271
|
-
if (!numerator.isConstant() || !denominator.isConstant()) {
|
|
272
|
-
return false;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
const num = numerator.getValue();
|
|
276
|
-
const den = denominator.getValue();
|
|
277
|
-
|
|
278
|
-
if (den === 0) return false;
|
|
279
|
-
|
|
280
|
-
const gcd = utils.gcd(Math.abs(num), Math.abs(den));
|
|
281
|
-
|
|
282
|
-
// Check if we can simplify
|
|
283
|
-
if (gcd > 1 || den < 0) {
|
|
284
|
-
return {
|
|
285
|
-
originalNum: num,
|
|
286
|
-
originalDen: den,
|
|
287
|
-
gcd: gcd,
|
|
288
|
-
simplifiedNum: den < 0 ? -num / gcd : num / gcd,
|
|
289
|
-
simplifiedDen: Math.abs(den) / gcd
|
|
290
|
-
};
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
return false;
|
|
294
|
-
},
|
|
295
|
-
(node, data) => {
|
|
296
|
-
const { simplifiedNum, simplifiedDen } = data;
|
|
297
|
-
|
|
298
|
-
if (simplifiedDen === 1) {
|
|
299
|
-
// Fraction reduces to whole number
|
|
300
|
-
const newNode = SimplificationEngine.createConstant(simplifiedNum, node.getFontSize(), node.numerator, node.denominator);
|
|
301
|
-
newNode.provenance.push(node.id);
|
|
302
|
-
return newNode;
|
|
303
|
-
} else {
|
|
304
|
-
// Create simplified fraction
|
|
305
|
-
const newNode = SimplificationEngine.rational(simplifiedNum, simplifiedDen, node.getFontSize());
|
|
306
|
-
_preserveComponentProvenance(newNode, node.numerator, node.denominator);
|
|
307
|
-
newNode.provenance.push(node.id);
|
|
308
|
-
return newNode;
|
|
309
|
-
}
|
|
310
|
-
},
|
|
311
|
-
(originalNode, ruleData, newNode) => {
|
|
312
|
-
const { originalNum, originalDen, simplifiedNum, simplifiedDen, gcd } = ruleData;
|
|
313
|
-
|
|
314
|
-
if (originalDen < 0 && gcd > 1) {
|
|
315
|
-
return `Simplified fraction: ${originalNum}/${originalDen} = ${simplifiedNum}/${simplifiedDen} (corrected sign and reduced by GCD ${gcd})`;
|
|
316
|
-
} else if (simplifiedDen === 1) {
|
|
317
|
-
return `Simplified fraction to whole number: ${originalNum}/${originalDen} = ${simplifiedNum}`;
|
|
318
|
-
} else if (gcd > 1) {
|
|
319
|
-
return `Simplified fraction: ${originalNum}/${originalDen} = ${simplifiedNum}/${simplifiedDen} (reduced by GCD ${gcd})`;
|
|
320
|
-
} else {
|
|
321
|
-
return `Corrected fraction sign: ${originalNum}/${originalDen} = ${simplifiedNum}/${simplifiedDen}`;
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
),
|
|
325
|
-
|
|
326
|
-
// Multiplication in numerator over constant denominator ((4x)/3 → 4/3*x)
|
|
327
|
-
SimplificationEngine.createRule("Simplify Multiplication in Rational",
|
|
328
|
-
(node) => {
|
|
329
|
-
let numerator = node.numerator;
|
|
330
|
-
if (!node.denominator.isConstant()) return false;
|
|
331
|
-
const denominator = node.denominator.getValue();
|
|
332
|
-
|
|
333
|
-
if (denominator === 0) return false;
|
|
334
|
-
|
|
335
|
-
// Unwrap parentheses and check for multiplication
|
|
336
|
-
numerator = SimplificationEngine.unwrapParentheses(numerator);
|
|
337
|
-
if (!SimplificationEngine.isBinaryOp(numerator, 'multiply')) return false;
|
|
338
|
-
|
|
339
|
-
const constOp = SimplificationEngine.hasConstantOperand(numerator);
|
|
340
|
-
if (!constOp) return false;
|
|
341
|
-
|
|
342
|
-
const numeratorCoeff = constOp.constant.getValue();
|
|
343
|
-
return {
|
|
344
|
-
numeratorCoeff: numeratorCoeff,
|
|
345
|
-
denominator: denominator,
|
|
346
|
-
expression: constOp.other
|
|
347
|
-
};
|
|
348
|
-
},
|
|
349
|
-
(node, data) => {
|
|
350
|
-
const { numeratorCoeff, denominator, expression } = data;
|
|
351
|
-
|
|
352
|
-
// Create rational coefficient: numeratorCoeff/denominator
|
|
353
|
-
const rationalCoeff = SimplificationEngine.rational(numeratorCoeff, denominator, node.getFontSize());
|
|
354
|
-
|
|
355
|
-
// Preserve lineage from the multiplication components
|
|
356
|
-
_preserveMultiplicationProvenance(rationalCoeff, node);
|
|
357
|
-
|
|
358
|
-
// Create multiplication: (numeratorCoeff/denominator) * expression
|
|
359
|
-
const newNode = SimplificationEngine.createBinaryOp(rationalCoeff, 'multiply', expression.clone(), node.getFontSize());
|
|
360
|
-
|
|
361
|
-
// The new expression node inherits from its direct components.
|
|
362
|
-
if (newNode.right) {
|
|
363
|
-
newNode.right.provenance.push(expression.id);
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
// Preserve lineage from the overall rational node
|
|
367
|
-
newNode.provenance.push(node.id);
|
|
368
|
-
|
|
369
|
-
return newNode;
|
|
370
|
-
},
|
|
371
|
-
(originalNode, ruleData, newNode) => {
|
|
372
|
-
const { numeratorCoeff, denominator } = ruleData;
|
|
373
|
-
|
|
374
|
-
const commonDivisor = utils.gcd(Math.abs(numeratorCoeff), Math.abs(denominator));
|
|
375
|
-
|
|
376
|
-
if (commonDivisor === 1) {
|
|
377
|
-
// This case is more about restructuring (e.g., (2x)/3 -> 2/3 * x), not cancellation.
|
|
378
|
-
return `Separated the coefficient from the variable, changing the fraction into a multiplication.`;
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
const simplifiedNum = numeratorCoeff / commonDivisor;
|
|
382
|
-
const simplifiedDen = denominator / commonDivisor;
|
|
383
|
-
|
|
384
|
-
let message = `The numerator and denominator share a common factor of ${commonDivisor}. `;
|
|
385
|
-
message += `Dividing both by ${commonDivisor} (${numeratorCoeff} ÷ ${commonDivisor} = ${simplifiedNum}, and ${denominator} ÷ ${commonDivisor} = ${simplifiedDen}) simplifies the fraction.`;
|
|
386
|
-
|
|
387
|
-
return message;
|
|
388
|
-
},
|
|
389
|
-
'rational'
|
|
390
|
-
),
|
|
391
|
-
|
|
392
|
-
// Cancel numeric coefficient when numerator is unary-negative monomial and denominator is negative constant
|
|
393
|
-
SimplificationEngine.createRule("Cancel Negative Coefficient",
|
|
394
|
-
(node) => {
|
|
395
|
-
if (node.type !== 'omdRationalNode') return false;
|
|
396
|
-
|
|
397
|
-
const numerator = SimplificationEngine.unwrapParentheses(node.numerator);
|
|
398
|
-
const denominator = SimplificationEngine.unwrapParentheses(node.denominator);
|
|
399
|
-
|
|
400
|
-
// We expect numerator to be unary minus wrapping a multiplication (e.g., -(2*x) )
|
|
401
|
-
if (!SimplificationEngine.isType(numerator, 'omdUnaryExpressionNode')) return false;
|
|
402
|
-
const inner = numerator.argument || numerator.operand;
|
|
403
|
-
if (!SimplificationEngine.isBinaryOp(inner, 'multiply')) return false;
|
|
404
|
-
|
|
405
|
-
// Find the constant operand inside the multiplication
|
|
406
|
-
const constOp = SimplificationEngine.hasConstantOperand(inner);
|
|
407
|
-
if (!constOp || !constOp.constant.isConstant()) return false;
|
|
408
|
-
|
|
409
|
-
// Denominator should be a constant and negative
|
|
410
|
-
if (!denominator.isConstant()) return false;
|
|
411
|
-
const denVal = denominator.getValue();
|
|
412
|
-
if (denVal >= 0) return false;
|
|
413
|
-
|
|
414
|
-
const coeffVal = constOp.constant.getValue();
|
|
415
|
-
|
|
416
|
-
// If absolute values match, we can cancel the coefficient
|
|
417
|
-
if (Math.abs(coeffVal) === Math.abs(denVal)) {
|
|
418
|
-
return {
|
|
419
|
-
innerMultiplication: inner,
|
|
420
|
-
otherFactor: constOp.other,
|
|
421
|
-
numeratorUnary: numerator,
|
|
422
|
-
denominatorNode: denominator
|
|
423
|
-
};
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
return false;
|
|
427
|
-
},
|
|
428
|
-
(node, data) => {
|
|
429
|
-
// Return the other factor (e.g., x) possibly adjusting sign if needed
|
|
430
|
-
const newNode = data.otherFactor.clone();
|
|
431
|
-
// Preserve granular provenance from the multiplication and the rational node
|
|
432
|
-
try { utils.applyProvenance(newNode, data.innerMultiplication || node.numerator, node.denominator, node); }
|
|
433
|
-
catch (e) {
|
|
434
|
-
newNode.provenance = newNode.provenance || [];
|
|
435
|
-
newNode.provenance.push(data.numeratorUnary.id);
|
|
436
|
-
newNode.provenance.push(data.denominatorNode.id);
|
|
437
|
-
newNode.provenance.push(node.id);
|
|
438
|
-
}
|
|
439
|
-
return newNode;
|
|
440
|
-
},
|
|
441
|
-
(originalNode, ruleData, newNode) => {
|
|
442
|
-
return `Canceled matching numeric factors between numerator and denominator`;
|
|
443
|
-
},
|
|
444
|
-
'rational'
|
|
445
|
-
),
|
|
446
|
-
|
|
447
|
-
// Handle negative constant numerator over multiplication with negative constant in denominator: (-a)/(-b * rest) -> a/(b * rest)
|
|
448
|
-
SimplificationEngine.createRule("Negative Constant Over Negative Product",
|
|
449
|
-
(node) => {
|
|
450
|
-
if (node.type !== 'omdRationalNode') return false;
|
|
451
|
-
|
|
452
|
-
const numerator = SimplificationEngine.unwrapParentheses(node.numerator);
|
|
453
|
-
const denominator = SimplificationEngine.unwrapParentheses(node.denominator);
|
|
454
|
-
|
|
455
|
-
if (!numerator.isConstant || !numerator.isConstant()) return false;
|
|
456
|
-
const numVal = numerator.getValue();
|
|
457
|
-
if (numVal >= 0) return false;
|
|
458
|
-
|
|
459
|
-
// Denominator must be a multiplication with a constant operand that's negative
|
|
460
|
-
if (!SimplificationEngine.isBinaryOp(denominator, 'multiply')) return false;
|
|
461
|
-
const constOp = SimplificationEngine.hasConstantOperand(denominator);
|
|
462
|
-
if (!constOp || !constOp.constant.isConstant()) return false;
|
|
463
|
-
const denConstVal = constOp.constant.getValue();
|
|
464
|
-
if (denConstVal >= 0) return false;
|
|
465
|
-
|
|
466
|
-
return {
|
|
467
|
-
numeratorNode: numerator,
|
|
468
|
-
denominatorNode: denominator,
|
|
469
|
-
denominatorConst: constOp.constant,
|
|
470
|
-
denominatorOther: constOp.other
|
|
471
|
-
};
|
|
472
|
-
},
|
|
473
|
-
(node, data) => {
|
|
474
|
-
const fontSize = node.getFontSize();
|
|
475
|
-
|
|
476
|
-
// Build new numerator absolute value
|
|
477
|
-
const newNumConst = SimplificationEngine.createConstant(Math.abs(data.numeratorNode.getValue()), fontSize);
|
|
478
|
-
|
|
479
|
-
// Build new denominator as (abs(denConst) * other)
|
|
480
|
-
const newDenConst = SimplificationEngine.createConstant(Math.abs(data.denominatorConst.getValue()), fontSize);
|
|
481
|
-
const newDenProduct = SimplificationEngine.createBinaryOp(newDenConst, 'multiply', data.denominatorOther.clone(), fontSize);
|
|
482
|
-
|
|
483
|
-
// Construct rational AST
|
|
484
|
-
const ast = {
|
|
485
|
-
type: 'OperatorNode', op: '/', fn: 'divide',
|
|
486
|
-
args: [newNumConst.toMathJSNode(), newDenProduct.toMathJSNode()],
|
|
487
|
-
clone: function() { return { ...this, args: this.args.map(a => a.clone()) }; }
|
|
488
|
-
};
|
|
489
|
-
|
|
490
|
-
const rational = new omdRationalNode(ast);
|
|
491
|
-
rational.setFontSize(fontSize);
|
|
492
|
-
rational.initialize();
|
|
493
|
-
|
|
494
|
-
// Debug: show matching components and their provenance
|
|
495
|
-
|
|
496
|
-
// Preserve provenance from original components and whole node.
|
|
497
|
-
// Attach granular provenance to numerator and denominator children as well as the rational node.
|
|
498
|
-
try {
|
|
499
|
-
// Numerator: if original numerator was a unary expression, use its inner operand (the constant '1')
|
|
500
|
-
const numeratorSource = (data && data.numeratorNode && (data.numeratorNode.argument || data.numeratorNode.operand)) || node.numerator;
|
|
501
|
-
if (rational.numerator && numeratorSource) {
|
|
502
|
-
utils.applyProvenance(rational.numerator, numeratorSource);
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
// Denominator: if original denominator was a multiplication, attach provenance to the left/right children
|
|
506
|
-
const denomSourceBinary = (data && data.denominatorNode) || node.denominator;
|
|
507
|
-
if (rational.denominator && SimplificationEngine.isBinaryOp(denomSourceBinary, 'multiply') && SimplificationEngine.isBinaryOp(rational.denominator, 'multiply')) {
|
|
508
|
-
// Attempt to map original const -> left, other -> right
|
|
509
|
-
const leftSource = (data && data.denominatorConst) || (denomSourceBinary.left || denomSourceBinary.right);
|
|
510
|
-
const rightSource = (data && data.denominatorOther) || ((denomSourceBinary.left === leftSource) ? denomSourceBinary.right : denomSourceBinary.left);
|
|
511
|
-
|
|
512
|
-
if (rational.denominator.left && leftSource) utils.applyProvenance(rational.denominator.left, leftSource);
|
|
513
|
-
if (rational.denominator.right && rightSource) utils.applyProvenance(rational.denominator.right, rightSource);
|
|
514
|
-
} else if (rational.denominator) {
|
|
515
|
-
// Fallback: attach provenance to the whole denominator
|
|
516
|
-
utils.applyProvenance(rational.denominator, node.denominator);
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
// Finally, attach provenance to the rational node itself
|
|
520
|
-
utils.applyProvenance(rational, node.numerator, node.denominator, node);
|
|
521
|
-
} catch (e) {
|
|
522
|
-
// Fallback to copying provenance arrays/ids
|
|
523
|
-
_preserveComponentProvenance(rational, node.numerator, node.denominator);
|
|
524
|
-
if (!rational.provenance.includes(node.id)) rational.provenance.push(node.id);
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
// (debug logs removed)
|
|
528
|
-
|
|
529
|
-
return rational;
|
|
530
|
-
},
|
|
531
|
-
(originalNode, ruleData, newNode) => `Canceled matching negative factors: simplified sign and magnitude`,
|
|
532
|
-
'rational'
|
|
533
|
-
),
|
|
534
|
-
|
|
535
|
-
// Distribute division over addition/subtraction ((4*x+6)/3 → 4/3*x + 2)
|
|
536
|
-
SimplificationEngine.createRule("Simplify Rational Division",
|
|
537
|
-
(node) => {
|
|
538
|
-
if (node.type !== 'omdRationalNode') {
|
|
539
|
-
return false;
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
let numerator = node.numerator;
|
|
543
|
-
const denominator = node.denominator;
|
|
544
|
-
|
|
545
|
-
if (!denominator.isConstant()) {
|
|
546
|
-
return false;
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
// Unwrap parentheses to check the underlying structure
|
|
550
|
-
const originalNumerator = numerator;
|
|
551
|
-
numerator = SimplificationEngine.unwrapParentheses(numerator);
|
|
552
|
-
|
|
553
|
-
if (!SimplificationEngine.isBinaryOp(numerator, 'add') &&
|
|
554
|
-
!SimplificationEngine.isBinaryOp(numerator, 'subtract')) {
|
|
555
|
-
return false;
|
|
556
|
-
}
|
|
557
|
-
|
|
558
|
-
// Flatten the sum in the numerator
|
|
559
|
-
const terms = [];
|
|
560
|
-
utils.flattenSum(numerator, terms);
|
|
561
|
-
|
|
562
|
-
return {
|
|
563
|
-
terms: terms,
|
|
564
|
-
denominator: denominator.getValue()
|
|
565
|
-
};
|
|
566
|
-
},
|
|
567
|
-
(node, data) => {
|
|
568
|
-
const { terms, denominator } = data;
|
|
569
|
-
const fontSize = node.getFontSize();
|
|
570
|
-
|
|
571
|
-
const distributedTerms = terms.map(term => {
|
|
572
|
-
const termValue = term.node.isConstant() ? term.node.getValue() : 1;
|
|
573
|
-
const numeratorValue = termValue * term.sign;
|
|
574
|
-
|
|
575
|
-
if (term.node.isConstant()) {
|
|
576
|
-
// For constants, create a simplified fraction or integer
|
|
577
|
-
const gcd = utils.gcd(Math.abs(numeratorValue), Math.abs(denominator));
|
|
578
|
-
const simplifiedNum = numeratorValue / gcd;
|
|
579
|
-
const simplifiedDen = denominator / gcd;
|
|
580
|
-
|
|
581
|
-
if (simplifiedDen === 1) {
|
|
582
|
-
const constantNode = SimplificationEngine.createConstant(simplifiedNum, fontSize);
|
|
583
|
-
_preserveDistributionProvenance(constantNode, term.node, node.denominator);
|
|
584
|
-
return { node: constantNode, sign: 1 };
|
|
585
|
-
} else {
|
|
586
|
-
const rationalNode = SimplificationEngine.rational(Math.abs(simplifiedNum), simplifiedDen, fontSize);
|
|
587
|
-
_preserveDistributionProvenance(rationalNode, term.node, node.denominator);
|
|
588
|
-
return { node: rationalNode, sign: simplifiedNum >= 0 ? 1 : -1 };
|
|
589
|
-
}
|
|
590
|
-
} else {
|
|
591
|
-
// For non-constants, create coefficient * term / denominator
|
|
592
|
-
if (numeratorValue === denominator) {
|
|
593
|
-
// Coefficient cancels with denominator
|
|
594
|
-
const newNode = term.node.clone();
|
|
595
|
-
newNode.provenance.push(term.node.id);
|
|
596
|
-
return { node: newNode, sign: 1 };
|
|
597
|
-
} else {
|
|
598
|
-
const rationalCoeff = SimplificationEngine.rational(Math.abs(numeratorValue), denominator, fontSize);
|
|
599
|
-
const multiplicationNode = SimplificationEngine.createBinaryOp(rationalCoeff, 'multiply', term.node.clone(), fontSize);
|
|
600
|
-
|
|
601
|
-
_preserveDistributionProvenance(rationalCoeff, term.node, node.denominator);
|
|
602
|
-
multiplicationNode.provenance.push(node.id);
|
|
603
|
-
|
|
604
|
-
return { node: multiplicationNode, sign: numeratorValue >= 0 ? 1 : -1 };
|
|
605
|
-
}
|
|
606
|
-
}
|
|
607
|
-
});
|
|
608
|
-
|
|
609
|
-
return utils.buildSumTree(distributedTerms, fontSize);
|
|
610
|
-
},
|
|
611
|
-
(originalNode, ruleData, newNode) => {
|
|
612
|
-
const { terms, denominator } = ruleData;
|
|
613
|
-
const numeratorStr = utils.nodeToString(originalNode.numerator);
|
|
614
|
-
const simplifiedTerms = terms.map(term => {
|
|
615
|
-
const coeff = (term.node.isConstant() ? term.node.getValue() : 1) * term.sign;
|
|
616
|
-
const gcd = utils.gcd(Math.abs(coeff), Math.abs(denominator));
|
|
617
|
-
const newNum = coeff / gcd;
|
|
618
|
-
const newDen = denominator / gcd;
|
|
619
|
-
const variablePart = term.node.isConstant() ? '' : utils.nodeToString(term.node);
|
|
620
|
-
if (newDen === 1) return `${newNum}${variablePart}`;
|
|
621
|
-
return `(${newNum}/${newDen})${variablePart}`;
|
|
622
|
-
}).join(' + ');
|
|
623
|
-
|
|
624
|
-
// Don't add extra parentheses if numerator already has them
|
|
625
|
-
const displayNumerator = numeratorStr.startsWith('(') && numeratorStr.endsWith(')') ?
|
|
626
|
-
numeratorStr : `(${numeratorStr})`;
|
|
627
|
-
return `Distributed division: ${displayNumerator}/${denominator} = ${simplifiedTerms}`;
|
|
628
|
-
},
|
|
629
|
-
'rational'
|
|
630
|
-
)
|
|
631
|
-
];
|
|
632
|
-
|
|
633
|
-
// ===== HELPER FUNCTIONS FOR PROVENANCE =====
|
|
634
|
-
// These helper functions consolidate the repetitive provenance logic
|
|
635
|
-
|
|
636
|
-
/**
|
|
637
|
-
* Preserves provenance from numerator and denominator components
|
|
638
|
-
*/
|
|
639
|
-
function _preserveComponentProvenance(newNode, numerator, denominator) {
|
|
640
|
-
if (numerator?.provenance) {
|
|
641
|
-
numerator.provenance.forEach(id => {
|
|
642
|
-
if (!newNode.provenance.includes(id)) newNode.provenance.push(id);
|
|
643
|
-
});
|
|
644
|
-
} else if (numerator) {
|
|
645
|
-
newNode.provenance.push(numerator.id);
|
|
646
|
-
}
|
|
647
|
-
|
|
648
|
-
if (denominator?.provenance) {
|
|
649
|
-
denominator.provenance.forEach(id => {
|
|
650
|
-
if (!newNode.provenance.includes(id)) newNode.provenance.push(id);
|
|
651
|
-
});
|
|
652
|
-
} else if (denominator) {
|
|
653
|
-
newNode.provenance.push(denominator.id);
|
|
654
|
-
}
|
|
655
|
-
}
|
|
656
|
-
|
|
657
|
-
/**
|
|
658
|
-
* Preserves provenance from multiplication components in rational nodes
|
|
659
|
-
*/
|
|
660
|
-
function _preserveMultiplicationProvenance(rationalCoeff, originalNode) {
|
|
661
|
-
// Handle numerator constant provenance
|
|
662
|
-
const numeratorConstant = originalNode.numerator?.left?.isConstant() ?
|
|
663
|
-
originalNode.numerator.left : originalNode.numerator?.right;
|
|
664
|
-
|
|
665
|
-
if (numeratorConstant?.provenance) {
|
|
666
|
-
numeratorConstant.provenance.forEach(id => {
|
|
667
|
-
if (!rationalCoeff.provenance.includes(id)) {
|
|
668
|
-
rationalCoeff.provenance.push(id);
|
|
669
|
-
}
|
|
670
|
-
});
|
|
671
|
-
} else if (numeratorConstant) {
|
|
672
|
-
rationalCoeff.provenance.push(numeratorConstant.id);
|
|
673
|
-
}
|
|
674
|
-
|
|
675
|
-
// Handle denominator provenance
|
|
676
|
-
if (originalNode.denominator?.provenance) {
|
|
677
|
-
originalNode.denominator.provenance.forEach(id => {
|
|
678
|
-
if (!rationalCoeff.provenance.includes(id)) {
|
|
679
|
-
rationalCoeff.provenance.push(id);
|
|
680
|
-
}
|
|
681
|
-
});
|
|
682
|
-
} else if (originalNode.denominator) {
|
|
683
|
-
rationalCoeff.provenance.push(originalNode.denominator.id);
|
|
684
|
-
}
|
|
685
|
-
}
|
|
686
|
-
|
|
687
|
-
/**
|
|
688
|
-
* Preserves provenance for distributed rational terms
|
|
689
|
-
*/
|
|
690
|
-
function _preserveDistributionProvenance(rationalNode, termNode, denominatorNode) {
|
|
691
|
-
// Numerator should preserve lineage from the original term
|
|
692
|
-
if (rationalNode.numerator && termNode) {
|
|
693
|
-
rationalNode.numerator.provenance = [];
|
|
694
|
-
|
|
695
|
-
if (termNode.provenance?.length > 0) {
|
|
696
|
-
termNode.provenance.forEach(id => {
|
|
697
|
-
rationalNode.numerator.provenance.push(id);
|
|
698
|
-
});
|
|
699
|
-
} else {
|
|
700
|
-
rationalNode.numerator.provenance.push(termNode.id);
|
|
701
|
-
}
|
|
702
|
-
}
|
|
703
|
-
|
|
704
|
-
// Denominator should preserve lineage from the original denominator
|
|
705
|
-
if (rationalNode.denominator && denominatorNode) {
|
|
706
|
-
rationalNode.denominator.provenance = [];
|
|
707
|
-
|
|
708
|
-
if (denominatorNode.provenance?.length > 0) {
|
|
709
|
-
denominatorNode.provenance.forEach(id => {
|
|
710
|
-
rationalNode.denominator.provenance.push(id);
|
|
711
|
-
});
|
|
712
|
-
} else {
|
|
713
|
-
rationalNode.denominator.provenance.push(denominatorNode.id);
|
|
714
|
-
}
|
|
715
|
-
}
|
|
716
|
-
|
|
717
|
-
// Preserve term's provenance on the rational node itself
|
|
718
|
-
if (termNode.provenance?.length > 0) {
|
|
719
|
-
termNode.provenance.forEach(id => {
|
|
720
|
-
if (!rationalNode.provenance.includes(id)) {
|
|
721
|
-
rationalNode.provenance.push(id);
|
|
722
|
-
}
|
|
723
|
-
});
|
|
724
|
-
}
|
|
725
|
-
}
|
|
1
|
+
import { SimplificationEngine } from '../omdSimplificationEngine.js';
|
|
2
|
+
import * as utils from '../simplificationUtils.js';
|
|
3
|
+
import { omdRationalNode } from '../../nodes/omdRationalNode.js';
|
|
4
|
+
|
|
5
|
+
// ===== RATIONAL NODE RULES =====
|
|
6
|
+
export const rationalRules = [
|
|
7
|
+
// Simplify when both numerator and denominator are unary negatives: (-a)/(-b) -> a/b
|
|
8
|
+
SimplificationEngine.createRule("Unary Minus Cancellation",
|
|
9
|
+
(node) => {
|
|
10
|
+
if (node.type !== 'omdRationalNode') return false;
|
|
11
|
+
|
|
12
|
+
const num = SimplificationEngine.unwrapParentheses(node.numerator);
|
|
13
|
+
const den = SimplificationEngine.unwrapParentheses(node.denominator);
|
|
14
|
+
|
|
15
|
+
if (SimplificationEngine.isType(num, 'omdUnaryExpressionNode') &&
|
|
16
|
+
SimplificationEngine.isType(den, 'omdUnaryExpressionNode')) {
|
|
17
|
+
// Both sides are unary; cancel the unary minus operators
|
|
18
|
+
return {
|
|
19
|
+
numeratorArg: num.argument || num.operand,
|
|
20
|
+
denominatorArg: den.argument || den.operand,
|
|
21
|
+
numeratorNode: num,
|
|
22
|
+
denominatorNode: den
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return false;
|
|
27
|
+
},
|
|
28
|
+
(node, data) => {
|
|
29
|
+
// Create a new rational node with arguments unwrapped
|
|
30
|
+
const fontSize = node.getFontSize();
|
|
31
|
+
const numArg = data.numeratorArg;
|
|
32
|
+
const denArg = data.denominatorArg;
|
|
33
|
+
|
|
34
|
+
// If the denominator argument is a constant 1, return the numerator directly (e.g., x/1 -> x)
|
|
35
|
+
if (denArg && typeof denArg.isConstant === 'function' && denArg.isConstant() && denArg.getValue() === 1) {
|
|
36
|
+
const result = numArg.clone();
|
|
37
|
+
// Preserve provenance from original nodes and the rational node
|
|
38
|
+
try { utils.applyProvenance(result, node.numerator, node.denominator, node); } catch (e) {
|
|
39
|
+
// Fallback to manual pushes if applyProvenance fails
|
|
40
|
+
result.provenance = result.provenance || [];
|
|
41
|
+
if (data.numeratorNode) result.provenance.push(data.numeratorNode.id);
|
|
42
|
+
if (data.denominatorNode) result.provenance.push(data.denominatorNode.id);
|
|
43
|
+
result.provenance.push(node.id);
|
|
44
|
+
}
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const newNum = numArg.clone();
|
|
49
|
+
const newDen = denArg.clone();
|
|
50
|
+
|
|
51
|
+
const ast = {
|
|
52
|
+
type: 'OperatorNode', op: '/', fn: 'divide',
|
|
53
|
+
args: [newNum.toMathJSNode(), newDen.toMathJSNode()],
|
|
54
|
+
clone: function() { return { ...this, args: this.args.map(a => a.clone()) }; }
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const rational = new omdRationalNode(ast);
|
|
58
|
+
rational.setFontSize(fontSize);
|
|
59
|
+
rational.initialize();
|
|
60
|
+
|
|
61
|
+
// Give granular provenance to numerator and denominator children
|
|
62
|
+
try {
|
|
63
|
+
// Attach provenance to the child numerator and denominator specifically
|
|
64
|
+
if (rational.numerator) utils.applyProvenance(rational.numerator, node.numerator);
|
|
65
|
+
if (rational.denominator) utils.applyProvenance(rational.denominator, node.denominator);
|
|
66
|
+
// Also attach provenance to the rational node itself
|
|
67
|
+
utils.applyProvenance(rational, node.numerator, node.denominator, node);
|
|
68
|
+
} catch (e) {
|
|
69
|
+
_preserveComponentProvenance(rational, data.numeratorNode.argument || data.numeratorNode.operand, data.denominatorNode.argument || data.denominatorNode.operand);
|
|
70
|
+
if (!rational.provenance.includes(node.id)) rational.provenance.push(node.id);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return rational;
|
|
74
|
+
},
|
|
75
|
+
(originalNode, ruleData, newNode) => {
|
|
76
|
+
return `Canceled unary negatives in fraction`;
|
|
77
|
+
},
|
|
78
|
+
'rational'
|
|
79
|
+
),
|
|
80
|
+
|
|
81
|
+
// Simplify when numerator is unary minus and denominator is constant -1: (-x)/-1 -> x
|
|
82
|
+
SimplificationEngine.createRule("Unary Numerator Divide By -1",
|
|
83
|
+
(node) => {
|
|
84
|
+
if (node.type !== 'omdRationalNode') return false;
|
|
85
|
+
const num = SimplificationEngine.unwrapParentheses(node.numerator);
|
|
86
|
+
const den = SimplificationEngine.unwrapParentheses(node.denominator);
|
|
87
|
+
|
|
88
|
+
if (SimplificationEngine.isType(num, 'omdUnaryExpressionNode') && den.isConstant && den.isConstant() && den.getValue() === -1) {
|
|
89
|
+
return { numeratorArg: num.argument || num.operand, numeratorNode: num, denominatorNode: den };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return false;
|
|
93
|
+
},
|
|
94
|
+
(node, data) => {
|
|
95
|
+
const newNode = data.numeratorArg.clone();
|
|
96
|
+
// Preserve granular provenance: numerator argument's provenance and the denominator and whole node
|
|
97
|
+
try {
|
|
98
|
+
// If the result is a leaf (variable/constant), ensure it gets provenance from numerator/denominator
|
|
99
|
+
utils.applyProvenance(newNode, node.numerator, node.denominator, node);
|
|
100
|
+
} catch (e) {
|
|
101
|
+
newNode.provenance = newNode.provenance || [];
|
|
102
|
+
if (data.numeratorNode) newNode.provenance.push(data.numeratorNode.id);
|
|
103
|
+
if (data.denominatorNode) newNode.provenance.push(data.denominatorNode.id);
|
|
104
|
+
newNode.provenance.push(node.id);
|
|
105
|
+
}
|
|
106
|
+
return newNode;
|
|
107
|
+
},
|
|
108
|
+
(originalNode, ruleData, newNode) => {
|
|
109
|
+
return `Divided by -1 cancels unary minus in numerator`;
|
|
110
|
+
},
|
|
111
|
+
'rational'
|
|
112
|
+
),
|
|
113
|
+
// Simplify x/x = 1 (variable divided by itself)
|
|
114
|
+
SimplificationEngine.createRule("Variable Self Division",
|
|
115
|
+
(node) => {
|
|
116
|
+
if (node.type !== 'omdRationalNode') {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const numerator = SimplificationEngine.unwrapParentheses(node.numerator);
|
|
121
|
+
const denominator = SimplificationEngine.unwrapParentheses(node.denominator);
|
|
122
|
+
|
|
123
|
+
// Check if both numerator and denominator are the same variable
|
|
124
|
+
if (SimplificationEngine.isType(numerator, 'omdVariableNode') &&
|
|
125
|
+
SimplificationEngine.isType(denominator, 'omdVariableNode') &&
|
|
126
|
+
numerator.name === denominator.name) {
|
|
127
|
+
return {
|
|
128
|
+
variable: numerator.name,
|
|
129
|
+
numeratorNode: numerator,
|
|
130
|
+
denominatorNode: denominator
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return false;
|
|
135
|
+
},
|
|
136
|
+
(node, data) => {
|
|
137
|
+
const newNode = SimplificationEngine.createConstant(1, node.getFontSize());
|
|
138
|
+
newNode.provenance.push(data.numeratorNode.id);
|
|
139
|
+
newNode.provenance.push(data.denominatorNode.id);
|
|
140
|
+
newNode.provenance.push(node.id);
|
|
141
|
+
return newNode;
|
|
142
|
+
},
|
|
143
|
+
(originalNode, ruleData, newNode) => {
|
|
144
|
+
const { variable } = ruleData;
|
|
145
|
+
return `Simplified variable self-division: ${variable}/${variable} = 1`;
|
|
146
|
+
}
|
|
147
|
+
),
|
|
148
|
+
|
|
149
|
+
// Simplify x^n/x = x^(n-1) (power divided by base)
|
|
150
|
+
SimplificationEngine.createRule("Power Base Division",
|
|
151
|
+
(node) => {
|
|
152
|
+
if (node.type !== 'omdRationalNode') {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const numerator = SimplificationEngine.unwrapParentheses(node.numerator);
|
|
157
|
+
const denominator = SimplificationEngine.unwrapParentheses(node.denominator);
|
|
158
|
+
|
|
159
|
+
// Check if numerator is a power and denominator is the same variable
|
|
160
|
+
if (SimplificationEngine.isType(numerator, 'omdPowerNode') &&
|
|
161
|
+
SimplificationEngine.isType(denominator, 'omdVariableNode') &&
|
|
162
|
+
SimplificationEngine.isType(numerator.base, 'omdVariableNode') &&
|
|
163
|
+
numerator.base.name === denominator.name &&
|
|
164
|
+
numerator.exponent.isConstant()) {
|
|
165
|
+
|
|
166
|
+
const exponent = numerator.exponent.getValue();
|
|
167
|
+
const newExponent = exponent - 1;
|
|
168
|
+
|
|
169
|
+
return {
|
|
170
|
+
variable: numerator.base.name,
|
|
171
|
+
originalExponent: exponent,
|
|
172
|
+
newExponent: newExponent,
|
|
173
|
+
baseNode: numerator.base,
|
|
174
|
+
powerNode: numerator,
|
|
175
|
+
denominatorNode: denominator
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return false;
|
|
180
|
+
},
|
|
181
|
+
(node, data) => {
|
|
182
|
+
const { variable, newExponent, baseNode, powerNode, denominatorNode } = data;
|
|
183
|
+
const fontSize = node.getFontSize();
|
|
184
|
+
|
|
185
|
+
let newNode;
|
|
186
|
+
if (newExponent === 0) {
|
|
187
|
+
// x^1/x = x^0 = 1
|
|
188
|
+
newNode = SimplificationEngine.createConstant(1, fontSize);
|
|
189
|
+
} else if (newExponent === 1) {
|
|
190
|
+
// x^2/x = x^1 = x
|
|
191
|
+
newNode = baseNode.clone();
|
|
192
|
+
} else {
|
|
193
|
+
// x^n/x = x^(n-1)
|
|
194
|
+
newNode = utils.createPowerTerm(baseNode.clone(), newExponent, fontSize);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Preserve provenance
|
|
198
|
+
newNode.provenance.push(powerNode.id);
|
|
199
|
+
newNode.provenance.push(denominatorNode.id);
|
|
200
|
+
newNode.provenance.push(node.id);
|
|
201
|
+
|
|
202
|
+
return newNode;
|
|
203
|
+
},
|
|
204
|
+
(originalNode, ruleData, newNode) => {
|
|
205
|
+
const { variable, originalExponent, newExponent } = ruleData;
|
|
206
|
+
|
|
207
|
+
if (newExponent === 0) {
|
|
208
|
+
return `Simplified power division: ${variable}^${originalExponent}/${variable} = 1`;
|
|
209
|
+
} else if (newExponent === 1) {
|
|
210
|
+
return `Simplified power division: ${variable}^${originalExponent}/${variable} = ${variable}`;
|
|
211
|
+
} else {
|
|
212
|
+
return `Simplified power division: ${variable}^${originalExponent}/${variable} = ${variable}^${newExponent}`;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
),
|
|
216
|
+
|
|
217
|
+
// Simplify cx/x = c (monomial divided by its variable)
|
|
218
|
+
SimplificationEngine.createRule("Monomial Variable Division",
|
|
219
|
+
(node) => {
|
|
220
|
+
if (node.type !== 'omdRationalNode') {
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const numerator = SimplificationEngine.unwrapParentheses(node.numerator);
|
|
225
|
+
const denominator = SimplificationEngine.unwrapParentheses(node.denominator);
|
|
226
|
+
|
|
227
|
+
// Check if numerator is a monomial and denominator is the same variable
|
|
228
|
+
const monomialInfo = SimplificationEngine.isMonomial(numerator);
|
|
229
|
+
if (monomialInfo &&
|
|
230
|
+
SimplificationEngine.isType(denominator, 'omdVariableNode') &&
|
|
231
|
+
monomialInfo.variable === denominator.name &&
|
|
232
|
+
monomialInfo.power === 1) {
|
|
233
|
+
|
|
234
|
+
return {
|
|
235
|
+
coefficient: monomialInfo.coefficient,
|
|
236
|
+
variable: monomialInfo.variable,
|
|
237
|
+
numeratorNode: numerator,
|
|
238
|
+
denominatorNode: denominator
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return false;
|
|
243
|
+
},
|
|
244
|
+
(node, data) => {
|
|
245
|
+
const { coefficient } = data;
|
|
246
|
+
const fontSize = node.getFontSize();
|
|
247
|
+
|
|
248
|
+
const newNode = SimplificationEngine.createConstant(coefficient, fontSize);
|
|
249
|
+
newNode.provenance.push(data.numeratorNode.id);
|
|
250
|
+
newNode.provenance.push(data.denominatorNode.id);
|
|
251
|
+
newNode.provenance.push(node.id);
|
|
252
|
+
|
|
253
|
+
return newNode;
|
|
254
|
+
},
|
|
255
|
+
(originalNode, ruleData, newNode) => {
|
|
256
|
+
const { coefficient, variable } = ruleData;
|
|
257
|
+
return `Simplified monomial division: ${coefficient}${variable}/${variable} = ${coefficient}`;
|
|
258
|
+
}
|
|
259
|
+
),
|
|
260
|
+
|
|
261
|
+
// Simplify fractions (e.g., 6/8 → 3/4, 4/2 → 2)
|
|
262
|
+
SimplificationEngine.createRule("Simplify Fraction",
|
|
263
|
+
(node) => {
|
|
264
|
+
if (node.type !== 'omdRationalNode') {
|
|
265
|
+
return false;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const numerator = node.numerator;
|
|
269
|
+
const denominator = node.denominator;
|
|
270
|
+
|
|
271
|
+
if (!numerator.isConstant() || !denominator.isConstant()) {
|
|
272
|
+
return false;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const num = numerator.getValue();
|
|
276
|
+
const den = denominator.getValue();
|
|
277
|
+
|
|
278
|
+
if (den === 0) return false;
|
|
279
|
+
|
|
280
|
+
const gcd = utils.gcd(Math.abs(num), Math.abs(den));
|
|
281
|
+
|
|
282
|
+
// Check if we can simplify
|
|
283
|
+
if (gcd > 1 || den < 0) {
|
|
284
|
+
return {
|
|
285
|
+
originalNum: num,
|
|
286
|
+
originalDen: den,
|
|
287
|
+
gcd: gcd,
|
|
288
|
+
simplifiedNum: den < 0 ? -num / gcd : num / gcd,
|
|
289
|
+
simplifiedDen: Math.abs(den) / gcd
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
return false;
|
|
294
|
+
},
|
|
295
|
+
(node, data) => {
|
|
296
|
+
const { simplifiedNum, simplifiedDen } = data;
|
|
297
|
+
|
|
298
|
+
if (simplifiedDen === 1) {
|
|
299
|
+
// Fraction reduces to whole number
|
|
300
|
+
const newNode = SimplificationEngine.createConstant(simplifiedNum, node.getFontSize(), node.numerator, node.denominator);
|
|
301
|
+
newNode.provenance.push(node.id);
|
|
302
|
+
return newNode;
|
|
303
|
+
} else {
|
|
304
|
+
// Create simplified fraction
|
|
305
|
+
const newNode = SimplificationEngine.rational(simplifiedNum, simplifiedDen, node.getFontSize());
|
|
306
|
+
_preserveComponentProvenance(newNode, node.numerator, node.denominator);
|
|
307
|
+
newNode.provenance.push(node.id);
|
|
308
|
+
return newNode;
|
|
309
|
+
}
|
|
310
|
+
},
|
|
311
|
+
(originalNode, ruleData, newNode) => {
|
|
312
|
+
const { originalNum, originalDen, simplifiedNum, simplifiedDen, gcd } = ruleData;
|
|
313
|
+
|
|
314
|
+
if (originalDen < 0 && gcd > 1) {
|
|
315
|
+
return `Simplified fraction: ${originalNum}/${originalDen} = ${simplifiedNum}/${simplifiedDen} (corrected sign and reduced by GCD ${gcd})`;
|
|
316
|
+
} else if (simplifiedDen === 1) {
|
|
317
|
+
return `Simplified fraction to whole number: ${originalNum}/${originalDen} = ${simplifiedNum}`;
|
|
318
|
+
} else if (gcd > 1) {
|
|
319
|
+
return `Simplified fraction: ${originalNum}/${originalDen} = ${simplifiedNum}/${simplifiedDen} (reduced by GCD ${gcd})`;
|
|
320
|
+
} else {
|
|
321
|
+
return `Corrected fraction sign: ${originalNum}/${originalDen} = ${simplifiedNum}/${simplifiedDen}`;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
),
|
|
325
|
+
|
|
326
|
+
// Multiplication in numerator over constant denominator ((4x)/3 → 4/3*x)
|
|
327
|
+
SimplificationEngine.createRule("Simplify Multiplication in Rational",
|
|
328
|
+
(node) => {
|
|
329
|
+
let numerator = node.numerator;
|
|
330
|
+
if (!node.denominator.isConstant()) return false;
|
|
331
|
+
const denominator = node.denominator.getValue();
|
|
332
|
+
|
|
333
|
+
if (denominator === 0) return false;
|
|
334
|
+
|
|
335
|
+
// Unwrap parentheses and check for multiplication
|
|
336
|
+
numerator = SimplificationEngine.unwrapParentheses(numerator);
|
|
337
|
+
if (!SimplificationEngine.isBinaryOp(numerator, 'multiply')) return false;
|
|
338
|
+
|
|
339
|
+
const constOp = SimplificationEngine.hasConstantOperand(numerator);
|
|
340
|
+
if (!constOp) return false;
|
|
341
|
+
|
|
342
|
+
const numeratorCoeff = constOp.constant.getValue();
|
|
343
|
+
return {
|
|
344
|
+
numeratorCoeff: numeratorCoeff,
|
|
345
|
+
denominator: denominator,
|
|
346
|
+
expression: constOp.other
|
|
347
|
+
};
|
|
348
|
+
},
|
|
349
|
+
(node, data) => {
|
|
350
|
+
const { numeratorCoeff, denominator, expression } = data;
|
|
351
|
+
|
|
352
|
+
// Create rational coefficient: numeratorCoeff/denominator
|
|
353
|
+
const rationalCoeff = SimplificationEngine.rational(numeratorCoeff, denominator, node.getFontSize());
|
|
354
|
+
|
|
355
|
+
// Preserve lineage from the multiplication components
|
|
356
|
+
_preserveMultiplicationProvenance(rationalCoeff, node);
|
|
357
|
+
|
|
358
|
+
// Create multiplication: (numeratorCoeff/denominator) * expression
|
|
359
|
+
const newNode = SimplificationEngine.createBinaryOp(rationalCoeff, 'multiply', expression.clone(), node.getFontSize());
|
|
360
|
+
|
|
361
|
+
// The new expression node inherits from its direct components.
|
|
362
|
+
if (newNode.right) {
|
|
363
|
+
newNode.right.provenance.push(expression.id);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// Preserve lineage from the overall rational node
|
|
367
|
+
newNode.provenance.push(node.id);
|
|
368
|
+
|
|
369
|
+
return newNode;
|
|
370
|
+
},
|
|
371
|
+
(originalNode, ruleData, newNode) => {
|
|
372
|
+
const { numeratorCoeff, denominator } = ruleData;
|
|
373
|
+
|
|
374
|
+
const commonDivisor = utils.gcd(Math.abs(numeratorCoeff), Math.abs(denominator));
|
|
375
|
+
|
|
376
|
+
if (commonDivisor === 1) {
|
|
377
|
+
// This case is more about restructuring (e.g., (2x)/3 -> 2/3 * x), not cancellation.
|
|
378
|
+
return `Separated the coefficient from the variable, changing the fraction into a multiplication.`;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
const simplifiedNum = numeratorCoeff / commonDivisor;
|
|
382
|
+
const simplifiedDen = denominator / commonDivisor;
|
|
383
|
+
|
|
384
|
+
let message = `The numerator and denominator share a common factor of ${commonDivisor}. `;
|
|
385
|
+
message += `Dividing both by ${commonDivisor} (${numeratorCoeff} ÷ ${commonDivisor} = ${simplifiedNum}, and ${denominator} ÷ ${commonDivisor} = ${simplifiedDen}) simplifies the fraction.`;
|
|
386
|
+
|
|
387
|
+
return message;
|
|
388
|
+
},
|
|
389
|
+
'rational'
|
|
390
|
+
),
|
|
391
|
+
|
|
392
|
+
// Cancel numeric coefficient when numerator is unary-negative monomial and denominator is negative constant
|
|
393
|
+
SimplificationEngine.createRule("Cancel Negative Coefficient",
|
|
394
|
+
(node) => {
|
|
395
|
+
if (node.type !== 'omdRationalNode') return false;
|
|
396
|
+
|
|
397
|
+
const numerator = SimplificationEngine.unwrapParentheses(node.numerator);
|
|
398
|
+
const denominator = SimplificationEngine.unwrapParentheses(node.denominator);
|
|
399
|
+
|
|
400
|
+
// We expect numerator to be unary minus wrapping a multiplication (e.g., -(2*x) )
|
|
401
|
+
if (!SimplificationEngine.isType(numerator, 'omdUnaryExpressionNode')) return false;
|
|
402
|
+
const inner = numerator.argument || numerator.operand;
|
|
403
|
+
if (!SimplificationEngine.isBinaryOp(inner, 'multiply')) return false;
|
|
404
|
+
|
|
405
|
+
// Find the constant operand inside the multiplication
|
|
406
|
+
const constOp = SimplificationEngine.hasConstantOperand(inner);
|
|
407
|
+
if (!constOp || !constOp.constant.isConstant()) return false;
|
|
408
|
+
|
|
409
|
+
// Denominator should be a constant and negative
|
|
410
|
+
if (!denominator.isConstant()) return false;
|
|
411
|
+
const denVal = denominator.getValue();
|
|
412
|
+
if (denVal >= 0) return false;
|
|
413
|
+
|
|
414
|
+
const coeffVal = constOp.constant.getValue();
|
|
415
|
+
|
|
416
|
+
// If absolute values match, we can cancel the coefficient
|
|
417
|
+
if (Math.abs(coeffVal) === Math.abs(denVal)) {
|
|
418
|
+
return {
|
|
419
|
+
innerMultiplication: inner,
|
|
420
|
+
otherFactor: constOp.other,
|
|
421
|
+
numeratorUnary: numerator,
|
|
422
|
+
denominatorNode: denominator
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
return false;
|
|
427
|
+
},
|
|
428
|
+
(node, data) => {
|
|
429
|
+
// Return the other factor (e.g., x) possibly adjusting sign if needed
|
|
430
|
+
const newNode = data.otherFactor.clone();
|
|
431
|
+
// Preserve granular provenance from the multiplication and the rational node
|
|
432
|
+
try { utils.applyProvenance(newNode, data.innerMultiplication || node.numerator, node.denominator, node); }
|
|
433
|
+
catch (e) {
|
|
434
|
+
newNode.provenance = newNode.provenance || [];
|
|
435
|
+
newNode.provenance.push(data.numeratorUnary.id);
|
|
436
|
+
newNode.provenance.push(data.denominatorNode.id);
|
|
437
|
+
newNode.provenance.push(node.id);
|
|
438
|
+
}
|
|
439
|
+
return newNode;
|
|
440
|
+
},
|
|
441
|
+
(originalNode, ruleData, newNode) => {
|
|
442
|
+
return `Canceled matching numeric factors between numerator and denominator`;
|
|
443
|
+
},
|
|
444
|
+
'rational'
|
|
445
|
+
),
|
|
446
|
+
|
|
447
|
+
// Handle negative constant numerator over multiplication with negative constant in denominator: (-a)/(-b * rest) -> a/(b * rest)
|
|
448
|
+
SimplificationEngine.createRule("Negative Constant Over Negative Product",
|
|
449
|
+
(node) => {
|
|
450
|
+
if (node.type !== 'omdRationalNode') return false;
|
|
451
|
+
|
|
452
|
+
const numerator = SimplificationEngine.unwrapParentheses(node.numerator);
|
|
453
|
+
const denominator = SimplificationEngine.unwrapParentheses(node.denominator);
|
|
454
|
+
|
|
455
|
+
if (!numerator.isConstant || !numerator.isConstant()) return false;
|
|
456
|
+
const numVal = numerator.getValue();
|
|
457
|
+
if (numVal >= 0) return false;
|
|
458
|
+
|
|
459
|
+
// Denominator must be a multiplication with a constant operand that's negative
|
|
460
|
+
if (!SimplificationEngine.isBinaryOp(denominator, 'multiply')) return false;
|
|
461
|
+
const constOp = SimplificationEngine.hasConstantOperand(denominator);
|
|
462
|
+
if (!constOp || !constOp.constant.isConstant()) return false;
|
|
463
|
+
const denConstVal = constOp.constant.getValue();
|
|
464
|
+
if (denConstVal >= 0) return false;
|
|
465
|
+
|
|
466
|
+
return {
|
|
467
|
+
numeratorNode: numerator,
|
|
468
|
+
denominatorNode: denominator,
|
|
469
|
+
denominatorConst: constOp.constant,
|
|
470
|
+
denominatorOther: constOp.other
|
|
471
|
+
};
|
|
472
|
+
},
|
|
473
|
+
(node, data) => {
|
|
474
|
+
const fontSize = node.getFontSize();
|
|
475
|
+
|
|
476
|
+
// Build new numerator absolute value
|
|
477
|
+
const newNumConst = SimplificationEngine.createConstant(Math.abs(data.numeratorNode.getValue()), fontSize);
|
|
478
|
+
|
|
479
|
+
// Build new denominator as (abs(denConst) * other)
|
|
480
|
+
const newDenConst = SimplificationEngine.createConstant(Math.abs(data.denominatorConst.getValue()), fontSize);
|
|
481
|
+
const newDenProduct = SimplificationEngine.createBinaryOp(newDenConst, 'multiply', data.denominatorOther.clone(), fontSize);
|
|
482
|
+
|
|
483
|
+
// Construct rational AST
|
|
484
|
+
const ast = {
|
|
485
|
+
type: 'OperatorNode', op: '/', fn: 'divide',
|
|
486
|
+
args: [newNumConst.toMathJSNode(), newDenProduct.toMathJSNode()],
|
|
487
|
+
clone: function() { return { ...this, args: this.args.map(a => a.clone()) }; }
|
|
488
|
+
};
|
|
489
|
+
|
|
490
|
+
const rational = new omdRationalNode(ast);
|
|
491
|
+
rational.setFontSize(fontSize);
|
|
492
|
+
rational.initialize();
|
|
493
|
+
|
|
494
|
+
// Debug: show matching components and their provenance
|
|
495
|
+
|
|
496
|
+
// Preserve provenance from original components and whole node.
|
|
497
|
+
// Attach granular provenance to numerator and denominator children as well as the rational node.
|
|
498
|
+
try {
|
|
499
|
+
// Numerator: if original numerator was a unary expression, use its inner operand (the constant '1')
|
|
500
|
+
const numeratorSource = (data && data.numeratorNode && (data.numeratorNode.argument || data.numeratorNode.operand)) || node.numerator;
|
|
501
|
+
if (rational.numerator && numeratorSource) {
|
|
502
|
+
utils.applyProvenance(rational.numerator, numeratorSource);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
// Denominator: if original denominator was a multiplication, attach provenance to the left/right children
|
|
506
|
+
const denomSourceBinary = (data && data.denominatorNode) || node.denominator;
|
|
507
|
+
if (rational.denominator && SimplificationEngine.isBinaryOp(denomSourceBinary, 'multiply') && SimplificationEngine.isBinaryOp(rational.denominator, 'multiply')) {
|
|
508
|
+
// Attempt to map original const -> left, other -> right
|
|
509
|
+
const leftSource = (data && data.denominatorConst) || (denomSourceBinary.left || denomSourceBinary.right);
|
|
510
|
+
const rightSource = (data && data.denominatorOther) || ((denomSourceBinary.left === leftSource) ? denomSourceBinary.right : denomSourceBinary.left);
|
|
511
|
+
|
|
512
|
+
if (rational.denominator.left && leftSource) utils.applyProvenance(rational.denominator.left, leftSource);
|
|
513
|
+
if (rational.denominator.right && rightSource) utils.applyProvenance(rational.denominator.right, rightSource);
|
|
514
|
+
} else if (rational.denominator) {
|
|
515
|
+
// Fallback: attach provenance to the whole denominator
|
|
516
|
+
utils.applyProvenance(rational.denominator, node.denominator);
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
// Finally, attach provenance to the rational node itself
|
|
520
|
+
utils.applyProvenance(rational, node.numerator, node.denominator, node);
|
|
521
|
+
} catch (e) {
|
|
522
|
+
// Fallback to copying provenance arrays/ids
|
|
523
|
+
_preserveComponentProvenance(rational, node.numerator, node.denominator);
|
|
524
|
+
if (!rational.provenance.includes(node.id)) rational.provenance.push(node.id);
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
// (debug logs removed)
|
|
528
|
+
|
|
529
|
+
return rational;
|
|
530
|
+
},
|
|
531
|
+
(originalNode, ruleData, newNode) => `Canceled matching negative factors: simplified sign and magnitude`,
|
|
532
|
+
'rational'
|
|
533
|
+
),
|
|
534
|
+
|
|
535
|
+
// Distribute division over addition/subtraction ((4*x+6)/3 → 4/3*x + 2)
|
|
536
|
+
SimplificationEngine.createRule("Simplify Rational Division",
|
|
537
|
+
(node) => {
|
|
538
|
+
if (node.type !== 'omdRationalNode') {
|
|
539
|
+
return false;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
let numerator = node.numerator;
|
|
543
|
+
const denominator = node.denominator;
|
|
544
|
+
|
|
545
|
+
if (!denominator.isConstant()) {
|
|
546
|
+
return false;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
// Unwrap parentheses to check the underlying structure
|
|
550
|
+
const originalNumerator = numerator;
|
|
551
|
+
numerator = SimplificationEngine.unwrapParentheses(numerator);
|
|
552
|
+
|
|
553
|
+
if (!SimplificationEngine.isBinaryOp(numerator, 'add') &&
|
|
554
|
+
!SimplificationEngine.isBinaryOp(numerator, 'subtract')) {
|
|
555
|
+
return false;
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
// Flatten the sum in the numerator
|
|
559
|
+
const terms = [];
|
|
560
|
+
utils.flattenSum(numerator, terms);
|
|
561
|
+
|
|
562
|
+
return {
|
|
563
|
+
terms: terms,
|
|
564
|
+
denominator: denominator.getValue()
|
|
565
|
+
};
|
|
566
|
+
},
|
|
567
|
+
(node, data) => {
|
|
568
|
+
const { terms, denominator } = data;
|
|
569
|
+
const fontSize = node.getFontSize();
|
|
570
|
+
|
|
571
|
+
const distributedTerms = terms.map(term => {
|
|
572
|
+
const termValue = term.node.isConstant() ? term.node.getValue() : 1;
|
|
573
|
+
const numeratorValue = termValue * term.sign;
|
|
574
|
+
|
|
575
|
+
if (term.node.isConstant()) {
|
|
576
|
+
// For constants, create a simplified fraction or integer
|
|
577
|
+
const gcd = utils.gcd(Math.abs(numeratorValue), Math.abs(denominator));
|
|
578
|
+
const simplifiedNum = numeratorValue / gcd;
|
|
579
|
+
const simplifiedDen = denominator / gcd;
|
|
580
|
+
|
|
581
|
+
if (simplifiedDen === 1) {
|
|
582
|
+
const constantNode = SimplificationEngine.createConstant(simplifiedNum, fontSize);
|
|
583
|
+
_preserveDistributionProvenance(constantNode, term.node, node.denominator);
|
|
584
|
+
return { node: constantNode, sign: 1 };
|
|
585
|
+
} else {
|
|
586
|
+
const rationalNode = SimplificationEngine.rational(Math.abs(simplifiedNum), simplifiedDen, fontSize);
|
|
587
|
+
_preserveDistributionProvenance(rationalNode, term.node, node.denominator);
|
|
588
|
+
return { node: rationalNode, sign: simplifiedNum >= 0 ? 1 : -1 };
|
|
589
|
+
}
|
|
590
|
+
} else {
|
|
591
|
+
// For non-constants, create coefficient * term / denominator
|
|
592
|
+
if (numeratorValue === denominator) {
|
|
593
|
+
// Coefficient cancels with denominator
|
|
594
|
+
const newNode = term.node.clone();
|
|
595
|
+
newNode.provenance.push(term.node.id);
|
|
596
|
+
return { node: newNode, sign: 1 };
|
|
597
|
+
} else {
|
|
598
|
+
const rationalCoeff = SimplificationEngine.rational(Math.abs(numeratorValue), denominator, fontSize);
|
|
599
|
+
const multiplicationNode = SimplificationEngine.createBinaryOp(rationalCoeff, 'multiply', term.node.clone(), fontSize);
|
|
600
|
+
|
|
601
|
+
_preserveDistributionProvenance(rationalCoeff, term.node, node.denominator);
|
|
602
|
+
multiplicationNode.provenance.push(node.id);
|
|
603
|
+
|
|
604
|
+
return { node: multiplicationNode, sign: numeratorValue >= 0 ? 1 : -1 };
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
});
|
|
608
|
+
|
|
609
|
+
return utils.buildSumTree(distributedTerms, fontSize);
|
|
610
|
+
},
|
|
611
|
+
(originalNode, ruleData, newNode) => {
|
|
612
|
+
const { terms, denominator } = ruleData;
|
|
613
|
+
const numeratorStr = utils.nodeToString(originalNode.numerator);
|
|
614
|
+
const simplifiedTerms = terms.map(term => {
|
|
615
|
+
const coeff = (term.node.isConstant() ? term.node.getValue() : 1) * term.sign;
|
|
616
|
+
const gcd = utils.gcd(Math.abs(coeff), Math.abs(denominator));
|
|
617
|
+
const newNum = coeff / gcd;
|
|
618
|
+
const newDen = denominator / gcd;
|
|
619
|
+
const variablePart = term.node.isConstant() ? '' : utils.nodeToString(term.node);
|
|
620
|
+
if (newDen === 1) return `${newNum}${variablePart}`;
|
|
621
|
+
return `(${newNum}/${newDen})${variablePart}`;
|
|
622
|
+
}).join(' + ');
|
|
623
|
+
|
|
624
|
+
// Don't add extra parentheses if numerator already has them
|
|
625
|
+
const displayNumerator = numeratorStr.startsWith('(') && numeratorStr.endsWith(')') ?
|
|
626
|
+
numeratorStr : `(${numeratorStr})`;
|
|
627
|
+
return `Distributed division: ${displayNumerator}/${denominator} = ${simplifiedTerms}`;
|
|
628
|
+
},
|
|
629
|
+
'rational'
|
|
630
|
+
)
|
|
631
|
+
];
|
|
632
|
+
|
|
633
|
+
// ===== HELPER FUNCTIONS FOR PROVENANCE =====
|
|
634
|
+
// These helper functions consolidate the repetitive provenance logic
|
|
635
|
+
|
|
636
|
+
/**
|
|
637
|
+
* Preserves provenance from numerator and denominator components
|
|
638
|
+
*/
|
|
639
|
+
function _preserveComponentProvenance(newNode, numerator, denominator) {
|
|
640
|
+
if (numerator?.provenance) {
|
|
641
|
+
numerator.provenance.forEach(id => {
|
|
642
|
+
if (!newNode.provenance.includes(id)) newNode.provenance.push(id);
|
|
643
|
+
});
|
|
644
|
+
} else if (numerator) {
|
|
645
|
+
newNode.provenance.push(numerator.id);
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
if (denominator?.provenance) {
|
|
649
|
+
denominator.provenance.forEach(id => {
|
|
650
|
+
if (!newNode.provenance.includes(id)) newNode.provenance.push(id);
|
|
651
|
+
});
|
|
652
|
+
} else if (denominator) {
|
|
653
|
+
newNode.provenance.push(denominator.id);
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
/**
|
|
658
|
+
* Preserves provenance from multiplication components in rational nodes
|
|
659
|
+
*/
|
|
660
|
+
function _preserveMultiplicationProvenance(rationalCoeff, originalNode) {
|
|
661
|
+
// Handle numerator constant provenance
|
|
662
|
+
const numeratorConstant = originalNode.numerator?.left?.isConstant() ?
|
|
663
|
+
originalNode.numerator.left : originalNode.numerator?.right;
|
|
664
|
+
|
|
665
|
+
if (numeratorConstant?.provenance) {
|
|
666
|
+
numeratorConstant.provenance.forEach(id => {
|
|
667
|
+
if (!rationalCoeff.provenance.includes(id)) {
|
|
668
|
+
rationalCoeff.provenance.push(id);
|
|
669
|
+
}
|
|
670
|
+
});
|
|
671
|
+
} else if (numeratorConstant) {
|
|
672
|
+
rationalCoeff.provenance.push(numeratorConstant.id);
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
// Handle denominator provenance
|
|
676
|
+
if (originalNode.denominator?.provenance) {
|
|
677
|
+
originalNode.denominator.provenance.forEach(id => {
|
|
678
|
+
if (!rationalCoeff.provenance.includes(id)) {
|
|
679
|
+
rationalCoeff.provenance.push(id);
|
|
680
|
+
}
|
|
681
|
+
});
|
|
682
|
+
} else if (originalNode.denominator) {
|
|
683
|
+
rationalCoeff.provenance.push(originalNode.denominator.id);
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
/**
|
|
688
|
+
* Preserves provenance for distributed rational terms
|
|
689
|
+
*/
|
|
690
|
+
function _preserveDistributionProvenance(rationalNode, termNode, denominatorNode) {
|
|
691
|
+
// Numerator should preserve lineage from the original term
|
|
692
|
+
if (rationalNode.numerator && termNode) {
|
|
693
|
+
rationalNode.numerator.provenance = [];
|
|
694
|
+
|
|
695
|
+
if (termNode.provenance?.length > 0) {
|
|
696
|
+
termNode.provenance.forEach(id => {
|
|
697
|
+
rationalNode.numerator.provenance.push(id);
|
|
698
|
+
});
|
|
699
|
+
} else {
|
|
700
|
+
rationalNode.numerator.provenance.push(termNode.id);
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
// Denominator should preserve lineage from the original denominator
|
|
705
|
+
if (rationalNode.denominator && denominatorNode) {
|
|
706
|
+
rationalNode.denominator.provenance = [];
|
|
707
|
+
|
|
708
|
+
if (denominatorNode.provenance?.length > 0) {
|
|
709
|
+
denominatorNode.provenance.forEach(id => {
|
|
710
|
+
rationalNode.denominator.provenance.push(id);
|
|
711
|
+
});
|
|
712
|
+
} else {
|
|
713
|
+
rationalNode.denominator.provenance.push(denominatorNode.id);
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
// Preserve term's provenance on the rational node itself
|
|
718
|
+
if (termNode.provenance?.length > 0) {
|
|
719
|
+
termNode.provenance.forEach(id => {
|
|
720
|
+
if (!rationalNode.provenance.includes(id)) {
|
|
721
|
+
rationalNode.provenance.push(id);
|
|
722
|
+
}
|
|
723
|
+
});
|
|
724
|
+
}
|
|
725
|
+
}
|