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
import tanh_sinh
import numpy as np
val, error_estimate = tanh_sinh.integrate(
lambda x: np.exp(x) * np.cos(x),
# Optional: Specify the function with its first and second derivative for
# better error estimation
# (
# lambda x: np.exp(x) * np.cos(x),
# lambda x: np.exp(x) * (np.cos(x) - np.sin(x)),
# lambda x: -2 * np.exp(x) * np.sin(x),
# )
0,
np.pi / 2,
1.0e-14,
)
If you want more digits, use mpmath for arbitrary precision arithmetic:
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
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)
3.141592653589793
3.1415926533203944
Relevant publications
- Hidetosi Takahasi, Masatake Mori, Double Exponential Formulas for Numerical Integration, PM. RIMS, Kyoto Univ., 9 (1974), 721-741
- 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
- David H. Bailey, Karthik Jeyabalan, and Xiaoye S. Li, Error function quadrature, Experiment. Math., Volume 14, Issue 3 (2005), 317-329
- David H. Bailey, Tanh-Sinh High-Precision Quadrature, 2006
Release files for tanh-sinh 0.3.21
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distributions (wheels)
| File | Reset | |||
|---|---|---|---|---|
| tanh_sinh-0.3.21-cp314-none-any.whl | CPython 3.14 | none | any | Details |
| tanh_sinh-0.3.21-cp313-none-any.whl | CPython 3.13 | none | any | Details |
| tanh_sinh-0.3.21-cp312-none-any.whl | CPython 3.12 | none | any | Details |
| tanh_sinh-0.3.21-cp311-none-any.whl | CPython 3.11 | none | any | Details |
| tanh_sinh-0.3.21-cp310-none-any.whl | CPython 3.10 | none | any | Details |
Total release size: 102.1 kB
Release files / tanh_sinh-0.3.21-cp314-none-any.whl
| Download URL | tanh_sinh-0.3.21-cp314-none-any.whl |
|---|---|
| Size | 22.4 kB |
| Tags | CPython 3.14 |
|
SHA-256 checksum How to use checksums |
bdd0fdb9573735bed20737fee37042c97cc89d2589727cce3bc8fd8ce301df5c
|
|
BLAKE2b-256 checksum How to use checksums |
2b6cbdae048c33f5e9e3862f63c4aa4834cf92d25d11a1378dab0a721bab1e0e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 18, 2026.
Transparency logRelease files / tanh_sinh-0.3.21-cp313-none-any.whl
| Download URL | tanh_sinh-0.3.21-cp313-none-any.whl |
|---|---|
| Size | 21.0 kB |
| Tags | CPython 3.13 |
|
SHA-256 checksum How to use checksums |
79b9b0608b047e37e8645848f63c1c270429aa1167b6b2429d4c980150b513fb
|
|
BLAKE2b-256 checksum How to use checksums |
6c7c68fd025caf397cb95121fa350de6e7e89ba155b0cb9b36b0ce0dd4cd37e7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 18, 2026.
Transparency logRelease files / tanh_sinh-0.3.21-cp312-none-any.whl
| Download URL | tanh_sinh-0.3.21-cp312-none-any.whl |
|---|---|
| Size | 20.9 kB |
| Tags | CPython 3.12 |
|
SHA-256 checksum How to use checksums |
2fd853410bad93e86113126b9d8c938aa7588dc0f2abd5cff32f02e94cdcaea9
|
|
BLAKE2b-256 checksum How to use checksums |
52b8ae225f6a4f13a51d076ba5c52356794df605656353706899c6705897ff09
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 18, 2026.
Transparency logRelease files / tanh_sinh-0.3.21-cp311-none-any.whl
| Download URL | tanh_sinh-0.3.21-cp311-none-any.whl |
|---|---|
| Size | 23.4 kB |
| Tags | CPython 3.11 |
|
SHA-256 checksum How to use checksums |
8261e6552cf8cc0258118f7d3289d5e5ffd598e55224003700baf70c6ccf3f3a
|
|
BLAKE2b-256 checksum How to use checksums |
da7867f74b693a0e702a38e765c4df035fd1005bb5699d150ad21256fd835039
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 18, 2026.
Transparency logRelease files / tanh_sinh-0.3.21-cp310-none-any.whl
| Download URL | tanh_sinh-0.3.21-cp310-none-any.whl |
|---|---|
| Size | 14.3 kB |
| Tags | CPython 3.10 |
|
SHA-256 checksum How to use checksums |
55aa7f41516d4dafa0a6cc89d7716b4f153e477fa2a92abbcec24934f4b1031d
|
|
BLAKE2b-256 checksum How to use checksums |
640e655a018ca92c78f3bcb9273984225d9677bca6f03f2d31c9bdbe52fb1209
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 18, 2026.
Transparency log