@teachinglab/omd 0.3.0 → 0.3.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/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 +156 -156
- 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/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/omdStepVisualizerTextBoxes.md +76 -76
- 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/omd/nodes/omdConstantNode.js +141 -141
- package/omd/nodes/omdGroupNode.js +67 -67
- package/omd/nodes/omdLeafNode.js +76 -76
- 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/omdVariableNode.js +122 -122
- package/omd/simplification/omdSimplification.js +140 -140
- package/omd/step-visualizer/omdStepVisualizer.js +947 -947
- package/omd/step-visualizer/omdStepVisualizerLayout.js +892 -892
- package/package.json +2 -1
- package/src/index.js +11 -0
- package/src/omdBalanceHanger.js +2 -1
- package/src/omdEquation.js +1 -1
- package/src/omdNumber.js +1 -1
- package/src/omdNumberLine.js +13 -7
- package/src/omdRatioChart.js +19 -0
- package/src/omdShapes.js +1 -1
- package/src/omdTapeDiagram.js +1 -1
- package/src/omdTerm.js +1 -1
|
@@ -1,79 +1,79 @@
|
|
|
1
|
-
# omdSimplification
|
|
2
|
-
|
|
3
|
-
This module provides the core logic for simplifying mathematical expression trees within the OMD library. It defines functions for applying simplification rules, finding opportunities for simplification, and managing the simplification process.
|
|
4
|
-
|
|
5
|
-
## Functions
|
|
6
|
-
|
|
7
|
-
### `simplifyExpression(rootNode)`
|
|
8
|
-
|
|
9
|
-
Simplifies an entire mathematical expression tree by repeatedly applying simplification rules until no further simplifications are possible or a maximum iteration limit is reached. This function ensures that the resulting expression is in its most simplified form.
|
|
10
|
-
|
|
11
|
-
- **`rootNode`** (`omdNode`): The root node of the expression tree to simplify.
|
|
12
|
-
- **Returns**: `{ foldedCount: number, newRoot: omdNode }`
|
|
13
|
-
- `foldedCount`: The total number of individual simplifications applied across all iterations.
|
|
14
|
-
- `newRoot`: The simplified expression tree. This is a fresh clone of the final simplified state, with its font size preserved from the original `rootNode`.
|
|
15
|
-
|
|
16
|
-
**Notes:**
|
|
17
|
-
|
|
18
|
-
- A maximum of `50` iterations is performed to prevent potential infinite loops.
|
|
19
|
-
- Handles errors gracefully; if an error occurs during simplification, the original node is returned.
|
|
20
|
-
|
|
21
|
-
### `simplifyStep(rootNode)`
|
|
22
|
-
|
|
23
|
-
Applies a single simplification step to the expression tree. This function finds the first applicable simplification rule and applies it, returning the result.
|
|
24
|
-
|
|
25
|
-
- **`rootNode`** (`omdNode`): The root node of the expression to simplify.
|
|
26
|
-
- **Returns**: `{ foldedCount: number, newRoot: omdNode, historyEntry: object | null }`
|
|
27
|
-
- `foldedCount`: `1` if a simplification was applied, `0` otherwise.
|
|
28
|
-
- `newRoot`: The expression tree after applying the single step. This will be a new `omdNode` instance if a simplification occurred.
|
|
29
|
-
- `historyEntry`: An object describing the applied simplification (e.g., rule name, affected nodes, message), or `null` if no simplification was applied.
|
|
30
|
-
|
|
31
|
-
**Notes:**
|
|
32
|
-
|
|
33
|
-
- Only the first matching rule found is applied in a single step.
|
|
34
|
-
- Preserves the font size from the original node.
|
|
35
|
-
- Handles errors gracefully; if an error occurs during rule application, the original node is returned.
|
|
36
|
-
|
|
37
|
-
### `findSimplificationOpportunities(rootNode)`
|
|
38
|
-
|
|
39
|
-
Traverses the expression tree (depth-first) to find all possible simplification opportunities. It identifies nodes where a simplification rule can be applied.
|
|
40
|
-
|
|
41
|
-
- **`rootNode`** (`omdNode`): The root node of the expression tree to search.
|
|
42
|
-
- **Returns**: `Array<{ node: omdNode, rule: object, ruleData: object }>` - An array of objects, where each object represents a simplification opportunity:
|
|
43
|
-
- `node`: The `omdNode` instance where a rule can be applied.
|
|
44
|
-
- `rule`: The simplification rule object that can be applied.
|
|
45
|
-
- `ruleData`: Data needed by the rule's `apply` method.
|
|
46
|
-
|
|
47
|
-
**Notes:**
|
|
48
|
-
|
|
49
|
-
- Uses a `visitedNodes` set to prevent infinite loops in cyclic graphs (though expression trees are typically acyclic).
|
|
50
|
-
- Only checks rules relevant to each node type, optimizing performance.
|
|
51
|
-
- For each node, it stops after finding the first applicable rule.
|
|
52
|
-
|
|
53
|
-
### `getAvailableRulesForNode(node)`
|
|
54
|
-
|
|
55
|
-
**Debug Function:** Lists all simplification rules that are potentially relevant to a given `omdNode`'s type, regardless of whether they can currently be applied.
|
|
56
|
-
|
|
57
|
-
- **`node`** (`omdNode`): The node to check.
|
|
58
|
-
- **Returns**: `Array<string>` - An array of rule names.
|
|
59
|
-
|
|
60
|
-
### `getApplicableRulesForNode(node)`
|
|
61
|
-
|
|
62
|
-
**Debug Function:** Lists simplification rules that can actually be applied to a given `omdNode` based on its current structure and values.
|
|
63
|
-
|
|
64
|
-
- **`node`** (`omdNode`): The node to check.
|
|
65
|
-
- **Returns**: `Array<string>` - An array of applicable rule names.
|
|
66
|
-
|
|
67
|
-
**Notes:**
|
|
68
|
-
|
|
69
|
-
- Handles errors gracefully by skipping rule checks that might fail.
|
|
70
|
-
|
|
71
|
-
## Integration with `omdNode`
|
|
72
|
-
|
|
73
|
-
This module also sets the `simplifyStep` function as the global simplification handler for `omdNode` instances. This allows any `omdNode` to call its `simplify()` method, which will internally use the `simplifyStep` function defined here.
|
|
74
|
-
|
|
75
|
-
## See Also
|
|
76
|
-
|
|
77
|
-
- [`Simplification Rules`](./simplificationRules.md) - Details the individual simplification rules.
|
|
78
|
-
- [`Simplification Engine`](./simplificationEngine.md) - (If applicable, for higher-level orchestration).
|
|
1
|
+
# omdSimplification
|
|
2
|
+
|
|
3
|
+
This module provides the core logic for simplifying mathematical expression trees within the OMD library. It defines functions for applying simplification rules, finding opportunities for simplification, and managing the simplification process.
|
|
4
|
+
|
|
5
|
+
## Functions
|
|
6
|
+
|
|
7
|
+
### `simplifyExpression(rootNode)`
|
|
8
|
+
|
|
9
|
+
Simplifies an entire mathematical expression tree by repeatedly applying simplification rules until no further simplifications are possible or a maximum iteration limit is reached. This function ensures that the resulting expression is in its most simplified form.
|
|
10
|
+
|
|
11
|
+
- **`rootNode`** (`omdNode`): The root node of the expression tree to simplify.
|
|
12
|
+
- **Returns**: `{ foldedCount: number, newRoot: omdNode }`
|
|
13
|
+
- `foldedCount`: The total number of individual simplifications applied across all iterations.
|
|
14
|
+
- `newRoot`: The simplified expression tree. This is a fresh clone of the final simplified state, with its font size preserved from the original `rootNode`.
|
|
15
|
+
|
|
16
|
+
**Notes:**
|
|
17
|
+
|
|
18
|
+
- A maximum of `50` iterations is performed to prevent potential infinite loops.
|
|
19
|
+
- Handles errors gracefully; if an error occurs during simplification, the original node is returned.
|
|
20
|
+
|
|
21
|
+
### `simplifyStep(rootNode)`
|
|
22
|
+
|
|
23
|
+
Applies a single simplification step to the expression tree. This function finds the first applicable simplification rule and applies it, returning the result.
|
|
24
|
+
|
|
25
|
+
- **`rootNode`** (`omdNode`): The root node of the expression to simplify.
|
|
26
|
+
- **Returns**: `{ foldedCount: number, newRoot: omdNode, historyEntry: object | null }`
|
|
27
|
+
- `foldedCount`: `1` if a simplification was applied, `0` otherwise.
|
|
28
|
+
- `newRoot`: The expression tree after applying the single step. This will be a new `omdNode` instance if a simplification occurred.
|
|
29
|
+
- `historyEntry`: An object describing the applied simplification (e.g., rule name, affected nodes, message), or `null` if no simplification was applied.
|
|
30
|
+
|
|
31
|
+
**Notes:**
|
|
32
|
+
|
|
33
|
+
- Only the first matching rule found is applied in a single step.
|
|
34
|
+
- Preserves the font size from the original node.
|
|
35
|
+
- Handles errors gracefully; if an error occurs during rule application, the original node is returned.
|
|
36
|
+
|
|
37
|
+
### `findSimplificationOpportunities(rootNode)`
|
|
38
|
+
|
|
39
|
+
Traverses the expression tree (depth-first) to find all possible simplification opportunities. It identifies nodes where a simplification rule can be applied.
|
|
40
|
+
|
|
41
|
+
- **`rootNode`** (`omdNode`): The root node of the expression tree to search.
|
|
42
|
+
- **Returns**: `Array<{ node: omdNode, rule: object, ruleData: object }>` - An array of objects, where each object represents a simplification opportunity:
|
|
43
|
+
- `node`: The `omdNode` instance where a rule can be applied.
|
|
44
|
+
- `rule`: The simplification rule object that can be applied.
|
|
45
|
+
- `ruleData`: Data needed by the rule's `apply` method.
|
|
46
|
+
|
|
47
|
+
**Notes:**
|
|
48
|
+
|
|
49
|
+
- Uses a `visitedNodes` set to prevent infinite loops in cyclic graphs (though expression trees are typically acyclic).
|
|
50
|
+
- Only checks rules relevant to each node type, optimizing performance.
|
|
51
|
+
- For each node, it stops after finding the first applicable rule.
|
|
52
|
+
|
|
53
|
+
### `getAvailableRulesForNode(node)`
|
|
54
|
+
|
|
55
|
+
**Debug Function:** Lists all simplification rules that are potentially relevant to a given `omdNode`'s type, regardless of whether they can currently be applied.
|
|
56
|
+
|
|
57
|
+
- **`node`** (`omdNode`): The node to check.
|
|
58
|
+
- **Returns**: `Array<string>` - An array of rule names.
|
|
59
|
+
|
|
60
|
+
### `getApplicableRulesForNode(node)`
|
|
61
|
+
|
|
62
|
+
**Debug Function:** Lists simplification rules that can actually be applied to a given `omdNode` based on its current structure and values.
|
|
63
|
+
|
|
64
|
+
- **`node`** (`omdNode`): The node to check.
|
|
65
|
+
- **Returns**: `Array<string>` - An array of applicable rule names.
|
|
66
|
+
|
|
67
|
+
**Notes:**
|
|
68
|
+
|
|
69
|
+
- Handles errors gracefully by skipping rule checks that might fail.
|
|
70
|
+
|
|
71
|
+
## Integration with `omdNode`
|
|
72
|
+
|
|
73
|
+
This module also sets the `simplifyStep` function as the global simplification handler for `omdNode` instances. This allows any `omdNode` to call its `simplify()` method, which will internally use the `simplifyStep` function defined here.
|
|
74
|
+
|
|
75
|
+
## See Also
|
|
76
|
+
|
|
77
|
+
- [`Simplification Rules`](./simplificationRules.md) - Details the individual simplification rules.
|
|
78
|
+
- [`Simplification Engine`](./simplificationEngine.md) - (If applicable, for higher-level orchestration).
|
|
79
79
|
- [`Configuration Options`](./configuration-options.md) - For configuring simplification behavior.
|
package/docs/api/omdSqrtNode.md
CHANGED
|
@@ -1,144 +1,144 @@
|
|
|
1
|
-
# omdSqrtNode
|
|
2
|
-
|
|
3
|
-
Represents a square root node in the mathematical expression tree. It handles the rendering of the radical symbol (√) and the expression under the root (radicand), ensuring correct visual layout and mathematical behavior.
|
|
4
|
-
|
|
5
|
-
## Class Definition
|
|
6
|
-
|
|
7
|
-
```javascript
|
|
8
|
-
export class omdSqrtNode extends omdNode
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## Constructor
|
|
12
|
-
|
|
13
|
-
### `new omdSqrtNode(astNodeData)`
|
|
14
|
-
|
|
15
|
-
Creates a new `omdSqrtNode` instance.
|
|
16
|
-
|
|
17
|
-
- **`astNodeData`** (`object`): The math.js AST node containing square root function information. It should have an `args` property, which is an array containing a single AST node for the radicand. The constructor also initializes the visual `radicalPath` and `radicalLine` elements.
|
|
18
|
-
|
|
19
|
-
## Static Methods
|
|
20
|
-
|
|
21
|
-
### `fromString(expressionString)`
|
|
22
|
-
|
|
23
|
-
Creates an `omdSqrtNode` from a string representation of a square root function. Requires `window.math` (math.js) to be available globally for parsing.
|
|
24
|
-
|
|
25
|
-
- **`expressionString`** (`string`): The square root expression as a string (e.g., `"sqrt(x+1)"`).
|
|
26
|
-
- **Returns**: `omdSqrtNode` - A new instance.
|
|
27
|
-
- **Throws**: `Error` if `math.js` is not available, if the string cannot be parsed, or if it does not represent a valid `sqrt` function.
|
|
28
|
-
|
|
29
|
-
## Public Properties
|
|
30
|
-
|
|
31
|
-
- **`argument`** (`omdNode`): The node representing the radicand (the expression under the root).
|
|
32
|
-
- **`value`** (`string`): Always `"sqrt"` for this node type.
|
|
33
|
-
- **`radicalPath`** (`jsvgPath`): The SVG path element that draws the radical symbol (√).
|
|
34
|
-
- **`radicalLine`** (`jsvgLine`): The SVG line element that draws the horizontal line above the radicand.
|
|
35
|
-
- **`args`** (`Array<object>`): The raw AST arguments passed to the constructor.
|
|
36
|
-
|
|
37
|
-
## Public Methods
|
|
38
|
-
|
|
39
|
-
### `computeDimensions()`
|
|
40
|
-
|
|
41
|
-
Calculates the dimensions of the square root node. It scales down the font size of the `argument`, computes its dimensions, and then determines the overall width (radical width + spacing + argument width) and height (argument height + extra height for the radical top).
|
|
42
|
-
|
|
43
|
-
- **Overrides**: `omdNode.computeDimensions()`.
|
|
44
|
-
|
|
45
|
-
### `updateLayout()`
|
|
46
|
-
|
|
47
|
-
Updates the layout of the square root node. It positions the `argument` node, then draws and positions the `radicalPath` and `radicalLine` elements relative to the argument, ensuring the radical symbol correctly encloses the radicand.
|
|
48
|
-
|
|
49
|
-
- **Overrides**: `omdNode.updateLayout()`.
|
|
50
|
-
|
|
51
|
-
### `clone()`
|
|
52
|
-
|
|
53
|
-
Creates a deep, structural clone of the square root node, including its `argument` node and visual elements. The cloned node's `provenance` array is updated to include the original node's ID.
|
|
54
|
-
|
|
55
|
-
- **Returns**: `omdSqrtNode` - A new, identical instance of the square root node.
|
|
56
|
-
|
|
57
|
-
### `highlightAll()`
|
|
58
|
-
|
|
59
|
-
Applies a highlight to the square root node itself and recursively highlights its `argument` node.
|
|
60
|
-
|
|
61
|
-
### `unhighlightAll()`
|
|
62
|
-
|
|
63
|
-
Removes the highlight from the square root node and recursively unhighlights its `argument` node.
|
|
64
|
-
|
|
65
|
-
### `toMathJSNode()`
|
|
66
|
-
|
|
67
|
-
Converts the `omdSqrtNode` back into its math.js AST representation. It creates a `FunctionNode` with `fn: 'sqrt'` and the converted `argument` AST.
|
|
68
|
-
|
|
69
|
-
- **Returns**: `object` - A math.js `FunctionNode` for the square root operation. The returned object also includes a `clone` method for compatibility.
|
|
70
|
-
|
|
71
|
-
### `toString()`
|
|
72
|
-
|
|
73
|
-
Converts the square root node to its string representation (e.g., `"sqrt(x+1)"`).
|
|
74
|
-
|
|
75
|
-
- **Returns**: `string`.
|
|
76
|
-
|
|
77
|
-
### `evaluate(variables)`
|
|
78
|
-
|
|
79
|
-
Evaluates the square root expression. It evaluates the `argument` and then calculates its square root.
|
|
80
|
-
|
|
81
|
-
- **`variables`** (`object`): A map of variable names to their numeric values.
|
|
82
|
-
- **Returns**: `number` - The evaluated square root, or `NaN` if the radicand cannot be evaluated or is negative.
|
|
83
|
-
|
|
84
|
-
### `isSquareRoot()`
|
|
85
|
-
|
|
86
|
-
Checks if the node represents a square root. Always returns `true` for `omdSqrtNode`.
|
|
87
|
-
|
|
88
|
-
- **Returns**: `boolean`.
|
|
89
|
-
|
|
90
|
-
### `isCubeRoot()`
|
|
91
|
-
|
|
92
|
-
Checks if the node represents a cube root. Always returns `false` for `omdSqrtNode`.
|
|
93
|
-
|
|
94
|
-
- **Returns**: `boolean`.
|
|
95
|
-
|
|
96
|
-
### `toPowerForm()`
|
|
97
|
-
|
|
98
|
-
Converts the square root node into an equivalent `omdPowerNode` representation (e.g., `sqrt(x)` becomes `x^(0.5)`).
|
|
99
|
-
|
|
100
|
-
- **Returns**: `omdPowerNode` - The equivalent power expression, or `null` if the `argument` is missing.
|
|
101
|
-
|
|
102
|
-
## Internal Methods
|
|
103
|
-
|
|
104
|
-
- **`parseValue()`**: Sets the `value` property to `"sqrt"`.
|
|
105
|
-
- **`createArgumentNode()`**: Creates an `omdNode` instance for the radicand from its AST, and adds it as a child.
|
|
106
|
-
- **`createRadicalElements()`**: Creates and initializes the `jsvgPath` for the radical symbol and the `jsvgLine` for the horizontal bar, adding them as children.
|
|
107
|
-
|
|
108
|
-
## Example
|
|
109
|
-
|
|
110
|
-
```javascript
|
|
111
|
-
import { omdSqrtNode } from '@teachinglab/omd';
|
|
112
|
-
import * as math from 'mathjs';
|
|
113
|
-
|
|
114
|
-
// Create from AST
|
|
115
|
-
const node = new omdSqrtNode({
|
|
116
|
-
type: 'FunctionNode',
|
|
117
|
-
fn: { name: 'sqrt' },
|
|
118
|
-
args: [ { type: 'SymbolNode', name: 'x' } ]
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
// Or from string
|
|
122
|
-
const node2 = omdSqrtNode.fromString('sqrt(x+1)');
|
|
123
|
-
|
|
124
|
-
// Render and layout
|
|
125
|
-
node.setFontSize(24);
|
|
126
|
-
node.initialize();
|
|
127
|
-
|
|
128
|
-
// Evaluate
|
|
129
|
-
const val = node.evaluate({ x: 9 }); // 3
|
|
130
|
-
|
|
131
|
-
// Convert to power form
|
|
132
|
-
const powerNode = node.toPowerForm(); // x^(1/2)
|
|
133
|
-
|
|
134
|
-
// Add to an SVG container to display
|
|
135
|
-
// const svgContainer = new jsvgContainer();
|
|
136
|
-
// svgContainer.addChild(node);
|
|
137
|
-
// document.body.appendChild(svgContainer.svgObject);
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
## See Also
|
|
141
|
-
|
|
142
|
-
- [`omdNode`](./omdNode.md) - The base class.
|
|
143
|
-
- [`omdPowerNode`](./omdPowerNode.md) - For power form (e.g., `x^(1/2)`).
|
|
144
|
-
- [`omdFunctionNode`](./omdFunctionNode.md) - For other mathematical functions.
|
|
1
|
+
# omdSqrtNode
|
|
2
|
+
|
|
3
|
+
Represents a square root node in the mathematical expression tree. It handles the rendering of the radical symbol (√) and the expression under the root (radicand), ensuring correct visual layout and mathematical behavior.
|
|
4
|
+
|
|
5
|
+
## Class Definition
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
export class omdSqrtNode extends omdNode
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Constructor
|
|
12
|
+
|
|
13
|
+
### `new omdSqrtNode(astNodeData)`
|
|
14
|
+
|
|
15
|
+
Creates a new `omdSqrtNode` instance.
|
|
16
|
+
|
|
17
|
+
- **`astNodeData`** (`object`): The math.js AST node containing square root function information. It should have an `args` property, which is an array containing a single AST node for the radicand. The constructor also initializes the visual `radicalPath` and `radicalLine` elements.
|
|
18
|
+
|
|
19
|
+
## Static Methods
|
|
20
|
+
|
|
21
|
+
### `fromString(expressionString)`
|
|
22
|
+
|
|
23
|
+
Creates an `omdSqrtNode` from a string representation of a square root function. Requires `window.math` (math.js) to be available globally for parsing.
|
|
24
|
+
|
|
25
|
+
- **`expressionString`** (`string`): The square root expression as a string (e.g., `"sqrt(x+1)"`).
|
|
26
|
+
- **Returns**: `omdSqrtNode` - A new instance.
|
|
27
|
+
- **Throws**: `Error` if `math.js` is not available, if the string cannot be parsed, or if it does not represent a valid `sqrt` function.
|
|
28
|
+
|
|
29
|
+
## Public Properties
|
|
30
|
+
|
|
31
|
+
- **`argument`** (`omdNode`): The node representing the radicand (the expression under the root).
|
|
32
|
+
- **`value`** (`string`): Always `"sqrt"` for this node type.
|
|
33
|
+
- **`radicalPath`** (`jsvgPath`): The SVG path element that draws the radical symbol (√).
|
|
34
|
+
- **`radicalLine`** (`jsvgLine`): The SVG line element that draws the horizontal line above the radicand.
|
|
35
|
+
- **`args`** (`Array<object>`): The raw AST arguments passed to the constructor.
|
|
36
|
+
|
|
37
|
+
## Public Methods
|
|
38
|
+
|
|
39
|
+
### `computeDimensions()`
|
|
40
|
+
|
|
41
|
+
Calculates the dimensions of the square root node. It scales down the font size of the `argument`, computes its dimensions, and then determines the overall width (radical width + spacing + argument width) and height (argument height + extra height for the radical top).
|
|
42
|
+
|
|
43
|
+
- **Overrides**: `omdNode.computeDimensions()`.
|
|
44
|
+
|
|
45
|
+
### `updateLayout()`
|
|
46
|
+
|
|
47
|
+
Updates the layout of the square root node. It positions the `argument` node, then draws and positions the `radicalPath` and `radicalLine` elements relative to the argument, ensuring the radical symbol correctly encloses the radicand.
|
|
48
|
+
|
|
49
|
+
- **Overrides**: `omdNode.updateLayout()`.
|
|
50
|
+
|
|
51
|
+
### `clone()`
|
|
52
|
+
|
|
53
|
+
Creates a deep, structural clone of the square root node, including its `argument` node and visual elements. The cloned node's `provenance` array is updated to include the original node's ID.
|
|
54
|
+
|
|
55
|
+
- **Returns**: `omdSqrtNode` - A new, identical instance of the square root node.
|
|
56
|
+
|
|
57
|
+
### `highlightAll()`
|
|
58
|
+
|
|
59
|
+
Applies a highlight to the square root node itself and recursively highlights its `argument` node.
|
|
60
|
+
|
|
61
|
+
### `unhighlightAll()`
|
|
62
|
+
|
|
63
|
+
Removes the highlight from the square root node and recursively unhighlights its `argument` node.
|
|
64
|
+
|
|
65
|
+
### `toMathJSNode()`
|
|
66
|
+
|
|
67
|
+
Converts the `omdSqrtNode` back into its math.js AST representation. It creates a `FunctionNode` with `fn: 'sqrt'` and the converted `argument` AST.
|
|
68
|
+
|
|
69
|
+
- **Returns**: `object` - A math.js `FunctionNode` for the square root operation. The returned object also includes a `clone` method for compatibility.
|
|
70
|
+
|
|
71
|
+
### `toString()`
|
|
72
|
+
|
|
73
|
+
Converts the square root node to its string representation (e.g., `"sqrt(x+1)"`).
|
|
74
|
+
|
|
75
|
+
- **Returns**: `string`.
|
|
76
|
+
|
|
77
|
+
### `evaluate(variables)`
|
|
78
|
+
|
|
79
|
+
Evaluates the square root expression. It evaluates the `argument` and then calculates its square root.
|
|
80
|
+
|
|
81
|
+
- **`variables`** (`object`): A map of variable names to their numeric values.
|
|
82
|
+
- **Returns**: `number` - The evaluated square root, or `NaN` if the radicand cannot be evaluated or is negative.
|
|
83
|
+
|
|
84
|
+
### `isSquareRoot()`
|
|
85
|
+
|
|
86
|
+
Checks if the node represents a square root. Always returns `true` for `omdSqrtNode`.
|
|
87
|
+
|
|
88
|
+
- **Returns**: `boolean`.
|
|
89
|
+
|
|
90
|
+
### `isCubeRoot()`
|
|
91
|
+
|
|
92
|
+
Checks if the node represents a cube root. Always returns `false` for `omdSqrtNode`.
|
|
93
|
+
|
|
94
|
+
- **Returns**: `boolean`.
|
|
95
|
+
|
|
96
|
+
### `toPowerForm()`
|
|
97
|
+
|
|
98
|
+
Converts the square root node into an equivalent `omdPowerNode` representation (e.g., `sqrt(x)` becomes `x^(0.5)`).
|
|
99
|
+
|
|
100
|
+
- **Returns**: `omdPowerNode` - The equivalent power expression, or `null` if the `argument` is missing.
|
|
101
|
+
|
|
102
|
+
## Internal Methods
|
|
103
|
+
|
|
104
|
+
- **`parseValue()`**: Sets the `value` property to `"sqrt"`.
|
|
105
|
+
- **`createArgumentNode()`**: Creates an `omdNode` instance for the radicand from its AST, and adds it as a child.
|
|
106
|
+
- **`createRadicalElements()`**: Creates and initializes the `jsvgPath` for the radical symbol and the `jsvgLine` for the horizontal bar, adding them as children.
|
|
107
|
+
|
|
108
|
+
## Example
|
|
109
|
+
|
|
110
|
+
```javascript
|
|
111
|
+
import { omdSqrtNode } from '@teachinglab/omd';
|
|
112
|
+
import * as math from 'mathjs';
|
|
113
|
+
|
|
114
|
+
// Create from AST
|
|
115
|
+
const node = new omdSqrtNode({
|
|
116
|
+
type: 'FunctionNode',
|
|
117
|
+
fn: { name: 'sqrt' },
|
|
118
|
+
args: [ { type: 'SymbolNode', name: 'x' } ]
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
// Or from string
|
|
122
|
+
const node2 = omdSqrtNode.fromString('sqrt(x+1)');
|
|
123
|
+
|
|
124
|
+
// Render and layout
|
|
125
|
+
node.setFontSize(24);
|
|
126
|
+
node.initialize();
|
|
127
|
+
|
|
128
|
+
// Evaluate
|
|
129
|
+
const val = node.evaluate({ x: 9 }); // 3
|
|
130
|
+
|
|
131
|
+
// Convert to power form
|
|
132
|
+
const powerNode = node.toPowerForm(); // x^(1/2)
|
|
133
|
+
|
|
134
|
+
// Add to an SVG container to display
|
|
135
|
+
// const svgContainer = new jsvgContainer();
|
|
136
|
+
// svgContainer.addChild(node);
|
|
137
|
+
// document.body.appendChild(svgContainer.svgObject);
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## See Also
|
|
141
|
+
|
|
142
|
+
- [`omdNode`](./omdNode.md) - The base class.
|
|
143
|
+
- [`omdPowerNode`](./omdPowerNode.md) - For power form (e.g., `x^(1/2)`).
|
|
144
|
+
- [`omdFunctionNode`](./omdFunctionNode.md) - For other mathematical functions.
|