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)

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.

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.1.tar.gz (9.4 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.1-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pyrootfinder-0.1.1.tar.gz
Algorithm Hash digest
SHA256 1275a32f1b7eaa1c21306af64047f404a92998e8213678e12880db3e4fe28f95
MD5 545a0bac51ef94c2992d935e0116b3f2
BLAKE2b-256 29c081a991167874beb2897f22b7e746cc0dcbaff8cb3c6eb00711625fffed9d

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for pyrootfinder-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5fa079e770abb26d059f658b09bbaccb57a51524c492b26637e2671b99f199d6
MD5 124e68daa9346f48c6e3cdf658205ff7
BLAKE2b-256 735ca840e42b276d5fce9f736a5e34237e5b886ef1866df65efb71e76315d324

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