Skip to main content

A fast and robust root-finding library written in C for Python, using the Modified Anderson-Bjork method: Ganchovski, N.; Smith, O.; Rackauckas, C.; Tomov, L.; Traykov, A. Improvements to the Modified Anderson–Björck(modAB) Root-Finding Algorithm. Algorithms 2026, 19, 332. https://doi.org/10.3390/a19050332 It finds the root of a single nonlinear equation f(x) = 0 within the specified interval [x1, x2].

Installation

pip install pymodab

Usage

import math
from pymodab import find_root, get_evaluation_count

# Find the root of cos(x) - x = 0 in [0, 1]
root = find_root(lambda x: math.cos(x) - x, 0, 1, 1e-3, 1e-3, 10)
print(f"Root: {root}")  # 0.7390851332086904

# Get the number of function evaluations
print(f"Evaluations: {get_evaluation_count()}")
print(f"Error:       {math.cos(root) - root}")

# Using default tolerances
root = find_root(lambda x: x**2 - 2, 1, 2)
print(f"sqrt(2) = {root}")  # 1.414213562373095

# Get the number of function evaluations
print(f"Evaluations: {get_evaluation_count()}")
print(f"Error:       {root**2 - 2}")

API

find_root(f, x1, x2, atol=1e-14, rtol=1e-14, max_iter=200)

Find the root of f(x) = 0 within the interval [x1, x2].

Parameters:

  • f: A continuous function of one variable, or the address (int) of a compiled C function double f(double)
  • x1, x2: Bracket interval endpoints (must satisfy f(x1) * f(x2) < 0)
  • atol: Absolute tolerance (default: 1e-14)
  • rtol: Relative tolerance (default: 1e-14)
  • max_iter: Maximum iterations (default: 200)

Returns: The root, or NaN if not found. Exceptions raised by f are propagated to the caller. This holds for the native extension only: with the ctypes fallback (pymodab.NATIVE is False), an exception in f is only printed to stderr, the search goes on with an undefined value of f(x), and the result cannot be trusted; it may be NaN or a wrong root.

find_root is a native CPython extension that calls f directly. Wheels are provided for Windows, Linux (x86_64 and aarch64, glibc and musl) and macOS (Intel and Apple Silicon); one wheel serves every CPython from 3.8 up. On any other platform pymodab falls back to a ctypes wrapper automatically; pymodab.NATIVE tells which one is used.

For maximum speed, pass a compiled function, e.g. from numba. It is then called from C with no Python overhead:

import math
from numba import cfunc
from pymodab import find_root

@cfunc("float64(float64)")
def f(x):
    return math.cos(x) - x

root = find_root(f.address, 0, 1)

get_evaluation_count()

Returns the number of function evaluations from the last root-finding call.

Version history

Version 1.2.0 is the rational modab (RModAB). It changes the secant steps in two ways. First, a rational correction: the secant step through the true residuals is corrected for the curvature of the function by the root of the 3-point model f(x) = (x - r)/(p·x + q) through the bracket and the endpoint it dropped last. The correction is capped at half the distance from the secant point to the nearer endpoint, and it is skipped, in favour of the Anderson-Björck secant, when the curvature changes sign within the bracket or the model is degenerate. The step takes two divisions, the same as the secant step, and the curvature test none, so a step costs little more than before. Second, a closing step: when the secant steps converge from one side, the next point is kept at least half the tolerance away from the endpoint just evaluated, so it crosses the root and closes the bracket. On the benchmark, 55 problems need fewer evaluations (39 one fewer, 15 two fewer, f35 three fewer), f37 and f80 need one more each, and the median drops from 13 to 11. All 101 need 1881 evaluations in total instead of 1951. In the timing benchmark below, pymodab is now 1.36 times as fast as cybrentq (1.29 with 1.1.4) and 10.6 times as fast as brentq (10.4).

Earlier versions:

  • 1.1.4 – Each fallback to bisection halves the margin of the switching test, and a switching bisection on a plateau does not count as a side move; 1951 evaluations on the benchmark.
  • 1.1.3 – A slow secant step is kept only if it cuts the residual more than 42 times (2^(1074/200) ≈ 41.4), replacing the step cap of 1.1.1; f101 needs 65 evaluations instead of 76.
  • 1.1.2 – Streamlined solver loop with the same roots and evaluation counts.
  • 1.1.1 – Separate bisection and Anderson-Björck steps, and a cap on slow secant steps, which fixes L. Tomov's counterexample, now problem f101.
  • 1.1.0 – Simpler overflow-safe switching test; the bracket is updated from the true residuals.
  • 1.0.10 – Overflow and NaN safeguards: the midpoint and secant point stay finite and inside the bracket, and a NaN from f stops the search.
  • 1.0.8 / 1.0.9 – A slow secant step is kept if it halves the residual; get_evaluation_count() counts every call to f.
  • 1.0.6 / 1.0.7 – Native CPython extension, about 2x faster than the ctypes wrapper, with wheels for all major platforms.

Algorithm

Modified Anderson-Björck's method is a new robust and efficient bracketing root-finding algorithm. It combines bisection with Anderson-Björk's method to achieve both fast performance and worst-case optimality.

References:

Ganchovski N.; Traykov A. Modified Anderson-Björck's method for solving non-linear equations in structural mechanics. IOP Conference Series: Materials Science and Engineering 2023, 1276 (1) 012010, IOP Publishing.
https://iopscience.iop.org/article/10.1088/1757-899X/1276/1/012010/pdf

Ganchovski, N.; Smith, O.; Rackauckas, C.; Tomov, L.; Traykov, A. Improvements to the Modified Anderson–Björck (modAB) Root-Finding Algorithm. Algorithms 2026, 19, 332. https://doi.org/10.3390/a19050332

License

MIT License

Benchmark results

The modAB algorithm is benchmarked against the available algorithms in Python/SciPy in respect to number of evaluations and execution times:

  • bisect- Bisection method
  • brentq - Brent’s method (van Wijngaarden–Dekker–Brent, 1973)
  • brenth - Brent–Dekker variant (hyperbolic extrapolation variant, 1975)
  • ridder - Ridder’s method (1979)
  • toms748 - Alefeld–Potra–Shi method (1995 - TOMS Algorithm 748)
  • chandr - Chandrupatla's method (1997) - scipy.optimize.elementwise.find_root
  • cybrentq - Cython implementation of brentq by Gledis Caushaj
  • RModAB - Rational Modified Anderson Bjork's method (Ganchovski & Traykov, 2023; improved 2026 by L.Tomov and N.Ganchovski; rational correction and closing step added in November 2026)
  • modAB_ct - the old version of pymodab 1.0.5 implemented with ctypes

Function evaluations

Func bisect brentq brenth ridder chandr cybrentq modAB_ct RModAB
SUM 4883 2810 2769 3425 2112 2810 1993 1881
AVG 48 28 27 34 21 28 20 19
MEDIAN 49 13 12 16 12 13 13 11
MIN 3 4 4 3 3 4 3 3
MAX 53 102 102 202 70 102 78 56
FACTOR 2.596x 1.494x 1.472x 1.821x 1.123x 1.494x 1.060x 1.000x

Execution times (ms per problem, 200 iterations)

Func bisect brentq brenth ridder chandr cybrentq modAB_ct RModAB
SUM 1458.11 901.30 891.77 1006.99 57970.14 115.41 174.74 85.14
AVG 14.4367 8.9238 8.8294 9.9702 573.9617 1.1426 1.7301 0.8430
MEDIAN 14.5688 4.8678 4.6496 5.7422 344.0286 0.6270 1.3687 0.6272
MIN 1.6549 1.8202 2.0263 1.7382 76.1830 0.2046 0.6398 0.1824
MAX 19.6980 33.9563 60.6919 60.1620 1910.0981 5.6508 5.5485 2.8541
FACTOR 17.126x 10.586x 10.474x 11.828x 680.890x 1.356x 2.052x 1.000x

Notes:

Last Run on: 08.11.2026
Intel(R) Core(TM) i7-1065G7 CPU @ 1.30GHz (1.50 GHz) with 16.0 GB RAM
Windows 11 Home
Python Version: 3.14.7
numpy Version: 2.4.6
scipy Version: 1.18.0
cybrentq Version: 0.1.5 - by Gledis Caushaj (https://github.com/gledi-ai/cybrentq)
modAB_ct: pymodab version 1.0.5 from PyPI - the previous implementation with ctypes
RModAB: pymodab version 1.2.0 - the latest implementation, rational modab (RModAB), as native C extension

Metadata

Release files for pymodab 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pymodab 1.2.0
File Size Uploaded Provenance Actions
pymodab-1.2.0.tar.gz 127.4 kB Without provenance Details for pymodab-1.2.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for pymodab 1.2.0
File Actions
pymodab-1.2.0-py3-none-any.whl Python 3 none any Details for pymodab-1.2.0-py3-none-any.whl
pymodab-1.2.0-cp38-abi3-win_amd64.whl CPython 3.8 abi3 Windows x86-64 Details for pymodab-1.2.0-cp38-abi3-win_amd64.whl
pymodab-1.2.0-cp38-abi3-musllinux_1_2_x86_64.whl CPython 3.8 abi3 Linux musl 1.2+ x86-64 Details for pymodab-1.2.0-cp38-abi3-musllinux_1_2_x86_64.whl
pymodab-1.2.0-cp38-abi3-musllinux_1_2_aarch64.whl CPython 3.8 abi3 Linux musl 1.2+ ARM64 Details for pymodab-1.2.0-cp38-abi3-musllinux_1_2_aarch64.whl
pymodab-1.2.0-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl CPython 3.8 abi3 Linux glibc 2.28+ ARM64, Linux glibc 2.17+ ARM64 Details for pymodab-1.2.0-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
pymodab-1.2.0-cp38-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl CPython 3.8 abi3 Linux glibc 2.5+ x86-64, Linux glibc 2.28+ x86-64 Details for pymodab-1.2.0-cp38-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
pymodab-1.2.0-cp38-abi3-macosx_10_9_universal2.whl CPython 3.8 abi3 macOS 10.9+ universal2 (ARM64, x86-64) Details for pymodab-1.2.0-cp38-abi3-macosx_10_9_universal2.whl

Total release size: 376.4 kB

Release files / pymodab-1.2.0.tar.gz

Download URL pymodab-1.2.0.tar.gz
Size 127.4 kB
Tags Source
SHA-256 checksum
How to use checksums
30dd49406d0821eb71c610f809dca0b08f16709dc92eb6b670ef1dcb92ddb992
BLAKE2b-256 checksum
How to use checksums
6216ab10a2194adde399a5559a67a7390afd17407aac80cb50636820b47f92a1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.7

Release files / pymodab-1.2.0-py3-none-any.whl

Download URL pymodab-1.2.0-py3-none-any.whl
Size 115.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
afbd48c9c069ea28b112f2ee1ae52a57b8aa79f6806ae1479de5b0fa5156b9f3
BLAKE2b-256 checksum
How to use checksums
6aa0f0eecf2e8b774b9ca6cc6c09a7f5ce45c6db7d93c31eaeb067b8f589a20c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.7

Release files / pymodab-1.2.0-cp38-abi3-win_amd64.whl

Download URL pymodab-1.2.0-cp38-abi3-win_amd64.whl
Size 15.8 kB
Tags CPython 3.8 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
9541f8b4f957cd62c89721bacd37367acdfa8a87d47288ff215fc8c6881ce1b7
BLAKE2b-256 checksum
How to use checksums
1170800bfd19e05bde4aca983cbe228b4937cc5f417a8d424a458067e35aad94
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.7

Release files / pymodab-1.2.0-cp38-abi3-musllinux_1_2_x86_64.whl

Download URL pymodab-1.2.0-cp38-abi3-musllinux_1_2_x86_64.whl
Size 25.2 kB
Tags CPython 3.8 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
665fe2ee7c8f0ab2aee4dd817cfddd25be0f5a123cb2564e001bfa34ab5743f4
BLAKE2b-256 checksum
How to use checksums
3e94a0578b50e20b866728fe292e47951eba86145c2483780187805213ac8497
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.7

Release files / pymodab-1.2.0-cp38-abi3-musllinux_1_2_aarch64.whl

Download URL pymodab-1.2.0-cp38-abi3-musllinux_1_2_aarch64.whl
Size 25.4 kB
Tags CPython 3.8 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
fe7e30b544e99b9f0ae61cdde94c53407f8ebc3fd7f55c5876d74d50bc6f7f36
BLAKE2b-256 checksum
How to use checksums
a60b10e092dfe087990d9738200d01eb863e456a847ed654116134d3ccb660ad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.7

Release files / pymodab-1.2.0-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL pymodab-1.2.0-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Size 25.5 kB
Tags CPython 3.8 Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
f0b9b0b011eb61dfa2e4c8e8603efdee479bcfc915319b1da908456bbd223dd8
BLAKE2b-256 checksum
How to use checksums
094ce05c5939f1f2a3c0bd87ec2a67a24982a6bb38a94fe975723285dbe6761f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.7

Release files / pymodab-1.2.0-cp38-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl

Download URL pymodab-1.2.0-cp38-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Size 24.6 kB
Tags CPython 3.8 Linux glibc 2.28+ x86-64 Linux glibc 2.5+ x86-64 abi3
SHA-256 checksum
How to use checksums
988abc470401c286b89acf41cf2c2737c87e81bc95210537d57710216c883cc7
BLAKE2b-256 checksum
How to use checksums
a68584bfabf7af2c6abf9aaeebaf62bd08d3234595d82b2e83e6155ff972f4a4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.7

Release files / pymodab-1.2.0-cp38-abi3-macosx_10_9_universal2.whl

Download URL pymodab-1.2.0-cp38-abi3-macosx_10_9_universal2.whl
Size 17.3 kB
Tags CPython 3.8 abi3 macOS 10.9+ universal2 (ARM64, x86-64)
SHA-256 checksum
How to use checksums
11493b77edad477ce87a71a3e318108834ef02b6e0e378c48c120ee19124f87c
BLAKE2b-256 checksum
How to use checksums
50c1553b9c93e8e32655fa1e881c5e25116e605761044da85f75a996464de55f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.7

Security for pymodab 1.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page