Skip to content
This repository was archived by the owner on Dec 23, 2019. It is now read-only.
/ evaluator Public archive

A simple arithmetic expression evaluator.

License

Notifications You must be signed in to change notification settings

rinald/evaluator

Repository files navigation

Evaluator

A simple evaluator for mathematical expressions.

The evaluator package reexports the two main objects: Expression and Function.

The Expression object represent a mathematical expression. It supports expressions with numbers, constants, operators, parentheses and also embedded mathematical functions.

Example usage:

from evaluator import Exp

e = Exp('(1+2)*3-4')
print(e.eval()) # prints 5

e = Exp('sin(1-ln(e))')
print(e.eval()) # prints 0

The Function object represents a multivariable mathematical function. It essentially is just an expression with variables. It is a callable object, so you can call it just like an ordinary function.

Example usage:

from evaluator import Fun

## Single variable function
f = Fun('x^2+2*x+1')
print(f(-1)) # prints 0

f = Fun('cos(theta)') # 'theta' is the variable in this case
print(f(0)) # prints 1

## Multivariable function
f = Fun('x+y')
print(f(1, 2)) # prints 3
print(f(y=2, x=1)) # same as above
print(f(x=1, z=2)) # error

For a list of operators, constants and functions, see evaluator/util.py

About

A simple arithmetic expression evaluator.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages