Skip to main content

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. sinxsin + x, densityd + e + nsity.
  • DERIV: d/dx, d/dy, d/dtheta are recognized as a single derivative token.
  • Numbers: Integers, decimals, and scientific notation. The e in 2e1 is scientific notation; 2e is 2 * 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


Download files

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

Source Distribution

mathio_py-0.1.0.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mathio_py-0.1.0-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

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

Hashes for mathio_py-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5835da7894ddd8492ef4b5a00ffefac5d3b24b5bc30a941d52f24e7139915321
MD5 21908d29b64992f9b99cb1d23e864cc3
BLAKE2b-256 fc94e91a37363b61deb5595442f459926a9bc87b42f33579f32d0704200ab6b3

See more details on using hashes here.

Provenance

The following attestation bundles were made for mathio_py-0.1.0.tar.gz:

Publisher: publish.yml on CrispyXYZ/mathio

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for mathio_py-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 99ca9e38e300902859db19fe177651567880137dbc1fb9375355ecd8ea2fcc84
MD5 97431509e4227b702a585657c31aa71e
BLAKE2b-256 8eb24376bee2ed8e12fff7ff4776d2614c0fac068f45cd8062e8162edc6b7616

See more details on using hashes here.

Provenance

The following attestation bundles were made for mathio_py-0.1.0-py3-none-any.whl:

Publisher: publish.yml on CrispyXYZ/mathio

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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