Skip to main content

Numerical root-finding made easy โ€” bisection, Newton-Raphson, Brent, and more

Project description

๐Ÿ” rootfinder

Numerical root-finding made ridiculously easy.

Solve f(x) = 0 with beautiful graphs and tables โ€” one import, one line.

pip install rootfinder

โšก Quick Start

from rootfinder import solve, bisection, newton, solve_all, plot_comparison

f = lambda x: x**3 - x - 2

# Auto-pick the best method
r = solve(f, a=1, b=2)
print(r.root)      # 1.5213797068045675
print(r)           # full result summary

# See the iteration table
r.table()

# Plot convergence graph
r.plot()

๐Ÿ“ฆ Installation

pip install rootfinder

Requires Python โ‰ฅ 3.8. Dependencies (auto-installed): numpy, matplotlib.


๐Ÿ› ๏ธ All Methods

Method Function Best For
Bisection bisection(f, a, b) Guaranteed convergence, simple
Newton-Raphson newton(f, x0) Fast (quadratic), needs derivative
Regula Falsi regula_falsi(f, a, b) Bracketed, often faster than bisection
Secant secant(f, x0, x1) Newton-like, no derivative needed
Brent brent(f, a, b) Gold standard โ€” fast + guaranteed
Ridder ridder(f, a, b) Quadratic + bracketed
Fixed-Point fixed_point(g, x0) For equations in x = g(x) form
Steffensen steffensen(f, x0) Fixed-point + acceleration
Mรผller muller(f, x0, x1, x2) Finds complex roots too!

๐Ÿ“– Usage Examples

Bisection โ€” guaranteed to converge

from rootfinder import bisection

f = lambda x: x**2 - 2          # find โˆš2

r = bisection(f, 1, 2)
print(r.root)                    # 1.4142135623730951
r.table()                        # print iteration table
r.plot()                         # plot convergence

Newton-Raphson โ€” fastest convergence

from rootfinder import newton
import math

# With explicit derivative (faster)
r = newton(math.sin, x0=3.0, df=math.cos)

# Without derivative (auto-computed numerically)
r = newton(lambda x: x**3 - 2, x0=1.0)

print(r)

Regula Falsi (False Position)

from rootfinder import regula_falsi

f = lambda x: x**3 - x - 2
r = regula_falsi(f, 1, 2)
r.table()

Brent's Method โ€” recommended default

from rootfinder import brent

f = lambda x: math.exp(-x) - x
r = brent(f, 0, 1)
print(r.summary())

Fixed-Point Iteration

from rootfinder import fixed_point
import math

# x^2 - x - 2 = 0  โ†’  rewrite as  x = sqrt(x + 2)
r = fixed_point(lambda x: math.sqrt(x + 2), x0=1.5)
print(r.root)   # 2.0

Mรผller's Method โ€” finds complex roots

from rootfinder import muller

# x^2 + 1 = 0  โ†’  complex roots ยฑi
r = muller(lambda x: x**2 + 1, x0=-1, x1=0, x2=1)

Compare ALL methods at once

from rootfinder import solve_all, plot_comparison

f = lambda x: x**3 - x - 2

results = solve_all(f, a=1, b=2)   # runs all methods, prints comparison table
plot_comparison(results)            # overlay convergence graph

Output:

โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
Method           Root               f(root)   Iters   Status
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
Bisection        1.5213797068046   1.776e-15     36   โœ“
Regula Falsi     1.52137970680457  2.220e-16     12   โœ“
Brent            1.5213797068046   0.000e+00      8   โœ“
Ridder           1.5213797068046   4.441e-16      9   โœ“
Newton           1.5213797068046   0.000e+00      6   โœ“
Secant           1.5213797068046   2.220e-16      8   โœ“
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

๐Ÿ“Š Visualization

from rootfinder import plot_function, plot_convergence, plot_comparison, plot_iterations

f = lambda x: x**3 - x - 2
r = bisection(f, 1, 2)

# 1. Plot the function with root marked
plot_function(f, a=0, b=3, root=r.root)

# 2. Plot how error decreased across iterations
plot_convergence(r)

# 3. Show iteration steps on the function
plot_iterations(f, r, a=1, b=2)

# 4. Compare multiple methods
from rootfinder import solve_all
results = solve_all(f, 1, 2)
plot_comparison(results)

๐Ÿ“‹ RootResult Object

Every method returns a RootResult with:

r = bisection(f, 1, 2)

r.root          # float: the root
r.f_root        # float: f(root), should be ~0
r.converged     # bool: did it converge?
r.iterations    # int: number of iterations
r.error         # float: final error estimate
r.method        # str: method name
r.steps         # list of IterationStep objects
r.xs            # list of x values per iteration
r.errors        # list of errors per iteration

r.table()       # print iteration table
r.plot()        # plot convergence
r.summary()     # one-line string summary

float(r)        # use root as a plain float

๐Ÿš€ Deploying / Publishing to PyPI

See PUBLISH.md for the step-by-step guide to upload your own version.


๐Ÿ“„ License

MIT โ€” free for any use.

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

rootfinder-1.0.1.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

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

rootfinder-1.0.1-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

Details for the file rootfinder-1.0.1.tar.gz.

File metadata

  • Download URL: rootfinder-1.0.1.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for rootfinder-1.0.1.tar.gz
Algorithm Hash digest
SHA256 4f4fdf62fbe085b1d229fa1d05724a31f29ade24b5dcd1b6444dc4c9c269dd0d
MD5 a2d883afb0d7fb3b54733c67b40a673e
BLAKE2b-256 ba4a215b85d666f5aefc9675c166b174bbecac50a38b00be91fa0db44cd1706f

See more details on using hashes here.

File details

Details for the file rootfinder-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: rootfinder-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for rootfinder-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d385b037402963953421ff3c37683a36bfbb9da76403dd0cb4cc09c0e94fa557
MD5 fc840f92f21c5c0edab062d0370d66f4
BLAKE2b-256 6a67aa062de63265578c2c18eef8f65f34e67c3952cf6bea6baa58aace04b5fa

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