mathjs 10.5.2 → 10.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/HISTORY.md +8 -0
- package/lib/browser/math.js +3 -3
- package/lib/browser/math.js.map +1 -1
- package/lib/cjs/header.js +2 -2
- package/lib/cjs/version.js +1 -1
- package/lib/esm/version.js +1 -1
- package/package.json +6 -11
- package/docs/command_line_interface.md +0 -87
- package/docs/core/chaining.md +0 -41
- package/docs/core/configuration.md +0 -144
- package/docs/core/extension.md +0 -263
- package/docs/core/index.md +0 -21
- package/docs/core/serialization.md +0 -50
- package/docs/custom_bundling.md +0 -116
- package/docs/datatypes/bignumbers.md +0 -102
- package/docs/datatypes/complex_numbers.md +0 -168
- package/docs/datatypes/fractions.md +0 -75
- package/docs/datatypes/index.md +0 -67
- package/docs/datatypes/matrices.md +0 -375
- package/docs/datatypes/numbers.md +0 -106
- package/docs/datatypes/units.md +0 -444
- package/docs/expressions/algebra.md +0 -110
- package/docs/expressions/customization.md +0 -379
- package/docs/expressions/expression_trees.md +0 -710
- package/docs/expressions/html_classes.md +0 -38
- package/docs/expressions/index.md +0 -21
- package/docs/expressions/parsing.md +0 -224
- package/docs/expressions/security.md +0 -89
- package/docs/expressions/syntax.md +0 -706
- package/docs/getting_started.md +0 -124
- package/docs/index.md +0 -39
- package/docs/reference/classes/densematrix.md +0 -247
- package/docs/reference/classes/fibonacciheap.md +0 -70
- package/docs/reference/classes/matrixindex.md +0 -133
- package/docs/reference/classes/matrixrange.md +0 -158
- package/docs/reference/classes/resultset.md +0 -47
- package/docs/reference/classes/sparsematrix.md +0 -245
- package/docs/reference/classes/unit.md +0 -242
- package/docs/reference/classes.md +0 -86
- package/docs/reference/constants.md +0 -29
- package/docs/reference/functions/abs.md +0 -46
- package/docs/reference/functions/acos.md +0 -49
- package/docs/reference/functions/acosh.md +0 -47
- package/docs/reference/functions/acot.md +0 -48
- package/docs/reference/functions/acoth.md +0 -46
- package/docs/reference/functions/acsc.md +0 -49
- package/docs/reference/functions/acsch.md +0 -46
- package/docs/reference/functions/add.md +0 -59
- package/docs/reference/functions/and.md +0 -53
- package/docs/reference/functions/apply.md +0 -56
- package/docs/reference/functions/arg.md +0 -53
- package/docs/reference/functions/asec.md +0 -49
- package/docs/reference/functions/asech.md +0 -46
- package/docs/reference/functions/asin.md +0 -49
- package/docs/reference/functions/asinh.md +0 -46
- package/docs/reference/functions/atan.md +0 -49
- package/docs/reference/functions/atan2.md +0 -56
- package/docs/reference/functions/atanh.md +0 -46
- package/docs/reference/functions/bellNumbers.md +0 -45
- package/docs/reference/functions/bignumber.md +0 -47
- package/docs/reference/functions/bin.md +0 -45
- package/docs/reference/functions/bitAnd.md +0 -51
- package/docs/reference/functions/bitNot.md +0 -51
- package/docs/reference/functions/bitOr.md +0 -52
- package/docs/reference/functions/bitXor.md +0 -51
- package/docs/reference/functions/boolean.md +0 -50
- package/docs/reference/functions/catalan.md +0 -45
- package/docs/reference/functions/cbrt.md +0 -60
- package/docs/reference/functions/ceil.md +0 -63
- package/docs/reference/functions/chain.md +0 -54
- package/docs/reference/functions/clone.md +0 -43
- package/docs/reference/functions/column.md +0 -45
- package/docs/reference/functions/combinations.md +0 -49
- package/docs/reference/functions/combinationsWithRep.md +0 -49
- package/docs/reference/functions/compare.md +0 -67
- package/docs/reference/functions/compareNatural.md +0 -92
- package/docs/reference/functions/compareText.md +0 -54
- package/docs/reference/functions/compile.md +0 -56
- package/docs/reference/functions/complex.md +0 -17
- package/docs/reference/functions/composition.md +0 -46
- package/docs/reference/functions/concat.md +0 -56
- package/docs/reference/functions/config.md +0 -44
- package/docs/reference/functions/conj.md +0 -50
- package/docs/reference/functions/cos.md +0 -51
- package/docs/reference/functions/cosh.md +0 -46
- package/docs/reference/functions/cot.md +0 -47
- package/docs/reference/functions/coth.md +0 -49
- package/docs/reference/functions/count.md +0 -45
- package/docs/reference/functions/createUnit.md +0 -52
- package/docs/reference/functions/cross.md +0 -58
- package/docs/reference/functions/csc.md +0 -47
- package/docs/reference/functions/csch.md +0 -49
- package/docs/reference/functions/ctranspose.md +0 -50
- package/docs/reference/functions/cube.md +0 -51
- package/docs/reference/functions/cumsum.md +0 -57
- package/docs/reference/functions/deepEqual.md +0 -53
- package/docs/reference/functions/derivative.md +0 -61
- package/docs/reference/functions/det.md +0 -49
- package/docs/reference/functions/diag.md +0 -61
- package/docs/reference/functions/diff.md +0 -70
- package/docs/reference/functions/distance.md +0 -80
- package/docs/reference/functions/divide.md +0 -55
- package/docs/reference/functions/dot.md +0 -48
- package/docs/reference/functions/dotDivide.md +0 -52
- package/docs/reference/functions/dotMultiply.md +0 -52
- package/docs/reference/functions/dotPow.md +0 -49
- package/docs/reference/functions/eigs.md +0 -54
- package/docs/reference/functions/equal.md +0 -75
- package/docs/reference/functions/equalText.md +0 -53
- package/docs/reference/functions/erf.md +0 -49
- package/docs/reference/functions/evaluate.md +0 -56
- package/docs/reference/functions/exp.md +0 -54
- package/docs/reference/functions/expm.md +0 -49
- package/docs/reference/functions/expm1.md +0 -54
- package/docs/reference/functions/factorial.md +0 -49
- package/docs/reference/functions/fft.md +0 -35
- package/docs/reference/functions/filter.md +0 -50
- package/docs/reference/functions/fix.md +0 -62
- package/docs/reference/functions/flatten.md +0 -46
- package/docs/reference/functions/floor.md +0 -66
- package/docs/reference/functions/forEach.md +0 -41
- package/docs/reference/functions/format.md +0 -127
- package/docs/reference/functions/fraction.md +0 -45
- package/docs/reference/functions/gamma.md +0 -49
- package/docs/reference/functions/gcd.md +0 -50
- package/docs/reference/functions/getMatrixDataType.md +0 -59
- package/docs/reference/functions/hasNumericValue.md +0 -55
- package/docs/reference/functions/help.md +0 -42
- package/docs/reference/functions/hex.md +0 -45
- package/docs/reference/functions/hypot.md +0 -51
- package/docs/reference/functions/identity.md +0 -57
- package/docs/reference/functions/ifft.md +0 -35
- package/docs/reference/functions/im.md +0 -53
- package/docs/reference/functions/import.md +0 -68
- package/docs/reference/functions/index.md +0 -55
- package/docs/reference/functions/intersect.md +0 -50
- package/docs/reference/functions/inv.md +0 -45
- package/docs/reference/functions/invmod.md +0 -47
- package/docs/reference/functions/isInteger.md +0 -55
- package/docs/reference/functions/isNaN.md +0 -56
- package/docs/reference/functions/isNegative.md +0 -55
- package/docs/reference/functions/isNumeric.md +0 -55
- package/docs/reference/functions/isPositive.md +0 -57
- package/docs/reference/functions/isPrime.md +0 -54
- package/docs/reference/functions/isZero.md +0 -59
- package/docs/reference/functions/kldivergence.md +0 -41
- package/docs/reference/functions/kron.md +0 -53
- package/docs/reference/functions/larger.md +0 -60
- package/docs/reference/functions/largerEq.md +0 -56
- package/docs/reference/functions/lcm.md +0 -54
- package/docs/reference/functions/leafCount.md +0 -52
- package/docs/reference/functions/leftShift.md +0 -52
- package/docs/reference/functions/lgamma.md +0 -46
- package/docs/reference/functions/log.md +0 -57
- package/docs/reference/functions/log10.md +0 -50
- package/docs/reference/functions/log1p.md +0 -54
- package/docs/reference/functions/log2.md +0 -50
- package/docs/reference/functions/lsolve.md +0 -51
- package/docs/reference/functions/lsolveAll.md +0 -51
- package/docs/reference/functions/lup.md +0 -52
- package/docs/reference/functions/lusolve.md +0 -59
- package/docs/reference/functions/mad.md +0 -50
- package/docs/reference/functions/map.md +0 -65
- package/docs/reference/functions/matrix.md +0 -56
- package/docs/reference/functions/matrixFromColumns.md +0 -49
- package/docs/reference/functions/matrixFromFunction.md +0 -54
- package/docs/reference/functions/matrixFromRows.md +0 -49
- package/docs/reference/functions/max.md +0 -61
- package/docs/reference/functions/mean.md +0 -57
- package/docs/reference/functions/median.md +0 -57
- package/docs/reference/functions/min.md +0 -61
- package/docs/reference/functions/mod.md +0 -59
- package/docs/reference/functions/mode.md +0 -50
- package/docs/reference/functions/multinomial.md +0 -46
- package/docs/reference/functions/multiply.md +0 -60
- package/docs/reference/functions/norm.md +0 -59
- package/docs/reference/functions/not.md +0 -50
- package/docs/reference/functions/nthRoot.md +0 -53
- package/docs/reference/functions/nthRoots.md +0 -58
- package/docs/reference/functions/number.md +0 -49
- package/docs/reference/functions/numeric.md +0 -52
- package/docs/reference/functions/oct.md +0 -45
- package/docs/reference/functions/ones.md +0 -59
- package/docs/reference/functions/or.md +0 -53
- package/docs/reference/functions/parse.md +0 -56
- package/docs/reference/functions/parser.md +0 -70
- package/docs/reference/functions/partitionSelect.md +0 -53
- package/docs/reference/functions/permutations.md +0 -51
- package/docs/reference/functions/pickRandom.md +0 -59
- package/docs/reference/functions/pinv.md +0 -44
- package/docs/reference/functions/pow.md +0 -65
- package/docs/reference/functions/print.md +0 -64
- package/docs/reference/functions/prod.md +0 -55
- package/docs/reference/functions/qr.md +0 -65
- package/docs/reference/functions/quantileSeq.md +0 -62
- package/docs/reference/functions/random.md +0 -54
- package/docs/reference/functions/randomInt.md +0 -53
- package/docs/reference/functions/range.md +0 -70
- package/docs/reference/functions/rationalize.md +0 -68
- package/docs/reference/functions/re.md +0 -53
- package/docs/reference/functions/reshape.md +0 -60
- package/docs/reference/functions/resize.md +0 -51
- package/docs/reference/functions/resolve.md +0 -46
- package/docs/reference/functions/rightArithShift.md +0 -52
- package/docs/reference/functions/rightLogShift.md +0 -52
- package/docs/reference/functions/rotate.md +0 -53
- package/docs/reference/functions/rotationMatrix.md +0 -57
- package/docs/reference/functions/round.md +0 -61
- package/docs/reference/functions/row.md +0 -45
- package/docs/reference/functions/sec.md +0 -47
- package/docs/reference/functions/sech.md +0 -49
- package/docs/reference/functions/setCartesian.md +0 -49
- package/docs/reference/functions/setDifference.md +0 -47
- package/docs/reference/functions/setDistinct.md +0 -43
- package/docs/reference/functions/setIntersect.md +0 -46
- package/docs/reference/functions/setIsSubset.md +0 -47
- package/docs/reference/functions/setMultiplicity.md +0 -46
- package/docs/reference/functions/setPowerset.md +0 -43
- package/docs/reference/functions/setSize.md +0 -47
- package/docs/reference/functions/setSymDifference.md +0 -47
- package/docs/reference/functions/setUnion.md +0 -46
- package/docs/reference/functions/sign.md +0 -52
- package/docs/reference/functions/simplify.md +0 -130
- package/docs/reference/functions/simplifyConstant.md +0 -52
- package/docs/reference/functions/simplifyCore.md +0 -50
- package/docs/reference/functions/sin.md +0 -51
- package/docs/reference/functions/sinh.md +0 -46
- package/docs/reference/functions/size.md +0 -50
- package/docs/reference/functions/slu.md +0 -57
- package/docs/reference/functions/smaller.md +0 -60
- package/docs/reference/functions/smallerEq.md +0 -56
- package/docs/reference/functions/sort.md +0 -56
- package/docs/reference/functions/sparse.md +0 -51
- package/docs/reference/functions/splitUnit.md +0 -37
- package/docs/reference/functions/sqrt.md +0 -50
- package/docs/reference/functions/sqrtm.md +0 -46
- package/docs/reference/functions/square.md +0 -51
- package/docs/reference/functions/squeeze.md +0 -53
- package/docs/reference/functions/std.md +0 -76
- package/docs/reference/functions/stirlingS2.md +0 -52
- package/docs/reference/functions/string.md +0 -49
- package/docs/reference/functions/subset.md +0 -65
- package/docs/reference/functions/subtract.md +0 -54
- package/docs/reference/functions/sum.md +0 -54
- package/docs/reference/functions/symbolicEqual.md +0 -62
- package/docs/reference/functions/tan.md +0 -49
- package/docs/reference/functions/tanh.md +0 -50
- package/docs/reference/functions/to.md +0 -47
- package/docs/reference/functions/trace.md +0 -50
- package/docs/reference/functions/transpose.md +0 -49
- package/docs/reference/functions/typeOf.md +0 -81
- package/docs/reference/functions/typed.md +0 -56
- package/docs/reference/functions/unaryMinus.md +0 -49
- package/docs/reference/functions/unaryPlus.md +0 -48
- package/docs/reference/functions/unequal.md +0 -74
- package/docs/reference/functions/unit.md +0 -48
- package/docs/reference/functions/usolve.md +0 -51
- package/docs/reference/functions/usolveAll.md +0 -51
- package/docs/reference/functions/variance.md +0 -78
- package/docs/reference/functions/xgcd.md +0 -47
- package/docs/reference/functions/xor.md +0 -53
- package/docs/reference/functions/zeros.md +0 -57
- package/docs/reference/functions.md +0 -297
- package/docs/reference/index.md +0 -5
- package/examples/advanced/convert_fraction_to_bignumber.js +0 -59
- package/examples/advanced/custom_argument_parsing.js +0 -98
- package/examples/advanced/custom_datatype.js +0 -56
- package/examples/advanced/custom_evaluate_using_factories.js +0 -19
- package/examples/advanced/custom_evaluate_using_import.js +0 -18
- package/examples/advanced/custom_loading.js +0 -33
- package/examples/advanced/custom_relational_functions.js +0 -62
- package/examples/advanced/custom_scope_objects.js +0 -115
- package/examples/advanced/expression_trees.js +0 -55
- package/examples/advanced/function_transform.js +0 -50
- package/examples/advanced/more_secure_eval.js +0 -36
- package/examples/advanced/use_bigint.js +0 -43
- package/examples/advanced/web_server/math_worker.js +0 -24
- package/examples/advanced/web_server/server.js +0 -80
- package/examples/algebra.js +0 -34
- package/examples/basic_usage.js +0 -49
- package/examples/bignumbers.js +0 -52
- package/examples/browser/angle_configuration.html +0 -134
- package/examples/browser/basic_usage.html +0 -39
- package/examples/browser/currency_conversion.html +0 -125
- package/examples/browser/custom_separators.html +0 -81
- package/examples/browser/plot.html +0 -78
- package/examples/browser/pretty_printing_with_mathjax.html +0 -122
- package/examples/browser/printing_html.html +0 -170
- package/examples/browser/requirejs_loading.html +0 -20
- package/examples/browser/rocket_trajectory_optimization.html +0 -301
- package/examples/browser/webworkers/webworkers.html +0 -80
- package/examples/browser/webworkers/worker.js +0 -28
- package/examples/chaining.js +0 -56
- package/examples/complex_numbers.js +0 -67
- package/examples/expressions.js +0 -188
- package/examples/fractions.js +0 -74
- package/examples/import.js +0 -95
- package/examples/matrices.js +0 -101
- package/examples/objects.js +0 -35
- package/examples/package.json +0 -3
- package/examples/serialization.js +0 -16
- package/examples/sparse_matrices.js +0 -19
- package/examples/units.js +0 -108
package/docs/custom_bundling.md
DELETED
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
# Custom bundling
|
|
2
|
-
|
|
3
|
-
Math.js is a large library containing many data types and functions.
|
|
4
|
-
It is well possible that you only need a small portion of the library.
|
|
5
|
-
Math.js allows for picking just the functions and data types you need.
|
|
6
|
-
This gives faster load times and smaller browser bundles. Math.js uses
|
|
7
|
-
ES modules, and creating small bundles using tree-shaking works out of
|
|
8
|
-
the box when using Webpack for example.
|
|
9
|
-
|
|
10
|
-
This page describes:
|
|
11
|
-
|
|
12
|
-
- How to use just a few functions for faster load times and smaller bundles.
|
|
13
|
-
- How to use light-weight, number only implementations of functions.
|
|
14
|
-
- What to expect from bundle sizes when using tree-shaking.
|
|
15
|
-
|
|
16
|
-
## Using just a few functions
|
|
17
|
-
|
|
18
|
-
Using the function `create`, a mathjs instance can be created.
|
|
19
|
-
The `all` object contains all functionality available in mathjs,
|
|
20
|
-
and a mathjs instance containing everything can be created like:
|
|
21
|
-
|
|
22
|
-
```js
|
|
23
|
-
import { create, all } from 'mathjs'
|
|
24
|
-
|
|
25
|
-
const math = create(all)
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
To create an instance with just a few functions, you have to pass the
|
|
29
|
-
factory functions of the functions you need, and all their dependencies.
|
|
30
|
-
For example the function `add` depends on the functions `addScalar`,
|
|
31
|
-
`equalScalar`, classes `DenseMatrix` and `SparseMatrix`, and more.
|
|
32
|
-
Because it is hard to figure out what the dependencies of a function are,
|
|
33
|
-
and the dependencies of the dependencies, mathjs provides ready made
|
|
34
|
-
collections of all dependencies for every function. For example all
|
|
35
|
-
factory functions of function `add` and its dependencies are available
|
|
36
|
-
as `addDependencies`.
|
|
37
|
-
|
|
38
|
-
Here is a full example of loading just a few functions in a mathjs instance:
|
|
39
|
-
|
|
40
|
-
```js
|
|
41
|
-
// file: custom_loading.js
|
|
42
|
-
|
|
43
|
-
import {
|
|
44
|
-
create,
|
|
45
|
-
fractionDependencies,
|
|
46
|
-
addDependencies,
|
|
47
|
-
divideDependencies,
|
|
48
|
-
formatDependencies
|
|
49
|
-
} from 'mathjs'
|
|
50
|
-
|
|
51
|
-
const config = {
|
|
52
|
-
// optionally, you can specify configuration
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// Create just the functions we need
|
|
56
|
-
const { fraction, add, divide, format } = create({
|
|
57
|
-
fractionDependencies,
|
|
58
|
-
addDependencies,
|
|
59
|
-
divideDependencies,
|
|
60
|
-
formatDependencies
|
|
61
|
-
}, config)
|
|
62
|
-
|
|
63
|
-
// Use the created functions
|
|
64
|
-
const a = fraction(1, 3)
|
|
65
|
-
const b = fraction(3, 7)
|
|
66
|
-
const c = add(a, b)
|
|
67
|
-
const d = divide(a, b)
|
|
68
|
-
console.log('c =', format(c)) // outputs "c = 16/21"
|
|
69
|
-
console.log('d =', format(d)) // outputs "d = 7/9"
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
This example can be bundled using for example Webpack:
|
|
73
|
-
|
|
74
|
-
```
|
|
75
|
-
npx webpack custom_loading.js -o custom_loading.bundle.js --mode=production
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
Only the used parts of mathjs will be bundled thanks to tree-shaking.
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
## Numbers only
|
|
82
|
-
|
|
83
|
-
The functions of mathjs support multiple data types out of the box, like
|
|
84
|
-
numbers, bignumbers, complex numbers, units, and matrices. Quite commonly however,
|
|
85
|
-
only support for numbers is needed and the other data-types are overkill.
|
|
86
|
-
|
|
87
|
-
To accomodate for this use case of only numbers only, mathjs offers light-weight,
|
|
88
|
-
number only implementations of all relevant functions. These are available by
|
|
89
|
-
importing from `'mathjs/number'` instead of `'mathjs'`:
|
|
90
|
-
|
|
91
|
-
```js
|
|
92
|
-
// use light-weight, numbers only implementations of functions
|
|
93
|
-
import { create, all } from 'mathjs/number'
|
|
94
|
-
|
|
95
|
-
const math = create(all)
|
|
96
|
-
console.log(add(2, 3)) // 5
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
## Bundle size
|
|
100
|
-
|
|
101
|
-
When using just a few functions of mathjs instead of the whole library,
|
|
102
|
-
you may expect the size of the bundle to be just a small fraction of the
|
|
103
|
-
complete library. However, to create the function `add` supporting all data
|
|
104
|
-
types, all these data types must be included: Unit, BigNumber, Complex,
|
|
105
|
-
DenseMatrix, SparseMatrix, etc. A rough idea of the size of different parts of
|
|
106
|
-
mathjs:
|
|
107
|
-
|
|
108
|
-
- About 5% is coming from core functionality like `create`, `import`, `factory`,
|
|
109
|
-
`typed-function`, etc.
|
|
110
|
-
- About 30% of the bundle size comes from the data classes `Complex`, `BigNumber`, `Fraction`, `Unit`, `SparseMatrix`, `DenseMatrix`.
|
|
111
|
-
- About 25% of the bundle size comes from the expression parser.
|
|
112
|
-
Half of this comes from the embedded docs.
|
|
113
|
-
- About 40% comes from the about 200 built-in functions and some constants.
|
|
114
|
-
|
|
115
|
-
To get a better insight in what is in your JavaScript bundle, you can use
|
|
116
|
-
a tool like [source-map-explorer](https://github.com/danvk/source-map-explorer).
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
# BigNumbers
|
|
2
|
-
|
|
3
|
-
For calculations with an arbitrary precision, math.js supports a `BigNumber`
|
|
4
|
-
datatype. BigNumber support is powered by
|
|
5
|
-
[decimal.js](https://github.com/MikeMcl/decimal.js/).
|
|
6
|
-
|
|
7
|
-
## Usage
|
|
8
|
-
|
|
9
|
-
A BigNumber can be created using the function `bignumber`:
|
|
10
|
-
|
|
11
|
-
```js
|
|
12
|
-
math.bignumber('2.3e+500') // BigNumber, 2.3e+500
|
|
13
|
-
```
|
|
14
|
-
|
|
15
|
-
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 BigNumber as input returns
|
|
17
|
-
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`,
|
|
19
|
-
which can be configured when instantiating math.js. To configure the use of
|
|
20
|
-
BigNumbers instead of [numbers](numbers.md) by default, configure math.js like:
|
|
21
|
-
|
|
22
|
-
```js
|
|
23
|
-
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
|
-
})
|
|
28
|
-
|
|
29
|
-
// use math
|
|
30
|
-
math.evaluate('0.1 + 0.2') // BigNumber, 0.3
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
The default precision for BigNumber is 64 digits, and can be configured with
|
|
34
|
-
the option `precision`.
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
## Support
|
|
38
|
-
|
|
39
|
-
Most functions in math.js support BigNumbers, but not all of them.
|
|
40
|
-
For example the function `random` doesn't support BigNumbers.
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
## Round-off errors
|
|
44
|
-
|
|
45
|
-
Calculations with BigNumber are much slower than calculations with Number,
|
|
46
|
-
but they can be executed with an arbitrary precision. By using a higher
|
|
47
|
-
precision, it is less likely that round-off errors occur:
|
|
48
|
-
|
|
49
|
-
```js
|
|
50
|
-
// round-off errors with numbers
|
|
51
|
-
math.add(0.1, 0.2) // Number, 0.30000000000000004
|
|
52
|
-
math.divide(0.3, 0.2) // Number, 1.4999999999999998
|
|
53
|
-
|
|
54
|
-
// no round-off errors with BigNumbers :)
|
|
55
|
-
math.add(math.bignumber(0.1), math.bignumber(0.2)) // BigNumber, 0.3
|
|
56
|
-
math.divide(math.bignumber(0.3), math.bignumber(0.2)) // BigNumber, 1.5
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
## Limitations
|
|
61
|
-
|
|
62
|
-
It's important to realize that BigNumbers do not solve *all* problems related
|
|
63
|
-
to precision and round-off errors. Numbers with an infinite number of digits
|
|
64
|
-
cannot be represented with a regular number nor a BigNumber. Though a BigNumber
|
|
65
|
-
can store a much larger number of digits, the amount of digits remains limited
|
|
66
|
-
if only to keep calculations fast enough to remain practical.
|
|
67
|
-
|
|
68
|
-
```js
|
|
69
|
-
const one = math.bignumber(1)
|
|
70
|
-
const three = math.bignumber(3)
|
|
71
|
-
const third = math.divide(one, three)
|
|
72
|
-
console.log(third.toString())
|
|
73
|
-
// outputs 0.3333333333333333333333333333333333333333333333333333333333333333
|
|
74
|
-
|
|
75
|
-
const ans = math.multiply(third, three)
|
|
76
|
-
console.log(ans.toString())
|
|
77
|
-
// outputs 0.9999999999999999999999999999999999999999999999999999999999999999
|
|
78
|
-
// this should be 1 again, but `third` is rounded to a limited number of digits 3
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
## Conversion
|
|
83
|
-
|
|
84
|
-
BigNumbers can be converted to numbers and vice versa using the functions
|
|
85
|
-
`number` and `bignumber`. When converting a BigNumber to a number, the high
|
|
86
|
-
precision of the BigNumber will be lost. When a BigNumber is too large to be represented
|
|
87
|
-
as Number, it will be initialized as `Infinity`.
|
|
88
|
-
|
|
89
|
-
```js
|
|
90
|
-
// converting numbers and BigNumbers
|
|
91
|
-
const a = math.number(0.3) // number, 0.3
|
|
92
|
-
const b = math.bignumber(a) // BigNumber, 0.3
|
|
93
|
-
const c = math.number(b) // number, 0.3
|
|
94
|
-
|
|
95
|
-
// exceeding the maximum of a number
|
|
96
|
-
const d = math.bignumber('1.2e500') // BigNumber, 1.2e+500
|
|
97
|
-
const e = math.number(d) // number, Infinity
|
|
98
|
-
|
|
99
|
-
// loosing precision when converting to number
|
|
100
|
-
const f = math.bignumber('0.2222222222222222222') // BigNumber, 0.2222222222222222222
|
|
101
|
-
const g = math.number(f) // number, 0.2222222222222222
|
|
102
|
-
```
|
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
# Complex Numbers
|
|
2
|
-
|
|
3
|
-
Math.js supports the creation, manipulation, and calculations with complex numbers.
|
|
4
|
-
Support of complex numbers is powered by the library [complex.js](https://github.com/infusion/Complex.js).
|
|
5
|
-
|
|
6
|
-
In mathematics, a complex number is an expression of the form `a + bi`,
|
|
7
|
-
where `a` and `b` are real numbers and `i` represents the imaginary number
|
|
8
|
-
defined as `i^2 = -1`. (In other words, `i` is the square root of `-1`.)
|
|
9
|
-
The real number `a` is called the real part of the complex number,
|
|
10
|
-
and the real number `b` is the imaginary part. For example, `3 + 2i` is a
|
|
11
|
-
complex number, having real part `3` and imaginary part `2`.
|
|
12
|
-
Complex numbers are often used in applied mathematics, control theory,
|
|
13
|
-
signal analysis, fluid dynamics and other fields.
|
|
14
|
-
|
|
15
|
-
## Usage
|
|
16
|
-
|
|
17
|
-
A complex number is created using the function `math.complex`. This function
|
|
18
|
-
accepts:
|
|
19
|
-
|
|
20
|
-
- two numbers representing the real and imaginary part of the value,
|
|
21
|
-
- a single string containing a complex value in the form `a + bi` where `a`
|
|
22
|
-
and `b` respectively represent the real and imaginary part of the complex number.
|
|
23
|
-
- an object with either properties `re` and `im` for the real and imaginary
|
|
24
|
-
part of the value, or two properties `r` and `phi` containing the polar
|
|
25
|
-
coordinates of a complex value.
|
|
26
|
-
The function returns a `Complex` object.
|
|
27
|
-
|
|
28
|
-
Syntax:
|
|
29
|
-
|
|
30
|
-
```js
|
|
31
|
-
math.complex(re: number) : Complex
|
|
32
|
-
math.complex(re: number, im: number) : Complex
|
|
33
|
-
math.complex(complex: Complex) : Complex
|
|
34
|
-
math.complex({re: Number, im: Number}) : Complex
|
|
35
|
-
math.complex({r: number, phi: number}) : Complex
|
|
36
|
-
math.complex({abs: number, arg: number}) : Complex
|
|
37
|
-
math.complex(str: string) : Complex
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
Examples:
|
|
41
|
-
|
|
42
|
-
```js
|
|
43
|
-
const a = math.complex(2, 3) // Complex 2 + 3i
|
|
44
|
-
a.re // Number 2
|
|
45
|
-
a.im // Number 3
|
|
46
|
-
|
|
47
|
-
const b = math.complex('4 - 2i') // Complex 4 - 2i
|
|
48
|
-
b.re = 5 // Number 5
|
|
49
|
-
b // Complex 5 - 2i
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
## Calculations
|
|
53
|
-
|
|
54
|
-
Most functions of math.js support complex numbers. Complex and real numbers
|
|
55
|
-
can be used together.
|
|
56
|
-
|
|
57
|
-
```js
|
|
58
|
-
const a = math.complex(2, 3) // Complex 2 + 3i
|
|
59
|
-
const b = math.complex('4 - 2i') // Complex 4 - 2i
|
|
60
|
-
|
|
61
|
-
math.re(a) // Number 2
|
|
62
|
-
math.im(a) // Number 3
|
|
63
|
-
math.conj(a) // Complex 2 - 3i
|
|
64
|
-
|
|
65
|
-
math.add(a, b) // Complex 6 + i
|
|
66
|
-
math.multiply(a, 2) // Complex 4 + 6i
|
|
67
|
-
math.sqrt(-4) // Complex 2i
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
## API
|
|
71
|
-
A `Complex` object created by `math.complex` contains the following properties and functions:
|
|
72
|
-
|
|
73
|
-
### complex.re
|
|
74
|
-
|
|
75
|
-
A number containing the real part of the complex number. Can be read and replaced.
|
|
76
|
-
|
|
77
|
-
### complex.im
|
|
78
|
-
|
|
79
|
-
A number containing the imaginary part of the complex number. Can be read and replaced.
|
|
80
|
-
|
|
81
|
-
### complex.clone()
|
|
82
|
-
|
|
83
|
-
Create a clone of the complex number.
|
|
84
|
-
|
|
85
|
-
### complex.equals(other)
|
|
86
|
-
|
|
87
|
-
Test whether a complex number equals another complex value.
|
|
88
|
-
|
|
89
|
-
Two complex numbers are equal when both their real and imaginary parts are
|
|
90
|
-
equal.
|
|
91
|
-
|
|
92
|
-
### complex.neg()
|
|
93
|
-
|
|
94
|
-
Returns a complex number with a real part and an imaginary part equal in magnitude but opposite in sign to the current complex number.
|
|
95
|
-
|
|
96
|
-
### complex.conjugate()
|
|
97
|
-
|
|
98
|
-
Returns a complex number with an equal real part and an imaginary part equal in magnitude but opposite in sign to the current complex number.
|
|
99
|
-
|
|
100
|
-
### complex.inverse()
|
|
101
|
-
|
|
102
|
-
Returns a complex number that is inverse of the current complex number.
|
|
103
|
-
|
|
104
|
-
### complex.toVector()
|
|
105
|
-
|
|
106
|
-
Get the vector representation of the current complex number. Returns an array of size 2.
|
|
107
|
-
|
|
108
|
-
### complex.toJSON()
|
|
109
|
-
|
|
110
|
-
Returns a JSON representation of the complex number, with signature
|
|
111
|
-
`{mathjs: 'Complex', re: number, im: number}`.
|
|
112
|
-
Used when serializing a complex number, see [Serialization](../core/serialization.md).
|
|
113
|
-
|
|
114
|
-
### complex.toPolar()
|
|
115
|
-
|
|
116
|
-
Get the polar coordinates of the complex number, returns
|
|
117
|
-
an object with properties `r` and `phi`.
|
|
118
|
-
|
|
119
|
-
### complex.toString()
|
|
120
|
-
|
|
121
|
-
Returns a string representation of the complex number, formatted
|
|
122
|
-
as `a + bi` where `a` is the real part and `b` the imaginary part.
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
### complex.format([precision: number])
|
|
126
|
-
|
|
127
|
-
Get a string representation of the complex number,
|
|
128
|
-
formatted as `a + bi` where `a` is the real part and `b` the imaginary part.
|
|
129
|
-
If precision is defined, the units value will be rounded to the provided
|
|
130
|
-
number of digits.
|
|
131
|
-
|
|
132
|
-
## Static methods
|
|
133
|
-
The following static methods can be accessed using `math.Complex`
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
### Complex.fromJSON(json)
|
|
137
|
-
|
|
138
|
-
Revive a complex number from a JSON object. Accepts
|
|
139
|
-
An object `{mathjs: 'Complex', re: number, im: number}`, where the property
|
|
140
|
-
`mathjs` is optional.
|
|
141
|
-
Used when deserializing a complex number, see [Serialization](../core/serialization.md).
|
|
142
|
-
|
|
143
|
-
### Complex.fromPolar(r: number, phi: number)
|
|
144
|
-
|
|
145
|
-
Create a complex number from polar coordinates.
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
### Complex.compare(a: Complex, b: Complex)
|
|
149
|
-
|
|
150
|
-
Returns the comparision result of two complex number:
|
|
151
|
-
|
|
152
|
-
- Returns 1 when the real part of `a` is larger than the real part of `b`
|
|
153
|
-
- Returns -1 when the real part of `a` is smaller than the real part of `b`
|
|
154
|
-
- Returns 1 when the real parts are equal
|
|
155
|
-
and the imaginary part of `a` is larger than the imaginary part of `b`
|
|
156
|
-
- Returns -1 when the real parts are equal
|
|
157
|
-
and the imaginary part of `a` is smaller than the imaginary part of `b`
|
|
158
|
-
- Returns 0 when both real and imaginary parts are equal.
|
|
159
|
-
|
|
160
|
-
Example:
|
|
161
|
-
```js
|
|
162
|
-
const a = math.complex(2, 3) // Complex 2 + 3i
|
|
163
|
-
const b = math.complex(2, 1) // Complex 2 + 1i
|
|
164
|
-
math.Complex.compare(a,b) // returns 1
|
|
165
|
-
|
|
166
|
-
//create from json
|
|
167
|
-
const c = math.Complex.fromJSON({mathjs: 'Complex', re: 4, im: 3}) // Complex 4 + 3i
|
|
168
|
-
```
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
# Fractions
|
|
2
|
-
|
|
3
|
-
For calculations with fractions, math.js supports a `Fraction` data type.
|
|
4
|
-
Fraction support is powered by [fraction.js](https://github.com/infusion/Fraction.js).
|
|
5
|
-
Unlike [numbers](numbers.md) and [BigNumbers](./bignumbers.md), fractions can
|
|
6
|
-
store numbers with infinitely repeating decimals, for example `1/3 = 0.3333333...`,
|
|
7
|
-
which can be represented as `0.(3)`, or `2/7` which can be represented as `0.(285714)`.
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
## Usage
|
|
11
|
-
|
|
12
|
-
A Fraction can be created using the function `fraction`:
|
|
13
|
-
|
|
14
|
-
```js
|
|
15
|
-
math.fraction('1/3') // Fraction, 1/3
|
|
16
|
-
math.fraction(2, 3) // Fraction, 2/3
|
|
17
|
-
math.fraction('0.(3)') // Fraction, 1/3
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
And can be used in functions like `add` and `multiply` like:
|
|
21
|
-
|
|
22
|
-
```js
|
|
23
|
-
math.add(math.fraction('1/3'), math.fraction('1/6')) // Fraction, 1/2
|
|
24
|
-
math.multiply(math.fraction('1/4'), math.fraction('1/2')) // Fraction, 1/8
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
Note that not all functions support fractions. For example trigonometric
|
|
28
|
-
functions doesn't support fractions. When not supported, the functions
|
|
29
|
-
will convert the input to numbers and return a number as result.
|
|
30
|
-
|
|
31
|
-
Most functions will determine the type of output from the type of input:
|
|
32
|
-
a number as input will return a number as output, a Fraction as input returns
|
|
33
|
-
a Fraction as output. Functions which cannot determine the type of output
|
|
34
|
-
from the input (for example `math.evaluate`) use the default number type `number`,
|
|
35
|
-
which can be configured when instantiating math.js. To configure the use of
|
|
36
|
-
fractions instead of [numbers](numbers.md) by default, configure math.js like:
|
|
37
|
-
|
|
38
|
-
```js
|
|
39
|
-
// Configure the default type of number: 'number' (default), 'BigNumber', or 'Fraction'
|
|
40
|
-
math.config({
|
|
41
|
-
number: 'Fraction'
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
// use the expression parser
|
|
45
|
-
math.evaluate('0.32 + 0.08') // Fraction, 2/5
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
## Support
|
|
49
|
-
|
|
50
|
-
The following functions support fractions:
|
|
51
|
-
|
|
52
|
-
- Arithmetic functions: `abs`, `add`, `ceil`, `cube`, `divide`, `dotDivide`, `dotMultiply`, `fix`, `floor`, `gcd`, `mod`, `multiply`, `round`, `sign`, `square`, `subtract`, `unaryMinus`, `unaryPlus`.
|
|
53
|
-
- Construction functions: `fraction`.
|
|
54
|
-
- Relational functions: `compare`, `deepEqual`, `equal`, `larger`, `largerEq`, `smaller`, `smallerEq`, `unequal`.
|
|
55
|
-
- Utils functions: `format`.
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
## Conversion
|
|
59
|
-
|
|
60
|
-
Fractions can be converted to numbers and vice versa using the functions
|
|
61
|
-
`number` and `fraction`. When converting a Fraction to a number, precision
|
|
62
|
-
may be lost when the value cannot represented in 16 digits.
|
|
63
|
-
|
|
64
|
-
```js
|
|
65
|
-
// converting numbers and fractions
|
|
66
|
-
const a = math.number(0.3) // number, 0.3
|
|
67
|
-
const b = math.fraction(a) // Fraction, 3/10
|
|
68
|
-
const c = math.number(b) // number, 0.3
|
|
69
|
-
|
|
70
|
-
// loosing precision when converting to number: a fraction can represent
|
|
71
|
-
// a number with an infinite number of repeating decimals, a number just
|
|
72
|
-
// stores about 16 digits and cuts consecutive digits.
|
|
73
|
-
const d = math.fraction('2/5') // Fraction, 2/5
|
|
74
|
-
const e = math.number(d) // number, 0.4
|
|
75
|
-
```
|
package/docs/datatypes/index.md
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
# Data Types
|
|
2
|
-
|
|
3
|
-
The functions of math.js support multiple data types, both native JavaScript
|
|
4
|
-
types as well as more advanced types implemented in math.js. The data types can
|
|
5
|
-
be mixed together in calculations, for example by adding a Number to a
|
|
6
|
-
Complex number or Array.
|
|
7
|
-
|
|
8
|
-
The supported data types are:
|
|
9
|
-
|
|
10
|
-
- Boolean
|
|
11
|
-
- [Number](numbers.md)
|
|
12
|
-
- [BigNumber](bignumbers.md)
|
|
13
|
-
- [Complex](complex_numbers.md)
|
|
14
|
-
- [Fraction](fractions.md)
|
|
15
|
-
- [Array](matrices.md)
|
|
16
|
-
- [Matrix](matrices.md)
|
|
17
|
-
- [Unit](units.md)
|
|
18
|
-
- String
|
|
19
|
-
|
|
20
|
-
Function [`math.typeOf(x)`](../reference/functions/typeOf.md) can be used to get
|
|
21
|
-
the type of a variable.
|
|
22
|
-
|
|
23
|
-
Example usage:
|
|
24
|
-
|
|
25
|
-
```js
|
|
26
|
-
// use numbers
|
|
27
|
-
math.subtract(7.1, 2.3) // 4.8
|
|
28
|
-
math.round(math.pi, 3) // 3.142
|
|
29
|
-
math.sqrt(4.41e2) // 21
|
|
30
|
-
|
|
31
|
-
// use BigNumbers
|
|
32
|
-
math.add(math.bignumber(0.1), math.bignumber(0.2)) // BigNumber, 0.3
|
|
33
|
-
|
|
34
|
-
// use Fractions
|
|
35
|
-
math.add(math.fraction(1), math.fraction(3)) // Fraction, 0.(3)
|
|
36
|
-
|
|
37
|
-
// use strings
|
|
38
|
-
math.add('hello ', 'world') // 'hello world'
|
|
39
|
-
math.max('A', 'D', 'C') // 'D'
|
|
40
|
-
|
|
41
|
-
// use complex numbers
|
|
42
|
-
const a = math.complex(2, 3) // 2 + 3i
|
|
43
|
-
a.re // 2
|
|
44
|
-
a.im // 3
|
|
45
|
-
const b = math.complex('4 - 2i') // 4 - 2i
|
|
46
|
-
math.add(a, b) // 6 + i
|
|
47
|
-
math.sqrt(-4) // 2i
|
|
48
|
-
|
|
49
|
-
// use arrays
|
|
50
|
-
const array = [1, 2, 3, 4, 5]
|
|
51
|
-
math.factorial(array) // Array, [1, 2, 6, 24, 120]
|
|
52
|
-
math.add(array, 3) // Array, [3, 5, 6, 7, 8]
|
|
53
|
-
|
|
54
|
-
// use matrices
|
|
55
|
-
const matrix = math.matrix([1, 4, 9, 16, 25]) // Matrix, [1, 4, 9, 16, 25]
|
|
56
|
-
math.sqrt(matrix) // Matrix, [1, 2, 3, 4, 5]
|
|
57
|
-
|
|
58
|
-
// use units
|
|
59
|
-
const a = math.unit(55, 'cm') // 550 mm
|
|
60
|
-
const b = math.unit('0.1m') // 100 mm
|
|
61
|
-
math.add(a, b) // 0.65 m
|
|
62
|
-
|
|
63
|
-
// check the type of a variable
|
|
64
|
-
math.typeOf(2) // 'number'
|
|
65
|
-
math.typeOf(math.unit('2 inch')) // 'Unit'
|
|
66
|
-
math.typeOf(math.sqrt(-4)) // 'Complex'
|
|
67
|
-
```
|