From 53e64ee18dfaebfce3178ce93e647e6abdca7db0 Mon Sep 17 00:00:00 2001 From: "firepick1 (localhost)" Date: Wed, 2 Aug 2017 13:39:55 -0700 Subject: [PATCH] resolve scope Node values --- lib/function/algebra/simplify/resolve.js | 10 +++---- test/function/algebra/simplify.test.js | 38 ++++++++++++++++-------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/lib/function/algebra/simplify/resolve.js b/lib/function/algebra/simplify/resolve.js index 83b98b425..c4b129360 100644 --- a/lib/function/algebra/simplify/resolve.js +++ b/lib/function/algebra/simplify/resolve.js @@ -1,6 +1,7 @@ 'use strict'; function factory(type, config, load, typed, math) { + var Node = math.expression.node.Node; var ConstantNode = math.expression.node.ConstantNode; var OperatorNode = math.expression.node.OperatorNode; var FunctionNode = math.expression.node.FunctionNode; @@ -22,17 +23,16 @@ function factory(type, config, load, typed, math) { * math.simplify.resolve(math.parse('x+y'), {x:1, y:2}) // Node {1 + 2} * math.simplify('x+y', {x:2, y:'x+x'}).toString(); // "6" * - * @param {Node} expr - * The expression to be simplified + * @param {Node} node + * The expression tree to be simplified */ - function resolve(expr, scope) { - var node = typeof expr === 'string' ? math.parse(expr) : expr; + function resolve(node, scope) { if (scope == null) { return node; } if (node.isSymbolNode) { var value = scope[node.name]; - if (typeof value === 'string') { + if (value instanceof Node) { return resolve(value, scope); } else if (typeof value === 'number') { return math.parse(`${value}`); diff --git a/test/function/algebra/simplify.test.js b/test/function/algebra/simplify.test.js index bed90b341..d2f78c03b 100644 --- a/test/function/algebra/simplify.test.js +++ b/test/function/algebra/simplify.test.js @@ -5,10 +5,19 @@ var math = require('../../../index'); describe('simplify', function() { function simplifyAndCompare(left, right, scope) { - if (scope) { - assert.equal(math.simplify(left, scope).toString(), math.parse(right).toString()); - } else { - assert.equal(math.simplify(left).toString(), math.parse(right).toString()); + try { + if (scope) { + assert.equal(math.simplify(left, scope).toString(), math.parse(right).toString()); + } else { + assert.equal(math.simplify(left).toString(), math.parse(right).toString()); + } + } catch (err) { + if (err instanceof Error) { + console.log(err.stack); + } else { + console.log(new Error(err)); + } + throw err; } } @@ -135,20 +144,25 @@ describe('simplify', function() { }); it('resolve() should substitute scoped constants', function() { - assert.equal(math.simplify.resolve('x+y', {x:1}).toString(), "1 + y"); // direct + assert.equal( + math.simplify.resolve(math.parse('x+y'), {x:1}).toString(), + "1 + y" + ); // direct simplifyAndCompare('x+y', 'x+y', {}); // operator simplifyAndCompare('x+y', 'y+1', {x:1}); - simplifyAndCompare('x+y', 'y+1', {x:'1'}); + simplifyAndCompare('x+y', 'y+1', {x:math.parse('1')}); simplifyAndCompare('x+y', '3', {x:1,y:2}); simplifyAndCompare('x+x+x', '3*x'); - simplifyAndCompare('y', 'x+1', {y:"1+x"}); - simplifyAndCompare('y', '3', {x:2, y:"1+x"}); - simplifyAndCompare('x+y', '3*x', {y:"x+x"}); - simplifyAndCompare('x+y', '6', {x:2,y:"x+x"}); - simplifyAndCompare('x+(y+2-1-1)', '6', {x:2,y:"x+x"}); // parentheses - simplifyAndCompare('log(x+y)', `${Math.log(6)}`, {x:2,y:"x+x"}); // function + simplifyAndCompare('y', 'x+1', {y:math.parse("1+x")}); + simplifyAndCompare('y', '3', {x:2, y:math.parse("1+x")}); + simplifyAndCompare('x+y', '3*x', {y:math.parse("x+x")}); + simplifyAndCompare('x+y', '6', {x:2,y:math.parse("x+x")}); + simplifyAndCompare('x+(y+2-1-1)', '6', {x:2,y:math.parse("x+x")}); // parentheses + simplifyAndCompare('log(x+y)', `${Math.log(6)}`, {x:2,y:math.parse("x+x")}); // function simplifyAndCompare('combinations( ceil(abs(sin(x)) * y), abs(x) )', 'combinations(ceil(0.9092974268256817 * y ), 2)', {x:-2}); + + // TODO(deal with accessor nodes) simplifyAndCompare('size(text)[1]', '11', {text: "hello world"}) }); describe('expression parser' ,function () {