1 |
|
2 |
|
3 | # Function compile
|
4 |
|
5 | Parse and compile an expression.
|
6 | Returns a an object with a function `evaluate([scope])` to evaluate the
|
7 | compiled expression.
|
8 |
|
9 |
|
10 | ## Syntax
|
11 |
|
12 | ```js
|
13 | math.compile(expr) // returns one node
|
14 | math.compile([expr1, expr2, expr3, ...]) // returns an array with nodes
|
15 | ```
|
16 |
|
17 | ### Parameters
|
18 |
|
19 | Parameter | Type | Description
|
20 | --------- | ---- | -----------
|
21 | `expr` | string | string[] | Array | Matrix | The expression to be compiled
|
22 |
|
23 | ### Returns
|
24 |
|
25 | Type | Description
|
26 | ---- | -----------
|
27 | {evaluate: Function} | Array.<{evaluate: Function}> | code An object with the compiled expression
|
28 |
|
29 |
|
30 | ## Examples
|
31 |
|
32 | ```js
|
33 | const code1 = math.compile('sqrt(3^2 + 4^2)')
|
34 | code1.evaluate() // 5
|
35 |
|
36 | let scope = {a: 3, b: 4}
|
37 | const code2 = math.compile('a * b') // 12
|
38 | code2.evaluate(scope) // 12
|
39 | scope.a = 5
|
40 | code2.evaluate(scope) // 20
|
41 |
|
42 | const nodes = math.compile(['a = 3', 'b = 4', 'a * b'])
|
43 | nodes[2].evaluate() // 12
|
44 | ```
|
45 |
|
46 |
|
47 | ## See also
|
48 |
|
49 | [parse](parse.md),
|
50 | [evaluate](evaluate.md)
|