diff --git a/lib/function/algebra/simplify/simplifyCore.js b/lib/function/algebra/simplify/simplifyCore.js index 1ad03148f..9ccf7b2ff 100644 --- a/lib/function/algebra/simplify/simplifyCore.js +++ b/lib/function/algebra/simplify/simplifyCore.js @@ -123,15 +123,19 @@ function factory(type, config, load, typed, math) { } return new ParenthesisNode(c); } else if (node.isFunctionNode) { - var args = node.args.map(function (arg) { - return simplifyCore(arg) - }); - if (args.length === 1) { - if (args[0].isParenthesisNode) { - args[0] = args[0].content; + if (node.fn == null) { + var args = node.args.map(function (arg) { + return simplifyCore(arg) + }); + if (args.length === 1) { + if (args[0].isParenthesisNode) { + args[0] = args[0].content; + } } + return new FunctionNode(node.name, args); } - return new FunctionNode(node.name, args); + } else { + // cannot simplify } return node; } diff --git a/test/function/algebra/simplify.test.js b/test/function/algebra/simplify.test.js index ceb405f24..3fe442f93 100644 --- a/test/function/algebra/simplify.test.js +++ b/test/function/algebra/simplify.test.js @@ -43,6 +43,17 @@ describe('simplify', function() { simplifyAndCompare('(-1)*x', '-x'); }); + it('should handle FunctionAssignmentNode', function() { + const node = math.expression.node; + const s = new node.FunctionAssignmentNode('sigma', ['x'], math.parse('1 / (1 + exp(-x))')); + const f = new node.FunctionNode(s, [new node.SymbolNode('x')]); + assert.equal(f.toString(), 'sigma(x) = 1 / (1 + exp(-x))(x)'); + assert.equal(f.eval({x: 5}), 0.9933071490757153); + const fsimplified = math.simplify.simplifyCore(f); + assert.equal(fsimplified.toString(), 'sigma(x) = 1 / (1 + exp(-x))(x)'); + assert.equal(fsimplified.eval({x: 5}), 0.9933071490757153); + }); + it('should simplify (n- -n1)', function() { simplifyAndCompare('2 + -3', '-1'); simplifyAndCompare('2 - 3', '-1');