OMD Documentation

omdUnaryExpressionNode

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.

Class Definition

export class omdUnaryExpressionNode extends omdNode

Constructor

new omdUnaryExpressionNode(ast)

Creates a new omdUnaryExpressionNode instance.

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.

Static Methods

fromString(expressionString)

Creates an omdUnaryExpressionNode from a string representation of a unary expression. Requires window.math (math.js) to be available globally for parsing.

Public Properties

Public Methods

computeDimensions()

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.

updateLayout()

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.

clone()

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.

toMathJSNode()

Converts the omdUnaryExpressionNode back into its math.js AST representation. It creates an OperatorNode with the unary operator and the converted operand AST.

toString()

Converts the unary expression node to its string representation. It adds parentheses around the operand if needsParentheses() returns true (e.g., for binary expressions).

needsParentheses()

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.

evaluate(variables)

Evaluates the unary expression. It evaluates the operand and then applies the unary operation (e.g., negation for '-').

Internal Methods

Example

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

// Create from AST: -x
const node = new omdUnaryExpressionNode({
  type: 'OperatorNode',
  op: '-',
  fn: 'unaryMinus',
  args: [
    { type: 'SymbolNode', name: 'x' }
  ]
});

// Create from AST: -(x + 1)
const complex = new omdUnaryExpressionNode({
  type: 'OperatorNode',
  op: '-',
  fn: 'unaryMinus',
  args: [{
    type: 'OperatorNode',
    op: '+',
    fn: 'add',
    args: [
      { type: 'SymbolNode', name: 'x' },
      { type: 'ConstantNode', value: 1 }
    ]
  }]
});

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

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

See Also

↑ Top