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.1 refines the rational step of 1.2.0. The 3-point model f(x) = (x - r)/(p·x + q) now goes through the Anderson-Björck-corrected bracket instead of the true residuals, with the true residual of the dropped endpoint as its third point. A step that would move the Anderson-Björck secant point by more than 0.7 of its distance to the nearer endpoint is no longer replaced by the secant point but clamped to that limit, on the side the model points to. The step still takes two divisions. On the benchmark, 11 problems need fewer evaluations (7 one fewer, f48 and f76 two fewer, f41 three fewer, f80 four fewer) and 4 need more (f13 and f15 one more, f85 and f100 three more), so all 101 need 1871 evaluations instead of 1881. On 200,000 random functions from ten families the new step saves 1.1 % of the evaluations, with no failures. In the timing benchmark below, pymodab is 1.34 times as fast as cybrentq and 9.9 times as fast as brentq.

Earlier versions:

  • 1.2.0 – The rational modab (RModAB): a rational correction bends the secant step to follow the curvature of the function, and a closing step keeps each new point at least half the tolerance away from the endpoint just evaluated; 1881 evaluations on the benchmark.
  • 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 1871
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.610x 1.502x 1.480x 1.831x 1.129x 1.502x 1.065x 1.000x

Execution times (ms per problem, 200 iterations)

Func bisect brentq brenth ridder chandr cybrentq modAB_ct RModAB
SUM 1480.45 935.32 887.77 1023.61 57058.75 113.52 172.83 84.39
AVG 14.6579 9.2606 8.7898 10.1348 564.9381 1.1240 1.7112 0.8355
MEDIAN 14.3018 5.1766 4.7206 5.7367 334.1344 0.6116 1.2809 0.6183
MIN 1.8050 1.5092 1.7821 1.5801 73.4375 0.2257 0.6471 0.1898
MAX 48.4630 42.4292 36.0671 53.3243 1928.2951 6.6491 5.1035 2.8232
FACTOR 17.543x 11.083x 10.520x 12.130x 676.144x 1.345x 2.048x 1.000x

Notes:

Last Run on: 09.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.1 - the latest implementation, rational modab (RModAB), as native C extension

Metadata

Release files for pymodab 1.2.1

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.1
File Size Uploaded Provenance Actions
pymodab-1.2.1.tar.gz 126.9 kB Without provenance Details for pymodab-1.2.1.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for pymodab 1.2.1
File Actions
pymodab-1.2.1-py3-none-any.whl Python 3 none any Details for pymodab-1.2.1-py3-none-any.whl
pymodab-1.2.1-cp38-abi3-win_amd64.whl CPython 3.8 abi3 Windows x86-64 Details for pymodab-1.2.1-cp38-abi3-win_amd64.whl
pymodab-1.2.1-cp38-abi3-musllinux_1_2_x86_64.whl CPython 3.8 abi3 Linux musl 1.2+ x86-64 Details for pymodab-1.2.1-cp38-abi3-musllinux_1_2_x86_64.whl
pymodab-1.2.1-cp38-abi3-musllinux_1_2_aarch64.whl CPython 3.8 abi3 Linux musl 1.2+ ARM64 Details for pymodab-1.2.1-cp38-abi3-musllinux_1_2_aarch64.whl
pymodab-1.2.1-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.1-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
pymodab-1.2.1-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.1-cp38-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
pymodab-1.2.1-cp38-abi3-macosx_10_9_universal2.whl CPython 3.8 abi3 macOS 10.9+ universal2 (ARM64, x86-64) Details for pymodab-1.2.1-cp38-abi3-macosx_10_9_universal2.whl

Total release size: 370.4 kB

Release files / pymodab-1.2.1.tar.gz

Download URL pymodab-1.2.1.tar.gz
Size 126.9 kB
Tags Source
SHA-256 checksum
How to use checksums
970f8ce3c9de3d710a6a18f78d3298a18fa90373517991df01a43a2e0bd21c50
BLAKE2b-256 checksum
How to use checksums
032957c867f57e61afba692aa987d019f4c53283b5d48310cedb1fbdba1bac63
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.1-py3-none-any.whl

Download URL pymodab-1.2.1-py3-none-any.whl
Size 114.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8b5092d1840d97e1d5266d910d5501ecaac7a5cf9afedab672789eb692cd0beb
BLAKE2b-256 checksum
How to use checksums
523fd75b3782da85b2901f6452005fa45d1fefd8f8f2996955cb121d42abe437
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.1-cp38-abi3-win_amd64.whl

Download URL pymodab-1.2.1-cp38-abi3-win_amd64.whl
Size 15.8 kB
Tags CPython 3.8 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
e3a57af73a121f823e748e183f79532aa4266dad9adab09cdff5750c6798af99
BLAKE2b-256 checksum
How to use checksums
bc9888c31308d81409054d701e1dfae9faa8396199de905e5d1182433cde5166
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.1-cp38-abi3-musllinux_1_2_x86_64.whl

Download URL pymodab-1.2.1-cp38-abi3-musllinux_1_2_x86_64.whl
Size 24.0 kB
Tags CPython 3.8 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
39aca5f7bac2e2886890064358cae916c34d529496a10b7fc5ff369388d60715
BLAKE2b-256 checksum
How to use checksums
0d83de74cda4bc653af890f7d1f31723d9ce87e10e1051d522264e2b0b738b9e
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.1-cp38-abi3-musllinux_1_2_aarch64.whl

Download URL pymodab-1.2.1-cp38-abi3-musllinux_1_2_aarch64.whl
Size 24.2 kB
Tags CPython 3.8 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
7470a1b19fa51b95c54905c22f7baee38360dc1a3f231ad45794f0c7aeda2c7f
BLAKE2b-256 checksum
How to use checksums
c8c274b4590997dea62065f69f0831ed04e8392c6f31f774feaa63066a20b0f9
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.1-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL pymodab-1.2.1-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Size 24.4 kB
Tags CPython 3.8 Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
504d1a6721d0588b7ec31c0b2d2be44731911d3c243c4699872a5c0e99a8f586
BLAKE2b-256 checksum
How to use checksums
1c28728acd81bfa1b57addf35e6538413ce6ca493dbcc63164e37706ddb4cad2
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.1-cp38-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl

Download URL pymodab-1.2.1-cp38-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Size 23.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
f16dd47327a2886d5e1740fe2f79a4d8da2cd3710c346c641bd432abaf251eab
BLAKE2b-256 checksum
How to use checksums
b9fd0ce01aa3b5dacdae3603fd2bbfbd8109add5580255184019cb6208bdb810
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.1-cp38-abi3-macosx_10_9_universal2.whl

Download URL pymodab-1.2.1-cp38-abi3-macosx_10_9_universal2.whl
Size 17.2 kB
Tags CPython 3.8 abi3 macOS 10.9+ universal2 (ARM64, x86-64)
SHA-256 checksum
How to use checksums
e72d991ed43f7257d4386f8f224969bf2ee6558b93cd231586201da1f5f66df2
BLAKE2b-256 checksum
How to use checksums
215fa7f8ab2e3ecd46cfe0539dd237c8bc383c83a0de7b4e8444e860e8e88cc5
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.1

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