mathy-core


Namemathy-core JSON
Version 0.9.4 PyPI version JSON
download
home_pagehttps://mathy.ai
SummaryComputer Algebra System for working with math expressions
upload_time2024-02-06 00:45:13
maintainer
docs_urlNone
authorJustin DuJardin
requires_python>=3.6
licenseAll rights reserved
keywords math
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # mathy_core: parse and manipulate math expressions

[![Build](https://github.com/mathy/mathy_core/workflows/Build/badge.svg)](https://github.com/mathy/mathy_core/actions)
[![Types](https://github.com/mathy/mathy_core/workflows/Types/badge.svg)](https://github.com/mathy/mathy_core/actions)
[![codecov](https://codecov.io/gh/mathy/mathy_core/branch/master/graph/badge.svg)](https://codecov.io/gh/mathy/mathy_core)
[![Pypi version](https://badgen.net/pypi/v/mathy-core)](https://pypi.org/project/mathy-core/)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)

Mathy core is a python package (_with type annotations_) for working with math problems. It has a tokenizer for converting plain text into tokens, a parser for converting tokens into expression trees, a rule-based system for manipulating the trees, a layout system for visualizing trees, and a set of problem generation functions that can be used to generate datasets for ML training.

## 🚀 Quickstart

You can install `mathy_core` from pip:

```bash
pip install mathy_core
```

## 📖 Documentation 

Check out https://core.mathy.ai for API documentation, examples, and more!

## Examples

Consider a few examples to get a feel for what Mathy core does.

### Evaluate an expression

Arithmetic is a snap.

```python
from mathy_core import ExpressionParser

expression = ExpressionParser().parse("4 + 2")
assert expression.evaluate() == 6
```

### Evaluate with variables

Variable values can be specified when evaluating an expression.

```python
from mathy_core import ExpressionParser, MathExpression

expression: MathExpression = ExpressionParser().parse("4x + 2y")
assert expression.evaluate({"x": 2, "y": 5}) == 18
```

### Transform an expression

Expressions can be changed using rules based on the properties of numbers.

```python
from mathy_core import ExpressionParser
from mathy_core.rules import DistributiveFactorOutRule

input = "4x + 2x"
output = "(4 + 2) * x"
parser = ExpressionParser()

input_exp = parser.parse(input)
output_exp = parser.parse(output)

# Verify that the rule transforms the tree as expected
change = DistributiveFactorOutRule().apply_to(input_exp)
assert str(change.result) == output

# Verify that both trees evaluate to the same value
ctx = {"x": 3}
assert input_exp.evaluate(ctx) == output_exp.evaluate(ctx)
```

<!-- ### Visualize a Tree -- needs mathy plugin in docs.sh -->
<!-- ### Generate Problems -- needs example snippet -->

## Development

Install the prerequisites in a virtual environment (python3 required)

```bash
sh tools/setup.sh
```

Run the test suite and view code-coverage statistics

```bash
sh tools/test.sh
```

The tests cover ~90% of the code so they're a good reference for how to use the various APIs.

## Semantic Versioning

Before Mathy Core reaches v1.0 the project is not guaranteed to have a consistent API, which means that types and classes may move around or be removed. That said, we try to be predictable when it comes to breaking changes, so the project uses semantic versioning to help users avoid breakage.

Specifically, new releases increase the `patch` semver component for new features and fixes, and the `minor` component when there are breaking changes. If you don't know much about semver strings, they're usually formatted `{major}.{minor}.{patch}` so increasing the `patch` component means incrementing the last number.

Consider a few examples:

| From Version | To Version | Changes are Breaking |
| :----------: | :--------: | :------------------: |
|    0.2.0     |   0.2.1    |          No          |
|    0.3.2     |   0.3.6    |          No          |
|    0.3.1     |   0.3.17   |          No          |
|    0.2.2     |   0.3.0    |         Yes          |

If you are concerned about breaking changes, you can pin the version in your requirements so that it does not go beyond the current semver `minor` component, for example if the current version was `0.1.37`:

```
mathy_core>=0.1.37,<0.2.0
```

## Contributors

Mathy Core wouldn't be possible without the wonderful contributions of the following people:

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
  <tbody>
    <tr>
      <td align="center" valign="top" width="14.28%"><a target="_blank" href="https://www.justindujardin.com/"><img src="https://avatars0.githubusercontent.com/u/101493?v=4" width="100px;" alt=""/><br /><sub><b>Justin DuJardin</b></sub></a></td>
      <td align="center" valign="top" width="14.28%"><a target="_blank" href="https://github.com/ElSupreme"><img src="https://avatars.githubusercontent.com/u/13594721?v=4" width="100px;" alt=""/><br /><sub><b>JT Stukes</b></sub></a></td>
    </tr>
  </tbody>
</table>

<!-- markdownlint-restore -->
<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!

            

Raw data

            {
    "_id": null,
    "home_page": "https://mathy.ai",
    "name": "mathy-core",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "",
    "keywords": "math",
    "author": "Justin DuJardin",
    "author_email": "justin@dujardinconsulting.com",
    "download_url": "https://files.pythonhosted.org/packages/c3/37/3e3700c353516a8f538ee559c8862bd2fcdf93fdc5b679299a4755683f0a/mathy_core-0.9.4.tar.gz",
    "platform": null,
    "description": "# mathy_core: parse and manipulate math expressions\n\n[![Build](https://github.com/mathy/mathy_core/workflows/Build/badge.svg)](https://github.com/mathy/mathy_core/actions)\n[![Types](https://github.com/mathy/mathy_core/workflows/Types/badge.svg)](https://github.com/mathy/mathy_core/actions)\n[![codecov](https://codecov.io/gh/mathy/mathy_core/branch/master/graph/badge.svg)](https://codecov.io/gh/mathy/mathy_core)\n[![Pypi version](https://badgen.net/pypi/v/mathy-core)](https://pypi.org/project/mathy-core/)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)\n\nMathy core is a python package (_with type annotations_) for working with math problems. It has a tokenizer for converting plain text into tokens, a parser for converting tokens into expression trees, a rule-based system for manipulating the trees, a layout system for visualizing trees, and a set of problem generation functions that can be used to generate datasets for ML training.\n\n## \ud83d\ude80 Quickstart\n\nYou can install `mathy_core` from pip:\n\n```bash\npip install mathy_core\n```\n\n## \ud83d\udcd6 Documentation \n\nCheck out https://core.mathy.ai for API documentation, examples, and more!\n\n## Examples\n\nConsider a few examples to get a feel for what Mathy core does.\n\n### Evaluate an expression\n\nArithmetic is a snap.\n\n```python\nfrom mathy_core import ExpressionParser\n\nexpression = ExpressionParser().parse(\"4 + 2\")\nassert expression.evaluate() == 6\n```\n\n### Evaluate with variables\n\nVariable values can be specified when evaluating an expression.\n\n```python\nfrom mathy_core import ExpressionParser, MathExpression\n\nexpression: MathExpression = ExpressionParser().parse(\"4x + 2y\")\nassert expression.evaluate({\"x\": 2, \"y\": 5}) == 18\n```\n\n### Transform an expression\n\nExpressions can be changed using rules based on the properties of numbers.\n\n```python\nfrom mathy_core import ExpressionParser\nfrom mathy_core.rules import DistributiveFactorOutRule\n\ninput = \"4x + 2x\"\noutput = \"(4 + 2) * x\"\nparser = ExpressionParser()\n\ninput_exp = parser.parse(input)\noutput_exp = parser.parse(output)\n\n# Verify that the rule transforms the tree as expected\nchange = DistributiveFactorOutRule().apply_to(input_exp)\nassert str(change.result) == output\n\n# Verify that both trees evaluate to the same value\nctx = {\"x\": 3}\nassert input_exp.evaluate(ctx) == output_exp.evaluate(ctx)\n```\n\n<!-- ### Visualize a Tree -- needs mathy plugin in docs.sh -->\n<!-- ### Generate Problems -- needs example snippet -->\n\n## Development\n\nInstall the prerequisites in a virtual environment (python3 required)\n\n```bash\nsh tools/setup.sh\n```\n\nRun the test suite and view code-coverage statistics\n\n```bash\nsh tools/test.sh\n```\n\nThe tests cover ~90% of the code so they're a good reference for how to use the various APIs.\n\n## Semantic Versioning\n\nBefore Mathy Core reaches v1.0 the project is not guaranteed to have a consistent API, which means that types and classes may move around or be removed. That said, we try to be predictable when it comes to breaking changes, so the project uses semantic versioning to help users avoid breakage.\n\nSpecifically, new releases increase the `patch` semver component for new features and fixes, and the `minor` component when there are breaking changes. If you don't know much about semver strings, they're usually formatted `{major}.{minor}.{patch}` so increasing the `patch` component means incrementing the last number.\n\nConsider a few examples:\n\n| From Version | To Version | Changes are Breaking |\n| :----------: | :--------: | :------------------: |\n|    0.2.0     |   0.2.1    |          No          |\n|    0.3.2     |   0.3.6    |          No          |\n|    0.3.1     |   0.3.17   |          No          |\n|    0.2.2     |   0.3.0    |         Yes          |\n\nIf you are concerned about breaking changes, you can pin the version in your requirements so that it does not go beyond the current semver `minor` component, for example if the current version was `0.1.37`:\n\n```\nmathy_core>=0.1.37,<0.2.0\n```\n\n## Contributors\n\nMathy Core wouldn't be possible without the wonderful contributions of the following people:\n\n<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->\n<!-- prettier-ignore-start -->\n<!-- markdownlint-disable -->\n<table>\n  <tbody>\n    <tr>\n      <td align=\"center\" valign=\"top\" width=\"14.28%\"><a target=\"_blank\" href=\"https://www.justindujardin.com/\"><img src=\"https://avatars0.githubusercontent.com/u/101493?v=4\" width=\"100px;\" alt=\"\"/><br /><sub><b>Justin DuJardin</b></sub></a></td>\n      <td align=\"center\" valign=\"top\" width=\"14.28%\"><a target=\"_blank\" href=\"https://github.com/ElSupreme\"><img src=\"https://avatars.githubusercontent.com/u/13594721?v=4\" width=\"100px;\" alt=\"\"/><br /><sub><b>JT Stukes</b></sub></a></td>\n    </tr>\n  </tbody>\n</table>\n\n<!-- markdownlint-restore -->\n<!-- prettier-ignore-end -->\n\n<!-- ALL-CONTRIBUTORS-LIST:END -->\n\nThis project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!\n",
    "bugtrack_url": null,
    "license": "All rights reserved",
    "summary": "Computer Algebra System for working with math expressions",
    "version": "0.9.4",
    "project_urls": {
        "Homepage": "https://mathy.ai"
    },
    "split_keywords": [
        "math"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "db0d434ede1bf362f37e9be49a00ddaee6e65c7c71ec1f06eaa838f768e0b1a2",
                "md5": "fc16ac4f2eab80df5cb71bffb3f41858",
                "sha256": "c471f216c55c1c1faf26e9d192d0b4b900b95c0b8ee38bc7d98564d61738400c"
            },
            "downloads": -1,
            "filename": "mathy_core-0.9.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "fc16ac4f2eab80df5cb71bffb3f41858",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 74143,
            "upload_time": "2024-02-06T00:45:11",
            "upload_time_iso_8601": "2024-02-06T00:45:11.911326Z",
            "url": "https://files.pythonhosted.org/packages/db/0d/434ede1bf362f37e9be49a00ddaee6e65c7c71ec1f06eaa838f768e0b1a2/mathy_core-0.9.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c3373e3700c353516a8f538ee559c8862bd2fcdf93fdc5b679299a4755683f0a",
                "md5": "e321a0fdc147fc5389c887fe5fe8cf79",
                "sha256": "69410412be65ec8f57da7dfa6d3eb5ee29c82820d40dbae81b68a45b33ad2a96"
            },
            "downloads": -1,
            "filename": "mathy_core-0.9.4.tar.gz",
            "has_sig": false,
            "md5_digest": "e321a0fdc147fc5389c887fe5fe8cf79",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 59613,
            "upload_time": "2024-02-06T00:45:13",
            "upload_time_iso_8601": "2024-02-06T00:45:13.295012Z",
            "url": "https://files.pythonhosted.org/packages/c3/37/3e3700c353516a8f538ee559c8862bd2fcdf93fdc5b679299a4755683f0a/mathy_core-0.9.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-06 00:45:13",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "mathy-core"
}
        
Elapsed time: 0.21375s