Skip to content

integrate #

vsl.integrate

adaptive_gauss_kronrod estimates finite-interval integrals with an embedded 7/15-point Gauss-Kronrod rule. It repeatedly subdivides the interval with the largest estimated error until the requested absolute/relative tolerance is met, or a configured interval, evaluation, or floating-point precision limit is reached.

import math
import vsl.func
import vsl.integrate

fn objective(x f64, _ []f64) f64 {
    return math.sin(20.0 * x)
}

f := func.Fn.new(f: objective)
result := integrate.adaptive_gauss_kronrod(f, integrate.GaussKronrodParams{
    a:       0.0
    b:       1.0
    abs_tol: 1e-10
    rel_tol: 1e-10
})!
println('${result.value} ± ${result.error_estimate} (${result.status})')

The reported error_estimate is an estimate, not a certified mathematical bound. Non-finite callback values and non-finite integral estimates have distinct statuses.

Improper intervals

adaptive_gauss_kronrod_improper accepts finite bounds and either sign of infinity through math.inf(1) and math.inf(-1). It maps one-sided tails onto [0, 1) with the corresponding Jacobian. A whole-line integral is split at zero and the absolute tolerance and execution budgets are divided between the two tails; therefore whole-line calls require a positive absolute tolerance. Reversed bounds negate the result. The transform and adaptive error estimate do not guarantee convergence for conditionally convergent or highly oscillatory tails.

Breakpoints and stiff ODE solvers are not included.

fn adaptive_gauss_kronrod #

fn adaptive_gauss_kronrod(f func.Fn, params GaussKronrodParams) !GaussKronrodResult

adaptive_gauss_kronrod integrates a function over a finite interval using the embedded 7/15-point Gauss-Kronrod rule and adaptive interval subdivision.

fn adaptive_gauss_kronrod_improper #

fn adaptive_gauss_kronrod_improper(f func.Fn, params ImproperGaussKronrodParams) !GaussKronrodResult

adaptive_gauss_kronrod_improper integrates over finite or infinite bounds. Infinite tails are mapped to [0, 1); a whole-line integral is split at zero.

fn IntegrationStatus.from #

fn IntegrationStatus.from[W](input W) !IntegrationStatus

from returns the IntegrationStatus value of an integer or of a field name, or an error if there is none.

enum IntegrationStatus #

enum IntegrationStatus {
	converged
	max_intervals
	max_evaluations
	non_finite_function_value
	non_finite_estimate
	precision_limit
}

IntegrationStatus reports why adaptive integration stopped.

struct GaussKronrodParams #

@[params]
struct GaussKronrodParams {
pub:
	a             f64
	b             f64
	abs_tol       f64 = 1e-10
	rel_tol       f64 = 1e-8
	max_intervals int = 1000
	max_evals     int = 30000
}

GaussKronrodParams configures finite-interval adaptive integration.

struct GaussKronrodResult #

struct GaussKronrodResult {
pub:
	value          f64
	error_estimate f64
	n_calls        int
	n_intervals    int
	status         IntegrationStatus
}

GaussKronrodResult contains the integral estimate and convergence diagnostics.

struct ImproperGaussKronrodParams #

@[params]
struct ImproperGaussKronrodParams {
pub:
	a             f64
	b             f64
	abs_tol       f64 = 1e-10
	rel_tol       f64 = 1e-8
	max_intervals int = 1000
	max_evals     int = 30000
}

ImproperGaussKronrodParams configures quadrature with finite or infinite bounds. Infinite endpoints must be opposite-signed for a whole-line integral.