Skip to content

Commit 6419cb2

Browse files
committed
refactor: Adopt new config options organization
1 parent b8fd428 commit 6419cb2

File tree

185 files changed

+1616
-1013
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

185 files changed

+1616
-1013
lines changed

docs/core/configuration.md

Lines changed: 119 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,124 @@
11
# Configuration
22

3-
Math.js contains a number of configuration options.
4-
These options can be applied on a created mathjs instance and changed afterwards.
3+
Math.js contains a number of configuration options. These options can be
4+
applied on a created mathjs instance and changed afterwards.
55

66
```js
77
import { create, all } from 'mathjs'
88

99
// create a mathjs instance with configuration
1010
const config = {
11-
relTol: 1e-12,
12-
absTol: 1e-15,
13-
matrix: 'Matrix',
1411
number: 'number',
15-
numberFallback: 'number',
16-
precision: 64,
17-
predictable: false,
18-
randomSeed: null,
19-
legacySubset: false
12+
compute: {
13+
defaultRelTol: 1e-12,
14+
defaultAbsTol: 1e-15,
15+
numberApproximate: 'number',
16+
randomSeed: null,
17+
uniformType: false,
18+
Matrix: {
19+
defaultType: 'Matrix'
20+
},
21+
BigNumber: {
22+
precision: 64,
23+
},
24+
},
25+
parse: {
26+
numberFallback: 'number',
27+
},
28+
compatibility: {
29+
subset: false
30+
}
2031
}
2132
const math = create(all, config)
2233

2334
// read the applied configuration
2435
console.log(math.config())
2536

2637
// change the configuration
27-
math.config({
28-
number: 'BigNumber'
29-
})
38+
math.config({ number: 'BigNumber' })
3039
```
3140

32-
The following configuration options are available:
33-
34-
- `relTol`. The minimum relative difference used to test equality between two
35-
compared values. This value is used by all relational functions.
36-
Default value is `1e-12`.
37-
38-
- `absTol`. The minimum absolute difference used to test equality between two
39-
compared values. This value is used by all relational functions.
40-
Default value is `1e-15`.
41-
42-
- `matrix`. The default type of matrix output for functions.
43-
Available values are: `'Matrix'` (default) or `'Array'`.
44-
Where possible, the type of matrix output from functions is determined from
45-
the function input: An array as input will return an Array, a Matrix as input
46-
will return a Matrix. In case of no matrix as input, the type of output is
47-
determined by the option `matrix`. In case of mixed matrix
48-
inputs, a matrix will be returned always.
49-
50-
- `number`. The type used to parse strings into a numeric value or create a new
51-
numeric value internally.
52-
53-
For most functions, the type of output is determined from the input:
54-
a number as input will return a number as output, a BigNumber as input
55-
returns a BigNumber as output. But for example the functions
56-
`math.evaluate('2+3')`, `math.parse('2+3')`, `math.range('1:10')`,
57-
and `math.unit('5cm')` use the `number` configuration setting.
58-
59-
Note that `math.sqrt(4)` will always return the number `2` regardless of
60-
the `number` configuration, because the numeric type can be determined from
61-
the input value.
62-
63-
Available values are: `'number'` (default), `'BigNumber'`, `'bigint'`, or `'Fraction'`.
64-
[BigNumbers](../datatypes/bignumbers.md) have higher precision than the default numbers of JavaScript,
65-
[bigint](../datatypes/bigints.md) can represent large integer numbers,
66-
and [`Fractions`](../datatypes/fractions.md) store values in terms of a numerator and
67-
denominator.
68-
69-
- `numberFallback`. When `number` is configured for example with value `'bigint'`,
70-
and a value cannot be represented as `bigint` like in `math.evaluate('2.3')`,
71-
the value will be parsed in the type configured with `numberFallback`.
72-
Available values: `'number'` (default) or `'BigNumber'`.
73-
74-
- `precision`. The maximum number of significant digits for BigNumbers.
75-
This setting only applies to BigNumbers, not to numbers.
76-
Default value is `64`.
77-
78-
- `predictable`. Predictable output type of functions. When true, output type
79-
depends only on the input types. When false (default), output type can vary
80-
depending on input values. For example `math.sqrt(-4)` returns `complex('2i')` when
81-
predictable is false, and returns `NaN` when true.
82-
Predictable output can be needed when programmatically handling the result of
83-
a calculation, but can be inconvenient for users when evaluating dynamic
84-
equations.
85-
86-
- `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`.
87-
88-
- `legacySubset`. When set to `true`, the `subset` function behaves as in earlier versions of math.js: retrieving a subset where the index size contains only one element, returns the value itself. When set to `false` (default), `subset` eliminates dimensions from the result only if the index dimension is a scalar; if the index dimension is a range, matrix, or array, the dimensions are preserved. This option is helpful for maintaining compatibility with legacy code that depends on the previous behavior.
89-
41+
The following configuration options and sub-options are available.
42+
43+
- {string} `number`. When the type of a numeric value is not otherwise
44+
specifically determined, either in parsing or in computation, the type
45+
given by this option is tried first. For example, when set to `Fraction`,
46+
`math.evaluate('0.15 + 02')` will return the exact rational number
47+
`math.fraction(7, 20)` and `math.sum([])` will return `math.fraction(0)`.
48+
Note there are separate backup options for parsing (`parse.numberFallback`)
49+
and computation (`compute.numberApproximate`) when this `number` type is
50+
not appropriate, see below. The currently supported values for this
51+
option are `number` (the default), `BigNumber` (which can support higher
52+
precision than the built-in JavaScript number type), `Fraction`, `bigint`
53+
(which can represent arbitrarily large integer numbers, but not
54+
fractions or decimals).
55+
56+
- compute:
57+
- {number} `defaultAbsTol`. The minimum absolute difference used to test
58+
equality between two compared values of floating-point (inexact) types.
59+
This value is used by all relational functions. Default value is `1e-15`.
60+
61+
- {number} `defaultRelTol`. The minimum relative difference used to test
62+
equality between two compared values of floating-point (inexact) types.
63+
This value is used by all relational functions. Default value is `1e-12`.
64+
65+
- {string} `numberApproximate`. The type to use for floating-point
66+
(approximate real) values as output when the type is not uniquely
67+
determined by the input types (e.g., the square root of a bigint or the
68+
value of a mathematical constant). Default value is `number`.
69+
70+
- {string|null} `randomSeed`. Set this option to seed pseudo-random number
71+
generation, making it deterministic. The pseudo-random number generator is
72+
reset with the seed provided each time this option is set to a new value.
73+
For example, setting it to `'a'` will always cause `math.random()` to
74+
return `0.43449421599986604` on its next call. Set to `null` to seed the
75+
pseudo random number generator with a random seed. Default value is `null`.
76+
77+
- {boolean} `uniformType`. When this option is true, the output type of any
78+
function depends only on the input types. When false, the output type can
79+
vary depending on input values within the same type. For example,
80+
`math.sqrt(-4)` returns `math.complex('2i')` when this option is false,
81+
and returns `NaN` when true. Predictable output can be needed when
82+
programmatically handling the result of a calculation, but can be
83+
inconvenient for users when evaluating dynamic equations. Default
84+
value is `false`.
85+
86+
- BigNumber:
87+
- {number} `precision`. The maximum number of significant digits kept
88+
for values of BigNumber type. Note that increasing it will not affect
89+
previously computed BigNumbers, but will retain the larger number
90+
of digits for future computations. Default value is `64`.
91+
92+
- Matrix:
93+
- {string} `defaultType`. The data type used to represent a matrix output
94+
by any function, when that type is not uniquely determined by the input
95+
types. Whenever possible, the type of matrix output from functions is
96+
determined from the function input: An array as input will return an
97+
Array and a Matrix as input will return a Matrix (which is also returned
98+
in case of mixed-type matrix inputs). However, some functions, such as
99+
`math.identity(n)`, have no matrix parameter. In such cases, the type
100+
of the output is determined by this option, including in the case of
101+
the `[...]` matrix-creating operator in mathjs expressions. Currently
102+
supported types are `Array` and `Matrix`, the default.
103+
104+
- parse:
105+
- {string} `numberFallback`. When the top-level `number` option is
106+
configured, for example, with value `'bigint'`, and a literal value in
107+
an expression, such as `'2.3'`, cannot be represented as that type,
108+
the value will instead be parsed into the type specified by
109+
`numberFallback`. The currently supported values are `BigNumber`,
110+
`Fraction`, and `number`, the default.
111+
112+
- compatibility:
113+
- {boolean} `subset`. When set to `true`, the `subset` function behaves
114+
as in earlier versions of math.js: retrieving a subset where the index
115+
size contains only one element, returns the value itself. When set to
116+
`false`, `subset` eliminates dimensions from the result only if the
117+
index dimension is a scalar; if the index dimension is a range, matrix,
118+
or array, the dimensions are preserved. This option is helpful for
119+
maintaining compatibility with legacy code that depends on the
120+
previous behavior. It is, however, deprecated and will be removed in some
121+
future version of mathjs.
90122

91123
## Examples
92124

@@ -98,25 +130,27 @@ This section shows a number of configuration examples.
98130
import { create, all } from 'mathjs'
99131

100132
const config = {
101-
matrix: 'Array' // Choose 'Matrix' (default) or 'Array'
133+
compute: {
134+
Matrix: { defaultType: 'Array' } // Choose 'Matrix' (default) or 'Array'
135+
}
102136
}
103137
const math = create(all, config)
104138

105139
// range will output an Array
106140
math.range(0, 4) // Array [0, 1, 2, 3]
107141

108142
// change the configuration from Arrays to Matrices
109-
math.config({
110-
matrix: 'Matrix' // Choose 'Matrix' (default) or 'Array'
111-
})
143+
math.config({ compute: { Matrix: { defaultType: 'Matrix' } } })
112144

113145
// range will output a Matrix
114146
math.range(0, 4) // Matrix [0, 1, 2, 3]
115147

116148
// create an instance of math.js with BigNumber configuration
117149
const bigmath = create(all, {
118-
number: 'BigNumber', // Choose 'number' (default), 'BigNumber', or 'Fraction'
119-
precision: 32 // 64 by default, only applicable for BigNumbers
150+
number: 'BigNumber'
151+
compute: {
152+
BigNumber: { precision: 32 } // Would be 64 by default
153+
}
120154
})
121155

122156
// parser will parse numbers as BigNumber now:
@@ -141,16 +175,20 @@ bigmath.evaluate('1 / 3') // BigNumber, 0.33333333333333333333333333333333
141175
142176
// change the configuration of math from Matrices to Arrays
143177
math.config({
144-
matrix: 'Array' // Choose 'Matrix' (default) or 'Array'
178+
compute: {
179+
Matrix: { defaultType: 'Array' }
180+
}
145181
})
146182
147183
// range will output an Array
148184
math.range(0, 4) // Array [0, 1, 2, 3]
149185
150186
// create a new instance of math.js with bignumber configuration
151-
const bigmath = math.create({
152-
number: 'BigNumber', // Choose 'number' (default), 'BigNumber', or 'Fraction'
153-
precision: 32 // 64 by default, only applicable for BigNumbers
187+
const bigmath = create(all, {
188+
number: 'BigNumber'
189+
compute: {
190+
BigNumber: { precision: 32 } // Would be 64 by default
191+
}
154192
})
155193
156194
// parser will parse numbers as BigNumber now:

docs/core/extension.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ where:
219219
- `recreateOnConfigChange: boolean`. If true, the imported factory will be
220220
created again when there is a change in the configuration. This is for
221221
example used for the constants like `pi`, which is different depending
222-
on the configsetting `number` which can be numbers or BigNumbers.
222+
on the config option `compute.numberApproximate` which can be `number`
223+
or `BigNumber`.
223224
- `formerly: string`. If present, the created function will also be
224225
accessible on the instance under the name given by the value of
225226
`formerly` as a (deprecated) synonym for the specified `name`. This

docs/datatypes/bigints.md

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,32 @@ math.bigint('42')
1313
```
1414

1515
Most functions can determine the type of output from the type of input:
16-
a `number` as input will return a `number` as output, a `bigint` as input returns
17-
a `bigint` as output. Functions which cannot determine the type of output
18-
from the input (for example `math.evaluate`) use the default number type `number`,
19-
which can be configured when instantiating math.js. To configure the use of
20-
`bigint` instead of [numbers](numbers.md) by default, configure math.js like:
16+
a `number` as input will return a `number` as output, a `bigint` as input
17+
returns a `bigint` as output, etc. However, when parsing a numeric literal
18+
like `'23'` or when computing the sum of an empty list of numbers, there is
19+
not any already-typed input to go by. The numeric type mathjs will try in such
20+
situations can be configured when instantiating math.js. To use `bigint`
21+
instead of [numbers](numbers.md),
2122

2223
```js
23-
math.config({
24-
number: 'bigint'
25-
})
24+
import { create, all } from 'mathjs'
25+
const math = create(all)
26+
27+
math.config({ number: 'bigint' })
2628

2729
// use math
2830
math.evaluate('70000000000000000123') // bigint 70000000000000000123n
2931
```
3032

33+
You may notice, however, in this setup the parser might encounter a non-integer
34+
literal like `2.5`, that can't be represented by a bigint. In such cases where
35+
the type specified by the top-level `number` configuration option is not
36+
appropriate, backup types can also be configured: the parser will use the
37+
`parse.numberFallback` option, that can be `number`, `BigNumber`, or
38+
`Fraction`; and computations (and similar items, like the values of physical
39+
constants) will use the `compute.numberApproximate` option, that can be
40+
`number` or `BigNumber`.
41+
3142
## Support
3243

3344
All basic arithmetic functions in math.js support `bigint`. Since `bigint` can only hold integer values, it is not applicable to for example trigonometric functions. When using a `bigint` in a function that does not support it, like `sqrt`, it will convert the `bigint` into a regular `number` and then execute the function:

docs/datatypes/bignumbers.md

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,44 @@ math.bignumber('2.3e+500') // BigNumber, 2.3e+500
1515
Most functions can determine the type of output from the type of input:
1616
a number as input will return a number as output, a BigNumber as input returns
1717
a BigNumber as output. Functions which cannot determine the type of output
18-
from the input (for example `math.evaluate`) use the default number type `number`,
18+
from the input (for example, parsing an expression using `math.evaluate`, or
19+
summing an empty list of numbers) use the default number type `number`,
1920
which can be configured when instantiating math.js. To configure the use of
2021
BigNumbers instead of [numbers](numbers.md) by default, configure math.js like:
2122

2223
```js
24+
import { create, all } from 'mathjs'
25+
const math = create(all)
26+
2327
math.config({
24-
number: 'BigNumber', // Default type of number:
25-
// 'number' (default), 'BigNumber', or 'Fraction'
26-
precision: 64, // Number of significant digits for BigNumbers
27-
relTol: 1e-60,
28-
absTol: 1e-63
28+
number: 'BigNumber' , // Default type of number: 'number' (default),
29+
// 'BigNumber', 'Fraction', or 'bigint'
30+
compute: {
31+
BigNumber: {
32+
precision: 64 // Number of significant digits for BigNumbers
33+
},
34+
defaultRelTol: 1e-60, // consider numbers equal if they differ by
35+
// less than this fraction of the size of the
36+
// larger,
37+
defaultAbsTol: 1e-63 // or if their absolute difference is less than
38+
} // this.
2939
})
3040

3141
// use math
3242
math.evaluate('0.1 + 0.2') // BigNumber, 0.3
3343
```
3444

3545
The default precision for BigNumber is 64 digits, and can be configured with
36-
the option `precision`.
37-
38-
Note that we also change the configuration of `relTol` and `absTol`
39-
to be close to the precision limit of our BigNumbers. `relTol` and `absTol` are used for
40-
example in relational and rounding functions (`equal`, `larger`, `smaller`,
41-
`round`, `floor`, etc) to determine when a value is nearly equal,
42-
see [Equality](numbers.md#equality). If we would leave `relTol` and `absTol` unchanged,
43-
having the default value of `1e-12` and `1e-15` respectively, we could get inaccurate and misleading
44-
results since we're now working with a higher precision.
46+
the option `compute.BigNumber.precision`.
47+
48+
Note that we also change the configuration of `compute.defaultRelTol` and
49+
`compute.defaultAbsTol` to be close to the precision limit of our BigNumbers.
50+
These options are used for example in relational and rounding functions
51+
(`equal`, `larger`, `smaller`, `round`, `floor`, etc) to determine when
52+
one value is nearly equal another, see [Equality](numbers.md#equality).
53+
If we would leave `defaultRelTol` and `defaultAbsTol` unchanged, having the
54+
default values of `1e-12` and `1e-15` respectively, we could get inaccurate
55+
and misleading results since we're now working with a higher precision.
4556

4657

4758
## Support

0 commit comments

Comments
 (0)