1 |
|
2 |
|
3 | # Function evaluate
|
4 |
|
5 | Evaluate an expression.
|
6 |
|
7 | Note the evaluating arbitrary expressions may involve security risks,
|
8 | see [https://mathjs.org/docs/expressions/security.html](https://mathjs.org/docs/expressions/security.html) for more information.
|
9 |
|
10 |
|
11 | ## Syntax
|
12 |
|
13 | ```js
|
14 | math.evaluate(expr)
|
15 | math.evaluate(expr, scope)
|
16 | math.evaluate([expr1, expr2, expr3, ...])
|
17 | math.evaluate([expr1, expr2, expr3, ...], scope)
|
18 | ```
|
19 |
|
20 | ### Parameters
|
21 |
|
22 | Parameter | Type | Description
|
23 | --------- | ---- | -----------
|
24 | `expr` | string | string[] | Matrix | The expression to be evaluated
|
25 | `scope` | Object | Scope to read/write variables
|
26 |
|
27 | ### Returns
|
28 |
|
29 | Type | Description
|
30 | ---- | -----------
|
31 | * | The result of the expression
|
32 |
|
33 |
|
34 | ## Examples
|
35 |
|
36 | ```js
|
37 | math.evaluate('(2+3)/4') // 1.25
|
38 | math.evaluate('sqrt(3^2 + 4^2)') // 5
|
39 | math.evaluate('sqrt(-4)') // 2i
|
40 | math.evaluate(['a=3', 'b=4', 'a*b']) // [3, 4, 12]
|
41 |
|
42 | let scope = {a:3, b:4}
|
43 | math.evaluate('a * b', scope) // 12
|
44 | ```
|
45 |
|
46 |
|
47 | ## See also
|
48 |
|
49 | [parse](parse.md),
|
50 | [compile](compile.md)
|