shunting-yard


Nameshunting-yard JSON
Version 1.0.12.post1 PyPI version JSON
download
home_pagehttps://www.github.com/charon25/ShuntingYard
SummaryCompute any math expression
upload_time2023-04-23 09:20:56
maintainer
docs_urlNone
authorPaul 'charon25' Kern
requires_python>=3.9
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Shuting-yard algorithm

This repo is a Python (>= 3.9) module containing an implementation of the [Shunting-yard algorithm](https://en.wikipedia.org/wiki/Shunting_yard_algorithm), which converts any "regular" mathematical expression into its [Reverse Polish Notation](https://en.wikipedia.org/wiki/Reverse_Polish_notation) equivalent, and then can evaluate it.

# Installation

This module is available [on Pypi](https://pypi.org/project/shunting-yard/). To install it, just run :

```bash
pip install shunting-yard --user
```

Or alternatively you can use :

```bash
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 :

```bash
python -m shunting_yard 1
```


# Usage

Either use it directly in a command line interface with :

```bash
python -m shunting_yard <expression>
```

For instance :

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

1 + cos(pi) = 0.0
```

Or you can use it in a Python script with :

```python
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 :

```python
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 :

```python
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', ')']

```

            

Raw data

            {
    "_id": null,
    "home_page": "https://www.github.com/charon25/ShuntingYard",
    "name": "shunting-yard",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": "",
    "keywords": "",
    "author": "Paul 'charon25' Kern",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/e2/70/9162321998f652bd1f138c678f92cd805d766fde179223874401a9533470/shunting-yard-1.0.12.post1.tar.gz",
    "platform": null,
    "description": "# Shuting-yard algorithm\r\n\r\nThis repo is a Python (>= 3.9) module containing an implementation of the [Shunting-yard algorithm](https://en.wikipedia.org/wiki/Shunting_yard_algorithm), which converts any \"regular\" mathematical expression into its [Reverse Polish Notation](https://en.wikipedia.org/wiki/Reverse_Polish_notation) equivalent, and then can evaluate it.\r\n\r\n# Installation\r\n\r\nThis module is available [on Pypi](https://pypi.org/project/shunting-yard/). To install it, just run :\r\n\r\n```bash\r\npip install shunting-yard --user\r\n```\r\n\r\nOr alternatively you can use :\r\n\r\n```bash\r\ngit clone https://github.com/charon25/ShuntingYard.git\r\ncd ShuntingYard\r\npython setup.py install --user\r\n```\r\n\r\nYou can check the installation was successful by running the following command and getting `1 = 1` as output :\r\n\r\n```bash\r\npython -m shunting_yard 1\r\n```\r\n\r\n\r\n# Usage\r\n\r\nEither use it directly in a command line interface with :\r\n\r\n```bash\r\npython -m shunting_yard <expression>\r\n```\r\n\r\nFor instance :\r\n\r\n```bash\r\n>>> python -m shunting_yard \"1 + cos(pi)\"\r\n\r\n1 + cos(pi) = 0.0\r\n```\r\n\r\nOr you can use it in a Python script with :\r\n\r\n```python\r\nimport shunting_yard as sy\r\n\r\nprint(sy.compute(\"2^32 - sqrt(tan(42))\"))\r\n```\r\n\r\n## Allowed functions\r\n\r\nBy default, the module can process the 5 basic operations (`+`, `-`, `*`, `/`, `^`) as well as those functions :\r\n - sqrt\r\n - sin, cos, tan\r\n - min, max (with 2, 3 or 4 arguments)\r\n - abs\r\n\r\nAs well as the constants `pi` and `e`.\r\n\r\nThese operations and functions can be mixed however you want.\r\n\r\nFurthermore, 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 :\r\n\r\n```python\r\nimport math\r\n\r\nadditional_functions = {\r\n    'gamma': (0, lambda:0.5772156649015329), # create new constant\r\n    'inc': (1, lambda x:x + 1), # using lambda\r\n    'exp': (1, math.exp), # using already existing function\r\n    'gcd3': (3, math.gcd) # 3 parameters\r\n}\r\n```\r\n\r\nThe `sy.compute` (and `sy.shuting_yard`) also have extra parameters :\r\n - `case_sensitive` (bool, defaults to `True`) : if `True`, will consider `sin` and `SIN` different functions.\r\n - `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.\r\n\r\n## Additional features\r\n\r\n### Implicit multiplication\r\n\r\nThis program supports implicit multiplication (when the `*` symbol is omitted). They can have multiple forms, such as :\r\n- `(1+2)(2+3)`\r\n- `2sin(pi)`\r\n- `(1+2)3`\r\n- `sin(pi)10`\r\n\r\n### Functions\r\n\r\nInstead of just calling the `sy.compute` function, you can break it into its parts :\r\n - `sy.shunting_yard` will return the RPN equivalent expression of the given mathematical expression ;\r\n - `sy.compute_rpn` will use this expression and compute its value (use the `additional_functions` parameters here).\r\n\r\nFurthermore, you can just use the `sy.tokenize` function to transform a mathematical expression into its base components (returns a generator).\r\n\r\nExamples :\r\n\r\n```python\r\nimport shunting_yard as sy\r\n\r\nprint(sy.shunting_yard('2 * sin(5/2)'))\r\n# 2 5 2 / sin *\r\n\r\nprint(sy.compute_rpn('pi 2 / sin'))\r\n# 1.0\r\n\r\nprint(sy.compute_rpn('3 inc', {'inc': (1, lambda x:x + 1)}))\r\n# 4\r\n\r\nprint(list(sy.tokenize('1+(2 * 3) - 4 * (2 / 3)')))\r\n# ['1', '+', '(', '2', '*', '3', ')', '-', '4', '*', '(', '2', '/', '3', ')']\r\n\r\n```\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Compute any math expression",
    "version": "1.0.12.post1",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e2709162321998f652bd1f138c678f92cd805d766fde179223874401a9533470",
                "md5": "a1297b98dd0eb72e78738a0ffb2fbb29",
                "sha256": "41ac4377b05ed5281ff9cc3921943292bdf40fe9d4cff52f09fd3edbb8a8b289"
            },
            "downloads": -1,
            "filename": "shunting-yard-1.0.12.post1.tar.gz",
            "has_sig": false,
            "md5_digest": "a1297b98dd0eb72e78738a0ffb2fbb29",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 11465,
            "upload_time": "2023-04-23T09:20:56",
            "upload_time_iso_8601": "2023-04-23T09:20:56.257296Z",
            "url": "https://files.pythonhosted.org/packages/e2/70/9162321998f652bd1f138c678f92cd805d766fde179223874401a9533470/shunting-yard-1.0.12.post1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-04-23 09:20:56",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "charon25",
    "github_project": "ShuntingYard",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "shunting-yard"
}
        
Elapsed time: 0.06073s