Get started
Install
pip install er2That is everything. PARI comes inside the cypari2 wheel, so there is no system PARI/GP to install, and SymPy arrives with it. ER2 needs Python 3.12 or later.
For notebooks, ask for the extra:
pip install "er2[jupyter]"
er2 kernel installA first program
Put this in first.er2:
sym x
f = x^2 + 2*x + 1
print(factor(f)) # (x + 1)^2
print(1/3) # 1/3, exact
print(isprime(2^521 - 1)) # Trueand run it:
er2 first.er2To see what the preparser produced — which is ordinary Python — ask for it:
er2 --show-python first.er2There is also a REPL: run er2 with no arguments. It keeps its history between sessions, completes names with Tab, and factor? shows the help of factor.
Python and ER2 side by side
The same lines in both, with what each prints. On the Python side the mathematical functions come from SymPy, which ER2 uses underneath.
| You write | Python prints | ER2 prints |
|---|---|---|
1/3 |
0.3333333333333333 |
1/3 |
1/3 + 1/6 |
0.5 |
1/2 |
10/4, 10//4 |
2.5, 2 |
5/2, 2 |
0.1 + 0.2 |
0.30000000000000004 |
0.30000000000000004 (floats are unchanged) |
2^100 |
102 (XOR) |
1267650600228229401496703205376 |
7 ^^ 2 |
syntax error | 5 (XOR; Python’s 7 ^ 2) |
sym x |
syntax error | declares the symbol x (Python: x = sympy.symbols("x")) |
factor(x^2 - 1) |
needs sympy.factor(x**2 - 1): (x - 1)*(x + 1) |
(x - 1)*(x + 1) |
expand((x + 1)^3) |
sympy.expand: x**3 + 3*x**2 + 3*x + 1 |
x^3 + 3*x^2 + 3*x + 1 |
factor(360) |
sympy.factorint(360): {2: 3, 3: 2, 5: 1} |
2^3 * 3^2 * 5 |
isprime(2^127 - 1) |
sympy.isprime(2**127 - 1): True |
True, proven by PARI |
phi(36) |
sympy.totient(36): 12 |
12 |
Mod(3, 7)^-1 |
pow(3, -1, 7): 5 |
Mod(5, 7) |
f"{a^2=}" with a = 3 |
(f"{a**2=}") a**2=9 |
a^2=9 |
Everything not in this table — def, class, import, lists, loops, any library — is Python, unchanged.
The three things to know
^ is power, not XOR. This is the one change that can silently alter pasted Python. Write ^^ when you want XOR. ER2 warns when a ^ shares a line with obvious bit manipulation.
Division is exact. 1/3 is the rational one third, not 0.333…, and 2^-1 is 1/2. Integers are ordinary Python ints, as fast as in Python; only / and ** change, and only between integers and rationals: a float anywhere gives a float, as in Python.
sym x declares a symbol. Or use the predefined _x, _y, _z, _n, _k, _p without declaring anything. Values built from symbols are kept in canonical form: (x + 1)^2 prints x^2 + 2*x + 1, sin(x)^2 prints 1/2 - cos(2*x)/2, and sin(x)^2 + cos(x)^2 is 1; factor gives the factored form. ER2 0.9 adds elliptic curves (EllipticCurve), interpolate, summation, nroots, arithmetic functions PARI lacks (piltz_tau, carmichael, …, @multiplicative for your own) and your own GP files (~/.config/er2/gp/); the README shows them.
Those, plus 5r (deprecated: it changes nothing since integers are int) and the way f"{a^2=}" echoes ER2 rather than the generated Python, are the whole of the difference from Python. The specification says so normatively and exhaustively.
In Jupyter and Quarto
Two routes, both supported:
- The ER2 kernel.
er2 kernel install, then pick “ER2” in Jupyter. In Quarto, putjupyter: er2in the front matter. - The extension.
%load_ext er2as the first cell of an ordinarypython3kernel.
Quarto cells stay ```{python}, because that is what Quarto executes and what editor tooling understands. This very site is built that way: its home page runs ER2 as it renders. To write ```{er2} cells instead, use the ER2-ENGINE Quarto extension (engine: er2).
Using the Python ecosystem
Plain import, and it is the real library — ER2 translates only .er2 sources:
import numpy as np
print(np.zeros(2^3).shape) # (8,)
print(np.array([2^3, 3^2]).dtype) # int64Integer subclasses int, so NumPy, pandas, SciPy and Matplotlib give it a real integer dtype. Rational is not a machine number: NumPy and pandas keep it in an object array — exactly, so 1/3 stays 1/3 — while SciPy’s ufuncs reject it. Ask for float(...) when you want speed rather than exactness.