UNPKG

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