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
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) |
lg |
\lg(x) |
ln |
\ln(x) |
log10 |
\log_{10}(x) |
log2 |
\log_2(x) |
prod |
\displaystyle \prod_{i=0}^n x_i |
sgn |
sgn(x) |
sin |
\sin(x) |
sinh |
\sinh(x) |
sqrt |
\sqrt{x} |
sum |
\displaystyle\sum_{i=0}^n x_i |
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).
sum
and prod
sums and multiplies all elements in variable if it is numpy.ndarray
and produces a float
.
! There is no error handling yet !