er2 0.7.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- er2-0.7.0/LICENSE +21 -0
- er2-0.7.0/PKG-INFO +292 -0
- er2-0.7.0/README.md +257 -0
- er2-0.7.0/er2/__init__.py +33 -0
- er2-0.7.0/er2/__main__.py +86 -0
- er2-0.7.0/er2/_lazy.py +88 -0
- er2-0.7.0/er2/backends/__init__.py +4 -0
- er2-0.7.0/er2/backends/pari_backend.py +1146 -0
- er2-0.7.0/er2/backends/pari_closures.py +111 -0
- er2-0.7.0/er2/backends/sympy_backend.py +220 -0
- er2-0.7.0/er2/data/pari_functions.csv +1169 -0
- er2-0.7.0/er2/dispatch.py +741 -0
- er2-0.7.0/er2/importer.py +71 -0
- er2-0.7.0/er2/kernel.py +113 -0
- er2-0.7.0/er2/oeis.py +285 -0
- er2-0.7.0/er2/prelude.py +118 -0
- er2-0.7.0/er2/preparser.py +342 -0
- er2-0.7.0/er2/printing.py +276 -0
- er2-0.7.0/er2/runtime/__init__.py +1 -0
- er2-0.7.0/er2/runtime/dirichlet.py +229 -0
- er2-0.7.0/er2/runtime/factorization.py +97 -0
- er2-0.7.0/er2/runtime/finite_field.py +440 -0
- er2-0.7.0/er2/runtime/modular.py +246 -0
- er2-0.7.0/er2/runtime/number_field.py +293 -0
- er2-0.7.0/er2/runtime/numbers.py +214 -0
- er2-0.7.0/er2/runtime/qfb.py +104 -0
- er2-0.7.0/er2/session.py +227 -0
- er2-0.7.0/pyproject.toml +99 -0
- er2-0.7.0/pyproject.toml.orig +107 -0
er2-0.7.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Enrique Pérez Herrero
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
er2-0.7.0/PKG-INFO
ADDED
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: er2
|
|
3
|
+
Version: 0.7.0
|
|
4
|
+
Summary: ER2 — mathematical Python: symbolic syntax, exact arithmetic and PARI/GP number theory on top of Python.
|
|
5
|
+
Keywords: mathematics,computer-algebra,number-theory,symbolic,exact-arithmetic,pari,sympy,preparser
|
|
6
|
+
Author: Enrique Pérez Herrero
|
|
7
|
+
Author-email: Enrique Pérez Herrero <energycode.org@gmail.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: Intended Audience :: Education
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
17
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
18
|
+
Classifier: Topic :: Software Development :: Interpreters
|
|
19
|
+
Classifier: Framework :: Jupyter
|
|
20
|
+
Classifier: Operating System :: OS Independent
|
|
21
|
+
Requires-Dist: sympy>=1.14
|
|
22
|
+
Requires-Dist: cypari2>=2.2
|
|
23
|
+
Requires-Dist: ipykernel>=6.29 ; extra == 'jupyter'
|
|
24
|
+
Requires-Dist: oeis-tools>=0.2.1 ; extra == 'oeis'
|
|
25
|
+
Maintainer: Enrique Pérez Herrero
|
|
26
|
+
Maintainer-email: Enrique Pérez Herrero <energycode.org@gmail.com>
|
|
27
|
+
Requires-Python: >=3.12
|
|
28
|
+
Project-URL: Documentation, https://oeistools.github.io/ER2/
|
|
29
|
+
Project-URL: Repository, https://github.com/oeistools/ER2
|
|
30
|
+
Project-URL: Issues, https://github.com/oeistools/ER2/issues
|
|
31
|
+
Project-URL: Changelog, https://github.com/oeistools/ER2/blob/main/CHANGELOG.md
|
|
32
|
+
Provides-Extra: jupyter
|
|
33
|
+
Provides-Extra: oeis
|
|
34
|
+
Description-Content-Type: text/markdown
|
|
35
|
+
|
|
36
|
+
<h1 align="center">
|
|
37
|
+
<picture>
|
|
38
|
+
<source media="(prefers-color-scheme: dark)" srcset="assets/er2-logo-dark.svg">
|
|
39
|
+
<img alt="ER2 — Mathematical Python" src="assets/er2-logo.svg" width="400">
|
|
40
|
+
</picture>
|
|
41
|
+
</h1>
|
|
42
|
+
|
|
43
|
+
[](https://github.com/oeistools/ER2/actions/workflows/ci.yml)
|
|
44
|
+
[](PLAN.md)
|
|
45
|
+
[](pyproject.toml)
|
|
46
|
+
[](LICENSE)
|
|
47
|
+
[](https://peps.python.org/pep-0008/)
|
|
48
|
+
[](https://github.com/astral-sh/ruff)
|
|
49
|
+
[](https://pari.math.u-bordeaux.fr/)
|
|
50
|
+
[](https://www.sympy.org/)
|
|
51
|
+
[](ARCHITECTURE.md#12-notebooks-jupyter-and-quarto)
|
|
52
|
+
[](ARCHITECTURE.md#12-notebooks-jupyter-and-quarto)
|
|
53
|
+
[](CITATION.cff)
|
|
54
|
+
[](CONTRIBUTING.md)
|
|
55
|
+
|
|
56
|
+
**ER2** is Python with mathematics built in: symbolic syntax, exact arithmetic by default, and
|
|
57
|
+
computational number theory powered by [PARI/GP](https://pari.math.u-bordeaux.fr/).
|
|
58
|
+
|
|
59
|
+
It is named after the Hungarian mathematician **Paul Erdős**. In Spanish, "Erdős" sounds like
|
|
60
|
+
"ER-dos", which is where *ER2* comes from.
|
|
61
|
+
|
|
62
|
+
## Why ER2?
|
|
63
|
+
|
|
64
|
+
- **Write mathematics as mathematics.** `x^2` is a power, `1/3` is exactly one third, and
|
|
65
|
+
`sym x` declares a symbol. There are no imports and no `Rational(1, 3)`.
|
|
66
|
+
- **Two engines, one language.** ER2 sends each computation to the right backend: SymPy for
|
|
67
|
+
symbolic algebra and PARI/GP, one of the fastest number theory systems, for integers, primes
|
|
68
|
+
and modular arithmetic.
|
|
69
|
+
- **It is still Python.** All Python syntax works, every library imports as usual, and ER2 runs in
|
|
70
|
+
Jupyter and Quarto with LaTeX output.
|
|
71
|
+
|
|
72
|
+
## Quick start
|
|
73
|
+
|
|
74
|
+
ER2 is not on PyPI yet. You need Python ≥ 3.12 and [uv](https://docs.astral.sh/uv/); PARI comes
|
|
75
|
+
inside the cypari2 wheel, so there is nothing else to install.
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
git clone https://github.com/oeistools/ER2.git && cd ER2
|
|
79
|
+
uv sync
|
|
80
|
+
uv run er2 examples/hello.er2
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
```text
|
|
84
|
+
Hello from ER2
|
|
85
|
+
1024 1/2
|
|
86
|
+
x^3 + 3*x^2 + 3*x + 1
|
|
87
|
+
True
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Then try the REPL (`uv run er2`), write your own `program.er2`, or open a notebook: see
|
|
91
|
+
[Jupyter and Quarto](#jupyter-and-quarto). `uv run er2 --show-python program.er2` shows the Python
|
|
92
|
+
that ER2 generates.
|
|
93
|
+
|
|
94
|
+
## A taste of ER2
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
import numpy as np # any Python library, imported as usual
|
|
98
|
+
|
|
99
|
+
sym x # declare a symbol
|
|
100
|
+
f = x^2 + 2*x + 1 # ^ means power
|
|
101
|
+
|
|
102
|
+
print(factor(f)) # (x + 1)^2
|
|
103
|
+
print(diff(f, x)) # 2*x + 2
|
|
104
|
+
print(latex(f)) # x^{2} + 2 x + 1 (any expression → LaTeX)
|
|
105
|
+
|
|
106
|
+
print(1/3) # 1/3, exact and not 0.333...
|
|
107
|
+
print(isprime(2^521 - 1)) # True (PARI)
|
|
108
|
+
print(phi(123456789)) # 82260072
|
|
109
|
+
print(factor(2^127 - 1)) # 2^127 - 1 is prime
|
|
110
|
+
print(factor(360)) # 2^3 * 3^2 * 5
|
|
111
|
+
|
|
112
|
+
for k in range(1, 20): # ordinary Python
|
|
113
|
+
if isprime(k):
|
|
114
|
+
print(k)
|
|
115
|
+
|
|
116
|
+
np.zeros(2^3) # ER2 numbers pass straight into libraries
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Examples
|
|
120
|
+
|
|
121
|
+
Each program runs with `uv run er2 examples/<name>.er2`, and its expected output is checked by the
|
|
122
|
+
test suite.
|
|
123
|
+
|
|
124
|
+
| Example | What it shows |
|
|
125
|
+
|---------|---------------|
|
|
126
|
+
| [hello.er2](https://github.com/oeistools/ER2/blob/main/examples/hello.er2) | The smallest tour: power, exact division, algebra, a primality test |
|
|
127
|
+
| [syntax.er2](https://github.com/oeistools/ER2/blob/main/examples/syntax.er2) | Every ER2 syntax difference from Python: `^`, `^^`, exact literals, `sym`, `5r` |
|
|
128
|
+
| [factorization.er2](https://github.com/oeistools/ER2/blob/main/examples/factorization.er2) | One `factor` for integers, rationals and polynomials; partial factorizations |
|
|
129
|
+
| [mvp.er2](https://github.com/oeistools/ER2/blob/main/examples/mvp.er2) | The MVP program: CAS and number theory together |
|
|
130
|
+
| [demo.qmd](https://github.com/oeistools/ER2/blob/main/examples/demo.qmd) | A Quarto tour with rendered math, including the OEIS (`make render`) |
|
|
131
|
+
| [mvp.ipynb](https://github.com/oeistools/ER2/blob/main/examples/mvp.ipynb) | The MVP as a Jupyter notebook |
|
|
132
|
+
|
|
133
|
+
## Documentation
|
|
134
|
+
|
|
135
|
+
The documentation site is at **<https://oeistools.github.io/ER2/>**, built by Quarto and executed by ER2 itself.
|
|
136
|
+
|
|
137
|
+
| If you want to… | Read |
|
|
138
|
+
|-----------------|------|
|
|
139
|
+
| **Use ER2** | This README, the [examples](#examples), and [docs/PARI_FUNCTIONS.md](https://github.com/oeistools/ER2/blob/main/docs/PARI_FUNCTIONS.md) (every PARI function and its ER2 name) |
|
|
140
|
+
| **Know exactly what the language is** | [docs/LANGUAGE.md](https://github.com/oeistools/ER2/blob/main/docs/LANGUAGE.md): the normative specification — the differences from Python, precedence, the number model, and what is stable before 1.0 |
|
|
141
|
+
| **Understand the design** | [ARCHITECTURE.md](https://github.com/oeistools/ER2/blob/main/ARCHITECTURE.md): the compatibility contract, the components, and the design decisions D1–D17 |
|
|
142
|
+
| **Follow the project** | [PLAN.md](https://github.com/oeistools/ER2/blob/main/PLAN.md) (milestones and acceptance criteria), [CHANGELOG.md](https://github.com/oeistools/ER2/blob/main/CHANGELOG.md) (releases and versioning policy), [docs/BENCHMARKS.md](https://github.com/oeistools/ER2/blob/main/docs/BENCHMARKS.md) |
|
|
143
|
+
| **Contribute** | [CONTRIBUTING.md](https://github.com/oeistools/ER2/blob/main/CONTRIBUTING.md): setup, tests, rules for each part of the code, good first contributions |
|
|
144
|
+
|
|
145
|
+
The rules for AI-assisted development are in [CLAUDE.md](https://github.com/oeistools/ER2/blob/main/CLAUDE.md).
|
|
146
|
+
|
|
147
|
+
## Algebra (0.5)
|
|
148
|
+
|
|
149
|
+
```python
|
|
150
|
+
sym x, y
|
|
151
|
+
|
|
152
|
+
det(Matrix([[2, 1], [1, 3]])) # 5, computed by PARI
|
|
153
|
+
kernel(Matrix([[1, 2], [2, 4]])) # [Matrix([[1], [-1/2]])]
|
|
154
|
+
smith_form(Matrix([[2, 4], [6, 8]])) # Matrix([[2, 0], [0, 4]]), as SymPy's
|
|
155
|
+
|
|
156
|
+
factor(x^8 - x, modulus=2) # x*(x + 1)*(x^3 + x + 1)*(x^3 + x^2 + 1)
|
|
157
|
+
isirreducible(x^2 + x + 1, modulus=2) # True
|
|
158
|
+
|
|
159
|
+
a = GF(9).gen() # the finite field with 9 elements
|
|
160
|
+
a^4, a.order(), minpoly(a) # 2, 8, x^2 + x + 2
|
|
161
|
+
|
|
162
|
+
resultant(x^2 + 1, x^3 - 2, x) # 5
|
|
163
|
+
discriminant(x^3 + x + 1) # -31
|
|
164
|
+
|
|
165
|
+
G = groebner([x^2 + y^2 - 1, x - y], x, y)
|
|
166
|
+
list(G) # [x - y, 2*y^2 - 1]
|
|
167
|
+
reduce(x^2 + y^2, G) # 1, the normal form
|
|
168
|
+
|
|
169
|
+
K = NumberField(x^2 + 5) # Q(sqrt(-5))
|
|
170
|
+
K.discriminant, K.class_number() # -20, 2
|
|
171
|
+
K.factor(2) # [((2, x + 1), 2)]: 2 ramifies
|
|
172
|
+
K.factor(3) # 3 splits into two primes
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
`resultant` follows the standard sign convention, which is PARI's, so
|
|
176
|
+
`resultant(f, g)` and `resultant(g, f)` differ in sign when both degrees are odd
|
|
177
|
+
(ARCHITECTURE.md §6, D16). A class number is only as good as the GRH unless you ask for
|
|
178
|
+
`certify=True`.
|
|
179
|
+
|
|
180
|
+
## Python compatibility
|
|
181
|
+
|
|
182
|
+
ER2 is a superset of Python and follows the same model as SageMath:
|
|
183
|
+
|
|
184
|
+
- **All Python syntax works**: classes, decorators, generators, `async`, `match`, f-strings, type hints, and the rest.
|
|
185
|
+
- **The whole ecosystem is available** through normal `import`: NumPy, SciPy, pandas, Matplotlib, and so on.
|
|
186
|
+
- **Only `.er2` sources are translated.** Your `.py` modules and installed libraries run as ordinary Python and are never modified.
|
|
187
|
+
- **Inside `.er2` files there are only a few deliberate differences**:
|
|
188
|
+
|
|
189
|
+
| Construct | Python | ER2 |
|
|
190
|
+
|-------------|---------------|-------------------------------|
|
|
191
|
+
| `a ^ b` | XOR | power |
|
|
192
|
+
| `a ^^ b` | syntax error | XOR |
|
|
193
|
+
| `1/3` | `0.333…` | exact rational `1/3` |
|
|
194
|
+
| `sym x, y` | syntax error | declares symbols `x`, `y` |
|
|
195
|
+
| `5r` | syntax error | the plain Python `int` 5 |
|
|
196
|
+
|
|
197
|
+
## Jupyter and Quarto
|
|
198
|
+
|
|
199
|
+
ER2 runs in notebooks and in Quarto documents:
|
|
200
|
+
|
|
201
|
+
- **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`.
|
|
202
|
+
- **Any Python kernel**: add `%load_ext er2` in the first cell.
|
|
203
|
+
- **LaTeX everywhere**: expressions render as math. `show(f)` displays one, and
|
|
204
|
+
`` `{python} latex(f)` `` puts inline math in Quarto text.
|
|
205
|
+
|
|
206
|
+
````markdown
|
|
207
|
+
---
|
|
208
|
+
title: "Mersenne primes"
|
|
209
|
+
jupyter: er2
|
|
210
|
+
---
|
|
211
|
+
|
|
212
|
+
```{python}
|
|
213
|
+
[p for p in range(2, 130) if isprime(2^p - 1)]
|
|
214
|
+
```
|
|
215
|
+
````
|
|
216
|
+
|
|
217
|
+
## How it works
|
|
218
|
+
|
|
219
|
+
ER2 does not introduce a new interpreter. It has three parts:
|
|
220
|
+
|
|
221
|
+
```text
|
|
222
|
+
.er2 source ──► preparser ──► plain Python ──► CPython
|
|
223
|
+
│
|
|
224
|
+
ER2 runtime (types, printing, dispatch)
|
|
225
|
+
│ │
|
|
226
|
+
SymPy cypari2 → PARI
|
|
227
|
+
(symbolic math) (number theory)
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
1. **Preparser.** A token-level, source-to-source translation from `.er2` to Python.
|
|
231
|
+
2. **Runtime.** The `er2` package provides the mathematical types and the functions (`factor`, `diff`, `phi`, …).
|
|
232
|
+
3. **Backends.** [SymPy](https://www.sympy.org/) handles symbolic computation, and PARI is accessed through [cypari2](https://github.com/sagemath/cypari2). ER2 chooses the backend automatically, so `factor` works on both polynomials and integers.
|
|
233
|
+
|
|
234
|
+
## Status and roadmap
|
|
235
|
+
|
|
236
|
+
**Version 0.7.0, early development.** Milestones M1–M7 are done (M3 was the MVP). Algebra
|
|
237
|
+
(matrices, finite fields, `F_p`, resultants, Gröbner bases, `NumberField`), series (power,
|
|
238
|
+
Dirichlet, Euler products) and the language specification are all in.
|
|
239
|
+
[docs/LANGUAGE.md](https://github.com/oeistools/ER2/blob/main/docs/LANGUAGE.md) now defines the language normatively, and
|
|
240
|
+
[CHANGELOG.md](https://github.com/oeistools/ER2/blob/main/CHANGELOG.md) says what is stable, what is still experimental, and what has
|
|
241
|
+
changed. What remains before 1.0 is the documentation site and the stability policy.
|
|
242
|
+
|
|
243
|
+
| Version | Focus | Status |
|
|
244
|
+
|---------|-------|--------|
|
|
245
|
+
| 0.1 | Preparser: `sym`, `^`, `_x`, exact integers and rationals; Jupyter kernel, `%load_ext er2`, Quarto | ✅ |
|
|
246
|
+
| 0.2 | CAS on SymPy: `expand`, `factor`, `simplify`, `diff`, `integrate`, `limit`, `solve`, `series` | ✅ |
|
|
247
|
+
| 0.3 | Number theory on PARI: primality, factorization, `phi`, `sigma`, `mu`, … (the MVP) | ✅ |
|
|
248
|
+
| 0.4 | Automatic backend selection, PARI types (`Mod`, `Qfb`, series), benchmarks, the OEIS | ✅ |
|
|
249
|
+
| 0.5 | Algebra: matrices, finite fields, resultants, Gröbner bases, number fields | ✅ |
|
|
250
|
+
| 0.6 | Series: power, Dirichlet, Euler products | ✅ |
|
|
251
|
+
| 0.7 | The language specification, `docs/LANGUAGE.md`; first PyPI release | ✅ |
|
|
252
|
+
| 1.0 | Frozen syntax and public API: documentation site, stability policy | in progress |
|
|
253
|
+
|
|
254
|
+
Beyond the table, every PARI function is available as `pari.<name>`, and PARI's sums and integrals
|
|
255
|
+
take Python functions (`pari.sum(lambda n: 1/n^2, 1, 10)`). Deeper CPython integration comes only
|
|
256
|
+
after the syntax is stable.
|
|
257
|
+
|
|
258
|
+
## Development
|
|
259
|
+
|
|
260
|
+
```bash
|
|
261
|
+
make install # uv sync + the ER2 kernel inside .venv
|
|
262
|
+
make test-fast # the tests without Jupyter and Quarto
|
|
263
|
+
make check # ruff + all tests, as in CI
|
|
264
|
+
make install-global # the `er2` command for your user, editable (uv tool)
|
|
265
|
+
make preview FILE=my_doc.qmd # live Quarto preview with the ER2 kernel
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
`make` lists every target. `make install-global` puts `er2` in `~/.local/bin`. It is an editable
|
|
269
|
+
install, so it follows changes to the code, and it registers the ER2 kernel for your user.
|
|
270
|
+
|
|
271
|
+
We keep dependencies deliberately minimal: `sympy` and `cypari2` at runtime, plus `ipykernel` and
|
|
272
|
+
`oeis-tools` as the optional `er2[jupyter]` and `er2[oeis]` extras. Development uses `pytest` and
|
|
273
|
+
`ruff`. The code follows [PEP 8](https://peps.python.org/pep-0008/).
|
|
274
|
+
|
|
275
|
+
## Contributing
|
|
276
|
+
|
|
277
|
+
ER2 is maintained by **Enrique Pérez Herrero** ([energycode.org@gmail.com](mailto:energycode.org@gmail.com)).
|
|
278
|
+
|
|
279
|
+
Contributions are welcome, from a bug report with a three-line example to a new feature.
|
|
280
|
+
[CONTRIBUTING.md](https://github.com/oeistools/ER2/blob/main/CONTRIBUTING.md) explains how to set up, what a pull request needs, and where to
|
|
281
|
+
start if you are new to the project.
|
|
282
|
+
|
|
283
|
+
## Citation
|
|
284
|
+
|
|
285
|
+
If you use ER2 in your work, please cite it. The metadata is in [CITATION.cff](https://github.com/oeistools/ER2/blob/main/CITATION.cff), and
|
|
286
|
+
GitHub shows it through **"Cite this repository"**. Please also cite
|
|
287
|
+
[PARI/GP](https://pari.math.u-bordeaux.fr/) and [SymPy](https://doi.org/10.7717/peerj-cs.103),
|
|
288
|
+
which ER2 builds on.
|
|
289
|
+
|
|
290
|
+
## License
|
|
291
|
+
|
|
292
|
+
[MIT](https://github.com/oeistools/ER2/blob/main/LICENSE)
|
er2-0.7.0/README.md
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
<h1 align="center">
|
|
2
|
+
<picture>
|
|
3
|
+
<source media="(prefers-color-scheme: dark)" srcset="assets/er2-logo-dark.svg">
|
|
4
|
+
<img alt="ER2 — Mathematical Python" src="assets/er2-logo.svg" width="400">
|
|
5
|
+
</picture>
|
|
6
|
+
</h1>
|
|
7
|
+
|
|
8
|
+
[](https://github.com/oeistools/ER2/actions/workflows/ci.yml)
|
|
9
|
+
[](PLAN.md)
|
|
10
|
+
[](pyproject.toml)
|
|
11
|
+
[](LICENSE)
|
|
12
|
+
[](https://peps.python.org/pep-0008/)
|
|
13
|
+
[](https://github.com/astral-sh/ruff)
|
|
14
|
+
[](https://pari.math.u-bordeaux.fr/)
|
|
15
|
+
[](https://www.sympy.org/)
|
|
16
|
+
[](ARCHITECTURE.md#12-notebooks-jupyter-and-quarto)
|
|
17
|
+
[](ARCHITECTURE.md#12-notebooks-jupyter-and-quarto)
|
|
18
|
+
[](CITATION.cff)
|
|
19
|
+
[](CONTRIBUTING.md)
|
|
20
|
+
|
|
21
|
+
**ER2** is Python with mathematics built in: symbolic syntax, exact arithmetic by default, and
|
|
22
|
+
computational number theory powered by [PARI/GP](https://pari.math.u-bordeaux.fr/).
|
|
23
|
+
|
|
24
|
+
It is named after the Hungarian mathematician **Paul Erdős**. In Spanish, "Erdős" sounds like
|
|
25
|
+
"ER-dos", which is where *ER2* comes from.
|
|
26
|
+
|
|
27
|
+
## Why ER2?
|
|
28
|
+
|
|
29
|
+
- **Write mathematics as mathematics.** `x^2` is a power, `1/3` is exactly one third, and
|
|
30
|
+
`sym x` declares a symbol. There are no imports and no `Rational(1, 3)`.
|
|
31
|
+
- **Two engines, one language.** ER2 sends each computation to the right backend: SymPy for
|
|
32
|
+
symbolic algebra and PARI/GP, one of the fastest number theory systems, for integers, primes
|
|
33
|
+
and modular arithmetic.
|
|
34
|
+
- **It is still Python.** All Python syntax works, every library imports as usual, and ER2 runs in
|
|
35
|
+
Jupyter and Quarto with LaTeX output.
|
|
36
|
+
|
|
37
|
+
## Quick start
|
|
38
|
+
|
|
39
|
+
ER2 is not on PyPI yet. You need Python ≥ 3.12 and [uv](https://docs.astral.sh/uv/); PARI comes
|
|
40
|
+
inside the cypari2 wheel, so there is nothing else to install.
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
git clone https://github.com/oeistools/ER2.git && cd ER2
|
|
44
|
+
uv sync
|
|
45
|
+
uv run er2 examples/hello.er2
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
```text
|
|
49
|
+
Hello from ER2
|
|
50
|
+
1024 1/2
|
|
51
|
+
x^3 + 3*x^2 + 3*x + 1
|
|
52
|
+
True
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Then try the REPL (`uv run er2`), write your own `program.er2`, or open a notebook: see
|
|
56
|
+
[Jupyter and Quarto](#jupyter-and-quarto). `uv run er2 --show-python program.er2` shows the Python
|
|
57
|
+
that ER2 generates.
|
|
58
|
+
|
|
59
|
+
## A taste of ER2
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
import numpy as np # any Python library, imported as usual
|
|
63
|
+
|
|
64
|
+
sym x # declare a symbol
|
|
65
|
+
f = x^2 + 2*x + 1 # ^ means power
|
|
66
|
+
|
|
67
|
+
print(factor(f)) # (x + 1)^2
|
|
68
|
+
print(diff(f, x)) # 2*x + 2
|
|
69
|
+
print(latex(f)) # x^{2} + 2 x + 1 (any expression → LaTeX)
|
|
70
|
+
|
|
71
|
+
print(1/3) # 1/3, exact and not 0.333...
|
|
72
|
+
print(isprime(2^521 - 1)) # True (PARI)
|
|
73
|
+
print(phi(123456789)) # 82260072
|
|
74
|
+
print(factor(2^127 - 1)) # 2^127 - 1 is prime
|
|
75
|
+
print(factor(360)) # 2^3 * 3^2 * 5
|
|
76
|
+
|
|
77
|
+
for k in range(1, 20): # ordinary Python
|
|
78
|
+
if isprime(k):
|
|
79
|
+
print(k)
|
|
80
|
+
|
|
81
|
+
np.zeros(2^3) # ER2 numbers pass straight into libraries
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Examples
|
|
85
|
+
|
|
86
|
+
Each program runs with `uv run er2 examples/<name>.er2`, and its expected output is checked by the
|
|
87
|
+
test suite.
|
|
88
|
+
|
|
89
|
+
| Example | What it shows |
|
|
90
|
+
|---------|---------------|
|
|
91
|
+
| [hello.er2](https://github.com/oeistools/ER2/blob/main/examples/hello.er2) | The smallest tour: power, exact division, algebra, a primality test |
|
|
92
|
+
| [syntax.er2](https://github.com/oeistools/ER2/blob/main/examples/syntax.er2) | Every ER2 syntax difference from Python: `^`, `^^`, exact literals, `sym`, `5r` |
|
|
93
|
+
| [factorization.er2](https://github.com/oeistools/ER2/blob/main/examples/factorization.er2) | One `factor` for integers, rationals and polynomials; partial factorizations |
|
|
94
|
+
| [mvp.er2](https://github.com/oeistools/ER2/blob/main/examples/mvp.er2) | The MVP program: CAS and number theory together |
|
|
95
|
+
| [demo.qmd](https://github.com/oeistools/ER2/blob/main/examples/demo.qmd) | A Quarto tour with rendered math, including the OEIS (`make render`) |
|
|
96
|
+
| [mvp.ipynb](https://github.com/oeistools/ER2/blob/main/examples/mvp.ipynb) | The MVP as a Jupyter notebook |
|
|
97
|
+
|
|
98
|
+
## Documentation
|
|
99
|
+
|
|
100
|
+
The documentation site is at **<https://oeistools.github.io/ER2/>**, built by Quarto and executed by ER2 itself.
|
|
101
|
+
|
|
102
|
+
| If you want to… | Read |
|
|
103
|
+
|-----------------|------|
|
|
104
|
+
| **Use ER2** | This README, the [examples](#examples), and [docs/PARI_FUNCTIONS.md](https://github.com/oeistools/ER2/blob/main/docs/PARI_FUNCTIONS.md) (every PARI function and its ER2 name) |
|
|
105
|
+
| **Know exactly what the language is** | [docs/LANGUAGE.md](https://github.com/oeistools/ER2/blob/main/docs/LANGUAGE.md): the normative specification — the differences from Python, precedence, the number model, and what is stable before 1.0 |
|
|
106
|
+
| **Understand the design** | [ARCHITECTURE.md](https://github.com/oeistools/ER2/blob/main/ARCHITECTURE.md): the compatibility contract, the components, and the design decisions D1–D17 |
|
|
107
|
+
| **Follow the project** | [PLAN.md](https://github.com/oeistools/ER2/blob/main/PLAN.md) (milestones and acceptance criteria), [CHANGELOG.md](https://github.com/oeistools/ER2/blob/main/CHANGELOG.md) (releases and versioning policy), [docs/BENCHMARKS.md](https://github.com/oeistools/ER2/blob/main/docs/BENCHMARKS.md) |
|
|
108
|
+
| **Contribute** | [CONTRIBUTING.md](https://github.com/oeistools/ER2/blob/main/CONTRIBUTING.md): setup, tests, rules for each part of the code, good first contributions |
|
|
109
|
+
|
|
110
|
+
The rules for AI-assisted development are in [CLAUDE.md](https://github.com/oeistools/ER2/blob/main/CLAUDE.md).
|
|
111
|
+
|
|
112
|
+
## Algebra (0.5)
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
sym x, y
|
|
116
|
+
|
|
117
|
+
det(Matrix([[2, 1], [1, 3]])) # 5, computed by PARI
|
|
118
|
+
kernel(Matrix([[1, 2], [2, 4]])) # [Matrix([[1], [-1/2]])]
|
|
119
|
+
smith_form(Matrix([[2, 4], [6, 8]])) # Matrix([[2, 0], [0, 4]]), as SymPy's
|
|
120
|
+
|
|
121
|
+
factor(x^8 - x, modulus=2) # x*(x + 1)*(x^3 + x + 1)*(x^3 + x^2 + 1)
|
|
122
|
+
isirreducible(x^2 + x + 1, modulus=2) # True
|
|
123
|
+
|
|
124
|
+
a = GF(9).gen() # the finite field with 9 elements
|
|
125
|
+
a^4, a.order(), minpoly(a) # 2, 8, x^2 + x + 2
|
|
126
|
+
|
|
127
|
+
resultant(x^2 + 1, x^3 - 2, x) # 5
|
|
128
|
+
discriminant(x^3 + x + 1) # -31
|
|
129
|
+
|
|
130
|
+
G = groebner([x^2 + y^2 - 1, x - y], x, y)
|
|
131
|
+
list(G) # [x - y, 2*y^2 - 1]
|
|
132
|
+
reduce(x^2 + y^2, G) # 1, the normal form
|
|
133
|
+
|
|
134
|
+
K = NumberField(x^2 + 5) # Q(sqrt(-5))
|
|
135
|
+
K.discriminant, K.class_number() # -20, 2
|
|
136
|
+
K.factor(2) # [((2, x + 1), 2)]: 2 ramifies
|
|
137
|
+
K.factor(3) # 3 splits into two primes
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
`resultant` follows the standard sign convention, which is PARI's, so
|
|
141
|
+
`resultant(f, g)` and `resultant(g, f)` differ in sign when both degrees are odd
|
|
142
|
+
(ARCHITECTURE.md §6, D16). A class number is only as good as the GRH unless you ask for
|
|
143
|
+
`certify=True`.
|
|
144
|
+
|
|
145
|
+
## Python compatibility
|
|
146
|
+
|
|
147
|
+
ER2 is a superset of Python and follows the same model as SageMath:
|
|
148
|
+
|
|
149
|
+
- **All Python syntax works**: classes, decorators, generators, `async`, `match`, f-strings, type hints, and the rest.
|
|
150
|
+
- **The whole ecosystem is available** through normal `import`: NumPy, SciPy, pandas, Matplotlib, and so on.
|
|
151
|
+
- **Only `.er2` sources are translated.** Your `.py` modules and installed libraries run as ordinary Python and are never modified.
|
|
152
|
+
- **Inside `.er2` files there are only a few deliberate differences**:
|
|
153
|
+
|
|
154
|
+
| Construct | Python | ER2 |
|
|
155
|
+
|-------------|---------------|-------------------------------|
|
|
156
|
+
| `a ^ b` | XOR | power |
|
|
157
|
+
| `a ^^ b` | syntax error | XOR |
|
|
158
|
+
| `1/3` | `0.333…` | exact rational `1/3` |
|
|
159
|
+
| `sym x, y` | syntax error | declares symbols `x`, `y` |
|
|
160
|
+
| `5r` | syntax error | the plain Python `int` 5 |
|
|
161
|
+
|
|
162
|
+
## Jupyter and Quarto
|
|
163
|
+
|
|
164
|
+
ER2 runs in notebooks and in Quarto documents:
|
|
165
|
+
|
|
166
|
+
- **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`.
|
|
167
|
+
- **Any Python kernel**: add `%load_ext er2` in the first cell.
|
|
168
|
+
- **LaTeX everywhere**: expressions render as math. `show(f)` displays one, and
|
|
169
|
+
`` `{python} latex(f)` `` puts inline math in Quarto text.
|
|
170
|
+
|
|
171
|
+
````markdown
|
|
172
|
+
---
|
|
173
|
+
title: "Mersenne primes"
|
|
174
|
+
jupyter: er2
|
|
175
|
+
---
|
|
176
|
+
|
|
177
|
+
```{python}
|
|
178
|
+
[p for p in range(2, 130) if isprime(2^p - 1)]
|
|
179
|
+
```
|
|
180
|
+
````
|
|
181
|
+
|
|
182
|
+
## How it works
|
|
183
|
+
|
|
184
|
+
ER2 does not introduce a new interpreter. It has three parts:
|
|
185
|
+
|
|
186
|
+
```text
|
|
187
|
+
.er2 source ──► preparser ──► plain Python ──► CPython
|
|
188
|
+
│
|
|
189
|
+
ER2 runtime (types, printing, dispatch)
|
|
190
|
+
│ │
|
|
191
|
+
SymPy cypari2 → PARI
|
|
192
|
+
(symbolic math) (number theory)
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
1. **Preparser.** A token-level, source-to-source translation from `.er2` to Python.
|
|
196
|
+
2. **Runtime.** The `er2` package provides the mathematical types and the functions (`factor`, `diff`, `phi`, …).
|
|
197
|
+
3. **Backends.** [SymPy](https://www.sympy.org/) handles symbolic computation, and PARI is accessed through [cypari2](https://github.com/sagemath/cypari2). ER2 chooses the backend automatically, so `factor` works on both polynomials and integers.
|
|
198
|
+
|
|
199
|
+
## Status and roadmap
|
|
200
|
+
|
|
201
|
+
**Version 0.7.0, early development.** Milestones M1–M7 are done (M3 was the MVP). Algebra
|
|
202
|
+
(matrices, finite fields, `F_p`, resultants, Gröbner bases, `NumberField`), series (power,
|
|
203
|
+
Dirichlet, Euler products) and the language specification are all in.
|
|
204
|
+
[docs/LANGUAGE.md](https://github.com/oeistools/ER2/blob/main/docs/LANGUAGE.md) now defines the language normatively, and
|
|
205
|
+
[CHANGELOG.md](https://github.com/oeistools/ER2/blob/main/CHANGELOG.md) says what is stable, what is still experimental, and what has
|
|
206
|
+
changed. What remains before 1.0 is the documentation site and the stability policy.
|
|
207
|
+
|
|
208
|
+
| Version | Focus | Status |
|
|
209
|
+
|---------|-------|--------|
|
|
210
|
+
| 0.1 | Preparser: `sym`, `^`, `_x`, exact integers and rationals; Jupyter kernel, `%load_ext er2`, Quarto | ✅ |
|
|
211
|
+
| 0.2 | CAS on SymPy: `expand`, `factor`, `simplify`, `diff`, `integrate`, `limit`, `solve`, `series` | ✅ |
|
|
212
|
+
| 0.3 | Number theory on PARI: primality, factorization, `phi`, `sigma`, `mu`, … (the MVP) | ✅ |
|
|
213
|
+
| 0.4 | Automatic backend selection, PARI types (`Mod`, `Qfb`, series), benchmarks, the OEIS | ✅ |
|
|
214
|
+
| 0.5 | Algebra: matrices, finite fields, resultants, Gröbner bases, number fields | ✅ |
|
|
215
|
+
| 0.6 | Series: power, Dirichlet, Euler products | ✅ |
|
|
216
|
+
| 0.7 | The language specification, `docs/LANGUAGE.md`; first PyPI release | ✅ |
|
|
217
|
+
| 1.0 | Frozen syntax and public API: documentation site, stability policy | in progress |
|
|
218
|
+
|
|
219
|
+
Beyond the table, every PARI function is available as `pari.<name>`, and PARI's sums and integrals
|
|
220
|
+
take Python functions (`pari.sum(lambda n: 1/n^2, 1, 10)`). Deeper CPython integration comes only
|
|
221
|
+
after the syntax is stable.
|
|
222
|
+
|
|
223
|
+
## Development
|
|
224
|
+
|
|
225
|
+
```bash
|
|
226
|
+
make install # uv sync + the ER2 kernel inside .venv
|
|
227
|
+
make test-fast # the tests without Jupyter and Quarto
|
|
228
|
+
make check # ruff + all tests, as in CI
|
|
229
|
+
make install-global # the `er2` command for your user, editable (uv tool)
|
|
230
|
+
make preview FILE=my_doc.qmd # live Quarto preview with the ER2 kernel
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
`make` lists every target. `make install-global` puts `er2` in `~/.local/bin`. It is an editable
|
|
234
|
+
install, so it follows changes to the code, and it registers the ER2 kernel for your user.
|
|
235
|
+
|
|
236
|
+
We keep dependencies deliberately minimal: `sympy` and `cypari2` at runtime, plus `ipykernel` and
|
|
237
|
+
`oeis-tools` as the optional `er2[jupyter]` and `er2[oeis]` extras. Development uses `pytest` and
|
|
238
|
+
`ruff`. The code follows [PEP 8](https://peps.python.org/pep-0008/).
|
|
239
|
+
|
|
240
|
+
## Contributing
|
|
241
|
+
|
|
242
|
+
ER2 is maintained by **Enrique Pérez Herrero** ([energycode.org@gmail.com](mailto:energycode.org@gmail.com)).
|
|
243
|
+
|
|
244
|
+
Contributions are welcome, from a bug report with a three-line example to a new feature.
|
|
245
|
+
[CONTRIBUTING.md](https://github.com/oeistools/ER2/blob/main/CONTRIBUTING.md) explains how to set up, what a pull request needs, and where to
|
|
246
|
+
start if you are new to the project.
|
|
247
|
+
|
|
248
|
+
## Citation
|
|
249
|
+
|
|
250
|
+
If you use ER2 in your work, please cite it. The metadata is in [CITATION.cff](https://github.com/oeistools/ER2/blob/main/CITATION.cff), and
|
|
251
|
+
GitHub shows it through **"Cite this repository"**. Please also cite
|
|
252
|
+
[PARI/GP](https://pari.math.u-bordeaux.fr/) and [SymPy](https://doi.org/10.7717/peerj-cs.103),
|
|
253
|
+
which ER2 builds on.
|
|
254
|
+
|
|
255
|
+
## License
|
|
256
|
+
|
|
257
|
+
[MIT](https://github.com/oeistools/ER2/blob/main/LICENSE)
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"""ER2 — mathematical Python.
|
|
2
|
+
|
|
3
|
+
Importing ``er2`` from Python has no side effects; ER2 printing and
|
|
4
|
+
``.er2`` imports are enabled only by the ER2 entry points (the ``er2``
|
|
5
|
+
command, the ER2 kernel, ``%load_ext er2``) or explicitly with
|
|
6
|
+
``er2.session.start()``.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
10
|
+
|
|
11
|
+
from er2.preparser import preparse
|
|
12
|
+
from er2.printing import latex, show
|
|
13
|
+
from er2.runtime.numbers import Integer, Rational
|
|
14
|
+
|
|
15
|
+
try:
|
|
16
|
+
__version__ = version("er2") # single source: pyproject.toml
|
|
17
|
+
except PackageNotFoundError: # running from a source tree, not installed
|
|
18
|
+
__version__ = "0+unknown"
|
|
19
|
+
__all__ = ["Integer", "Rational", "latex", "preparse", "show"]
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def load_ipython_extension(shell):
|
|
23
|
+
"""Enable ER2 in IPython: ``%load_ext er2``."""
|
|
24
|
+
from er2.session import load_ipython_extension
|
|
25
|
+
|
|
26
|
+
load_ipython_extension(shell)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def unload_ipython_extension(shell):
|
|
30
|
+
"""Disable ER2 in IPython: ``%unload_ext er2``."""
|
|
31
|
+
from er2.session import unload_ipython_extension
|
|
32
|
+
|
|
33
|
+
unload_ipython_extension(shell)
|