Skip to main content

logo

PyPi Version PyPI pyversions GitHub stars PyPi downloads

Discord

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

Metadata

Release files for tanh-sinh 0.3.22

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for tanh-sinh 0.3.22
File Actions
tanh_sinh-0.3.22-cp315-none-any.whl CPython 3.15 none any Details for tanh_sinh-0.3.22-cp315-none-any.whl
tanh_sinh-0.3.22-cp314-none-any.whl CPython 3.14 none any Details for tanh_sinh-0.3.22-cp314-none-any.whl
tanh_sinh-0.3.22-cp313-none-any.whl CPython 3.13 none any Details for tanh_sinh-0.3.22-cp313-none-any.whl
tanh_sinh-0.3.22-cp312-none-any.whl CPython 3.12 none any Details for tanh_sinh-0.3.22-cp312-none-any.whl
tanh_sinh-0.3.22-cp311-none-any.whl CPython 3.11 none any Details for tanh_sinh-0.3.22-cp311-none-any.whl
tanh_sinh-0.3.22-cp310-none-any.whl CPython 3.10 none any Details for tanh_sinh-0.3.22-cp310-none-any.whl

Total release size: 201.3 kB

Release files / tanh_sinh-0.3.22-cp315-none-any.whl

Download URL tanh_sinh-0.3.22-cp315-none-any.whl
Size 37.7 kB
Tags CPython 3.15
SHA-256 checksum
How to use checksums
972a8beaae7d065743aea42d66def31ff6d19a6e53990955204331824a152a99
BLAKE2b-256 checksum
How to use checksums
c1756f8fad26b1bd2580cbff235d5e417f5e5b780b0cf8fb0ad70231ce41b7d0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 9, 2026.

Transparency log

Release files / tanh_sinh-0.3.22-cp314-none-any.whl

Download URL tanh_sinh-0.3.22-cp314-none-any.whl
Size 37.4 kB
Tags CPython 3.14
SHA-256 checksum
How to use checksums
f304e0b7a9115167160c8eeac227ad78e12684a09c0693ba2cee53784d51c54d
BLAKE2b-256 checksum
How to use checksums
887e40dc1cf1e1820152bcdf77fe9d23cd469109bee9a1cb5d2cea4cb857888c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 9, 2026.

Transparency log

Release files / tanh_sinh-0.3.22-cp313-none-any.whl

Download URL tanh_sinh-0.3.22-cp313-none-any.whl
Size 33.6 kB
Tags CPython 3.13
SHA-256 checksum
How to use checksums
813b439b4e5d65311fd7137204b5d11f68e9afbb96f6a164254f19aeef52ab5f
BLAKE2b-256 checksum
How to use checksums
78537b80243c6b65689d482dcf431f25ecff19563d1dae8ff47832bbe252b298
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 9, 2026.

Transparency log

Release files / tanh_sinh-0.3.22-cp312-none-any.whl

Download URL tanh_sinh-0.3.22-cp312-none-any.whl
Size 33.3 kB
Tags CPython 3.12
SHA-256 checksum
How to use checksums
c42ee0701fc870f9d509f41c380b2191905f86a92cee5fad9dd5e89b15104e13
BLAKE2b-256 checksum
How to use checksums
644f453b13d0c515f79f0a1ef962b99b3b4c6da536199d6c3bc8b4eec5813644
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 9, 2026.

Transparency log

Release files / tanh_sinh-0.3.22-cp311-none-any.whl

Download URL tanh_sinh-0.3.22-cp311-none-any.whl
Size 36.7 kB
Tags CPython 3.11
SHA-256 checksum
How to use checksums
9adbd62f641b268933d6b147d5991329250145cd5b558dda8632a80a3953b832
BLAKE2b-256 checksum
How to use checksums
ed55af9afb7e74b4a07e8f367ae1e9af0c88351c014c55352541bec9291ac0e0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 9, 2026.

Transparency log

Release files / tanh_sinh-0.3.22-cp310-none-any.whl

Download URL tanh_sinh-0.3.22-cp310-none-any.whl
Size 22.6 kB
Tags CPython 3.10
SHA-256 checksum
How to use checksums
c097fcd91c122f0e40b20fa1d67ca7ecc8862acf227d1af59c7293c86bb10cb1
BLAKE2b-256 checksum
How to use checksums
d0b2d16acb2be35a5fa2bd16ffb636f6b295d0b813b0167daa4d5f34be44dad1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 9, 2026.

Transparency log

Security for tanh-sinh 0.3.22

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.

Full provenance

All 6 files in this release have been uploaded with provenance metadata.

Select from 6 files

Attestation for tanh_sinh-0.3.22-cp315-none-any.whl

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 Nov 9, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page