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