Skip to content

Add Brent's Method for root finding (numerical analysis) - #13063

Merged
cclauss merged 8 commits into
TheAlgorithms:masterfrom
debesh00800:brent_method
Sep 8, 2026
Merged

cclauss merged 8 commits into
TheAlgorithms:masterfrom
debesh00800:brent_method

Conversation

@debesh00800

Copy link
Copy Markdown
Contributor

Description:
This PR adds Brent’s Method for root finding, a robust numerical algorithm combining bisection, secant, and inverse quadratic interpolation. The implementation includes:

  • Type hints for all parameters and return values
  • Docstrings with examples and error handling
  • Doctests that pass local testing
  • Error handling when the interval endpoints do not bracket a root

References:

Fixes #13047

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist:

  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

@algorithms-keeper algorithms-keeper Bot added the require descriptive names This PR needs descriptive function and/or variable names label Oct 1, 2025

@algorithms-keeper algorithms-keeper Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

Comment thread maths/brent_method.py Outdated
Comment thread maths/brent_method.py Outdated
Comment thread maths/brent_method.py Outdated
@algorithms-keeper algorithms-keeper Bot added the awaiting reviews This PR is ready to be reviewed label Oct 1, 2025
@algorithms-keeper algorithms-keeper Bot added the tests are failing Do not merge until tests pass label Oct 1, 2025
@algorithms-keeper algorithms-keeper Bot removed the require descriptive names This PR needs descriptive function and/or variable names label Oct 1, 2025
@algorithms-keeper algorithms-keeper Bot added tests are failing Do not merge until tests pass and removed tests are failing Do not merge until tests pass labels Oct 1, 2025
@debesh00800

Copy link
Copy Markdown
Contributor Author

Hi @mindaugl , can the PR be merged or are there any reviews pending?

@priya-sundaram-dev priya-sundaram-dev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed as part of the maths/ triage (#15081). This is a genuinely new algorithm — the repo has bisection, secant_method, and newton_raphson under maths/numerical_analysis/ but no Brent's method, so it fills a real gap. CI is green (build, ruff, pre-commit, docs) and I verified it converges: the two doctests pass, plus a battery of 2,383 random bracketed cubics and stress cases (flat cubic root (x-0.5)**3, x**5-0.3, transcendental cos(x)-x, sin(x) near π) all return roots with residual < 1e-6, 0 failures.

Two things before merge:

  1. Placement (needs change): every other root-finder lives in maths/numerical_analysis/ (bisection.py, secant_method.py, newton_raphson.py). Please move this to maths/numerical_analysis/brent_method.py so it sits with its siblings and DIRECTORY.md groups it correctly.

  2. Robustness note (optional, non-blocking): the classic algorithm tracks a boolean mflag (was the previous step a bisection?) to decide when to accept an interpolation step. This PR uses iteration <= 1 / iteration > 1 as a proxy instead. Bracketing is still preserved on every step by the if fl * fs < 0 update, so correctness/termination hold (confirmed by the fuzz run), but the iteration-index proxy can accept an interpolation step the real mflag would reject, which only costs a few extra iterations in adversarial cases. Worth a comment noting the deviation, or switching to a real mflag if you want the textbook behavior — either is fine.

Verdict: ready to merge once moved into numerical_analysis/. Nice, well-documented contribution.

@priya-sundaram-dev priya-sundaram-dev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed as part of the maths/ awaiting-reviews triage (#15081).

Verdict: genuinely new + correct — recommend merge after one relocation.

Not a duplicate. The repo's bracketing/root-finders all live in maths/numerical_analysis/ (bisection.py, bisection_2.py, newton_raphson.py, secant_method.py). There's no Brent's method today, and it's a worthwhile addition — it's the standard "combine bisection + secant + inverse-quadratic-interpolation" method that scipy.optimize.brentq implements.

Correctness — verified empirically. I ran the implementation against scipy.optimize.brentq:

  • All doctest/manual cases converge exactly (x³−x−2 → 1.52138, cos(x)−x → 0.73909, x²−612, eˣ−5, etc.).
  • 2271 random bracketed polynomial roots (degree 2–6): 0 failures vs brentq (|f(root)| < 1e-4).

One change requested before merge:

  1. Move the file to maths/numerical_analysis/brent_method.py so it sits with the other root-finders instead of at the maths/ root. (DIRECTORY.md will regenerate.)

Two optional polish notes (non-blocking):
2. The mflag safeguard in canonical Brent tracks whether the previous step was a bisection; here it's approximated by iteration <= 1 / iteration > 1. Empirically it still converges on every case I threw at it, but a true boolean mflag (set when the bisection fallback fires, cleared otherwise) matches the reference algorithm exactly and is easier for readers to verify against Wikipedia/Numerical Recipes.
3. d, c = c, right reassigns d before it's read in the next iteration's abs(c - d) branch — the logic works out, but a one-line comment on what d (the second-previous iterate) represents would help future readers.

Nice, well-documented contribution — the parameter docstring and the sign-check ValueError doctest are exactly the style we want.

@algorithms-keeper algorithms-keeper Bot removed the awaiting reviews This PR is ready to be reviewed label Sep 8, 2026
@cclauss
cclauss merged commit 16e3b40 into TheAlgorithms:master Sep 8, 2026
6 checks passed
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.

Add Brent’s Method for root finding (Numerical Analysis)

4 participants