项目作者: sbesada

项目描述 :
pymep is a simple python math expression parser.It is a recursive LR parser (Left-Right Parser) without backtracking
高级语言: Python
项目地址: git://github.com/sbesada/python.math.expression.parser.pymep.git
创建时间: 2019-09-12T19:20:05Z
项目社区:https://github.com/sbesada/python.math.expression.parser.pymep

开源协议:Apache License 2.0

下载


python.math.expression.parser.pymep

pymep can parse or evaluate math expressions and it is tested using matlab/octave

This algorithm does not use a decision tree. It is a kind of Recursive Ascent Parser (https://en.wikipedia.org/wiki/Recursive_ascent_parser). In fact, it is LR parser (Left-Right Parser) without backtracking. This recursive algorithm is faster than decision trees

pypi version

https://pypi.org/project/pymep/

Installation

pip install pymep

Features

math functions

  • sin, cos, tan, sinh, cosh, tanh, asin, acos, atan, log, log10, sqrt
  • pi, e
  • real or complex numbers

parentheses

  1. fx= 2*(e*2)

variables

  • Expressions in vars

    1. var = {"x":"1+2+3+4+5", "Z":1}
    2. eval(" 2*(-(((z*3)*sqrt(x^(2)))+3))",var)

Examples

Parse or eveluate expressions with real numbers:

  1. from pymep.realParser import parse
  2. from pymep.realParser import eval
  3. #Real Expresion parser
  4. fx="cos(10)"
  5. print(parse(fx))
  6. xi=5
  7. fx = "1 + x"
  8. print(eval(fx, xi))
  9. var = {"x":"1+1", "Z":1}
  10. eval(" 2*(-(((z*3)*sqrt(x^(2)))+3))",var)

Parse or evaluate expressions with complex numbers:

  1. from pymep.complexParser import parse
  2. from pymep.complexParser import eval
  3. from pymep.complex import Complex
  4. #Operation with complex numbers
  5. a = Complex(1,2)
  6. print(a.__radd__(10).__complex__())
  7. print(Complex.radd(10, a).__complex__())
  8. #Complex Expresion parser
  9. fx="cos(10+2j)"
  10. print(parse(fx).__complex__())
  11. xi=5
  12. fx = "1 +j+x"
  13. print(eval(fx, xi).__complex__())
  14. var={"x":"1+2j", "Y":complex(2,1)}
  15. f_x = "x-y"
  16. eval(f_x,var).__complex__()

There is a full list of examples inside!!

How to execute the tests

  1. from pymep.realParserTest import functionXTest;
  2. p = functionXTest();
  3. p.test_one();
  4. p.test_three();

Vulnerabilities

  1. Zero vulnerabilities on https://snyk.io/vuln/pip:pymep

Notes

  • The java version of this library is: https://github.com/sbesada/java.math.expression.parser
  • Regarding to the OS where you excute the tests, it is possible that some tests fail beacause of rounding issues. The mathematical library that was used in this project is “math”. In the future, it is possible that the math library changes.

Professional Services

If you are interested in logical parsers or any task related to parsers, you can consult my professional services page https://github.com/sbesada/professional.services

Donation

If you think that my work deserves a donation, you can do it: https://sbesada.github.io/