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 Distribution

tanh_sinh-0.3.16-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file tanh_sinh-0.3.16-py3-none-any.whl.

File metadata

  • Download URL: tanh_sinh-0.3.16-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for tanh_sinh-0.3.16-py3-none-any.whl
Algorithm Hash digest
SHA256 7e522ee0f443aecb6c3716438112e1d608376a3ab8e8252ec31b73659c54bffa
MD5 0a1958a9bd2e879ad14b5508a50e129d
BLAKE2b-256 fc086f67a081847e9bcf9bd7450ae1af6094c08d58c1ad70b7c225c9187617e3

See more details on using hashes here.

Provenance

The following attestation bundles were made for tanh_sinh-0.3.16-py3-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 Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page