This function is intended to simplify expressions provided in a string format. As an example:
Input: "+-cos(3x) + (1/4)x^n"
Output: , which will render as the statement below
Latex formatting |
---|
${{ x}^{ n} \over 4}-\cos\left(3~ x\right)$ |
Code Block | ||
---|---|---|
| ||
/* Annoyed about having to format randomly generated math statements? Stop formatting with a ton of if statements! Just questSimplifyTex(string math1, bool includePlus) and it will handle the rest. Input a string of text, like you would on wolframalpha and it will return the math, but simplified and already in TeX The include plus option makes fractions include a plus even if they're the only argument */ function questSimplifyTex(math1, includePlus){ function questFractionsPlus(node, options) { if ((node.type === 'OperatorNode') && (node.fn === 'divide')) { if(node.args[0].fn === 'unaryMinus'){ //negative fraction return('-{' + node.args[0].args[0].toTex(options) + ' \\over ' + node.args[1].toTex(options) + '}'); } else { //positive fraction return('+{' + node.args[0].toTex(options) + ' \\over ' + node.args[1].toTex(options) + '}'); } } } function questFractions(node, options) { if ((node.type === 'OperatorNode') && (node.fn === 'divide')) { if(node.args[0].fn === 'unaryMinus'){ //negative fraction return('-{' + node.args[0].args[0].toTex(options) + ' \\over ' + node.args[1].toTex(options) + '}'); } else { //positive fraction return('{' + node.args[0].toTex(options) + ' \\over ' + node.args[1].toTex(options) + '}'); } } } /* Documentation for math.js customizations is available at https://mathjs.org/docs/reference/functions/simplify.html The rules already baked into the math.js library can be accessed by calling math.simplify.rules, I use firefox inspect element to do this, just right click on a quest editor page and select inspect element. As edge cases arise, they can be added to the rules below */ var questRules = [ //General Rules {l:'(n) * -1/c1', r:'-n/c1'}, {l:'n * -1', r:'-n'}, {l:'(n + n1)/c', r:'n/c + n1/c'}, {l:'-( n + n1)',r:'- n - n1'}, //Cos and Sin specific rules (Making it general creates issues) {l:'n + -cos(n1)/c',r: 'n - (1/c)*cos(n1)'}, {l:'cos(n1)/c',r: '(1/c)*cos(n1)'}, {l:'n + -sin(n1)/c',r: 'n - (1/c)*sin(n1)'}, {l:'sin(n1)/c',r: '(1/c)*sin(n1)'}, //Complex Number Rules (For displaying complex numbers) {l:'(1/c)*i',r:'i/c'}, {l:'i + n', r:'n + i'}, {l:'i - n', r:'-n + i'}, {l:'-i + n', r:'n - i'}, {l:'-i - n', r:'-n - i'}, {l: '(n1/c2 - i)/c',r:'n1/(c2*c) - i/c'}, {l: '(n1/c2 + i)/c',r:'n1/(c2*c) + i/c'}, {l: '(-n1/c2 - i)/c',r:'-n1/(c2*c) - i/c'}, {l: '(-n1/c2 + i)/c',r:'-n1/(c2*c) + i/c'}, {l: '(1 - i)/c',r:'1/c - i/c'}, {l: '(1 + i)/c',r:'1/c + i/c'}, {l: '(-1 - i)/c',r:'-1/c - i/c'}, {l: '(-1 + i)/c',r:'-1/c + i/c'}, {l: 'n + -i/c',r:'n - i/c'} ]; var mathrules = math.simplify.rules.slice(); questRules = mathrules.concat(questRules); var math1_node = math.parse(math1); math1_node = math.simplify(math1_node, questRules); if(includePlus){ var math1Tex = math1_node.toTex({handler: questFractionsPlus}); } else { var math1Tex = math1_node.toTex({handler: questFractions}); } return(math1Tex); } |
...