OMD Documentation

omdSqrtNode

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.

Class Definition

export class omdSqrtNode extends omdNode

Constructor

new omdSqrtNode(astNodeData)

Creates a new omdSqrtNode instance.

Static Methods

fromString(expressionString)

Creates an omdSqrtNode from a string representation of a square root function. Requires window.math (math.js) to be available globally for parsing.

Public Properties

Public Methods

computeDimensions()

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).

updateLayout()

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.

clone()

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.

highlightAll()

Applies a highlight to the square root node itself and recursively highlights its argument node.

unhighlightAll()

Removes the highlight from the square root node and recursively unhighlights its argument node.

toMathJSNode()

Converts the omdSqrtNode back into its math.js AST representation. It creates a FunctionNode with fn: 'sqrt' and the converted argument AST.

toString()

Converts the square root node to its string representation (e.g., "sqrt(x+1)").

evaluate(variables)

Evaluates the square root expression. It evaluates the argument and then calculates its square root.

isSquareRoot()

Checks if the node represents a square root. Always returns true for omdSqrtNode.

isCubeRoot()

Checks if the node represents a cube root. Always returns false for omdSqrtNode.

toPowerForm()

Converts the square root node into an equivalent omdPowerNode representation (e.g., sqrt(x) becomes x^(0.5)).

Internal Methods

Example

import { omdSqrtNode } from '@teachinglab/omd';
import * as math from 'mathjs';

// Create from AST
const node = new omdSqrtNode({
  type: 'FunctionNode',
  fn: { name: 'sqrt' },
  args: [ { type: 'SymbolNode', name: 'x' } ]
});

// Or from string
const node2 = omdSqrtNode.fromString('sqrt(x+1)');

// Render and layout
node.setFontSize(24);
node.initialize();

// Evaluate
const val = node.evaluate({ x: 9 }); // 3

// Convert to power form
const powerNode = node.toPowerForm(); // x^(1/2)

// Add to an SVG container to display
// const svgContainer = new jsvgContainer();
// svgContainer.addChild(node);
// document.body.appendChild(svgContainer.svgObject);

See Also

↑ Top