OMD Documentation

omdRationalNode

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.

Class Definition

export class omdRationalNode extends omdNode

Constructor

new omdRationalNode(astNodeData)

Creates a new omdRationalNode instance.

Static Methods

fromString(expressionString)

Creates an omdRationalNode from a string containing a division. Requires window.math (math.js) to be available globally for parsing.

Public Properties

Public Methods

computeDimensions()

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

updateLayout()

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.

getAlignmentBaseline()

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.

isConstant()

Checks if both the numerator and the denominator are constant numerical values.

getRationalValue()

Retrieves the rational value of the node as a numerator/denominator pair. This method only works if the fraction is constant.

getValue()

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.

clone()

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.

toMathJSNode()

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.

toString()

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.

evaluate(variables)

Evaluates the rational expression by evaluating the numerator and denominator and then performing the division.

reduce()

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.

isProper()

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.

Internal Methods

Example

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

// Create a fraction from a string
const fraction = omdRationalNode.fromString('(a+b)/(a-b)');

// Set font size and render
fraction.setFontSize(28);
fraction.initialize();

// Evaluate with given variables
const result = fraction.evaluate({ a: 3, b: 1 });
console.log(result); // Output: 2 (since (3+1)/(3-1) = 4/2 = 2)

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

See Also

↑ Top