UNPKG

3.11 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### ES modules
25
26Load the functions you need and use them:
27
28```js
29import { sqrt } from 'mathjs'
30
31console.log(sqrt(-4).toString()) // 2i
32```
33
34To use lightweight, number only implementations of all functions:
35
36```js
37import { sqrt } from 'mathjs/number'
38
39console.log(sqrt(4).toString()) // 2
40console.log(sqrt(-4).toString()) // NaN
41```
42
43You can create a mathjs instance allowing [configuration](core/configuration.md) and importing of external functions as follows:
44
45```js
46import { create, all } from 'mathjs'
47
48const config = { }
49const math = create(all, config)
50
51console.log(math.sqrt(-4).toString()) // 2i
52```
53
54How to optimize your bundle size using tree-shaking is described on the page
55[Custom bundling](custom_bundling.md).
56
57
58### Node.js
59
60Load math.js in [node.js](https://nodejs.org/) (CommonJS module system):
61
62```js
63const { sqrt } = require('mathjs')
64
65console.log(sqrt(-4).toString()) // 2i
66```
67
68
69### Browser
70
71Math.js can be loaded as a regular JavaScript file in the browser, use the global
72variable `math` to access the libary once loaded:
73
74```html
75<!DOCTYPE HTML>
76<html>
77<head>
78 <script src="math.js" type="text/javascript"></script>
79</head>
80<body>
81 <script type="text/javascript">
82 console.log(math.sqrt(-4).toString()) // 2i
83 </script>
84</body>
85</html>
86```
87
88## Use
89
90Math.js can be used similar to JavaScript's built-in Math library. Besides that,
91math.js can evaluate expressions (see [Expressions](expressions/index.md)) and
92supports chaining (see [Chaining](core/chaining.md)).
93
94The example code below shows how to use math.js. More examples can be found in the
95section [Examples](https://mathjs.org/examples/index.html).
96
97```js
98// functions and constants
99math.round(math.e, 3) // 2.718
100math.atan2(3, -3) / math.pi // 0.75
101math.log(10000, 10) // 4
102math.sqrt(-4) // 2i
103math.pow([[-1, 2], [3, 1]], 2) // [[7, 0], [0, 7]]
104
105// expressions
106math.evaluate('12 / (2.3 + 0.7)') // 4
107math.evaluate('12.7 cm to inch') // 5 inch
108math.evaluate('sin(45 deg) ^ 2') // 0.5
109math.evaluate('9 / 3 + 2i') // 3 + 2i
110math.evaluate('det([-1, 2; 3, 1])') // -7
111
112// chained operations
113math.chain(3)
114 .add(4)
115 .multiply(2)
116 .done() // 14
117```
118
119## Next
120
121To learn more about math.js, check out the available documentation and examples:
122
123- [Documentation](index.md)
124- [Examples](https://mathjs.org/examples/index.html)