Open In App

How to Evaluate Expressions with math.js?

Last Updated : 23 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Math.js is a powerful library that can be used to perform complex mathematical computations and evaluations. It eases working with mathematical expressions, allowing you to evaluate single expressions, handle multiple expressions, and use variables with defined scopes.

You can install math.js using npm with the following command:

npm i mathjs

Alternatively, you can include it in your HTML file directly from a CDN:

<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/10.0.0/math.js"></script>

Approach

  • Evaluating a Single Expression
  • Evaluating Multiple Expressions
  • Using Scope to Evaluate Expressions

Evaluating a Single Expression

In this approach, we are using Math.js to evaluate a single mathematical expression. By importing the Math.js library and using its evaluate method, we can compute the result of the expression 2 + 3 * sqrt(4) / 2 and print it to the console.

Example: The below example evaluates a single expression with Math.js.

JavaScript
// script.js

const math = require('mathjs');

const expression = '2 + 3 * sqrt(4) / 2';
const result = math.evaluate(expression);

console.log(`The result of the expression "${expression}" is ${result}`);

Output

The result of the expression "2 + 3 * sqrt(4) / 2" is 5

Evaluating Multiple Expressions

In this approach, we are using Math.js to evaluate multiple mathematical expressions. We define an array of expressions and use the evaluate method to compute their results. The script then prints each expression alongside its computed result.

Example: The below example evaluates multiple expressions with Math.js.

JavaScript
// script.js

const math = require('mathjs');

const expressions = [
    '2 + 3 * sqrt(4) / 2',
    'sin(pi / 4) ^ 2',
    'log(100, 10)'
];
const results = expressions.map(expr => math.evaluate(expr));

expressions.forEach((expr, index) => {
    console.log(`The result of the expression "${expr}" is ${results[index]}`);
});

Output

The result of the expression "2 + 3 * sqrt(4) / 2" is 5
The result of the expression "sin(pi / 4) ^ 2" is 0.4999999999999999
The result of the expression "log(100, 10)" is 2

Using Scope to Evaluate Expressions

In this approach, we are using Math.js to evaluate an expression with predefined variables. By defining a scope object with variable values and passing it to the evaluate method, we can compute the result of the expression a * b + c with the given scope and print the result.

Example: The below example uses scope to evaluate expressions with Math.js.

JavaScript
// script.js

const math = require('mathjs');

const scope = {
    a: 5,
    b: 2,
    c: 3
};
const expression = 'a * b + c';
const result = math.evaluate(expression, scope);

console.log(`The result of the expression "${expression}" 
    with scope ${JSON.stringify(scope)} is ${result}`);

Output:

The result of the expression "a * b + c" with scope {"a":5,"b":2,"c":3} is 13

Next Article

Similar Reads