Skip to content

Commit

Permalink
add support of logbase, nthRoot, e, pi
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgeny Metelkin committed Feb 7, 2024
1 parent bce3ef2 commit 672f1a0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/cmathml-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ const dictFunc = require('./dictionary');
}
*/

function cMathMLHandler(_this, { handler, csymbols }){
function cMathMLHandler(_this, { handler, csymbols}) {
if (_this.type === 'SymbolNode') {
let definitionURL = csymbols && csymbols[_this.name];
if (typeof definitionURL === 'undefined') {
return `<ci>${_this.name}</ci>`;
} else {
if (_this.name === 'pi') {
return `<pi/>`;
} else if (_this.name === 'e') {
return `<exponentiale/>`;
} else if (definitionURL) {
// use <csymbol here>
return `<csymbol definitionURL="${definitionURL}">${_this.name}</csymbol>`;
} else {
return `<ci>${_this.name}</ci>`;
}
} else if (_this.type === 'ConstantNode') {
let isExponential = String(_this.value) // if it is exponential form
Expand All @@ -25,6 +29,10 @@ function cMathMLHandler(_this, { handler, csymbols }){
return `<cn type="e-notation">${value[1]}<sep/>${value[2]}</cn>`;
} else if (isBoolean) {
return `<${_this.value}/>`
} else if (_this.value === Infinity) {
return `<infinity/>`;
} else if (isNaN(_this.value)) {
return `<notanumber/>`;
} else {
return `<cn>${_this.value}</cn>`;
}
Expand All @@ -37,8 +45,14 @@ function cMathMLHandler(_this, { handler, csymbols }){
return `<apply><power/>${args[0]}<cn>2</cn></apply>`;
} else if(_this.fn.name==='log' && _this.args.length===2) {
return `<apply><log/><logbase>${args[1]}</logbase>${args[0]}</apply>`;
} else if(_this.fn.name==='logbase') {
return `<apply><log/><logbase>${args[1]}</logbase>${args[0]}</apply>`;
} else if(_this.fn.name==='log2') {
return `<apply><log/><logbase><cn>2</cn></logbase>${args[0]}</apply>`;
} else if (_this.fn.name==='nthRoot' && _this.args.length>=2) {
return `<apply><root/><degree>${args[1]}</degree>${args[0]}</apply>`;
} else if (_this.fn.name==='nthRoot' && _this.args.length===1) {
return `<apply><root/>${args[0]}</apply>`;
} else if (_this.fn.name === 'piecewise') {
if (args.length === 0) throw new Error('piecewise function must have at least one argument.');

Expand Down
4 changes: 4 additions & 0 deletions test/cases/nodes.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,9 @@
"piecewise without otherwise": {
"formula": "123 * piecewise(x > 1, 1, x > 2, 2)",
"expected": "<apply><times/><cn>123</cn><piecewise><piece><cn>1</cn><apply><gt/><ci>x</ci><cn>1</cn></apply></piece><piece><cn>2</cn><apply><gt/><ci>x</ci><cn>2</cn></apply></piece></piecewise></apply>"
},
"Infinity and e": {
"formula": "Infinity * e * NaN * pi",
"expected": "<apply><times/><apply><times/><apply><times/><infinity/><exponentiale/></apply><notanumber/></apply><pi/></apply>"
}
}

0 comments on commit 672f1a0

Please sign in to comment.