#916 simplifyCore loses custom functions

This commit is contained in:
firepick1 (localhost) 2017-08-08 20:48:24 -07:00
parent 6ffef7f0a3
commit dccec26cb1
2 changed files with 22 additions and 7 deletions

View File

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

View File

@ -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');