Sunday, 10 September 2017

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.718
math.atan2(3, -3) / math.pi;      // 0.75
math.log(1000, 10);              // 3
math.sqrt(-4);                    // 2i
math.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.25
math.eval('sqrt(3^2 + 4^2)');        // 5
math.eval('sqrt(-4)');               // 2i
math.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 matrix
math.matrix();                          // Matrix, size [0]
math.matrix([0, 1, 2]);                 // Matrix, size [3]
math.matrix([[0, 1, 2], [0, 1, 2]]);    // Matrix, size [2,3]

//Matrix with zeros
math.zeros(3, 2);   // Matrix, size [3, 2], [[0, 0], [0, 0], [0, 0]]

//Matrix with ones
math.ones(2, 3);    // Matrix, size [2, 3],    [[1, 1, 1], [1, 1, 1]]

//Diagonal Matrix
math.eye(2, 3);  // Matrix, size [2, 3], [[1, 0, 0], [0, 1, 0]]

//Matrix values within range
math.range('2:1:6');     //generate matrix from 2 to 6 and increment by 1 [2, 3, 4, 5]

//Random valued matrix
math.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 product
math.dot([2, 4, 1], [2, 2, 3]);       // returns number 15

//cross product
math.cross([[1, 2, 3]], [[4], [5], [6]]); // returns [[-3, 6, -3]]

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

Add/Subtract:

let  matrix1 = math.matrix([[2, 0], [-1, 3]]);               
let  matrix2 = math.matrix([[7, 1], [-2, 3]]);  

// perform addition
math.add(matrix1 , matrix2);                      // Matrix, [[9, 1], [-3, 6]]

// perform subtraction
math.subtract(matrix1 , matrix2);                      // Matrix, [[-5, -1], [1, 0]]

The 'add' and 'subtract' methods make it easy to subtract high dimensional matrices.


Transpose:

let A = [[1, 2, 3], [4, 5, 6]];
math.transpose(A); // returns [[1, 4], [2, 5], [3, 6]]

The transpose of a matrix can be achieved through the 'transpose(<matrix>)' method where any matrix object can be passed as an argument.


No comments:

Post a Comment