compile.md (1190B)
1 <!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. --> 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 ### Throws 31 32 Type | Description 33 ---- | ----------- 34 Error | 35 36 ## Examples 37 38 ```js 39 const code1 = math.compile('sqrt(3^2 + 4^2)') 40 code1.evaluate() // 5 41 42 let scope = {a: 3, b: 4} 43 const code2 = math.compile('a * b') // 12 44 code2.evaluate(scope) // 12 45 scope.a = 5 46 code2.evaluate(scope) // 20 47 48 const nodes = math.compile(['a = 3', 'b = 4', 'a * b']) 49 nodes[2].evaluate() // 12 50 ``` 51 52 53 ## See also 54 55 [parse](parse.md), 56 [evaluate](evaluate.md)