@teachinglab/omd 0.1.4 → 0.1.5

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.
Files changed (42) hide show
  1. package/canvas/tools/EraserTool.js +1 -1
  2. package/canvas/tools/PencilTool.js +1 -1
  3. package/canvas/tools/SelectTool.js +1 -1
  4. package/docs/api/configuration-options.md +198 -104
  5. package/docs/api/eventManager.md +83 -68
  6. package/docs/api/focusFrameManager.md +145 -150
  7. package/docs/api/index.md +106 -91
  8. package/docs/api/main.md +63 -58
  9. package/docs/api/omdBinaryExpressionNode.md +86 -227
  10. package/docs/api/omdCanvas.md +84 -142
  11. package/docs/api/omdConfigManager.md +113 -192
  12. package/docs/api/omdConstantNode.md +53 -117
  13. package/docs/api/omdDisplay.md +87 -121
  14. package/docs/api/omdEquationNode.md +174 -161
  15. package/docs/api/omdEquationSequenceNode.md +259 -301
  16. package/docs/api/omdEquationStack.md +157 -103
  17. package/docs/api/omdFunctionNode.md +83 -141
  18. package/docs/api/omdGroupNode.md +79 -182
  19. package/docs/api/omdHelpers.md +88 -96
  20. package/docs/api/omdLeafNode.md +86 -163
  21. package/docs/api/omdNode.md +202 -101
  22. package/docs/api/omdOperationDisplayNode.md +118 -139
  23. package/docs/api/omdOperatorNode.md +92 -127
  24. package/docs/api/omdParenthesisNode.md +134 -122
  25. package/docs/api/omdPopup.md +192 -117
  26. package/docs/api/omdPowerNode.md +132 -127
  27. package/docs/api/omdRationalNode.md +145 -128
  28. package/docs/api/omdSimplification.md +79 -110
  29. package/docs/api/omdSqrtNode.md +144 -79
  30. package/docs/api/omdStepVisualizer.md +147 -115
  31. package/docs/api/omdStepVisualizerHighlighting.md +66 -61
  32. package/docs/api/omdStepVisualizerInteractiveSteps.md +109 -129
  33. package/docs/api/omdStepVisualizerLayout.md +71 -60
  34. package/docs/api/omdStepVisualizerTextBoxes.md +77 -68
  35. package/docs/api/omdToolbar.md +131 -102
  36. package/docs/api/omdTranscriptionService.md +96 -76
  37. package/docs/api/omdTreeDiff.md +170 -134
  38. package/docs/api/omdUnaryExpressionNode.md +137 -174
  39. package/docs/api/omdUtilities.md +83 -70
  40. package/docs/api/omdVariableNode.md +123 -148
  41. package/index.js +2 -2
  42. package/package.json +1 -1
@@ -1,110 +1,79 @@
1
- # omdSimplification
2
-
3
- Core module that provides the main entry points for expression simplification.
4
-
5
- ## Functions
6
-
7
-
8
- ### `simplifyExpression(rootNode)`
9
-
10
- Simplifies an entire mathematical expression tree by repeatedly applying simplification rules.
11
-
12
- **Parameters:**
13
- - `rootNode` (`omdNode`): The root node of the expression to simplify.
14
-
15
- **Returns:** `{ foldedCount: number, newRoot: omdNode }`
16
- - `foldedCount`: Number of simplifications applied.
17
- - `newRoot`: The simplified expression tree (fresh clone if any simplifications occurred).
18
-
19
- **Notes:**
20
- - Limited to 50 iterations to prevent infinite loops.
21
- - Preserves font size from the original node.
22
- - Handles errors gracefully by returning the original node if an error occurs.
23
-
24
- **Example:**
25
- ```javascript
26
- import { simplifyExpression } from './omd/simplification/omdSimplification.js';
27
-
28
- const result = simplifyExpression(node);
29
- console.log(result.newRoot.toString());
30
- console.log(`Applied ${result.foldedCount} simplifications`);
31
- ```
32
-
33
-
34
- ### `simplifyStep(rootNode)`
35
-
36
- Applies a single simplification step to the expression tree.
37
-
38
- **Parameters:**
39
- - `rootNode` (`omdNode`): The root node of the expression.
40
-
41
- **Returns:** `{ foldedCount: number, newRoot: omdNode, historyEntry: object|null }`
42
- - `foldedCount`: 1 if a simplification was applied, 0 otherwise.
43
- - `newRoot`: The expression tree after applying the step.
44
- - `historyEntry`: Object describing the applied simplification, or `null` if none.
45
-
46
- **Notes:**
47
- - Only applies the first matching rule found.
48
- - Preserves font size from the original node.
49
- - Handles errors gracefully by returning the original node if an error occurs.
50
-
51
- **Example:**
52
- ```javascript
53
- import { simplifyStep } from './omd/simplification/omdSimplification.js';
54
-
55
- const stepResult = simplifyStep(node);
56
- if (stepResult.historyEntry) {
57
- console.log('Applied:', stepResult.historyEntry);
58
- }
59
- ```
60
-
61
-
62
- ### `findSimplificationOpportunities(rootNode)`
63
-
64
- Finds all possible simplification opportunities in an expression tree.
65
-
66
- **Parameters:**
67
- - `rootNode` (`omdNode`): The root node to search.
68
-
69
- **Returns:** `Array<{ node: omdNode, rule: object, ruleData: object }>`
70
- - `node`: The node where a rule can be applied.
71
- - `rule`: The applicable rule.
72
- - `ruleData`: Data needed to apply the rule.
73
-
74
- **Notes:**
75
- - Uses depth-first traversal.
76
- - Skips already visited nodes.
77
- - Only checks rules relevant to each node type.
78
- - Stops after finding the first applicable rule for each node.
79
-
80
-
81
- ### Debug Functions
82
-
83
- > **Note:** The following functions are intended for development and debugging, not for production use.
84
-
85
- #### `getAvailableRulesForNode(node)`
86
-
87
- Lists all rules that could potentially apply to a node.
88
-
89
- **Parameters:**
90
- - `node` (`omdNode`): The node to check.
91
-
92
- **Returns:** `Array<string>` - Array of rule names.
93
-
94
- #### `getApplicableRulesForNode(node)`
95
-
96
- Lists rules that can actually be applied to a node.
97
-
98
- **Parameters:**
99
- - `node` (`omdNode`): The node to check.
100
-
101
- **Returns:** `Array<string>` - Array of applicable rule names.
102
-
103
- **Notes:**
104
- - Handles errors gracefully by skipping failed rule checks.
105
-
106
- ## See Also
107
-
108
- - [Simplification Rules](./simplificationRules.md)
109
- - [Simplification Engine](./simplificationEngine.md)
110
- - [Configuration Options](./configuration-options.md)
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
+ - [`Configuration Options`](./configuration-options.md) - For configuring simplification behavior.
@@ -1,79 +1,144 @@
1
- # omdSqrtNode
2
-
3
- Represents a square root node in the mathematical expression tree. Handles rendering of the radical symbol and the expression under the root.
4
-
5
- ## Class: `omdSqrtNode extends omdNode`
6
-
7
- ```js
8
- import { omdSqrtNode } from './omd/nodes/omdSqrtNode.js';
9
- ```
10
-
11
- ### Constructor
12
-
13
- ```js
14
- new omdSqrtNode(astNodeData)
15
- ```
16
- - `astNodeData` {Object}: Math.js AST node with a single argument (the radicand)
17
-
18
- ### Properties
19
-
20
- Inherits all properties from [omdNode](./omdNode.md), plus:
21
-
22
- - `argument` (`omdNode`): The radicand node (expression under the root)
23
- - `value` (`string`): Always "sqrt"
24
- - `radicalPath` (`jsvgPath`): The radical symbol (√)
25
- - `radicalLine` (`jsvgLine`): The horizontal line above the radicand
26
-
27
- ### Methods
28
-
29
- Inherits all methods from [omdNode](./omdNode.md), plus:
30
-
31
- - `toString()`: Returns string representation, e.g. `sqrt(x+1)`
32
- - `toMathJSNode()`: Converts to a math.js-compatible AST node
33
- - `evaluate(variables)`: Evaluates the square root numerically
34
- - `variables` {Object}: Variable name to value mapping
35
- - **Returns:** `number` (or `NaN` if radicand can't be evaluated)
36
- - `simplify()`: Attempts to simplify the expression (delegates to central simplification engine)
37
- - **Returns:** `{ success, newRoot, message }`
38
- - `clone()`: Deep clone of the node
39
- - `highlightAll()`, `unhighlightAll()`: Recursively highlight/unhighlight this node and its argument
40
- - `toPowerForm()`: Returns an equivalent `omdPowerNode` (x^(1/2)), or `null` if no argument
41
-
42
- #### Static Methods
43
-
44
- - `fromString(expressionString)`: Create a sqrt node from a string (must be a sqrt function)
45
- - `expressionString` {string}
46
- - **Returns:** `omdSqrtNode`
47
- - **Throws:** Error if not a sqrt function or if math.js is not available
48
-
49
- ### Example
50
-
51
- ```js
52
- import { omdSqrtNode } from './omd/nodes/omdSqrtNode.js';
53
-
54
- // Create from AST
55
- const node = new omdSqrtNode({
56
- type: 'FunctionNode',
57
- fn: { name: 'sqrt' },
58
- args: [ { type: 'SymbolNode', name: 'x' } ]
59
- });
60
-
61
- // Or from string (if static method is available)
62
- const node2 = omdSqrtNode.fromString('sqrt(x+1)');
63
-
64
- // Render and layout
65
- node.setFontSize(24);
66
- node.computeDimensions();
67
- node.updateLayout();
68
-
69
- // Evaluate
70
- const val = node.evaluate({ x: 9 }); // 3
71
-
72
- // Convert to power form
73
- const powerNode = node.toPowerForm(); // x^(1/2)
74
- ```
75
-
76
- ### See Also
77
- - [omdNode](./omdNode.md) — Parent class
78
- - [omdPowerNode](./omdPowerNode.md) — For power form (x^(1/2))
79
- - [omdFunctionNode](./omdFunctionNode.md) For other 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.