How to use the eval function from mathjs

Find comprehensive JavaScript mathjs.eval code examples handpicked from public code repositorys.

2
3
4
5
6
7
8
9
10
11
const math = require('mathjs');
module.exports = {
  action: 'copy',
  query: query => {
    try {
      const ans = math.eval(query);
      if (typeof ans === 'function') {
        throw new Error('function returned');
      }
      const items = [];
fork icon68
star icon721
watch icon25

+ 9 other calls in file

27
28
29
30
31
32
33
34
35
36
 *                          - originalSize
 *                          - ratio
 * @return {[type]}         [description]
 */
function getSizeFromRatio(options) {
  var ratio = math.eval(options.ratio);
  return Math.floor(options.size * ratio);
}

function executeResize(options, callback) {
fork icon20
star icon84
watch icon12

+ 13 other calls in file

58
59
60
61
62
63
64
65
66
67
clickOperat(ope, event) {
    const _this = this,
        res = document.querySelector('.result-text');
    switch (ope) {
        case '+/-':
            res.innerHTML = result = math.eval(result + '*-1');
            _this.resize();
            _this.isEqual ? _this.flag = true : '';
            break;
        case '%':
fork icon15
star icon58
watch icon3

+ 47 other calls in file

24
25
26
27
28
29
30
31
32
33
}

run(message, args) {
    var { equation } = args;
    try {
        var solution = math.eval(equation)
    } catch (err) {
        return message.channel.send(`❎ | I couldn\'t solve that equation! \`${err}\``)
    }
    
fork icon44
star icon40
watch icon6

+ 7 other calls in file

6
7
8
9
10
11
12
13
14
15
'#expr': new MagicWord(
  '#expr',
  true,
  (parsingData, [expression = '']) => {
    try {
      return math.eval(expression);
    } catch (e) {
      return `<span class="error"> ${e} </span>`;
    }
  },
fork icon9
star icon42
watch icon5

+ 7 other calls in file

15
16
17
18
19
20
21
22
23
24
/**
 *  @param: eval_string
 *  @param: x
 *  return: 
 */
var f = (eval_string, x) => math.eval(eval_string, { x: x });

/**
 *  @param: eval_string
 *  @param: x
fork icon0
star icon11
watch icon2

+ 13 other calls in file

25
26
27
28
29
30
31
32
33
34

async run(msg, args) { // eslint-disable-line consistent-return
        let out = `${msg.author} asked \` ${args.query}\`\n`;

        try {
                return msg.say(out + math.eval(args.query));
        } catch (err) {
                msg.channel.startTyping();
                wolfram.query(args.query, (error, results) => {
                        if (error) {
fork icon0
star icon2
watch icon1

+ 13 other calls in file

45
46
47
48
49
50
51
52
53
54
Prevent various problems with numbers formatting
*/
var vectoreq = equation.split('*').join('.*').split('/').join('./').split('^').join('.^');
vectoreq = vectoreq.split('..*').join('.*').split('../').join('./').split('..^').join('.^');
 //check if eachseries is being elaborated
var evaluated = math.eval(vectoreq,scope);//evaluate the new value/function in the scope
/*
  Dealing with mathjs result cases:
    1) With ResultSet results it gets the entries[0] array which is the actual elaboration
    2) Converting Unit objects to their value in said measure
fork icon23
star icon77
watch icon11

+ 5 other calls in file

164
165
166
167
168
169
170
171
172
173
 */
function getMatrix(evalAsMath) {
  if (evalAsMath){
    row0 = [
      mathjs.eval($('#matrix-row-0-col-0').val()),
      mathjs.eval($('#matrix-row-0-col-1').val()),
      mathjs.eval($('#matrix-row-0-col-2').val()),
    ];

    row1 = [
fork icon75
star icon0
watch icon0

+ 35 other calls in file

2
3
4
5
6
7
8
9
10
11
var math = require('mathjs');

describe('Numerical methods', function() {

    describe('Integrate', function () {
        var f = math.eval('f(x) = 2 + sin(2 * sqrt(x))');

        it('Simpson rule', function () {
            var result = Integrals.integrate(f, 1, 6, 5, "simpson");
            result.should.be.approximately(8.18301550, 10e-8);
fork icon0
star icon1
watch icon13

+ 13 other calls in file

2
3
4
5
6
7
8
9
10
11
const math = require('mathjs')

Subiex.registerCommand('math', 'default', (message, bot) => {
  let result
  try {
    result = math.eval(message.content)
  } catch (error) {
    console.log('Failed math calculation ' + message.content + '\nError: ' + e.stack)
    return 'Error while evaluating the math expression.'
  } finally {
fork icon11
star icon20
watch icon2

47
48
49
50
51
52
53
54
55

let result;

try {
  // noinspection JSUnresolvedFunction
  result = MathJS.eval(args);
} catch (e) {
  result = e;
}
fork icon2
star icon0
watch icon2

+ 5 other calls in file

45
46
47
48
49
50
51
52
53
54

var radiusW = 0;


while (time > 0) {
    radiusW += math.eval([`t=${time}`, equation])[1];
    time -= inc;
}

var yw = radiusW * Math.cos(Wheading * Math.PI / 180); // y component of vector of wind
fork icon1
star icon3
watch icon14

159
160
161
162
163
164
165
166
167
168
assert(_.isArray(calibratorData0), "expected calibratorData to be an array");
const calibratorData = _.sortBy(calibratorData0, calibratorVariable);
const dataLE = _.last(_.filter(calibratorData, x => x[calibratorVariable] <= targetValue));
const dataGE = _.first(_.filter(calibratorData, x => x[calibratorVariable] >= targetValue));
const valueLE = math.eval(dataLE[calibratorVariable]);
const valueGE = math.eval(dataGE[calibratorVariable]);
const volumeLE = math.eval(dataLE.volume);
const volumeGE = math.eval(dataGE.volume);
if (math.equal(valueLE, targetValue)) {
        item.volume = volumeLE;
fork icon0
star icon9
watch icon3

+ 9 other calls in file

222
223
224
225
226
227
228
229
230
231
            let y;
            if (nexty) {
                y = nexty;
            } else {
                mathjs.eval("x="+i, scope);
                y = mathjs.eval(fx, scope);
            }
            
//            if (y > (ysize/zoom)/2 || y < -(ysize/zoom)/2) { //too conservative
//                continue;
fork icon0
star icon1
watch icon0

+ 15 other calls in file

120
121
122
123
124
125
126
127
128
129
    throw new Error('Dir must be \'left\', \'middle\' or \'right\'');

try { 
    point = math.bignumber(point);
    if (dir === 'middle' || dir === 'mid') {
        let s1 = math.eval(expression.toString(), { x: point.sub(SMALL_NUMBER) });
        let s2 = math.eval(expression.toString(), { x: point.add(SMALL_NUMBER) });

        if (math.abs(s1.sub(s2)).greaterThan(math.EPSILON))
            throw new Error(`Limit does not converge from both sides: x<sup>-</sup> = ${s1.toExponential()} while x<sup>+</sup> = ${s2.toExponential()}`);
fork icon0
star icon0
watch icon5

+ 69 other calls in file