mathjs 10.5.2 → 10.5.3
Sign up to get free protection for your applications and to get access to all the features.
- 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/datatypes/units.md
DELETED
@@ -1,444 +0,0 @@
|
|
1
|
-
# Units
|
2
|
-
|
3
|
-
Math.js supports units. Units can be used to do calculations and to perform
|
4
|
-
conversions.
|
5
|
-
|
6
|
-
## Usage
|
7
|
-
|
8
|
-
Units can be created using the function `math.unit`. This function accepts
|
9
|
-
either a single string argument containing a value and unit, or two arguments,
|
10
|
-
the first being a numeric value and the second a string containing a unit.
|
11
|
-
Most units support prefixes like `k` or `kilo`, and many units have both a
|
12
|
-
full name and an abbreviation. The returned object is a `Unit`.
|
13
|
-
|
14
|
-
Syntax:
|
15
|
-
|
16
|
-
```js
|
17
|
-
math.unit(value: number, name: string) : Unit
|
18
|
-
math.unit(unit: string) : Unit
|
19
|
-
math.unit(unit: Unit) : Unit
|
20
|
-
```
|
21
|
-
|
22
|
-
Example usage:
|
23
|
-
|
24
|
-
```js
|
25
|
-
const a = math.unit(45, 'cm') // Unit 450 mm
|
26
|
-
const b = math.unit('0.1 kilogram') // Unit 100 gram
|
27
|
-
const c = math.unit('2 inch') // Unit 2 inch
|
28
|
-
const d = math.unit('90 km/h') // Unit 90 km/h
|
29
|
-
const e = math.unit('101325 kg/(m s^2)') // Unit 101325 kg / (m s^2)
|
30
|
-
|
31
|
-
const d = c.to('cm') // Unit 5.08 cm
|
32
|
-
b.toNumber('gram') // Number 100
|
33
|
-
math.number(b, 'gram') // Number 100
|
34
|
-
|
35
|
-
c.equals(a) // false
|
36
|
-
c.equals(d) // true
|
37
|
-
c.equalBase(a) // true
|
38
|
-
c.equalBase(b) // false
|
39
|
-
|
40
|
-
d.toString() // String "5.08 cm"
|
41
|
-
```
|
42
|
-
|
43
|
-
Use care when creating a unit with multiple terms in the denominator. Implicit multiplication has the same operator precedence as explicit multiplication and division, which means these three expressions are identical:
|
44
|
-
|
45
|
-
```js
|
46
|
-
// These three are identical
|
47
|
-
const correct1 = math.unit('8.314 m^3 Pa / mol / K') // Unit 8.314 (m^3 Pa) / (mol K)
|
48
|
-
const correct2 = math.unit('8.314 (m^3 Pa) / (mol K)') // Unit 8.314 (m^3 Pa) / (mol K)
|
49
|
-
const correct3 = math.unit('8.314 (m^3 * Pa) / (mol * K)') // Unit 8.314 (m^3 Pa) / (mol K)
|
50
|
-
```
|
51
|
-
But this expression, which omits the second `/` between `mol` and `K`, results in the wrong value:
|
52
|
-
|
53
|
-
```js
|
54
|
-
// Missing the second '/' between 'mol' and 'K'
|
55
|
-
const incorrect = math.unit('8.314 m^3 Pa / mol K') // Unit 8.314 (m^3 Pa K) / mol
|
56
|
-
```
|
57
|
-
|
58
|
-
## Calculations
|
59
|
-
|
60
|
-
The operations that support units are `add`, `subtract`, `multiply`, `divide`, `pow`, `abs`, `sqrt`, `square`, `cube`, and `sign`.
|
61
|
-
Trigonometric functions like `cos` are also supported when the argument is an angle.
|
62
|
-
|
63
|
-
```js
|
64
|
-
const a = math.unit(45, 'cm') // Unit 450 mm
|
65
|
-
const b = math.unit('0.1m') // Unit 100 mm
|
66
|
-
math.add(a, b) // Unit 0.65 m
|
67
|
-
math.multiply(b, 2) // Unit 200 mm
|
68
|
-
|
69
|
-
const c = math.unit(45, 'deg') // Unit 45 deg
|
70
|
-
math.cos(c) // Number 0.7071067811865476
|
71
|
-
|
72
|
-
// Kinetic energy of average sedan on highway
|
73
|
-
const d = math.unit('80 mi/h') // Unit 80 mi/h
|
74
|
-
const e = math.unit('2 tonne') // Unit 2 tonne
|
75
|
-
const f = math.multiply(0.5, math.multipy(math.pow(d, 2), e))
|
76
|
-
// 1.2790064742399996 MJ
|
77
|
-
```
|
78
|
-
|
79
|
-
Operations with arrays are supported too:
|
80
|
-
|
81
|
-
```js
|
82
|
-
// Force on a charged particle moving through a magnetic field
|
83
|
-
const B = math.evaluate('[1, 0, 0] T') // [1 T, 0 T, 0 T]
|
84
|
-
const v = math.evaluate('[0, 1, 0] m/s') // [0 m / s, 1 m / s, 0 m / s]
|
85
|
-
const q = math.evaluate('1 C') // 1 C
|
86
|
-
|
87
|
-
const F = math.multiply(q, math.cross(v, B)) // [0 N, 0 N, -1 N]
|
88
|
-
```
|
89
|
-
|
90
|
-
All arithmetic operators act on the value of the unit as it is represented in SI units.
|
91
|
-
This may lead to surprising behavior when working with temperature scales like `celsius` (or `degC`) and `fahrenheit` (or `degF`).
|
92
|
-
In general you should avoid calculations using `celsius` and `fahrenheit`. Rather, use `kelvin` (or `K`) and `rankine` (or `degR`) instead.
|
93
|
-
This example highlights some problems when using `celsius` and `fahrenheit` in calculations:
|
94
|
-
|
95
|
-
```js
|
96
|
-
const T_14F = math.unit('14 degF') // Unit 14 degF (263.15 K)
|
97
|
-
const T_28F = math.multiply(T1, 2) // Unit 487.67 degF (526.3 K), not 28 degF
|
98
|
-
|
99
|
-
const Tnegative = math.unit(-13, 'degF') // Unit -13 degF (248.15 K)
|
100
|
-
const Tpositive = math.abs(T1) // Unit -13 degF (248.15 K), not 13 degF
|
101
|
-
|
102
|
-
const Trate1 = math.evaluate('5 (degC/hour)') // Unit 5 degC/hour
|
103
|
-
const Trate2 = math.evaluate('(5 degC)/hour') // Unit 278.15 degC/hour
|
104
|
-
```
|
105
|
-
|
106
|
-
The expression parser supports units too. This is described in the section about
|
107
|
-
units on the page [Syntax](../expressions/syntax.md#units).
|
108
|
-
|
109
|
-
## User-Defined Units
|
110
|
-
|
111
|
-
You can add your own units to Math.js using the `math.createUnit` function. The following example defines a new unit `furlong`, then uses the user-defined unit in a calculation:
|
112
|
-
|
113
|
-
```js
|
114
|
-
math.createUnit('furlong', '220 yards')
|
115
|
-
math.evaluate('1 mile to furlong') // 8 furlong
|
116
|
-
```
|
117
|
-
|
118
|
-
If you cannot express the new unit in terms of any existing unit, then the second argument can be omitted. In this case, a new *base unit* is created:
|
119
|
-
|
120
|
-
```js
|
121
|
-
// A 'foo' cannot be expressed in terms of any other unit.
|
122
|
-
math.createUnit('foo')
|
123
|
-
math.evaluate('8 foo * 4 feet') // 32 foo feet
|
124
|
-
```
|
125
|
-
|
126
|
-
The second argument to `createUnit` can also be a configuration object consisting of the following properties:
|
127
|
-
|
128
|
-
* **definition** A `string` or `Unit` which defines the user-defined unit in terms of existing built-in or user-defined units. If omitted, a new base unit is created.
|
129
|
-
* **prefixes** A `string` indicating which prefixes math.js should use with the new unit. Possible values are `'none'`, `'short'`, `'long'`, `'binary_short'`, or `'binary_long'`. Default is `'none'`.
|
130
|
-
* **offset** A value applied when converting to the unit. This is very helpful for temperature scales that do not share a zero with the absolute temperature scale. For example, if we were defining fahrenheit for the first time, we would use: `math.createUnit('fahrenheit', {definition: '0.555556 kelvin', offset: 459.67})`
|
131
|
-
* **aliases** An array of strings to alias the new unit. Example: `math.createUnit('knot', {definition: '0.514444 m/s', aliases: ['knots', 'kt', 'kts']})`
|
132
|
-
* **baseName** A `string` that specifies the name of the new dimension in case one needs to be created. Every unit in math.js has a dimension: length, time, velocity, etc. If the unit's `definition` doesn't match any existing dimension, or it is a new base unit, then `createUnit` will create a new dimension with the name `baseName` and assign it to the new unit. The default is to append `'_STUFF'` to the unit's name. If the unit already matches an existing dimension, this option has no effect.
|
133
|
-
|
134
|
-
An optional `options` object can also be supplied as the last argument to `createUnits`. Currently only the `override` option is supported:
|
135
|
-
|
136
|
-
```js
|
137
|
-
// Redefine the mile (would not be the first time in history)
|
138
|
-
math.createUnit('mile', '1609.347218694 m', {override: true})
|
139
|
-
```
|
140
|
-
Base units created without specifying a definition cannot be overridden.
|
141
|
-
|
142
|
-
### Create several units at once
|
143
|
-
Multiple units can defined using a single call to `createUnit` by passing an object map as the first argument, where each key in the object is the name of a new unit and the value is either a string defining the unit, or an object with the configuration properties listed above. If the value is an empty string or an object lacking a definition property, a new base unit is created.
|
144
|
-
|
145
|
-
For example:
|
146
|
-
|
147
|
-
```js
|
148
|
-
math.createUnit( {
|
149
|
-
foo: {
|
150
|
-
prefixes: 'long',
|
151
|
-
baseName: 'essence-of-foo'
|
152
|
-
},
|
153
|
-
bar: '40 foo',
|
154
|
-
baz: {
|
155
|
-
definition: '1 bar/hour',
|
156
|
-
prefixes: 'long'
|
157
|
-
}
|
158
|
-
},
|
159
|
-
{
|
160
|
-
override: true
|
161
|
-
})
|
162
|
-
math.evaluate('50000 kilofoo/s') // 4.5 gigabaz
|
163
|
-
```
|
164
|
-
|
165
|
-
### Return Value
|
166
|
-
`createUnit` returns the created unit, or, when multiple units are created, the last unit created. Since `createUnit` is also compatible with the expression parser, this allows you to do things like this:
|
167
|
-
|
168
|
-
```js
|
169
|
-
math.evaluate('45 mile/hour to createUnit("knot", "0.514444m/s")')
|
170
|
-
// 39.103964668651976 knot
|
171
|
-
```
|
172
|
-
|
173
|
-
### Support of custom characters in unit names
|
174
|
-
Per default, the name of a new unit:
|
175
|
-
- should start by a latin (A-Z or a-z) character
|
176
|
-
- should contain only numeric (0-9) or latin characters
|
177
|
-
|
178
|
-
It is possible to allow the usage of special characters (such as Greek alphabet, cyrillic alphabet, any Unicode symbols, etc.) by overriding the `Unit.isValidAlpha` static method. For example:
|
179
|
-
```js
|
180
|
-
const isAlphaOriginal = math.Unit.isValidAlpha
|
181
|
-
const isGreekLowercaseChar = function (c) {
|
182
|
-
const charCode = c.charCodeAt(0)
|
183
|
-
return charCode > 944 && charCode < 970
|
184
|
-
}
|
185
|
-
math.Unit.isValidAlpha = function (c) {
|
186
|
-
return isAlphaOriginal(c) || isGreekLowercaseChar(c)
|
187
|
-
}
|
188
|
-
|
189
|
-
math.createUnit('θ', '1 rad')
|
190
|
-
math.evaluate('1θ + 3 deg').toNumber('deg') // 60.29577951308232
|
191
|
-
```
|
192
|
-
|
193
|
-
## API
|
194
|
-
A `Unit` object contains the following functions:
|
195
|
-
|
196
|
-
### unit.clone()
|
197
|
-
Clone the unit, returns a new unit with the same parameters.
|
198
|
-
|
199
|
-
### unit.equalBase(unit)
|
200
|
-
Test whether a unit has the same base as an other unit:
|
201
|
-
length, mass, etc.
|
202
|
-
|
203
|
-
### unit.equals(unit)
|
204
|
-
Test whether a unit equals an other unit. Units are equal
|
205
|
-
when they have the same base and same value when normalized to SI units.
|
206
|
-
|
207
|
-
### unit.format([options])
|
208
|
-
Get a string representation of the unit. The function
|
209
|
-
will determine the best fitting prefix for the unit. See the [Format](../reference/functions/format.md)
|
210
|
-
page for available options.
|
211
|
-
|
212
|
-
### unit.fromJSON(json)
|
213
|
-
Revive a unit from a JSON object. Accepts
|
214
|
-
An object `{mathjs: 'Unit', value: number, unit: string, fixPrefix: boolean}`,
|
215
|
-
where the property `mathjs` and `fixPrefix` are optional.
|
216
|
-
Used when deserializing a unit, see [Serialization](../core/serialization.md).
|
217
|
-
|
218
|
-
### unit.splitUnit(parts)
|
219
|
-
Split a unit into the specified parts. For example:
|
220
|
-
|
221
|
-
```js
|
222
|
-
const u = math.unit(1, 'm')
|
223
|
-
u.splitUnit(['ft', 'in']) // 3 feet,3.3700787401574765 inch
|
224
|
-
```
|
225
|
-
|
226
|
-
### unit.to(unitName)
|
227
|
-
Convert the unit to a specific unit name. Returns a clone of
|
228
|
-
the unit with a fixed prefix and unit.
|
229
|
-
|
230
|
-
### unit.toJSON()
|
231
|
-
Returns a JSON representation of the unit, with signature
|
232
|
-
`{mathjs: 'Unit', value: number, unit: string, fixPrefix: boolean}`.
|
233
|
-
Used when serializing a unit, see [Serialization](../core/serialization.md).
|
234
|
-
|
235
|
-
### unit.toNumber(unitName)
|
236
|
-
Get the value of a unit when converted to the
|
237
|
-
specified unit (a unit with optional prefix but without value).
|
238
|
-
The type of the returned value is always `number`.
|
239
|
-
|
240
|
-
### unit.toNumeric(unitName)
|
241
|
-
Get the value of a unit when converted to the
|
242
|
-
specified unit (a unit with optional prefix but without value).
|
243
|
-
The type of the returned value depends on how the unit was created and
|
244
|
-
can be `number`, `Fraction`, or `BigNumber`.
|
245
|
-
|
246
|
-
### unit.toSI()
|
247
|
-
Returns a clone of a unit represented in SI units. Works with units with or without a value.
|
248
|
-
|
249
|
-
### unit.toString()
|
250
|
-
Get a string representation of the unit. The function will
|
251
|
-
determine the best fitting prefix for the unit.
|
252
|
-
|
253
|
-
## Unit reference
|
254
|
-
|
255
|
-
This section lists all available units, prefixes, and physical constants. These can be used via the Unit object, or via `math.evaluate()`.
|
256
|
-
|
257
|
-
## Reference
|
258
|
-
|
259
|
-
Math.js comes with the following built-in units.
|
260
|
-
|
261
|
-
Base | Unit
|
262
|
-
------------------- | ---
|
263
|
-
Length | meter (m), inch (in), foot (ft), yard (yd), mile (mi), link (li), rod (rd), chain (ch), angstrom, mil
|
264
|
-
Surface area | m2, sqin, sqft, sqyd, sqmi, sqrd, sqch, sqmil, acre, hectare
|
265
|
-
Volume | m3, litre (l, L, lt, liter), cc, cuin, cuft, cuyd, teaspoon, tablespoon
|
266
|
-
Liquid volume | minim (min), fluiddram (fldr), fluidounce (floz), gill (gi), cup (cp), pint (pt), quart (qt), gallon (gal), beerbarrel (bbl), oilbarrel (obl), hogshead, drop (gtt)
|
267
|
-
Angles | rad (radian), deg (degree), grad (gradian), cycle, arcsec (arcsecond), arcmin (arcminute)
|
268
|
-
Time | second (s, secs, seconds), minute (mins, minutes), hour (h, hr, hrs, hours), day (days), week (weeks), month (months), year (years), decade (decades), century (centuries), millennium (millennia)
|
269
|
-
Frequency | hertz (Hz)
|
270
|
-
Mass | gram(g), tonne, ton, grain (gr), dram (dr), ounce (oz), poundmass (lbm, lb, lbs), hundredweight (cwt), stick, stone
|
271
|
-
Electric current | ampere (A)
|
272
|
-
Temperature | kelvin (K), celsius (degC), fahrenheit (degF), rankine (degR)
|
273
|
-
Amount of substance | mole (mol)
|
274
|
-
Luminous intensity | candela (cd)
|
275
|
-
Force | newton (N), dyne (dyn), poundforce (lbf), kip
|
276
|
-
Energy | joule (J), erg, Wh, BTU, electronvolt (eV)
|
277
|
-
Power | watt (W), hp
|
278
|
-
Pressure | Pa, psi, atm, torr, bar, mmHg, mmH2O, cmH2O
|
279
|
-
Electricity and magnetism | ampere (A), coulomb (C), watt (W), volt (V), ohm, farad (F), weber (Wb), tesla (T), henry (H), siemens (S), electronvolt (eV)
|
280
|
-
Binary | bits (b), bytes (B)
|
281
|
-
|
282
|
-
Note: all time units are based on the Julian year, with one month being 1/12th of a Julian year, a year being one Julian year, a decade being 10 Julian years, a century being 100, and a millennium being 1000.
|
283
|
-
|
284
|
-
Note that all relevant units can also be written in plural form, for example `5 meters` instead of `5 meter` or `10 seconds` instead of `10 second`.
|
285
|
-
|
286
|
-
Surface and volume units can alternatively be expressed in terms of length units raised to a power, for example `100 in^2` instead of `100 sqin`.
|
287
|
-
|
288
|
-
### Prefixes
|
289
|
-
|
290
|
-
The following decimal prefixes are available.
|
291
|
-
|
292
|
-
Name | Abbreviation | Value
|
293
|
-
------- | ------------- | -----
|
294
|
-
deca | da | 1e1
|
295
|
-
hecto | h | 1e2
|
296
|
-
kilo | k | 1e3
|
297
|
-
mega | M | 1e6
|
298
|
-
giga | G | 1e9
|
299
|
-
tera | T | 1e12
|
300
|
-
peta | P | 1e15
|
301
|
-
exa | E | 1e18
|
302
|
-
zetta | Z | 1e21
|
303
|
-
yotta | Y | 1e24
|
304
|
-
|
305
|
-
Name | Abbreviation | Value
|
306
|
-
------ | ------------- | -----
|
307
|
-
deci | d | 1e-1
|
308
|
-
centi | c | 1e-2
|
309
|
-
milli | m | 1e-3
|
310
|
-
micro | u | 1e-6
|
311
|
-
nano | n | 1e-9
|
312
|
-
pico | p | 1e-12
|
313
|
-
femto | f | 1e-15
|
314
|
-
atto | a | 1e-18
|
315
|
-
zepto | z | 1e-21
|
316
|
-
yocto | y | 1e-24
|
317
|
-
|
318
|
-
The following binary prefixes are available.
|
319
|
-
They can be used with units `bits` (`b`) and `bytes` (`B`).
|
320
|
-
|
321
|
-
Name | Abbreviation | Value
|
322
|
-
---- | ------------ | -----
|
323
|
-
kibi | Ki | 1024
|
324
|
-
mebi | Mi | 1024^2
|
325
|
-
gibi | Gi | 1024^3
|
326
|
-
tebi | Ti | 1024^4
|
327
|
-
pebi | Pi | 1024^5
|
328
|
-
exi | Ei | 1024^6
|
329
|
-
zebi | Zi | 1024^7
|
330
|
-
yobi | Yi | 1024^8
|
331
|
-
|
332
|
-
Name | Abbreviation | Value
|
333
|
-
----- | ------------ | -----
|
334
|
-
kilo | k | 1e3
|
335
|
-
mega | M | 1e6
|
336
|
-
giga | G | 1e9
|
337
|
-
tera | T | 1e12
|
338
|
-
peta | P | 1e15
|
339
|
-
exa | E | 1e18
|
340
|
-
zetta | Z | 1e21
|
341
|
-
yotta | Y | 1e24
|
342
|
-
|
343
|
-
|
344
|
-
### Physical Constants
|
345
|
-
|
346
|
-
Math.js includes the following physical constants. See [Wikipedia](https://en.wikipedia.org/wiki/Physical_constants) for more information.
|
347
|
-
|
348
|
-
|
349
|
-
#### Universal constants
|
350
|
-
|
351
|
-
Name | Symbol | Value | Unit
|
352
|
-
----------------------|--------------------------------------------------------|-------------------|-------------------------------------------------------
|
353
|
-
speedOfLight | <i>c</i> | 299792458 | m · s<sup>-1</sup>
|
354
|
-
gravitationConstant | <i>G</i> | 6.6738480e-11 | m<sup>3</sup> · kg<sup>-1</sup> · s<sup>-2</sup>
|
355
|
-
planckConstant | <i>h</i> | 6.626069311e-34 | J · s
|
356
|
-
reducedPlanckConstant | <i><span style="text-decoration:overline">h</span></i> | 1.05457172647e-34 | J · s
|
357
|
-
|
358
|
-
|
359
|
-
#### Electromagnetic constants
|
360
|
-
|
361
|
-
Name | Symbol | Value | Unit
|
362
|
-
--------------------------|--------------------------------------------------|-----------------------|----------------------------------------
|
363
|
-
magneticConstant | <i>μ<sub>0</sub></i> | 1.2566370614e-6 | N · A<sup>-2</sup>
|
364
|
-
electricConstant | <i>ε<sub>0</sub></i> | 8.854187817e-12 | F · m<sup>-1</sup>
|
365
|
-
vacuumImpedance | <i>Z<sub>0</sub></i> | 376.730313461 | Ω
|
366
|
-
coulomb | <i>κ</i> | 8.9875517873681764e9 | N · m<sup>2</sup> · C<sup>-2</sup>
|
367
|
-
elementaryCharge | <i>e</i> | 1.60217656535e-19 | C
|
368
|
-
bohrMagneton | <i>μ<sub>B</sub></i> | 9.2740096820e-24 | J · T<sup>-1</sup>
|
369
|
-
conductanceQuantum | <i>G<sub>0</sub></i> | 7.748091734625e-5 | S
|
370
|
-
inverseConductanceQuantum | <i>G<sub>0</sub><sup>-1</sup></i> | 12906.403721742 | Ω
|
371
|
-
magneticFluxQuantum | <i><font face="Symbol">f</font><sub>0</sub></i> | 2.06783375846e-15 | Wb
|
372
|
-
nuclearMagneton | <i>μ<sub>N</sub></i> | 5.0507835311e-27 | J · T<sup>-1</sup>
|
373
|
-
klitzing | <i>R<sub>K</sub></i> | 25812.807443484 | Ω
|
374
|
-
|
375
|
-
<!-- TODO: implement josephson
|
376
|
-
josephson | <i>K<sub>J</sub></i> | 4.8359787011e-14 | Hz · V<sup>-1</sup>
|
377
|
-
-->
|
378
|
-
|
379
|
-
|
380
|
-
#### Atomic and nuclear constants
|
381
|
-
|
382
|
-
Name | Symbol | Value | Unit
|
383
|
-
------------------------|------------------------------|-----------------------|----------------------------------
|
384
|
-
bohrRadius | <i>a<sub>0</sub></i> | 5.291772109217e-11 | m
|
385
|
-
classicalElectronRadius | <i>r<sub>e</sub></i> | 2.817940326727e-15 | m
|
386
|
-
electronMass | <i>m<sub>e</sub></i> | 9.1093829140e-31 | kg
|
387
|
-
fermiCoupling | <i>G<sub>F</sub></i> | 1.1663645e-5 | GeV<sup>-2</sup>
|
388
|
-
fineStructure | <i>α</i> | 7.297352569824e-3 | -
|
389
|
-
hartreeEnergy | <i>E<abbr>h</abbr> </i> | 4.3597443419e-18 | J
|
390
|
-
protonMass | <i>m<sub>p</sub></i> | 1.67262177774e-27 | kg
|
391
|
-
deuteronMass | <i>m<sub>d</sub></i> | 3.3435830926e-27 | kg
|
392
|
-
neutronMass | <i>m<sub>n</sub></i> | 1.6749271613e-27 | kg
|
393
|
-
quantumOfCirculation | <i>h / (2m<sub>e</sub>)</i> | 3.636947552024e-4 | m<sup>2</sup> · s<sup>-1</sup>
|
394
|
-
rydberg | <i>R<sub>∞</sub></i> | 10973731.56853955 | m<sup>-1</sup>
|
395
|
-
thomsonCrossSection | | 6.65245873413e-29 | m<sup>2</sup>
|
396
|
-
weakMixingAngle | | 0.222321 | -
|
397
|
-
efimovFactor | | 22.7 | -
|
398
|
-
|
399
|
-
|
400
|
-
#### Physico-chemical constants
|
401
|
-
|
402
|
-
Name | Symbol | Value | Unit
|
403
|
-
--------------------|------------------------------|---------------------|--------------------------------------------
|
404
|
-
atomicMass | <i>m<sub>u</sub></i> | 1.66053892173e-27 | kg
|
405
|
-
avogadro | <i>N<sub>A</sub></i> | 6.0221412927e23 | mol<sup>-1</sup>
|
406
|
-
boltzmann | <i>k</i> | 1.380648813e-23 | J · K<sup>-1</sup>
|
407
|
-
faraday | <i>F</i> | 96485.336521 | C · mol<sup>-1</sup>
|
408
|
-
firstRadiation | <i>c<sub>1</sub></i> | 3.7417715317e-16 | W · m<sup>2</sup>
|
409
|
-
loschmidt | <i>n<sub>0</sub></i> | 2.686780524e25 | m<sup>-3</sup>
|
410
|
-
gasConstant | <i>R</i> | 8.314462175 | J · K<sup>-1</sup> · mol<sup>-1</sup>
|
411
|
-
molarPlanckConstant | <i>N<sub>A</sub> · h</i>| 3.990312717628e-10| J · s · mol<sup>-1</sup>
|
412
|
-
molarVolume | <i>V<sub>m</sub></i> | 2.241396820e-10 | m<sup>3</sup> · mol<sup>-1</sup>
|
413
|
-
sackurTetrode | | -1.164870823 | -
|
414
|
-
secondRadiation | <i>c<sub>2</sub></i> | 1.438777013e-2 | m · K
|
415
|
-
stefanBoltzmann | <i>σ</i> | 5.67037321e-8 | W · m<sup>-2</sup> · K<sup>-4</sup>
|
416
|
-
wienDisplacement | <i>b</i> | 2.897772126e-3 | m · K
|
417
|
-
|
418
|
-
<!-- TODO: implement spectralRadiance
|
419
|
-
spectralRadiance | <i>c<sub>1L</sub></i> | 1.19104286953e-16 | W · m<sup>2</sup> · sr<sup>-1</sup>
|
420
|
-
-->
|
421
|
-
|
422
|
-
Note that the values of `loschmidt` and `molarVolume` are at `T = 273.15 K` and `p = 101.325 kPa`.
|
423
|
-
The value of `sackurTetrode` is at `T = 1 K` and `p = 101.325 kPa`.
|
424
|
-
|
425
|
-
|
426
|
-
#### Adopted values
|
427
|
-
|
428
|
-
Name | Symbol | Value | Unit
|
429
|
-
--------------|------------------------------|---------|-------------------------
|
430
|
-
molarMass | <i>M<sub>u</sub></i> | 1e-3 | kg · mol<sup>-1</sup>
|
431
|
-
molarMassC12 | <i>M(<sub>12</sub>C)</i> | 1.2e-2 | kg · mol<sup>-1</sup>
|
432
|
-
gravity | <i>g<sub>n</sub></i> | 9.80665 | m · s<sup>-2</sup>
|
433
|
-
atm | <i>atm</i> | 101325 | Pa
|
434
|
-
|
435
|
-
|
436
|
-
#### Natural units
|
437
|
-
|
438
|
-
Name | Symbol | Value | Unit
|
439
|
-
------------------|-----------------------|--------------------|-----
|
440
|
-
planckLength | <i>l<sub>P</sub></i> | 1.61619997e-35 | m
|
441
|
-
planckMass | <i>m<sub>P</sub></i> | 2.1765113e-8 | kg
|
442
|
-
planckTime | <i>t<sub>P</sub></i> | 5.3910632e-44 | s
|
443
|
-
planckCharge | <i>q<sub>P</sub></i> | 1.87554595641e-18 | C
|
444
|
-
planckTemperature | <i>T<sub>P</sub></i> | 1.41683385e+32 | K
|
@@ -1,110 +0,0 @@
|
|
1
|
-
# Algebra (symbolic computation)
|
2
|
-
|
3
|
-
math.js has built-in support for symbolic computation ([CAS](https://www.wikiwand.com/en/Computer_algebra_system)). It can parse expressions into an expression tree and do algebraic operations like simplification and derivation on the tree.
|
4
|
-
|
5
|
-
> It's worth mentioning an excellent extension on math.js here: [mathsteps](https://github.com/socraticorg/mathsteps), a step-by-step math solver library that is focused on pedagogy (how best to teach). The math problems it focuses on are pre-algebra and algebra problems involving simplifying expressions.
|
6
|
-
|
7
|
-
|
8
|
-
## Simplify
|
9
|
-
|
10
|
-
The function [`math.simplify`](../reference/functions/simplify.md) simplifies an expression tree:
|
11
|
-
|
12
|
-
```js
|
13
|
-
// simplify an expression
|
14
|
-
console.log(math.simplify('3 + 2 / 4').toString()) // '7 / 2'
|
15
|
-
console.log(math.simplify('2x + 3x').toString()) // '5 * x'
|
16
|
-
console.log(math.simplify('x^2 + x + 3 + x^2').toString()) // '2 * x ^ 2 + x + 3'
|
17
|
-
console.log(math.simplify('x * y * -x / (x ^ 2)').toString()) // '-y'
|
18
|
-
```
|
19
|
-
|
20
|
-
The function accepts either a string or an expression tree (`Node`) as input, and outputs a simplified expression tree (`Node`). This node tree can be transformed and evaluated as described in detail on the page [Expression trees](expression_trees.md).
|
21
|
-
|
22
|
-
```js
|
23
|
-
// work with an expression tree, evaluate results
|
24
|
-
const f = math.parse('2x + x')
|
25
|
-
const simplified = math.simplify(f)
|
26
|
-
console.log(simplified.toString()) // '3 * x'
|
27
|
-
console.log(simplified.evaluate({x: 4})) // 12
|
28
|
-
```
|
29
|
-
|
30
|
-
Note that `simplify` has an optional argument `scope` that allows the definitions of variables in the expression (as numeric values, or as further expressions) to be specified and used in the simplification, e.g. continuing the previous example,
|
31
|
-
|
32
|
-
```js
|
33
|
-
console.log(math.simplify(f, {x: 4}).toString()) // 12
|
34
|
-
console.log(math.simplify(f, {x: math.parse('y+z')}).toString()) // '3*(y+z)'
|
35
|
-
```
|
36
|
-
|
37
|
-
In general, simplification is an inherently dfficult problem; in fact, for certain classes of expressions and algebraic equivalences, it is undecidable whether a given expression is equivalent to zero. Moreover, simplification generally depends on the properties of the operations involved; since multiplication (for example) may have different properties (e.g., it might or might not be commutative) depending on the domain under consideration, different simplifications might be appropriate.
|
38
|
-
|
39
|
-
As a result, `simplify()` has an additional optional argument, `options`, which controls its behavior. This argument is an object specifying any of various properties concerning the simplification process. See the [detailed documentation](../reference/functions/simplify.md) for a complete list, but currently the two most important properties are as follows. Note that the `options` argument may only be specified if the `scope` is as well.
|
40
|
-
|
41
|
-
- `exactFractions` - a boolean which specifies whether non-integer numerical constants should be simplified to rational numbers when possible (true), or always converted to decimal notation (false).
|
42
|
-
- `context` - an object whose keys are the names of operations ('add', 'multiply', etc.) and whose values specify algebraic properties of the corresponding operation (currently any of 'total', 'trivial', 'commutative', and 'associative'). Simplifications will only be performed if the properties they rely on are true in the given context. For example,
|
43
|
-
```js
|
44
|
-
const expr = math.parse('x*y-y*x')
|
45
|
-
console.log(math.simplify(expr).toString()) // 0; * is commutative by default
|
46
|
-
console.log(math.simplify(expr, {}, {context: {multiply: {commutative: false}}}))
|
47
|
-
// 'x*y-y*x'; the order of the right multiplication can't be reversed.
|
48
|
-
```
|
49
|
-
|
50
|
-
Note that the default context is very permissive (allows a lot of simplifications) but that there is also a `math.simplify.realContext` that only allows simplifications that are guaranteed to preserve the value of the expression on all real numbers:
|
51
|
-
```js
|
52
|
-
const rational = math.parse('(x-1)*x/(x-1)')
|
53
|
-
console.log(math.simplify(expr, {}, {context: math.simplify.realContext})
|
54
|
-
// '(x-1)*x/(x-1)'; canceling the 'x-1' makes the expression defined at 1
|
55
|
-
```
|
56
|
-
|
57
|
-
For more details on the theory of expression simplification, see:
|
58
|
-
|
59
|
-
- [Strategies for simplifying math expressions (Stackoverflow)](https://stackoverflow.com/questions/7540227/strategies-for-simplifying-math-expressions)
|
60
|
-
- [Symbolic computation - Simplification (Wikipedia)](https://en.wikipedia.org/wiki/Symbolic_computation#Simplification)
|
61
|
-
|
62
|
-
|
63
|
-
## Derivative
|
64
|
-
|
65
|
-
The function [`math.derivative`](../reference/functions/derivative.md) finds the symbolic derivative of an expression:
|
66
|
-
|
67
|
-
```js
|
68
|
-
// calculate a derivative
|
69
|
-
console.log(math.derivative('2x^2 + 3x + 4', 'x').toString()) // '4 * x + 3'
|
70
|
-
console.log(math.derivative('sin(2x)', 'x').toString()) // '2 * cos(2 * x)'
|
71
|
-
```
|
72
|
-
|
73
|
-
Similar to the function `math.simplify`, `math.derivative` accepts either a string or an expression tree (`Node`) as input, and outputs a simplified expression tree (`Node`).
|
74
|
-
|
75
|
-
```js
|
76
|
-
// work with an expression tree, evaluate results
|
77
|
-
const h = math.parse('x^2 + x')
|
78
|
-
const x = math.parse('x')
|
79
|
-
const dh = math.derivative(h, x)
|
80
|
-
console.log(dh.toString()) // '2 * x + 1'
|
81
|
-
console.log(dh.evaluate({x: 3})) // '7'
|
82
|
-
```
|
83
|
-
|
84
|
-
The rules used by `math.derivative` can be found on Wikipedia:
|
85
|
-
|
86
|
-
- [Differentiation rules (Wikipedia)](https://en.wikipedia.org/wiki/Differentiation_rules)
|
87
|
-
|
88
|
-
|
89
|
-
## Rationalize
|
90
|
-
|
91
|
-
The function [`math.transform`](../reference/functions/transform.md) transforms a rationalizable expression in a rational fraction.
|
92
|
-
If rational fraction is one variable polynomial then converts the numerator and denominator in canonical form, with decreasing exponents, returning the coefficients of numerator.
|
93
|
-
|
94
|
-
```js
|
95
|
-
|
96
|
-
math.rationalize('2x/y - y/(x+1)')
|
97
|
-
// (2*x^2-y^2+2*x)/(x*y+y)
|
98
|
-
math.rationalize('(2x+1)^6')
|
99
|
-
// 64*x^6+192*x^5+240*x^4+160*x^3+60*x^2+12*x+1
|
100
|
-
math.rationalize('2x/( (2x-1) / (3x+2) ) - 5x/ ( (3x+4) / (2x^2-5) ) + 3')
|
101
|
-
// -20*x^4+28*x^3+104*x^2+6*x-12)/(6*x^2+5*x-4)
|
102
|
-
|
103
|
-
math.rationalize('x+x+x+y',{y:1}) // 3*x+1
|
104
|
-
math.rationalize('x+x+x+y',{}) // 3*x+y
|
105
|
-
|
106
|
-
const ret = math.rationalize('x+x+x+y',{},true)
|
107
|
-
// ret.expression=3*x+y, ret.variables = ["x","y"]
|
108
|
-
const ret = math.rationalize('-2+5x^2',{},true)
|
109
|
-
// ret.expression=5*x^2-2, ret.variables = ["x"], ret.coefficients=[-2,0,5]
|
110
|
-
```
|