Skip to main content

Cython implementations of multiple root-finding methods.

Project description

cy-root

PyPI GitHub


(Not this root)

 

A simple root-finding package written in Cython. Many of the implemented methods can't be found in common Python libraries. Not a serious one so please only use these codes as learning materials.

Context:

I had to find root of this beast of a function, which has no known bound.

$$ f(x) = \frac{1}{\sum\limits_{j=1}^{\infty} \left(\prod\limits_{k=j}^{\infty} \frac{1}{k \cdot x + 1} \right) } - p $$

Fortunately, Sidi's method came to the rescue.

Requirements

  • Python 3.6+
  • Cython (if you want to build from .pyx files)
  • numpy
  • sympy
  • A C/C++ compiler

Installation

Make sure you have all the dependencies installed, then clone this repo and run:

git clone git://github.com/inspiros/cy-root.git
cd cy-root
pip install .

For uninstalling:

pip uninstall cy-root

Supported algorithms

Note: For more information about the listed algorithms, please use Google until I update the references.

Scalar root:

  • Bracketing methods: (methods that require lower and upper bounds)
    • Bisect
    • Regula Falsi (False Position)
    • Illinois
    • Pegasus
    • Anderson–Björck
    • Dekker
    • Brent
    • Chandrupatla
    • Ridders
    • TOMS748
    • Wu
    • ITP
  • Newton-like methods: (methods that require derivative and higher order derivatives)
    • Newton-Raphson
    • Halley
    • Householder
  • Quasi-Newton methods: (methods that approximate derivative or use interpolation)
    • Secant
    • Sidi
    • Steffensen
    • Inverse Quadratic Interpolation
    • Hyperbolic Interpolation
    • Muller (for complex root)

Vector root:

Not yet.

Usage

Some examples:

Use find_root_scalar and pass method name as first argument.

from cyroot import find_root_scalar

f = lambda x: x ** 2 - 612
result = find_root_scalar(method='itp', f=f, a=-10, b=50)
print(result)

Output:

RootResults(root=24.73863375370596, f_root=-1.1368683772161603e-13, iters=8, f_calls=11, a=24.73863375370596, b=24.73863375370596, f_a=-1.1368683772161603e-13, f_b=-1.1368683772161603e-13, precision=6.353294779160024e-08, error=1.1368683772161603e-13, converged=True, optimal=True)

Alternatively, import the function directly. You can also see the full input arguments of by using help() on them.

from cyroot import muller

# This function has no real root
f = lambda x: x ** 4 + 4 * x ** 2 + 5
# But Muller's method can be used to find complex root
result = muller(f, x0=0, x1=10, x2=20)
print(result)

Output:

RootResults(root=(0.34356074972251255+1.4553466902253551j), f_root=(-8.881784197001252e-16-1.7763568394002505e-15j), iters=43, f_calls=43, precision=3.177770418807502e-08, error=1.9860273225978185e-15, converged=True, optimal=True)

The returned result is a namedtuple whose elements depend on the type of the method:

  • Common:
    • root: the solved root
    • f_root: value evaluated at root
    • iters: number of iterations
    • f_calls: number of function calls
    • precision: width of final bracket (for bracketing methods) or absolute difference of root with the last estimation
    • error: absolute value of f_root
    • converged: True if the stopping criterion is met, False if the procedure terminated early.
    • optimal: True only if the error tolerance is satisfied.
  • Exclusive to bracketing methods:
    • a: final lower bound
    • b: final upper bound
    • f_a: value evaluated at final lower bound
    • f_b: value evaluated at final upper bound
  • Exclusive to Newton-like methods:
    • df_root: derivative or tuple of derivatives (of increasing orders) evaluated at root

Note: converged might sometimes be True even if the solution is not optimal, which means the routine stopped because the precision tolerance is satisfied.

For more examples, please refer to the examples folder.

License

The code is released under the MIT license. See LICENSE.txt for details.

References

https://en.wikipedia.org/wiki/Root-finding_algorithms

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

cy-root-0.1.4.tar.gz (925.3 kB view hashes)

Uploaded Source

Built Distributions

cy_root-0.1.4-cp310-cp310-win_amd64.whl (1.5 MB view hashes)

Uploaded CPython 3.10 Windows x86-64

cy_root-0.1.4-cp310-cp310-win32.whl (1.4 MB view hashes)

Uploaded CPython 3.10 Windows x86

cy_root-0.1.4-cp310-cp310-musllinux_1_1_x86_64.whl (5.3 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

cy_root-0.1.4-cp310-cp310-musllinux_1_1_i686.whl (5.2 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ i686

cy_root-0.1.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.7 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

cy_root-0.1.4-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (4.5 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

cy_root-0.1.4-cp310-cp310-macosx_10_9_x86_64.whl (1.6 MB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

cy_root-0.1.4-cp39-cp39-win_amd64.whl (1.5 MB view hashes)

Uploaded CPython 3.9 Windows x86-64

cy_root-0.1.4-cp39-cp39-win32.whl (1.4 MB view hashes)

Uploaded CPython 3.9 Windows x86

cy_root-0.1.4-cp39-cp39-musllinux_1_1_x86_64.whl (5.4 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

cy_root-0.1.4-cp39-cp39-musllinux_1_1_i686.whl (5.2 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ i686

cy_root-0.1.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.8 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

cy_root-0.1.4-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (4.7 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

cy_root-0.1.4-cp39-cp39-macosx_10_9_x86_64.whl (1.6 MB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

cy_root-0.1.4-cp38-cp38-win_amd64.whl (1.5 MB view hashes)

Uploaded CPython 3.8 Windows x86-64

cy_root-0.1.4-cp38-cp38-win32.whl (1.4 MB view hashes)

Uploaded CPython 3.8 Windows x86

cy_root-0.1.4-cp38-cp38-musllinux_1_1_x86_64.whl (5.6 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

cy_root-0.1.4-cp38-cp38-musllinux_1_1_i686.whl (5.4 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ i686

cy_root-0.1.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.8 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

cy_root-0.1.4-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (4.7 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

cy_root-0.1.4-cp38-cp38-macosx_10_9_x86_64.whl (1.6 MB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

cy_root-0.1.4-cp37-cp37m-win_amd64.whl (1.5 MB view hashes)

Uploaded CPython 3.7m Windows x86-64

cy_root-0.1.4-cp37-cp37m-win32.whl (1.4 MB view hashes)

Uploaded CPython 3.7m Windows x86

cy_root-0.1.4-cp37-cp37m-musllinux_1_1_x86_64.whl (5.1 MB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ x86-64

cy_root-0.1.4-cp37-cp37m-musllinux_1_1_i686.whl (4.9 MB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ i686

cy_root-0.1.4-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.5 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

cy_root-0.1.4-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (4.3 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

cy_root-0.1.4-cp37-cp37m-macosx_10_9_x86_64.whl (1.6 MB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

cy_root-0.1.4-cp36-cp36m-win_amd64.whl (1.5 MB view hashes)

Uploaded CPython 3.6m Windows x86-64

cy_root-0.1.4-cp36-cp36m-win32.whl (1.4 MB view hashes)

Uploaded CPython 3.6m Windows x86

cy_root-0.1.4-cp36-cp36m-musllinux_1_1_x86_64.whl (5.1 MB view hashes)

Uploaded CPython 3.6m musllinux: musl 1.1+ x86-64

cy_root-0.1.4-cp36-cp36m-musllinux_1_1_i686.whl (4.9 MB view hashes)

Uploaded CPython 3.6m musllinux: musl 1.1+ i686

cy_root-0.1.4-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.5 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ x86-64

cy_root-0.1.4-cp36-cp36m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (4.3 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

cy_root-0.1.4-cp36-cp36m-macosx_10_9_x86_64.whl (1.6 MB view hashes)

Uploaded CPython 3.6m macOS 10.9+ x86-64

Supported by

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