HCubature.jl

repository·master·Indexed 41 days ago

https://github.com/juliamath/hcubature.jl

A pure-Julia implementation of multidimensional h-adaptive integration. It provides the `hcubature` function for n-dimensional integrals over a hypercube and `hquadrature` as a convenience wrapper for 1D integration, adaptively subdividing the integration volume to meet specified relative (`rtol`) and absolute (`atol`) error tolerances.

Tokens
684
Snippets
3
Records
3
Agent score
1%

What's inside HCubature.jl

  1. Use `hcubature` for multidimensional integration

    master

    The hcubature function computes the n-dimensional integral of a function f(x) over a hypercube defined by the corners a and b. It uses an h-adaptive algorithm that subdivides the integration volume until the estimated error meets the requested tolerances.

    Key Requirements:

    • f must be a function that accepts an n-dimensional vector x and returns an integrand. The integrand can be a real number, complex number, vector, or matrix (any type supporting +, -, * real, and norm).
    • a and b must be vectors or tuples of length n representing the integration limits for each dimension.
    • For performance with vector/matrix-valued integrands, it is recommended to use StaticArrays.jl.
    • The integrand f(x) will be passed an SVector{n,T} from the StaticArrays package.

    Return Value: A tuple (I, E) where I is the estimated integral and E is the estimated error.

    using HCubature
    # Example: 2D integral
    f(x) = x[1] * x[2]
    a = [0, 0]
    b = [1, 1]
    I, E = hcubature(f, a, b)
  2. Use `hquadrature` for 1D integration

    master

    The hquadrature function is a convenience wrapper around hcubature designed for 1-dimensional integrals. It allows you to pass scalar values for x, a, and b instead of 1-component vectors.

    Signature: hquadrature(f, a, b; norm=norm, rtol=sqrt(eps), atol=0, maxevals=typemax(Int), initdiv=1)

    Return Value: A tuple (I, E) of the estimated integral and estimated error.

    Note: For 1D integrals requiring more flexibility (like choosing quadrature order), consider using QuadGK.jl instead.

    using HCubature
    # Example: 1D integral
    f(x) = x^2
    I, E = hquadrature(f, 0, 1)
  3. Configure `hcubature` tolerances and limits

    master

    When calling hcubature(f, a, b; ...), you can control the convergence and execution behavior using the following keyword arguments:

    • rtol: Relative tolerance. The algorithm stops when E ≤ max(rtol * norm(I), atol). Default is sqrt(eps(T)).
    • atol: Absolute tolerance. The algorithm stops when E ≤ max(rtol * norm(I), atol). Default is 0.
    • maxevals: Maximum number of function evaluations. Default is typemax(Int).
    • initdiv: Initial number of segments the volume is divided into along each dimension. Default is 1.
    • norm: The function used to calculate the norm for error estimation and convergence testing. Default is norm (useful for vector-valued integrands with different scalings).
    hcubature(f, a, b; norm=norm, rtol=sqrt(eps), atol=0, maxevals=typemax(Int), initdiv=1)