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
evalon 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:
αu→alpha*u2u→2*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
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 demathpy-0.0.2.tar.gz.
File metadata
- Download URL: demathpy-0.0.2.tar.gz
- Upload date:
- Size: 11.7 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
491bf86a6478e20cd78ea237852bd6c6f90f1567ddaea82e0f737cfaddd3f853
|
|
| MD5 |
04a8ebd03c6a148be4d5a6792f577295
|
|
| BLAKE2b-256 |
8f07489668b2cf85ec0749ecedcf52976cff9da0e5575a7a2e1688c48bd28e66
|
File details
Details for the file demathpy-0.0.2-py3-none-any.whl.
File metadata
- Download URL: demathpy-0.0.2-py3-none-any.whl
- Upload date:
- Size: 13.6 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
542117b1f6bd0dd002105a242ebd9cabbbe6b323999944d6f64684733b866a3a
|
|
| MD5 |
3b54aeeef2c653272ed20aca96692391
|
|
| BLAKE2b-256 |
af858aa94f7167a9bc7ce050d654435714cd58053ac7fe8caf4a86465cebb174
|