Parse custom math notation and evaluate symbolically with SymPy
Project description
mathio
Parse custom mathematical notation and evaluate symbolically with SymPy. Also supports generating Wolfram Language code as an opt-in backend.
Installation
pip install mathio-py
SymPy is installed automatically as a required dependency.
Quick Start
from mathio import convert, to_wolfram
# Evaluate symbolically (default)
convert("sin(π/4)") # Unicode pretty-print
convert("sin(π/4)", fmt="plain") # "sqrt(2)/2"
convert("sin(π/4)", fmt="latex") # "\frac{\sqrt{2}}{2}"
# Generate Wolfram Language code
to_wolfram("sinx") # "Sin[x]"
to_wolfram("int sinx d x") # "Integrate[Sin[x], x]"
CLI Usage
python -m mathio "sin(π/4)" # SymPy evaluation (pretty)
python -m mathio --plain "sin(π/4)" # SymPy (plain text)
python -m mathio --latex "sin(π/4)" # SymPy (LaTeX)
python -m mathio --wolfram "sinx" # Wolfram Language code
python -m mathio --wolfram -f input.txt # Batch Wolfram generation
Grammar
expression : add_expr
add_expr : mul_expr (('+'|'-') mul_expr)*
mul_expr : power_expr (('*'|'/') power_expr)*
power_expr : implicit_mul ('^' power_expr)? (right-associative)
implicit_mul : factor+ (n-ary, left-associative)
factor : integral | derivative | func_app | unary_minus | atom
integral : 'int' ('[' expression ',' expression ']')? expression 'd' IDENT
derivative : DERIV implicit_mul
func_app : FUNC '(' expression ')'
| FUNC implicit_mul (stops at next FUNC)
unary_minus : '-' factor
atom : NUMBER | CONSTANT | IDENT | '(' expression ')'
Lexical Rules
- Keyword splitting: Reserved keywords are split from adjacent identifiers via longest-prefix matching. e.g.
sinx→sin+x,density→d+e+nsity. - DERIV:
d/dx,d/dy,d/dthetaare recognized as a single derivative token. - Numbers: Integers, decimals, and scientific notation. The
ein2e1is scientific notation;2eis2 * e. - Unicode constants:
π→pi,∞→infty.
More Examples
from mathio import convert, to_wolfram
# SymPy evaluation
convert("sinx") # sin(x)
convert("int sinx d x") # -cos(x)
convert("d/dx sinx") # cos(x)
convert("int [0,1] x d x") # 1/2
convert("2^10") # 1024
convert("1/3 + 1/6") # 1/2
# Wolfram generation
to_wolfram("sinπx") # Sin[Times[Pi, x]]
to_wolfram("4x/3y") # Times[Times[4, x], Power[Times[3, y], -1]]
to_wolfram("d/dx sinx cosx") # D[Times[Sin[x], Cos[x]], x]
to_wolfram("sin cos x y") # Sin[Cos[Times[x, y]]]
to_wolfram("sin x ^ 2") # Power[Sin[x], 2]
Architecture
mathio/
tokenizer.py # Custom lexer with keyword splitting & DERIV
parser.py # Recursive-descent parser → AST
ast_nodes.py # AST node dataclasses
sympy_executor.py # AST → SymPy evaluation (default backend)
wolfram_generator.py # AST → Wolfram Language code (opt-in)
converter.py # Facade: tokenize → parse → execute/generate
config.py # Function/constant/keyword mappings
errors.py # Exception classes with source-context errors
__main__.py # CLI entry point
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mathio_py-0.1.0.tar.gz.
File metadata
- Download URL: mathio_py-0.1.0.tar.gz
- Upload date:
- Size: 16.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5835da7894ddd8492ef4b5a00ffefac5d3b24b5bc30a941d52f24e7139915321
|
|
| MD5 |
21908d29b64992f9b99cb1d23e864cc3
|
|
| BLAKE2b-256 |
fc94e91a37363b61deb5595442f459926a9bc87b42f33579f32d0704200ab6b3
|
Provenance
The following attestation bundles were made for mathio_py-0.1.0.tar.gz:
Publisher:
publish.yml on CrispyXYZ/mathio
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mathio_py-0.1.0.tar.gz -
Subject digest:
5835da7894ddd8492ef4b5a00ffefac5d3b24b5bc30a941d52f24e7139915321 - Sigstore transparency entry: 1717848272
- Sigstore integration time:
-
Permalink:
CrispyXYZ/mathio@4a513528cf29cdca3c94f44a210174ecd0dc914e -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/CrispyXYZ
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4a513528cf29cdca3c94f44a210174ecd0dc914e -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file mathio_py-0.1.0-py3-none-any.whl.
File metadata
- Download URL: mathio_py-0.1.0-py3-none-any.whl
- Upload date:
- Size: 14.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
99ca9e38e300902859db19fe177651567880137dbc1fb9375355ecd8ea2fcc84
|
|
| MD5 |
97431509e4227b702a585657c31aa71e
|
|
| BLAKE2b-256 |
8eb24376bee2ed8e12fff7ff4776d2614c0fac068f45cd8062e8162edc6b7616
|
Provenance
The following attestation bundles were made for mathio_py-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on CrispyXYZ/mathio
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mathio_py-0.1.0-py3-none-any.whl -
Subject digest:
99ca9e38e300902859db19fe177651567880137dbc1fb9375355ecd8ea2fcc84 - Sigstore transparency entry: 1717848416
- Sigstore integration time:
-
Permalink:
CrispyXYZ/mathio@4a513528cf29cdca3c94f44a210174ecd0dc914e -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/CrispyXYZ
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4a513528cf29cdca3c94f44a210174ecd0dc914e -
Trigger Event:
workflow_dispatch
-
Statement type: