# How to use the mathjs.eval function in mathjs

## To help you get started, we’ve selected a few mathjs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

heedy / heedy / 0.3 / frontend / js / sagas / analysis.js View on Github
if (analysis.xdataset) {
if (
analysis.stream.length === 0 || analysis.stream.split("/").length != 3
) {
yield put({
type: "ANALYSIS_ERROR",
value: "Invalid correlation stream name"
});
return;
}
query.stream = analysis.stream;
query.transform = analysis.transform;
} else {
// Make sure that dt is a number
try {
let dt = math.eval(analysis.dt);
if (isNaN(dt) || dt &lt; 0.001) {
yield put({
type: "ANALYSIS_ERROR",
value: "Invalid time delta (" + analysis.dt + ")"
});
return;
}
query.dt = dt;
} catch (e) {
yield put({
type: "ANALYSIS_ERROR",
value: "Invalid time delta (" + analysis.dt + ")"
});
return;
}
}
fukaoi / crystal-nodejs / spec / js / file_run_npm.js View on Github
const math = require('mathjs');

math.round(math.e, 3)            // 2.718
math.atan2(3, -3) / math.pi      // 0.75
math.log(10000, 10)              // 4
math.sqrt(-4)                    // 2i
math.derivative('x^2 + x', 'x')  // 2*x+1
math.pow([[-1, 2], [3, 1]], 2)
// [[7, 0], [0, 7]]

// expressions
math.eval('1.2 * (2 + 4.5)')     // 7.8
math.eval('12.7 cm to inch')     // 5 inch
math.eval('sin(45 deg) ^ 2')     // 0.5
math.eval('9 / 3 + 2i')          // 3 + 2i
math.eval('det([-1, 2; 3, 1])')  // -7

// chaining
const res = math.chain(3)
.multiply(2)
.done() // 14

toCrystal({result: res});
keybase / keybase-bot / demos / es7 / math-bot-es7.js View on Github
const msgReply = s => {
let a1, a2, ans, b1, b2, eqn
try {
ans = '= ' + mathjs['eval'](s).toString()
} catch (e) {
a1 = Math.floor(Math.random() * 10)
b1 = Math.floor(Math.random() * 10)
a2 = Math.floor(Math.random() * 10)
b2 = Math.floor(Math.random() * 10)
eqn = '(' + a1 + ' + ' + b1 + 'i) * (' + a2 + ' + ' + b2 + 'i)'
ans =
"Sorry, I can't do that math. Did you know " + eqn + ' = ' + mathjs['eval'](eqn).toString() + '? True.'
}
return ans
}
diefarbe / node-rest-api / src / utils / Animations.ts View on Github
private static signalAnimationHelper(channelAnimation: IChannelAnimation, value: number): IChannelInfo {
const scope = {
signal: value
};
return {
downDecrement: channelAnimation.downDecrement === undefined ? undefined : math.eval(channelAnimation.downDecrement, scope),
downDecrementDelay: channelAnimation.downDecrementDelay === undefined ? undefined : math.eval(channelAnimation.downDecrementDelay, scope),
downHoldDelay: channelAnimation.downHoldDelay === undefined ? undefined : math.eval(channelAnimation.downHoldDelay, scope),
downHoldLevel: channelAnimation.downHoldLevel === undefined ? undefined : math.eval(channelAnimation.downHoldLevel, scope),
downMinimumLevel: channelAnimation.downMinimumLevel === undefined ? undefined : math.eval(channelAnimation.downMinimumLevel, scope),

direction: channelAnimation.direction === undefined ? undefined : channelAnimation.direction,
effectId: channelAnimation.effectId === undefined ? undefined : math.eval(channelAnimation.effectId, scope),

upHoldDelay: channelAnimation.upHoldDelay === undefined ? undefined : math.eval(channelAnimation.upHoldDelay, scope),
upHoldLevel: channelAnimation.upHoldLevel === undefined ? undefined : math.eval(channelAnimation.upHoldLevel, scope),
upIncrement: channelAnimation.upIncrement === undefined ? undefined : math.eval(channelAnimation.upIncrement, scope),
upIncrementDelay: channelAnimation.upIncrementDelay === undefined ? undefined : math.eval(channelAnimation.upIncrementDelay, scope),
upMaximumLevel: channelAnimation.upMaximumLevel === undefined ? undefined : math.eval(channelAnimation.upMaximumLevel, scope),

startDelay: channelAnimation.startDelay === undefined ? undefined : math.eval(channelAnimation.startDelay, scope),
transition: channelAnimation.transition === undefined ? undefined : math.eval(channelAnimation.transition, scope),
};
diefarbe / node-rest-api / src / modules / signals.ts View on Github
private signalAnimationHelper(channelAnimation: ChannelAnimation, value: number): ChannelInfo {
const scope = {
signal: value
};
return {
upHoldLevel: channelAnimation.upHoldLevel === undefined ? undefined : math.eval(channelAnimation.upHoldLevel, scope),
downHoldLevel: channelAnimation.downHoldLevel === undefined ? undefined : math.eval(channelAnimation.downHoldLevel, scope),
upMaximumLevel: channelAnimation.upMaximumLevel === undefined ? undefined : math.eval(channelAnimation.upMaximumLevel, scope),
downMinimumLevel: channelAnimation.downMinimumLevel === undefined ? undefined : math.eval(channelAnimation.downMinimumLevel, scope),
upHoldDelay: channelAnimation.upHoldDelay === undefined ? undefined : math.eval(channelAnimation.upHoldDelay, scope),
downHoldDelay: channelAnimation.downHoldDelay === undefined ? undefined : math.eval(channelAnimation.downHoldDelay, scope),
upIncrement: channelAnimation.upIncrement === undefined ? undefined : math.eval(channelAnimation.upIncrement, scope),
downDecrement: channelAnimation.downDecrement === undefined ? undefined : math.eval(channelAnimation.downDecrement, scope),
upIncrementDelay: channelAnimation.upIncrementDelay === undefined ? undefined : math.eval(channelAnimation.upIncrementDelay, scope),
downDecrementDelay: channelAnimation.downDecrementDelay === undefined ? undefined : math.eval(channelAnimation.downDecrementDelay, scope),
startDelay: channelAnimation.startDelay === undefined ? undefined : math.eval(channelAnimation.startDelay, scope),
effectId: channelAnimation.effectId === undefined ? undefined : math.eval(channelAnimation.effectId, scope),
direction: channelAnimation.direction === undefined ? undefined : math.eval(channelAnimation.direction, scope),
transition: channelAnimation.transition === undefined ? undefined : math.eval(channelAnimation.transition, scope),
};
}
getguesstimate / guesstimate-app / src / models / function-form.js View on Github
_calculate(){
let shorten = (str) => { return str.substring(1, str.length); };
let shortened = shorten(this.state);
let correct = math.eval(replaced);
return {mean: correct, stdev: 0};
}
_inputs(){
javascript-machine-learning / movielens-recommender-system-javascript / src / strategies / linearRegression.js View on Github
export function computeCost(X, y, theta) {
let m = y.length;

let predictions = math.eval('X * theta', {
X,
theta,
});

let sqrErrors = math.eval('(predictions - y).^2', {
predictions,
y,
});

let J = math.eval(1 / (2 * m) * sum(sqrErrors), {
m,
sqrErrors,
});

return J;
}
PresearchOfficial / presearch-packages / packages / math / index.js View on Github
async function trigger(query) {
if(!isNaN(query)) {
return false;
}
try {
mathjs.eval(query);
return true;
}
catch (error) {
return false;
}
}
diefarbe / node-rest-api / src / modules / signals.ts View on Github
private signalAnimationHelper(channelAnimation: ChannelAnimation, value: number): ChannelInfo {
const scope = {
signal: value
};
return {
upHoldLevel: channelAnimation.upHoldLevel === undefined ? undefined : math.eval(channelAnimation.upHoldLevel, scope),
downHoldLevel: channelAnimation.downHoldLevel === undefined ? undefined : math.eval(channelAnimation.downHoldLevel, scope),
upMaximumLevel: channelAnimation.upMaximumLevel === undefined ? undefined : math.eval(channelAnimation.upMaximumLevel, scope),
downMinimumLevel: channelAnimation.downMinimumLevel === undefined ? undefined : math.eval(channelAnimation.downMinimumLevel, scope),
upHoldDelay: channelAnimation.upHoldDelay === undefined ? undefined : math.eval(channelAnimation.upHoldDelay, scope),
downHoldDelay: channelAnimation.downHoldDelay === undefined ? undefined : math.eval(channelAnimation.downHoldDelay, scope),
upIncrement: channelAnimation.upIncrement === undefined ? undefined : math.eval(channelAnimation.upIncrement, scope),
downDecrement: channelAnimation.downDecrement === undefined ? undefined : math.eval(channelAnimation.downDecrement, scope),
upIncrementDelay: channelAnimation.upIncrementDelay === undefined ? undefined : math.eval(channelAnimation.upIncrementDelay, scope),
downDecrementDelay: channelAnimation.downDecrementDelay === undefined ? undefined : math.eval(channelAnimation.downDecrementDelay, scope),
startDelay: channelAnimation.startDelay === undefined ? undefined : math.eval(channelAnimation.startDelay, scope),
effectId: channelAnimation.effectId === undefined ? undefined : math.eval(channelAnimation.effectId, scope),
direction: channelAnimation.direction === undefined ? undefined : math.eval(channelAnimation.direction, scope),
transition: channelAnimation.transition === undefined ? undefined : math.eval(channelAnimation.transition, scope),
};
}
diefarbe / node-rest-api / src / modules / signals.ts View on Github
private signalAnimationHelper(channelAnimation: ChannelAnimation, value: number): ChannelInfo {
const scope = {
signal: value
};
return {
upHoldLevel: channelAnimation.upHoldLevel === undefined ? undefined : math.eval(channelAnimation.upHoldLevel, scope),
downHoldLevel: channelAnimation.downHoldLevel === undefined ? undefined : math.eval(channelAnimation.downHoldLevel, scope),
upMaximumLevel: channelAnimation.upMaximumLevel === undefined ? undefined : math.eval(channelAnimation.upMaximumLevel, scope),
downMinimumLevel: channelAnimation.downMinimumLevel === undefined ? undefined : math.eval(channelAnimation.downMinimumLevel, scope),
upHoldDelay: channelAnimation.upHoldDelay === undefined ? undefined : math.eval(channelAnimation.upHoldDelay, scope),
downHoldDelay: channelAnimation.downHoldDelay === undefined ? undefined : math.eval(channelAnimation.downHoldDelay, scope),
upIncrement: channelAnimation.upIncrement === undefined ? undefined : math.eval(channelAnimation.upIncrement, scope),
downDecrement: channelAnimation.downDecrement === undefined ? undefined : math.eval(channelAnimation.downDecrement, scope),
upIncrementDelay: channelAnimation.upIncrementDelay === undefined ? undefined : math.eval(channelAnimation.upIncrementDelay, scope),
downDecrementDelay: channelAnimation.downDecrementDelay === undefined ? undefined : math.eval(channelAnimation.downDecrementDelay, scope),
startDelay: channelAnimation.startDelay === undefined ? undefined : math.eval(channelAnimation.startDelay, scope),
effectId: channelAnimation.effectId === undefined ? undefined : math.eval(channelAnimation.effectId, scope),
direction: channelAnimation.direction === undefined ? undefined : math.eval(channelAnimation.direction, scope),
transition: channelAnimation.transition === undefined ? undefined : math.eval(channelAnimation.transition, scope),
};
}

## mathjs

Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser with support for symbolic computation, comes with a large set of built-in functions and constants, and offers an integrated solution to work with dif

Apache-2.0