mathsteps-experimental-fork
Version:
Step by step math solutions. Experimental Fork
69 lines (65 loc) • 2.91 kB
JavaScript
'use strict';
const config = require('../../config.js');
const index = require('../../node/index.js');
const ChangeTypes = require('../../types/changeType/ChangeTypes.js');
const evaluateHandlersForBuiltInFunctions = {
// Trigonometry: basic
sin: { fn: config.default.sin, stepId: ChangeTypes.ChangeTypes.KEMU_FUNCTION_VALUE },
cos: { fn: config.default.cos, stepId: ChangeTypes.ChangeTypes.KEMU_FUNCTION_VALUE },
tg: { fn: config.default.tan, stepId: ChangeTypes.ChangeTypes.KEMU_FUNCTION_VALUE },
ctg: { fn: config.default.cot, stepId: ChangeTypes.ChangeTypes.KEMU_FUNCTION_VALUE },
asin: { fn: config.default.asin, stepId: ChangeTypes.ChangeTypes.KEMU_FUNCTION_VALUE },
acos: { fn: config.default.acos, stepId: ChangeTypes.ChangeTypes.KEMU_FUNCTION_VALUE },
atan: { fn: config.default.atan, stepId: ChangeTypes.ChangeTypes.KEMU_FUNCTION_VALUE },
acot: { fn: config.default.acot, stepId: ChangeTypes.ChangeTypes.KEMU_FUNCTION_VALUE },
// Other
exp: { fn: config.default.exp, stepId: ChangeTypes.ChangeTypes.KEMU_FUNCTION_VALUE },
sqrt: { fn: config.default.sqrt, stepId: ChangeTypes.ChangeTypes.KEMU_NUMERICAL_SQRT },
};
function calculateNumericalValue(node, simplifyCtx) {
let rv = null;
if (!simplifyCtx.expressionCtx
|| !simplifyCtx.expressionCtx.isNumerical()) ;
else if (index.default.Type.isFunction(node) && index.default.Type.isConstant(node.args[0])) {
// One arg function with constant argument: f(const).
const evaluateHandler = evaluateHandlersForBuiltInFunctions[node.fn.name];
if (evaluateHandler) {
const argument = node.args[0].value;
const value = evaluateHandler.fn(argument);
const newNode = index.default.Creator.constant(value);
rv = {
changeType: evaluateHandler.stepId,
rootNode: newNode,
};
}
}
else if (index.default.Type.isFunction(node)
&& (node.fn.name === 'logXY')
&& index.default.Type.isConstant(node.args[0])
&& index.default.Type.isConstant(node.args[1])) {
// logarithm from constant and with constant base.
// Example: logXY(10, 100) = 2
const base = node.args[0].value;
const x = node.args[1].x.value;
const value = config.default.log(x, base);
const newNode = index.default.Creator.constant(value);
rv = {
changeType: ChangeTypes.ChangeTypes.KEMU_FUNCTION_VALUE,
rootNode: newNode,
};
}
else if ((node.op == '/')
&& index.default.Type.isConstant(node.args[0])
&& index.default.Type.isConstant(node.args[1])) {
// a / b
const a = node.args[0].value;
const b = node.args[1].value;
const newNode = index.default.Creator.constant(config.default.divide(a, b));
rv = {
changeType: ChangeTypes.ChangeTypes.KEMU_NUMERICAL_DIV,
rootNode: newNode,
};
}
return rv
}
module.exports = calculateNumericalValue;