Repository navigation
Conversation
Replace sympy.primerange with a custom prime sieve
Pin the invariants of the first 1000 primes (count, spot values, sum) that qnwequi's Weyl and Haber sequences depend on, and the exclusive upper bound matching sympy.primerange(0, n). Co-Authored-By: Claude Fable 5 <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
The required changelog or release-note entry is missing, and the helper documentation misstates its exclusive upper bound.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Removes SymPy as a runtime dependency by replacing its prime generator with a NumPy sieve.
Changes:
- Adds
_primes_uptoand uses it inqnwequi. - Adds prime-sequence regression tests.
- Removes SymPy from package, test, and documentation environments.
File summaries
| File | Description |
|---|---|
quantecon/quad.py |
Implements and uses the prime sieve. |
quantecon/tests/test_quad.py |
Tests prime values and boundaries. |
pyproject.toml |
Removes SymPy dependencies. |
environment.yml |
Removes SymPy from development dependencies. |
environment_np2.yml |
Removes SymPy from NumPy 2 environment. |
docs/rtd-requirements.txt |
Removes SymPy from documentation dependencies. |
Review details
- Files reviewed: 6/6 changed files
- Comments generated: 2
- Review effort level: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| if equidist_pp is None: | ||
| import sympy as sym | ||
| equidist_pp = np.sqrt(np.array(list(sym.primerange(0, 7920)))) | ||
| equidist_pp = np.sqrt(_primes_upto(7920)) |
|
|
||
| def _primes_upto(n): | ||
| """ | ||
| Return all primes up to n, by the Sieve of Eratosthenes. |
There was a problem hiding this comment.
Good point. Changed the function name and the docstring.
The upper bound is exclusive; name and docstring now say so (addresses a Copilot review comment on #947). Co-Authored-By: Claude Fable 5 <[email protected]>
quantecon-services
left a comment
There was a problem hiding this comment.
Thanks @oyamad, this is a really nice, tidy change and it looks good to merge from my side. 🎉 The ~45 MB of sympy + mpmath that every pip install quantecon used to pay for one function-local call is gone, and qnwequi behaves exactly as before.
I went a bit beyond the CI run and checked the new sieve against sympy directly:
- Same primes.
_primes_below(7920)returns exactly the same 1000 primes assympy.primerange(0, 7920), and the two agree for every upper bound from 0 to 2999. - Same
qnwequioutput. The defaultequidist_pparray is bit-for-bit identical to the old one, sokind="W"andkind="H"results are unchanged. I compared 56 shape combinations, up to 1000 dimensions, all identical. Together with the existing MATLAB-reference tests for W and H (which already go through the defaultequidist_pppath) and the new invariants test, the "output unchanged" criterion in #880 is well covered. - Fresh venv is clean. A
pip installof this branch into a fresh venv pulls in nosympyormpmath;import quanteconworks andtest_quad.pypasses with sympy absent. That ticks the "fresh venv" criterion in #880 too. As a bonus, the sieve runs in about 20 µs versus roughly 1 ms forprimerange(after a 0.2 s import). - Merges cleanly onto current
main.
On the open Copilot comment asking for a changelog entry: per AGENTS.md, release notes live on GitHub Releases and per-PR changelog entries are not added, so that thread can safely be ignored. The lighter footprint can be mentioned in the next release notes instead.
Two optional nits, neither blocking:
- The
qnwequidocstring still saysequidist_pp : ... TODO: I don't know what this does. Since this PR pins down exactly what it is, a one-liner along the lines of "square roots of the first 1000 primes by default; only the firstdentries are used by the Weyl and Haber sequences" would be a nice improvement, here or in a follow-up. math.isqrtinstead ofint(n**0.5), see the inline suggestion.
A few housekeeping thoughts for after the merge:
- Keep #880 open for the
requestshalf (or split that into its own issue). - When the next release is cut, note the dropped dependency in the release notes, and remember to drop
sympyfrom the conda-forge feedstock run requirements (and the emscripten-forge recipe, if it lists it), since this PR only updates the metadata in this repo. - Nice side benefit for the JupyterLite work in #933: one fewer heavy wheel to load in the browser.
Generated by Claude Code
Co-authored-by: QuantEcon <[email protected]>
|
thanks @oyamad. Merging. |
Catches the @-operator migration up to main (#831, #937, #947, #949). Conflict in quantecon/tests/test_matrix_eqn.py: #831 appended test_solve_discrete_riccati_system_beta_ge_one_failure_message directly after the assert this branch rewrote to the @ operator. Kept both sides. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Resolve the
sympyhalf of #880:sympy.primerange(0, 7920)inqnwequiwith a custom implementation_primes_upto(Claude Code Fable 5 recommended the most primitive algorithm).sympydependency.