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.21a2-cp314-none-any.whl (22.4 kB view details)

Uploaded CPython 3.14

tanh_sinh-0.3.21a2-cp313-none-any.whl (21.1 kB view details)

Uploaded CPython 3.13

tanh_sinh-0.3.21a2-cp312-none-any.whl (21.0 kB view details)

Uploaded CPython 3.12

tanh_sinh-0.3.21a2-cp311-none-any.whl (23.4 kB view details)

Uploaded CPython 3.11

tanh_sinh-0.3.21a2-cp310-none-any.whl (14.4 kB view details)

Uploaded CPython 3.10

File details

Details for the file tanh_sinh-0.3.21a2-cp314-none-any.whl.

File metadata

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

File hashes

Hashes for tanh_sinh-0.3.21a2-cp314-none-any.whl
Algorithm Hash digest
SHA256 16c2f87ea1e3cc1051e8f0cf495b2048acfb0de559023c738dc14c3edebbba61
MD5 198a4be1d3a780e4f8188b1b67fae286
BLAKE2b-256 3ddd4458a51012d8b0fe21866cf98913f7add39e003ec9d6322bf0c9196f5124

See more details on using hashes here.

Provenance

The following attestation bundles were made for tanh_sinh-0.3.21a2-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.21a2-cp313-none-any.whl.

File metadata

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

File hashes

Hashes for tanh_sinh-0.3.21a2-cp313-none-any.whl
Algorithm Hash digest
SHA256 27bdbd8ef22cdc72a2a1271effcc04c9e291639203d1e45265f29f17572b2475
MD5 ca9b37bd7340dc7edb95434ef946beca
BLAKE2b-256 f432393023be73f142b7faa8f036ae098148660ea69dd9b85b70f0c44d5f608d

See more details on using hashes here.

Provenance

The following attestation bundles were made for tanh_sinh-0.3.21a2-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.21a2-cp312-none-any.whl.

File metadata

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

File hashes

Hashes for tanh_sinh-0.3.21a2-cp312-none-any.whl
Algorithm Hash digest
SHA256 1e2e5365bda6a56a77c3393017dd09711a5930b73427d7c5662058b85c5d73f1
MD5 ec28667daa43a1e46fd11da9c7f2b1b6
BLAKE2b-256 839ee404f091704301c18c509fdb2a26c351fbe705056b4feab8b411fb657f76

See more details on using hashes here.

Provenance

The following attestation bundles were made for tanh_sinh-0.3.21a2-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.21a2-cp311-none-any.whl.

File metadata

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

File hashes

Hashes for tanh_sinh-0.3.21a2-cp311-none-any.whl
Algorithm Hash digest
SHA256 b040cb69b504a8e307939120b7b5271b4d36ef1b13c33b8be55c6a91bc5fb222
MD5 cbd06fb4a3138d7111df0611281ffbdf
BLAKE2b-256 81f055ccc004172b3e6201bbb60889607cc589fa64f2b922a2a2ac500caeb4f6

See more details on using hashes here.

Provenance

The following attestation bundles were made for tanh_sinh-0.3.21a2-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.21a2-cp310-none-any.whl.

File metadata

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

File hashes

Hashes for tanh_sinh-0.3.21a2-cp310-none-any.whl
Algorithm Hash digest
SHA256 8fd391c3060899cd41d533b3a7d67c0352933926be630e1807f58f09dc2405c2
MD5 d4131a441c362f6f1b0f3331bbb07e76
BLAKE2b-256 391bcf61ffe3c675c1f6c1275a9f78f4310dc3fd2215c8ce776b3410a95bf3e8

See more details on using hashes here.

Provenance

The following attestation bundles were made for tanh_sinh-0.3.21a2-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