flexsolve


Nameflexsolve JSON
Version 0.5.7 PyPI version JSON
download
home_pagehttps://github.com/yoelcortes/flexsolve
SummaryFlexible function solvers
upload_time2024-05-24 15:56:08
maintainerNone
docs_urlNone
authorYoel Cortes-Pena
requires_python>=3.9
licenseMIT
keywords solve equation function flexible
VCS
bugtrack_url
requirements numpy numba
Travis-CI
coveralls test coverage No coveralls.
            ========================================================
flexsolve: Flexible function solvers
========================================================
.. image:: http://img.shields.io/badge/license-MIT-blue.svg?style=flat
   :target: https://github.com/yoelcortes/flexsolve/blob/master/LICENSE.txt
   :alt: license
.. image:: http://img.shields.io/pypi/v/flexsolve.svg?style=flat
   :target: https://pypi.python.org/pypi/flexsolve
   :alt: Version_status
.. image:: https://img.shields.io/pypi/pyversions/flexsolve.svg
   :target: https://pypi.python.org/pypi/flexsolve
   :alt: Supported_versions
.. image:: https://coveralls.io/repos/github/yoelcortes/flexsolve/badge.svg?branch=master
   :target: https://coveralls.io/github/yoelcortes/flexsolve?branch=master


.. contents::

What is flexsolve?
------------------

flexsolve presents a flexible set of function solvers by defining alternative
tolerance conditions for accepting a solution. These solvers also implement
methods like Wegstein and Aitken-Steffensen acceleration to reach solutions
quicker.

Installation
------------

Get the latest version of flexsolve from `PyPI <https://pypi.python.org/pypi/flexsolve/>`__. If you have an installation of Python with pip, simple install it with:

    $ pip install flexsolve

To get the git version, run:

    $ git clone git://github.com/yoelcortes/flexsolve

Documentation
-------------

Flexsolve solvers can solve a variety of specifications:

* Solve x where f(x) = x (iterative):

  * **fixed_point**: Simple fixed point iteration.

  * **wegstein**: Wegstein's accelerated iteration method.

  * **aitken**: Aitken-Steffensen accelerated iteration method.

* Solve x where f(x) = 0 and x0 < x < x1 (bounded):

  * **bisection**: Simple bisection method

  * **false_position**: Simple false position method.

  * **IQ_interpolation**: Inverse quadratic interpolation solver (similar to `scipy.optimize.brentq <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.optimize.brentq.html>`__)

* Solve x where f(x) = 0 (open):

  * **secant**: Simple secant method.

  * **aitken_secant**: Secant method with Aitken acceleration.

Parameters for each solver are pretty consitent and straight forward:

* **f**: objective function in the form of `f(x, *args)`.

* **x**: 
  
  * Iterative solvers: Root guess. Solver begins the iteration by evaluating `f(x)`.

* **x0, x1**: 

  * Bounded solvers: Root bracket. Solution must lie within `x0` and `x1`.
  
  * Open solvers: Initial and second guess. Second guess, 'x1', is optional.
  
* **xtol=0.**: Solver stops when the root lies within `xtol`.

* **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.

* **args=()**: Arguments to pass to `f`.

* **maxiter=50**: Maximum number of iterations.

* **checkiter=True**: Whether to raise a Runtimer error when tolerance could not be satisfied before the maximum number of iterations.

* **checkroot=False**: Whether satisfying both tolerances, `xtol` and `ytol`, are required for termination.

* **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) > 0.).

Here are some examples using flexsolve's Profiler object to test and compare
different solvers. In the graphs, the points are the solver iterations and 
the lines represent f(x). The lines and points are offset to make them more visible
(so all the points are actually on the same curve!). The shaded area is just to 
help us relate the points to the curve (not an actual interval):

.. code-block:: python

    >>> import flexsolve as flx 
    >>> from scipy import optimize as opt
    >>> x0, x1 = [-5, 5]
    >>> f = lambda x: x**3 - 40 + 2*x 
    >>> p = flx.Profiler(f) # When called, it returns f(x) and saves the results.
    >>> opt.brentq(p, x0, x1, xtol=1e-8)
    3.225240462778411
    >>> p.archive('[Scipy] Brent-Q') # Save/archive results with given name
    >>> opt.brenth(p, x0, x1)
    3.2252404627917794
    >>> p.archive('[Scipy] Brent-H')
    >>> flx.IQ_interpolation(p, x0, x1)
    3.225240462796626
    >>> p.archive('IQ-interpolation')
    >>> flx.false_position(p, x0, x1)
    3.225240462687035
    >>> p.archive('False position')
    >>> p.plot(r'$f(x) = 0 = x^3 + 2 \cdot x - 40$ where $-5 < x < 5$')

.. image:: https://raw.githubusercontent.com/yoelcortes/flexsolve/master/docs/images/bounded_solvers_example.png

.. code-block:: python

    >>> p = flx.Profiler(f)
    >>> x_guess = -5
    >>> flx.aitken_secant(p, x_guess)
    3.22524046279178
    >>> p.archive('Aitken')
    >>> flx.secant(p, x_guess)
    3.2252404627918057
    >>> p.archive('Secant')
    >>> opt.newton(p, x_guess)
    3.2252404627918065
    >>> p.archive('[Scipy] Newton')
    >>> p.plot(r'$f(x) = 0 = x^3 + 2 \cdot x - 40$')

.. image:: https://raw.githubusercontent.com/yoelcortes/flexsolve/master/docs/images/general_solvers_example.png

.. code-block:: python

    >>> # Note that x = 40/x^2 - 2/x is the same
    >>> # objective function as x**3 - 40 + 2*x = 0
    >>> f = lambda x: 40/x**2 - 2/x
    >>> p = flx.Profiler(f)
    >>> x_guess = 5.
    >>> flx.wegstein(p, x_guess)
    3.2252404626726996
    >>> p.archive('Wegstein')
    >>> flx.aitken(p, x_guess)
    3.2252404627250075
    >>> p.archive('Aitken')
    >>> p.plot(r'$f(x) = x = \frac{40}{x^2} - \frac{2}{x}$',
    ...        markbounds=False)
    >>> # Fixed-point iteration is non-convergent for this equation,
    >>> # so we do not include it here

.. image:: https://raw.githubusercontent.com/yoelcortes/flexsolve/master/docs/images/fixed_point_solvers_example.png

If you have multiple layers of functions with solvers, you can speed up 
calculations in flexsolve by JIT compiling them with `numba <https://numba.pydata.org/numba-doc/dev/index.html>`__.
The following example benchmarks flexsolve's speed with and without compiling:

.. code-block:: python

    >>> import flexsolve as flx
    >>> from numba import njit
    >>> f = lambda x, y: x**3 - 40 + y*x 
    >>> g = lambda y, z: y + flx.IQ_interpolation(f, -100, 100, args=(y,))
    >>> h = lambda z: z - flx.IQ_interpolation(g, -10, 10, args=(z,))
    >>> # Time solver without compiling
    >>> %timeit flx.IQ_interpolation(h, -5, 15)
    352 µs ± 5.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
    >>> f = njit(f)
    >>> g = njit(g)
    >>> h = njit(h)
    >>> # First run is slower because it need to compile
    >>> x = flx.IQ_interpolation(h, -5, 5) 
    >>> # Time solver after compiling
    >>> %timeit flx.IQ_interpolation(h, -5, 5)
    4.32 µs ± 79.4 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
    
The iterative methods for solving f(x) = x (e.g. fixed-point, Wegstain, Aitken) are 
capable of solving multi-dimensional problems. Simply make sure x is an array 
and f(x) returns an array with the same dimensions. In fact, the
`The Biorefinery Simulation and Techno-Economic Analysis Modules (BioSTEAM) <https://biosteam.readthedocs.io/en/latest/>`_ 
uses flexsolve to solve many chemical engineering problems, including 
process recycle stream flow rates and vapor-liquid equilibrium.

Bug reports
-----------

To report bugs, please use the flexsolve's Bug Tracker at:

    https://github.com/yoelcortes/flexsolve


License information
-------------------

See ``LICENSE.txt`` for information on the terms & conditions for usage
of this software, and a DISCLAIMER OF ALL WARRANTIES.

Although not required by the flexsolve license, if it is convenient for you,
please cite flexsolve if used in your work. Please also consider contributing
any changes you make back, and benefit the community.


Citation
--------

To cite flexsolve in publications use:

    Yoel Cortes-Pena (2019). flexsolve: Flexible function solvers.
    https://github.com/yoelcortes/flexsolve

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/yoelcortes/flexsolve",
    "name": "flexsolve",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "solve, equation, function, flexible",
    "author": "Yoel Cortes-Pena",
    "author_email": "yoelcortes@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/e7/0e/1c1e6ff246b8bea2718392b4045cfc8d666c53f63bb4ef9ff70373d93468/flexsolve-0.5.7.tar.gz",
    "platform": "Windows",
    "description": "========================================================\r\nflexsolve: Flexible function solvers\r\n========================================================\r\n.. image:: http://img.shields.io/badge/license-MIT-blue.svg?style=flat\r\n   :target: https://github.com/yoelcortes/flexsolve/blob/master/LICENSE.txt\r\n   :alt: license\r\n.. image:: http://img.shields.io/pypi/v/flexsolve.svg?style=flat\r\n   :target: https://pypi.python.org/pypi/flexsolve\r\n   :alt: Version_status\r\n.. image:: https://img.shields.io/pypi/pyversions/flexsolve.svg\r\n   :target: https://pypi.python.org/pypi/flexsolve\r\n   :alt: Supported_versions\r\n.. image:: https://coveralls.io/repos/github/yoelcortes/flexsolve/badge.svg?branch=master\r\n   :target: https://coveralls.io/github/yoelcortes/flexsolve?branch=master\r\n\r\n\r\n.. contents::\r\n\r\nWhat is flexsolve?\r\n------------------\r\n\r\nflexsolve presents a flexible set of function solvers by defining alternative\r\ntolerance conditions for accepting a solution. These solvers also implement\r\nmethods like Wegstein and Aitken-Steffensen acceleration to reach solutions\r\nquicker.\r\n\r\nInstallation\r\n------------\r\n\r\nGet the latest version of flexsolve from `PyPI <https://pypi.python.org/pypi/flexsolve/>`__. If you have an installation of Python with pip, simple install it with:\r\n\r\n    $ pip install flexsolve\r\n\r\nTo get the git version, run:\r\n\r\n    $ git clone git://github.com/yoelcortes/flexsolve\r\n\r\nDocumentation\r\n-------------\r\n\r\nFlexsolve solvers can solve a variety of specifications:\r\n\r\n* Solve x where f(x) = x (iterative):\r\n\r\n  * **fixed_point**: Simple fixed point iteration.\r\n\r\n  * **wegstein**: Wegstein's accelerated iteration method.\r\n\r\n  * **aitken**: Aitken-Steffensen accelerated iteration method.\r\n\r\n* Solve x where f(x) = 0 and x0 < x < x1 (bounded):\r\n\r\n  * **bisection**: Simple bisection method\r\n\r\n  * **false_position**: Simple false position method.\r\n\r\n  * **IQ_interpolation**: Inverse quadratic interpolation solver (similar to `scipy.optimize.brentq <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.optimize.brentq.html>`__)\r\n\r\n* Solve x where f(x) = 0 (open):\r\n\r\n  * **secant**: Simple secant method.\r\n\r\n  * **aitken_secant**: Secant method with Aitken acceleration.\r\n\r\nParameters for each solver are pretty consitent and straight forward:\r\n\r\n* **f**: objective function in the form of `f(x, *args)`.\r\n\r\n* **x**: \r\n  \r\n  * Iterative solvers: Root guess. Solver begins the iteration by evaluating `f(x)`.\r\n\r\n* **x0, x1**: \r\n\r\n  * Bounded solvers: Root bracket. Solution must lie within `x0` and `x1`.\r\n  \r\n  * Open solvers: Initial and second guess. Second guess, 'x1', is optional.\r\n  \r\n* **xtol=0.**: Solver stops when the root lies within `xtol`.\r\n\r\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.\r\n\r\n* **args=()**: Arguments to pass to `f`.\r\n\r\n* **maxiter=50**: Maximum number of iterations.\r\n\r\n* **checkiter=True**: Whether to raise a Runtimer error when tolerance could not be satisfied before the maximum number of iterations.\r\n\r\n* **checkroot=False**: Whether satisfying both tolerances, `xtol` and `ytol`, are required for termination.\r\n\r\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) > 0.).\r\n\r\nHere are some examples using flexsolve's Profiler object to test and compare\r\ndifferent solvers. In the graphs, the points are the solver iterations and \r\nthe lines represent f(x). The lines and points are offset to make them more visible\r\n(so all the points are actually on the same curve!). The shaded area is just to \r\nhelp us relate the points to the curve (not an actual interval):\r\n\r\n.. code-block:: python\r\n\r\n    >>> import flexsolve as flx \r\n    >>> from scipy import optimize as opt\r\n    >>> x0, x1 = [-5, 5]\r\n    >>> f = lambda x: x**3 - 40 + 2*x \r\n    >>> p = flx.Profiler(f) # When called, it returns f(x) and saves the results.\r\n    >>> opt.brentq(p, x0, x1, xtol=1e-8)\r\n    3.225240462778411\r\n    >>> p.archive('[Scipy] Brent-Q') # Save/archive results with given name\r\n    >>> opt.brenth(p, x0, x1)\r\n    3.2252404627917794\r\n    >>> p.archive('[Scipy] Brent-H')\r\n    >>> flx.IQ_interpolation(p, x0, x1)\r\n    3.225240462796626\r\n    >>> p.archive('IQ-interpolation')\r\n    >>> flx.false_position(p, x0, x1)\r\n    3.225240462687035\r\n    >>> p.archive('False position')\r\n    >>> p.plot(r'$f(x) = 0 = x^3 + 2 \\cdot x - 40$ where $-5 < x < 5$')\r\n\r\n.. image:: https://raw.githubusercontent.com/yoelcortes/flexsolve/master/docs/images/bounded_solvers_example.png\r\n\r\n.. code-block:: python\r\n\r\n    >>> p = flx.Profiler(f)\r\n    >>> x_guess = -5\r\n    >>> flx.aitken_secant(p, x_guess)\r\n    3.22524046279178\r\n    >>> p.archive('Aitken')\r\n    >>> flx.secant(p, x_guess)\r\n    3.2252404627918057\r\n    >>> p.archive('Secant')\r\n    >>> opt.newton(p, x_guess)\r\n    3.2252404627918065\r\n    >>> p.archive('[Scipy] Newton')\r\n    >>> p.plot(r'$f(x) = 0 = x^3 + 2 \\cdot x - 40$')\r\n\r\n.. image:: https://raw.githubusercontent.com/yoelcortes/flexsolve/master/docs/images/general_solvers_example.png\r\n\r\n.. code-block:: python\r\n\r\n    >>> # Note that x = 40/x^2 - 2/x is the same\r\n    >>> # objective function as x**3 - 40 + 2*x = 0\r\n    >>> f = lambda x: 40/x**2 - 2/x\r\n    >>> p = flx.Profiler(f)\r\n    >>> x_guess = 5.\r\n    >>> flx.wegstein(p, x_guess)\r\n    3.2252404626726996\r\n    >>> p.archive('Wegstein')\r\n    >>> flx.aitken(p, x_guess)\r\n    3.2252404627250075\r\n    >>> p.archive('Aitken')\r\n    >>> p.plot(r'$f(x) = x = \\frac{40}{x^2} - \\frac{2}{x}$',\r\n    ...        markbounds=False)\r\n    >>> # Fixed-point iteration is non-convergent for this equation,\r\n    >>> # so we do not include it here\r\n\r\n.. image:: https://raw.githubusercontent.com/yoelcortes/flexsolve/master/docs/images/fixed_point_solvers_example.png\r\n\r\nIf you have multiple layers of functions with solvers, you can speed up \r\ncalculations in flexsolve by JIT compiling them with `numba <https://numba.pydata.org/numba-doc/dev/index.html>`__.\r\nThe following example benchmarks flexsolve's speed with and without compiling:\r\n\r\n.. code-block:: python\r\n\r\n    >>> import flexsolve as flx\r\n    >>> from numba import njit\r\n    >>> f = lambda x, y: x**3 - 40 + y*x \r\n    >>> g = lambda y, z: y + flx.IQ_interpolation(f, -100, 100, args=(y,))\r\n    >>> h = lambda z: z - flx.IQ_interpolation(g, -10, 10, args=(z,))\r\n    >>> # Time solver without compiling\r\n    >>> %timeit flx.IQ_interpolation(h, -5, 15)\r\n    352 \u00c2\u00b5s \u00c2\u00b1 5.6 \u00c2\u00b5s per loop (mean \u00c2\u00b1 std. dev. of 7 runs, 1000 loops each)\r\n    >>> f = njit(f)\r\n    >>> g = njit(g)\r\n    >>> h = njit(h)\r\n    >>> # First run is slower because it need to compile\r\n    >>> x = flx.IQ_interpolation(h, -5, 5) \r\n    >>> # Time solver after compiling\r\n    >>> %timeit flx.IQ_interpolation(h, -5, 5)\r\n    4.32 \u00c2\u00b5s \u00c2\u00b1 79.4 ns per loop (mean \u00c2\u00b1 std. dev. of 7 runs, 100000 loops each)\r\n    \r\nThe iterative methods for solving f(x) = x (e.g. fixed-point, Wegstain, Aitken) are \r\ncapable of solving multi-dimensional problems. Simply make sure x is an array \r\nand f(x) returns an array with the same dimensions. In fact, the\r\n`The Biorefinery Simulation and Techno-Economic Analysis Modules (BioSTEAM) <https://biosteam.readthedocs.io/en/latest/>`_ \r\nuses flexsolve to solve many chemical engineering problems, including \r\nprocess recycle stream flow rates and vapor-liquid equilibrium.\r\n\r\nBug reports\r\n-----------\r\n\r\nTo report bugs, please use the flexsolve's Bug Tracker at:\r\n\r\n    https://github.com/yoelcortes/flexsolve\r\n\r\n\r\nLicense information\r\n-------------------\r\n\r\nSee ``LICENSE.txt`` for information on the terms & conditions for usage\r\nof this software, and a DISCLAIMER OF ALL WARRANTIES.\r\n\r\nAlthough not required by the flexsolve license, if it is convenient for you,\r\nplease cite flexsolve if used in your work. Please also consider contributing\r\nany changes you make back, and benefit the community.\r\n\r\n\r\nCitation\r\n--------\r\n\r\nTo cite flexsolve in publications use:\r\n\r\n    Yoel Cortes-Pena (2019). flexsolve: Flexible function solvers.\r\n    https://github.com/yoelcortes/flexsolve\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Flexible function solvers",
    "version": "0.5.7",
    "project_urls": {
        "Download": "https://github.com/yoelcortes/flexsolve.git",
        "Homepage": "https://github.com/yoelcortes/flexsolve"
    },
    "split_keywords": [
        "solve",
        " equation",
        " function",
        " flexible"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e70e1c1e6ff246b8bea2718392b4045cfc8d666c53f63bb4ef9ff70373d93468",
                "md5": "7eef5aeb4b81665509d5004770c0fdeb",
                "sha256": "d5d75114928417d5ef3341a5ecf5cf35905752b718d7227a0612b140044d236a"
            },
            "downloads": -1,
            "filename": "flexsolve-0.5.7.tar.gz",
            "has_sig": false,
            "md5_digest": "7eef5aeb4b81665509d5004770c0fdeb",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 154848,
            "upload_time": "2024-05-24T15:56:08",
            "upload_time_iso_8601": "2024-05-24T15:56:08.433259Z",
            "url": "https://files.pythonhosted.org/packages/e7/0e/1c1e6ff246b8bea2718392b4045cfc8d666c53f63bb4ef9ff70373d93468/flexsolve-0.5.7.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-24 15:56:08",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "yoelcortes",
    "github_project": "flexsolve",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "numpy",
            "specs": []
        },
        {
            "name": "numba",
            "specs": []
        }
    ],
    "lcname": "flexsolve"
}
        
Elapsed time: 0.24721s