Skip to content
/ pexpr Public

Evaluate results given by math expressions or export them as latex format

License

Notifications You must be signed in to change notification settings

NielXu/pexpr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

91 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pexpr

pexpr is a module that contains tools to convert from expressions to Abstract Syntax Tree. After that, it can be evaluated(if possible), transformed(prefix, infix, postfix) or even converted to Latex code to generate the expression PDF.

CLI

To quickly calculate a math expression just type in command line:

python ast.py "(1*2+max(4,5)/3)"

and the result will be:

3.666666666666667

To view the tree, enable the view feature by:

python ast.py -v "(1*2+max(4,5)/3)"

The result will be the following:

3.666666666666667

       _____(+)_______________
      /                       \
   _(*)_                ______(/)_
  /     \              /          \
(1)     (2)        _(max)_        (3)
                  /       \
                (4)       (5)

To generate a PDF from the given expression, enable preview feature:

python ast.py -p "(1*2+max(4,5)/3)"

Note: To use features related to PDF, please download miktex

Note: quotation is required since there are special characters in some cases

Note: brackets are required since minus sign is a special character

For more information, enter:

python ast.py -h

Abstract-Syntax-Tree

To build a AST(Abstract Syntax Tree), the recommend way would be using build function in ast.py.

For example, build("1+1") will be converted to the tree:

        +
      /   \
     1     1

the same as build("a+b"):

        +
      /   \
     a     b

However, only the first tree can be evaluated since it contains numbers but not variables:

evaluate(build("1+2")) # 3.0

A tree with parentheses will override the precedence:

      3*(1+2)               3*1+2
        *                     +
      /   \                 /   \
     3     +               *     2
         /   \           /   \
        2     1         3     1

And the results, of course, are different:

evaluate(build("3*(1+2)"))     # 9.0
evaluate(build("3*1+2"))       # 5.0

Tree viewer

To view the tree that created by the build function, simply use view() function in ast.py:

a = build("2^(1+2)*1+3/4+10*2*3")
view(a)

the output will be displayed on the console:

                               _____________(+)______________
                              /                              \
                       _____(+)_____                    _____(*)_
                      /             \                  /         \
       _____________(*)_           _(/)_           __(*)_        (3)
      /                 \         /     \         /      \
   _(^)_____            (1)     (3)     (4)     (10)     (2)
  /         \
(2)        _(+)_
          /     \
        (1)     (2)

Latex

Note: To use features related to PDF, please download miktex

lat.py module allows you to convert from python math expressions to latex codes. For instance:

tree = build("2+pi*sin(x+2)/cos(x^(y+1))")  # Build a ast first
lat_expr = genlat(tree)                     # Convert to latex code
# The output will be:
# ${2+\frac{{\pi*sin({x+2})}}{cos({x^{y+1}})}}$

Furthermore, you can generate .tex file by given latex code. The file will only contains the header, footer and the given expression:

gentex(lat_expr, "/location", "name")   # Save .tex file to location with name

After that, a pdf file can be generated by compiling the .tex file

genpdf("/location/name.tex", "/target")   # Compiling .tex file from source and save the files to target location

pdflatex will compile the .tex file and generate files such as log files and so on. If you want only the pdf file, enable the rm optional arg:

genpdf("/location/name.tex", "/target", rm=True)  # This will remove all additional files except for pdf

There's also a quicker way to generate the PDF file, it merges the previous functions into one function:

quickgen(tree, "/target", "name")  # This will generate only the .pdf file with name to target location
quickgen(tree, "/target", "name", op=True)  # This will open the pdf file with system default app

Operations

Here are some symbols and special numbers that AST supports:

Binary Operations

Symbol Operation Example
+ Addition 1+1
- Subtraction 1-2
* Multiplication 1*1
/ Division 1/1
^ Power 1^2
max Max value of two numbers max(1,2)
min Min value of two numbers min(1,2)
log Logarithm given base and exponent log(2, 1)

Unary Operations

Symbol Operation Example
sin Trig function, sin sin(0)
cos Trig function, cos cos(0)
tan Trig function, tan tan(1)
asin Inverse trig function, arcsin asin(1)
acos Inverse trig function, arccos acos(1)
atan Inverse trig function, arctan atan(1)
lg Logarithm with base 10 lg(1)
ln Logarithm with base e ln(1)
sqrt Square root sqrt(0)
abs Absolute value abs(-1)
~ Negation ~-5

Note: Some negative sign(-) will be converted to negation(~) when building AST

Special Numbers

Symbol
e
pi

TODO

  • Document
  • Special numbers: e, pi
  • Math functions: sin, cos, tan, asin, acos, atan, ln, log, abs
  • Testing
  • LaTeX code
  • Error handling
  • AST PDF size

About

Evaluate results given by math expressions or export them as latex format

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages