2022-03-01 13:04:09 +01:00

61 lines
1.6 KiB
Markdown

---
layout: default
---
<!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. -->
<h1 id="function-compile">Function compile <a href="#function-compile" title="Permalink">#</a></h1>
Parse and compile an expression.
Returns a an object with a function `evaluate([scope])` to evaluate the
compiled expression.
<h2 id="syntax">Syntax <a href="#syntax" title="Permalink">#</a></h2>
```js
math.compile(expr) // returns one node
math.compile([expr1, expr2, expr3, ...]) // returns an array with nodes
```
<h3 id="parameters">Parameters <a href="#parameters" title="Permalink">#</a></h3>
Parameter | Type | Description
--------- | ---- | -----------
`expr` | string &#124; string[] &#124; Array &#124; Matrix | The expression to be compiled
<h3 id="returns">Returns <a href="#returns" title="Permalink">#</a></h3>
Type | Description
---- | -----------
{evaluate: Function} &#124; Array.&lt;{evaluate: Function}&gt; | code An object with the compiled expression
<h3 id="throws">Throws <a href="#throws" title="Permalink">#</a></h3>
Type | Description
---- | -----------
Error |
<h2 id="examples">Examples <a href="#examples" title="Permalink">#</a></h2>
```js
const code1 = math.compile('sqrt(3^2 + 4^2)')
code1.evaluate() // 5
let scope = {a: 3, b: 4}
const code2 = math.compile('a * b') // 12
code2.evaluate(scope) // 12
scope.a = 5
code2.evaluate(scope) // 20
const nodes = math.compile(['a = 3', 'b = 4', 'a * b'])
nodes[2].evaluate() // 12
```
<h2 id="see-also">See also <a href="#see-also" title="Permalink">#</a></h2>
[parse](parse.html),
[evaluate](evaluate.html)