UNPKG

mathjs

Version:

Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser and offers an integrated solution to work with numbers, big numbers, complex numbers, units, and matrices.

54 lines (46 loc) 1.53 kB
var math = require('../index'); // Filter an expression tree console.log('Filter all symbol nodes "x" in the expression "x^2 + x/4 + 3*y"'); var node = math.parse('x^2 + x/4 + 3*y'); var filtered = node.filter(function (node) { return node.type == 'SymbolNode' && node.name == 'x'; }); // returns an array with two entries: two SymbolNodes 'x' filtered.forEach(function (node) { console.log(node.type, node.toString()) }); // outputs: // SymbolNode x // SymbolNode x // Traverse an expression tree console.log(); console.log('Traverse the expression tree of expression "3 * x + 2"'); var node1 = math.parse('3 * x + 2'); node1.traverse(function (node, path, parent) { switch (node.type) { case 'OperatorNode': console.log(node.type, node.op); break; case 'ConstantNode': console.log(node.type, node.value); break; case 'SymbolNode': console.log(node.type, node.name); break; default: console.log(node.type); } }); // outputs: // OperatorNode + // OperatorNode * // ConstantNode 3 // SymbolNode x // ConstantNode 2 // transform an expression tree console.log(); console.log('Replace all symbol nodes "x" in expression "x^2 + 5*x" with a constant 3'); var node2 = math.parse('x^2 + 5*x'); var transformed = node2.transform(function (node, path, parent) { if (node.type == 'SymbolNode' && node.name == 'x') { return new math.expression.node.ConstantNode(3); } else { return node; } }); console.log(transformed.toString()); // outputs: '(3 ^ 2) + (5 * 3)'