Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/function/algebra/simplify.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ export const createSimplify = /* #__PURE__ */ factory(name, dependencies, (
}
newRule.l = removeParens(parse(newRule.l))
newRule.r = removeParens(parse(newRule.r))
for (const prop of ['imposeContext', 'repeat', 'assuming']) {
for (const prop of ['imposeContext', 'repeat', 'assuming', 'ifCondition']) {
if (prop in ruleObject) {
newRule[prop] = ruleObject[prop]
}
Expand Down Expand Up @@ -698,6 +698,12 @@ export const createSimplify = /* #__PURE__ */ factory(name, dependencies, (
}
}

if (matches && rule.ifCondition) {
if (!rule.ifCondition(matches)) {
matches = false
}
}

if (matches) {
// const before = res.toString({parenthesis: 'all'})

Expand Down
16 changes: 16 additions & 0 deletions test/unit-tests/function/algebra/simplify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -705,4 +705,20 @@ describe('simplify', function () {
assert(!isNaN(posex.evaluate(zeroes)))
assert.notEqual(expr.evaluate(zeroes), posex.evaluate(zeroes))
})

it('can should simplify when given an ifCondition rule', function () {
const yBeforeXRule = [
{
l: 've1 + ve2',
r: 've2 + ve1',
ifCondition: (matches) => {
return matches.placeholders.ve2.toString().indexOf('y') >= 0 && matches.placeholders.ve1.toString().indexOf('x') >= 0
}
}
]

simplifyAndCompare('x + y', 'y + x', yBeforeXRule)
simplifyAndCompare('y + x', 'y + x', yBeforeXRule)
simplifyAndCompare('x^2 + y^2', 'y^2 + x^2', yBeforeXRule)
})
})