From d1a5b5e65cc8818d50d9cddbd010223403a45289 Mon Sep 17 00:00:00 2001 From: Nick Lassonde Date: Wed, 22 Feb 2023 21:21:38 -0800 Subject: [PATCH 1/2] add exactConstantFunctions to simplify, to tell mathjs to not convert eg. sin(4) into a number, but leave it is an exact constant --- src/function/algebra/simplifyConstant.js | 4 ++-- test/unit-tests/function/algebra/simplify.test.js | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/function/algebra/simplifyConstant.js b/src/function/algebra/simplifyConstant.js index db86045a19..e2954216c7 100644 --- a/src/function/algebra/simplifyConstant.js +++ b/src/function/algebra/simplifyConstant.js @@ -360,8 +360,8 @@ 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 + if (!args.some(isNode) && !options.exactConstantFunctions) { try { return _eval(node.name, args, options) } catch (ignoreandcontinue) { } diff --git a/test/unit-tests/function/algebra/simplify.test.js b/test/unit-tests/function/algebra/simplify.test.js index 63bce25bcc..8a564438dd 100644 --- a/test/unit-tests/function/algebra/simplify.test.js +++ b/test/unit-tests/function/algebra/simplify.test.js @@ -300,7 +300,12 @@ 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 }) }) it('should collect like terms', function () { From 7ce14f3e2732e9e7e4bafd6a2c9abba41d046f2d Mon Sep 17 00:00:00 2001 From: Nick Lassonde Date: Wed, 22 Feb 2023 21:34:52 -0800 Subject: [PATCH 2/2] allow for calling certain basic functions, such as abs, rounding, basic arithmetic, that should return an exact value --- src/function/algebra/simplifyConstant.js | 5 ++++- test/unit-tests/function/algebra/simplify.test.js | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/function/algebra/simplifyConstant.js b/src/function/algebra/simplifyConstant.js index e2954216c7..782f999fdd 100644 --- a/src/function/algebra/simplifyConstant.js +++ b/src/function/algebra/simplifyConstant.js @@ -361,7 +361,10 @@ export const createSimplifyConstant = /* #__PURE__ */ factory(name, dependencies const args = node.args.map(arg => foldFraction(arg, options)) // If all args are numbers and we want to simplify constanct functions down into a number - if (!args.some(isNode) && !options.exactConstantFunctions) { + // 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) { } diff --git a/test/unit-tests/function/algebra/simplify.test.js b/test/unit-tests/function/algebra/simplify.test.js index 8a564438dd..9af74694d3 100644 --- a/test/unit-tests/function/algebra/simplify.test.js +++ b/test/unit-tests/function/algebra/simplify.test.js @@ -306,6 +306,8 @@ describe('simplify', function () { 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 () {