mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-18 14:59:29 +00:00
54 lines
1.1 KiB
Markdown
54 lines
1.1 KiB
Markdown
# Function parse
|
|
|
|
Parse an expression.
|
|
Returns a node tree which can be compiled and evaluated.
|
|
|
|
|
|
## Syntax
|
|
|
|
```js
|
|
math.parse(expr)
|
|
math.parse(expr, nodes)
|
|
math.parse([expr1, expr2, expr3, ...])
|
|
math.parse([expr1, expr2, expr3, ...], nodes)
|
|
```
|
|
|
|
### Parameters
|
|
|
|
Parameter | Type | Description
|
|
--------- | ---- | -----------
|
|
`expr` | String | String[] | Matrix | Expression to be parsed
|
|
`nodes` | Object<String, Node> | Optional custom nodes
|
|
|
|
### Returns
|
|
|
|
Type | Description
|
|
---- | -----------
|
|
Node | Node[] | A node tree
|
|
|
|
|
|
## Examples
|
|
|
|
```js
|
|
var node = math.parse('sqrt(3^2 + 4^2)');
|
|
node.compile(math).eval(); // 5
|
|
|
|
var scope = {a: 3, b: 4}
|
|
var node = math.parse('a * b'); // 12
|
|
var code = node.compile(math);
|
|
code.eval(scope); // 12
|
|
scope.a = 5;
|
|
code.eval(scope); // 20
|
|
|
|
var nodes = math.parse(['a = 3', 'b = 4', 'a * b']);
|
|
var scope2 = {};
|
|
nodes.map(function(node) {
|
|
return node.compile(math).eval(scope2);
|
|
}); // returns [3, 4, 12]
|
|
```
|
|
|
|
|
|
|
|
|
|
<!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. -->
|