numexpr
numexpr copied to clipboard
How to calculate only math expression ? (without using outside variables)
Hi, I want to calculate expression which is pure in math (do not contains python variables), such as: "1 + 1 + 5" "3 * 423 - 43243" but not "2 * a - 7 * b". How can I make sure your algorithms do not refer to python variables ?
You probably want to parse the expression with an Abstract Syntax Tree (AST) first before passing it to NumExpr. You can see the official documentation here:
https://docs.python.org/3/library/ast.html
and the better but unofficial documentation:
https://greentreesnakes.readthedocs.io/en/latest/
The implementation of numexpr3
makes use of the Python AST using a function dictionary, whereas NumExpr 2.7 uses a custom AST implementation in expressions.py
. Although given your use case the Sympy
package may be more-so what you want here.