Skip to main content

A clean, robust, and easy-to-use Python package for finding roots of equations using multiple numerical algorithms.

Project description

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)

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyrootfinder-0.1.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyrootfinder-0.1.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file pyrootfinder-0.1.0.tar.gz.

File metadata

  • Download URL: pyrootfinder-0.1.0.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for pyrootfinder-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ab0ac234a091bf855712b8bb977b8b18798a680e2a74852d25d56a346b97d583
MD5 2eccd7d7343930febcbbc5244d739de0
BLAKE2b-256 64c74b6ae2dee17f80c8213175b0cbef553e352a10db68fb7b2c5c85c198e449

See more details on using hashes here.

File details

Details for the file pyrootfinder-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pyrootfinder-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for pyrootfinder-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9100fb6232dd5451553d7ae29254ccc4a3a4e610a6d0010fb9e9d96c1314d4e8
MD5 f7b5ef86b8244db9993c193f931e75e2
BLAKE2b-256 f6c8069df9bb2eb79331e14b2818396f5c4fcb75aff4fc09a91ce18fe5f1066c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page