Skip to content

Commit

Permalink
piecewise function
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgeny Metelkin committed Jun 22, 2021
1 parent d9236e6 commit d558fae
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mathjs-mathml",
"version": "0.3.0",
"version": "0.3.1",
"description": "Translation of mathjs object to MathML",
"main": "src/index.js",
"scripts": {
Expand Down
23 changes: 18 additions & 5 deletions src/cmathml-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,28 @@ function cMathMLHandler(_this, { handler, csymbols }){
} else if (_this.type === 'FunctionNode') {
let args = _this.args
.map((arg) => arg.toString({handler, csymbols}));
if(_this.fn.name==='cube'){
if (_this.fn.name==='cube') {
return `<apply><power/>${args[0]}<cn>3</cn></apply>`;
}else if(_this.fn.name==='square'){
} else if(_this.fn.name==='square') {
return `<apply><power/>${args[0]}<cn>2</cn></apply>`;
}else if(_this.fn.name==='log' && _this.args.length===2){
} 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==='log2'){
} else if(_this.fn.name==='log2') {
return `<apply><log/><logbase><cn>2</cn></logbase>${args[0]}</apply>`;
}else{ // change only function name
} else if (_this.fn.name === 'piecewise') {
if (args.length === 0) throw new Error('piecewise function must have at least one argument.');

let hasOtherwise = args.length % 2 == 1; // is odd
let otherwiseTag = hasOtherwise ? `<otherwise>${args[args.length - 1]}</otherwise>` : ``;

let piecesCount = Math.floor(args.length / 2);
let piecesTags = [];
for (let i = 0; i < piecesCount; i++) {
let tag = `<piece>${args[2*i+1]}${args[2*i]}</piece>`;
piecesTags.push(tag);
}
return `<piecewise>${piecesTags.join('')}${otherwiseTag}</piecewise>`;
} else { // change only function name
return `<apply><${dictFunc[_this.fn.name] || _this.fn.name}/>${args.join('')}</apply>`;
}
} else if (_this.type === 'OperatorNode') {
Expand Down
8 changes: 8 additions & 0 deletions test/cases/nodes.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,13 @@
"not false": {
"formula": "not false",
"expected": "<apply><not/><false/></apply>"
},
"piecewise": {
"formula": "123 * piecewise(x > 1, 1, x > 2, 2, 0)",
"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><otherwise><cn>0</cn></otherwise></piecewise></apply>"
},
"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>"
}
}

0 comments on commit d558fae

Please sign in to comment.