ceexp 0.3.4__tar.gz → 0.3.6__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.
Files changed (28) hide show
  1. {ceexp-0.3.4/src/ceexp.egg-info → ceexp-0.3.6}/PKG-INFO +2 -2
  2. {ceexp-0.3.4 → ceexp-0.3.6}/README.md +1 -1
  3. {ceexp-0.3.4 → ceexp-0.3.6}/pyproject.toml +1 -1
  4. {ceexp-0.3.4 → ceexp-0.3.6}/src/ceexp/__init__.py +1 -1
  5. {ceexp-0.3.4 → ceexp-0.3.6}/src/ceexp/benchmark.py +23 -7
  6. {ceexp-0.3.4 → ceexp-0.3.6}/src/ceexp/cli.py +3 -0
  7. {ceexp-0.3.4 → ceexp-0.3.6}/src/ceexp/constant.py +5 -1
  8. {ceexp-0.3.4 → ceexp-0.3.6}/src/ceexp/expansions.py +49 -6
  9. {ceexp-0.3.4 → ceexp-0.3.6}/src/ceexp/residue.py +6 -0
  10. {ceexp-0.3.4 → ceexp-0.3.6/src/ceexp.egg-info}/PKG-INFO +2 -2
  11. {ceexp-0.3.4 → ceexp-0.3.6}/tests/test_cli.py +12 -0
  12. {ceexp-0.3.4 → ceexp-0.3.6}/tests/test_properties.py +18 -1
  13. {ceexp-0.3.4 → ceexp-0.3.6}/LICENSE +0 -0
  14. {ceexp-0.3.4 → ceexp-0.3.6}/LICENSE-docs.md +0 -0
  15. {ceexp-0.3.4 → ceexp-0.3.6}/setup.cfg +0 -0
  16. {ceexp-0.3.4 → ceexp-0.3.6}/src/ceexp/series.py +0 -0
  17. {ceexp-0.3.4 → ceexp-0.3.6}/src/ceexp/verify.py +0 -0
  18. {ceexp-0.3.4 → ceexp-0.3.6}/src/ceexp.egg-info/SOURCES.txt +0 -0
  19. {ceexp-0.3.4 → ceexp-0.3.6}/src/ceexp.egg-info/dependency_links.txt +0 -0
  20. {ceexp-0.3.4 → ceexp-0.3.6}/src/ceexp.egg-info/entry_points.txt +0 -0
  21. {ceexp-0.3.4 → ceexp-0.3.6}/src/ceexp.egg-info/requires.txt +0 -0
  22. {ceexp-0.3.4 → ceexp-0.3.6}/src/ceexp.egg-info/top_level.txt +0 -0
  23. {ceexp-0.3.4 → ceexp-0.3.6}/tests/test_benchmark.py +0 -0
  24. {ceexp-0.3.4 → ceexp-0.3.6}/tests/test_champernowne_comparison.py +0 -0
  25. {ceexp-0.3.4 → ceexp-0.3.6}/tests/test_expansions.py +0 -0
  26. {ceexp-0.3.4 → ceexp-0.3.6}/tests/test_reference.py +0 -0
  27. {ceexp-0.3.4 → ceexp-0.3.6}/tests/test_theorems.py +0 -0
  28. {ceexp-0.3.4 → ceexp-0.3.6}/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.4
3
+ Version: 0.3.6
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
@@ -34,7 +34,7 @@ Certified computations, proofs and reproducible experiments on classical unit-fr
34
34
  constant** `C = 0.235711131719232931374143…` (OEIS [A033308](https://oeis.org/A033308)) and
35
35
  base-$b$ Champernowne constants.
36
36
 
37
- **Author:** Nicolás León Parra · **Version:** v0.3.4 · **Code:** MIT · **Text and data:** CC BY 4.0
37
+ **Author:** Nicolás León Parra · **Version:** v0.3.6 · **Code:** MIT · **Text and data:** CC BY 4.0
38
38
 
39
39
  ## Quickstart
40
40
 
@@ -11,7 +11,7 @@ Certified computations, proofs and reproducible experiments on classical unit-fr
11
11
  constant** `C = 0.235711131719232931374143…` (OEIS [A033308](https://oeis.org/A033308)) and
12
12
  base-$b$ Champernowne constants.
13
13
 
14
- **Author:** Nicolás León Parra · **Version:** v0.3.4 · **Code:** MIT · **Text and data:** CC BY 4.0
14
+ **Author:** Nicolás León Parra · **Version:** v0.3.6 · **Code:** MIT · **Text and data:** CC BY 4.0
15
15
 
16
16
  ## Quickstart
17
17
 
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "ceexp"
7
- version = "0.3.4"
7
+ version = "0.3.6"
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" }
@@ -13,4 +13,4 @@ from .series import (BLOCK_RULES, CARRY_MODES, blocks, blockwise_series, nico_le
13
13
  try:
14
14
  __version__ = _pkg_version("ceexp")
15
15
  except PackageNotFoundError: # pragma: no cover - editable / uninstalled checkout
16
- __version__ = "0.3.4"
16
+ __version__ = "0.3.6"
@@ -288,16 +288,32 @@ def run_benchmark(
288
288
 
289
289
 
290
290
  def main() -> None:
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
291
+ raw_args = list(sys.argv[1:])
292
+ measure_memory = "--memory" in raw_args
293
+ out_override: str | None = None
294
+ pos_args: list[str] = []
295
+ i = 0
296
+ while i < len(raw_args):
297
+ if raw_args[i] == "--memory":
298
+ i += 1
299
+ elif raw_args[i] == "--output" and i + 1 < len(raw_args):
300
+ out_override = raw_args[i + 1]
301
+ i += 2
302
+ else:
303
+ pos_args.append(raw_args[i])
304
+ i += 1
305
+
306
+ n_digits = int(pos_args[0]) if pos_args else 50_000
294
307
  report = run_benchmark(n_digits=n_digits, measure_memory=measure_memory)
295
308
 
296
- repo_root = Path(__file__).resolve().parent.parent.parent
297
- if (repo_root / "pyproject.toml").exists():
298
- out_path = repo_root / "experiments" / "results" / "precision_benchmark.json"
309
+ if out_override is not None:
310
+ out_path = Path(out_override)
299
311
  else:
300
- out_path = Path.cwd() / "precision_benchmark.json"
312
+ repo_root = Path(__file__).resolve().parent.parent.parent
313
+ if (repo_root / "pyproject.toml").exists():
314
+ out_path = repo_root / "experiments" / "results" / "precision_benchmark.json"
315
+ else:
316
+ out_path = Path.cwd() / "precision_benchmark.json"
301
317
  out_path.parent.mkdir(parents=True, exist_ok=True)
302
318
  out_path.write_text(json.dumps(report, indent=2) + "\n", encoding="utf-8")
303
319
 
@@ -91,6 +91,7 @@ def build_parser() -> argparse.ArgumentParser:
91
91
  p_bench = sub.add_parser("bench", help="Run the precision-failure benchmark.")
92
92
  p_bench.add_argument("n_digits", nargs="?", type=int, default=50_000)
93
93
  p_bench.add_argument("--memory", action="store_true", help="Measure peak KB separately.")
94
+ p_bench.add_argument("--output", help="Optional path to save the JSON report.")
94
95
 
95
96
  p_ord = sub.add_parser("order", help="Compute multiplicative order of a mod prime p.")
96
97
  p_ord.add_argument("a", type=int)
@@ -107,6 +108,8 @@ def main(argv: Sequence[str] | None = None) -> int:
107
108
  bench_argv = ["ceexp-bench", str(args.n_digits)]
108
109
  if args.memory:
109
110
  bench_argv.append("--memory")
111
+ if args.output:
112
+ bench_argv.extend(["--output", args.output])
110
113
  old_argv = sys.argv
111
114
  try:
112
115
  sys.argv = bench_argv
@@ -105,7 +105,11 @@ def first_primes(count: int) -> list[int]:
105
105
 
106
106
 
107
107
  def digits_in_base(n: int, base: int = 10) -> list[int]:
108
- """Digits of n >= 1 in the given base, most significant first."""
108
+ """Digits of n >= 1 in the given base (>= 2), most significant first."""
109
+ if base < 2:
110
+ raise ValueError(f"base must be >= 2, got {base}")
111
+ if n < 1:
112
+ raise ValueError(f"n must be >= 1, got {n}")
109
113
  out = []
110
114
  while n:
111
115
  n, r = divmod(n, base)
@@ -31,18 +31,61 @@ def _digits(n: int) -> float:
31
31
  return n.bit_length() * LOG10_2
32
32
 
33
33
 
34
+ def _ceil_div_rem(d: int, n: int) -> tuple[int, int]:
35
+ """Exact (a, a*n - d) with a = ceil(d / n), avoiding O(N^2) Python division."""
36
+ bl_n = n.bit_length()
37
+ q_bits = max(0, d.bit_length() - bl_n)
38
+ shift = bl_n - (q_bits + 64)
39
+ if shift > 256 and type(d) is int:
40
+ a = (d >> shift) // (n >> shift)
41
+ prod = n * a
42
+ if prod < d:
43
+ while prod < d:
44
+ a += 1
45
+ prod += n
46
+ else:
47
+ while prod - n >= d:
48
+ a -= 1
49
+ prod -= n
50
+ return a, prod - d
51
+ q, r = divmod(d, n)
52
+ if r == 0:
53
+ return q, 0
54
+ return q + 1, n - r
55
+
56
+
57
+ def _floor_div_rem(d: int, n: int) -> tuple[int, int]:
58
+ """Exact (b, d - b*n) with b = floor(d / n), avoiding O(N^2) Python division."""
59
+ bl_n = n.bit_length()
60
+ q_bits = max(0, d.bit_length() - bl_n)
61
+ shift = bl_n - (q_bits + 64)
62
+ if shift > 256 and type(d) is int:
63
+ b = (d >> shift) // (n >> shift)
64
+ prod = n * b
65
+ if prod > d:
66
+ while prod > d:
67
+ b -= 1
68
+ prod -= n
69
+ else:
70
+ while prod + n <= d:
71
+ b += 1
72
+ prod += n
73
+ return b, d - prod
74
+ return divmod(d, n)
75
+
76
+
34
77
  def engel(n: int, d: int, max_terms: int = 10**6, guard: int = 40) -> list[int]:
35
78
  budget = _digits(d) - guard
36
79
  out, used = [], 0.0
37
80
  for _ in range(max_terms):
38
81
  if n <= 0:
39
82
  break
40
- a = -(-d // n)
83
+ a, n_next = _ceil_div_rem(d, n)
41
84
  used += _digits(a) if a > 1 else 0.0
42
85
  if used > budget:
43
86
  break
44
87
  out.append(a)
45
- n = n * a - d
88
+ n = n_next
46
89
  return out
47
90
 
48
91
 
@@ -54,12 +97,12 @@ def pierce(n: int, d: int, max_terms: int = 10**6, guard: int = 40) -> list[int]
54
97
  for _ in range(max_terms):
55
98
  if n <= 0:
56
99
  break
57
- b = d // n
100
+ b, n_next = _floor_div_rem(d, n)
58
101
  used += _digits(b) if b > 1 else LOG10_2
59
102
  if used > budget:
60
103
  break
61
104
  out.append(b)
62
- n = d - b * n
105
+ n = n_next
63
106
  return out
64
107
 
65
108
 
@@ -125,12 +168,12 @@ def luroth(n: int, d: int, max_terms: int = 10**6, guard: int = 40) -> list[int]
125
168
  for _ in range(max_terms):
126
169
  if n <= 0:
127
170
  break
128
- a = -(-d // n)
171
+ a, rem = _ceil_div_rem(d, n)
129
172
  used += _digits(a) + (_digits(a - 1) if a > 2 else 0.0)
130
173
  if used > budget:
131
174
  break
132
175
  out.append(a)
133
- n = (a - 1) * (a * n - d)
176
+ n = (a - 1) * rem
134
177
  return out
135
178
 
136
179
 
@@ -21,6 +21,8 @@ def residues(n_terms: int, base: int = 10) -> tuple[list[int], list[int], list[i
21
21
 
22
22
  def legendre(a: int, p: int) -> int:
23
23
  """Legendre symbol (a/p) for an odd prime p, by Euler's criterion."""
24
+ if p < 3 or p % 2 == 0 or not _is_prime_mr(p):
25
+ raise ValueError(f"p must be an odd prime, got {p}")
24
26
  t = pow(a % p, (p - 1) // 2, p)
25
27
  return -1 if t == p - 1 else t
26
28
 
@@ -107,6 +109,10 @@ def _prime_factors(n: int) -> set[int]:
107
109
 
108
110
  def multiplicative_order(a: int, p: int) -> int:
109
111
  """Order of a modulo the prime p (a not divisible by p), using Pollard's rho."""
112
+ if p < 2 or not _is_prime_mr(p):
113
+ raise ValueError(f"p must be prime, got {p}")
114
+ if a % p == 0:
115
+ raise ValueError(f"a={a} is divisible by p={p}")
110
116
  m = p - 1
111
117
  for f in _prime_factors(m):
112
118
  while m % f == 0 and pow(a, m // f, p) == 1:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ceexp
3
- Version: 0.3.4
3
+ Version: 0.3.6
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
@@ -34,7 +34,7 @@ Certified computations, proofs and reproducible experiments on classical unit-fr
34
34
  constant** `C = 0.235711131719232931374143…` (OEIS [A033308](https://oeis.org/A033308)) and
35
35
  base-$b$ Champernowne constants.
36
36
 
37
- **Author:** Nicolás León Parra · **Version:** v0.3.4 · **Code:** MIT · **Text and data:** CC BY 4.0
37
+ **Author:** Nicolás León Parra · **Version:** v0.3.6 · **Code:** MIT · **Text and data:** CC BY 4.0
38
38
 
39
39
  ## Quickstart
40
40
 
@@ -54,6 +54,18 @@ class TestCLI(unittest.TestCase):
54
54
  self.assertEqual(rc, 0)
55
55
  self.assertIn("ord_100000000000031(10) = 50000000000015", buf.getvalue())
56
56
 
57
+ def test_bench_subcommand_with_memory_flag(self) -> None:
58
+ with tempfile.TemporaryDirectory() as tmp:
59
+ out_json = Path(tmp) / "bench.json"
60
+ buf = io.StringIO()
61
+ with redirect_stdout(buf):
62
+ rc = cli.main(["bench", "1000", "--memory", "--output", str(out_json)])
63
+ self.assertEqual(rc, 0)
64
+ self.assertTrue(out_json.exists())
65
+ out = buf.getvalue()
66
+ self.assertIn("backend=", out)
67
+ self.assertIn("KB)", out)
68
+
57
69
 
58
70
  if __name__ == "__main__":
59
71
  unittest.main()
@@ -98,13 +98,30 @@ class TestProperties(unittest.TestCase):
98
98
  self.assertAlmostEqual(float(term.u_exact), term.u, places=8)
99
99
 
100
100
  def test_multiplicative_order_large_primes_exact_minimality(self) -> None:
101
- for p in (1_000_000_000_039, 100_000_000_000_031):
101
+ # 60_013_200_343 - 1 = 2 * 3 * 100003 * 100019 (forces Pollard's rho split).
102
+ for p in (60_013_200_343, 1_000_000_000_039, 100_000_000_000_031):
102
103
  ord_10 = ceexp.multiplicative_order(10, p)
103
104
  self.assertEqual((p - 1) % ord_10, 0)
104
105
  self.assertEqual(pow(10, ord_10, p), 1)
105
106
  for q in _prime_factors(ord_10):
106
107
  self.assertNotEqual(pow(10, ord_10 // q, p), 1)
107
108
 
109
+ def test_input_validation_rejects_invalid_domains(self) -> None:
110
+ with self.assertRaises(ValueError):
111
+ ceexp.digits_in_base(5, base=1)
112
+ with self.assertRaises(ValueError):
113
+ ceexp.digits_in_base(0, base=10)
114
+ with self.assertRaises(ValueError):
115
+ ceexp.prime_blockwise_series(5, base=1)
116
+ with self.assertRaises(ValueError):
117
+ ceexp.multiplicative_order(10, 10)
118
+ with self.assertRaises(ValueError):
119
+ ceexp.multiplicative_order(2, 15)
120
+ with self.assertRaises(ValueError):
121
+ ceexp.legendre(10, 2)
122
+ with self.assertRaises(ValueError):
123
+ ceexp.legendre(10, 15)
124
+
108
125
 
109
126
  if __name__ == "__main__":
110
127
  unittest.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