1 |
|
2 |
|
3 | # Function parse
|
4 |
|
5 | Parse an expression. Returns a node tree, which can be evaluated by
|
6 | invoking node.evaluate().
|
7 |
|
8 | Note the evaluating arbitrary expressions may involve security risks,
|
9 | see [https://mathjs.org/docs/expressions/security.html](https://mathjs.org/docs/expressions/security.html) for more information.
|
10 |
|
11 |
|
12 | ## Syntax
|
13 |
|
14 | ```js
|
15 | math.parse(expr)
|
16 | math.parse(expr, options)
|
17 | math.parse([expr1, expr2, expr3, ...])
|
18 | math.parse([expr1, expr2, expr3, ...], options)
|
19 | ```
|
20 |
|
21 | ### Parameters
|
22 |
|
23 | Parameter | Type | Description
|
24 | --------- | ---- | -----------
|
25 | `expr` | string | string[] | Matrix | Expression to be parsed
|
26 | `options` | {nodes: Object<string, Node>} | Available options:</br>- `nodes` a set of custom nodes
|
27 |
|
28 | ### Returns
|
29 |
|
30 | Type | Description
|
31 | ---- | -----------
|
32 | Node | Node[] | node
|
33 |
|
34 |
|
35 | ## Examples
|
36 |
|
37 | ```js
|
38 | const node1 = math.parse('sqrt(3^2 + 4^2)')
|
39 | node1.compile().evaluate() // 5
|
40 |
|
41 | let scope = {a:3, b:4}
|
42 | const node2 = math.parse('a * b') // 12
|
43 | const code2 = node2.compile()
|
44 | code2.evaluate(scope) // 12
|
45 | scope.a = 5
|
46 | code2.evaluate(scope) // 20
|
47 |
|
48 | const nodes = math.parse(['a = 3', 'b = 4', 'a * b'])
|
49 | nodes[2].compile().evaluate() // 12
|
50 | ```
|
51 |
|
52 |
|
53 | ## See also
|
54 |
|
55 | [evaluate](evaluate.md),
|
56 | [compile](compile.md)
|