resolve scope Node values

This commit is contained in:
firepick1 (localhost) 2017-08-02 13:39:55 -07:00
parent 709f1bc075
commit 53e64ee18d
2 changed files with 31 additions and 17 deletions

View File

@ -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}`);

View File

@ -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 () {