69 lines
1.8 KiB
Markdown
69 lines
1.8 KiB
Markdown
# Math expression parser
|
|
|
|
Math expression evaluation library. It supports most of useful math operations and functions. Expressions can contain variables which can be substituted with `int`s, `float`s or `numpy.ndarray`s.
|
|
|
|
## Example usage
|
|
|
|
```python
|
|
from parser import Parser
|
|
|
|
parser = Parser("(-b + sqrt(b^2-4a c))/(2a)")
|
|
|
|
parser.variables_names # {'c', 'a', 'b'}
|
|
|
|
parser.evaluate({"a": 1, "b": -3, "c": 2}) # 1.0
|
|
|
|
parser.evaluate({"a": [1, 1, 1], "b": [-5, -6, -9], "c": [6, 9, 20]}) # [2. 3. 4.]
|
|
```
|
|
## Expression syntax
|
|
|
|
Expression can contain numbers or variable names with functions applyed to them, separated with operators or united with braces.
|
|
|
|
Theese are supported:
|
|
|
|
Functions:
|
|
|
|
|name| math |
|
|
|--|--|
|
|
| `abs` | $\|x\|$ |
|
|
| `acos` | $\cos^{-1}(x)$ |
|
|
| `acosh` | $\cosh^{-1}(x)$ |
|
|
| `acot` | $\cot^{-1}(x)$ |
|
|
| `asin` | $\sin^{-1}(x)$ |
|
|
| `asinh` | $\sinh^{-1}(x)$ |
|
|
| `atan` | $\tan^{-1}(x)$ |
|
|
| `avg` | $\overline X$ |
|
|
| `cos` | $\cos(x)$ |
|
|
| `cosh` | $\cosh(x)$ |
|
|
| `cot` | $\cot(x)$ |
|
|
| `exp` | $\exp(x)$ |
|
|
| `inf` | $\inf(X)$ |
|
|
| `lg` | $\lg(x)$ |
|
|
| `ln` | $\ln(x)$ |
|
|
| `log10` | $\log_{10}(x)$ |
|
|
| `log2` | $\log_2(x)$ |
|
|
| `max` | $\sup(X)$ |
|
|
| `min` | $\inf(X)$ |
|
|
| `prod` | $\displaystyle \prod_{i=0}^n x_i$ |
|
|
| `sgn` | $sgn(x)$ |
|
|
| `sign` | $sgn(x)$ |
|
|
| `sin` | $\sin(x)$ |
|
|
| `sinh` | $\sinh(x)$ |
|
|
| `sqrt` | $\sqrt{x}$ |
|
|
| `sum` | $\displaystyle\sum_{i=0}^n x_i$ |
|
|
| `sup` | $\sup(X)$ |
|
|
| `tan` | $\tan(x)$ |
|
|
| `tanh` | $\tanh(x)$ |
|
|
|
|
Operators: `+`, `-`, `*`, `/`, `^`, `%`
|
|
|
|
Braces: `()`, `[]`, `{}`
|
|
|
|
Floating points: `.`, `,`
|
|
|
|
Functions have only one argument, provided in braces. Operators must have two operands except minus (if it is the first character of equation or braced expression).
|
|
|
|
`avg`, `sum`, `max`, `sup`, `min`, `inf` and `prod` applied on `numpy.ndarray` produce `float`.
|
|
|
|
**! There is no error handling yet !**
|