simple-squiggle

A restricted subset of Squiggle
Log | Files | Refs | README

simplify.md (5060B)


      1 <!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. -->
      2 
      3 # Function simplify
      4 
      5 Simplify an expression tree.
      6 
      7 A list of rules are applied to an expression, repeating over the list until
      8 no further changes are made.
      9 It's possible to pass a custom set of rules to the function as second
     10 argument. 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 
     21 String and object rules consist of a left and right pattern. The left is
     22 used to match against the expression and the right determines what matches
     23 are replaced with. The main difference between a pattern and a normal
     24 expression is that variables starting with the following characters are
     25 interpreted as wildcards:
     26 
     27 - 'n' - matches any Node
     28 - 'c' - matches any ConstantNode
     29 - 'v' - matches any Node that is not a ConstantNode
     30 
     31 The default list of rules is exposed on the function as `simplify.rules`
     32 and can be used as a basis to built a set of custom rules.
     33 
     34 To specify a rule as a string, separate the left and right pattern by '->'
     35 When specifying a rule as an object, the following keys are meaningful:
     36 - l - the left pattern
     37 - r - the right pattern
     38 - s - in lieu of l and r, the string form that is broken at -> to give them
     39 - repeat - whether to repeat this rule until the expression stabilizes
     40 - assuming - gives a context object, as in the 'context' option to
     41     simplify. Every property in the context object must match the current
     42     context in order, or else the rule will not be applied.
     43 - imposeContext - gives a context object, as in the 'context' option to
     44     simplify. Any settings specified will override the incoming context
     45     for all matches of this rule.
     46 
     47 For more details on the theory, see:
     48 
     49 - [Strategies for simplifying math expressions (Stackoverflow)](https://stackoverflow.com/questions/7540227/strategies-for-simplifying-math-expressions)
     50 - [Symbolic computation - Simplification (Wikipedia)](https://en.wikipedia.org/wiki/Symbolic_computation#Simplification)
     51 
     52  An optional `options` argument can be passed as last argument of `simplify`.
     53  Currently available options (defaults in parentheses):
     54  - `consoleDebug` (false): whether to write the expression being simplified
     55    and any changes to it, along with the rule responsible, to console
     56  - `context` (simplify.defaultContext): an object giving properties of
     57    each operator, which determine what simplifications are allowed. The
     58    currently meaningful properties are commutative, associative,
     59    total (whether the operation is defined for all arguments), and
     60    trivial (whether the operation applied to a single argument leaves
     61    that argument unchanged). The default context is very permissive and
     62    allows almost all simplifications. Only properties differing from
     63    the default need to be specified; the default context is used as a
     64    fallback. Additional contexts `simplify.realContext` and
     65    `simplify.positiveContext` are supplied to cause simplify to perform
     66    just simplifications guaranteed to preserve all values of the expression
     67    assuming all variables and subexpressions are real numbers or
     68    positive real numbers, respectively. (Note that these are in some cases
     69    more restrictive than the default context; for example, the default
     70    context will allow `x/x` to simplify to 1, whereas
     71    `simplify.realContext` will not, as `0/0` is not equal to 1.)
     72  - `exactFractions` (true): whether to try to convert all constants to
     73    exact rational numbers.
     74  - `fractionsLimit` (10000): when `exactFractions` is true, constants will
     75    be expressed as fractions only when both numerator and denominator
     76    are smaller than `fractionsLimit`.
     77 
     78 
     79 ## Syntax
     80 
     81 ```js
     82 simplify(expr)
     83 simplify(expr, rules)
     84 simplify(expr, rules)
     85 simplify(expr, rules, scope)
     86 simplify(expr, rules, scope, options)
     87 simplify(expr, scope)
     88 simplify(expr, scope, options)
     89 ```
     90 
     91 ### Parameters
     92 
     93 Parameter | Type | Description
     94 --------- | ---- | -----------
     95 `expr` | Node &#124; string |  The expression to be simplified
     96 `rules` | Array&lt;{l:string, r: string} &#124; string &#124; function&gt; |  Optional list with custom rules
     97 
     98 ### Returns
     99 
    100 Type | Description
    101 ---- | -----------
    102 Node | Returns the simplified form of `expr`
    103 
    104 
    105 ### Throws
    106 
    107 Type | Description
    108 ---- | -----------
    109 
    110 
    111 ## Examples
    112 
    113 ```js
    114 math.simplify('2 * 1 * x ^ (2 - 1)')      // Node "2 * x"
    115 math.simplify('2 * 3 * x', {x: 4})        // Node "24"
    116 const f = math.parse('2 * 1 * x ^ (2 - 1)')
    117 math.simplify(f)                          // Node "2 * x"
    118 math.simplify('0.4 * x', {}, {exactFractions: true})  // Node "x * 2 / 5"
    119 math.simplify('0.4 * x', {}, {exactFractions: false}) // Node "0.4 * x"
    120 ```
    121 
    122 
    123 ## See also
    124 
    125 [simplifyCore](simplifyCore.md),
    126 [derivative](derivative.md),
    127 [evaluate](evaluate.md),
    128 [parse](parse.md),
    129 [rationalize](rationalize.md),
    130 [resolve](resolve.md)