Write math, run code. A DSL for mathematical physics.
Project description
PhysLang
Write math, run code.
PhysLang is a domain-specific language that lets you write mathematical notation directly and execute it as Python. No more translation errors between paper and code.
> let beta = 1.0
> let mu = U/2
> compute sum(n, 1, 100, n)
= 5050
> compute integrate(sin(x), x, 0, pi)
= 2.0
Installation
pip install physlang
That's it! The first time you run physlang, it automatically:
- Creates a desktop shortcut with the Ĥ (Hamiltonian) icon
- Adds PhysLang IDE to your applications menu
For Students
Just run these two commands:
pip install physlang
physlang
You'll see the Ĥ icon appear on your desktop. Double-click it anytime to open the IDE!
Manual Desktop Setup
If the automatic setup didn't work, you can run:
physlang --install # Add desktop shortcut
physlang --uninstall # Remove it
Optional: JAX Support
For GPU acceleration and automatic differentiation:
pip install physlang[jax]
Quick Start
Command Line
# Start interactive REPL
physlang
# Execute single expression
physlang -c "sum(n, 1, 100, n)"
# Execute file
physlang program.phi
# Show generated Python
physlang -p -c "integrate(x^2, x, 0, 1)"
# Use JAX backend
physlang -j -c "grad(f, 3.0)"
Web IDE
PhysLang includes a full web-based IDE with symbol palette, real-time translation, and code export:
# Open the IDE in your browser
physlang --ide
# Or start with a local server (for some browsers)
physlang --ide --serve
From Python:
from physlang import launch_ide
launch_ide()
The IDE features:
- Math Editor: Write with Unicode symbols (α, β, ∑, ∫, ², etc.)
- Symbol Palette: Click to insert Greek letters, operators, subscripts/superscripts
- Auto-conversion: Type
\alpha→ α,^2→ ²,\sum→ ∑ - Real-time Translation: See Python/JAX code generated instantly
- Export: Download as .txt, .py, or JAX .py
- Works Offline: Fully self-contained, no server needed
Python API
from physlang import run, compile_to_python, compile_to_jax
# Execute directly
result = run("sum(n, 1, 100, n)") # 5050
# Get Python code
code = compile_to_python("let f(x) = x^2\ncompute f(5)")
print(code)
# Get JAX code (GPU + autodiff)
jax_code = compile_to_jax("let f(x) = x^2\ncompute grad(f, 3.0)")
Typing Math
PhysLang accepts both Unicode math symbols and LaTeX notation. Type whichever is easier:
| LaTeX | Unicode | Description |
|---|---|---|
\alpha |
a |
Greek letters |
\beta |
b |
|
\pi |
p |
|
\sum |
`` | Summation |
\int |
`` | Integral |
\infty |
`` | Infinity |
^2 |
`` | Superscripts |
_1 |
`` | Subscripts |
\dagger |
`` | Hermitian conjugate |
\leq |
`` | Less or equal |
\RR |
`` | Real numbers |
In the REPL, press Tab after typing LaTeX to convert it to Unicode.
Language Reference
Variables
let x = 5
let beta = 1.0
let mu = U/2
Functions
let f(x) = x^2 + 2*x + 1
let g(x, y) = x^2 + y^2
compute f(5) # 36
Arithmetic
3 + 4 * 2 # 11
2^10 # 1024 (or 2 for Unicode)
sqrt(2) # 1.414...
exp(1) # 2.718...
log(e) # 1.0
sin(pi/2) # 1.0
Summation
# ASCII syntax
sum(n, 1, 100, n) # 5050
sum(k, 1, 10, k^2) # 385
# Unicode syntax (equivalent)
sum_{n=1}^{100} n # 5050
Integration
# Definite integral
integrate(sin(x), x, 0, pi) # 2.0
integrate(x^2, x, 0, 1) # 0.333...
# Unicode syntax
_0^ sin(x) dx # 2.0
Linear Algebra
let A = matrix([[1, 2], [3, 4]])
compute det(A) # -2.0
compute trace(A) # 5.0
compute norm(array(3, 4)) # 5.0
compute inv(A) # inverse
compute eig(A) # eigenvalues
Quantum Notation
# Hermitian conjugate
A^dagger # or A
# Commutator
comm(A, B) # [A, B] = AB - BA
# Anticommutator
anticomm(A, B) # {A, B} = AB + BA
# Trace
Tr(rho * H)
# Tensor product
kron(A, B) # A x B
Operators (Quantum Field Theory)
operator c[i, sigma] : fermion
operator a[k] : boson
operator S[i] : spin
JAX Backend
Use -j flag or compile_to_jax() for:
- GPU acceleration
- Automatic differentiation
- JIT compilation
from physlang import compile_to_jax
code = compile_to_jax("""
let f(x) = x^3 + 2*x^2 + x
compute grad(f, 2.0)
""")
# Computes df/dx at x=2.0 using JAX autodiff
Built-in Functions
Math Functions
sin, cos, tan, exp, log, log10, sqrt, abs
sinh, cosh, tanh, arcsin, arccos, arctan
floor, ceil, round, min, max
factorial, gamma
Linear Algebra
det, trace, norm, inv, eig, eigvals
matrix_power, kron, outer, inner, cross
Array Operations
array, matrix, zeros, ones, eye, diag
Calculus
sum(i, start, end, expr) - Summation
prod(i, start, end, expr) - Product
integrate(expr, var, lower, upper) - Integration
grad(f, x) - Gradient (JAX only)
hessian(f) - Hessian matrix (JAX only)
Examples
Gauss Sum
compute sum(n, 1, 100, n) # = 100*101/2 = 5050
Basel Problem
let s = sum(n, 1, 10000, 1/n^2)
compute s # approximates pi^2/6
Gaussian Integral
compute integrate(exp(-x^2), x, -10, 10) # approximates sqrt(pi)
Matrix Operations
let A = matrix([[1, 2], [3, 4]])
let B = matrix([[5, 6], [7, 8]])
compute A @ B # matrix multiplication
compute det(A @ B) # determinant of product
Gradient Descent (JAX)
let f(x) = (x - 3)^2
let x = 0.0
let lr = 0.1
# x = x - lr * grad(f, x) # update step
File Extension
PhysLang files use .phi extension:
physlang program.phi
Why PhysLang?
Every physics paper has equations. Implementing them in code introduces bugs:
mu = 0instead ofmu = U/2- Wrong index ordering in tensor products
- Missing signs in commutators
- Incorrect Metropolis-Hastings ratios
PhysLang eliminates translation errors by letting you write the math directly.
Links
Contributing
Contributions welcome. See CONTRIBUTING.md.
License
Apache License 2.0
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 physlang-0.1.1.tar.gz.
File metadata
- Download URL: physlang-0.1.1.tar.gz
- Upload date:
- Size: 90.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
842bea90b2c7182dbe6e916e2ea536161c3447734c32112047f1cf2447ec3441
|
|
| MD5 |
9545b1dafba26a818c309430170155c9
|
|
| BLAKE2b-256 |
b24ffd65e13ad3e08b2d7c9050ac883252ef0c541d53be148270516f56c5a284
|
File details
Details for the file physlang-0.1.1-py3-none-any.whl.
File metadata
- Download URL: physlang-0.1.1-py3-none-any.whl
- Upload date:
- Size: 100.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3c44c43ca6e51136213f06145a1b910b24c3d3ec2f1dd59ff669ec9d0874dccc
|
|
| MD5 |
4d7824050d6fe3513a67316c7fcb866e
|
|
| BLAKE2b-256 |
4da422124eb549685d10ac080a9c77a091fd42c3dea804cc2150e31095bb2798
|