Skip to content
oeistoolsPublic

About

ER2 is Python with mathematics built in: symbolic syntax, exact arithmetic by default, and computational number theory powered by PARI/GP

Topics

Resources

Contributing

Stars

2 stars

Watchers

0 watching

Forks

Repository files navigation

ER2 — Mathematical Python

PyPI CI Python License: MIT ER2 — mathematical Python

ER2 is Python with mathematics built in: symbolic syntax, exact arithmetic by default, and computational number theory powered by PARI/GP.

It is named after the Hungarian mathematician Paul Erdős. In Spanish, "Erdős" sounds like "ER-dos", which is where ER2 comes from.

Why ER2?

  • Write mathematics as mathematics. x^2 is a power, 1/3 is exactly one third, and sym x declares a symbol. There are no imports and no Rational(1, 3).
  • Two engines, one language. ER2 sends each computation to the right backend. PARI/GP, one of the fastest number theory systems, computes integers, primes and modular arithmetic, and also the algebra of symbols: polynomials, rational functions and sin, cos, exp… of linear arguments live in PARI in canonical form (sin(x)^2 + cos(x)^2 is 1 as soon as it is written), tens to thousands of times faster than SymPy. SymPy covers the rest of calculus and simplification.
  • It is still Python. All Python syntax works, every library imports as usual, and ER2 runs in Jupyter and Quarto with LaTeX output.

Quick start

ER2 is on PyPI as er2. It needs Python ≥ 3.12. PARI comes inside the cypari2 wheel, so there is nothing else to install.

Platform Status
Linux ✅ Supported, and tested in CI
macOS ✅ Supported, Apple Silicon and Intel, and tested in CI
Windows ❌ Not supported yet: cypari2 has no Windows build, in progress upstream in cypari2#186 (WSL works, as Linux)
pip install er2                 # or: uv tool install er2
pip install "er2[jupyter]"      # with the Jupyter kernel
pip install "er2[oeis]"         # with access to the OEIS

Save this as hello.er2 and run er2 hello.er2:

print("Hello from ER2")
print(2^10, 1/3 + 1/6)      # ^ is power; division is exact
sym x
print(expand((x + 1)^3))    # symbolic algebra
print(isprime(2^127 - 1))   # number theory, by PARI
Hello from ER2
1024 1/2
x^3 + 3*x^2 + 3*x + 1
True

Then try the REPL (er2) or open a notebook: see Jupyter and Quarto. er2 --show-python hello.er2 shows the Python that ER2 generates. To work on ER2 itself, clone the repository instead: see Development.

A taste of ER2

import numpy as np            # any Python library, imported as usual

sym x                         # declare a symbol
f = x^2 + 2*x + 1             # ^ means power

print(factor(f))              # (x + 1)^2
print(diff(f, x))             # 2*x + 2
print(latex(f))               # x^{2} + 2 x + 1  (any expression → LaTeX)

print(1/3)                    # 1/3, exact and not 0.333...
print(isprime(2^521 - 1))     # True  (PARI)
print(phi(123456789))         # 82260072
print(factor(2^127 - 1))      # 2^127 - 1 is prime
print(factor(360))            # 2^3 * 3^2 * 5

for k in range(1, 20):        # ordinary Python
    if isprime(k):
        print(k)

np.zeros(2^3)                 # ER2 numbers pass straight into libraries

Examples

Each program runs with uv run er2 examples/<name>.er2, and its expected output is checked by the test suite.

Example What it shows
hello.er2 The smallest tour: power, exact division, algebra, a primality test
syntax.er2 Every ER2 syntax difference from Python: ^, ^^, exact literals, sym, 5r
factorization.er2 One factor for integers, rationals and polynomials; partial factorizations
mvp.er2 The MVP program: CAS and number theory together
demo.qmd A Quarto tour with rendered math, including the OEIS (make render)
mvp.ipynb The MVP as a Jupyter notebook

Documentation

The documentation site is at https://oeistools.github.io/ER2/, built by Quarto and executed by ER2 itself.

If you want to… Read
Use ER2 This README, the examples, and docs/PARI_FUNCTIONS.md (every PARI function and its ER2 name)
Decide whether ER2 fits docs/COMPARISON.md: what ER2 gives over Python, SymPy, SageMath and PARI/GP, the measured costs, and where it stops
Know exactly what the language is docs/LANGUAGE.md: the normative specification — the differences from Python, precedence, the number model, and what is stable before 1.0
Understand the design ARCHITECTURE.md: the compatibility contract, the components, and the design decisions D1–D24
Follow the project PLAN.md (milestones and acceptance criteria), CHANGELOG.md (releases and versioning policy), docs/BENCHMARKS.md
Contribute docs/OVERVIEW.md (the pipeline and the modules in two pages), then CONTRIBUTING.md: setup, tests, rules for each part of the code, good first contributions

The rules for AI-assisted development are in CLAUDE.md.

Algebra (0.5)

sym x, y

det(Matrix([[2, 1], [1, 3]]))        # 5, computed by PARI
kernel(Matrix([[1, 2], [2, 4]]))     # [Matrix([[1], [-1/2]])]
smith_form(Matrix([[2, 4], [6, 8]])) # Matrix([[2, 0], [0, 4]]), as SymPy's

factor(x^8 - x, modulus=2)           # x*(x + 1)*(x^3 + x + 1)*(x^3 + x^2 + 1)
isirreducible(x^2 + x + 1, modulus=2)   # True

a = GF(9).gen()                      # the finite field with 9 elements
a^4, a.order(), minpoly(a)           # 2, 8, x^2 + x + 2

resultant(x^2 + 1, x^3 - 2, x)       # 5
discriminant(x^3 + x + 1)            # -31

G = groebner([x^2 + y^2 - 1, x - y], x, y)
list(G)                              # [x - y, 2*y^2 - 1]
reduce(x^2 + y^2, G)                 # 1, the normal form

K = NumberField(x^2 + 5)             # Q(sqrt(-5))
K.discriminant, K.class_number()     # -20, 2
K.factor(2)                          # [((2, x + 1), 2)]: 2 ramifies
K.factor(3)                          # 3 splits into two primes

resultant follows the standard sign convention, which is PARI's, so resultant(f, g) and resultant(g, f) differ in sign when both degrees are odd (ARCHITECTURE.md §6, D16). A class number is only as good as the GRH unless you ask for certify=True.

Symbolic core and more (0.9)

Values built from symbols live in PARI, in canonical form: identities hold as soon as they are written, and algebra and calculus run tens to thousands of times faster than SymPy, with SymPy's answers (docs/LANGUAGE.md §6.2).

sym x, n

(x + 1)^2                        # x^2 + 2*x + 1
sin(x)^2 + cos(x)^2              # 1, decided when built
sin(x)^2                         # 1/2 - cos(2*x)/2
diff(tan(x), x)                  # 2/(cos(2*x) + 1)
integrate(x*exp(x), x)           # (x - 1)*exp(x)
series(tan(x), x, 0, 6)          # x + x^3/3 + 2*x^5/15 + O(x^6)

interpolate([1, 4, 9], x)        # x^2
summation(x^3, (x, 1, n))        # n^4/4 + n^3/2 + n^2/4
nroots(x^2 - 2, n=5)             # [-1.4142, 1.4142]
factor(x^4 - 10*x^2 + 1, extension=sqrt(2))
                                 # (x^2 - 2*sqrt(2)*x - 1)*(x^2 + 2*sqrt(2)*x - 1)

E = EllipticCurve(-1, 1)         # y^2 = x^3 - x + 1
E.conductor(), E.rank()          # 92, 1
E.reduce(7).order(), E.ap(7)     # 12, -4
EllipticCurve(0, 1).torsion_points()
                                 # [O, (-1, 0), (0, 1), (0, -1), (2, 3), (2, -3)]

piltz_tau(12, 3), dedekind_psi(10, 2), carmichael(15)   # 18, 130, 4

@multiplicative                  # your own arithmetic function, from p^e
def psi2(p, e):
    return p^(2*e - 2) * (p^2 + 1)
psi2(10)                         # 130

Python compatibility

ER2 is a superset of Python and follows the same model as SageMath:

  • All Python syntax works: classes, decorators, generators, async, match, f-strings, type hints, and the rest.

  • The whole ecosystem is available through normal import: NumPy, SciPy, pandas, Matplotlib, and so on.

  • Only .er2 sources are translated. Your .py modules and installed libraries run as ordinary Python and are never modified.

  • Inside .er2 files there are only a few deliberate differences:

    Construct Python ER2
    a ^ b XOR power
    a ^^ b syntax error XOR
    1/3 0.333… exact rational 1/3
    sym x, y syntax error declares symbols x, y
    5r syntax error 5 (deprecated)

Jupyter and Quarto

ER2 runs in notebooks and in Quarto documents:

  • ER2 kernel: run er2 kernel install, then pick "ER2" in Jupyter. In Quarto, set jupyter: er2 in the front matter. If Quarto or your editor reports Jupyter kernel 'er2' not found, it is only looking inside the project's virtual environment: run er2 kernel install --sys-prefix as well. In VS Code, the Quarto Preview button uses the system Python unless QUARTO_PYTHON is set. Add "terminal.integrated.env.linux": {"QUARTO_PYTHON": "${workspaceFolder}/.venv/bin/python"} to .vscode/settings.json.
  • Any Python kernel: add %load_ext er2 in the first cell.
  • ```{er2} cells in Quarto: with the ER2-ENGINE extension (quarto add oeistools/ER2-ENGINE, then engine: er2 in the front matter), cells are written ```{er2} and highlighted as ER2. Without it, Quarto does not execute them, so cells stay ```{python} (docs/LANGUAGE.md §10 R2).
  • LaTeX everywhere: expressions render as math. show(f) displays one, and `{python} latex(f)` puts inline math in Quarto text.
---
title: "Mersenne primes"
jupyter: er2
---

```{python}
[p for p in range(2, 130) if isprime(2^p - 1)]
```

Ecosystem

ER2 is the language; three sibling repositories of the oeistools organization (topic er2) work with it:

Repository What it is Install
ER2 The language: preparser, runtime, PARI and SymPy backends, the er2 kernel pip install er2
ER2-ENGINE Quarto engine: ```{er2} cells with engine: er2 (LANGUAGE.md §10 R2, D31) quarto add oeistools/ER2-ENGINE
ER2-VSCODE VS Code language support: highlighting of .er2 files the .vsix of its repository
PARI-GP-ENGINE The same Quarto engine for PARI/GP code, ER2-ENGINE's sibling quarto add oeistools/PARI-GP-ENGINE

Each is versioned and released on its own (PyPI, Quarto, VS Code), and follows ER2's docs/LANGUAGE.md as the definition of the language.

How it works

ER2 does not introduce a new interpreter. It has three parts:

.er2 source ──► preparser ──► plain Python ──► CPython
                                                  │
                                    ER2 runtime (types, printing, dispatch)
                                          │                 │
                                        SymPy          cypari2 → PARI
                                 (general calculus)   (number theory, polynomials,
                                                       rational and elementary functions)
  1. Preparser. A token-level, source-to-source translation from .er2 to Python.
  2. Runtime. The er2 package provides the mathematical types and the functions (factor, diff, phi, …).
  3. Backends. PARI, through cypari2, computes number theory and the symbolic values ER2 keeps in canonical form (rational functions, and sin, cos, exp… of linear arguments); SymPy handles the rest of symbolic computation. ER2 chooses the backend automatically and both give the same answer, so factor works on both polynomials and integers.

Status and roadmap

Version 0.9.1, early development (prepared 2026-12-01, not yet a release). Milestones M1–M7 are done (M3 was the MVP). Algebra (matrices, finite fields, F_p, resultants, Gröbner bases, NumberField), series (power, Dirichlet, Euler products) and the language specification are all in. docs/LANGUAGE.md now defines the language normatively, and CHANGELOG.md says what is stable, what is still experimental, and what has changed. 0.9.0, the symbolic core on PARI, is on PyPI, the documentation site is live, and docs/STABILITY.md sets the stability policy. What remains is 1.0 itself, which freezes the syntax and the public API.

Version Focus Status
0.1 Preparser: sym, ^, _x, exact integers and rationals; Jupyter kernel, %load_ext er2, Quarto ✅
0.2 CAS on SymPy: expand, factor, simplify, diff, integrate, limit, solve, series ✅
0.3 Number theory on PARI: primality, factorization, phi, sigma, mu, … (the MVP) ✅
0.4 Automatic backend selection, PARI types (Mod, Qfb, series), benchmarks, the OEIS ✅
0.5 Algebra: matrices, finite fields, resultants, Gröbner bases, number fields ✅
0.6 Series: power, Dirichlet, Euler products ✅
0.7 The language specification, docs/LANGUAGE.md; first PyPI release ✅
0.8 Speed: integers are plain int, PARI loads on demand, faster rationals and matrices; better REPL and errors ✅
0.9 Symbolic core on PARI: rational functions, sin/cos/exp… of linear arguments in canonical form, solve and linear systems (D25–D28); elliptic curves; arithmetic functions and the user's GP functions (D29, D30) ✅
1.0 Frozen syntax and public API (documentation site and stability policy done) in progress

Beyond the table, every PARI function is available as pari.<name>, and PARI's sums and integrals take Python functions (pari.sum(lambda n: 1/n^2, 1, 10)). Your own GP functions too: put .gp files in ~/.config/er2/gp/ (or in the directories of ER2_GP_PATH) and ER2 reads them when PARI starts, so jordantot(n,k)=... in a file is pari.jordantot(10, 2). A .gp file runs as PARI code when it is read, so keep there only files you trust. Deeper CPython integration comes only after the syntax is stable.

Development

make install                    # uv sync + the ER2 kernel inside .venv
make test-fast                  # the tests without Jupyter and Quarto
make check                      # ruff, mypy and all tests, as in CI
make install-global             # the `er2` command for your user, editable (uv tool)
make preview FILE=my_doc.qmd    # live Quarto preview with the ER2 kernel

make lists every target. make install-global puts er2 in ~/.local/bin. It is an editable install, so it follows changes to the code, and it registers the ER2 kernel for your user.

We keep dependencies deliberately minimal: sympy and cypari2 at runtime, plus ipykernel and oeis-tools as the optional er2[jupyter] and er2[oeis] extras. Development uses pytest and ruff. The code follows PEP 8.

Contributing

ER2 is maintained by Enrique Pérez Herrero (energycode.org@gmail.com).

Contributions are welcome, from a bug report with a three-line example to a new feature. CONTRIBUTING.md explains how to set up, what a pull request needs, and where to start if you are new to the project.

Citation

If you use ER2 in your work, please cite it. The metadata is in CITATION.cff, and GitHub shows it through "Cite this repository". Please also cite PARI/GP and SymPy, which ER2 builds on.

License

(c) Enrique Pérez Herrero. MIT

About

ER2 is Python with mathematics built in: symbolic syntax, exact arithmetic by default, and computational number theory powered by PARI/GP

Topics

Resources

Contributing

Stars

2 stars

Watchers

0 watching

Forks

Releases

Used by

Contributors

Languages