Skip to main content

tanh-sinh quadrature for Python

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

tanh_sinh-0.3.20-cp314-none-any.whl (21.8 kB view details)

Uploaded CPython 3.14

tanh_sinh-0.3.20-cp313-none-any.whl (20.6 kB view details)

Uploaded CPython 3.13

tanh_sinh-0.3.20-cp312-none-any.whl (20.6 kB view details)

Uploaded CPython 3.12

tanh_sinh-0.3.20-cp311-none-any.whl (23.0 kB view details)

Uploaded CPython 3.11

tanh_sinh-0.3.20-cp310-none-any.whl (13.9 kB view details)

Uploaded CPython 3.10

File details

Details for the file tanh_sinh-0.3.20-cp314-none-any.whl.

File metadata

  • Download URL: tanh_sinh-0.3.20-cp314-none-any.whl
  • Upload date:
  • Size: 21.8 kB
  • Tags: CPython 3.14
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tanh_sinh-0.3.20-cp314-none-any.whl
Algorithm Hash digest
SHA256 8825fd7d8283e4be7adda8aa9a835f983f8c58fb097d9052f03f889d9026324d
MD5 fcc55777aa261defa299b6c4d048cb6a
BLAKE2b-256 fb0484fdd0a3a9a97b01fd5c5c4b9c73df340f2ffb975b5384d82dc743ed03de

See more details on using hashes here.

Provenance

The following attestation bundles were made for tanh_sinh-0.3.20-cp314-none-any.whl:

Publisher: release.yml on sigma-py/tanh-sinh-dev

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tanh_sinh-0.3.20-cp313-none-any.whl.

File metadata

  • Download URL: tanh_sinh-0.3.20-cp313-none-any.whl
  • Upload date:
  • Size: 20.6 kB
  • Tags: CPython 3.13
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tanh_sinh-0.3.20-cp313-none-any.whl
Algorithm Hash digest
SHA256 4724ba7c2c02154e94c6327cd9bb5395bfa5757c8751c3a2cfab0cccd8eb5016
MD5 983a599ea7a4419018d8e430d80da527
BLAKE2b-256 f1cb6713f487db575a1b9ba1cfbc5e8bfa644b0a0b001dde53c7795b3b326a0e

See more details on using hashes here.

Provenance

The following attestation bundles were made for tanh_sinh-0.3.20-cp313-none-any.whl:

Publisher: release.yml on sigma-py/tanh-sinh-dev

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tanh_sinh-0.3.20-cp312-none-any.whl.

File metadata

  • Download URL: tanh_sinh-0.3.20-cp312-none-any.whl
  • Upload date:
  • Size: 20.6 kB
  • Tags: CPython 3.12
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tanh_sinh-0.3.20-cp312-none-any.whl
Algorithm Hash digest
SHA256 bc5c6414c8b5e665b5a48fe93872bee35e713ddd006924c13a160be1f1c9db50
MD5 89b4750dc3b00a8ee213f0bc5cbc7629
BLAKE2b-256 ec16110fb7f511a09bf4af78c3bb6b78e0780b848c221438097e60a384526036

See more details on using hashes here.

Provenance

The following attestation bundles were made for tanh_sinh-0.3.20-cp312-none-any.whl:

Publisher: release.yml on sigma-py/tanh-sinh-dev

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tanh_sinh-0.3.20-cp311-none-any.whl.

File metadata

  • Download URL: tanh_sinh-0.3.20-cp311-none-any.whl
  • Upload date:
  • Size: 23.0 kB
  • Tags: CPython 3.11
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tanh_sinh-0.3.20-cp311-none-any.whl
Algorithm Hash digest
SHA256 4f0d09f713f580ad0073628be6a73c8485fd6fbc20fbc78d7e96b34c8b6c7613
MD5 3542d83c96c839f82954690a11781d63
BLAKE2b-256 9f7a4b4bf62b8014b8efbaccb805a6957273cfe3ad56e94d4fed088c33914058

See more details on using hashes here.

Provenance

The following attestation bundles were made for tanh_sinh-0.3.20-cp311-none-any.whl:

Publisher: release.yml on sigma-py/tanh-sinh-dev

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tanh_sinh-0.3.20-cp310-none-any.whl.

File metadata

  • Download URL: tanh_sinh-0.3.20-cp310-none-any.whl
  • Upload date:
  • Size: 13.9 kB
  • Tags: CPython 3.10
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tanh_sinh-0.3.20-cp310-none-any.whl
Algorithm Hash digest
SHA256 96075681c93b80e9faf7502779fed42baa30bdabccac826dd069eb9310e6d1d4
MD5 35f3c953aba90cd22dfd13279437f253
BLAKE2b-256 aaeda563b5bb43e969746fba66fb9dff1280520ab8b5bec180e8962cbe951bab

See more details on using hashes here.

Provenance

The following attestation bundles were made for tanh_sinh-0.3.20-cp310-none-any.whl:

Publisher: release.yml on sigma-py/tanh-sinh-dev

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page