Skip to content

Commit

Permalink
Fixed #1013: simplify wrongly simplifing some expressions containin…
Browse files Browse the repository at this point in the history
…g unary minus, like `0 - -x`
  • Loading branch information
josdejong committed Jan 13, 2018
1 parent ba03824 commit 8724ae0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 2 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
instead of `f(x, y, z)`. Thanks @joelhoover.
- Fixed `simplify` throwing an error in some cases when simplifying unknown
functions, for example `simplify('f(4)')`. Thanks @joelhoover.
- Fixed #1013: `simplify` wrongly simplifing some expressions containing unary
minus, like `0 - -x`. Thanks @joelhoover.
- Fixed an error in an example in the documentation of `xor`. Thanks @denisx.


Expand Down
3 changes: 0 additions & 3 deletions lib/function/algebra/simplify/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ function factory(type, config, load, typed, math) {
}

function isAssociative(node, context) {
if (!node.args || node.args.length <=1) {
return true;
}
var name = node.fn.toString();
if (context && context.hasOwnProperty(name) && context[name].hasOwnProperty('associative')) {
return context[name].associative;
Expand Down
10 changes: 10 additions & 0 deletions test/function/algebra/simplify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ describe('simplify', function() {
assert.equal(result, "x + y - 1");
});

it('should simplify convert minus and unary minus', function() {
// see https://github.com/josdejong/mathjs/issues/1013
assert.equal(math.simplify('0 - -1', {}).toString(), '1');
assert.equal(math.simplify('0 - -x', {}).toString(), 'x');
assert.equal(math.simplify('0----x', {}).toString(), 'x');
assert.equal(math.simplify('1 - -x', {}).toString(), 'x + 1');
assert.equal(math.simplify('0 - (-x)', {}).toString(), 'x');
assert.equal(math.simplify('-(-x)', {}).toString(), 'x');
assert.equal(math.simplify('0 - (x - y)', {}).toString(), 'y - x');
});

it('should handle custom functions', function() {
function doubleIt (x) { return x + x }
Expand Down

0 comments on commit 8724ae0

Please sign in to comment.