6

I am trying to use math.js (http://mathjs.org/docs/reference/functions/inv.html) 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

Thanks

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)); }
fred
  • 117
  • 1
  • 1
  • 5

5 Answers5

7

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 mathjs cdn, and include it to your page HEAD. This way you don't need to download anything to your server.


More info about CDN:

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.

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!

Aminadav Glickshtein
  • 23,232
  • 12
  • 77
  • 117
6

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: http://mathjs.org/docs/getting_started.html

Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
victor sosa
  • 899
  • 13
  • 27
3

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

In your head tag:

<script src="math.js" type="text/javascript"></script>
Charlie Fish
  • 18,491
  • 19
  • 86
  • 179
2

Import

import { create, all } from 'mathjs'

init

const math = create(all,  {})

usage

console.log(math.sqrt(-4).toString()) 
Balaji
  • 9,657
  • 5
  • 47
  • 47
2

In the year 2022, you can use

<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjs/9.2.0/math.js'></script>

inside the head tag. Once math.js is loaded, the global symbol math representing it can be used to do many kinds of mathematic computations once considered hard or impossible to perform.

It is possible to demonstrate the use of math.js that can run right here. Let me try to show runnable code on this web page as an answer.

function print (value) {
  const precision = 14
  console.log(math.format(value, precision))
}

print(math.round(math.e, 3))   // 2.718
print(math.atan2(3, -3) / math.pi )
print(math.log(10000, 10))
print(math.sqrt(-4))
print(math.pow([[-1, 2], [3, 1]], 2))     // [[7, 0], [0, 7]]
print(math.evaluate('12 / (2.3 + 0.7)'))
print(math.evaluate('12.7 cm to inch'))    // 5 inch
print(math.evaluate('sin(45 deg) ^ 2'))    // 0.5
print(math.evaluate('det([-1, 2; 3, 1])')) // -7

// matrix inversion
var mat_a = [[1, 2], [3, -4]]
var b1 = [[2], [1]]
var mat_ai = math.inv(mat_a) 
// check the inversion; OK
print( math.multiply(mat_a, mat_ai) )
// get sol A.x=b; x=?
var x = math.multiply(mat_ai, b1)
print( {x} )  // solution x
print( math.multiply(mat_a, x) ) // expect b; OK

// use of linear-eq solve
// Matrix must be a lower triangular matrix if use `lsolve()`
var x2 = math.lusolve(mat_a, b1)
print( {x2} ); // OK

// Use lup(A) instead of A
// This is preferable method
var x3 = math.lusolve(math.lup(mat_a), b1)
print( {x3} ); // OK

// LU decomposition
var m = [[2, 1], [1, 4]]
var r = math.lup(m)
print({r}) //ok {'r': {'L': [[1, 0], [0.5, 1]], 'U': [[2, 1], [0, 3.5]], 'p': [0, 1]}}
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjs/9.2.0/math.js'></script>
swatchai
  • 17,400
  • 3
  • 39
  • 58