{"id":15664492,"url":"https://github.com/yoelcortes/flexsolve","last_synced_at":"2025-04-14T16:14:30.621Z","repository":{"id":57431361,"uuid":"222858671","full_name":"yoelcortes/flexsolve","owner":"yoelcortes","description":"Flexible function solvers","archived":false,"fork":false,"pushed_at":"2024-05-24T16:05:44.000Z","size":17930,"stargazers_count":19,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-14T16:14:24.750Z","etag":null,"topics":["aitken-delta-squared-method","fixed-point","quadratic-interpolation","secant-method","solver","steffensen-s-method","wegstein-s-method"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yoelcortes.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-11-20T05:33:06.000Z","updated_at":"2025-03-18T10:20:15.000Z","dependencies_parsed_at":"2024-06-12T18:26:13.972Z","dependency_job_id":null,"html_url":"https://github.com/yoelcortes/flexsolve","commit_stats":{"total_commits":175,"total_committers":1,"mean_commits":175.0,"dds":0.0,"last_synced_commit":"ea2386a84bd774084b1c477d1f244312a9dac9e1"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yoelcortes%2Fflexsolve","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yoelcortes%2Fflexsolve/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yoelcortes%2Fflexsolve/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yoelcortes%2Fflexsolve/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yoelcortes","download_url":"https://codeload.github.com/yoelcortes/flexsolve/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248914115,"owners_count":21182359,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aitken-delta-squared-method","fixed-point","quadratic-interpolation","secant-method","solver","steffensen-s-method","wegstein-s-method"],"created_at":"2024-10-03T13:42:48.387Z","updated_at":"2025-04-14T16:14:30.600Z","avatar_url":"https://github.com/yoelcortes.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"========================================================\nflexsolve: Flexible function solvers\n========================================================\n.. image:: http://img.shields.io/badge/license-MIT-blue.svg?style=flat\n   :target: https://github.com/yoelcortes/flexsolve/blob/master/LICENSE.txt\n   :alt: license\n.. image:: http://img.shields.io/pypi/v/flexsolve.svg?style=flat\n   :target: https://pypi.python.org/pypi/flexsolve\n   :alt: Version_status\n.. image:: https://img.shields.io/pypi/pyversions/flexsolve.svg\n   :target: https://pypi.python.org/pypi/flexsolve\n   :alt: Supported_versions\n.. image:: https://coveralls.io/repos/github/yoelcortes/flexsolve/badge.svg?branch=master\n   :target: https://coveralls.io/github/yoelcortes/flexsolve?branch=master\n\n\n.. contents::\n\nWhat is flexsolve?\n------------------\n\nflexsolve presents a flexible set of function solvers by defining alternative\ntolerance conditions for accepting a solution. These solvers also implement\nmethods like Wegstein and Aitken-Steffensen acceleration to reach solutions\nquicker.\n\nInstallation\n------------\n\nGet the latest version of flexsolve from `PyPI \u003chttps://pypi.python.org/pypi/flexsolve/\u003e`__. If you have an installation of Python with pip, simple install it with:\n\n    $ pip install flexsolve\n\nTo get the git version, run:\n\n    $ git clone git://github.com/yoelcortes/flexsolve\n\nDocumentation\n-------------\n\nFlexsolve solvers can solve a variety of specifications:\n\n* Solve x where f(x) = x (iterative):\n\n  * **fixed_point**: Simple fixed point iteration.\n\n  * **wegstein**: Wegstein's accelerated iteration method.\n\n  * **aitken**: Aitken-Steffensen accelerated iteration method.\n\n* Solve x where f(x) = 0 and x0 \u003c x \u003c x1 (bounded):\n\n  * **bisection**: Simple bisection method\n\n  * **false_position**: Simple false position method.\n\n  * **IQ_interpolation**: Inverse quadratic interpolation solver (similar to `scipy.optimize.brentq \u003chttps://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.optimize.brentq.html\u003e`__)\n\n* Solve x where f(x) = 0 (open):\n\n  * **secant**: Simple secant method.\n\n  * **aitken_secant**: Secant method with Aitken acceleration.\n\nParameters for each solver are pretty consitent and straight forward:\n\n* **f**: objective function in the form of `f(x, *args)`.\n\n* **x**: \n  \n  * Iterative solvers: Root guess. Solver begins the iteration by evaluating `f(x)`.\n\n* **x0, x1**: \n\n  * Bounded solvers: Root bracket. Solution must lie within `x0` and `x1`.\n  \n  * Open solvers: Initial and second guess. Second guess, 'x1', is optional.\n  \n* **xtol=0.**: Solver stops when the root lies within `xtol`.\n\n* **ytol=5e-8**: Solver stops when the f(x) lies within `ytol` of the root. Iterative solvers (which solve functions of the form f(x) = x) do not accept a `ytol` argument as xtol and ytol are actually mathematically equivalent.\n\n* **args=()**: Arguments to pass to `f`.\n\n* **maxiter=50**: Maximum number of iterations.\n\n* **checkiter=True**: Whether to raise a Runtimer error when tolerance could not be satisfied before the maximum number of iterations.\n\n* **checkroot=False**: Whether satisfying both tolerances, `xtol` and `ytol`, are required for termination.\n\n* **checkbounds=True**: Whether to raise a ValueError when in a bounded solver when the root is not certain to lie within bounds (i.e. f(x0) * f(x1) \u003e 0.).\n\nHere are some examples using flexsolve's Profiler object to test and compare\ndifferent solvers. In the graphs, the points are the solver iterations and \nthe lines represent f(x). The lines and points are offset to make them more visible\n(so all the points are actually on the same curve!). The shaded area is just to \nhelp us relate the points to the curve (not an actual interval):\n\n.. code-block:: python\n\n    \u003e\u003e\u003e import flexsolve as flx \n    \u003e\u003e\u003e from scipy import optimize as opt\n    \u003e\u003e\u003e x0, x1 = [-5, 5]\n    \u003e\u003e\u003e f = lambda x: x**3 - 40 + 2*x \n    \u003e\u003e\u003e p = flx.Profiler(f) # When called, it returns f(x) and saves the results.\n    \u003e\u003e\u003e opt.brentq(p, x0, x1, xtol=1e-8)\n    3.225240462778411\n    \u003e\u003e\u003e p.archive('[Scipy] Brent-Q') # Save/archive results with given name\n    \u003e\u003e\u003e opt.brenth(p, x0, x1)\n    3.2252404627917794\n    \u003e\u003e\u003e p.archive('[Scipy] Brent-H')\n    \u003e\u003e\u003e flx.IQ_interpolation(p, x0, x1)\n    3.225240462796626\n    \u003e\u003e\u003e p.archive('IQ-interpolation')\n    \u003e\u003e\u003e flx.false_position(p, x0, x1)\n    3.225240462687035\n    \u003e\u003e\u003e p.archive('False position')\n    \u003e\u003e\u003e p.plot(r'$f(x) = 0 = x^3 + 2 \\cdot x - 40$ where $-5 \u003c x \u003c 5$')\n\n.. image:: https://raw.githubusercontent.com/yoelcortes/flexsolve/master/docs/images/bounded_solvers_example.png\n\n.. code-block:: python\n\n    \u003e\u003e\u003e p = flx.Profiler(f)\n    \u003e\u003e\u003e x_guess = -5\n    \u003e\u003e\u003e flx.aitken_secant(p, x_guess)\n    3.22524046279178\n    \u003e\u003e\u003e p.archive('Aitken')\n    \u003e\u003e\u003e flx.secant(p, x_guess)\n    3.2252404627918057\n    \u003e\u003e\u003e p.archive('Secant')\n    \u003e\u003e\u003e opt.newton(p, x_guess)\n    3.2252404627918065\n    \u003e\u003e\u003e p.archive('[Scipy] Newton')\n    \u003e\u003e\u003e p.plot(r'$f(x) = 0 = x^3 + 2 \\cdot x - 40$')\n\n.. image:: https://raw.githubusercontent.com/yoelcortes/flexsolve/master/docs/images/general_solvers_example.png\n\n.. code-block:: python\n\n    \u003e\u003e\u003e # Note that x = 40/x^2 - 2/x is the same\n    \u003e\u003e\u003e # objective function as x**3 - 40 + 2*x = 0\n    \u003e\u003e\u003e f = lambda x: 40/x**2 - 2/x\n    \u003e\u003e\u003e p = flx.Profiler(f)\n    \u003e\u003e\u003e x_guess = 5.\n    \u003e\u003e\u003e flx.wegstein(p, x_guess)\n    3.2252404626726996\n    \u003e\u003e\u003e p.archive('Wegstein')\n    \u003e\u003e\u003e flx.aitken(p, x_guess)\n    3.2252404627250075\n    \u003e\u003e\u003e p.archive('Aitken')\n    \u003e\u003e\u003e p.plot(r'$f(x) = x = \\frac{40}{x^2} - \\frac{2}{x}$',\n    ...        markbounds=False)\n    \u003e\u003e\u003e # Fixed-point iteration is non-convergent for this equation,\n    \u003e\u003e\u003e # so we do not include it here\n\n.. image:: https://raw.githubusercontent.com/yoelcortes/flexsolve/master/docs/images/fixed_point_solvers_example.png\n\nIf you have multiple layers of functions with solvers, you can speed up \ncalculations in flexsolve by JIT compiling them with `numba \u003chttps://numba.pydata.org/numba-doc/dev/index.html\u003e`__.\nThe following example benchmarks flexsolve's speed with and without compiling:\n\n.. code-block:: python\n\n    \u003e\u003e\u003e import flexsolve as flx\n    \u003e\u003e\u003e from numba import njit\n    \u003e\u003e\u003e f = lambda x, y: x**3 - 40 + y*x \n    \u003e\u003e\u003e g = lambda y, z: y + flx.IQ_interpolation(f, -100, 100, args=(y,))\n    \u003e\u003e\u003e h = lambda z: z - flx.IQ_interpolation(g, -10, 10, args=(z,))\n    \u003e\u003e\u003e # Time solver without compiling\n    \u003e\u003e\u003e %timeit flx.IQ_interpolation(h, -5, 15)\n    352 µs ± 5.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n    \u003e\u003e\u003e f = njit(f)\n    \u003e\u003e\u003e g = njit(g)\n    \u003e\u003e\u003e h = njit(h)\n    \u003e\u003e\u003e # First run is slower because it need to compile\n    \u003e\u003e\u003e x = flx.IQ_interpolation(h, -5, 5) \n    \u003e\u003e\u003e # Time solver after compiling\n    \u003e\u003e\u003e %timeit flx.IQ_interpolation(h, -5, 5)\n    4.32 µs ± 79.4 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n    \nThe iterative methods for solving f(x) = x (e.g. fixed-point, Wegstain, Aitken) are \ncapable of solving multi-dimensional problems. Simply make sure x is an array \nand f(x) returns an array with the same dimensions. In fact, the\n`The Biorefinery Simulation and Techno-Economic Analysis Modules (BioSTEAM) \u003chttps://biosteam.readthedocs.io/en/latest/\u003e`_ \nuses flexsolve to solve many chemical engineering problems, including \nprocess recycle stream flow rates and vapor-liquid equilibrium.\n\nBug reports\n-----------\n\nTo report bugs, please use the flexsolve's Bug Tracker at:\n\n    https://github.com/yoelcortes/flexsolve\n\n\nLicense information\n-------------------\n\nSee ``LICENSE.txt`` for information on the terms \u0026 conditions for usage\nof this software, and a DISCLAIMER OF ALL WARRANTIES.\n\nAlthough not required by the flexsolve license, if it is convenient for you,\nplease cite flexsolve if used in your work. Please also consider contributing\nany changes you make back, and benefit the community.\n\n\nCitation\n--------\n\nTo cite flexsolve in publications use:\n\n    Yoel Cortes-Pena (2019). flexsolve: Flexible function solvers.\n    https://github.com/yoelcortes/flexsolve\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyoelcortes%2Fflexsolve","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyoelcortes%2Fflexsolve","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyoelcortes%2Fflexsolve/lists"}