UNPKG

2.88 kBMarkdownView Raw
1# Getting Started
2
3This getting started describes how to install, load, and use math.js.
4
5
6## Install
7
8Math.js can be installed using various package managers like [npm](https://npmjs.org/), or by just downloading the library from the website: [https://mathjs.org/download.html](https://mathjs.org/download.html).
9
10To install via npm, run:
11
12 npm install mathjs
13
14Other ways to install math.js are described on the [website](https://mathjs.org/download.html).
15
16
17## Load
18
19Math.js can be used in node.js and in the browser. The library must be loaded
20and instantiated. When creating an instance, one can optionally provide
21configuration options as described in
22[Configuration](core/configuration.md).
23
24### ES6 modules
25
26Load math.js using ES6 import:
27
28```js
29// load math.js
30import * as math from 'mathjs'
31
32// use math.js
33math.sqrt(-4) // 2i
34```
35
36### Node.js
37
38Load math.js in [node.js](https://nodejs.org/):
39
40```js
41// load math.js
42const math = require('mathjs')
43
44// use math.js
45math.sqrt(-4) // 2i
46```
47
48
49### Browser
50
51Math.js can be loaded as a regular JavaScript file in the browser, use the global
52variable `math` to access the libary once loaded:
53
54```html
55<!DOCTYPE HTML>
56<html>
57<head>
58 <script src="math.js" type="text/javascript"></script>
59</head>
60<body>
61 <script type="text/javascript">
62 // use the math.js libary
63 math.sqrt(-4) // 2i
64 </script>
65</body>
66</html>
67```
68
69If support for old browsers (Internet Explorer 8 and older) is required,
70the [es5-shim](https://github.com/kriskowal/es5-shim) library must be loaded
71as well.
72
73
74### Require.js
75
76Load math.js in the browser using [require.js](https://requirejs.org/):
77
78```js
79require.config({
80 paths: {
81 mathjs: 'path/to/mathjs',
82 }
83})
84require(['mathjs'], function (math) {
85 // use math.js
86 math.sqrt(-4) // 2i
87})
88```
89
90## Use
91
92Math.js can be used similar to JavaScript's built-in Math library. Besides that,
93math.js can evaluate expressions (see [Expressions](expressions/index.md)) and
94supports chaining (see [Chaining](core/chaining.md)).
95
96The example code below shows how to use math.js. More examples can be found in the
97section [Examples](https://mathjs.org/examples/index.html).
98
99```js
100// functions and constants
101math.round(math.e, 3) // 2.718
102math.atan2(3, -3) / math.pi // 0.75
103math.log(10000, 10) // 4
104math.sqrt(-4) // 2i
105math.pow([[-1, 2], [3, 1]], 2) // [[7, 0], [0, 7]]
106
107// expressions
108math.eval('12 / (2.3 + 0.7)') // 4
109math.eval('12.7 cm to inch') // 5 inch
110math.eval('sin(45 deg) ^ 2') // 0.5
111math.eval('9 / 3 + 2i') // 3 + 2i
112math.eval('det([-1, 2; 3, 1])') // -7
113
114// chained operations
115math.chain(3)
116 .add(4)
117 .multiply(2)
118 .done() // 14
119```
120
121## Next
122
123To learn more about math.js, check out the available documentation and examples:
124
125- [Documentation](index.md)
126- [Examples](https://mathjs.org/examples/index.html)