UNPKG

5.06 kBMarkdownView Raw
1# Configuration
2
3Math.js contains a number of configuration options.
4These options can be applied on a created mathjs instance and changed afterwards.
5
6```js
7import { create, all } from 'mathjs'
8
9// create a mathjs instance with configuration
10const config = {
11 epsilon: 1e-12,
12 matrix: 'Matrix',
13 number: 'number',
14 precision: 64,
15 predictable: false,
16 randomSeed: null
17}
18const math = create(all, config)
19
20// read the applied configuration
21console.log(math.config())
22
23// change the configuration
24math.config({
25 number: 'BigNumber'
26})
27```
28
29The following configuration options are available:
30
31- `epsilon`. The minimum relative difference used to test equality between two
32 compared values. This value is used by all relational functions.
33 Default value is `1e-12`.
34
35- `matrix`. The default type of matrix output for functions.
36 Available values are: `'Matrix'` (default) or `'Array'`.
37 Where possible, the type of matrix output from functions is determined from
38 the function input: An array as input will return an Array, a Matrix as input
39 will return a Matrix. In case of no matrix as input, the type of output is
40 determined by the option `matrix`. In case of mixed matrix
41 inputs, a matrix will be returned always.
42
43- `number`. The type of numeric output for functions which cannot
44 determine the numeric type from the inputs. For most functions though,
45 the type of output is determined from the the input:
46 a number as input will return a number as output,
47 a BigNumber as input returns a BigNumber as output.
48
49 For example the functions `math.evaluate('2+3')`, `math.parse('2+3')`,
50 `math.range('1:10')`, and `math.unit('5cm')` use the `number` configuration
51 setting. But `math.sqrt(4)` will always return the number `2`
52 regardless of the `number` configuration, because the input is a number.
53
54 Available values are: `'number'` (default), `'BigNumber'`, or `'Fraction'`.
55 [BigNumbers](../datatypes/bignumbers.js) have higher precision than the default
56 numbers of JavaScript, and [`Fractions`](../datatypes/fractions.js) store
57 values in terms of a numerator and denominator.
58
59- `precision`. The maximum number of significant digits for BigNumbers.
60 This setting only applies to BigNumbers, not to numbers.
61 Default value is `64`.
62
63- `predictable`. Predictable output type of functions. When true, output type
64 depends only on the input types. When false (default), output type can vary
65 depending on input values. For example `math.sqrt(-4)` returns `complex('2i')` when
66 predictable is false, and returns `NaN` when true.
67 Predictable output can be needed when programmatically handling the result of
68 a calculation, but can be inconvenient for users when evaluating dynamic
69 equations.
70
71- `randomSeed`. Set this option to seed pseudo random number generation, making it deterministic. The pseudo random number generator is reset with the seed provided each time this option is set. For example, setting it to `'a'` will cause `math.random()` to return `0.43449421599986604` upon the first call after setting the option every time. Set to `null` to seed the pseudo random number generator with a random seed. Default value is `null`.
72
73
74## Examples
75
76This section shows a number of configuration examples.
77
78### node.js
79
80```js
81import { create, all } from 'mathjs'
82
83const config = {
84 matrix: 'Array' // Choose 'Matrix' (default) or 'Array'
85}
86const math = create(all, config)
87
88// range will output an Array
89math.range(0, 4) // Array [0, 1, 2, 3]
90
91// change the configuration from Arrays to Matrices
92math.config({
93 matrix: 'Matrix' // Choose 'Matrix' (default) or 'Array'
94})
95
96// range will output a Matrix
97math.range(0, 4) // Matrix [0, 1, 2, 3]
98
99// create an instance of math.js with BigNumber configuration
100const bigmath = create(all, {
101 number: 'BigNumber', // Choose 'number' (default), 'BigNumber', or 'Fraction'
102 precision: 32 // 64 by default, only applicable for BigNumbers
103})
104
105// parser will parse numbers as BigNumber now:
106bigmath.evaluate('1 / 3') // BigNumber, 0.33333333333333333333333333333333
107```
108
109### browser
110
111
112```html
113<!DOCTYPE HTML>
114<html>
115<head>
116 <script src="math.js" type="text/javascript"></script>
117</head>
118<body>
119 <script type="text/javascript">
120 // the default instance of math.js is available as 'math'
121
122 // range will output a Matrix
123 math.range(0, 4) // Matrix [0, 1, 2, 3]
124
125 // change the configuration of math from Matrices to Arrays
126 math.config({
127 matrix: 'Array' // Choose 'Matrix' (default) or 'Array'
128 })
129
130 // range will output an Array
131 math.range(0, 4) // Array [0, 1, 2, 3]
132
133 // create a new instance of math.js with bignumber configuration
134 const bigmath = math.create({
135 number: 'BigNumber', // Choose 'number' (default), 'BigNumber', or 'Fraction'
136 precision: 32 // 64 by default, only applicable for BigNumbers
137 })
138
139 // parser will parse numbers as BigNumber now:
140 bigmath.evaluate('1 / 3') // BigNumber, 0.33333333333333333333333333333333
141 </script>
142</body>
143</html>
144```