math_engine

1.0.2 • Public • Published

Math engine guide

This package can be installed in every node project as well as react-native-cli and expo-cli. You can use this simple script to parse and get the result, not only the math result but also the steps trace,of string expressions.

Waring

Note that it is not going to work with the usual math writing e.g. 2+2-1, but with a special formatting (that is described below) where spaces are not allowed.

Installation

It's very easy to download, just use the following command.

npm install math_engine

Syntax

As said before, it needs a special syntax, but luckily it is very simple to get used to it.
An example of expression can be: m[2,2]. Where the first letter m stays for multiplication, and the two numbers inside the brackets are the arguments of the operation.
Just follow the rule (without spaces):
operation id + [ + arg1 + , +arg2 +]
Of course you can create multiple nested operations like:
m[2,a[4,2]] is the equivalent of: 2 × (4 + 2) = 12.

Warning

Not all the operations takes two arguments. For example sine, cosine, square-root and others must contain only one number inside the brackets. For example below is reported a small table where you can find the square-root operation.

Do Don't
q[4] q[4,4]

Supported operations

Name Usage Number of arguments
sum a[arg1,arg2] 2
subtraction s[arg1,arg2] 2
division d[arg1,arg2] 2
multiplication m[arg1,arg2] 2
power p[arg1,arg2] 2
square-root q[arg] 1
cosine c[arg] 1
sine i[arg] 1
tangent t[arg] 1
exponential e[arg] 1
logarithm (log10) l[arg] 1
binary AND A[arg] 1
binary OR O[arg] 1
binary NOT N[arg] 1

Using variables

This simple script allows you to use variables inside your expression. You must specify your variables inside the expression by adding 'x' + index.

const expression = 'm[x1,x2]';
const variables = [20,30]; // result => 20 × 30 => 600

Warning

The count for the index is starting from 1 and not from 0 (as usual in arrays)

Usage (javascript)

The javascript code is very simple, first you need to import the package by using:

import MathEngine from "math_engine";

Then define the expression to evaluate, and the variables (if they are necessary):

const expression = 'm[q[100],x1]';
const variables = [20]; // must be an array (can also be empty [] if you are not using variables);

Now, create the engine object by passing the expression, the variables array, and the language that is needed if you want to get all the math steps ( can only be 'it', 'en'). If it is empty, the script will use 'en' as default:

let engine = new MathEngine(expression, variables, 'en');

Now, (if you are using variables) call replace() on the engine object to replace the 'x1' (x2 , x3 ,x4...) with the corresponding variable (20):

engine.replace(); // this is not needed if you are not using variables. expression = 'm[q[100],20]'

Last step, call evaluate() (this is an async function that needs .then()) on the engine object to get an object that contains the result and the steps.

engine.evaluate().then((res) => {console.log(res.result); console.log(res.stepsTrace)}).catch((error) => {console.error(error)});

Full code

import MathEngine from "math_engine";
const expression = 'm[q[100],x1]';
const variables = [20]; // must be an array (can also be empty [] if you are not using variables);
let engine = new MathEngine(expression, variables, 'en');
engine.replace(); // this is not needed if you are not using variables. expression = 'm[q[100],20]'
engine.evaluate().then((res) => {console.log(res.result); console.log(res.stepsTrace)}).catch((error) => {console.error(error)});

Contact me

If you have any questions or suggestions please contact me at: francesco.podesta03@gmail.com

Readme

Keywords

none

Package Sidebar

Install

npm i math_engine

Weekly Downloads

2

Version

1.0.2

License

ISC

Unpacked Size

14.5 kB

Total Files

10

Last publish

Collaborators

  • francesco_pod