Skip to content

Commit a9d5e30

Browse files
committed
fix single minus sign bug
1 parent c2da6c0 commit a9d5e30

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

lib/trees/default_order.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function remove_duplicate_negatives(tree) {
1111
var operator = tree[0];
1212
var operands = tree.slice(1);
1313

14-
if (operator === '-' && operands[0][0] === '-') {
14+
if (operator === '-' && Array.isArray(operands[0]) && operands[0][0] === '-') {
1515
return remove_duplicate_negatives(operands[0][1]);
1616
}
1717

@@ -43,7 +43,7 @@ function normalize_negatives_in_factors(tree) {
4343
var operands_no_negatives = [];
4444

4545
for (var i = 0; i < operands.length; i++) {
46-
if (operands[i][0] === '-') {
46+
if (Array.isArray(operands[i]) && operands[i][0] === '-') {
4747
sign *= -1;
4848
operands_no_negatives.push(operands[i][1]);
4949
}
@@ -78,6 +78,9 @@ function sort_key(tree, params = {}) {
7878
}
7979
if (typeof tree === 'string') {
8080
// if string is a constant, return number with value?
81+
if(tree === "-" || tree === "+") {
82+
return [8, 'plus_minus_string', tree]
83+
}
8184
return [1, 'symbol', tree];
8285
}
8386
if (typeof tree === 'boolean') {
@@ -227,7 +230,7 @@ function coeff_factors_from_term(term, string_factors) {
227230
}
228231
f[ind]++;
229232
continue;
230-
} else if (Array.isArray(factor) && factor[0] === "^" || factor[0] === '-') {
233+
} else if (Array.isArray(factor) && (factor[0] === "^" || factor[0] === '-')) {
231234
let result = coeff_factors_from_term(factor, string_factors);
232235
for (let ind in result.factor_contains) {
233236
if (f[ind] === undefined) {

spec/slow_simplify.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,11 @@ describe("collect like terms and factor", function () {
563563
me.fromText("$(x-b+g) + (y-a+f)% + (z-c+e)deg").default_order().tree)
564564
})
565565

566+
it("handle lone - or + signs", function () {
567+
expect(me.fromText("cos(2 pi (-))").collect_like_terms_factors().tree).toEqual(me.fromText("cos(2 pi (-))").tree)
568+
expect(me.fromText("cos(2 pi (+))").collect_like_terms_factors().tree).toEqual(me.fromText("cos(2 pi (+))").tree)
569+
})
570+
566571
});
567572

568573
describe("matrix and vector simplify", function () {

0 commit comments

Comments
 (0)