Skip to content

HMath.Calculus

Module index · Numerical conventions

Differentiation and integration guide

Description

Checked, finite-difference differentiation and numerical quadrature for Double callbacks. User callbacks must be total and side-effect free. Budgets bound refinement; error estimates are numerical estimates, not rigorous bounds.

On this page

API Purpose
Estimate Estimated value, absolute error estimate and callback evaluation count.
Estimate Declaration and type contract below.
estimateValue Returned numerical derivative/integral estimate.
estimateError Absolute error estimate from the selected approximation/refinement rule; not a universal rigorous bound.
evaluations Number of function evaluations used by the calculation.
Tolerance Positive absolute tolerance, nonnegative relative tolerance and positive budget.
Tolerance Declaration and type contract below.
absoluteTolerance Requested finite nonnegative absolute error threshold.
relativeTolerance Requested finite nonnegative error threshold relative to estimate magnitude.
maximumEvaluations Positive caller-supplied function-evaluation budget.
defaultTolerance Default adaptive tolerance: 1e-10 absolute, 1e-8 relative, 100000 evaluations.
Difference One-sided or central first differences.
Forward Declaration and type contract below.
Backward Declaration and type contract below.
Central Declaration and type contract below.
derivative First derivative with explicit positive finite step; rounded-away steps fail.
derivativeN Arbitrary nonnegative derivative order by recursively centered differences.
complexDerivative Complex-step first derivative.
richardson Richardson extrapolation of a central first difference.
ridders Ridders extrapolation with factor 1.4 and a best-error stopping criterion.
limit One-sided numerical limit, approaching geometrically from the requested side.
partialDerivative Partial derivative at a zero-based coordinate.
gradient Gradient using one central difference per coordinate.
jacobian Row-major Jacobian; callback output dimensions must remain constant.
hessian Hessian with centered diagonal and mixed differences.
directionalDerivative Directional derivative; the direction is used as supplied, without normalization.
jvp Jacobian-vector product by directional central differences.
vjp Vector-Jacobian product as the gradient of a scalar contraction.
hvp Hessian-vector product with four directional/coordinate samples per component.
divergence Divergence of a vector field with equal input and output dimensions.
curl Curl of a three-dimensional vector field.
laplacian Laplacian as the sum of centered second coordinate differences.
Quadrature Composite Newton-Cotes formulas.
Trapezoidal Declaration and type contract below.
Simpson Declaration and type contract below.
ThreeEighths Declaration and type contract below.
integrateComposite Composite finite-interval integration with exact panel constraints.
romberg Romberg integration using trapezoidal refinement and Richardson diagonals.
gaussLegendre n-point Gauss-Legendre quadrature; nodes are refined by Newton iteration.
gaussHermite Weighted Gaussian quadrature on the real line for weight exp(-x^2).
gaussLaguerre Weighted Gaussian quadrature on [0,infinity) for weight exp(-x).
gaussChebyshev Weighted first-kind Chebyshev rule on [-1,1], weight 1/sqrt(1-x^2).
gaussKronrod Embedded 7-point Gauss / 15-point Kronrod estimate.
adaptiveIntegrate Adaptive Gauss-Kronrod with global summed error and a strict evaluation budget.
clenshawCurtis Clenshaw-Curtis quadrature on n cosine-spaced panels using exact moments of the cosine interpolant. n must be positive and at most 4096.
tanhSinh Tanh-sinh quadrature with geometric step refinement on a finite interval.
integrateInfinite Integrate on finite, half-infinite or doubly-infinite intervals by an open coordinate transform followed by adaptive Gauss-Kronrod.
integrateOscillatory Integrate f(x)cos(omegax) or f(x)sin(omegax), splitting at least once per half-period to avoid accepting an aliased embedded quadrature estimate.
integrateND Tensor-product midpoint integration over a box.
monteCarlo Reproducible Monte Carlo box integral with an explicit 64-bit seed.
quasiMonteCarlo Deterministic Halton-box quadrature, up to 16 dimensions.
cumulativeIntegral Cumulative trapezoidal integrals at strictly increasing sample coordinates.
pathIntegral Scalar line integral f(r(t))*|r'(t)| along an explicitly parameterized path.
lineIntegral Oriented vector-field line integral along a parameterized path.
surfaceIntegral Scalar surface integral over a rectangular parameter domain in R3.
volumeIntegral Volume integral over a three-dimensional rectangular box.

Documentation

Estimate

data Estimate a Source #

Estimated value, absolute error estimate and callback evaluation count.

Constructors

Estimate 

Fields

  • estimateValue :: a

    Returned numerical derivative/integral estimate.

  • estimateError :: Double

    Absolute error estimate from the selected approximation/refinement rule; not a universal rigorous bound.

  • evaluations :: Int

    Number of function evaluations used by the calculation.

Instances

Instances details
Show a => Show (Estimate a) Source # 
Instance details

Defined in HMath.Calculus

Methods

showsPrec :: Int -> Estimate a -> ShowS #

show :: Estimate a -> String #

showList :: [Estimate a] -> ShowS #

Eq a => Eq (Estimate a) Source # 
Instance details

Defined in HMath.Calculus

Methods

(==) :: Estimate a -> Estimate a -> Bool #

(/=) :: Estimate a -> Estimate a -> Bool #

Tolerance

data Tolerance Source #

Positive absolute tolerance, nonnegative relative tolerance and positive budget.

Constructors

Tolerance 

Fields

Instances

Instances details
Show Tolerance Source # 
Instance details

Defined in HMath.Calculus

Eq Tolerance Source # 
Instance details

Defined in HMath.Calculus

defaultTolerance

defaultTolerance :: Tolerance Source #

Default adaptive tolerance: 1e-10 absolute, 1e-8 relative, 100000 evaluations.

Difference

data Difference Source #

One-sided or central first differences.

Constructors

Forward 
Backward 
Central 

Instances

Instances details
Show Difference Source # 
Instance details

Defined in HMath.Calculus

Eq Difference Source # 
Instance details

Defined in HMath.Calculus

derivative

derivative :: Difference -> Double -> (Double -> Double) -> Double -> Either MathError Double Source #

First derivative with explicit positive finite step; rounded-away steps fail.

derivativeN

derivativeN :: Int -> Double -> (Double -> Double) -> Double -> Either MathError Double Source #

Arbitrary nonnegative derivative order by recursively centered differences. Work grows exponentially with the requested order; orders are limited to 12.

complexDerivative

complexDerivative :: Double -> (Complex Double -> Complex Double) -> Double -> Either MathError Double Source #

Complex-step first derivative. The callback must have a holomorphic extension and be real on the real axis.

richardson

richardson :: Tolerance -> Double -> (Double -> Double) -> Double -> Either MathError (Estimate Double) Source #

Richardson extrapolation of a central first difference. Each row halves the step. Evaluation budget is 2 per row; convergence uses consecutive diagonals.

ridders

ridders :: Tolerance -> Double -> (Double -> Double) -> Double -> Either MathError (Estimate Double) Source #

Ridders extrapolation with factor 1.4 and a best-error stopping criterion.

limit

limit :: Difference -> Tolerance -> Double -> (Double -> Double) -> Double -> Either MathError (Estimate Double) Source #

One-sided numerical limit, approaching geometrically from the requested side. Central compares the average of both sides; it cannot prove continuity.

partialDerivative

partialDerivative :: Int -> Double -> ([Double] -> Double) -> [Double] -> Either MathError Double Source #

Partial derivative at a zero-based coordinate.

gradient

gradient :: Double -> ([Double] -> Double) -> [Double] -> Either MathError [Double] Source #

Gradient using one central difference per coordinate.

jacobian

jacobian :: Double -> ([Double] -> [Double]) -> [Double] -> Either MathError (Matrix Double) Source #

Row-major Jacobian; callback output dimensions must remain constant.

hessian

hessian :: Double -> ([Double] -> Double) -> [Double] -> Either MathError (Matrix Double) Source #

Hessian with centered diagonal and mixed differences.

directionalDerivative

directionalDerivative :: Double -> ([Double] -> Double) -> [Double] -> [Double] -> Either MathError Double Source #

Directional derivative; the direction is used as supplied, without normalization.

jvp

jvp :: Double -> ([Double] -> [Double]) -> [Double] -> [Double] -> Either MathError [Double] Source #

Jacobian-vector product by directional central differences.

vjp

vjp :: Double -> ([Double] -> [Double]) -> [Double] -> [Double] -> Either MathError [Double] Source #

Vector-Jacobian product as the gradient of a scalar contraction.

hvp

hvp :: Double -> ([Double] -> Double) -> [Double] -> [Double] -> Either MathError [Double] Source #

Hessian-vector product with four directional/coordinate samples per component.

divergence

divergence :: Double -> ([Double] -> [Double]) -> [Double] -> Either MathError Double Source #

Divergence of a vector field with equal input and output dimensions.

curl

curl :: Double -> ([Double] -> [Double]) -> [Double] -> Either MathError [Double] Source #

Curl of a three-dimensional vector field.

laplacian

laplacian :: Double -> ([Double] -> Double) -> [Double] -> Either MathError Double Source #

Laplacian as the sum of centered second coordinate differences.

Quadrature

data Quadrature Source #

Composite Newton-Cotes formulas. Simpson panels must be a multiple of 2 and ThreeEighths panels a multiple of 3.

Instances

Instances details
Show Quadrature Source # 
Instance details

Defined in HMath.Calculus

Eq Quadrature Source # 
Instance details

Defined in HMath.Calculus

integrateComposite

integrateComposite :: Quadrature -> Int -> (Double -> Double) -> Double -> Double -> Either MathError Double Source #

Composite finite-interval integration with exact panel constraints.

romberg

romberg :: Tolerance -> (Double -> Double) -> Double -> Double -> Either MathError (Estimate Double) Source #

Romberg integration using trapezoidal refinement and Richardson diagonals.

gaussLegendre

gaussLegendre :: Int -> (Double -> Double) -> Double -> Double -> Either MathError Double Source #

n-point Gauss-Legendre quadrature; nodes are refined by Newton iteration.

gaussHermite

gaussHermite :: Int -> (Double -> Double) -> Either MathError Double Source #

Weighted Gaussian quadrature on the real line for weight exp(-x^2). Uses the symmetric Jacobi recurrence and a checked implicit QL eigensystem.

gaussLaguerre

gaussLaguerre :: Int -> (Double -> Double) -> Either MathError Double Source #

Weighted Gaussian quadrature on [0,infinity) for weight exp(-x).

gaussChebyshev

gaussChebyshev :: Int -> (Double -> Double) -> Either MathError Double Source #

Weighted first-kind Chebyshev rule on [-1,1], weight 1/sqrt(1-x^2).

gaussKronrod

gaussKronrod :: (Double -> Double) -> Double -> Double -> Either MathError (Estimate Double) Source #

Embedded 7-point Gauss / 15-point Kronrod estimate. Nodes and weights are the standard Gauss-Kronrod pair used by QUADPACK dqk15 (Netlib).

adaptiveIntegrate

adaptiveIntegrate :: Tolerance -> (Double -> Double) -> Double -> Double -> Either MathError (Estimate Double) Source #

Adaptive Gauss-Kronrod with global summed error and a strict evaluation budget. The interval with the largest estimated error is bisected first.

clenshawCurtis

clenshawCurtis :: Int -> (Double -> Double) -> Double -> Double -> Either MathError Double Source #

Clenshaw-Curtis quadrature on n cosine-spaced panels using exact moments of the cosine interpolant. n must be positive and at most 4096.

tanhSinh

tanhSinh :: Tolerance -> (Double -> Double) -> Double -> Double -> Either MathError (Estimate Double) Source #

Tanh-sinh quadrature with geometric step refinement on a finite interval. Rounded-to-endpoint nodes are omitted, supporting integrable endpoint poles.

integrateInfinite

integrateInfinite :: Tolerance -> (Double -> Double) -> Double -> Double -> Either MathError (Estimate Double) Source #

Integrate on finite, half-infinite or doubly-infinite intervals by an open coordinate transform followed by adaptive Gauss-Kronrod. Reversed bounds change the sign. Divergent integrals have no guaranteed detection criterion.

integrateOscillatory

integrateOscillatory :: Bool -> Double -> Tolerance -> (Double -> Double) -> Double -> Double -> Either MathError (Estimate Double) Source #

Integrate f(x)*cos(omega*x) or f(x)*sin(omega*x), splitting at least once per half-period to avoid accepting an aliased embedded quadrature estimate.

integrateND

integrateND :: Int -> Int -> ([Double] -> Double) -> [(Double, Double)] -> Either MathError Double Source #

Tensor-product midpoint integration over a box. The product of panel counts is checked against the explicit budget before callback evaluation.

monteCarlo

monteCarlo :: Integer -> Int -> ([Double] -> Double) -> [(Double, Double)] -> Either MathError (Estimate Double) Source #

Reproducible Monte Carlo box integral with an explicit 64-bit seed. Error is one estimated standard error from Welford's sample variance.

quasiMonteCarlo

quasiMonteCarlo :: Int -> ([Double] -> Double) -> [(Double, Double)] -> Either MathError (Estimate Double) Source #

Deterministic Halton-box quadrature, up to 16 dimensions. No probabilistic error bound is claimed; estimateError is the difference of the two halves.

cumulativeIntegral

cumulativeIntegral :: [(Double, Double)] -> Either MathError [Double] Source #

Cumulative trapezoidal integrals at strictly increasing sample coordinates.

pathIntegral

pathIntegral :: Tolerance -> (Double -> [Double]) -> (Double -> [Double]) -> ([Double] -> Double) -> Double -> Double -> Either MathError (Estimate Double) Source #

Scalar line integral f(r(t))*|r'(t)| along an explicitly parameterized path.

lineIntegral

lineIntegral :: Tolerance -> (Double -> [Double]) -> (Double -> [Double]) -> ([Double] -> [Double]) -> Double -> Double -> Either MathError (Estimate Double) Source #

Oriented vector-field line integral along a parameterized path.

surfaceIntegral

surfaceIntegral :: Int -> Int -> ([Double] -> [Double]) -> ([Double] -> [Double]) -> ([Double] -> [Double]) -> ([Double] -> Double) -> [(Double, Double)] -> Either MathError Double Source #

Scalar surface integral over a rectangular parameter domain in R3.

volumeIntegral

volumeIntegral :: Int -> Int -> ([Double] -> Double) -> [(Double, Double)] -> Either MathError Double Source #

Volume integral over a three-dimensional rectangular box.