Skip to main content
pyrootfinder logo

Python Root Finder

PyPI version License: MIT

A clean, robust, and easy-to-use Python package for finding roots of single-variable, real-valued functions using a suite of classic numerical algorithms.

Key Features

  • Consistent API: All solvers return a standardized RootResult object.
  • Robust Error Handling: Solvers handle edge cases like convergence failure or bad inputs gracefully without crashing.
  • Clear & Informative Output: The result object reports the root, success status, iterations, and diagnostic messages.
  • Suite of Algorithms:
    • Bracketing methods: bisection, secant, brentq (recommended)
    • Derivative-based methods: newton, halley

Installation

Install using pip:

pip install pyrootfinder

Quick Start

Find the root of the function:

$$ f(x) = x^3 - x - 2 $$

import pyrootfinder as rf
import math

# define the function
f = lambda x: x**3 - x - 2

# use a bracketing method (brent's method is highly recommended)
# root lies between 1 and 2
result_brent = rf.brentq(f, a=1, b=2)
print(result_brent)

# use a derivative-based method (newton's method)
f_prime = lambda x: 3*x**2 - 1
result_newton = rf.newton(f, x0=1.5, f_prime=f_prime)
print(result_newton)

Example Output

---- Brent's (Brentq) Result (Success) ----
    Root: 1.5213797068
    Function Value: 0.000000e+00
    Iterations: 9
    Message: Convergence achieved.
-------------------------------------------

---- Newton-Raphson Result (Success) ----
    Root: 1.5213797068
    Function Value: -1.110223e-16
    Iterations: 3
    Message: Convergence achieved.
-----------------------------------------

Handling Failures

The library is designed to fail gracefully. If a root cannot be found within the given bracket or iteration limit, a clear failure message is returned.

# find a root where none exists in the given bracket
result_fail = rf.bisection(lambda x: x**2 + 1, a=-10, b=10)
print(result_fail)

Failure Output

---- Bisection Result (Failed) ----
     Message: Root not bracketed or multiple roots exist in [a, b].
     Iterations: 0
-----------------------------------

API Overview

All solvers return a RootResult object with attributes such as root, iterations, success, and message.

rf.bisection(f, a, b, tol=1e-8, max_iter=100)
rf.secant(f, a, b, tol=1e-8, max_iter=100)
rf.brentq(f, a, b, tol=1e-8, max_iter=100)
rf.newton(f, x0, f_prime, tol=1e-8, max_iter=100)
rf.halley(f, x0, f_prime, f_prime2, tol=1e-8, max_iter=100)

Hacktoberfest

This repository is participating in Hacktoberfest. Contributions are welcome from developers of all experience levels. If you're new to open source, check out issues labeled good first issue or help wanted.

To contribute:

  • Fork the repository
  • Create a new branch
  • Make your changes
  • Submit a pull request

Please read the CONTRIBUTING.md and CODE_OF_CONDUCT.md before submitting.

Community and Support

If you have questions, suggestions, or feedback:

  • Open an issue on GitHub
  • Join discussions under relevant pull requests
  • Refer to the documentation and examples in the repo

License

This project is licensed under the MIT License.

Metadata

Release files for pyrootfinder 0.1.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 pyrootfinder 0.1.1
File Size Uploaded
pyrootfinder-0.1.1.tar.gz 9.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyrootfinder 0.1.1
File Interpreter ABI Platform
pyrootfinder-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 16.9 kB

Release files / pyrootfinder-0.1.1.tar.gz

Download URL pyrootfinder-0.1.1.tar.gz
Size 9.4 kB
Tags Source
SHA-256 checksum
How to use checksums
1275a32f1b7eaa1c21306af64047f404a92998e8213678e12880db3e4fe28f95
BLAKE2b-256 checksum
How to use checksums
29c081a991167874beb2897f22b7e746cc0dcbaff8cb3c6eb00711625fffed9d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.1

Release files / pyrootfinder-0.1.1-py3-none-any.whl

Download URL pyrootfinder-0.1.1-py3-none-any.whl
Size 7.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5fa079e770abb26d059f658b09bbaccb57a51524c492b26637e2671b99f199d6
BLAKE2b-256 checksum
How to use checksums
735ca840e42b276d5fce9f736a5e34237e5b886ef1866df65efb71e76315d324
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.1

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

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