@teachinglab/omd 0.6.1 → 0.6.3
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 +606 -547
- package/omd/core/omdUtilities.js +113 -113
- package/omd/display/omdDisplay.js +1045 -963
- 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 -57
- 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
package/docs/api/omdTreeDiff.md
CHANGED
|
@@ -1,170 +1,170 @@
|
|
|
1
|
-
# omdTreeDiff
|
|
2
|
-
|
|
3
|
-
Implements a robust tree differencing algorithm to identify changes between two `omdNode` expression trees. This module is crucial for `omdStepVisualizerHighlighting`, providing precise visual feedback on how mathematical expressions transform from one step to the next.
|
|
4
|
-
|
|
5
|
-
## Algorithm Overview
|
|
6
|
-
|
|
7
|
-
`omdTreeDiff` uses a multi-stage approach to compare two expression trees (`oldEquation` and `newEquation`):
|
|
8
|
-
|
|
9
|
-
1. **Special Cases**: It first attempts to identify common pedagogical patterns (like adding/subtracting the same value from both sides, or simple identity/double negative simplifications). If found, these specific changes are highlighted directly.
|
|
10
|
-
2. **Optimal Subtree Matching**: If no special cases apply, it proceeds to find the largest, non-overlapping set of matched subtrees between the old and new expressions. A match can be either structurally identical or semantically equivalent (e.g., `2+3` and `5`).
|
|
11
|
-
3. **Identify Changes**: Any parts of the `newEquation` that are *not* part of these optimal matches are considered the actual changes and are returned for highlighting.
|
|
12
|
-
4. **Educational Mode**: When enabled, the algorithm includes additional heuristics to highlight subtle simplifications that might not involve a direct structural change but are important for understanding the transformation (e.g., removing `+ 0`).
|
|
13
|
-
|
|
14
|
-
## Class Definition
|
|
15
|
-
|
|
16
|
-
```javascript
|
|
17
|
-
export class omdTreeDiff
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
This class is not meant to be instantiated. All its methods are static.
|
|
21
|
-
|
|
22
|
-
## Static Methods
|
|
23
|
-
|
|
24
|
-
### `findChangedNodes(oldEquation, newEquation, options)`
|
|
25
|
-
|
|
26
|
-
Main entry point for the tree differencing algorithm. Compares two `omdEquationNode` instances (or any `omdNode` subtrees) and returns a list of nodes in the `newEquation` that have changed or are new.
|
|
27
|
-
|
|
28
|
-
- **`oldEquation`** (`omdNode`): The root node of the previous expression tree.
|
|
29
|
-
- **`newEquation`** (`omdNode`): The root node of the current expression tree.
|
|
30
|
-
- **`options`** (`object`, optional): Configuration options:
|
|
31
|
-
- `educationalMode` (`boolean`): If `true`, the diff algorithm will also highlight mathematically neutral changes that are pedagogically significant (e.g., removing `+ 0`). Default: `false`.
|
|
32
|
-
- **Returns**: `Array<omdNode>` - An array of `omdNode` instances from `newEquation` that should be highlighted.
|
|
33
|
-
|
|
34
|
-
### `findEquationSpecialCases(oldEquation, newEquation)`
|
|
35
|
-
|
|
36
|
-
Identifies specific equation-level transformation patterns, such as adding/subtracting the same operation to both sides of an equation. This allows for more intuitive highlighting in common step-by-step solution scenarios.
|
|
37
|
-
|
|
38
|
-
- **`oldEquation`** (`omdEquationNode`): The previous equation.
|
|
39
|
-
- **`newEquation`** (`omdEquationNode`): The current equation.
|
|
40
|
-
- **Returns**: `Array<omdNode>` - An array of nodes to highlight for these special cases, or an empty array if no such pattern is found.
|
|
41
|
-
|
|
42
|
-
### `diffSubtrees(oldTree, newTree, educationalMode)`
|
|
43
|
-
|
|
44
|
-
Core recursive differencing method. It first checks for various pedagogical highlighting patterns (common prefix, variable preservation, type differences, subtraction patterns). If none apply, it falls back to finding an optimal matching between subtrees of the `oldTree` and `newTree`. Nodes in `newTree` that do not have a match in `oldTree` are considered changed.
|
|
45
|
-
|
|
46
|
-
- **`oldTree`** (`omdNode`): The root of the old subtree.
|
|
47
|
-
- **`newTree`** (`omdNode`): The root of the new subtree.
|
|
48
|
-
- **`educationalMode`** (`boolean`): Same as in `findChangedNodes`.
|
|
49
|
-
- **Returns**: `Array<omdNode>` - An array of unmatched leaf nodes in `newTree`.
|
|
50
|
-
|
|
51
|
-
### `findEducationalHighlights(oldTree, newTree, optimalMatches)`
|
|
52
|
-
|
|
53
|
-
When `educationalMode` is enabled, this method identifies additional nodes to highlight for pedagogical reasons, even if they don't represent a structural change. This includes cases like the removal of additive/multiplicative identities or double negations.
|
|
54
|
-
|
|
55
|
-
- **`oldTree`** (`omdNode`): The old expression tree.
|
|
56
|
-
- **`newTree`** (`omdNode`): The new expression tree.
|
|
57
|
-
- **`optimalMatches`** (`Array`): The list of optimally matched subtrees.
|
|
58
|
-
- **Returns**: `Array<omdNode>` - Additional nodes to highlight.
|
|
59
|
-
|
|
60
|
-
### `findAdditiveIdentityChanges(oldTree, newTree)`
|
|
61
|
-
|
|
62
|
-
Identifies changes related to the removal of additive identities (e.g., `+ 0` or `- 0`).
|
|
63
|
-
|
|
64
|
-
- **`oldTree`** (`omdNode`): The old expression tree.
|
|
65
|
-
- **`newTree`** (`omdNode`): The new expression tree.
|
|
66
|
-
- **Returns**: `Array<omdNode>` - Nodes to highlight for additive identity changes.
|
|
67
|
-
|
|
68
|
-
### `findMultiplicativeIdentityChanges(oldTree, newTree)`
|
|
69
|
-
|
|
70
|
-
Identifies changes related to the removal of multiplicative identities (e.g., `* 1` or `/ 1`).
|
|
71
|
-
|
|
72
|
-
- **`oldTree`** (`omdNode`): The old expression tree.
|
|
73
|
-
- **`newTree`** (`omdNode`): The new expression tree.
|
|
74
|
-
- **Returns**: `Array<omdNode>` - Nodes to highlight for multiplicative identity changes.
|
|
75
|
-
|
|
76
|
-
### `findDoubleNegativeChanges(oldTree, newTree)`
|
|
77
|
-
|
|
78
|
-
Identifies changes related to the simplification of double negatives (e.g., `--x` to `x`).
|
|
79
|
-
|
|
80
|
-
- **`oldTree`** (`omdNode`): The old expression tree.
|
|
81
|
-
- **`newTree`** (`omdNode`): The new expression tree.
|
|
82
|
-
- **Returns**: `Array<omdNode>` - Nodes to highlight for double negative removal.
|
|
83
|
-
|
|
84
|
-
### `findCommonPrefixHighlights(oldTree, newTree)`
|
|
85
|
-
|
|
86
|
-
Identifies highlighting patterns based on common prefixes between the string representations of the old and new trees. For example, if `"2x + 4"` becomes `"2x + 4 - 4"`, it highlights only the `"- 4"` part.
|
|
87
|
-
|
|
88
|
-
- **`oldTree`** (`omdNode`): The old expression tree.
|
|
89
|
-
- **`newTree`** (`omdNode`): The new expression tree.
|
|
90
|
-
- **Returns**: `Array<omdNode>` - Nodes to highlight for common prefix patterns.
|
|
91
|
-
|
|
92
|
-
### `findVariablePreservationHighlights(oldTree, newTree)`
|
|
93
|
-
|
|
94
|
-
Identifies highlighting patterns where a variable term remains the same but associated constants change. For example, `"2x + 4"` becoming `"2x + 2"` should highlight only the changed constant.
|
|
95
|
-
|
|
96
|
-
- **`oldTree`** (`omdNode`): The old expression tree.
|
|
97
|
-
- **`newTree`** (`omdNode`): The new expression tree.
|
|
98
|
-
- **Returns**: `Array<omdNode>` - Nodes to highlight for variable preservation patterns.
|
|
99
|
-
|
|
100
|
-
### `findTypeDifferenceHighlights(oldTree, newTree)`
|
|
101
|
-
|
|
102
|
-
Identifies highlighting patterns where the type of an expression changes significantly (e.g., a constant becoming a binary expression). In such cases, it highlights the new expression.
|
|
103
|
-
|
|
104
|
-
- **`oldTree`** (`omdNode`): The old expression tree.
|
|
105
|
-
- **`newTree`** (`omdNode`): The new expression tree.
|
|
106
|
-
- **Returns**: `Array<omdNode>` - Nodes to highlight for type difference patterns.
|
|
107
|
-
|
|
108
|
-
### `findSubtractionPatternHighlights(oldTree, newTree)`
|
|
109
|
-
|
|
110
|
-
Identifies highlighting patterns specific to subtraction, where the old tree matches the left side of a new subtraction. For example, `"x + 2"` becoming `"x + 2 - 2"` should highlight only the `"- 2"` part.
|
|
111
|
-
|
|
112
|
-
- **`oldTree`** (`omdNode`): The old expression tree.
|
|
113
|
-
- **`newTree`** (`omdNode`): The new expression tree.
|
|
114
|
-
- **Returns**: `Array<omdNode>` - Nodes to highlight for subtraction patterns.
|
|
115
|
-
|
|
116
|
-
### `findAllSubtreeMatches(oldTree, newTree)`
|
|
117
|
-
|
|
118
|
-
Generates all possible subtree matches between two expression trees. A match is determined by structural or string equivalence.
|
|
119
|
-
|
|
120
|
-
- **`oldTree`** (`omdNode`): The old expression tree.
|
|
121
|
-
- **`newTree`** (`omdNode`): The new expression tree.
|
|
122
|
-
- **Returns**: `Array<object>` - An array of match objects, each containing `oldNode`, `newNode`, `size`, `score`, and `type` of match.
|
|
123
|
-
|
|
124
|
-
### `selectOptimalMatching(matches)`
|
|
125
|
-
|
|
126
|
-
Given a list of all possible subtree matches, this method selects the optimal, non-overlapping set of matches. It uses a greedy approach, prioritizing larger and higher-scoring matches.
|
|
127
|
-
|
|
128
|
-
- **`matches`** (`Array<object>`): The array of potential matches from `findAllSubtreeMatches`.
|
|
129
|
-
- **Returns**: `Array<object>` - The selected optimal matches.
|
|
130
|
-
|
|
131
|
-
### `findUnmatchedLeafNodes(newTree, matches)`
|
|
132
|
-
|
|
133
|
-
Identifies all leaf nodes in the `newTree` that are not covered by any of the `optimalMatches`. These are the nodes that represent the actual changes.
|
|
134
|
-
|
|
135
|
-
- **`newTree`** (`omdNode`): The new expression tree.
|
|
136
|
-
- **`matches`** (`Array<object>`): The array of selected optimal matches.
|
|
137
|
-
- **Returns**: `Array<omdNode>` - An array of unmatched leaf nodes.
|
|
138
|
-
|
|
139
|
-
### `findUnmatchedOldNodes(oldTree, matches)`
|
|
140
|
-
|
|
141
|
-
Finds leaf nodes in the `oldTree` that are not covered by any match. These represent nodes that were removed or transformed.
|
|
142
|
-
|
|
143
|
-
- **`oldTree`** (`omdNode`): The old expression tree.
|
|
144
|
-
- **`matches`** (`Array<object>`): The array of selected optimal matches.
|
|
145
|
-
- **Returns**: `Array<omdNode>` - An array of unmatched leaf nodes from the old tree.
|
|
146
|
-
|
|
147
|
-
## Internal Helper Methods
|
|
148
|
-
|
|
149
|
-
- **`_findCommonPrefix(str1, str2)`**: Finds the longest common string prefix between two strings.
|
|
150
|
-
- **`getAllSubtrees(root)`**: Recursively collects all subtrees from a given root node.
|
|
151
|
-
- **`calculateSimilarity(tree1, tree2)`**: Determines the similarity score between two subtrees, considering structural and string equivalence.
|
|
152
|
-
- **`treesStructurallyEqual(tree1, tree2)`**: Checks for exact structural equality between two subtrees.
|
|
153
|
-
- **`getSubtreeSize(root)`**: Calculates the number of nodes in a subtree.
|
|
154
|
-
- **`hasNodeOverlap(node, usedNodes)`**: Checks if a node or any of its descendants overlap with a set of already used nodes.
|
|
155
|
-
- **`markSubtreeAsUsed(root, usedNodes)`**: Marks all nodes in a subtree as used by adding them to a `Set`.
|
|
156
|
-
- **`debugPrintTree(node, depth)`**: A utility function for debugging that prints the structure of an `omdNode` tree to the console.
|
|
157
|
-
|
|
158
|
-
## How it Works
|
|
159
|
-
|
|
160
|
-
See the Algorithm Overview above for a summary of the process. The class is designed for internal use by step visualizer components.
|
|
161
|
-
|
|
162
|
-
### Example
|
|
163
|
-
|
|
164
|
-
This class is typically used internally by `omdStepVisualizerHighlighting`:
|
|
165
|
-
|
|
166
|
-
```javascript
|
|
167
|
-
// Example of internal usage within omdStepVisualizerHighlighting:
|
|
168
|
-
// const changedNodes = omdTreeDiff.findChangedNodes(previousEquation, currentEquation, { educationalMode: true });
|
|
169
|
-
// changedNodes.forEach(node => node.setExplainHighlight(true));
|
|
1
|
+
# omdTreeDiff
|
|
2
|
+
|
|
3
|
+
Implements a robust tree differencing algorithm to identify changes between two `omdNode` expression trees. This module is crucial for `omdStepVisualizerHighlighting`, providing precise visual feedback on how mathematical expressions transform from one step to the next.
|
|
4
|
+
|
|
5
|
+
## Algorithm Overview
|
|
6
|
+
|
|
7
|
+
`omdTreeDiff` uses a multi-stage approach to compare two expression trees (`oldEquation` and `newEquation`):
|
|
8
|
+
|
|
9
|
+
1. **Special Cases**: It first attempts to identify common pedagogical patterns (like adding/subtracting the same value from both sides, or simple identity/double negative simplifications). If found, these specific changes are highlighted directly.
|
|
10
|
+
2. **Optimal Subtree Matching**: If no special cases apply, it proceeds to find the largest, non-overlapping set of matched subtrees between the old and new expressions. A match can be either structurally identical or semantically equivalent (e.g., `2+3` and `5`).
|
|
11
|
+
3. **Identify Changes**: Any parts of the `newEquation` that are *not* part of these optimal matches are considered the actual changes and are returned for highlighting.
|
|
12
|
+
4. **Educational Mode**: When enabled, the algorithm includes additional heuristics to highlight subtle simplifications that might not involve a direct structural change but are important for understanding the transformation (e.g., removing `+ 0`).
|
|
13
|
+
|
|
14
|
+
## Class Definition
|
|
15
|
+
|
|
16
|
+
```javascript
|
|
17
|
+
export class omdTreeDiff
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
This class is not meant to be instantiated. All its methods are static.
|
|
21
|
+
|
|
22
|
+
## Static Methods
|
|
23
|
+
|
|
24
|
+
### `findChangedNodes(oldEquation, newEquation, options)`
|
|
25
|
+
|
|
26
|
+
Main entry point for the tree differencing algorithm. Compares two `omdEquationNode` instances (or any `omdNode` subtrees) and returns a list of nodes in the `newEquation` that have changed or are new.
|
|
27
|
+
|
|
28
|
+
- **`oldEquation`** (`omdNode`): The root node of the previous expression tree.
|
|
29
|
+
- **`newEquation`** (`omdNode`): The root node of the current expression tree.
|
|
30
|
+
- **`options`** (`object`, optional): Configuration options:
|
|
31
|
+
- `educationalMode` (`boolean`): If `true`, the diff algorithm will also highlight mathematically neutral changes that are pedagogically significant (e.g., removing `+ 0`). Default: `false`.
|
|
32
|
+
- **Returns**: `Array<omdNode>` - An array of `omdNode` instances from `newEquation` that should be highlighted.
|
|
33
|
+
|
|
34
|
+
### `findEquationSpecialCases(oldEquation, newEquation)`
|
|
35
|
+
|
|
36
|
+
Identifies specific equation-level transformation patterns, such as adding/subtracting the same operation to both sides of an equation. This allows for more intuitive highlighting in common step-by-step solution scenarios.
|
|
37
|
+
|
|
38
|
+
- **`oldEquation`** (`omdEquationNode`): The previous equation.
|
|
39
|
+
- **`newEquation`** (`omdEquationNode`): The current equation.
|
|
40
|
+
- **Returns**: `Array<omdNode>` - An array of nodes to highlight for these special cases, or an empty array if no such pattern is found.
|
|
41
|
+
|
|
42
|
+
### `diffSubtrees(oldTree, newTree, educationalMode)`
|
|
43
|
+
|
|
44
|
+
Core recursive differencing method. It first checks for various pedagogical highlighting patterns (common prefix, variable preservation, type differences, subtraction patterns). If none apply, it falls back to finding an optimal matching between subtrees of the `oldTree` and `newTree`. Nodes in `newTree` that do not have a match in `oldTree` are considered changed.
|
|
45
|
+
|
|
46
|
+
- **`oldTree`** (`omdNode`): The root of the old subtree.
|
|
47
|
+
- **`newTree`** (`omdNode`): The root of the new subtree.
|
|
48
|
+
- **`educationalMode`** (`boolean`): Same as in `findChangedNodes`.
|
|
49
|
+
- **Returns**: `Array<omdNode>` - An array of unmatched leaf nodes in `newTree`.
|
|
50
|
+
|
|
51
|
+
### `findEducationalHighlights(oldTree, newTree, optimalMatches)`
|
|
52
|
+
|
|
53
|
+
When `educationalMode` is enabled, this method identifies additional nodes to highlight for pedagogical reasons, even if they don't represent a structural change. This includes cases like the removal of additive/multiplicative identities or double negations.
|
|
54
|
+
|
|
55
|
+
- **`oldTree`** (`omdNode`): The old expression tree.
|
|
56
|
+
- **`newTree`** (`omdNode`): The new expression tree.
|
|
57
|
+
- **`optimalMatches`** (`Array`): The list of optimally matched subtrees.
|
|
58
|
+
- **Returns**: `Array<omdNode>` - Additional nodes to highlight.
|
|
59
|
+
|
|
60
|
+
### `findAdditiveIdentityChanges(oldTree, newTree)`
|
|
61
|
+
|
|
62
|
+
Identifies changes related to the removal of additive identities (e.g., `+ 0` or `- 0`).
|
|
63
|
+
|
|
64
|
+
- **`oldTree`** (`omdNode`): The old expression tree.
|
|
65
|
+
- **`newTree`** (`omdNode`): The new expression tree.
|
|
66
|
+
- **Returns**: `Array<omdNode>` - Nodes to highlight for additive identity changes.
|
|
67
|
+
|
|
68
|
+
### `findMultiplicativeIdentityChanges(oldTree, newTree)`
|
|
69
|
+
|
|
70
|
+
Identifies changes related to the removal of multiplicative identities (e.g., `* 1` or `/ 1`).
|
|
71
|
+
|
|
72
|
+
- **`oldTree`** (`omdNode`): The old expression tree.
|
|
73
|
+
- **`newTree`** (`omdNode`): The new expression tree.
|
|
74
|
+
- **Returns**: `Array<omdNode>` - Nodes to highlight for multiplicative identity changes.
|
|
75
|
+
|
|
76
|
+
### `findDoubleNegativeChanges(oldTree, newTree)`
|
|
77
|
+
|
|
78
|
+
Identifies changes related to the simplification of double negatives (e.g., `--x` to `x`).
|
|
79
|
+
|
|
80
|
+
- **`oldTree`** (`omdNode`): The old expression tree.
|
|
81
|
+
- **`newTree`** (`omdNode`): The new expression tree.
|
|
82
|
+
- **Returns**: `Array<omdNode>` - Nodes to highlight for double negative removal.
|
|
83
|
+
|
|
84
|
+
### `findCommonPrefixHighlights(oldTree, newTree)`
|
|
85
|
+
|
|
86
|
+
Identifies highlighting patterns based on common prefixes between the string representations of the old and new trees. For example, if `"2x + 4"` becomes `"2x + 4 - 4"`, it highlights only the `"- 4"` part.
|
|
87
|
+
|
|
88
|
+
- **`oldTree`** (`omdNode`): The old expression tree.
|
|
89
|
+
- **`newTree`** (`omdNode`): The new expression tree.
|
|
90
|
+
- **Returns**: `Array<omdNode>` - Nodes to highlight for common prefix patterns.
|
|
91
|
+
|
|
92
|
+
### `findVariablePreservationHighlights(oldTree, newTree)`
|
|
93
|
+
|
|
94
|
+
Identifies highlighting patterns where a variable term remains the same but associated constants change. For example, `"2x + 4"` becoming `"2x + 2"` should highlight only the changed constant.
|
|
95
|
+
|
|
96
|
+
- **`oldTree`** (`omdNode`): The old expression tree.
|
|
97
|
+
- **`newTree`** (`omdNode`): The new expression tree.
|
|
98
|
+
- **Returns**: `Array<omdNode>` - Nodes to highlight for variable preservation patterns.
|
|
99
|
+
|
|
100
|
+
### `findTypeDifferenceHighlights(oldTree, newTree)`
|
|
101
|
+
|
|
102
|
+
Identifies highlighting patterns where the type of an expression changes significantly (e.g., a constant becoming a binary expression). In such cases, it highlights the new expression.
|
|
103
|
+
|
|
104
|
+
- **`oldTree`** (`omdNode`): The old expression tree.
|
|
105
|
+
- **`newTree`** (`omdNode`): The new expression tree.
|
|
106
|
+
- **Returns**: `Array<omdNode>` - Nodes to highlight for type difference patterns.
|
|
107
|
+
|
|
108
|
+
### `findSubtractionPatternHighlights(oldTree, newTree)`
|
|
109
|
+
|
|
110
|
+
Identifies highlighting patterns specific to subtraction, where the old tree matches the left side of a new subtraction. For example, `"x + 2"` becoming `"x + 2 - 2"` should highlight only the `"- 2"` part.
|
|
111
|
+
|
|
112
|
+
- **`oldTree`** (`omdNode`): The old expression tree.
|
|
113
|
+
- **`newTree`** (`omdNode`): The new expression tree.
|
|
114
|
+
- **Returns**: `Array<omdNode>` - Nodes to highlight for subtraction patterns.
|
|
115
|
+
|
|
116
|
+
### `findAllSubtreeMatches(oldTree, newTree)`
|
|
117
|
+
|
|
118
|
+
Generates all possible subtree matches between two expression trees. A match is determined by structural or string equivalence.
|
|
119
|
+
|
|
120
|
+
- **`oldTree`** (`omdNode`): The old expression tree.
|
|
121
|
+
- **`newTree`** (`omdNode`): The new expression tree.
|
|
122
|
+
- **Returns**: `Array<object>` - An array of match objects, each containing `oldNode`, `newNode`, `size`, `score`, and `type` of match.
|
|
123
|
+
|
|
124
|
+
### `selectOptimalMatching(matches)`
|
|
125
|
+
|
|
126
|
+
Given a list of all possible subtree matches, this method selects the optimal, non-overlapping set of matches. It uses a greedy approach, prioritizing larger and higher-scoring matches.
|
|
127
|
+
|
|
128
|
+
- **`matches`** (`Array<object>`): The array of potential matches from `findAllSubtreeMatches`.
|
|
129
|
+
- **Returns**: `Array<object>` - The selected optimal matches.
|
|
130
|
+
|
|
131
|
+
### `findUnmatchedLeafNodes(newTree, matches)`
|
|
132
|
+
|
|
133
|
+
Identifies all leaf nodes in the `newTree` that are not covered by any of the `optimalMatches`. These are the nodes that represent the actual changes.
|
|
134
|
+
|
|
135
|
+
- **`newTree`** (`omdNode`): The new expression tree.
|
|
136
|
+
- **`matches`** (`Array<object>`): The array of selected optimal matches.
|
|
137
|
+
- **Returns**: `Array<omdNode>` - An array of unmatched leaf nodes.
|
|
138
|
+
|
|
139
|
+
### `findUnmatchedOldNodes(oldTree, matches)`
|
|
140
|
+
|
|
141
|
+
Finds leaf nodes in the `oldTree` that are not covered by any match. These represent nodes that were removed or transformed.
|
|
142
|
+
|
|
143
|
+
- **`oldTree`** (`omdNode`): The old expression tree.
|
|
144
|
+
- **`matches`** (`Array<object>`): The array of selected optimal matches.
|
|
145
|
+
- **Returns**: `Array<omdNode>` - An array of unmatched leaf nodes from the old tree.
|
|
146
|
+
|
|
147
|
+
## Internal Helper Methods
|
|
148
|
+
|
|
149
|
+
- **`_findCommonPrefix(str1, str2)`**: Finds the longest common string prefix between two strings.
|
|
150
|
+
- **`getAllSubtrees(root)`**: Recursively collects all subtrees from a given root node.
|
|
151
|
+
- **`calculateSimilarity(tree1, tree2)`**: Determines the similarity score between two subtrees, considering structural and string equivalence.
|
|
152
|
+
- **`treesStructurallyEqual(tree1, tree2)`**: Checks for exact structural equality between two subtrees.
|
|
153
|
+
- **`getSubtreeSize(root)`**: Calculates the number of nodes in a subtree.
|
|
154
|
+
- **`hasNodeOverlap(node, usedNodes)`**: Checks if a node or any of its descendants overlap with a set of already used nodes.
|
|
155
|
+
- **`markSubtreeAsUsed(root, usedNodes)`**: Marks all nodes in a subtree as used by adding them to a `Set`.
|
|
156
|
+
- **`debugPrintTree(node, depth)`**: A utility function for debugging that prints the structure of an `omdNode` tree to the console.
|
|
157
|
+
|
|
158
|
+
## How it Works
|
|
159
|
+
|
|
160
|
+
See the Algorithm Overview above for a summary of the process. The class is designed for internal use by step visualizer components.
|
|
161
|
+
|
|
162
|
+
### Example
|
|
163
|
+
|
|
164
|
+
This class is typically used internally by `omdStepVisualizerHighlighting`:
|
|
165
|
+
|
|
166
|
+
```javascript
|
|
167
|
+
// Example of internal usage within omdStepVisualizerHighlighting:
|
|
168
|
+
// const changedNodes = omdTreeDiff.findChangedNodes(previousEquation, currentEquation, { educationalMode: true });
|
|
169
|
+
// changedNodes.forEach(node => node.setExplainHighlight(true));
|
|
170
170
|
```
|
|
@@ -1,137 +1,137 @@
|
|
|
1
|
-
# omdUnaryExpressionNode
|
|
2
|
-
|
|
3
|
-
Represents unary operations (like negation) in mathematical expressions (e.g., `-x`, `-(a + b)`). This node handles rendering, layout, evaluation, and conversion to math.js AST for expressions with a single operand.
|
|
4
|
-
|
|
5
|
-
## Class Definition
|
|
6
|
-
|
|
7
|
-
```javascript
|
|
8
|
-
export class omdUnaryExpressionNode extends omdNode
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## Constructor
|
|
12
|
-
|
|
13
|
-
### `new omdUnaryExpressionNode(ast)`
|
|
14
|
-
|
|
15
|
-
Creates a new `omdUnaryExpressionNode` instance.
|
|
16
|
-
|
|
17
|
-
- **`ast`** (`object`): The math.js AST node for the unary operation. It must contain:
|
|
18
|
-
- `args`: An array with exactly one operand AST node.
|
|
19
|
-
- `op`: The operator symbol (e.g., `'-'`, `'+'`).
|
|
20
|
-
- `fn`: The operation name (e.g., `'unaryMinus'`, `'unaryPlus'`).
|
|
21
|
-
|
|
22
|
-
During construction, it creates an `omdOperatorNode` for the unary operator and an `omdNode` for the operand. It throws an error if the AST does not have exactly one argument.
|
|
23
|
-
|
|
24
|
-
## Static Methods
|
|
25
|
-
|
|
26
|
-
### `fromString(expressionString)`
|
|
27
|
-
|
|
28
|
-
Creates an `omdUnaryExpressionNode` from a string representation of a unary expression. Requires `window.math` (math.js) to be available globally for parsing.
|
|
29
|
-
|
|
30
|
-
- **`expressionString`** (`string`): The unary expression as a string (e.g., `"-x"`, `"-(a+b)"`).
|
|
31
|
-
- **Returns**: `omdUnaryExpressionNode` - A new instance.
|
|
32
|
-
- **Throws**: `Error` if `math.js` is not available, if the string cannot be parsed, or if it does not represent a valid unary minus operation.
|
|
33
|
-
|
|
34
|
-
## Public Properties
|
|
35
|
-
|
|
36
|
-
- **`op`** (`omdOperatorNode`): The `omdOperatorNode` representing the unary operator (e.g., `-`).
|
|
37
|
-
- **`operand`** (`omdNode`): The `omdNode` representing the expression being operated on.
|
|
38
|
-
- **`operation`** (`string`): The name of the operation (e.g., `'unaryMinus'`).
|
|
39
|
-
|
|
40
|
-
## Public Methods
|
|
41
|
-
|
|
42
|
-
### `computeDimensions()`
|
|
43
|
-
|
|
44
|
-
Calculates the dimensions of the unary expression node. It sums the widths of the operator and the operand to get the total width. The height is the maximum of the operator's and operand's heights. No extra spacing is added between the unary operator and its operand.
|
|
45
|
-
|
|
46
|
-
- **Overrides**: `omdNode.computeDimensions()`.
|
|
47
|
-
|
|
48
|
-
### `updateLayout()`
|
|
49
|
-
|
|
50
|
-
Updates the layout of the unary expression node. It positions the operator to the left and the operand to its right, both vertically centered within the node's total height.
|
|
51
|
-
|
|
52
|
-
- **Overrides**: `omdNode.updateLayout()`.
|
|
53
|
-
|
|
54
|
-
### `clone()`
|
|
55
|
-
|
|
56
|
-
Creates a deep, structural clone of the unary expression node, including its `op` and `operand` nodes. The cloned node's `provenance` array is updated to include the original node's ID.
|
|
57
|
-
|
|
58
|
-
- **Returns**: `omdUnaryExpressionNode` - A new, identical instance of the unary expression node.
|
|
59
|
-
|
|
60
|
-
### `toMathJSNode()`
|
|
61
|
-
|
|
62
|
-
Converts the `omdUnaryExpressionNode` back into its math.js AST representation. It creates an `OperatorNode` with the unary operator and the converted operand AST.
|
|
63
|
-
|
|
64
|
-
- **Returns**: `object` - A math.js `OperatorNode` for the unary operation. The returned object also includes a `clone` method for compatibility.
|
|
65
|
-
|
|
66
|
-
### `toString()`
|
|
67
|
-
|
|
68
|
-
Converts the unary expression node to its string representation. It adds parentheses around the operand if `needsParentheses()` returns `true` (e.g., for binary expressions).
|
|
69
|
-
|
|
70
|
-
- **Returns**: `string` - Format: `"-operand"` or `"-(operand)"`.
|
|
71
|
-
|
|
72
|
-
### `needsParentheses()`
|
|
73
|
-
|
|
74
|
-
Checks if the operand needs to be wrapped in parentheses when converted to a string. This is typically `true` if the operand is a binary expression, to maintain correct order of operations.
|
|
75
|
-
|
|
76
|
-
- **Returns**: `boolean` - `true` if parentheses are required around the operand.
|
|
77
|
-
|
|
78
|
-
### `evaluate(variables)`
|
|
79
|
-
|
|
80
|
-
Evaluates the unary expression. It evaluates the `operand` and then applies the unary operation (e.g., negation for `'-'`).
|
|
81
|
-
|
|
82
|
-
- **`variables`** (`object`): A map of variable names to their numeric values.
|
|
83
|
-
- **Returns**: `number` - The result of the unary operation, or `NaN` if the operand cannot be evaluated to a number.
|
|
84
|
-
|
|
85
|
-
## Internal Methods
|
|
86
|
-
|
|
87
|
-
- **`createOperatorNode(ast)`**: Creates an `omdOperatorNode` for the unary operator from its AST, setting its parent to this node.
|
|
88
|
-
- **`createExpressionNode(ast)`**: Creates an `omdNode` instance for the operand from its AST, setting its parent to this node.
|
|
89
|
-
|
|
90
|
-
## Example
|
|
91
|
-
|
|
92
|
-
```javascript
|
|
93
|
-
import { omdUnaryExpressionNode } from '@teachinglab/omd';
|
|
94
|
-
import * as math from 'mathjs';
|
|
95
|
-
|
|
96
|
-
// Create from AST: -x
|
|
97
|
-
const node = new omdUnaryExpressionNode({
|
|
98
|
-
type: 'OperatorNode',
|
|
99
|
-
op: '-',
|
|
100
|
-
fn: 'unaryMinus',
|
|
101
|
-
args: [
|
|
102
|
-
{ type: 'SymbolNode', name: 'x' }
|
|
103
|
-
]
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
// Create from AST: -(x + 1)
|
|
107
|
-
const complex = new omdUnaryExpressionNode({
|
|
108
|
-
type: 'OperatorNode',
|
|
109
|
-
op: '-',
|
|
110
|
-
fn: 'unaryMinus',
|
|
111
|
-
args: [{
|
|
112
|
-
type: 'OperatorNode',
|
|
113
|
-
op: '+',
|
|
114
|
-
fn: 'add',
|
|
115
|
-
args: [
|
|
116
|
-
{ type: 'SymbolNode', name: 'x' },
|
|
117
|
-
{ type: 'ConstantNode', value: 1 }
|
|
118
|
-
]
|
|
119
|
-
}]
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
// Render and layout
|
|
123
|
-
node.setFontSize(24);
|
|
124
|
-
node.initialize();
|
|
125
|
-
|
|
126
|
-
// Add to an SVG container to display
|
|
127
|
-
// const svgContainer = new jsvgContainer();
|
|
128
|
-
// svgContainer.addChild(node);
|
|
129
|
-
// document.body.appendChild(svgContainer.svgObject);
|
|
130
|
-
```
|
|
131
|
-
|
|
132
|
-
## See Also
|
|
133
|
-
|
|
134
|
-
- [`omdNode`](./omdNode.md) - Parent class.
|
|
135
|
-
- [`omdOperatorNode`](./omdOperatorNode.md) - For the operator symbol.
|
|
136
|
-
- [`omdBinaryExpressionNode`](./omdBinaryExpressionNode.md) - Often used for complex operands within a unary expression.
|
|
137
|
-
- [`omdConstantNode`](./omdConstantNode.md) - For numeric operands.
|
|
1
|
+
# omdUnaryExpressionNode
|
|
2
|
+
|
|
3
|
+
Represents unary operations (like negation) in mathematical expressions (e.g., `-x`, `-(a + b)`). This node handles rendering, layout, evaluation, and conversion to math.js AST for expressions with a single operand.
|
|
4
|
+
|
|
5
|
+
## Class Definition
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
export class omdUnaryExpressionNode extends omdNode
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Constructor
|
|
12
|
+
|
|
13
|
+
### `new omdUnaryExpressionNode(ast)`
|
|
14
|
+
|
|
15
|
+
Creates a new `omdUnaryExpressionNode` instance.
|
|
16
|
+
|
|
17
|
+
- **`ast`** (`object`): The math.js AST node for the unary operation. It must contain:
|
|
18
|
+
- `args`: An array with exactly one operand AST node.
|
|
19
|
+
- `op`: The operator symbol (e.g., `'-'`, `'+'`).
|
|
20
|
+
- `fn`: The operation name (e.g., `'unaryMinus'`, `'unaryPlus'`).
|
|
21
|
+
|
|
22
|
+
During construction, it creates an `omdOperatorNode` for the unary operator and an `omdNode` for the operand. It throws an error if the AST does not have exactly one argument.
|
|
23
|
+
|
|
24
|
+
## Static Methods
|
|
25
|
+
|
|
26
|
+
### `fromString(expressionString)`
|
|
27
|
+
|
|
28
|
+
Creates an `omdUnaryExpressionNode` from a string representation of a unary expression. Requires `window.math` (math.js) to be available globally for parsing.
|
|
29
|
+
|
|
30
|
+
- **`expressionString`** (`string`): The unary expression as a string (e.g., `"-x"`, `"-(a+b)"`).
|
|
31
|
+
- **Returns**: `omdUnaryExpressionNode` - A new instance.
|
|
32
|
+
- **Throws**: `Error` if `math.js` is not available, if the string cannot be parsed, or if it does not represent a valid unary minus operation.
|
|
33
|
+
|
|
34
|
+
## Public Properties
|
|
35
|
+
|
|
36
|
+
- **`op`** (`omdOperatorNode`): The `omdOperatorNode` representing the unary operator (e.g., `-`).
|
|
37
|
+
- **`operand`** (`omdNode`): The `omdNode` representing the expression being operated on.
|
|
38
|
+
- **`operation`** (`string`): The name of the operation (e.g., `'unaryMinus'`).
|
|
39
|
+
|
|
40
|
+
## Public Methods
|
|
41
|
+
|
|
42
|
+
### `computeDimensions()`
|
|
43
|
+
|
|
44
|
+
Calculates the dimensions of the unary expression node. It sums the widths of the operator and the operand to get the total width. The height is the maximum of the operator's and operand's heights. No extra spacing is added between the unary operator and its operand.
|
|
45
|
+
|
|
46
|
+
- **Overrides**: `omdNode.computeDimensions()`.
|
|
47
|
+
|
|
48
|
+
### `updateLayout()`
|
|
49
|
+
|
|
50
|
+
Updates the layout of the unary expression node. It positions the operator to the left and the operand to its right, both vertically centered within the node's total height.
|
|
51
|
+
|
|
52
|
+
- **Overrides**: `omdNode.updateLayout()`.
|
|
53
|
+
|
|
54
|
+
### `clone()`
|
|
55
|
+
|
|
56
|
+
Creates a deep, structural clone of the unary expression node, including its `op` and `operand` nodes. The cloned node's `provenance` array is updated to include the original node's ID.
|
|
57
|
+
|
|
58
|
+
- **Returns**: `omdUnaryExpressionNode` - A new, identical instance of the unary expression node.
|
|
59
|
+
|
|
60
|
+
### `toMathJSNode()`
|
|
61
|
+
|
|
62
|
+
Converts the `omdUnaryExpressionNode` back into its math.js AST representation. It creates an `OperatorNode` with the unary operator and the converted operand AST.
|
|
63
|
+
|
|
64
|
+
- **Returns**: `object` - A math.js `OperatorNode` for the unary operation. The returned object also includes a `clone` method for compatibility.
|
|
65
|
+
|
|
66
|
+
### `toString()`
|
|
67
|
+
|
|
68
|
+
Converts the unary expression node to its string representation. It adds parentheses around the operand if `needsParentheses()` returns `true` (e.g., for binary expressions).
|
|
69
|
+
|
|
70
|
+
- **Returns**: `string` - Format: `"-operand"` or `"-(operand)"`.
|
|
71
|
+
|
|
72
|
+
### `needsParentheses()`
|
|
73
|
+
|
|
74
|
+
Checks if the operand needs to be wrapped in parentheses when converted to a string. This is typically `true` if the operand is a binary expression, to maintain correct order of operations.
|
|
75
|
+
|
|
76
|
+
- **Returns**: `boolean` - `true` if parentheses are required around the operand.
|
|
77
|
+
|
|
78
|
+
### `evaluate(variables)`
|
|
79
|
+
|
|
80
|
+
Evaluates the unary expression. It evaluates the `operand` and then applies the unary operation (e.g., negation for `'-'`).
|
|
81
|
+
|
|
82
|
+
- **`variables`** (`object`): A map of variable names to their numeric values.
|
|
83
|
+
- **Returns**: `number` - The result of the unary operation, or `NaN` if the operand cannot be evaluated to a number.
|
|
84
|
+
|
|
85
|
+
## Internal Methods
|
|
86
|
+
|
|
87
|
+
- **`createOperatorNode(ast)`**: Creates an `omdOperatorNode` for the unary operator from its AST, setting its parent to this node.
|
|
88
|
+
- **`createExpressionNode(ast)`**: Creates an `omdNode` instance for the operand from its AST, setting its parent to this node.
|
|
89
|
+
|
|
90
|
+
## Example
|
|
91
|
+
|
|
92
|
+
```javascript
|
|
93
|
+
import { omdUnaryExpressionNode } from '@teachinglab/omd';
|
|
94
|
+
import * as math from 'mathjs';
|
|
95
|
+
|
|
96
|
+
// Create from AST: -x
|
|
97
|
+
const node = new omdUnaryExpressionNode({
|
|
98
|
+
type: 'OperatorNode',
|
|
99
|
+
op: '-',
|
|
100
|
+
fn: 'unaryMinus',
|
|
101
|
+
args: [
|
|
102
|
+
{ type: 'SymbolNode', name: 'x' }
|
|
103
|
+
]
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
// Create from AST: -(x + 1)
|
|
107
|
+
const complex = new omdUnaryExpressionNode({
|
|
108
|
+
type: 'OperatorNode',
|
|
109
|
+
op: '-',
|
|
110
|
+
fn: 'unaryMinus',
|
|
111
|
+
args: [{
|
|
112
|
+
type: 'OperatorNode',
|
|
113
|
+
op: '+',
|
|
114
|
+
fn: 'add',
|
|
115
|
+
args: [
|
|
116
|
+
{ type: 'SymbolNode', name: 'x' },
|
|
117
|
+
{ type: 'ConstantNode', value: 1 }
|
|
118
|
+
]
|
|
119
|
+
}]
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
// Render and layout
|
|
123
|
+
node.setFontSize(24);
|
|
124
|
+
node.initialize();
|
|
125
|
+
|
|
126
|
+
// Add to an SVG container to display
|
|
127
|
+
// const svgContainer = new jsvgContainer();
|
|
128
|
+
// svgContainer.addChild(node);
|
|
129
|
+
// document.body.appendChild(svgContainer.svgObject);
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## See Also
|
|
133
|
+
|
|
134
|
+
- [`omdNode`](./omdNode.md) - Parent class.
|
|
135
|
+
- [`omdOperatorNode`](./omdOperatorNode.md) - For the operator symbol.
|
|
136
|
+
- [`omdBinaryExpressionNode`](./omdBinaryExpressionNode.md) - Often used for complex operands within a unary expression.
|
|
137
|
+
- [`omdConstantNode`](./omdConstantNode.md) - For numeric operands.
|