Expression trees #

When parsing an expression via math.parse(expr), math.js generates an expression tree and returns the root node of the tree. An expression tree can be used to analyze, manipulate, and evaluate expressions.

Example:

var node = math.parse('sqrt(2 + x)');

In this case, the expression sqrt(2 + x) is parsed as:

  FunctionNode    sqrt
                   |
  OperatorNode     +
                  / \
  ConstantNode   2   x   SymbolNode

Alternatively, this expression tree can be build by manually creating nodes:

var node1 = new math.expression.node.ConstantNode(2);
var node2 = new math.expression.node.SymbolNode('x');
var node3 = new math.expression.node.OperatorNode('+', 'add', [node1, node2]);
var node4 = new math.expression.node.FunctionNode('sqrt', [node3]);

The resulting expression tree with root node node4 is equal to the expression tree generated by math.parse('sqrt(2 + x)').

API #

Methods #

All nodes have the following methods:

Properties #

Each Node has the following properties:

Nodes #

math.js has the following types of nodes. All nodes are available at the namespace math.expression.node.

AccessorNode #

Construction:

new AccessorNode(object: Node, index: IndexNode)

Properties:

Examples:

var node1 = math.parse('a[3]');

var object = new math.expression.node.SymbolNode('a');
var index = new math.expression.node.IndexNode([3]);
var node2 = new math.expression.node.AccessorNode(object, index);

ArrayNode #

Construction:

new ArrayNode(items: Node[])

Properties:

Examples:

var node1 = math.parse('[1, 2, 3]');

var one    = new math.expression.node.ConstantNode(1);
var two    = new math.expression.node.ConstantNode(2);
var three  = new math.expression.node.ConstantNode(3);
var node2  = new math.expression.node.ArrayNode([one, two, three]);

AssignmentNode #

Construction:

new AssignmentNode(object: SymbolNode, value: Node)
new AssignmentNode(object: SymbolNode | AccessorNode, index: IndexNode, value: Node)

Properties:

Examples:

var node1 = math.parse('a = 3');

var object = new math.expression.node.SymbolNode('a');
var value = new math.expression.node.ConstantNode(3);
var node2 = new math.expression.node.AssignmentNode(object, value);

BlockNode #

A BlockNode is created when parsing a multi line expression like a=2;b=3 or a=2\nb=3. Evaluating a BlockNode returns a ResultSet. The results can be retrieved via ResultSet.entries or ResultSet.valueOf(), which contains an Array with the results of the visible lines (i.e. lines not ending with a semicolon).

Construction:

block = new BlockNode(Array.<{node: Node} | {node: Node, visible: boolean}>)

Properties:

Examples:

var block1 = math.parse('a=1; b=2; c=3');

var a = new math.expression.node.SymbolNode('a');
var one = new math.expression.node.ConstantNode(1);
var ass1 = new math.expression.node.AssignmentNode(a, one);

var b = new math.expression.node.SymbolNode('b');
var two = new math.expression.node.ConstantNode(2);
var ass2 = new math.expression.node.AssignmentNode(b, two);

var c = new math.expression.node.SymbolNode('c');
var three = new math.expression.node.ConstantNode(3);
var ass3 = new math.expression.node.AssignmentNode(c, three);

var block2 = new BlockNode([
  {node: ass1, visible: false},
  {node: ass2, visible: false},
  {node: ass3, visible: true}
]);

ConditionalNode #

Construction:

new ConditionalNode(condition: Node, trueExpr: Node, falseExpr: Node)

Properties:

Examples:

var node1 = math.parse('a > 0 ? a : -a');

var a         = new math.expression.node.SymbolNode('a');
var zero      = new math.expression.node.ConstantNode(0);
var condition = new math.expression.node.OperatorNode('>', 'larger', [a, zero]);
var trueExpr  = a;
var falseExpr = new math.expression.node.OperatorNode('-', 'unaryMinus', [a]);
var node2     = new math.expression.node.ConditionalNode(condition, trueExpr, falseExpr);

ConstantNode #

Construction:

new ConstantNode(value: * [, valueType: string])

Properties:

Examples:

var node1 = math.parse('2.4');

var node2 = new math.expression.node.ConstantNode(2.4);
var node3 = new math.expression.node.ConstantNode('2.4', 'number');

FunctionAssignmentNode #

Construction:

new FunctionAssignmentNode(name: string, params: string[], expr: Node)

Properties:

Examples:

var node1 = math.parse('f(x) = x^2');

var x      = new math.expression.node.SymbolNode('x');
var two    = new math.expression.node.ConstantNode(2);
var expr   = new math.expression.node.OperatorNode('^', 'pow', [x, 2]);
var node2  = new math.expression.node.FunctionAssignmentNode('f', ['x'], expr);

FunctionNode #

Construction:

new FunctionNode(fn: Node | string, args: Node[])

Properties:

Examples:

var node1 = math.parse('sqrt(4)');

var four  = new math.expression.node.ConstantNode(4);
var node3 = new math.expression.node.FunctionNode(new SymbolNode('sqrt'), [four]);

IndexNode #

Construction:

new IndexNode(dimensions: Node[])
new IndexNode(dimensions: Node[], dotNotation: boolean)

Each dimension can be a single value, a range, or a property. The values of indices are one-based, including range end.

An optional property dotNotation can be provided describing whether this index was written using dot notation like a.b, or using bracket notation like a["b"]. Default value is false. This information is used when stringifying the IndexNode.

Properties:

Examples:

var node1 = math.parse('A[1:3, 2]');

var A     = new math.expression.node.SymbolNode('A');
var one   = new math.expression.node.ConstantNode(1);
var two   = new math.expression.node.ConstantNode(2);
var three = new math.expression.node.ConstantNode(3);

var range = new math.expression.node.RangeNode(one, three);
var index = new math.expression.node.IndexNode([range, two]);
var node2 = new math.expression.node.AccessNode(A, index);

ObjectNode #

Construction:

new ObjectNode(properties: Object.<string, Node>)

Properties:

Examples:

var node1 = math.parse('{a: 1, b: 2, c: 3}');

var a = new math.expression.node.ConstantNode(1);
var b = new math.expression.node.ConstantNode(2);
var c = new math.expression.node.ConstantNode(3);
var node2  = new math.expression.node.ObjectNode({a: a, b: b, c: c});

OperatorNode #

Construction:

new OperatorNode(op: string, fn: string, args: Node[])

Properties:

Examples:

var node1 = math.parse('2.3 + 5');

var a     = new math.expression.node.ConstantNode(2.3);
var b     = new math.expression.node.ConstantNode(5);
var node2 = new math.expression.node.OperatorNode('+', 'add', [a, b]);

ParenthesisNode #

Construction:

new ParenthesisNode(content: Node)

Properties:

Examples:

var node1 = math.parse('(1)');

var a     = new math.expression.node.ConstantNode(1);
var node2 = new math.expression.node.ParenthesisNode(a);

RangeNode #

Construction:

new RangeNode(start: Node, end: Node [, step: Node])

Properties:

Examples:

var node1 = math.parse('1:10');
var node2 = math.parse('0:2:10');

var zero = new math.expression.node.ConstantNode(0);
var one = new math.expression.node.ConstantNode(1);
var two = new math.expression.node.ConstantNode(2);
var ten = new math.expression.node.ConstantNode(10);

var node3 = new math.expression.node.RangeNode(one, ten);
var node4 = new math.expression.node.RangeNode(zero, ten, two);

SymbolNode #

Construction:

new SymbolNode(name: string)

Properties:

Examples:

var node = math.parse('x');

var x = new math.expression.node.SymbolNode('x');
Fork me on GitHub