Skip to main content

PDE/ODE math backend

Project description

Demathpy

A Python library for parsing and safely evaluating symbolic Ordinary and Partial Differential Equations (ODEs/PDEs) on numerical grids.

This repository provides:

  • A lightweight symbol normalizer that converts human-readable mathematical notation into valid Python expressions.
  • A secure evaluation engine that evaluates PDE/ODE right-hand sides on NumPy grids without using raw eval on untrusted input.
  • Built-in support for common differential operators and vector calculus notation.

Key Features

1. Symbol Normalization

The parser supports Unicode and mathematical shorthand, including:

  • Greek letters: α, β, γ, λ, ε, φ, θalpha, beta, gamma, lam, epsilon, phi, theta
  • Powers: u², v³u**2, v**3
  • Implicit multiplication:
    • αualpha*u
    • 2u2*u
    • (u+1)(v+1)(u+1)*(v+1)
  • Absolute values: |u|abs(u)
  • Common functions:
    sin, cos, tan, exp, log, tanh, sech, sign, sqrt

2. Differential Operator Support

The parser recognizes and evaluates:

  • First derivatives:
    • du/dt, u_t
  • Second derivatives:
    • dxx(u), dzz(u)
  • Laplacian:
    • ∇²u, lap(u)
  • Gradient:
    • ∇u, grad(u)
  • Divergence:
    • ∇·(A), div(A)

These are mapped to finite-difference operators implemented in NumPy.

3. PDE / ODE Parsing

The library provides:

  • parse_pde(equation: str)
    Parses a PDE string and returns (lhs_variable, rhs_expression).

  • evaluate_rhs(rhs: str, grids: dict, constants: dict, grid_dx: float)
    Safely evaluates the right-hand side on NumPy arrays representing fields and parameters.

Supports equations such as:

∂T/∂t = α ∇²T - σ T
u_t = D dxx(u) + f(u)
∂φ/∂t = ∇·((1 + T²) ∇φ)

4. Secure Evaluation Environment

Only a restricted set of functions and operators are exposed. No access to Python builtins, file I/O, or unsafe functions. All variables must come from: grids: NumPy arrays for fields (u, T, phi, etc.) constants: scalar parameters (alpha, beta, lambda, etc.)

5. Coordinate-Aware PDEs

The evaluator automatically provides spatial coordinate grids: x, z as NumPy arrays derived from grid shape and spacing. This enables anisotropic and spatially varying coefficients such as: α x dxx(T) + α z dzz(T) Typical Use:

from pde import parse_pde, evaluate_rhs

lhs, rhs = parse_pde("∂T/∂t = α ∇²T - σ T")

result = evaluate_rhs(
    rhs,
    grids={"T": T_grid},
    constants={"alpha": 0.1, "sigma": 0.01},
    grid_dx=0.01
)

6. Purpose

This project is intended as a safe mathematical expression parser for: Scientific computing PDE/ODE solvers Physics and engineering simulations Educational or sandboxed equation evaluation

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

demathpy-0.0.1.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

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

demathpy-0.0.1-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file demathpy-0.0.1.tar.gz.

File metadata

  • Download URL: demathpy-0.0.1.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.27 {"installer":{"name":"uv","version":"0.9.27","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for demathpy-0.0.1.tar.gz
Algorithm Hash digest
SHA256 d893db19d77dc371db61f146d51417ebc62beb330b6b4cc5947dbfdbc624864a
MD5 226c901060855855a15361e611ef741b
BLAKE2b-256 7035d1e49e855e78742b0ea23a47178bd325c9e8242e6558dcd821035ebfcdc2

See more details on using hashes here.

File details

Details for the file demathpy-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: demathpy-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 13.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.27 {"installer":{"name":"uv","version":"0.9.27","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for demathpy-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b422c70cff1c27cb9631a46b6d9d38653b39ada0425e2968397114af86b55491
MD5 5504e3c70e1433517896dda6b7b37297
BLAKE2b-256 1b9eb28e178e5dcc368fade74d9f3e4d490cea63406169f32f41345b60bb9853

See more details on using hashes here.

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