BT

Math.js: Versatile Math Library For JavaScript

by Roopesh Shenoy on Jan 20, 2014 |

 Math.js is an open source Math library for JavaScript and Node.js for working with numbers, big numbers, complex numbers, units and matrices. It also features a flexible expression parser. InfoQ got in touch with the project founder, Jos De Jong, to know more.

Jos explains the motivation behind the project –

For JavaScript there are nice libraries out there to work with matrices, complex numbers, statistics, etc. What is lacking though is an integrated solution to do advanced mathematics. Most of the existing libraries come with a chained API, which works very intuitive, but only accepts data types known by the library itself. Hence the integration issue: matrix libraries can't handle complex numbers, and vice versa. You can't combine them. The API of math.js is the same as that of JavaScript's Math object and built in operators: static functions accepting various types of input. Math.js extends this API with support for advanced data types, functions, and constants.

I hope math.js will help making maths in application development trivial and more fun, and helps bridging the gap between the the world of developers and the academic world.

Math.js can be used in three ways –

  1. Using static functions and constants (like JavaScript's Math object)
    math.add(2, 3);                  // 5 
    math.sqrt(-4);                   // 2i 
    math.pow([[-1, 2], [3, 1]], 2);  // [[7, 0], [0, 7]]
  2. Evaluate string expressions
    math.eval('1.2 * (2 +  4.5)'); //7.8
    math.eval('5.08 cm to inch'); //2 inch 
  3. Using chaining operations
    math.select(3)
        .add(4)
        .multiply(2)
        .done();      //14

There are several examples on the math.js website as well as in the documentation

Jos is hoping to reach the first stable version 1.0 within a couple of months.

What's left before 1.0 is writing a reference documentation, reaching 100% code coverage with the unit tests and resolving some rough edges here and there.

1.0 will also see the API getting stabilized, after which the focus might shift to optimization. Jos identifies several possibilities such as using typed arrays, parallelization, asm.js for potential performance improvements.

On why Jos chose JavaScript instead of something like Python which already has libraries such as NumPi/SciPi -

Latest years we have seen an enormous push towards the cloud and web applications, and I'm fully into this myself too. Browsers and JavaScript engines are becoming faster and faster. Since a couple of years you can run JavaScript server side using node.js. This opens up a lot of new possibilities, which has resulted in an explosion of the JavaScript ecosystem and its popularity. It looks like JavaScript is becoming the most ubiquitous language ever. JavaScript is far from the perfect language, but I like JavaScript and its community a lot.

There are already several end-user projects using math.js. Jos’s own project, mathnotepad is powered by math.js and in early stages of development. Math.js is also used by numerics, a popular calculator project. 

To get started with Math.js, head over to the documentation

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT