MathExpr is a Python library for parsing and evaluating mathematical expressions.
To get started with this project, please follow these steps:
- Python 3.8+
- pip 20.0+
To install the library, run the following command:
pip install mathexprMathExpr provides the following features:
- Parsing mathematical expressions
- Evaluating mathematical expressions
- Evaluating mathematical expressions with variables
To use MathExpr, import the MathParse class and use the parse and evaluate methods.
Simple usage example:
from mathexpr import MathExpr
math_string = "(2 + 3) * 4^3"
result = MathExpr.evaluate(math_string)
print(result)
>>> 320.0Usage with variables:
from mathexpr import MathExpr
math_string = "x + y"
result = MathExpr.evaluate(math_string, {"x": 2, "y": 3})
print(result)
>>> 5Debug the AST:
from mathexpr import MathExpr, print_ast
math_string = "(2 + 3) * 4^3"
ast = MathExpr.parse(math_string)
print_ast(ast)
>>> TokenType.MUL
>>> ├──TokenType.ADD
>>> │ ├──NumNode(value=2.0)
>>> │ ╰──NumNode(value=3.0)
>>> ╰──TokenType.POW
>>> │ ├──NumNode(value=4.0)
>>> │ ╰──NumNode(value=3.0)If you would like to contribute to this project, please fork the repository and create a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.