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.

Since version 1.0.6, find_root is a native CPython extension that calls f directly, which is about 2x faster than the previous ctypes wrapper. Version 1.0.7 ships it as a wheel 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 ctypes automatically; pymodab.NATIVE tells which one is used.

Version 1.0.8 improves the fallback from secant to bisection steps: a secant step that does not shrink the bracket fast enough is still kept if it at least halves the best residual of the bracket. This avoids needless bisection steps when the secant converges from one side, and falls back sooner when it stalls. get_evaluation_count() now returns the exact number of calls to f. Version 1.0.9 ships that work as wheels for every supported platform.

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.

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
  • modAB - Modified Anderson Bjork's method (Ganchovski & Traykov, 2023; improved 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 modAB
SUM 4832 2733 2693 3357 2042 2733 1915 1897
AVG 48 27 27 34 20 27 19 19
MEDIAN 49 13 12 16 12 13 12 12
MIN 3 4 4 3 3 4 3 3
MAX 53 102 102 202 58 102 55 55
FACTOR 2.547x 1.441x 1.420x 1.770x 1.076x 1.441x 1.009x 1.000x

Execution times (ms per problem, 200 iterations)

Func bisect brentq brenth ridder chandr cybrentq modAB_ct modAB
SUM 1354.57 809.24 790.70 947.48 53762.45 110.12 163.98 85.69
AVG 13.5457 8.0924 7.9070 9.4748 537.6245 1.1012 1.6398 0.8569
MEDIAN 13.5512 4.3220 4.0672 5.2287 334.8545 0.6377 1.3381 0.6569
MIN 1.3982 1.6644 1.8143 1.4678 65.1690 0.2064 0.5470 0.1606
MAX 24.3726 30.9711 34.3799 53.6214 1545.0283 5.6255 4.8475 2.8676
FACTOR 15.807x 9.443x 9.227x 11.057x 627.384x 1.285x 1.914x 1.000x

Notes:

Last Run on: 20.09.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
modAB: pymodab version 1.0.9 - the latest implementation as native C extension

Metadata

Release files for pymodab 1.0.9

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.0.9
File Size Uploaded Provenance Actions
pymodab-1.0.9.tar.gz 115.4 kB Without provenance Details for pymodab-1.0.9.tar.gz

Built distributions (wheels)

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

Total release size: 330.0 kB

Release files / pymodab-1.0.9.tar.gz

Download URL pymodab-1.0.9.tar.gz
Size 115.4 kB
Tags Source
SHA-256 checksum
How to use checksums
8f9e1d4fbabc58756cda67d700746dcb8ee3ddfe3c3c8e4e1ba19be280f9ab55
BLAKE2b-256 checksum
How to use checksums
20fe2f3c131d83e8702673e836c397790a412a04dc1be8fa5f86462d45206e38
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.0.9-py3-none-any.whl

Download URL pymodab-1.0.9-py3-none-any.whl
Size 107.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4fae7c9ad0eb29616401561e7861a3deeb4fb76a57c6984032fefe38b9a8098d
BLAKE2b-256 checksum
How to use checksums
88d842d02bccd487efc2d74cc0af311d6a663cc989e2638307aeb12259cebcae
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.0.9-cp38-abi3-win_amd64.whl

Download URL pymodab-1.0.9-cp38-abi3-win_amd64.whl
Size 14.1 kB
Tags CPython 3.8 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
121369751992d583b1eb7efcb9d9cb0b2e6e89c7ce27f4aba298c384c1d2a29a
BLAKE2b-256 checksum
How to use checksums
96be6e77ac65e18a875b3898d903580af7dedc78d339ab64ad06df621406bd9f
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.0.9-cp38-abi3-musllinux_1_2_x86_64.whl

Download URL pymodab-1.0.9-cp38-abi3-musllinux_1_2_x86_64.whl
Size 19.0 kB
Tags CPython 3.8 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
faf9f6cfb2e1cd7328be1c10aebe621d19e4773689d4c52fd24be1876adfc630
BLAKE2b-256 checksum
How to use checksums
065ce20c2f420363aca78163ce40e8b8473f4fb74dd219ed04abc7476e8ce347
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.0.9-cp38-abi3-musllinux_1_2_aarch64.whl

Download URL pymodab-1.0.9-cp38-abi3-musllinux_1_2_aarch64.whl
Size 19.3 kB
Tags CPython 3.8 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
9badde81c979eab144742d65b65d4f24f5c5b22cafa390e1ce800b382b0f8a52
BLAKE2b-256 checksum
How to use checksums
b5dd3bde67922843c686f4287c3d5c938076e8f1167e49669b61a28850bb1fba
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.0.9-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL pymodab-1.0.9-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Size 19.7 kB
Tags CPython 3.8 Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
354965ab94343e42e6a561650794d83d8de53f95b1430cf3831f5e0b3ce99614
BLAKE2b-256 checksum
How to use checksums
7892e702d1df467f9d3c60fb290551daf4d34248a050a1acba107fcee3717d24
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.0.9-cp38-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl

Download URL pymodab-1.0.9-cp38-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Size 19.0 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
ee23ef23611282d69d7b219606d793ef8568ae9d870cd8fdf78912ba5b037499
BLAKE2b-256 checksum
How to use checksums
e0481ef9df9d34e6d1577f3c2cad46b037dfb431f1abe5371265ccb19a8dd6cf
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.0.9-cp38-abi3-macosx_10_9_universal2.whl

Download URL pymodab-1.0.9-cp38-abi3-macosx_10_9_universal2.whl
Size 15.8 kB
Tags CPython 3.8 abi3 macOS 10.9+ universal2 (ARM64, x86-64)
SHA-256 checksum
How to use checksums
63599f24af360e74ebf719c0c3bb48de9101e5a2362e5ceaf7b4f2c469a274ed
BLAKE2b-256 checksum
How to use checksums
9dc0918213a8896eabf1e26eba111a57fdb85947cebaf1fd2771f4f77442a529
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.0.9

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