@teachinglab/omd 0.5.7 → 0.6.0
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 +190 -77
- package/README.old.md +138 -0
- package/index.js +3 -0
- package/npm-docs/DOCUMENTATION_SUMMARY.md +220 -0
- package/npm-docs/README.md +251 -0
- package/npm-docs/api/api-reference.md +85 -0
- package/npm-docs/api/configuration-options.md +198 -0
- package/npm-docs/api/eventManager.md +83 -0
- package/npm-docs/api/expression-nodes.md +561 -0
- package/npm-docs/api/focusFrameManager.md +145 -0
- package/npm-docs/api/index.md +106 -0
- package/npm-docs/api/main.md +63 -0
- package/npm-docs/api/omdBinaryExpressionNode.md +86 -0
- package/npm-docs/api/omdCanvas.md +84 -0
- package/npm-docs/api/omdConfigManager.md +113 -0
- package/npm-docs/api/omdConstantNode.md +53 -0
- package/npm-docs/api/omdDisplay.md +87 -0
- package/npm-docs/api/omdEquationNode.md +174 -0
- package/npm-docs/api/omdEquationSequenceNode.md +259 -0
- package/npm-docs/api/omdEquationStack.md +193 -0
- package/npm-docs/api/omdFunctionNode.md +83 -0
- package/npm-docs/api/omdGroupNode.md +79 -0
- package/npm-docs/api/omdHelpers.md +88 -0
- package/npm-docs/api/omdLeafNode.md +86 -0
- package/npm-docs/api/omdNode.md +202 -0
- package/npm-docs/api/omdOperationDisplayNode.md +118 -0
- package/npm-docs/api/omdOperatorNode.md +92 -0
- package/npm-docs/api/omdParenthesisNode.md +134 -0
- package/npm-docs/api/omdPopup.md +192 -0
- package/npm-docs/api/omdPowerNode.md +132 -0
- package/npm-docs/api/omdRationalNode.md +145 -0
- package/npm-docs/api/omdSequenceNode.md +128 -0
- package/npm-docs/api/omdSimplification.md +79 -0
- package/npm-docs/api/omdSqrtNode.md +144 -0
- package/npm-docs/api/omdStepVisualizer.md +147 -0
- package/npm-docs/api/omdStepVisualizerHighlighting.md +66 -0
- package/npm-docs/api/omdStepVisualizerInteractiveSteps.md +109 -0
- package/npm-docs/api/omdStepVisualizerLayout.md +71 -0
- package/npm-docs/api/omdStepVisualizerNodeUtils.md +140 -0
- package/npm-docs/api/omdStepVisualizerTextBoxes.md +77 -0
- package/npm-docs/api/omdToolbar.md +131 -0
- package/npm-docs/api/omdTranscriptionService.md +96 -0
- package/npm-docs/api/omdTreeDiff.md +170 -0
- package/npm-docs/api/omdUnaryExpressionNode.md +137 -0
- package/npm-docs/api/omdUtilities.md +83 -0
- package/npm-docs/api/omdVariableNode.md +123 -0
- package/npm-docs/api/selectTool.md +74 -0
- package/npm-docs/api/simplificationEngine.md +98 -0
- package/npm-docs/api/simplificationRules.md +77 -0
- package/npm-docs/api/simplificationUtils.md +64 -0
- package/npm-docs/api/transcribe.md +43 -0
- package/npm-docs/guides/equations.md +854 -0
- package/npm-docs/guides/factory-functions.md +354 -0
- package/npm-docs/guides/getting-started.md +318 -0
- package/npm-docs/guides/quick-examples.md +525 -0
- package/npm-docs/guides/visualizations.md +682 -0
- package/npm-docs/json-schemas.md +826 -0
- package/omd/utils/omdTranscriptionService.js +1 -1
- package/package.json +4 -3
- package/src/index.js +2 -0
- package/src/omdFactory.js +150 -0
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# omdRationalNode
|
|
2
|
+
|
|
3
|
+
Represents a fraction, with a numerator and a denominator, such as `1/2` or `(x+1)/(x-1)`. It handles the distinct visual layout of a fraction, with the numerator positioned above the denominator, separated by a horizontal line.
|
|
4
|
+
|
|
5
|
+
## Class Definition
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
export class omdRationalNode extends omdNode
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Constructor
|
|
12
|
+
|
|
13
|
+
### `new omdRationalNode(astNodeData)`
|
|
14
|
+
|
|
15
|
+
Creates a new `omdRationalNode` instance.
|
|
16
|
+
|
|
17
|
+
- **`astNodeData`** (`object`): The math.js AST for the division operation. It must be an `OperatorNode` with `op: '/'` and an `args` array containing two elements: the numerator and the denominator. The constructor also creates and initializes the visual `fractionLine`.
|
|
18
|
+
|
|
19
|
+
## Static Methods
|
|
20
|
+
|
|
21
|
+
### `fromString(expressionString)`
|
|
22
|
+
|
|
23
|
+
Creates an `omdRationalNode` from a string containing a division. Requires `window.math` (math.js) to be available globally for parsing.
|
|
24
|
+
|
|
25
|
+
- **`expressionString`** (`string`): The fractional expression as a string (e.g., `"x / 2"`, `"(a+b)/(a-b)"`).
|
|
26
|
+
- **Returns**: `omdRationalNode` - 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 division expression.
|
|
28
|
+
|
|
29
|
+
## Public Properties
|
|
30
|
+
|
|
31
|
+
- **`numerator`** (`omdNode`): The node representing the top part of the fraction.
|
|
32
|
+
- **`denominator`** (`omdNode`): The node representing the bottom part of the fraction.
|
|
33
|
+
- **`fractionLine`** (`jsvgLine`): The visual horizontal line separating the numerator and denominator.
|
|
34
|
+
|
|
35
|
+
## Public Methods
|
|
36
|
+
|
|
37
|
+
### `computeDimensions()`
|
|
38
|
+
|
|
39
|
+
Calculates the dimensions of the rational node. It scales down the font size of the numerator and denominator, computes their individual dimensions, and then determines the total width (maximum of numerator/denominator width plus spacing) and total height (sum of numerator, denominator, and padding for the fraction line).
|
|
40
|
+
|
|
41
|
+
- **Overrides**: `omdNode.computeDimensions()`.
|
|
42
|
+
|
|
43
|
+
### `updateLayout()`
|
|
44
|
+
|
|
45
|
+
Updates the layout of the rational node. It positions the `numerator` at the top, the `fractionLine` in the middle, and the `denominator` at the bottom, ensuring they are horizontally centered and have appropriate vertical spacing.
|
|
46
|
+
|
|
47
|
+
- **Overrides**: `omdNode.updateLayout()`.
|
|
48
|
+
|
|
49
|
+
### `getAlignmentBaseline()`
|
|
50
|
+
|
|
51
|
+
Calculates the vertical alignment point for the node. For a rational node, this is the y-position of the fraction bar, which serves as the visual baseline.
|
|
52
|
+
|
|
53
|
+
- **Overrides**: `omdNode.getAlignmentBaseline()`.
|
|
54
|
+
|
|
55
|
+
### `isConstant()`
|
|
56
|
+
|
|
57
|
+
Checks if both the numerator and the denominator are constant numerical values.
|
|
58
|
+
|
|
59
|
+
- **Returns**: `boolean`.
|
|
60
|
+
|
|
61
|
+
### `getRationalValue()`
|
|
62
|
+
|
|
63
|
+
Retrieves the rational value of the node as a numerator/denominator pair. This method only works if the fraction is constant.
|
|
64
|
+
|
|
65
|
+
- **Returns**: `{ num: number, den: number }`.
|
|
66
|
+
- **Throws**: `Error` if the rational node is not constant.
|
|
67
|
+
|
|
68
|
+
### `getValue()`
|
|
69
|
+
|
|
70
|
+
Retrieves the numerical value of the rational node by dividing the numerator's value by the denominator's value. This method only works if the fraction is constant.
|
|
71
|
+
|
|
72
|
+
- **Returns**: `number`.
|
|
73
|
+
- **Throws**: `Error` if the rational node is not constant or if division by zero occurs.
|
|
74
|
+
|
|
75
|
+
### `clone()`
|
|
76
|
+
|
|
77
|
+
Creates a deep, structural clone of the rational node, including its `numerator` and `denominator` nodes. The `fractionLine` is also recreated. The cloned node's `provenance` array is updated to include the original node's ID.
|
|
78
|
+
|
|
79
|
+
- **Returns**: `omdRationalNode` - A new, identical instance of the rational node.
|
|
80
|
+
|
|
81
|
+
### `toMathJSNode()`
|
|
82
|
+
|
|
83
|
+
Converts the `omdRationalNode` back into its math.js AST representation. It creates an `OperatorNode` with `op: '/'` and `fn: 'divide'`, containing the converted numerator and denominator ASTs.
|
|
84
|
+
|
|
85
|
+
- **Returns**: `object` - A math.js `OperatorNode` for division. The returned object also includes a `clone` method for compatibility.
|
|
86
|
+
|
|
87
|
+
### `toString()`
|
|
88
|
+
|
|
89
|
+
Converts the node to its string representation, adding parentheses around the numerator and/or denominator if they are complex expressions (e.g., binary expressions) to maintain correct order of operations.
|
|
90
|
+
|
|
91
|
+
- **Returns**: `string` - e.g., `"(x + 1) / (x - 1)"`.
|
|
92
|
+
|
|
93
|
+
### `evaluate(variables)`
|
|
94
|
+
|
|
95
|
+
Evaluates the rational expression by evaluating the numerator and denominator and then performing the division.
|
|
96
|
+
|
|
97
|
+
- **`variables`** (`object`): A map of variable names to their values.
|
|
98
|
+
- **Returns**: `number` - The result of the division.
|
|
99
|
+
- **Throws**: `Error` if the denominator evaluates to zero.
|
|
100
|
+
|
|
101
|
+
### `reduce()`
|
|
102
|
+
|
|
103
|
+
If the fraction is constant, this method reduces it to its lowest terms (e.g., `4/8` becomes `1/2`). If the reduced denominator is `1`, it returns an `omdConstantNode` representing the integer value. It uses a `gcd` (greatest common divisor) helper function internally.
|
|
104
|
+
|
|
105
|
+
- **Returns**: `omdRationalNode` | `omdConstantNode` - The reduced fraction node, or an `omdConstantNode` if it simplifies to an integer.
|
|
106
|
+
|
|
107
|
+
### `isProper()`
|
|
108
|
+
|
|
109
|
+
Checks if the fraction is proper (the absolute value of the numerator is less than the absolute value of the denominator). This check only applies to constant fractions.
|
|
110
|
+
|
|
111
|
+
- **Returns**: `boolean` | `null` - `true` if proper, `false` if improper, `null` if the fraction is not constant.
|
|
112
|
+
|
|
113
|
+
## Internal Methods
|
|
114
|
+
|
|
115
|
+
- **`parseValue()`**: Sets the `value` property to `"/"`.
|
|
116
|
+
- **`createOperand(ast)`**: Creates an `omdNode` instance for the numerator or denominator from its AST, attempting to preserve provenance.
|
|
117
|
+
|
|
118
|
+
## Example
|
|
119
|
+
|
|
120
|
+
```javascript
|
|
121
|
+
import { omdRationalNode } from '@teachinglab/omd';
|
|
122
|
+
import * as math from 'mathjs';
|
|
123
|
+
|
|
124
|
+
// Create a fraction from a string
|
|
125
|
+
const fraction = omdRationalNode.fromString('(a+b)/(a-b)');
|
|
126
|
+
|
|
127
|
+
// Set font size and render
|
|
128
|
+
fraction.setFontSize(28);
|
|
129
|
+
fraction.initialize();
|
|
130
|
+
|
|
131
|
+
// Evaluate with given variables
|
|
132
|
+
const result = fraction.evaluate({ a: 3, b: 1 });
|
|
133
|
+
console.log(result); // Output: 2 (since (3+1)/(3-1) = 4/2 = 2)
|
|
134
|
+
|
|
135
|
+
// Add to an SVG container to display
|
|
136
|
+
// const svgContainer = new jsvgContainer();
|
|
137
|
+
// svgContainer.addChild(fraction);
|
|
138
|
+
// document.body.appendChild(svgContainer.svgObject);
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## See Also
|
|
142
|
+
|
|
143
|
+
- [`omdNode`](./omdNode.md) - The base class.
|
|
144
|
+
- [`omdConstantNode`](./omdConstantNode.md) - For constant numerators/denominators.
|
|
145
|
+
- [`omdBinaryExpressionNode`](./omdBinaryExpressionNode.md) - For complex numerators/denominators.
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# omdEquationSequenceNode
|
|
2
|
+
|
|
3
|
+
Represents a sequence of mathematical steps, typically a series of equations that show the process of solving a problem. This node is a container that manages the layout, alignment, and simplification history of its steps.
|
|
4
|
+
|
|
5
|
+
## Class: `omdEquationSequenceNode extends omdNode`
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
import { omdEquationSequenceNode } from './omd/nodes/omdEquationSequenceNode.js';
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
### Constructor
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
new omdEquationSequenceNode(steps)
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
**Parameters:**
|
|
18
|
+
- `steps` {Array<[`omdNode`](./omdNode.md)>} - An array of nodes (usually `omdEquationNode` or `omdOperationDisplayNode`) that make up the sequence.
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
### Static Methods
|
|
22
|
+
|
|
23
|
+
#### `fromSteps(stepsArray)`
|
|
24
|
+
Creates an `omdEquationSequenceNode` from an array of expression strings.
|
|
25
|
+
|
|
26
|
+
```javascript
|
|
27
|
+
const sequence = omdEquationSequenceNode.fromSteps([
|
|
28
|
+
'2x + 4 = 10',
|
|
29
|
+
'2x = 6',
|
|
30
|
+
'x = 3'
|
|
31
|
+
]);
|
|
32
|
+
```
|
|
33
|
+
- **Parameters:**
|
|
34
|
+
- `stepsArray` {Array<string>} - An array of strings, where each string is a step (either an equation or an expression).
|
|
35
|
+
- **Returns:** `omdEquationSequenceNode` A new instance.
|
|
36
|
+
- **Throws:** Error if a string is not a valid equation or expression, or if math.js is not available.
|
|
37
|
+
|
|
38
|
+
### Core Methods
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
#### `addStep(step, options)`
|
|
42
|
+
Adds a new step to the end of the sequence.
|
|
43
|
+
|
|
44
|
+
- **Parameters:**
|
|
45
|
+
- `step` {omdNode | string} - The node or expression string to add. If a string, it is parsed as an equation or expression.
|
|
46
|
+
- `options` {Object|string} (optional) - Options for the step, such as `description` and `stepMark` (importance level), or a description string for backward compatibility.
|
|
47
|
+
- `importance` {number} (optional) - Importance level (0, 1, or 2) if using legacy signature.
|
|
48
|
+
- **Returns:** `number` - The index of the newly added step.
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
#### `simplify()`
|
|
54
|
+
Applies one round of simplification to the **last step** in the sequence and adds the result as a new step.
|
|
55
|
+
|
|
56
|
+
- **Returns:** `Object` - An object detailing the result: `{ success, foldedCount, isFinalSimplification, message }`.
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
#### `simplifyAll(maxIterations)`
|
|
62
|
+
Repeatedly calls `simplify()` on the last step until no more simplifications can be applied or the iteration limit is reached.
|
|
63
|
+
|
|
64
|
+
- **Parameters:**
|
|
65
|
+
- `maxIterations` {number} (optional) - A safeguard against infinite loops. Defaults to `50`.
|
|
66
|
+
- **Returns:** `Object` - An object summarizing the process: `{ success, totalSteps, iterations, message }`.
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
#### `applyEquationOperation(value, operation)`
|
|
72
|
+
Applies an operation (e.g., 'add', 'subtract', 'multiply', 'divide') to both sides of the last equation in the sequence. This typically adds two new steps: a visual display of the operation and the resulting new equation.
|
|
73
|
+
|
|
74
|
+
- **Parameters:**
|
|
75
|
+
- `value` {number} - The value to apply.
|
|
76
|
+
- `operation` {string} - The name of the operation ('add', 'subtract', 'multiply', 'divide').
|
|
77
|
+
- **Returns:** `omdEquationSequenceNode` The sequence instance, for chaining.
|
|
78
|
+
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
#### `applyEquationFunction(functionName)`
|
|
83
|
+
Applies a function (e.g., 'sqrt') to both sides of the last equation and adds the result as a new step.
|
|
84
|
+
|
|
85
|
+
- **Parameters:**
|
|
86
|
+
- `functionName` {string} - The name of the function.
|
|
87
|
+
- **Returns:** `omdEquationSequenceNode` The sequence instance, for chaining.
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
### Other Key Methods
|
|
91
|
+
|
|
92
|
+
- `getCurrentEquation()`: Returns the last `omdEquationNode` in the sequence, or `null` if none.
|
|
93
|
+
- `getCurrentStep()`: Returns the current step node (may not be an equation).
|
|
94
|
+
- `getSimplificationHistory()`: Returns an array of all simplification events that have occurred.
|
|
95
|
+
- `rebuildNodeMap()`: Re-indexes all nodes within the sequence, which is crucial for provenance tracking and highlighting.
|
|
96
|
+
- `updateStepsVisibility(predicate)`: Shows or hides steps based on a provided function, useful for filtering and UI.
|
|
97
|
+
- `clear()`: Removes all steps and history from the sequence.
|
|
98
|
+
- `navigateToStep(index)`: Navigates to a specific step by index.
|
|
99
|
+
- `nextStep()`, `previousStep()`: Navigate forward/backward through steps.
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
### Layout and Provenance
|
|
103
|
+
|
|
104
|
+
The `omdEquationSequenceNode` automatically aligns all `omdEquationNode` steps by their equals signs, creating a clean, readable layout. It also tracks provenance for all nodes, supporting step-by-step highlighting and history.
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
### Example
|
|
108
|
+
|
|
109
|
+
```javascript
|
|
110
|
+
// Create a sequence with an initial equation
|
|
111
|
+
const sequence = omdEquationSequenceNode.fromSteps([
|
|
112
|
+
'2 * (x + 1) = 10'
|
|
113
|
+
]);
|
|
114
|
+
|
|
115
|
+
// Apply operations and simplifications
|
|
116
|
+
sequence.applyEquationOperation(2, 'divide'); // Adds a step: (2*(x+1))/2 = 10/2
|
|
117
|
+
sequence.simplifyAll(); // Simplifies to x+1=5, then x=4
|
|
118
|
+
|
|
119
|
+
// Add the sequence to a display
|
|
120
|
+
const display = new omdDisplay(document.getElementById('container'));
|
|
121
|
+
display.render(sequence);
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### See Also
|
|
125
|
+
|
|
126
|
+
- [`omdNode`](./omdNode.md) - The base class.
|
|
127
|
+
- [`omdEquationNode`](./omdEquationNode.md) - The primary type of node held within a sequence.
|
|
128
|
+
- [`omdStepVisualizer`](./omdStepVisualizer.md) - A subclass that adds interactive dots and explanations to the sequence.
|
|
@@ -0,0 +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).
|
|
79
|
+
- [`Configuration Options`](./configuration-options.md) - For configuring simplification behavior.
|
|
@@ -0,0 +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.
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# omdStepVisualizer
|
|
2
|
+
|
|
3
|
+
Visualizes a sequence of mathematical steps (typically equations) and provides interactive explanations for how one step transforms into the next. It extends `omdEquationSequenceNode` by adding a visual track of dots and lines to the right of the steps, which can be clicked to reveal details about the simplification or operation applied.
|
|
4
|
+
|
|
5
|
+
## Class Hierarchy
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
omdNode
|
|
9
|
+
└─ omdEquationSequenceNode
|
|
10
|
+
└─ omdStepVisualizer
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
See: [`omdEquationSequenceNode`](./omdEquationSequenceNode.md) for base sequence functionality.
|
|
14
|
+
|
|
15
|
+
## Constructor
|
|
16
|
+
|
|
17
|
+
### `new omdStepVisualizer(steps)`
|
|
18
|
+
|
|
19
|
+
Creates a new `omdStepVisualizer` instance.
|
|
20
|
+
|
|
21
|
+
- **`steps`** (`Array<omdNode>`): An array of nodes (usually `omdEquationNode`) representing the initial steps in the sequence.
|
|
22
|
+
|
|
23
|
+
During construction, it initializes internal arrays for `stepDots` and `stepLines`, creates a `visualContainer` for these elements, and sets up managers for `highlighting`, `textBoxes`, and `layout`. It also populates `nodeToStepMap` for efficient lookup.
|
|
24
|
+
|
|
25
|
+
## Public Properties
|
|
26
|
+
|
|
27
|
+
- **`stepDots`** (`Array<jsvgEllipse>`): An array of `jsvgEllipse` objects representing the clickable dots for each equation step.
|
|
28
|
+
- **`stepLines`** (`Array<jsvgLine>`): An array of `jsvgLine` objects connecting the step dots.
|
|
29
|
+
- **`visualContainer`** (`jsvgLayoutGroup`): The `jsvgLayoutGroup` that holds the `stepDots` and `stepLines`.
|
|
30
|
+
- **`dotRadius`** (`number`): The radius of the step dots, determined by `omdConfigManager`.
|
|
31
|
+
- **`lineWidth`** (`number`): The stroke width of the connecting lines.
|
|
32
|
+
- **`visualSpacing`** (`number`): The horizontal spacing between the equation sequence and the visual tracker.
|
|
33
|
+
- **`activeDotIndex`** (`number`): The 0-based index of the currently active (clicked) dot. `-1` if none.
|
|
34
|
+
- **`dotsClickable`** (`boolean`): Controls whether the step dots can be clicked to show explanations. Default: `true`.
|
|
35
|
+
- **`nodeToStepMap`** (`Map<string, number>`): A map from `omdNode` IDs to their corresponding step index in the sequence.
|
|
36
|
+
- **`stepVisualizerHighlights`** (`Set<string>`): A set of node IDs that are currently highlighted by the visualizer.
|
|
37
|
+
- **`highlighting`** ([`omdStepVisualizerHighlighting`](./omdStepVisualizerHighlighting.md)): The manager responsible for all highlighting logic.
|
|
38
|
+
- **`textBoxManager`** ([`omdStepVisualizerTextBoxes`](./omdStepVisualizerTextBoxes.md)): The manager responsible for creating, positioning, and removing the explanation text boxes.
|
|
39
|
+
- **`layoutManager`** ([`omdStepVisualizerLayout`](./omdStepVisualizerLayout.md)): The manager responsible for layout and z-ordering of all visual elements.
|
|
40
|
+
|
|
41
|
+
## Public Methods
|
|
42
|
+
|
|
43
|
+
### `rebuildVisualizer()`
|
|
44
|
+
|
|
45
|
+
Forces a complete rebuild of the visual elements (dots and lines) from scratch. This is useful after significant changes to the underlying `steps` array.
|
|
46
|
+
|
|
47
|
+
### `addStep(step, options)`
|
|
48
|
+
|
|
49
|
+
Adds a new step to the sequence. This method overrides the base `omdEquationSequenceNode.addStep` to also create and manage the corresponding visual dot and connecting line for the new step.
|
|
50
|
+
|
|
51
|
+
- **`step`** (`omdNode` | `string`): The node object or expression string for the step.
|
|
52
|
+
- **`options`** (`object`): Options for the step, such as `description` and `stepMark` (importance level).
|
|
53
|
+
|
|
54
|
+
### `getNodeStepNumber(nodeId)`
|
|
55
|
+
|
|
56
|
+
Retrieves the step number (index) associated with a given `omdNode` ID within the sequence.
|
|
57
|
+
|
|
58
|
+
- **`nodeId`** (`string`): The ID of the node to look up.
|
|
59
|
+
- **Returns**: `number` | `undefined` - The 0-based step index, or `undefined` if the node is not found within a step.
|
|
60
|
+
|
|
61
|
+
### `computeDimensions()`
|
|
62
|
+
|
|
63
|
+
Calculates the overall dimensions of the visualizer, accounting for the width and height of the equation sequence and the additional space required for the visual tracker (dots and lines).
|
|
64
|
+
|
|
65
|
+
- **Overrides**: `omdEquationSequenceNode.computeDimensions()`.
|
|
66
|
+
|
|
67
|
+
### `updateLayout()`
|
|
68
|
+
|
|
69
|
+
Updates the layout of the visualizer, positioning both the equation sequence and the visual tracker elements. It delegates to the `layoutManager` for precise positioning of dots and lines.
|
|
70
|
+
|
|
71
|
+
- **Overrides**: `omdEquationSequenceNode.updateLayout()`.
|
|
72
|
+
|
|
73
|
+
### `undoLastOperation()`
|
|
74
|
+
|
|
75
|
+
Removes the most recent operation and its resulting equation from the sequence. This method overrides the base `omdEquationSequenceNode.undoLastOperation` to also trigger a `rebuildVisualizer()` to ensure visual elements are synchronized.
|
|
76
|
+
|
|
77
|
+
- **Returns**: `boolean` - `true` if an operation was undone, `false` otherwise.
|
|
78
|
+
|
|
79
|
+
### `setDotColor(dotIndex, color)`
|
|
80
|
+
|
|
81
|
+
Sets the fill and stroke color of a specific step dot.
|
|
82
|
+
|
|
83
|
+
- **`dotIndex`** (`number`): The index of the dot to color.
|
|
84
|
+
- **`color`** (`string`): The new color.
|
|
85
|
+
|
|
86
|
+
### `setLineAboveColor(dotIndex, color)`
|
|
87
|
+
|
|
88
|
+
Sets the stroke color of the line segment directly above a specific step dot.
|
|
89
|
+
|
|
90
|
+
- **`dotIndex`** (`number`): The index of the dot whose preceding line should be colored.
|
|
91
|
+
- **`color`** (`string`): The new color.
|
|
92
|
+
|
|
93
|
+
### `setDotsClickable(enabled)`
|
|
94
|
+
|
|
95
|
+
Enables or disables the ability for users to click on the step dots to reveal explanations.
|
|
96
|
+
|
|
97
|
+
- **`enabled`** (`boolean`): `true` to enable clicking, `false` to disable.
|
|
98
|
+
|
|
99
|
+
### `getStepTextBoxes()`
|
|
100
|
+
|
|
101
|
+
Retrieves the array of currently visible explanation text box components managed by the `textBoxManager`.
|
|
102
|
+
|
|
103
|
+
- **Returns**: `Array<omdStepVisualizerTextBox>` - The active text box instances.
|
|
104
|
+
|
|
105
|
+
## Internal Methods
|
|
106
|
+
|
|
107
|
+
- **`_initializeVisualElements()`**: Creates and initializes the visual dots and lines for all existing equation steps, and populates the `nodeToStepMap`.
|
|
108
|
+
- **`_createStepDot(equation, index)`**: Creates a single `jsvgEllipse` representing a step dot, associates it with an equation, and adds it to the `visualContainer`.
|
|
109
|
+
- **`_createStepLine(fromIndex, toIndex)`**: Creates a `jsvgLine` connecting two step dots and adds it to the `visualContainer`.
|
|
110
|
+
- **`_clearVisualElements()`**: Removes all existing dots, lines, and text boxes from the display and resets internal arrays.
|
|
111
|
+
- **`_handleDotClick(dot, dotIndex)`**: Event handler for when a step dot is clicked. It manages the active dot state, triggers highlighting, and creates/removes explanation text boxes.
|
|
112
|
+
- **`setActiveDot(dotIndex)`**: Sets a specific dot as the visually active one, changing its color and triggering the display of its explanation text box.
|
|
113
|
+
- **`_clearActiveDot()`**: Clears the currently active dot, resetting its color, removing its text box, and clearing highlights.
|
|
114
|
+
- **`_getSimplificationDataForDot(dotIndex)`**: Gathers and formats the simplification and operation data relevant to a specific step dot, used to populate the explanation text box.
|
|
115
|
+
- **`_createDefaultSimplificationData(message)`**: Helper to create a default data object for step explanations.
|
|
116
|
+
- **`_findPreviousVisibleEquationIndex(currentIndex)`**: Finds the index of the last visible equation step before the given `currentIndex`.
|
|
117
|
+
- **`_getRelevantSimplifications(simplificationHistory, startIndex, endIndex)`**: Filters the full simplification history to find entries relevant to a specific range of steps.
|
|
118
|
+
- **`_createMultipleSimplificationsData(simplifications)`**: Formats data for displaying multiple simplification events in a single text box.
|
|
119
|
+
- **`_checkForOperationStep(equationIndex)`**: Checks if a step at a given index is an `omdOperationDisplayNode` and extracts its data.
|
|
120
|
+
- **`_checkForSingleSimplification(simplificationHistory, equationIndex)`**: Checks for a single simplification entry relevant to a specific step.
|
|
121
|
+
- **`_getFallbackSimplificationData(equationIndex)`**: Provides a default explanation if no specific simplification or operation data is found for a step.
|
|
122
|
+
|
|
123
|
+
## Example
|
|
124
|
+
|
|
125
|
+
```javascript
|
|
126
|
+
import { omdStepVisualizer } from '@teachinglab/omd';
|
|
127
|
+
import { omdEquationNode } from '@teachinglab/omd';
|
|
128
|
+
import { omdDisplay } from '@teachinglab/omd';
|
|
129
|
+
|
|
130
|
+
// 1. Define the steps of a solution (must be omdEquationNode for dots to appear)
|
|
131
|
+
const steps = [
|
|
132
|
+
omdEquationNode.fromString('2x + 4 = 10'),
|
|
133
|
+
omdEquationNode.fromString('2x = 6'),
|
|
134
|
+
omdEquationNode.fromString('x = 3')
|
|
135
|
+
];
|
|
136
|
+
|
|
137
|
+
// 2. Create the visualizer
|
|
138
|
+
const visualizer = new omdStepVisualizer(steps);
|
|
139
|
+
|
|
140
|
+
// 3. Add it to a display container
|
|
141
|
+
const display = new omdDisplay(document.getElementById('container'));
|
|
142
|
+
display.render(visualizer);
|
|
143
|
+
|
|
144
|
+
// Now the steps will be displayed with interactive dots on the right.
|
|
145
|
+
// Only omdEquationNode steps are visualized with dots/lines.
|
|
146
|
+
// Clicking the dot next to "2x = 6" will explain that 4 was subtracted from both sides.
|
|
147
|
+
```
|