UNPKG

1.47 kBMarkdownView Raw
1<!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. -->
2
3# Function parser
4
5Create a parser. The function creates a new `math.Parser` object.
6
7
8## Syntax
9
10```js
11math.parser()
12```
13
14### Parameters
15
16Parameter | Type | Description
17--------- | ---- | -----------
18
19
20### Returns
21
22Type | Description
23---- | -----------
24Parser | Parser
25
26
27## Examples
28
29```js
30const parser = new math.parser()
31
32// evaluate expressions
33const a = parser.evaluate('sqrt(3^2 + 4^2)') // 5
34const b = parser.evaluate('sqrt(-4)') // 2i
35const c = parser.evaluate('2 inch in cm') // 5.08 cm
36const d = parser.evaluate('cos(45 deg)') // 0.7071067811865476
37
38// define variables and functions
39parser.evaluate('x = 7 / 2') // 3.5
40parser.evaluate('x + 3') // 6.5
41parser.evaluate('function f(x, y) = x^y') // f(x, y)
42parser.evaluate('f(2, 3)') // 8
43
44// get and set variables and functions
45const x = parser.get('x') // 7
46const f = parser.get('f') // function
47const g = f(3, 2) // 9
48parser.set('h', 500)
49const i = parser.evaluate('h / 2') // 250
50parser.set('hello', function (name) {
51 return 'hello, ' + name + '!'
52})
53parser.evaluate('hello("user")') // "hello, user!"
54
55// clear defined functions and variables
56parser.clear()
57```
58
59
60## See also
61
62[evaluate](evaluate.md),
63[compile](compile.md),
64[parse](parse.md)