Skip to content

⚙️ Core: Refactor Newton-Raphson Solver#443

Draft
fderuiter wants to merge 3 commits intomainfrom
core-newton-raphson-refactor-18224543845005302773
Draft

⚙️ Core: Refactor Newton-Raphson Solver#443
fderuiter wants to merge 3 commits intomainfrom
core-newton-raphson-refactor-18224543845005302773

Conversation

@fderuiter
Copy link
Owner

🔧 The Violation:
The calculate_final_size function in math_explorer/src/epidemiology/analytics.rs contained a hardcoded implementation of the Newton-Raphson method, violating DRY (logic duplication of a standard algorithm) and SRP (mixing domain logic with numerical solving). It also used string-based error handling.

🛠️ The Fix:

  1. Extracted Logic: Implemented a robust NewtonRaphson solver in math_explorer/src/pure_math/analysis/roots.rs. This struct implements the existing RootFinder trait (using numerical differentiation) and exposes a specialized find_root_with_derivative method for high-precision use cases.
  2. Composability: The solver supports optional bounds handling (with backtracking) and configurable iterations/tolerance.
  3. Refactoring: Updated calculate_final_size to use the new NewtonRaphson component, injecting the analytical derivative of the final size equation.
  4. Error Handling: Enhanced EpidemiologyError to wrap AnalysisError, moving away from stringly-typed errors.

📉 Debt Reduction:

  • Removed ad-hoc solver loop from epidemiology domain code.
  • Centralized root-finding logic in the analysis module.
  • Improved error safety with typed errors.

🧪 Integrity:

  • Added unit tests for NewtonRaphson covering linear, quadratic, and bounded scenarios.
  • Verified test_final_size_high_r0 passes with the new implementation.

PR created automatically by Jules for task 18224543845005302773 started by @fderuiter

- Implemented `NewtonRaphson` struct in `pure_math::analysis::roots`.
- Implemented `RootFinder` trait for `NewtonRaphson` with numerical differentiation.
- Added `find_root_with_derivative` method to `NewtonRaphson` for analytical derivatives.
- Refactored `epidemiology::analytics::calculate_final_size` to use `NewtonRaphson`.
- Updated `EpidemiologyError` to include `AnalysisError`.
- Added unit tests for `NewtonRaphson` and verified existing tests.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
@google-labs-jules
Copy link
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

fderuiter and others added 2 commits February 7, 2026 18:40
Combined multiline `find_root_with_derivative` call into a single line to satisfy `rustfmt`.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
Fixed `clippy::doc_overindented_list_items` violation by reducing indentation of continuation line in `find_root_with_derivative` doc comment.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant