-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now that simplifyCore does not do any constant folding, clients may wish to access that behavior via simplifyConstant. Moreover, exporting it makes it easier to use in custom rule lists for simplify(). Also adds docs, embedded docs, and tests for simplifyConstant(). Also fixes simplifyCore() on logical functions (they always return boolean, rather than "short-circuiting"). Resolves #2459.
- Loading branch information
Showing
13 changed files
with
166 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/expression/embeddedDocs/function/algebra/simplifyConstant.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export const simplifyConstantDocs = { | ||
name: 'simplifyConstant', | ||
category: 'Algebra', | ||
syntax: [ | ||
'simplifyConstant(expr)', | ||
'simplifyConstant(expr, options)' | ||
], | ||
description: 'Replace constant subexpressions of node with their values.', | ||
examples: [ | ||
'simplifyConatant("(3-3)*x")', | ||
'simplifyConstant(parse("z-cos(tau/8)"))' | ||
], | ||
seealso: [ | ||
'simplify', 'simplifyCore', 'evaluate' | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// test simplifyConstant | ||
import assert from 'assert' | ||
|
||
import math from '../../../../src/defaultInstance.js' | ||
|
||
describe('simplifyConstant', function () { | ||
const testSimplifyConstant = function (expr, expected, opts = {}, simpOpts = {}) { | ||
let actual = math.simplifyConstant(math.parse(expr), simpOpts).toString(opts) | ||
assert.strictEqual(actual, expected) | ||
actual = math.simplifyConstant(expr, simpOpts).toString(opts) | ||
assert.strictEqual(actual, expected) | ||
} | ||
|
||
it('should evaluate constant subexpressions', () => { | ||
testSimplifyConstant('2+2', '4') | ||
testSimplifyConstant('x+3*5', 'x + 15') | ||
testSimplifyConstant('f(sin(0))', 'f(0)') | ||
testSimplifyConstant('[10/2, y, 8-4]', '[5, y, 4]') | ||
}) | ||
|
||
it('should by default convert decimals into fractions', () => { | ||
testSimplifyConstant('0.5 x', '1 / 2 x') | ||
}) | ||
|
||
it('should coalesce constants in a multi-argument expression', () => { | ||
testSimplifyConstant('3 + x + 7 + y', '10 + x + y') | ||
testSimplifyConstant('3 * x * 7 * y', '21 * x * y') | ||
}) | ||
|
||
it('should respect simplify options', () => { | ||
testSimplifyConstant('0.5 x', '0.5 * x', { implicit: 'show' }, | ||
{ exactFractions: false }) | ||
testSimplifyConstant('0.001 x', '0.001 * x', { implicit: 'show' }, | ||
{ fractionsLimit: 999 }) | ||
testSimplifyConstant('3 * x * 7 * y', '3 * x * 7 * y', {}, | ||
{ context: { multiply: { commutative: false } } }) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.