Skip to content

Commit 4a6464a

Browse files
committed
add another rule for removing brackets involving subtraction
This rule exists to cover a really fiddly case which emerges because of how unary minus is handled inside a product. When looking for additive terms in the expression `-a - 2*b`, the second term is rewritten so that the unary minus is at the bottom of the tree: `(-2) * b` instead of `-(2*b)`. This means that the expression ends up looking like `-a + ((-2)*b)`, so it doesn't match the rule `(-?;b - ?;c)`, because the unary minus is too deep. I've spent a while trying to get the matcher to look deeper for unary minus, but that led to a lot of other things breaking. In the end, I've added another version of the rule which deals with a `+` inside the brackets instead of a `-`.
1 parent 1a89f9b commit 4a6464a

File tree

4 files changed

+11
-0
lines changed

4 files changed

+11
-0
lines changed

runtime/scripts/jme-rules.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2054,6 +2054,7 @@ var simplificationRules = jme.rules.simplificationRules = {
20542054
['imaginary:$n;z * ?;y `where im(z)<0', 'acsg', '-(eval(-z)*y)'], // Pull negation out of products involving negative imaginary numbers
20552055
['-(?;a+?`+;b)','','-a-b'], // Expand negated brackets
20562056
['?;a+(-?;b-?;c)','','a-b-c'], // Remove brackets involving subtraction
2057+
['?;a+(-?;b+?;c)','','a-b+c'], // Remove brackets involving subtraction
20572058
['?;a/?;b/?;c','','a/(b*c)'] // Prefer a product on the denominator to a string of divisions
20582059
],
20592060
collectComplex: [

tests/jme-runtime.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8244,6 +8244,7 @@ var simplificationRules = jme.rules.simplificationRules = {
82448244
['imaginary:$n;z * ?;y `where im(z)<0', 'acsg', '-(eval(-z)*y)'], // Pull negation out of products involving negative imaginary numbers
82458245
['-(?;a+?`+;b)','','-a-b'], // Expand negated brackets
82468246
['?;a+(-?;b-?;c)','','a-b-c'], // Remove brackets involving subtraction
8247+
['?;a+(-?;b+?;c)','','a-b+c'], // Remove brackets involving subtraction
82478248
['?;a/?;b/?;c','','a/(b*c)'] // Prefer a product on the denominator to a string of divisions
82488249
],
82498250
collectComplex: [

tests/jme/jme-tests.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2184,6 +2184,14 @@ Numbas.queueScript('jme_tests',['qunit','jme','jme-rules','jme-display','jme-cal
21842184
assert.equal(simplifyExpression('a/(b/c*d)', 'all'), 'a*c/(b*d)', 'a/(b/c*d)');
21852185
});
21862186

2187+
QUnit.test('brackets involving subtraction', function(assert) {
2188+
function simplifyExpression(expr,rules) {
2189+
return Numbas.jme.display.simplifyExpression(expr,rules || '',Numbas.jme.builtinScope);
2190+
}
2191+
2192+
assert.equal(simplifyExpression('1 + (-a - 2b)'), '1 - a - 2b');
2193+
});
2194+
21872195
QUnit.test('localisation doesn\'t affect treeToJME', function(assert) {
21882196
var notation = Numbas.locale.default_number_notation;
21892197
Numbas.locale.default_number_notation = ['plain-eu'];

tests/numbas-runtime.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7835,6 +7835,7 @@ var simplificationRules = jme.rules.simplificationRules = {
78357835
['imaginary:$n;z * ?;y `where im(z)<0', 'acsg', '-(eval(-z)*y)'], // Pull negation out of products involving negative imaginary numbers
78367836
['-(?;a+?`+;b)','','-a-b'], // Expand negated brackets
78377837
['?;a+(-?;b-?;c)','','a-b-c'], // Remove brackets involving subtraction
7838+
['?;a+(-?;b+?;c)','','a-b+c'], // Remove brackets involving subtraction
78387839
['?;a/?;b/?;c','','a/(b*c)'] // Prefer a product on the denominator to a string of divisions
78397840
],
78407841
collectComplex: [

0 commit comments

Comments
 (0)