Softnami
Author: Hussain Mir Ali

I am interested in web and mobile technologies.

If you any questions or feedback then message me at devtips@softnami.com.

Announcements

Call predictor: AI based call prediction

# Using Math.js Library

When building compute heavy applications the need for custom math functions will put significant load on developers. But math.js library promises to ease development by providing essential math operations from everyday uses cases to focused scientific domains.

Basic Operations:

Math.js supports the following basic types:

• Boolean
• Number
• BigNumber
• Complex
• Fraction
• Array
• Matrix
• Unit
• String
`math.round(math.e, 3);            // 2.718math.atan2(3, -3) / math.pi;      // 0.75math.log(1000, 10);              // 3math.sqrt(-4);                    // 2imath.pow([[-1, 2], [3, 1]], 2);     // [[7, 0], [0, 7]]`

Math.js has all the usual functions that any programming language provides by default. In this case basic operations like square root, log, power, number rounding and trigonometric functions

Custom Functions:

The real advantage of using math.js is that it allows developers to write custom functions. So the functions are not limited to certain level of complexity.

`math.eval('(2+3)/4');                // 1.25math.eval('sqrt(3^2 + 4^2)');        // 5math.eval('sqrt(-4)');               // 2imath.eval(['a=3', 'b=4', 'a*b']);,   // [3, 4, 12]let scope = {a:3, b:4};math.eval('a^2 + (2*a*b)+ b^2', scope); // 49`

The 'eval' method parses the string to perform computation. The 'scope' variable can be passed to allow value referencing. The ability to reference values allows developers to build modular functions that can be used with different arguments.

Matrix Operations:

Math.js is very good for matrix manipulation tasks. Implementing explicit functions to do matrix operations would take significant development time. So Math.js eases the development process quite a bit.

`//General matrixmath.matrix();                          // Matrix, size math.matrix([0, 1, 2]);                 // Matrix, size math.matrix([[0, 1, 2], [0, 1, 2]]);    // Matrix, size [2,3]//Matrix with zerosmath.zeros(3, 2);   // Matrix, size [3, 2], [[0, 0], [0, 0], [0, 0]]//Matrix with onesmath.ones(2, 3);    // Matrix, size [2, 3],    [[1, 1, 1], [1, 1, 1]]//Diagonal Matrixmath.eye(2, 3);  // Matrix, size [2, 3], [[1, 0, 0], [0, 1, 0]]//Matrix values within rangemath.range('2:1:6');     //generate matrix from 2 to 6 and increment by 1 [2, 3, 4, 5]//Random valued matrixmath.random([2, 3]); // returns a 2x3 matrix with random numbers between 0 and 1`

The 'matrix' method is useful to generate a new matrix object. Math.js provides various ways to generate a matrix like random valued matrix, diagonal matrix and range matrix.

Element Wise Operation:

`let a = [[9, 5], [6, 1]];let b = [[3, 2], [5, 2]];math.dotMultiply(a, b); // returns [[27, 10], [30, 2]]`

The 'dotMultiply(<matrix1>, <matrix2>)' method allows developers to multiply matrices element wise. Similar to 'multiply' other element wise operations can also be done via 'eval' method.

Dot Product/Cross Product:

`//dot productmath.dot([2, 4, 1], [2, 2, 3]);       // returns number 15//cross productmath.cross([[1, 2, 3]], [, , ]); // returns [[-3, 6, -3]]`

The 'dot' and 'cross' methods allow developers to easily compute dot and cross products of  matrices.

`let  matrix1 = math.matrix([[2, 0], [-1, 3]]);               let  matrix2 = math.matrix([[7, 1], [-2, 3]]);  // perform additionmath.add(matrix1 , matrix2);                      // Matrix, [[9, 1], [-3, 6]]// perform subtractionmath.subtract(matrix1 , matrix2);                      // Matrix, [[-5, -1], [1, 0]]`
`let A = [[1, 2, 3], [4, 5, 6]];math.transpose(A); // returns [[1, 4], [2, 5], [3, 6]]`