how to include and use math.js


I am trying to use math.js (<a href="http://mathjs.org/docs/reference/functions/inv.html" rel="nofollow">http://mathjs.org/docs/reference/functions/inv.html</a>) but I can't see how to include it in an HTML file.

I have read that you can include it by adding this to the file:

var math = require('mathjs');

but that didn't work for me

I realize this sounds like a really stupid question but I have tried to figure it out myself and have not been successful.

To answer my question just show a complete HTML file (not just a bit of code from one) that successfully uses math.js


I tried what Alexander O'Mara suggested and I got this:

/** * Math.js can easily be extended with functions and variables using the * `import` function. The function `import` accepts a module name or an object * containing functions and variables. */ // load math.js (using node.js) var math = require('../index'); /** * Define new functions and variables */ math.import({ myConstant: 42, hello: function (name) { return 'hello, ' + name + '!'; } }); // defined methods can be used in both JavaScript as well as the parser print(math.myConstant * 2); // 84 print(math.hello('user')); // 'hello, user!' print(math.eval('myConstant + 10')); // 52 print(math.eval('hello("user")')); // 'hello, user!' /** * Import the math library numbers.js, https://github.com/sjkaliski/numbers.js * The library must be installed first using npm: * npm install numbers */ try { // load the numbers.js library var numbers = require('numbers'); // import the numbers.js library into math.js math.import(numbers, {wrap: true, silent: true}); if (math.fibonacci) { // calculate fibonacci print(math.fibonacci(7)); // 13 print(math.eval('fibonacci(7)')); // 13 } } catch (err) { console.log('Warning: To import numbers.js, the library must ' + 'be installed first via `npm install numbers`.'); } /** * Import the math library numeric.js, http://numericjs.com/ * The library must be installed first using npm: * npm install numeric */ try { // load the numeric.js library var numeric = require('numeric'); // import the numeric.js library into math.js math.import(numeric, {wrap: true, silent: true}); if (math.eig) { // calculate eigenvalues of a matrix print(math.eval('eig([1, 2; 4, 3])').lambda.x); // [5, -1]; // solve AX = b var A = math.eval('[1, 2, 3; 2, -1, 1; 3, 0, -1]'); var b = [9, 8, 3]; print(math.solve(A, b)); // [2, -1, 3] } } catch (err) { console.log('Warning: To import numeric.js, the library must ' + 'be installed first via `npm install numeric`.'); } /** * By default, the function import does not allow overriding existing functions. * Existing functions can be overridden by specifying option `override: true` */ math.import({ pi: 3.14 }, { override: true }); print(math.pi); // returns 3.14 instead of 3.141592653589793 /** * Helper function to output a value in the console. Value will be formatted. * @param {*} value */ function print (value) { var precision = 14; console.log(math.format(value, precision)); }


You shouldn't use require in the browser. You should try something like this:

<!DOCTYPE HTML> <html> <head> <script src="math.js" type="text/javascript"></script> </head> <body> <script type="text/javascript"> // use math.js math.sqrt(-4); // 2i </script> </body> </html>

check the docs: <a href="http://mathjs.org/docs/getting_started.html" rel="nofollow">http://mathjs.org/docs/getting_started.html</a>


They have a restful API you can use, <a href="http://api.mathjs.org" rel="nofollow">http://api.mathjs.org</a>. OR you should be able to just download the production from <a href="http://mathjs.org/download.html" rel="nofollow">here</a> and include that in your JS.

In your head tag:

<script src="math.js" type="text/javascript"></script>


The easiest way to you is to include math.js from a CDN:

<head> <script src=https://cdnjs.cloudflare.com/ajax/libs/mathjs/3.3.0/math.min.js></script> </head>

Every script you need, search in Google for the CDN (in this example <a href="http://www.google.com/search?q=mathjs+cdn" rel="nofollow">mathjs cdn</a>, and include it to your page HEAD. This way you don't need to download anything to your server.

<hr />

More info about CDN:

<ul><li><a href="https://developers.google.com/speed/libraries/" rel="nofollow">Google CDN</a></li> </ul><blockquote>

The Google Hosted Libraries is a stable, reliable, high-speed, globally available content distribution network for the most popular, open-source JavaScript libraries.

Google works directly with the key stakeholders for each library effort and accepts the latest versions as they are released.

</blockquote> <ul><li><a href="https://cdnjs.com/" rel="nofollow">cdnjs.com</a></li> </ul><blockquote>

Everyone loves the Google CDN right? Even Microsoft runs their own CDN. The problem is, they only host the most popular libraries. We host all the popular libraries - JavaScript, CSS, SWF, images, etc! Join us now on GitHub!



  • Kentico Repeater HTML Properties showing with selected transformation
  • How to change display text in item template based on eval in gridview?
  • How are 32 bit JavaScript numbers resulting from a bit-wise operation converted back to 64 bit numbe
  • Cassandra: What is a subcolumn
  • SQL append distinct values from two columns and two tables
  • Need a consistent TimePicker gizmo for mobile web site
  • Divide a $1 by 3 and adjusting 1 cent
  • Is it possible to run clang with llc flags
  • Validate jQuery plugin, field not required
  • In-place sed command not working
  • Adding elements to a huge XML file
  • How to generate and display a QR Code in ionic 2
  • Access variable of ScriptContext using Nashorn JavaScript Engine (Java 8)
  • How integrated is Collada to OpenGL ES
  • Jackson Parser: ignore deserializing for type mismatch
  • Checking free space on FTP server
  • Can I check if a recipient has an automatic reply before I send an email?
  • DomPDF {PAGE_NUM} not on first page
  • Splitting given String into two variables - php
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • Cross-Platform Protobuf Serialization
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Shallow update not allowed (git > 1.9)
  • Build own AppleScript numerical error handling
  • Websockets service method fails during R startup
  • Why winpcap requires both .lib and .dll to run?
  • align graphs with different xlab
  • Return words with double consecutive letters
  • How do I rollback to a specific git commit
  • Python: how to group similar lists together in a list of lists?
  • Revoking OAuth Access Token Results in 404 Not Found
  • Turn off referential integrity in Derby? is it possible?
  • Authorize attributes not working in MVC 4
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Busy indicator not showing up in wpf window [duplicate]
  • reshape alternating columns in less time and using less memory
  • Reading document lines to the user (python)
  • Python/Django TangoWithDjango Models and Databases
  • Net Present Value in Excel for Grouped Recurring CF