simple-squiggle

A restricted subset of Squiggle
Log | Files | Refs | README

getting_started.md (3106B)


      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 ### ES modules
     25 
     26 Load the functions you need and use them:
     27 
     28 ```js
     29 import { sqrt } from 'mathjs'
     30 
     31 console.log(sqrt(-4).toString()) // 2i
     32 ```
     33 
     34 To use lightweight, number only implementations of all functions:
     35 
     36 ```js
     37 import { sqrt } from 'mathjs/number'
     38 
     39 console.log(sqrt(4).toString()) // 2
     40 console.log(sqrt(-4).toString()) // NaN
     41 ```
     42 
     43 You can create a mathjs instance allowing [configuration](core/configuration.md) and importing of external functions as follows:
     44 
     45 ```js
     46 import { create, all } from 'mathjs'
     47 
     48 const config = { }
     49 const math = create(all, config)
     50 
     51 console.log(math.sqrt(-4).toString()) // 2i
     52 ```
     53 
     54 How 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 
     60 Load math.js in [node.js](https://nodejs.org/) (CommonJS module system):
     61 
     62 ```js
     63 const { sqrt } = require('mathjs')
     64 
     65 console.log(sqrt(-4).toString()) // 2i
     66 ```
     67 
     68 
     69 ### Browser
     70 
     71 Math.js can be loaded as a regular JavaScript file in the browser, use the global
     72 variable `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 
     90 Math.js can be used similar to JavaScript's built-in Math library. Besides that,
     91 math.js can evaluate expressions (see [Expressions](expressions/index.md)) and
     92 supports chaining (see [Chaining](core/chaining.md)).
     93 
     94 The example code below shows how to use math.js. More examples can be found in the
     95 section [Examples](https://mathjs.org/examples/index.html).
     96 
     97 ```js
     98 // functions and constants
     99 math.round(math.e, 3)                // 2.718
    100 math.atan2(3, -3) / math.pi          // 0.75
    101 math.log(10000, 10)                  // 4
    102 math.sqrt(-4)                        // 2i
    103 math.pow([[-1, 2], [3, 1]], 2)       // [[7, 0], [0, 7]]
    104 
    105 // expressions
    106 math.evaluate('12 / (2.3 + 0.7)')    // 4
    107 math.evaluate('12.7 cm to inch')     // 5 inch
    108 math.evaluate('sin(45 deg) ^ 2')     // 0.5
    109 math.evaluate('9 / 3 + 2i')          // 3 + 2i
    110 math.evaluate('det([-1, 2; 3, 1])')  // -7
    111 
    112 // chained operations
    113 math.chain(3)
    114     .add(4)
    115     .multiply(2)
    116     .done() // 14
    117 ```
    118 
    119 ## Next
    120 
    121 To 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)