Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add exactConstantFunctions to simplify, to tell mathjs to not convert constant functions into decimals #2905

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/function/algebra/simplifyConstant.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,11 @@ export const createSimplifyConstant = /* #__PURE__ */ factory(name, dependencies
if (operatorFunctions.indexOf(node.name) === -1) {
const args = node.args.map(arg => foldFraction(arg, options))

// If all args are numbers
if (!args.some(isNode)) {
// If all args are numbers and we want to simplify constanct functions down into a number
// allow calling the following simple functions that return exact values
const exactFunctions = [...operatorFunctions, 'abs', 'ceil', 'fix', 'floor', 'round', 'sign', 'subtract', 'unaryMinus', 'unaryPlus']

if (!args.some(isNode) && (!options.exactConstantFunctions || exactFunctions.indexOf(node.name) > -1)) {
try {
return _eval(node.name, args, options)
} catch (ignoreandcontinue) { }
Expand Down
7 changes: 7 additions & 0 deletions test/unit-tests/function/algebra/simplify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,14 @@ describe('simplify', function () {
})

it('should simplify non-rational expressions with no symbols to number', function () {
// default turn all constants into decimals
simplifyAndCompare('3+sin(4)', '2.2431975046920716')
// exactConstantFunctions lets sin, cos, log, etc stay in exact form
simplifyAndCompare('3+sin(4)', '3+sin(4)', {}, { exactConstantFunctions: true })
// but still make sure to consolidate the numeric constants
simplifyAndCompare('3+(4*3)', '15', {}, { exactConstantFunctions: true })
// when we say we want exact functions, abs is still exact
simplifyAndCompare('abs(-2)', '2', {}, { exactConstantFunctions: true })
})

it('should collect like terms', function () {
Expand Down