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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rootfinder-1.0.0.tar.gz.
File metadata
- Download URL: rootfinder-1.0.0.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
290ab8d9c59542c23b28c364bab9d2f01b1717cb0bf5303e0de93b404297bc45
|
|
| MD5 |
7145d8a4829f2cf24c04ca9bcecf6e3b
|
|
| BLAKE2b-256 |
0cea05c9a871b677b7aac3fec7af91fb98c4d7c5a195c703321a0e1a0b6e6d91
|
File details
Details for the file rootfinder-1.0.0-py3-none-any.whl.
File metadata
- Download URL: rootfinder-1.0.0-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0d72e9ff9993447dbe11cb8ae9b9d695b36179ac5a5ce42d6181b17d25ef6156
|
|
| MD5 |
c30e3367628c1ebaa9fe736383463383
|
|
| BLAKE2b-256 |
a4ac198d66f3724cc5e9acd813c25e7ed6c202095d799a9da2ec41a99808583b
|