UNPKG

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