ceexp 0.3.2__tar.gz → 0.3.4__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.
- {ceexp-0.3.2/src/ceexp.egg-info → ceexp-0.3.4}/PKG-INFO +30 -6
- {ceexp-0.3.2 → ceexp-0.3.4}/README.md +27 -5
- {ceexp-0.3.2 → ceexp-0.3.4}/pyproject.toml +3 -1
- {ceexp-0.3.2 → ceexp-0.3.4}/src/ceexp/__init__.py +7 -3
- {ceexp-0.3.2 → ceexp-0.3.4}/src/ceexp/benchmark.py +50 -30
- ceexp-0.3.4/src/ceexp/cli.py +121 -0
- {ceexp-0.3.2 → ceexp-0.3.4}/src/ceexp/expansions.py +1 -2
- {ceexp-0.3.2 → ceexp-0.3.4}/src/ceexp/series.py +10 -6
- {ceexp-0.3.2 → ceexp-0.3.4/src/ceexp.egg-info}/PKG-INFO +30 -6
- {ceexp-0.3.2 → ceexp-0.3.4}/src/ceexp.egg-info/SOURCES.txt +2 -0
- {ceexp-0.3.2 → ceexp-0.3.4}/src/ceexp.egg-info/entry_points.txt +1 -0
- {ceexp-0.3.2 → ceexp-0.3.4}/src/ceexp.egg-info/requires.txt +3 -0
- ceexp-0.3.4/tests/test_cli.py +59 -0
- {ceexp-0.3.2 → ceexp-0.3.4}/tests/test_expansions.py +6 -4
- {ceexp-0.3.2 → ceexp-0.3.4}/tests/test_properties.py +37 -7
- {ceexp-0.3.2 → ceexp-0.3.4}/LICENSE +0 -0
- {ceexp-0.3.2 → ceexp-0.3.4}/LICENSE-docs.md +0 -0
- {ceexp-0.3.2 → ceexp-0.3.4}/setup.cfg +0 -0
- {ceexp-0.3.2 → ceexp-0.3.4}/src/ceexp/constant.py +0 -0
- {ceexp-0.3.2 → ceexp-0.3.4}/src/ceexp/residue.py +0 -0
- {ceexp-0.3.2 → ceexp-0.3.4}/src/ceexp/verify.py +0 -0
- {ceexp-0.3.2 → ceexp-0.3.4}/src/ceexp.egg-info/dependency_links.txt +0 -0
- {ceexp-0.3.2 → ceexp-0.3.4}/src/ceexp.egg-info/top_level.txt +0 -0
- {ceexp-0.3.2 → ceexp-0.3.4}/tests/test_benchmark.py +0 -0
- {ceexp-0.3.2 → ceexp-0.3.4}/tests/test_champernowne_comparison.py +0 -0
- {ceexp-0.3.2 → ceexp-0.3.4}/tests/test_reference.py +0 -0
- {ceexp-0.3.2 → ceexp-0.3.4}/tests/test_theorems.py +0 -0
- {ceexp-0.3.2 → ceexp-0.3.4}/tests/test_variants_and_arithmetic.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: ceexp
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.4
|
|
4
4
|
Summary: Interval-certified Engel, Pierce, Luroth, and continued-fraction expansions and precision benchmarks
|
|
5
5
|
Author-email: Nicolás León Parra <leonparranicolas@gmail.com>
|
|
6
6
|
License: MIT
|
|
@@ -17,17 +17,41 @@ Requires-Dist: numpy>=1.26; extra == "experiments"
|
|
|
17
17
|
Requires-Dist: scipy>=1.11; extra == "experiments"
|
|
18
18
|
Provides-Extra: benchmark
|
|
19
19
|
Requires-Dist: mpmath>=1.2; extra == "benchmark"
|
|
20
|
+
Provides-Extra: fast
|
|
21
|
+
Requires-Dist: gmpy2>=2.1; extra == "fast"
|
|
20
22
|
Dynamic: license-file
|
|
21
23
|
|
|
22
|
-
# Expansions and residues of the Copeland–Erdős constant
|
|
24
|
+
# Expansions and residues of the Copeland–Erdős constant (`ceexp`)
|
|
23
25
|
|
|
26
|
+
[](https://github.com/Nicolas11Leon/copeland-erdos-expansions/actions/workflows/ci.yml)
|
|
27
|
+
[](https://pypi.org/project/ceexp/)
|
|
28
|
+
[](https://pypi.org/project/ceexp/)
|
|
29
|
+
[](LICENSE)
|
|
24
30
|
[](https://doi.org/10.5281/zenodo.24693622)
|
|
25
31
|
|
|
26
|
-
Certified computations, proofs and reproducible experiments on
|
|
27
|
-
**
|
|
28
|
-
|
|
32
|
+
Certified computations, proofs and reproducible experiments on classical unit-fraction expansions
|
|
33
|
+
(**Engel, Pierce, Lüroth, greedy Egyptian, and continued fractions**) of the **Copeland–Erdős
|
|
34
|
+
constant** `C = 0.235711131719232931374143…` (OEIS [A033308](https://oeis.org/A033308)) and
|
|
35
|
+
base-$b$ Champernowne constants.
|
|
29
36
|
|
|
30
|
-
**Author:** Nicolás León Parra · **
|
|
37
|
+
**Author:** Nicolás León Parra · **Version:** v0.3.4 · **Code:** MIT · **Text and data:** CC BY 4.0
|
|
38
|
+
|
|
39
|
+
## Quickstart
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
pip install ceexp # zero required dependencies
|
|
43
|
+
pip install "ceexp[fast]" # optional gmpy2 acceleration
|
|
44
|
+
pip install "ceexp[benchmark]" # optional mpmath comparison
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
ceexp certify --expansion engel --digits 10000 --limit 10
|
|
49
|
+
ceexp certify --expansion luroth --digits-file my_constant.txt --digits 5000
|
|
50
|
+
ceexp bench 50000
|
|
51
|
+
ceexp order 10 100000000000031
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
See [docs/api.md](docs/api.md) for the full Python API reference.
|
|
31
55
|
|
|
32
56
|
## What is in this repository
|
|
33
57
|
|
|
@@ -1,12 +1,34 @@
|
|
|
1
|
-
# Expansions and residues of the Copeland–Erdős constant
|
|
1
|
+
# Expansions and residues of the Copeland–Erdős constant (`ceexp`)
|
|
2
2
|
|
|
3
|
+
[](https://github.com/Nicolas11Leon/copeland-erdos-expansions/actions/workflows/ci.yml)
|
|
4
|
+
[](https://pypi.org/project/ceexp/)
|
|
5
|
+
[](https://pypi.org/project/ceexp/)
|
|
6
|
+
[](LICENSE)
|
|
3
7
|
[](https://doi.org/10.5281/zenodo.24693622)
|
|
4
8
|
|
|
5
|
-
Certified computations, proofs and reproducible experiments on
|
|
6
|
-
**
|
|
7
|
-
|
|
9
|
+
Certified computations, proofs and reproducible experiments on classical unit-fraction expansions
|
|
10
|
+
(**Engel, Pierce, Lüroth, greedy Egyptian, and continued fractions**) of the **Copeland–Erdős
|
|
11
|
+
constant** `C = 0.235711131719232931374143…` (OEIS [A033308](https://oeis.org/A033308)) and
|
|
12
|
+
base-$b$ Champernowne constants.
|
|
8
13
|
|
|
9
|
-
**Author:** Nicolás León Parra · **
|
|
14
|
+
**Author:** Nicolás León Parra · **Version:** v0.3.4 · **Code:** MIT · **Text and data:** CC BY 4.0
|
|
15
|
+
|
|
16
|
+
## Quickstart
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
pip install ceexp # zero required dependencies
|
|
20
|
+
pip install "ceexp[fast]" # optional gmpy2 acceleration
|
|
21
|
+
pip install "ceexp[benchmark]" # optional mpmath comparison
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
ceexp certify --expansion engel --digits 10000 --limit 10
|
|
26
|
+
ceexp certify --expansion luroth --digits-file my_constant.txt --digits 5000
|
|
27
|
+
ceexp bench 50000
|
|
28
|
+
ceexp order 10 100000000000031
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
See [docs/api.md](docs/api.md) for the full Python API reference.
|
|
10
32
|
|
|
11
33
|
## What is in this repository
|
|
12
34
|
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "ceexp"
|
|
7
|
-
version = "0.3.
|
|
7
|
+
version = "0.3.4"
|
|
8
8
|
description = "Interval-certified Engel, Pierce, Luroth, and continued-fraction expansions and precision benchmarks"
|
|
9
9
|
authors = [{ name = "Nicolás León Parra", email = "leonparranicolas@gmail.com" }]
|
|
10
10
|
license = { text = "MIT" }
|
|
@@ -25,11 +25,13 @@ Repository = "https://github.com/Nicolas11Leon/copeland-erdos-expansions"
|
|
|
25
25
|
Issues = "https://github.com/Nicolas11Leon/copeland-erdos-expansions/issues"
|
|
26
26
|
|
|
27
27
|
[project.scripts]
|
|
28
|
+
ceexp = "ceexp.cli:main"
|
|
28
29
|
ceexp-bench = "ceexp.benchmark:main"
|
|
29
30
|
|
|
30
31
|
[project.optional-dependencies]
|
|
31
32
|
experiments = ["numpy>=1.26", "scipy>=1.11"]
|
|
32
33
|
benchmark = ["mpmath>=1.2"]
|
|
34
|
+
fast = ["gmpy2>=2.1"]
|
|
33
35
|
|
|
34
36
|
[tool.setuptools.packages.find]
|
|
35
37
|
where = ["src"]
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"""ceexp: expansions, block-wise unit-fraction series and residues of the Copeland-Erdos constant."""
|
|
2
|
+
from importlib.metadata import PackageNotFoundError, version as _pkg_version
|
|
3
|
+
|
|
2
4
|
from .constant import (concatenation_digits, copeland_erdos_digits, cumulative_digit_counts,
|
|
3
|
-
digits_in_base, first_primes, int_from_digits, int_to_digits, primes_up_to
|
|
4
|
-
unlimited_int_digits)
|
|
5
|
+
digits_in_base, first_primes, int_from_digits, int_to_digits, primes_up_to)
|
|
5
6
|
from .expansions import (certified, certified_interval, continued_fraction, engel,
|
|
6
7
|
greedy_egyptian, luroth, pierce)
|
|
7
8
|
from .residue import (legendre, multiplicative_order, residues, rounding_identity_holds,
|
|
@@ -9,4 +10,7 @@ from .residue import (legendre, multiplicative_order, residues, rounding_identit
|
|
|
9
10
|
from .series import (BLOCK_RULES, CARRY_MODES, blocks, blockwise_series, nico_leon_series,
|
|
10
11
|
prime_blockwise_series)
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
try:
|
|
14
|
+
__version__ = _pkg_version("ceexp")
|
|
15
|
+
except PackageNotFoundError: # pragma: no cover - editable / uninstalled checkout
|
|
16
|
+
__version__ = "0.3.4"
|
|
@@ -15,19 +15,21 @@ import math
|
|
|
15
15
|
import sys
|
|
16
16
|
import time
|
|
17
17
|
import tracemalloc
|
|
18
|
+
from functools import partial
|
|
18
19
|
from pathlib import Path
|
|
19
|
-
from typing import Callable
|
|
20
|
+
from typing import Any, Callable
|
|
20
21
|
|
|
21
22
|
try:
|
|
22
23
|
import mpmath
|
|
23
24
|
except ImportError: # pragma: no cover - optional dependency
|
|
24
25
|
mpmath = None
|
|
25
26
|
|
|
26
|
-
from .constant import copeland_erdos_digits, digits_in_base
|
|
27
|
+
from .constant import copeland_erdos_digits, digits_in_base, int_to_digits
|
|
27
28
|
from .expansions import (
|
|
28
29
|
certified_interval,
|
|
29
30
|
continued_fraction,
|
|
30
31
|
engel,
|
|
32
|
+
gmpy2 as _gmpy2,
|
|
31
33
|
luroth,
|
|
32
34
|
pierce,
|
|
33
35
|
)
|
|
@@ -51,8 +53,6 @@ def _champernowne_digits(n_digits: int, base: int) -> list[int]:
|
|
|
51
53
|
|
|
52
54
|
|
|
53
55
|
def _base_digits_to_decimal_str(digits: list[int], base: int, n_digits: int) -> str:
|
|
54
|
-
sys.set_int_max_str_digits(max(10000, n_digits + 100))
|
|
55
|
-
|
|
56
56
|
def to_int(seq: list[int], cache: dict[int, int]) -> int:
|
|
57
57
|
n = len(seq)
|
|
58
58
|
if n <= 64:
|
|
@@ -68,7 +68,7 @@ def _base_digits_to_decimal_str(digits: list[int], base: int, n_digits: int) ->
|
|
|
68
68
|
|
|
69
69
|
num = to_int(digits, {})
|
|
70
70
|
scaled = (num * (10**n_digits)) // (base ** len(digits))
|
|
71
|
-
return
|
|
71
|
+
return int_to_digits(scaled).zfill(n_digits)
|
|
72
72
|
|
|
73
73
|
|
|
74
74
|
def run_float64(digits: str, mode: str, max_terms: int) -> list[int]:
|
|
@@ -183,15 +183,21 @@ def first_divergence_index(truth: list[int], candidate: list[int]) -> int | None
|
|
|
183
183
|
return None
|
|
184
184
|
|
|
185
185
|
|
|
186
|
-
def _measure(
|
|
187
|
-
|
|
188
|
-
|
|
186
|
+
def _measure(
|
|
187
|
+
fn: Callable[[], list[int]], measure_memory: bool = False
|
|
188
|
+
) -> tuple[list[int], float, float | None]:
|
|
189
|
+
"""Run `fn` cleanly for timing; optionally measure peak KB in a separate pass."""
|
|
189
190
|
ts = time.monotonic()
|
|
190
191
|
res = fn()
|
|
191
192
|
ms = round((time.monotonic() - ts) * 1000, 2)
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
193
|
+
peak_kb: float | None = None
|
|
194
|
+
if measure_memory:
|
|
195
|
+
tracemalloc.start()
|
|
196
|
+
fn()
|
|
197
|
+
_, peak = tracemalloc.get_traced_memory()
|
|
198
|
+
tracemalloc.stop()
|
|
199
|
+
peak_kb = round(peak / 1024, 1)
|
|
200
|
+
return res, ms, peak_kb
|
|
195
201
|
|
|
196
202
|
|
|
197
203
|
def build_benchmark_constants(n_digits: int) -> dict[str, str]:
|
|
@@ -214,70 +220,78 @@ def run_benchmark(
|
|
|
214
220
|
decimal_precs: tuple[int, ...] = (50, 200, 1000),
|
|
215
221
|
trunc_digits: tuple[int, ...] = (500, 2000),
|
|
216
222
|
modes: tuple[str, ...] = ("engel", "pierce", "luroth", "continued_fraction"),
|
|
217
|
-
|
|
223
|
+
measure_memory: bool = False,
|
|
224
|
+
) -> dict[str, Any]:
|
|
218
225
|
"""Benchmark uncertified engines against interval-certified ground truth."""
|
|
219
226
|
constants = build_benchmark_constants(n_digits)
|
|
220
|
-
|
|
227
|
+
cases: dict[str, Any] = {}
|
|
228
|
+
report: dict[str, Any] = {
|
|
221
229
|
"n_digits": n_digits,
|
|
230
|
+
"backend": "gmpy2" if _gmpy2 is not None else "python-int",
|
|
222
231
|
"mpmath_version": getattr(mpmath, "__version__", None),
|
|
223
|
-
"cases":
|
|
232
|
+
"cases": cases,
|
|
224
233
|
}
|
|
225
234
|
|
|
226
235
|
for name, digits in constants.items():
|
|
227
236
|
case_entry: dict[str, object] = {}
|
|
228
237
|
for mode in modes:
|
|
229
238
|
exp_fn = EXPANSIONS[mode]
|
|
230
|
-
truth, cert_ms, cert_kb = _measure(
|
|
239
|
+
truth, cert_ms, cert_kb = _measure(
|
|
240
|
+
lambda: certified_interval(exp_fn, digits), measure_memory=measure_memory
|
|
241
|
+
)
|
|
231
242
|
max_terms = len(truth)
|
|
232
243
|
engines: dict[str, object] = {}
|
|
233
244
|
|
|
234
245
|
def record(label: str, fn: Callable[[], list[int]]) -> None:
|
|
235
|
-
cand, ms, kb = _measure(fn)
|
|
246
|
+
cand, ms, kb = _measure(fn, measure_memory=measure_memory)
|
|
236
247
|
div_k = first_divergence_index(truth, cand)
|
|
237
248
|
entry: dict[str, object] = {
|
|
238
249
|
"terms_produced": len(cand),
|
|
239
250
|
"first_divergence_k": div_k,
|
|
240
251
|
"elapsed_ms": ms,
|
|
241
|
-
"peak_kb": kb,
|
|
242
252
|
}
|
|
253
|
+
if kb is not None:
|
|
254
|
+
entry["peak_kb"] = kb
|
|
243
255
|
if div_k is not None:
|
|
244
256
|
entry["truth_term"] = str(truth[div_k - 1])[:24]
|
|
245
257
|
entry["engine_term"] = str(cand[div_k - 1])[:24]
|
|
246
258
|
engines[label] = entry
|
|
247
259
|
|
|
248
|
-
record("float64",
|
|
260
|
+
record("float64", partial(run_float64, digits, mode, max_terms))
|
|
249
261
|
for prec in decimal_precs:
|
|
250
262
|
record(
|
|
251
263
|
f"decimal_{prec}",
|
|
252
|
-
|
|
264
|
+
partial(run_decimal, digits, mode, prec, max_terms),
|
|
253
265
|
)
|
|
254
266
|
if mpmath is not None:
|
|
255
267
|
record(
|
|
256
268
|
f"mpmath_{prec}",
|
|
257
|
-
|
|
269
|
+
partial(run_mpmath, digits, mode, prec, max_terms),
|
|
258
270
|
)
|
|
259
271
|
for td in trunc_digits:
|
|
260
272
|
if td < n_digits:
|
|
261
273
|
record(
|
|
262
274
|
f"trunc_{td}",
|
|
263
|
-
|
|
264
|
-
digits, exp_fn, d, max_terms
|
|
265
|
-
),
|
|
275
|
+
partial(run_uncertified_truncation, digits, exp_fn, td, max_terms),
|
|
266
276
|
)
|
|
267
277
|
|
|
268
|
-
|
|
278
|
+
entry_mode: dict[str, Any] = {
|
|
269
279
|
"certified_terms": max_terms,
|
|
270
280
|
"certified_elapsed_ms": cert_ms,
|
|
271
|
-
"certified_peak_kb": cert_kb,
|
|
272
281
|
"engines": engines,
|
|
273
282
|
}
|
|
274
|
-
|
|
283
|
+
if cert_kb is not None:
|
|
284
|
+
entry_mode["certified_peak_kb"] = cert_kb
|
|
285
|
+
case_entry[mode] = entry_mode
|
|
286
|
+
cases[name] = case_entry
|
|
275
287
|
return report
|
|
276
288
|
|
|
277
289
|
|
|
278
290
|
def main() -> None:
|
|
279
|
-
|
|
280
|
-
|
|
291
|
+
args = [a for a in sys.argv[1:] if a != "--memory"]
|
|
292
|
+
measure_memory = "--memory" in sys.argv[1:]
|
|
293
|
+
n_digits = int(args[0]) if args else 50_000
|
|
294
|
+
report = run_benchmark(n_digits=n_digits, measure_memory=measure_memory)
|
|
281
295
|
|
|
282
296
|
repo_root = Path(__file__).resolve().parent.parent.parent
|
|
283
297
|
if (repo_root / "pyproject.toml").exists():
|
|
@@ -288,7 +302,8 @@ def main() -> None:
|
|
|
288
302
|
out_path.write_text(json.dumps(report, indent=2) + "\n", encoding="utf-8")
|
|
289
303
|
|
|
290
304
|
print(
|
|
291
|
-
f"Precision-failure benchmark (N={n_digits:,} digits,
|
|
305
|
+
f"Precision-failure benchmark (N={n_digits:,} digits, "
|
|
306
|
+
f"backend={report['backend']}, mpmath={report['mpmath_version']})"
|
|
292
307
|
)
|
|
293
308
|
for name, modes in report["cases"].items():
|
|
294
309
|
for mode, data in modes.items():
|
|
@@ -298,9 +313,14 @@ def main() -> None:
|
|
|
298
313
|
else f"{eng}=ok({info['terms_produced']})"
|
|
299
314
|
for eng, info in data["engines"].items()
|
|
300
315
|
)
|
|
316
|
+
mem_str = (
|
|
317
|
+
f", {data['certified_peak_kb']:.0f}KB"
|
|
318
|
+
if "certified_peak_kb" in data
|
|
319
|
+
else ""
|
|
320
|
+
)
|
|
301
321
|
print(
|
|
302
322
|
f" {name:18s} [{mode:18s}] cert={data['certified_terms']:5d} "
|
|
303
|
-
f"({data['certified_elapsed_ms']:.0f}ms
|
|
323
|
+
f"({data['certified_elapsed_ms']:.0f}ms{mem_str}) -> {summary}"
|
|
304
324
|
)
|
|
305
325
|
print(f"Saved report to {out_path}")
|
|
306
326
|
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"""Command-line interface for ceexp."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import re
|
|
7
|
+
import sys
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Sequence
|
|
10
|
+
|
|
11
|
+
from . import __version__
|
|
12
|
+
from .benchmark import EXPANSIONS, _champernowne_digits, main as bench_main
|
|
13
|
+
from .constant import copeland_erdos_digits
|
|
14
|
+
from .expansions import certified_interval, greedy_egyptian
|
|
15
|
+
from .residue import multiplicative_order
|
|
16
|
+
|
|
17
|
+
CLI_EXPANSIONS = {**EXPANSIONS, "greedy": greedy_egyptian}
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _load_digits_file(path: Path, limit: int | None) -> str:
|
|
21
|
+
raw = path.read_text(encoding="utf-8").strip()
|
|
22
|
+
if "." in raw:
|
|
23
|
+
raw = raw.split(".", 1)[1]
|
|
24
|
+
digits = re.sub(r"\D", "", raw)
|
|
25
|
+
if not digits:
|
|
26
|
+
raise ValueError(f"No decimal digits found in {path}")
|
|
27
|
+
return digits[:limit] if limit is not None else digits
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _cmd_certify(args: argparse.Namespace) -> int:
|
|
31
|
+
if args.digits_file:
|
|
32
|
+
digits = _load_digits_file(Path(args.digits_file), args.digits)
|
|
33
|
+
elif args.constant == "champernowne-10":
|
|
34
|
+
digits = "".join(str(d) for d in _champernowne_digits(args.digits, 10))
|
|
35
|
+
else:
|
|
36
|
+
digits = copeland_erdos_digits(args.digits)
|
|
37
|
+
|
|
38
|
+
exp_fn = CLI_EXPANSIONS[args.expansion]
|
|
39
|
+
terms = certified_interval(exp_fn, digits)
|
|
40
|
+
shown = terms[: args.limit] if args.limit else terms
|
|
41
|
+
print(f"Certified {len(terms)} terms ({args.expansion}, N={len(digits)} digits):")
|
|
42
|
+
print(", ".join(str(t) for t in shown))
|
|
43
|
+
return 0
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _cmd_order(args: argparse.Namespace) -> int:
|
|
47
|
+
val = multiplicative_order(args.a, args.p)
|
|
48
|
+
print(f"ord_{args.p}({args.a}) = {val}")
|
|
49
|
+
return 0
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
53
|
+
parser = argparse.ArgumentParser(
|
|
54
|
+
prog="ceexp",
|
|
55
|
+
description="Interval-certified expansions and precision benchmarks.",
|
|
56
|
+
)
|
|
57
|
+
parser.add_argument("--version", action="version", version=f"ceexp {__version__}")
|
|
58
|
+
sub = parser.add_subparsers(dest="command", required=True)
|
|
59
|
+
|
|
60
|
+
p_cert = sub.add_parser("certify", help="Interval-certify an expansion of a constant.")
|
|
61
|
+
p_cert.add_argument(
|
|
62
|
+
"--expansion",
|
|
63
|
+
choices=sorted(CLI_EXPANSIONS.keys()),
|
|
64
|
+
default="engel",
|
|
65
|
+
help="Expansion type (default: engel).",
|
|
66
|
+
)
|
|
67
|
+
p_cert.add_argument(
|
|
68
|
+
"--constant",
|
|
69
|
+
choices=("copeland-erdos", "champernowne-10"),
|
|
70
|
+
default="copeland-erdos",
|
|
71
|
+
help="Built-in constant when --digits-file is omitted.",
|
|
72
|
+
)
|
|
73
|
+
p_cert.add_argument(
|
|
74
|
+
"--digits-file",
|
|
75
|
+
help="Path to a text file containing fractional digits (0.xxxx or xxxx).",
|
|
76
|
+
)
|
|
77
|
+
p_cert.add_argument(
|
|
78
|
+
"--digits",
|
|
79
|
+
type=int,
|
|
80
|
+
default=5000,
|
|
81
|
+
help="Number of decimal digits to use (default: 5000).",
|
|
82
|
+
)
|
|
83
|
+
p_cert.add_argument(
|
|
84
|
+
"--limit",
|
|
85
|
+
type=int,
|
|
86
|
+
default=20,
|
|
87
|
+
help="Maximum number of certified terms to print (0 = all, default: 20).",
|
|
88
|
+
)
|
|
89
|
+
p_cert.set_defaults(func=_cmd_certify)
|
|
90
|
+
|
|
91
|
+
p_bench = sub.add_parser("bench", help="Run the precision-failure benchmark.")
|
|
92
|
+
p_bench.add_argument("n_digits", nargs="?", type=int, default=50_000)
|
|
93
|
+
p_bench.add_argument("--memory", action="store_true", help="Measure peak KB separately.")
|
|
94
|
+
|
|
95
|
+
p_ord = sub.add_parser("order", help="Compute multiplicative order of a mod prime p.")
|
|
96
|
+
p_ord.add_argument("a", type=int)
|
|
97
|
+
p_ord.add_argument("p", type=int)
|
|
98
|
+
p_ord.set_defaults(func=_cmd_order)
|
|
99
|
+
|
|
100
|
+
return parser
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def main(argv: Sequence[str] | None = None) -> int:
|
|
104
|
+
parser = build_parser()
|
|
105
|
+
args = parser.parse_args(list(argv) if argv is not None else None)
|
|
106
|
+
if args.command == "bench":
|
|
107
|
+
bench_argv = ["ceexp-bench", str(args.n_digits)]
|
|
108
|
+
if args.memory:
|
|
109
|
+
bench_argv.append("--memory")
|
|
110
|
+
old_argv = sys.argv
|
|
111
|
+
try:
|
|
112
|
+
sys.argv = bench_argv
|
|
113
|
+
bench_main()
|
|
114
|
+
finally:
|
|
115
|
+
sys.argv = old_argv
|
|
116
|
+
return 0
|
|
117
|
+
return int(args.func(args))
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
if __name__ == "__main__":
|
|
121
|
+
raise SystemExit(main())
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Every function takes the real as an exact rational n/d (typically d = 10^N for N known
|
|
4
4
|
digits) and returns only terms that the available precision certifies: a term is emitted
|
|
5
5
|
only while the digits it consumes stay below the precision budget minus `guard` digits.
|
|
6
|
-
|
|
6
|
+
Use ``certified_interval`` for a formal interval proof over [0.digits, 0.digits + 10^-N].
|
|
7
7
|
|
|
8
8
|
Definitions (x > 0):
|
|
9
9
|
Engel: x = 1/a1 + 1/(a1 a2) + 1/(a1 a2 a3) + ..., a_k = ceil(1/x_k), x_{k+1} = a_k x_k - 1
|
|
@@ -161,5 +161,4 @@ __all__ = [
|
|
|
161
161
|
"certified",
|
|
162
162
|
"certified_interval",
|
|
163
163
|
"common_prefix",
|
|
164
|
-
"log10",
|
|
165
164
|
]
|
|
@@ -13,7 +13,7 @@ Block rules:
|
|
|
13
13
|
"""
|
|
14
14
|
from __future__ import annotations
|
|
15
15
|
|
|
16
|
-
from dataclasses import dataclass
|
|
16
|
+
from dataclasses import dataclass, field
|
|
17
17
|
from fractions import Fraction
|
|
18
18
|
|
|
19
19
|
from .constant import digits_in_base, first_primes
|
|
@@ -41,7 +41,13 @@ def blocks(primes: list[int], rule: str, base: int = 10) -> list[tuple[int, int]
|
|
|
41
41
|
class SeriesTerm:
|
|
42
42
|
denominator: int # signed for the "signed" mode
|
|
43
43
|
u: float # 9-digit float summary of u_k = a_k^2 * |E_k|
|
|
44
|
-
|
|
44
|
+
_u_num: int = field(default=0, repr=False, compare=False)
|
|
45
|
+
_u_den: int = field(default=1, repr=False, compare=False)
|
|
46
|
+
|
|
47
|
+
@property
|
|
48
|
+
def u_exact(self) -> Fraction:
|
|
49
|
+
"""Exact rational u_k = a_k^2 * |E_k|, computed lazily on access."""
|
|
50
|
+
return Fraction(self._u_num, self._u_den)
|
|
45
51
|
|
|
46
52
|
|
|
47
53
|
def blockwise_series(blks: list[tuple[int, int]], base: int = 10, carry: str = "ceil") -> list[SeriesTerm]:
|
|
@@ -61,8 +67,7 @@ def blockwise_series(blks: list[tuple[int, int]], base: int = 10, carry: str = "
|
|
|
61
67
|
if carry == "none":
|
|
62
68
|
a = (2 * base**pos + value) // (2 * value) # round(base^pos / value)
|
|
63
69
|
err = abs(block - S // a)
|
|
64
|
-
|
|
65
|
-
out.append(SeriesTerm(a, (a * a * err * 10**9 // S) / 1e9, u_ex))
|
|
70
|
+
out.append(SeriesTerm(a, (a * a * err * 10**9 // S) / 1e9, a * a * err, S))
|
|
66
71
|
continue
|
|
67
72
|
x = target - acc
|
|
68
73
|
if carry == "ceil":
|
|
@@ -76,8 +81,7 @@ def blockwise_series(blks: list[tuple[int, int]], base: int = 10, carry: str = "
|
|
|
76
81
|
a = (2 * S + abs(x)) // (2 * abs(x))
|
|
77
82
|
acc += sign * (S // a)
|
|
78
83
|
err = abs(target - acc)
|
|
79
|
-
|
|
80
|
-
out.append(SeriesTerm(sign * a, (a * a * err * 10**9 // S) / 1e9, u_ex))
|
|
84
|
+
out.append(SeriesTerm(sign * a, (a * a * err * 10**9 // S) / 1e9, a * a * err, S))
|
|
81
85
|
return out
|
|
82
86
|
|
|
83
87
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: ceexp
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.4
|
|
4
4
|
Summary: Interval-certified Engel, Pierce, Luroth, and continued-fraction expansions and precision benchmarks
|
|
5
5
|
Author-email: Nicolás León Parra <leonparranicolas@gmail.com>
|
|
6
6
|
License: MIT
|
|
@@ -17,17 +17,41 @@ Requires-Dist: numpy>=1.26; extra == "experiments"
|
|
|
17
17
|
Requires-Dist: scipy>=1.11; extra == "experiments"
|
|
18
18
|
Provides-Extra: benchmark
|
|
19
19
|
Requires-Dist: mpmath>=1.2; extra == "benchmark"
|
|
20
|
+
Provides-Extra: fast
|
|
21
|
+
Requires-Dist: gmpy2>=2.1; extra == "fast"
|
|
20
22
|
Dynamic: license-file
|
|
21
23
|
|
|
22
|
-
# Expansions and residues of the Copeland–Erdős constant
|
|
24
|
+
# Expansions and residues of the Copeland–Erdős constant (`ceexp`)
|
|
23
25
|
|
|
26
|
+
[](https://github.com/Nicolas11Leon/copeland-erdos-expansions/actions/workflows/ci.yml)
|
|
27
|
+
[](https://pypi.org/project/ceexp/)
|
|
28
|
+
[](https://pypi.org/project/ceexp/)
|
|
29
|
+
[](LICENSE)
|
|
24
30
|
[](https://doi.org/10.5281/zenodo.24693622)
|
|
25
31
|
|
|
26
|
-
Certified computations, proofs and reproducible experiments on
|
|
27
|
-
**
|
|
28
|
-
|
|
32
|
+
Certified computations, proofs and reproducible experiments on classical unit-fraction expansions
|
|
33
|
+
(**Engel, Pierce, Lüroth, greedy Egyptian, and continued fractions**) of the **Copeland–Erdős
|
|
34
|
+
constant** `C = 0.235711131719232931374143…` (OEIS [A033308](https://oeis.org/A033308)) and
|
|
35
|
+
base-$b$ Champernowne constants.
|
|
29
36
|
|
|
30
|
-
**Author:** Nicolás León Parra · **
|
|
37
|
+
**Author:** Nicolás León Parra · **Version:** v0.3.4 · **Code:** MIT · **Text and data:** CC BY 4.0
|
|
38
|
+
|
|
39
|
+
## Quickstart
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
pip install ceexp # zero required dependencies
|
|
43
|
+
pip install "ceexp[fast]" # optional gmpy2 acceleration
|
|
44
|
+
pip install "ceexp[benchmark]" # optional mpmath comparison
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
ceexp certify --expansion engel --digits 10000 --limit 10
|
|
49
|
+
ceexp certify --expansion luroth --digits-file my_constant.txt --digits 5000
|
|
50
|
+
ceexp bench 50000
|
|
51
|
+
ceexp order 10 100000000000031
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
See [docs/api.md](docs/api.md) for the full Python API reference.
|
|
31
55
|
|
|
32
56
|
## What is in this repository
|
|
33
57
|
|
|
@@ -4,6 +4,7 @@ README.md
|
|
|
4
4
|
pyproject.toml
|
|
5
5
|
src/ceexp/__init__.py
|
|
6
6
|
src/ceexp/benchmark.py
|
|
7
|
+
src/ceexp/cli.py
|
|
7
8
|
src/ceexp/constant.py
|
|
8
9
|
src/ceexp/expansions.py
|
|
9
10
|
src/ceexp/residue.py
|
|
@@ -17,6 +18,7 @@ src/ceexp.egg-info/requires.txt
|
|
|
17
18
|
src/ceexp.egg-info/top_level.txt
|
|
18
19
|
tests/test_benchmark.py
|
|
19
20
|
tests/test_champernowne_comparison.py
|
|
21
|
+
tests/test_cli.py
|
|
20
22
|
tests/test_expansions.py
|
|
21
23
|
tests/test_properties.py
|
|
22
24
|
tests/test_reference.py
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"""Tests for the ceexp command-line interface."""
|
|
2
|
+
|
|
3
|
+
import io
|
|
4
|
+
import sys
|
|
5
|
+
import tempfile
|
|
6
|
+
import unittest
|
|
7
|
+
from contextlib import redirect_stdout
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
ROOT = Path(__file__).resolve().parent.parent
|
|
11
|
+
sys.path.insert(0, str(ROOT / "src"))
|
|
12
|
+
|
|
13
|
+
from ceexp import cli # noqa: E402
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class TestCLI(unittest.TestCase):
|
|
17
|
+
def test_certify_copeland_erdos_engel(self) -> None:
|
|
18
|
+
buf = io.StringIO()
|
|
19
|
+
with redirect_stdout(buf):
|
|
20
|
+
rc = cli.main(
|
|
21
|
+
["certify", "--expansion", "engel", "--digits", "1000", "--limit", "6"]
|
|
22
|
+
)
|
|
23
|
+
self.assertEqual(rc, 0)
|
|
24
|
+
out = buf.getvalue()
|
|
25
|
+
self.assertIn("5, 6, 15, 15, 20, 360", out)
|
|
26
|
+
|
|
27
|
+
def test_certify_custom_digits_file(self) -> None:
|
|
28
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
29
|
+
p = Path(tmp) / "custom.txt"
|
|
30
|
+
# 2/5 = 0.4000... -> Luroth [3, 3, 3, ...]
|
|
31
|
+
p.write_text("0.4" + "0" * 199 + "\n", encoding="utf-8")
|
|
32
|
+
buf = io.StringIO()
|
|
33
|
+
with redirect_stdout(buf):
|
|
34
|
+
rc = cli.main(
|
|
35
|
+
[
|
|
36
|
+
"certify",
|
|
37
|
+
"--expansion",
|
|
38
|
+
"luroth",
|
|
39
|
+
"--digits-file",
|
|
40
|
+
str(p),
|
|
41
|
+
"--digits",
|
|
42
|
+
"200",
|
|
43
|
+
"--limit",
|
|
44
|
+
"5",
|
|
45
|
+
]
|
|
46
|
+
)
|
|
47
|
+
self.assertEqual(rc, 0)
|
|
48
|
+
self.assertIn("3, 3, 3, 3, 3", buf.getvalue())
|
|
49
|
+
|
|
50
|
+
def test_order_subcommand(self) -> None:
|
|
51
|
+
buf = io.StringIO()
|
|
52
|
+
with redirect_stdout(buf):
|
|
53
|
+
rc = cli.main(["order", "10", "100000000000031"])
|
|
54
|
+
self.assertEqual(rc, 0)
|
|
55
|
+
self.assertIn("ord_100000000000031(10) = 50000000000015", buf.getvalue())
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
if __name__ == "__main__":
|
|
59
|
+
unittest.main()
|
|
@@ -10,14 +10,14 @@ DIGITS = copeland_erdos_digits(6000)
|
|
|
10
10
|
|
|
11
11
|
class TestExpansions(unittest.TestCase):
|
|
12
12
|
def test_engel_two_engines_and_reconstruction(self):
|
|
13
|
-
terms =
|
|
13
|
+
terms = certified_interval(engel, DIGITS)
|
|
14
14
|
self.assertGreater(len(terms), 100)
|
|
15
15
|
self.assertEqual(terms[:6], [5, 6, 15, 15, 20, 360])
|
|
16
16
|
self.assertEqual(engel_decimal(DIGITS, len(terms)), terms)
|
|
17
17
|
self.assertGreater(matching_digits(reconstruct(terms, False, 6000), DIGITS), 2900)
|
|
18
18
|
|
|
19
19
|
def test_pierce_two_engines_and_reconstruction(self):
|
|
20
|
-
terms =
|
|
20
|
+
terms = certified_interval(pierce, DIGITS)
|
|
21
21
|
self.assertGreater(len(terms), 100)
|
|
22
22
|
self.assertEqual(terms[:6], [4, 17, 35, 133, 611, 725])
|
|
23
23
|
self.assertEqual(pierce_decimal(DIGITS, len(terms)), terms)
|
|
@@ -25,7 +25,9 @@ class TestExpansions(unittest.TestCase):
|
|
|
25
25
|
|
|
26
26
|
def test_interval_certificate_agrees_with_precision_doubling(self):
|
|
27
27
|
for f in (engel, pierce, greedy_egyptian):
|
|
28
|
-
a
|
|
28
|
+
a = certified_interval(f, DIGITS)
|
|
29
|
+
with self.assertWarns(DeprecationWarning):
|
|
30
|
+
b = certified(f, DIGITS)
|
|
29
31
|
k = min(len(a), len(b))
|
|
30
32
|
self.assertGreater(k, 10)
|
|
31
33
|
self.assertEqual(a[:k], b[:k], f.__name__)
|
|
@@ -37,7 +39,7 @@ class TestExpansions(unittest.TestCase):
|
|
|
37
39
|
self.assertEqual(long[: len(short)], short, f.__name__)
|
|
38
40
|
|
|
39
41
|
def test_greedy_first_terms(self):
|
|
40
|
-
terms =
|
|
42
|
+
terms = certified_interval(greedy_egyptian, DIGITS)
|
|
41
43
|
self.assertEqual(terms[:5], [5, 29, 815, 725041, 857823500393])
|
|
42
44
|
|
|
43
45
|
|
|
@@ -4,13 +4,16 @@ import decimal
|
|
|
4
4
|
import random
|
|
5
5
|
import sys
|
|
6
6
|
import unittest
|
|
7
|
+
from fractions import Fraction
|
|
8
|
+
from math import isqrt
|
|
7
9
|
from pathlib import Path
|
|
8
10
|
|
|
9
11
|
ROOT = Path(__file__).resolve().parent.parent
|
|
10
12
|
sys.path.insert(0, str(ROOT / "src"))
|
|
11
13
|
|
|
12
14
|
import ceexp # noqa: E402
|
|
13
|
-
from ceexp import verify # noqa: E402
|
|
15
|
+
from ceexp import benchmark, verify # noqa: E402
|
|
16
|
+
from ceexp.residue import _prime_factors # noqa: E402
|
|
14
17
|
|
|
15
18
|
|
|
16
19
|
class TestProperties(unittest.TestCase):
|
|
@@ -32,6 +35,15 @@ class TestProperties(unittest.TestCase):
|
|
|
32
35
|
self.assertEqual(ceexp.int_to_digits(parsed), big_str)
|
|
33
36
|
self.assertEqual(sys.get_int_max_str_digits(), orig_max_digits)
|
|
34
37
|
|
|
38
|
+
# Benchmark must also leave sys.get_int_max_str_digits untouched.
|
|
39
|
+
benchmark.run_benchmark(
|
|
40
|
+
n_digits=5000,
|
|
41
|
+
decimal_precs=(50,),
|
|
42
|
+
trunc_digits=(500,),
|
|
43
|
+
modes=("engel",),
|
|
44
|
+
)
|
|
45
|
+
self.assertEqual(sys.get_int_max_str_digits(), orig_max_digits)
|
|
46
|
+
|
|
35
47
|
def test_interval_certificate_property_random_reals(self) -> None:
|
|
36
48
|
rng = random.Random(2026)
|
|
37
49
|
expansions = (
|
|
@@ -44,7 +56,6 @@ class TestProperties(unittest.TestCase):
|
|
|
44
56
|
prefix = "".join(str(rng.randrange(10)) for _ in range(250))
|
|
45
57
|
if set(prefix) <= {"0"}:
|
|
46
58
|
continue
|
|
47
|
-
# Pick a random interior point inside (L, U) by appending 40 random digits.
|
|
48
59
|
interior = prefix + "".join(str(rng.randrange(10)) for _ in range(40))
|
|
49
60
|
n_int = ceexp.int_from_digits(interior)
|
|
50
61
|
d_int = 10 ** len(interior)
|
|
@@ -55,6 +66,23 @@ class TestProperties(unittest.TestCase):
|
|
|
55
66
|
inner_terms = exp_fn(n_int, d_int)
|
|
56
67
|
self.assertEqual(inner_terms[: len(cert)], cert)
|
|
57
68
|
|
|
69
|
+
def test_luroth_external_reference_and_reconstruction(self) -> None:
|
|
70
|
+
# 2/5 = 0.4 has the exact periodic Luroth expansion [3, 3, 3, ...].
|
|
71
|
+
self.assertEqual(ceexp.luroth(2 * 10**60, 5 * 10**60)[:10], [3] * 10)
|
|
72
|
+
|
|
73
|
+
# Reconstruct sqrt(2) - 1 from its certified Luroth terms and verify digits.
|
|
74
|
+
K = 200
|
|
75
|
+
sqrt2_m1 = ceexp.int_to_digits(isqrt(2 * 10 ** (2 * K)) - 10**K).zfill(K)
|
|
76
|
+
terms = ceexp.certified_interval(ceexp.luroth, sqrt2_m1)
|
|
77
|
+
self.assertGreater(len(terms), 80)
|
|
78
|
+
val = Fraction(0)
|
|
79
|
+
prefix_prod = 1
|
|
80
|
+
for a in terms[:40]:
|
|
81
|
+
val += Fraction(1, a * prefix_prod)
|
|
82
|
+
prefix_prod *= a * (a - 1)
|
|
83
|
+
recon_digits = ceexp.int_to_digits((val.numerator * 10**40) // val.denominator).zfill(40)
|
|
84
|
+
self.assertEqual(recon_digits[:35], sqrt2_m1[:35])
|
|
85
|
+
|
|
58
86
|
def test_series_u_exact_property_and_alias(self) -> None:
|
|
59
87
|
for rule in ("prime", "digit", "mod_base", "digital_root"):
|
|
60
88
|
for carry in ("ceil", "signed", "none"):
|
|
@@ -62,18 +90,20 @@ class TestProperties(unittest.TestCase):
|
|
|
62
90
|
s2 = ceexp.nico_leon_series(15, rule=rule, base=10, carry=carry)
|
|
63
91
|
self.assertEqual(s1, s2)
|
|
64
92
|
for term in s1:
|
|
93
|
+
a = abs(term.denominator)
|
|
65
94
|
self.assertGreaterEqual(term.u_exact, 0)
|
|
66
|
-
|
|
95
|
+
# Proven bound from Prop 6: u_k < a / (a - 1) for a >= 2.
|
|
96
|
+
if a >= 2:
|
|
97
|
+
self.assertLess(term.u_exact, Fraction(a, a - 1))
|
|
67
98
|
self.assertAlmostEqual(float(term.u_exact), term.u, places=8)
|
|
68
99
|
|
|
69
|
-
def
|
|
70
|
-
# Primes > 10^12 where naive sqrt(p) trial division is slow.
|
|
100
|
+
def test_multiplicative_order_large_primes_exact_minimality(self) -> None:
|
|
71
101
|
for p in (1_000_000_000_039, 100_000_000_000_031):
|
|
72
102
|
ord_10 = ceexp.multiplicative_order(10, p)
|
|
73
103
|
self.assertEqual((p - 1) % ord_10, 0)
|
|
74
104
|
self.assertEqual(pow(10, ord_10, p), 1)
|
|
75
|
-
|
|
76
|
-
|
|
105
|
+
for q in _prime_factors(ord_10):
|
|
106
|
+
self.assertNotEqual(pow(10, ord_10 // q, p), 1)
|
|
77
107
|
|
|
78
108
|
|
|
79
109
|
if __name__ == "__main__":
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|