UNPKG

2.94 kBMarkdownView Raw
1<!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. -->
2
3# Function simplify
4
5Simplify an expression tree.
6
7A list of rules are applied to an expression, repeating over the list until
8no further changes are made.
9It's possible to pass a custom set of rules to the function as second
10argument. A rule can be specified as an object, string, or function:
11
12 const rules = [
13 { l: 'n1*n3 + n2*n3', r: '(n1+n2)*n3' },
14 'n1*n3 + n2*n3 -> (n1+n2)*n3',
15 function (node) {
16 // ... return a new node or return the node unchanged
17 return node
18 }
19 ]
20
21String and object rules consist of a left and right pattern. The left is
22used to match against the expression and the right determines what matches
23are replaced with. The main difference between a pattern and a normal
24expression is that variables starting with the following characters are
25interpreted as wildcards:
26
27- 'n' - matches any Node
28- 'c' - matches any ConstantNode
29- 'v' - matches any Node that is not a ConstantNode
30
31The default list of rules is exposed on the function as `simplify.rules`
32and can be used as a basis to built a set of custom rules.
33
34For more details on the theory, see:
35
36- [Strategies for simplifying math expressions (Stackoverflow)](https://stackoverflow.com/questions/7540227/strategies-for-simplifying-math-expressions)
37- [Symbolic computation - Simplification (Wikipedia)](https://en.wikipedia.org/wiki/Symbolic_computation#Simplification)
38
39 An optional `options` argument can be passed as last argument of `simplify`.
40 There is currently one option available:
41 - `exactFractions`: a boolean which is `true` by default.
42 - `fractionsLimit`: when `exactFractions` is true, a fraction will be returned
43 only when both numerator and denominator are smaller than `fractionsLimit`.
44 Default value is 10000.
45
46
47## Syntax
48
49```js
50simplify(expr)
51simplify(expr, rules)
52simplify(expr, rules)
53simplify(expr, rules, scope)
54simplify(expr, rules, scope, options)
55simplify(expr, scope)
56simplify(expr, scope, options)
57```
58
59### Parameters
60
61Parameter | Type | Description
62--------- | ---- | -----------
63`expr` | Node &#124; string | The expression to be simplified
64`rules` | Array&lt;{l:string, r: string} &#124; string &#124; function&gt; | Optional list with custom rules
65
66### Returns
67
68Type | Description
69---- | -----------
70Node | Returns the simplified form of `expr`
71
72
73## Examples
74
75```js
76math.simplify('2 * 1 * x ^ (2 - 1)') // Node "2 * x"
77math.simplify('2 * 3 * x', {x: 4}) // Node "24"
78const f = math.parse('2 * 1 * x ^ (2 - 1)')
79math.simplify(f) // Node "2 * x"
80math.simplify('0.4 * x', {}, {exactFractions: true}) // Node "x * 2 / 5"
81math.simplify('0.4 * x', {}, {exactFractions: false}) // Node "0.4 * x"
82```
83
84
85## See also
86
87[derivative](derivative.md),
88[parse](parse.md),
89[evaluate](evaluate.md),
90[rationalize](rationalize.md)