Replies: 3 comments 6 replies
-
|
We can discuss how to improve on this. Let's do that in the discussions section. |
Beta Was this translation helpful? Give feedback.
-
|
The upcoming journey seems to be long, very long... Then, let us state that some operations on integers are absolutely safe. These are: addition, division, multiplication. Then the corresponding simplification rules are: safeIntegerSimplification = [
{ l: "m1+m2", r: "forceSimplifyConstants(m1+m2)" },
{ l: "m1-m2", r: "forceSimplifyConstants(m1-m2)" },
{ l: "m1*m2", r: "forceSimplifyConstants(m1*m2)" },
];
function forceSimplifyConstantsInNode(node){
if(node.fn && node.fn.name === 'forceSimplifyConstants'){
return math.simplifyConstant(node.args[0]);
}
for(let i = 0; node.args && i < node.args.length; i++){
node.args[i] = forceSimplifyConstantsInNode(node.args[i]);
}
return node;
}Now we can deal with fractions, e.g. The problem sounds classic and should have been solved somehow - e.g. in Maxima. |
Beta Was this translation helpful? Give feedback.
-
|
I did a completely other attempt, my it didn't work for me (it runs forever). Maybe someone can find anything useful in it :) let mathjsRules = {};
mathjsRules.simplifyCarefully = [
{ l: 'ce1', r: 'carefullySimplifyConstants(ce1)', repeat: false },
mathjs_helpers.carefullySimplifyConstantsInNode,
];
let mathjs_helpers = {
carefullySimplifyConstantsInNode : function(node){
if(node.fn && node.fn.name === 'carefullySimplifyConstants'){
let simp = math.simplifyConstant(node.args[0]/*,{exactFractions:true}*/);
if (simp.toTex().length < 10){
return simp;
}
//Failure!
}
for(let i = 0; node.args && i < node.args.length; i++){
node.args[i] = mathjs_helpers.carefullySimplifyConstantsInNode(node.args[i]);
}
return node;
},
};
let e1 = math.parse('1+2+sqrt(3)+sqrt(4)');
math.simplify(e1).toTex();
math.simplify(e1,mathjsRules.simplifyCarefully, {repeat:false}).toTex(); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
The function
simplifyalways evaluatessqrtinstead of leaving some forms such assqrt(2)unchanged.To Reproduce
Simplify the following:
The result is a
ConstantNodewith value1.4142135623730951, but I expected aFunctionNodewhere thesqrtfunction was not applied.When its argument is an exact, it feels like
sqrtshould only simplify to a constant if the result is an exact integer. This would be akin todividethat simplifies fractions, but does not apply division if the result is not an integer.Beta Was this translation helpful? Give feedback.
All reactions