函数 compile #
解析和编译表达式。返回一个具有 evaluate([scope]) 函数的对象,用于评估编译后的表达式。
Syntax #
math.compile(expr) // returns one node
math.compile([expr1, expr2, expr3, ...]) // returns an array with nodes
Parameters #
| Parameter |
Type |
Description |
expr |
string | string[] | Array | Matrix |
要编译的表达式 |
Returns #
| Type |
Description |
| {evaluate: Function} | Array.<{evaluate: Function}> |
code 包含编译后表达式的对象 |
Throws #
Examples #
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
另请参阅 #
parse, evaluate