Mathematical expression parser: cython wrapper around the 'C++ Mathematical Expression Toolkit Library'
Project description
cexprtk is a cython wrapper around “C++ Mathematical Expression Toolkit Library (ExprTk) “ by Arash Partow. Using cexprtk a powerful mathematical expression engine can be incorporated into your python project.
Installation
The latest version of cexprtk can be installed using pip :
$ pip install cexprtk
Usage
The cexprtk module contains two functions check_expression and evaluate_expression; the first can be used validate a mathematical formula before calculating its value using the latter.
Example: Evaluate a simple equation
The following shows how the arithmetic expression (5+5) * 23 can be evaluated:
>>> import cexprtk
>>> cexprtk.evaluate_expression("(5+5) * 23", {})
230.0
Example: Using Variables
Variables can be used within expressions by passing a dictionary to the evaluate_expression function. This maps variable names to their values. The expression from the previous example can be re-calculated using variable values:
>>> import cexprtk
>>> cexprtk.evaluate_expression("(A+B) * C", {"A" : 5, "B" : 5, "C" : 23})
230.0
API Reference
For information about expressions supported by cexprtk please refer to the original C++ ExprTK documentation:
check_expression (expression)
Check that expression can be parsed. If successful do nothing, if unsuccessful raise ParseException.
Parameters:
expression (str) Formula to be evaluated
Raises ParseException: If expression is invalid.
evaluate_expression (expression, variables)
Evaluate a mathematical formula using the exprtk library and return result.
For more information about supported functions and syntax see the exprtk C++ library website http://code.google.com/p/exprtk/
Parameters:
expression (str) Expression to be evaluated.
variables (dict) Dictionary containing variable name, variable value pairs to be used in expression.
Returns (float): Evaluated expression
Raises ParseException: if expression is invalid.
License
cexprtk is released under the same terms as the ExprTK library the Common Public License Version 1.0 (CPL).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.