function mathJsonToMathjs(mathJson, config) {
let result;
if (mathJson === undefined) return undefined;
if (
typeof mathJson === 'number' ||
mathJson.num !== undefined
) {
let n =
typeof mathJson === 'number'
? mathJson
: mathJson.num;
if (config.number === 'BigNumber')
n = window.math.bignumber(n);
result = new window.math.expression.node.ConstantNode(
n
);
result = applySuperscriptAsPower(
result,
mathJson,
config
);
} else if (
typeof mathJson === 'string' ||
mathJson.sym !== undefined
) {
const BUILT_IN_CONSTANTS = {
π: window.math.pi,
τ: window.math.tau,
ℯ: window.math.e,
ⅇ: window.math.e,
e: window.math.e,
ϕ: window.math.phi, // GREEK SMALL LETTER PHI
ⅈ: window.math.i, // ⅈ DOUBLE-STRUCK ITALIC SMALL I
ⅉ: window.math.i, // ⅉ DOUBLE-STRUCK ITALIC SMALL J
i: window.math.i, //
};
const symbol =
typeof mathJson === 'string'
? mathJson
: mathJson.sym;
if (BUILT_IN_CONSTANTS[symbol]) {
result = new window.math.expression.node.ConstantNode(
BUILT_IN_CONSTANTS[symbol]
);
} else {
result = new window.math.expression.node.SymbolNode(
MASTON.asSymbol(mathJson)
);
}
result = applySuperscriptAsPower(
result,
mathJson,
config
);
} else if (mathJson.op !== undefined) {
if (
mathJson.lhs !== undefined &&
mathJson.rhs !== undefined
) {
const OPERATOR_FUNCTIONS = {
'+': 'add',
'-': 'subtract',
'*': 'multiply',
'/': 'divide',
// '.*': 'dotMultiply',
// './': 'dotDivide',
'%': 'mod',
mod: 'mod',
};
const args = [
mathJsonToMathjs(mathJson.lhs, config),
mathJsonToMathjs(mathJson.rhs, config),
];
result = new window.math.expression.node.OperatorNode(
mathJson.op,
OPERATOR_FUNCTIONS[mathJson.op],
args
);
} else if (mathJson.rhs !== undefined) {
const UNARY_OPERATOR_FUNCTIONS = {
'-': 'unaryMinus',
'+': 'unaryPlus',
// '~': 'bitNot',
// 'not': 'not'
};
result = new window.math.expression.node.OperatorNode(
mathJson.op,
UNARY_OPERATOR_FUNCTIONS[mathJson.op],
[mathJsonToMathjs(mathJson.rhs, config)]
);
}
} else if (mathJson.fn) {
if (
mathJson.fn === 'log' ||
(mathJson.fn === 'ln' &&
mathJson.fn.sub !== undefined)
) {
result = new window.math.expression.node.FunctionNode(
'log',
getMathjsArgsWithSub(mathJson, config)
);
} else if (mathJson.fn === 'lb') {
const args = getMathjsArgs(mathJson, config);
args.push(
new window.math.expression.node.ConstantNode(
window.math.bignumber(2)
)
);
result = new window.math.expression.node.FunctionNode(
'log',
args
);
} else if (mathJson.fn === 'lg') {
result = new window.math.expression.node.FunctionNode(
new window.math.expression.node.SymbolNode(
'log10'
),
getMathjsArgs(mathJson, config)
);
} else {
const fnName =
{
'+': 'add',
'-': 'subtract',
'*': 'multiply',
'/': 'divide',
randomReal: 'random',
randomInteger: 'randomInt',
Gamma: 'gamma',
Re: 're',
Im: 'im',
binom: 'composition',
ucorner: 'ceil',
lcorner: 'floor',
arccos: 'acos',
arcsin: 'asin',
arctan: 'atan',
arcosh: 'acosh',
arsinh: ' asinh',
}[mathJson.fn] || mathJson.fn;
result = new window.math.expression.node.FunctionNode(
fnName,
getMathjsArgs(mathJson, config)
);
}
} else if (mathJson.group) {
result = applySuperscriptAsPower(
mathJsonToMathjs(mathJson.group, config),
mathJson,
config
);
}
return result;
}