Skip to content

Commit 9eb057a

Browse files
committed
Merge remote-tracking branch 'origin/develop' into develop
2 parents 9bf11be + 84c3abd commit 9eb057a

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

src/function/algebra/simplify.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -285,12 +285,20 @@ export const createSimplify = /* #__PURE__ */ factory(name, dependencies, (
285285
{ l: 'log(e)', r: '1' },
286286

287287
// temporary rules
288+
// Note initially we tend constants to the right because like-term
289+
// collection prefers the left, and we would rather collect nonconstants
288290
{ l: 'n-n1', r: 'n+-n1' }, // temporarily replace 'subtract' so we can further flatten the 'add' operator
289-
{ l: '-(c*v)', r: '(-c) * v' }, // make non-constant terms positive
290-
{ l: '-v', r: '(-1) * v' },
291+
{ l: '-(c*v)', r: 'v * (-c)' }, // make non-constant terms positive
292+
{ l: '-v', r: 'v * (-1)' },
291293
{ l: 'n/n1^n2', r: 'n*n1^-n2' }, // temporarily replace 'divide' so we can further flatten the 'multiply' operator
292294
{ l: 'n/n1', r: 'n*n1^-1' },
293295

296+
// remove parenthesis in the case of negating a quantity
297+
{ l: 'n1 + (n2 + n3)*(-1)', r: 'n1 + n2*(-1) + n3*(-1)' },
298+
// subsume resulting -1 into constants where possible
299+
{ l: '(-1) * c', r: '-c' },
300+
{ l: '(-1) * (-c)', r: 'c' },
301+
294302
// expand nested exponentiation
295303
{ l: '(n ^ n1) ^ n2', r: 'n ^ (n1 * n2)' },
296304

@@ -302,17 +310,15 @@ export const createSimplify = /* #__PURE__ */ factory(name, dependencies, (
302310
// collect like terms
303311
{ l: 'n+n', r: '2*n' },
304312
{ l: 'n+-n', r: '0' },
305-
{ l: 'n1*n2 + n2', r: '(n1+1)*n2' },
306-
{ l: 'n1*n3 + n2*n3', r: '(n1+n2)*n3' },
307-
308-
// remove parenthesis in the case of negating a quantitiy
309-
{ l: 'n1 + -1 * (n2 + n3)', r: 'n1 + -1 * n2 + -1 * n3' },
313+
{ l: 'v*n + v', r: 'v*(n+1)' }, // NOTE: leftmost position is special:
314+
{ l: 'n3*n1 + n3*n2', r: 'n3*(n1+n2)' }, // All sub-monomials tried there.
315+
{ l: 'n*c + c', r: '(n+1)*c' },
310316

311317
simplifyConstant,
312318

313319
{ l: '(-n)*n1', r: '-(n*n1)' }, // make factors positive (and undo 'make non-constant terms positive')
314320

315-
// ordering of constants
321+
// final ordering of constants
316322
{ l: 'c+v', r: 'v+c', context: { add: { commutative: false } } },
317323
{ l: 'v*c', r: 'c*v', context: { multiply: { commutative: false } } },
318324

@@ -376,7 +382,7 @@ export const createSimplify = /* #__PURE__ */ factory(name, dependencies, (
376382
r: removeParens(parse(rule.r))
377383
}
378384
if (rule.context) {
379-
newRule.evaluate = rule.context
385+
newRule.context = rule.context
380386
}
381387
if (rule.evaluate) {
382388
newRule.evaluate = parse(rule.evaluate)

test/unit-tests/function/algebra/simplify.test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,12 @@ describe('simplify', function () {
231231
simplifyAndCompare('x+x', '2*x')
232232
simplifyAndCompare('2x+x', '3*x')
233233
simplifyAndCompare('2(x+1)+(x+1)', '3*(x + 1)')
234-
simplifyAndCompare('y*x^2+2*x^2', '(y+2)*x^2')
234+
simplifyAndCompare('2(x+1)+x+1', '3*(x + 1)')
235+
simplifyAndCompare('y*x^2+2*x^2', 'x^2*(y+2)')
236+
simplifyAndCompare('x*y + y*x', '2*x*y')
237+
simplifyAndCompare('x*y - y*x', '0')
238+
simplifyAndCompare('x^2*y^3*z - y*z*y*x^2*y', '0')
239+
simplifyAndCompare('x^2*y^3*z - y*z*x^2*y', 'x^2*z*(y^3-y^2)')
235240
})
236241

237242
it('should collect separated like terms', function () {
@@ -246,6 +251,7 @@ describe('simplify', function () {
246251
simplifyAndCompare('10 - (x - 2)', '12 - x')
247252
simplifyAndCompare('x - (y + x)', '-y')
248253
simplifyAndCompare('x - (y - (y - x))', '0')
254+
simplifyAndCompare('5 + (5 * x) - (3 * x) + 2', '2*x+7')
249255
})
250256

251257
it('should collect separated like factors', function () {

0 commit comments

Comments
 (0)