Skip to main content

Compute any math expression

Project description

Shuting-yard algorithm

This repo is a Python (>= 3.9) module containing an implementation of the Shunting-yard algorithm, which converts any "regular" mathematical expression into its Reverse Polish Notation equivalent, and then can evaluate it.

Installation

This module is available on Pypi. To install it, just run :

pip install shunting-yard --user

Or alternatively you can use :

git clone https://github.com/charon25/ShuntingYard.git
cd ShuntingYard
python setup.py install --user

You can check the installation was successful by running the following command and getting 1 = 1 as output :

python -m shunting_yard 1

Usage

Either use it directly in a command line interface with :

python -m shunting_yard <expression>

For instance :

>>> python -m shunting_yard "1 + cos(pi)"

1 + cos(pi) = 0.0

Or you can use it in a Python script with :

import shunting_yard as sy

print(sy.compute("2^32 - sqrt(tan(42))"))

Allowed functions

By default, the module can process the 5 basic operations (+, -, *, /, ^) as well as those functions :

  • sqrt
  • sin, cos, tan
  • min, max (with 2, 3 or 4 arguments)
  • abs

As well as the constants pi and e.

These operations and functions can be mixed however you want.

Furthermore, you can add more functions with the optional parameters additional_functions of the sy.compute function. It should be a dictionary whose keys are string, and values are a tuple containing first the number of expected argument to the function, and then the function itself. For example :

import math

additional_functions = {
    'gamma': (0, lambda:0.5772156649015329), # create new constant
    'inc': (1, lambda x:x + 1), # using lambda
    'exp': (1, math.exp), # using already existing function
    'gcd3': (3, math.gcd) # 3 parameters
}

The sy.compute (and sy.shuting_yard) also have extra parameters :

  • case_sensitive (bool, defaults to True) : if True, will consider sin and SIN different functions.
  • variable (str, optional) : if defined, will consider any token matching it as a number. This is useful in expression such as min(x, 1) to get x to behave as a number.

Additional features

Implicit multiplication

This program supports implicit multiplication (when the * symbol is omitted). They can have multiple forms, such as :

  • (1+2)(2+3)
  • 2sin(pi)
  • (1+2)3
  • sin(pi)10

Functions

Instead of just calling the sy.compute function, you can break it into its parts :

  • sy.shunting_yard will return the RPN equivalent expression of the given mathematical expression ;
  • sy.compute_rpn will use this expression and compute its value (use the additional_functions parameters here).

Furthermore, you can just use the sy.tokenize function to transform a mathematical expression into its base components (returns a generator).

Examples :

import shunting_yard as sy

print(sy.shunting_yard('2 * sin(5/2)'))
# 2 5 2 / sin *

print(sy.compute_rpn('pi 2 / sin'))
# 1.0

print(sy.compute_rpn('3 inc', {'inc': (1, lambda x:x + 1)}))
# 4

print(list(sy.tokenize('1+(2 * 3) - 4 * (2 / 3)')))
# ['1', '+', '(', '2', '*', '3', ')', '-', '4', '*', '(', '2', '/', '3', ')']

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

shunting-yard-1.0.12.post1.tar.gz (11.5 kB view details)

Uploaded Source

File details

Details for the file shunting-yard-1.0.12.post1.tar.gz.

File metadata

  • Download URL: shunting-yard-1.0.12.post1.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for shunting-yard-1.0.12.post1.tar.gz
Algorithm Hash digest
SHA256 41ac4377b05ed5281ff9cc3921943292bdf40fe9d4cff52f09fd3edbb8a8b289
MD5 a1297b98dd0eb72e78738a0ffb2fbb29
BLAKE2b-256 e2709162321998f652bd1f138c678f92cd805d766fde179223874401a9533470

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page