tanh-sinh


Nametanh-sinh JSON
Version 0.3.8 PyPI version JSON
download
home_pageNone
Summarytanh-sinh quadrature for Python
upload_time2024-07-22 08:11:43
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseNone
keywords mathematics physics engineering quadrature integration numerical integration
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <p align="center">
  <a href="https://github.com/nschloe/tanh_sinh"><img alt="logo" src="https://raw.githubusercontent.com/sigma-py/tanh_sinh/main/logo/logo-tanhsinh.svg" width="60%"></a>
</p>

[![PyPi Version](https://img.shields.io/pypi/v/tanh_sinh.svg?style=flat-square)](https://pypi.org/project/tanh_sinh)
[![PyPI pyversions](https://img.shields.io/pypi/pyversions/tanh_sinh.svg?style=flat-square)](https://pypi.org/pypi/tanh_sinh/)
[![GitHub stars](https://img.shields.io/github/stars/nschloe/tanh_sinh.svg?style=flat-square&logo=github&label=Stars&logoColor=white)](https://github.com/nschloe/tanh_sinh)
[![PyPi downloads](https://img.shields.io/pypi/dm/tanh_sinh.svg?style=flat-square)](https://pypistats.org/packages/tanh_sinh)

[![Discord](https://img.shields.io/static/v1?logo=discord&logoColor=white&label=chat&message=on%20discord&color=7289da&style=flat-square)](https://discord.gg/hnTJ5MRX2Y)

The rather modern tanh-sinh quadrature is different from classical Gaussian
integration methods in that it doesn't integrate any function exactly, not even
polynomials of low degree. Its tremendous usefulness rather comes from the fact
that a wide variety of functions, even seemingly difficult ones with
(integrable) singularities, can be integrated with _arbitrary_ precision.

Install with

```
pip install tanh-sinh
```

and use it like

```python
import tanh_sinh
import numpy as np

val, error_estimate = tanh_sinh.integrate(
    lambda x: np.exp(x) * np.cos(x),
    0,
    np.pi / 2,
    1.0e-14,
    # Optional: Specify first and second derivative for better error estimation
    # f_derivatives={
    #     1: lambda x: np.exp(x) * (np.cos(x) - np.sin(x)),
    #     2: lambda x: -2 * np.exp(x) * np.sin(x),
    # },
)
```

If you want more digits, use [mpmath](http://mpmath.org/) for arbitrary precision
arithmetic:

```python
import tanh_sinh
from mpmath import mp
import sympy

mp.dps = 50

val, error_estimate = tanh_sinh.integrate(
    lambda x: mp.exp(x) * sympy.cos(x),
    0,
    mp.pi / 2,
    1.0e-50,  # !
    mode="mpmath",
)
```

If the function has a singularity at a boundary, it needs to be shifted such that the
singularity is at 0. (This is to avoid round-off errors for points that are very close
to the singularity.)
If there are singularities at both ends, the function can be shifted both ways and be
handed off to `integrate_lr`; For example, for the function `1 / sqrt(1 - x**2)`, this
gives

```python
import numpy
import tanh_sinh

# def f(x):
#    return 1 / numpy.sqrt(1 - x ** 2)

val, error_estimate = tanh_sinh.integrate_lr(
    lambda x: 1 / numpy.sqrt(-(x**2) + 2 * x),  # = 1 / sqrt(1 - (x-1)**2)
    lambda x: 1 / numpy.sqrt(-(x**2) + 2 * x),  # = 1 / sqrt(1 - (-(x-1))**2)
    2,  # length of the interval
    1.0e-10,
)
print(numpy.pi)
print(val)
```

<!--pytest-codeblocks:expected-output--->

```
3.141592653589793
3.1415926533203944
```

### Relevant publications

- [Hidetosi Takahasi, Masatake Mori, Double Exponential Formulas for Numerical Integration, PM. RIMS, Kyoto Univ., 9 (1974), 721-741](https://doi.org/10.2977%2Fprims%2F1195192451)
- [Masatake Mori, Discovery of the double exponential transformation and its developments, Publications of the Research Institute for Mathematical Sciences, 41 (4): 897–935, ISSN 0034-5318](https://doi.org/10.2977/prims/1145474600)
- [David H. Bailey, Karthik Jeyabalan, and Xiaoye S. Li, Error function quadrature, Experiment. Math., Volume 14, Issue 3 (2005), 317-329](https://projecteuclid.org/euclid.em/1128371757)
- [David H. Bailey, Tanh-Sinh High-Precision Quadrature, 2006](https://www.davidhbailey.com/dhbpapers/dhb-tanh-sinh.pdf)

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "tanh-sinh",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "mathematics, physics, engineering, quadrature, integration, numerical integration",
    "author": null,
    "author_email": "Nico Schl\u00f6mer <nico.schloemer@gmail.com>",
    "download_url": null,
    "platform": null,
    "description": "<p align=\"center\">\n  <a href=\"https://github.com/nschloe/tanh_sinh\"><img alt=\"logo\" src=\"https://raw.githubusercontent.com/sigma-py/tanh_sinh/main/logo/logo-tanhsinh.svg\" width=\"60%\"></a>\n</p>\n\n[![PyPi Version](https://img.shields.io/pypi/v/tanh_sinh.svg?style=flat-square)](https://pypi.org/project/tanh_sinh)\n[![PyPI pyversions](https://img.shields.io/pypi/pyversions/tanh_sinh.svg?style=flat-square)](https://pypi.org/pypi/tanh_sinh/)\n[![GitHub stars](https://img.shields.io/github/stars/nschloe/tanh_sinh.svg?style=flat-square&logo=github&label=Stars&logoColor=white)](https://github.com/nschloe/tanh_sinh)\n[![PyPi downloads](https://img.shields.io/pypi/dm/tanh_sinh.svg?style=flat-square)](https://pypistats.org/packages/tanh_sinh)\n\n[![Discord](https://img.shields.io/static/v1?logo=discord&logoColor=white&label=chat&message=on%20discord&color=7289da&style=flat-square)](https://discord.gg/hnTJ5MRX2Y)\n\nThe rather modern tanh-sinh quadrature is different from classical Gaussian\nintegration methods in that it doesn't integrate any function exactly, not even\npolynomials of low degree. Its tremendous usefulness rather comes from the fact\nthat a wide variety of functions, even seemingly difficult ones with\n(integrable) singularities, can be integrated with _arbitrary_ precision.\n\nInstall with\n\n```\npip install tanh-sinh\n```\n\nand use it like\n\n```python\nimport tanh_sinh\nimport numpy as np\n\nval, error_estimate = tanh_sinh.integrate(\n    lambda x: np.exp(x) * np.cos(x),\n    0,\n    np.pi / 2,\n    1.0e-14,\n    # Optional: Specify first and second derivative for better error estimation\n    # f_derivatives={\n    #     1: lambda x: np.exp(x) * (np.cos(x) - np.sin(x)),\n    #     2: lambda x: -2 * np.exp(x) * np.sin(x),\n    # },\n)\n```\n\nIf you want more digits, use [mpmath](http://mpmath.org/) for arbitrary precision\narithmetic:\n\n```python\nimport tanh_sinh\nfrom mpmath import mp\nimport sympy\n\nmp.dps = 50\n\nval, error_estimate = tanh_sinh.integrate(\n    lambda x: mp.exp(x) * sympy.cos(x),\n    0,\n    mp.pi / 2,\n    1.0e-50,  # !\n    mode=\"mpmath\",\n)\n```\n\nIf the function has a singularity at a boundary, it needs to be shifted such that the\nsingularity is at 0. (This is to avoid round-off errors for points that are very close\nto the singularity.)\nIf there are singularities at both ends, the function can be shifted both ways and be\nhanded off to `integrate_lr`; For example, for the function `1 / sqrt(1 - x**2)`, this\ngives\n\n```python\nimport numpy\nimport tanh_sinh\n\n# def f(x):\n#    return 1 / numpy.sqrt(1 - x ** 2)\n\nval, error_estimate = tanh_sinh.integrate_lr(\n    lambda x: 1 / numpy.sqrt(-(x**2) + 2 * x),  # = 1 / sqrt(1 - (x-1)**2)\n    lambda x: 1 / numpy.sqrt(-(x**2) + 2 * x),  # = 1 / sqrt(1 - (-(x-1))**2)\n    2,  # length of the interval\n    1.0e-10,\n)\nprint(numpy.pi)\nprint(val)\n```\n\n<!--pytest-codeblocks:expected-output--->\n\n```\n3.141592653589793\n3.1415926533203944\n```\n\n### Relevant publications\n\n- [Hidetosi Takahasi, Masatake Mori, Double Exponential Formulas for Numerical Integration, PM. RIMS, Kyoto Univ., 9 (1974), 721-741](https://doi.org/10.2977%2Fprims%2F1195192451)\n- [Masatake Mori, Discovery of the double exponential transformation and its developments, Publications of the Research Institute for Mathematical Sciences, 41 (4): 897\u2013935, ISSN 0034-5318](https://doi.org/10.2977/prims/1145474600)\n- [David H. Bailey, Karthik Jeyabalan, and Xiaoye S. Li, Error function quadrature, Experiment. Math., Volume 14, Issue 3 (2005), 317-329](https://projecteuclid.org/euclid.em/1128371757)\n- [David H. Bailey, Tanh-Sinh High-Precision Quadrature, 2006](https://www.davidhbailey.com/dhbpapers/dhb-tanh-sinh.pdf)\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "tanh-sinh quadrature for Python",
    "version": "0.3.8",
    "project_urls": {
        "Homepage": "https://github.com/sigma-py/tanh_sinh",
        "Issues": "https://github.com/sigma-py/tanh_sinh/issues"
    },
    "split_keywords": [
        "mathematics",
        " physics",
        " engineering",
        " quadrature",
        " integration",
        " numerical integration"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "78db22ab5097a70d4b6f8ab03b41acf8f8de0a9076af41847819bba74327e83c",
                "md5": "907e11442331ca211431b8293a5ff9cb",
                "sha256": "cc4000d238b6d817dde20329d18c3352f79b45d5c941a041ecdfce5e2c0ccd04"
            },
            "downloads": -1,
            "filename": "tanh_sinh-0.3.8-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "907e11442331ca211431b8293a5ff9cb",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 13850,
            "upload_time": "2024-07-22T08:11:43",
            "upload_time_iso_8601": "2024-07-22T08:11:43.704308Z",
            "url": "https://files.pythonhosted.org/packages/78/db/22ab5097a70d4b6f8ab03b41acf8f8de0a9076af41847819bba74327e83c/tanh_sinh-0.3.8-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-07-22 08:11:43",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "sigma-py",
    "github_project": "tanh_sinh",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "tanh-sinh"
}
        
Elapsed time: 4.22239s