mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-25 15:07:57 +00:00
#916 simplifyCore loses custom functions
This commit is contained in:
parent
6ffef7f0a3
commit
dccec26cb1
@ -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;
|
||||
}
|
||||
|
||||
@ -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');
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user