This project implements a system for evaluating mathematical expressions and computing their derivatives. It leverages a custom-built LR parser to process expressions, and it supports a wide range of mathematical operations, including basic arithmetic, trigonometric functions, exponentiation, and logarithms.
- Evaluation: The program evaluates mathematical expressions given as strings, including support for variables (e.g.,
X). - Differentiation: It computes the derivative of the expression by constructing and traversing an operation tree.
- Python and Haskell Implementations: The project is implemented in both Python and Haskell, demonstrating flexibility across programming languages.
- Tree Visualization: In the Python version, the operation tree used for differentiation can be visualized using Graphviz.
The LR parser is built using the following context-free grammar:
E -> E + T
| E - T
| T
T -> T * F
| T / F
| F
F -> G ^ F
| G
G -> ( E )
| - G
| Func ( E )
| NUMBER
| X
Func -> sin
| cos
| tg
| arcsin
| arccos
| arctg
| exp
| ln
This grammar allows the parser to recognize and process a wide variety of mathematical expressions.
-
Evaluation and Differentiation:
- The main functionality is encapsulated in the
Eval and Diff using LR Parser.pyfile. - The script evaluates the expression and computes its derivative, printing the results to the console.
- The main functionality is encapsulated in the
-
Tree Visualization:
- The
tree_vizualize_functions.pyfile contains functions to convert the operation tree into a.pngfile using Graphviz. - you will need to add them in order to work.
- The
- The Haskell implementation is available in the
Eval_and_Diff_using_LR_Parser.hsfile. - It provides similar functionality for evaluating expressions and computing derivatives.
Given the expression:
X^7 + sin(X) - ln(X^2)
The program will:
- Evaluate the expression for a given value of
X. - Compute and display the derivative of the expression.
- (Python) Visualize the operation tree for the expression.
The Python implementation includes a comparison against traditional evaluation and differentiation methods (e.g., using Python's eval and SymPy).