Skip to main content

mathy_core: parse and manipulate math expressions

Build Types codecov Pypi version Code style: 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:

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.

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.

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.

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)

Development

Install the prerequisites in a virtual environment (python3 required)

sh tools/setup.sh

Run the test suite and view code-coverage statistics

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:


Justin DuJardin

JT Stukes

This project follows the all-contributors specification. Contributions of any kind welcome!

Release files for mathy-core 0.9.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mathy-core 0.9.3
File Size Uploaded
mathy_core-0.9.3.tar.gz 58.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mathy-core 0.9.3
File Interpreter ABI Platform
mathy_core-0.9.3-py3-none-any.whl Python 3 none any Details

Total release size: 131.3 kB

Release files / mathy_core-0.9.3.tar.gz

Download URL mathy_core-0.9.3.tar.gz
Size 58.8 kB
Tags Source
SHA-256 checksum
How to use checksums
6339999bd6c086ecfa68004cd965ead90a1b53f558e25da2bd5219dbf2fbc6ee
BLAKE2b-256 checksum
How to use checksums
8f9a9422a1be319ecaf56b2a01ef44bc4f7e978402915ad9f14d94ba970b7e48
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / mathy_core-0.9.3-py3-none-any.whl

Download URL mathy_core-0.9.3-py3-none-any.whl
Size 72.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9ef2363c0aec1a4ef04f9447c7dc0bbac43f6de0bd912e33622d729f42c3f263
BLAKE2b-256 checksum
How to use checksums
bb8446c86900a336d7a8beb727f9e5a173fd4c07125228d5faa23962c10eab2c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release history Release notifications | RSS feed

0.9.5

2 release files

0.9.4

2 release files

This release

0.9.3 This release

2 release files

0.9.2

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.6

2 release files

0.8.5

2 release files

0.8.4

2 release files

0.8.3

2 release files

0.8.2

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.20

2 release files

0.7.19

2 release files

0.7.18

2 release files

0.7.17

2 release files

0.7.16

2 release files

0.7.14

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page