From 4787237b315572dd356e89eb78e62376179faac8 Mon Sep 17 00:00:00 2001 From: "firepick1 (localhost)" Date: Thu, 24 Aug 2017 20:18:06 -0700 Subject: [PATCH 1/3] #933 +unaryMinus to subtract --- lib/function/algebra/simplify/simplifyCore.js | 3 +++ test/function/algebra/simplify.test.js | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/lib/function/algebra/simplify/simplifyCore.js b/lib/function/algebra/simplify/simplifyCore.js index b290fafc1..2927b5bcf 100644 --- a/lib/function/algebra/simplify/simplifyCore.js +++ b/lib/function/algebra/simplify/simplifyCore.js @@ -49,6 +49,9 @@ function factory(type, config, load, typed, math) { if (type.isConstantNode(a1) && a1.value === "0") { return a0; } + if (type.isOperatorNode(a1) && a1.op === '-' && a1.fn === 'unaryMinus') { + return new OperatorNode('-', 'subtract', [a0,a1.args[0]]); + } return new OperatorNode(node.op, node.fn, a1 ? [a0,a1] : [a0]); } else if (node.op === "-") { if (type.isConstantNode(a0) && a1) { diff --git a/test/function/algebra/simplify.test.js b/test/function/algebra/simplify.test.js index 9e7c668bf..b0f64b30c 100644 --- a/test/function/algebra/simplify.test.js +++ b/test/function/algebra/simplify.test.js @@ -53,6 +53,12 @@ describe('simplify', function() { assert.equal(fsimplified.eval()(5), 0.9933071490757153); }); + it('should simplifyCore convert +unaryMinus to subtract', function() { + var result = math.simplify('x + y + a', [math.simplify.simplifyCore], {a: -1}).toString() + assert.equal(result, "x + y - 1"); + }); + + it('should handle custom functions', function() { function doubleIt (x) { return x + x } var node = math.expression.node; From e9fa99983f18bd83eac07d864d0b43b855345143 Mon Sep 17 00:00:00 2001 From: "firepick1 (localhost)" Date: Thu, 24 Aug 2017 20:34:41 -0700 Subject: [PATCH 2/3] #933 nargs --- lib/function/algebra/simplify/simplifyCore.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/function/algebra/simplify/simplifyCore.js b/lib/function/algebra/simplify/simplifyCore.js index 2927b5bcf..ee7af5d35 100644 --- a/lib/function/algebra/simplify/simplifyCore.js +++ b/lib/function/algebra/simplify/simplifyCore.js @@ -49,7 +49,7 @@ function factory(type, config, load, typed, math) { if (type.isConstantNode(a1) && a1.value === "0") { return a0; } - if (type.isOperatorNode(a1) && a1.op === '-' && a1.fn === 'unaryMinus') { + if (node.args.length === 2 && type.isOperatorNode(a1) && a1.op === '-' && a1.fn === 'unaryMinus') { return new OperatorNode('-', 'subtract', [a0,a1.args[0]]); } return new OperatorNode(node.op, node.fn, a1 ? [a0,a1] : [a0]); From 5b175d56a51923c0efd2ac64e805bc70d3bfa0aa Mon Sep 17 00:00:00 2001 From: "firepick1 (localhost)" Date: Mon, 28 Aug 2017 06:35:40 -0700 Subject: [PATCH 3/3] #934 unaryMinus simplifyCore --- lib/function/algebra/simplify/simplifyCore.js | 8 +++++++- test/function/algebra/simplify.test.js | 3 ++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/function/algebra/simplify/simplifyCore.js b/lib/function/algebra/simplify/simplifyCore.js index ee7af5d35..987807da5 100644 --- a/lib/function/algebra/simplify/simplifyCore.js +++ b/lib/function/algebra/simplify/simplifyCore.js @@ -61,7 +61,7 @@ function factory(type, config, load, typed, math) { return new OperatorNode("-", "unaryMinus", [a1]); } } - if (node.fn === "subtract") { + if (node.fn === "subtract" && node.args.length === 2) { if (type.isConstantNode(a1) && a1.value === "0") { return a0; } @@ -70,8 +70,14 @@ function factory(type, config, load, typed, math) { } return new OperatorNode(node.op, node.fn, [a0,a1]); } else if (node.fn === "unaryMinus") { + if (type.isOperatorNode(a0)) { + if (a0.fn === 'unaryMinus') { + return a0.args[0]; + } + } return new OperatorNode(node.op, node.fn, [a0]); } + throw new Error('never happens'); } else if (node.op === "*") { if (type.isConstantNode(a0)) { if (a0.value === "0") { diff --git a/test/function/algebra/simplify.test.js b/test/function/algebra/simplify.test.js index b0f64b30c..838e02a1a 100644 --- a/test/function/algebra/simplify.test.js +++ b/test/function/algebra/simplify.test.js @@ -54,6 +54,7 @@ describe('simplify', function() { }); it('should simplifyCore convert +unaryMinus to subtract', function() { + simplifyAndCompareEval('--2', '2'); var result = math.simplify('x + y + a', [math.simplify.simplifyCore], {a: -1}).toString() assert.equal(result, "x + y - 1"); }); @@ -143,9 +144,9 @@ describe('simplify', function() { }); it('should collect separated like factors', function() { + simplifyAndCompare('x*y*-x/(x^2)', '-y'); simplifyAndCompare('x/2*x', 'x^2/2'); simplifyAndCompare('x*2*x', '2*x^2'); - simplifyAndCompare('x*y*-x/(x^2)', '-y'); }); it('should handle non-existing functions like a pro', function() {