1.0.2 • Published 3 years ago

math_engine v1.0.2

Weekly downloads
4
License
ISC
Repository
-
Last release
3 years ago

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: m2,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,a4,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.

DoDon't
q4q4,4

Supported operations

NameUsageNumber of arguments
sumaarg1,arg22
subtractionsarg1,arg22
divisiondarg1,arg22
multiplicationmarg1,arg22
powerparg1,arg22
square-rootqarg1
cosinecarg1
sineiarg1
tangenttarg1
exponentialearg1
logarithm (log10)larg1
binary ANDAarg1
binary OROarg1
binary NOTNarg1

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